├── .github └── workflows │ └── dotnet-desktop.yml ├── .gitignore ├── LICENSE ├── LeetCodeNet.Tests ├── Easy │ ├── Array │ │ ├── AverageSalaryExcludingtheMinimumandMaximumSalary_1491_test.cs │ │ ├── BinarySearch_704_test.cs │ │ ├── CheckIfNandItsDoubleExist_1346_test.cs │ │ ├── ContainsDuplicate_217_test.cs │ │ ├── CountNegativeNumbersinaSortedMatrix_1351_test.cs │ │ ├── DeterminetheWinnerofaBowlingGame_2660_test.cs │ │ ├── DuplicateZeros_1089_test.cs │ │ ├── FindAllNumbersDisappearedinanArray_448_test.cs │ │ ├── FindLuckyIntegerinanArray_1394_test.cs │ │ ├── FindNumberswithEvenNumberofDigits_1295_test.cs │ │ ├── FindPivotIndex_724_test.cs │ │ ├── FindSmallestLetterGreaterThanTarget_744_test.cs │ │ ├── Finding3-DigitEvenNumbers_2094_test.cs │ │ ├── FindtheDistanceValueBetweenTwoArrays_1385_test.cs │ │ ├── FindtheDistinctDifferenceArray_2670_test.cs │ │ ├── FindtheHighestAltitude_1732_test.cs │ │ ├── FirstBadVersion_278_test.cs │ │ ├── FloodFill_733_test.cs │ │ ├── GuessNumberHigherorLower_374_test.cs │ │ ├── HeightChecker_1051_test.cs │ │ ├── IntersectionOfTwoArraysII_350_test.cs │ │ ├── KidsWiththeGreatestNumberofCandies_1431_test.cs │ │ ├── KthMissingPositiveNumber_1539_test.cs │ │ ├── LastStoneWeight_1046_test.cs │ │ ├── MajorityElement_169_test.cs │ │ ├── MatrixDiagonalSum_1572_test.cs │ │ ├── MaxConsecutiveOnes_485_test.cs │ │ ├── MoveZeroes_283_test.cs │ │ ├── NeitherMinimumnorMaximum_2733_test.cs │ │ ├── NextGreaterElement_I_496_test.cs │ │ ├── PlusOne_66_test.cs │ │ ├── RemoveDuplicatesFromSortedArray_26_test.cs │ │ ├── RemoveElement_27_test.cs │ │ ├── ReplaceElementswithGreatestElementonRightSide_1299_test.cs │ │ ├── ReshapeTheMatrix_566_test.cs │ │ ├── RichestCustomerWealth_1672_test.cs │ │ ├── RowWithMaximumOnes_2643_test.cs │ │ ├── RunningSumof1dArray_1480_test.cs │ │ ├── SearchInsertPosition_35_test.cs │ │ ├── Semi-OrderedPermutation_2717_test.cs │ │ ├── SpecialArrayWithXElementsGreaterThanorEqualX_1608_test.cs │ │ ├── SquaresofaSortedArray_977_test.cs │ │ ├── SumofAllOddLengthSubarrays_1588_test.cs │ │ ├── TheKWeakestRowsinaMatrix_1337_test.cs │ │ ├── ThirdMaximumNumber_414_test.cs │ │ ├── TransposeMatrix_867_test.cs │ │ ├── TwoSum_1_test.cs │ │ └── ValidMountainArray_941_test.cs │ ├── BitManipulation │ │ ├── ConvertBinaryNumberinaLinkedListtoInteger_1290_test.cs │ │ ├── HammingDistance_461_test.cs │ │ ├── MissingNumber_268_test.cs │ │ ├── Numberof1Bits_191_test.cs │ │ ├── NumberofStepstoReduceaNumbertoZero_1342_test.cs │ │ ├── PowerofTwo_231_test.cs │ │ ├── ReverseBits_190_test.cs │ │ ├── SignleNumber_136_test.cs │ │ └── SortIntegersbyTheNumberof1Bits_1356_test.cs │ ├── DP │ │ ├── BestTimetoBuyandSellStock_121_test.cs │ │ ├── CanPlaceFlowers_605_test.cs │ │ ├── ClimbingStairs_70_test.cs │ │ ├── FibonacciNumber_509_test.cs │ │ ├── MinCostClimbingStairs_746_test.cs │ │ └── PascalsTriangle_118_test.cs │ ├── Design │ │ ├── DesignHashMap_706_test.cs │ │ ├── DesignHashSet_705_test.cs │ │ ├── DesignParkingSystem_1603_test.cs │ │ ├── ImplementQueueusingStacks_232_test.cs │ │ ├── ImplementStackusingQueues_225_test.cs │ │ ├── MinStack_155_test.cs │ │ └── RangeSumQuery_Immutable_303_test.cs │ ├── LeetMath │ │ ├── CanMakeArithmeticProgressionFromSequence_1502_Test.cs │ │ ├── CheckIfItIsaStraightLine_1232_test.cs │ │ ├── CountOddNumbersinanIntervalRange_1523_test.cs │ │ ├── FizzBuzz_412_test.cs │ │ ├── HappyNumber_202_test.cs │ │ ├── LargestPerimeterTriangle_976_test.cs │ │ ├── PalindromeNumber_9_test.cs │ │ ├── RomanToInteger_13_test.cs │ │ ├── SignoftheProductofanArray_1822_test.cs │ │ ├── SqrtX_69_test.cs │ │ ├── SubtracttheProductandSumofDigitsofanInteger_1281_test.cs │ │ ├── TotalDistanceTraveled_2739_test.cs │ │ └── ValidPerfectSquare_367_test.cs │ ├── LinkedList │ │ ├── LinkedListCycle_141_test.cs │ │ ├── MergeTwoSortedLists_21_test.cs │ │ ├── MiddleoftheLinkedList_876_test.cs │ │ ├── PalindromeLinkedList_234_test.cs │ │ ├── RemoveDuplicatesfromSortedList_83_test.cs │ │ ├── RemoveLinkedListElements_203_test.cs │ │ └── ReverseLinkedList_206_test.cs │ ├── Strings │ │ ├── BackspaceStringCompare_844_test.cs │ │ ├── CheckifOneStringSwapCanMakeStringsEqual_1790_test.cs │ │ ├── CountBinarySubstrings_696_test.cs │ │ ├── DecryptStringfromAlphabettoIntegerMapping_1309_test.cs │ │ ├── FindtheDifference_389_test.cs │ │ ├── FirstUniqueCharacterinaString_387_test.cs │ │ ├── GoalParserInterpretation_1678_test.cs │ │ ├── ImplementstrStr_28_test.cs │ │ ├── IsSubsequence_392_test.cs │ │ ├── IsomorphicStrings_205_test.cs │ │ ├── LexicographicallySmallestPalindrome_2697_test.cs │ │ ├── LongestCommonPrefix_14_test.cs │ │ ├── LongestPalindrome_409_test.cs │ │ ├── MergeStringsAlternately_1768_test.cs │ │ ├── MinimizeStringLength_2716_test.cs │ │ ├── MinimumStringLengthAfterRemovingSubstrings_2696_test.cs │ │ ├── MinimumTimetoTypeWordUsingSpecialTypewriter_1974_test.cs │ │ ├── RansomNote_383_test.cs │ │ ├── ReplaceAllDigitswithCharacters_1844_test.cs │ │ ├── ReverseString_344_test.cs │ │ ├── ReverseVowelsofaString_345_test.cs │ │ ├── ReverseWordsinaStringIII_557_test.cs │ │ ├── ToLowerCase_709_test.cs │ │ ├── ValidAnagram_242_test.cs │ │ ├── ValidPalindrome_125_test.cs │ │ ├── ValidParentheses_20_test.cs │ │ └── VerifyinganAlienDictionary_953_test.cs │ └── Trees │ │ ├── AverageofLevelsinBinaryTree_637_test.cs │ │ ├── BinaryTreeInorderTraversal_94_test.cs │ │ ├── BinaryTreePostorderTraversal_145_test.cs │ │ ├── BinaryTreePreorderTraversal_144_test.cs │ │ ├── ConvertSortedArraytoBinarySearchTree_108_test.cs │ │ ├── InvertBinaryTree_226_test.cs │ │ ├── LowestCommonAncestorofaBinarySearchTree_235_test.cs │ │ ├── MaximumDepthofBinaryTree_104_test.cs │ │ ├── MergeTwoBinaryTrees_617_test.cs │ │ ├── N_aryTreePreorderTraversal_589_test.cs │ │ ├── PathSum_112_test.cs │ │ ├── SearchinaBinarySearchTree_700_test.cs │ │ ├── SumofLeftLeaves_404_test.cs │ │ ├── SymmetricTree_101_test.cs │ │ └── TwoSumIvInputisaBst653_test.cs ├── Hard │ └── LinkedList │ │ └── MergekSortedLists_23_test.cs ├── LeetCodeNet.Tests.csproj ├── Medium │ ├── Array │ │ ├── BoatstoSavePeople_881_test.cs │ │ ├── BullsandCows_299_test.cs │ │ ├── CourseSchedule_207_test.cs │ │ ├── FindFirstandLastPositionofElementinSortedArray_34_test.cs │ │ ├── FindMinimuminRotatedSortedArray_153_test.cs │ │ ├── FindtheValueofthePartition_2740_test.cs │ │ ├── FirstCompletelyPaintedRoworColumn_2661_test.cs │ │ ├── MaxConsecutiveOnes III_1004_test.cs │ │ ├── MaximumDistanceBetweenaPairofValues_1855_test.cs │ │ ├── MaximumNumberofVowelsinaSubstringofGivenLength_1456_test.cs │ │ ├── MaximumSubarray_53_test.cs │ │ ├── MergeSortedArray_88_test.cs │ │ ├── MinimizeMaximumofArray_2439_test.cs │ │ ├── NumberofAdjacentElementsWiththeSameColor_2672_test.cs │ │ ├── NumberofEnclaves_1020_test.cs │ │ ├── NumberofIslands_200_test.cs │ │ ├── PeakIndexinaMountainArray_852_test.cs │ │ ├── Permutations_46_test.cs │ │ ├── RottingOranges_994_test.cs │ │ ├── Searcha2DMatrix_74_test.cs │ │ ├── SearchinRotatedSortedArray_33_test.cs │ │ ├── SpiralMatrix_54_test.cs │ │ ├── SuccessfulPairsofSpellsandPotions_2300_test.cs │ │ ├── SumofMatrixAfterQueries_2718_test.cs │ │ ├── TopKFrequentElements_347_test.cs │ │ ├── TopKFrequentWords_692_test.cs │ │ ├── TwoSumII_167_test.cs │ │ └── ValidateStackSequences_946_test.cs │ ├── DP │ │ ├── FindthePunishmentNumberofanInteger_2698_test.cs │ │ ├── LongestPalindromicSubsequence_516_test.cs │ │ ├── MinimumCostForTickets_983_test.cs │ │ ├── MinimumPathSum_64_test.cs │ │ └── UniquePaths_62_test.cs │ ├── Design │ │ ├── DesignAddandSearchWordsDataStructure_211_test.cs │ │ ├── DesignBrowserHistory_1472_test.cs │ │ ├── FrequencyTracker_2671_test.cs │ │ └── ImplementTriePrefixTree_208_test.cs │ ├── Graph │ │ ├── CloneGraph_133_test.cs │ │ ├── CountUnreachablePairsofNodesinanUndirectedGraph_2316_test.cs │ │ ├── NumberofOperationstoMakeNetworkConnected_1319_test.cs │ │ └── ReorderRoutestoMakeAllPathsLeadtotheCityZero_1466_test.cs │ ├── LeetMath │ │ ├── NumberofZero-FilledSubarrays_2348_test.cs │ │ ├── ReverseInteger_7_test.cs │ │ └── SumofSquareNumbers_633_test.cs │ ├── LinkedList │ │ ├── AddTwoNumbers_2_test.cs │ │ └── LinkedListCycleII_142_test.cs │ ├── Strings │ │ ├── BreakaPalindrome_1328_test.cs │ │ ├── CountAndSay_38_test.cs │ │ ├── DecodeString_394_test.cs │ │ ├── FindAllAnagramsinaString_438_test.cs │ │ ├── FindUniqueBinaryString_1980_test.cs │ │ ├── LetterCasePermutation_784_test.cs │ │ ├── LexicographicallySmallestStringAfterSubstringOperation_2734_test.cs │ │ ├── LongestPalindromicSubstring_5_test.cs │ │ ├── LongestRepeatingCharacterReplacement_424_test.cs │ │ ├── LongestSubstringWithoutRepeatingCharacters_3_test.cs │ │ ├── OptimalPartitionofString_2405_test.cs │ │ ├── PermutationinString_567_test.cs │ │ ├── RemovingStarsFromaString_2390_test.cs │ │ ├── ShiftingLetters_848_test.cs │ │ └── SimplifyPath_71_test.cs │ └── Trees │ │ ├── BinaryTreeLevelOrderTraversal_102_test.cs │ │ ├── CheckCompletenessofaBinaryTree_958_test.cs │ │ ├── ConvertSortedListtoBinarySearchTree_109_test.cs │ │ ├── KthLargestSuminaBinaryTree_2583.cs │ │ ├── SumRootToLeafNumbers_129_test.cs │ │ └── ValidateBinarySearchTree_98_test.cs └── Usings.cs ├── LeetCodeNet.sln ├── LeetCodeNet ├── AssemblyInfo.cs ├── DataStructs │ ├── ListNode.cs │ ├── NaryTreeNode.cs │ ├── Node.cs │ └── TreeNode.cs ├── Easy │ ├── Array │ │ ├── AverageSalaryExcludingtheMinimumandMaximumSalary_1491.cs │ │ ├── BinarySearch_704.cs │ │ ├── CheckIfNandItsDoubleExist_1346.cs │ │ ├── ContainsDuplicate_217.cs │ │ ├── CountNegativeNumbersinaSortedMatrix_1351.cs │ │ ├── DeterminetheWinnerofaBowlingGame_2660.cs │ │ ├── DuplicateZeros_1089.cs │ │ ├── FindAllNumbersDisappearedinanArray_448.cs │ │ ├── FindLuckyIntegerinanArray_1394.cs │ │ ├── FindNumberswithEvenNumberofDigits_1295.cs │ │ ├── FindPivotIndex_724.cs │ │ ├── FindSmallestLetterGreaterThanTarget_744.cs │ │ ├── Finding3-DigitEvenNumbers_2094.cs │ │ ├── FindtheDistanceValueBetweenTwoArrays_1385.cs │ │ ├── FindtheDistinctDifferenceArray_2670.cs │ │ ├── FindtheHighestAltitude_1732.cs │ │ ├── FirstBadVersion_278.cs │ │ ├── FloodFill_733.cs │ │ ├── GuessNumberHigherorLower_374.cs │ │ ├── HeightChecker_1051.cs │ │ ├── IntersectionOfTwoArraysII_350.cs │ │ ├── KidsWiththeGreatestNumberofCandies_1431.cs │ │ ├── KthMissingPositiveNumber_1539.cs │ │ ├── LastStoneWeight_1046.cs │ │ ├── MajorityElement_169.cs │ │ ├── MatrixDiagonalSum_1572.cs │ │ ├── MaxConsecutiveOnes_485.cs │ │ ├── MoveZeroes_283.cs │ │ ├── NeitherMinimumnorMaximum_2733.cs │ │ ├── NextGreaterElement I_496.cs │ │ ├── PlusOne_66.cs │ │ ├── RemoveDuplicatesFromSortedArray_26.cs │ │ ├── RemoveElement_27.cs │ │ ├── ReplaceElementswithGreatestElementonRightSide_1299.cs │ │ ├── ReshapeTheMatrix_566.cs │ │ ├── RichestCustomerWealth_1672.cs │ │ ├── RowWithMaximumOnes_2643.cs │ │ ├── RunningSumof1dArray_1480.cs │ │ ├── SearchInsertPosition_35.cs │ │ ├── Semi-OrderedPermutation_2717.cs │ │ ├── SortArrayByParity_905.cs │ │ ├── SpecialArrayWithXElementsGreaterThanorEqualX_1608.cs │ │ ├── SquaresofaSortedArray_977.cs │ │ ├── SumofAllOddLengthSubarrays_1588.cs │ │ ├── TheKWeakestRowsinaMatrix_1337.cs │ │ ├── ThirdMaximumNumber_414.cs │ │ ├── TransposeMatrix_867.cs │ │ ├── TwoSum_1.cs │ │ └── ValidMountainArray_941.cs │ ├── BitManipulation │ │ ├── HammingDistance_461.cs │ │ ├── MissingNumber_268.cs │ │ ├── Numberof1Bits_191.cs │ │ ├── NumberofStepstoReduceaNumbertoZero_1342.cs │ │ ├── PowerofTwo_231.cs │ │ ├── ReverseBits_190.cs │ │ ├── SingleNumber_136.cs │ │ └── SortIntegersbyTheNumberof1Bits_1356.cs │ ├── DP │ │ ├── BestTimetoBuyandSellStock_121.cs │ │ ├── CanPlaceFlowers_605.cs │ │ ├── ClimbingStairs_70.cs │ │ ├── FibonacciNumber_509.cs │ │ ├── MinCostClimbingStairs_746.cs │ │ └── PascalsTriangle_118.cs │ ├── Design │ │ ├── DesignHashMap_706.cs │ │ ├── DesignHashSet_705.cs │ │ ├── DesignParkingSystem_1603.cs │ │ ├── ImplementQueueusingStacks_232.cs │ │ ├── ImplementStackusingQueues_225.cs │ │ ├── MinStack_155.cs │ │ └── RangeSumQuery_Immutable_303.cs │ ├── LeetMath │ │ ├── CanMakeArithmeticProgressionFromSequence_1502.cs │ │ ├── CheckIfItIsaStraightLine_1232.cs │ │ ├── ConvertBinaryNumberinaLinkedListtoInteger_1290.cs │ │ ├── CountOddNumbersinanIntervalRange_1523.cs │ │ ├── FizzBuzz_412.cs │ │ ├── HappyNumber_202.cs │ │ ├── LargestPerimeterTriangle_976.cs │ │ ├── PalindromeNumber_9.cs │ │ ├── RomanToInteger_13.cs │ │ ├── SignoftheProductofanArray_1822.cs │ │ ├── SqrtX_69.cs │ │ ├── SubtracttheProductandSumofDigitsofanInteger_1281.cs │ │ ├── TotalDistanceTraveled_2739.cs │ │ └── ValidPerfectSquare_367.cs │ ├── LinkedList │ │ ├── DeleteNodeinaLinkedList_237.cs │ │ ├── LinkedListCycle_141.cs │ │ ├── MergeTwoSortedLists_21.cs │ │ ├── MiddleoftheLinkedList_876.cs │ │ ├── PalindromeLinkedList_234.cs │ │ ├── RemoveDuplicatesfromSortedList_83.cs │ │ ├── RemoveLinkedListElements_203.cs │ │ └── ReverseLinkedList_206.cs │ ├── Strings │ │ ├── BackspaceStringCompare_844.cs │ │ ├── CheckifOneStringSwapCanMakeStringsEqual_1790.cs │ │ ├── CountBinarySubstrings_696.cs │ │ ├── DecryptStringfromAlphabettoIntegerMapping_1309.cs │ │ ├── FindtheDifference_389.cs │ │ ├── FirstUniqueCharacterinaString_387.cs │ │ ├── GoalParserInterpretation_1678.cs │ │ ├── ImplementstrStr_28.cs │ │ ├── IsSubsequence_392.cs │ │ ├── IsomorphicStrings_205.cs │ │ ├── LexicographicallySmallestPalindrome_2697.cs │ │ ├── LongestCommonPrefix_14.cs │ │ ├── LongestPalindrome_409.cs │ │ ├── MergeStringsAlternately_1768.cs │ │ ├── MinimizeStringLength_2716.cs │ │ ├── MinimumStringLengthAfterRemovingSubstrings_2696.cs │ │ ├── MinimumTimetoTypeWordUsingSpecialTypewriter_1974.cs │ │ ├── RansomNote_383.cs │ │ ├── ReplaceAllDigitswithCharacters_1844.cs │ │ ├── ReverseString_344.cs │ │ ├── ReverseVowelsofaString_345.cs │ │ ├── ReverseWordsinaStringIII_557.cs │ │ ├── ToLowerCase_709.cs │ │ ├── ValidAnagram_242.cs │ │ ├── ValidPalindrome_125.cs │ │ ├── ValidParentheses_20.cs │ │ └── VerifyinganAlienDictionary_953.cs │ └── Trees │ │ ├── AverageofLevelsinBinaryTree_637.cs │ │ ├── BinaryTreeInorderTraversal_94.cs │ │ ├── BinaryTreePostorderTraversal_145.cs │ │ ├── BinaryTreePreorderTraversal_144.cs │ │ ├── ConvertSortedArraytoBinarySearchTree_108.cs │ │ ├── InvertBinaryTree_226.cs │ │ ├── LowestCommonAncestorofaBinarySearchTree_235.cs │ │ ├── MaximumDepthofBinaryTree_104.cs │ │ ├── MergeTwoBinaryTrees_617.cs │ │ ├── N-aryTreePreorderTraversal_589.cs │ │ ├── PathSum_112.cs │ │ ├── SearchinaBinarySearchTree_700.cs │ │ ├── SumofLeftLeaves_404.cs │ │ ├── SymmetricTree_101.cs │ │ └── TwoSumIVInputisaBST_653.cs ├── Hard │ └── LinkedList │ │ └── MergekSortedLists_23.cs ├── LeetCodeNet.csproj └── Medium │ ├── Array │ ├── BoatstoSavePeople_881.cs │ ├── BullsandCows_299.cs │ ├── CourseSchedule_207.cs │ ├── FindFirstandLastPositionofElementinSortedArray_34.cs │ ├── FindMinimuminRotatedSortedArray_153.cs │ ├── FindtheValueofthePartition_2740.cs │ ├── FirstCompletelyPaintedRoworColumn_2661.cs │ ├── MaxConsecutiveOnesIII_1004.cs │ ├── MaximumDistanceBetweenaPairofValues_1855.cs │ ├── MaximumNumberofVowelsinaSubstringofGivenLength_1456.cs │ ├── MaximumSubarray_53.cs │ ├── MergeSortedArray_88.cs │ ├── MinimizeMaximumofArray_2439.cs │ ├── NumberofAdjacentElementsWiththeSameColor_2672.cs │ ├── NumberofEnclaves_1020.cs │ ├── NumberofIslands_200.cs │ ├── PeakIndexinaMountainArray_852.cs │ ├── Permutations_46.cs │ ├── RottingOranges_994.cs │ ├── Searcha2DMatrix_74.cs │ ├── SearchinRotatedSortedArray_33.cs │ ├── SpiralMatrix_54.cs │ ├── SuccessfulPairsofSpellsandPotions_2300.cs │ ├── SumofMatrixAfterQueries_2718.cs │ ├── TopKFrequentElements_347.cs │ ├── TopKFrequentWords_692.cs │ ├── TwoSumII_167.cs │ └── ValidateStackSequences_946.cs │ ├── DP │ ├── FindthePunishmentNumberofanInteger_2698.cs │ ├── LongestPalindromicSubsequence_516.cs │ ├── MinimumCostForTickets_983.cs │ ├── MinimumPathSum_64.cs │ └── UniquePaths_62.cs │ ├── Design │ ├── DesignAddandSearchWordsDataStructure_211.cs │ ├── DesignBrowserHistory_1472.cs │ ├── FrequencyTracker_2671.cs │ └── ImplementTrie(PrefixTree)_208.cs │ ├── Graph │ ├── CloneGraph_133.cs │ ├── CountUnreachablePairsofNodesinanUndirectedGraph_2316.cs │ ├── NumberofOperationstoMakeNetworkConnected_1319.cs │ └── ReorderRoutestoMakeAllPathsLeadtotheCityZero_1466.cs │ ├── LeetMath │ ├── NumberofZero-FilledSubarrays_2348.cs │ ├── ReverseInteger_7.cs │ └── SumofSquareNumbers_633.cs │ ├── LinkedList │ ├── AddTwoNumbers_2.cs │ ├── LinkedListCycleII_142.cs │ └── LinkedListRandomNode_382.cs │ ├── Strings │ ├── BreakaPalindrome_1328.cs │ ├── CountAndSay_38.cs │ ├── DecodeString_394.cs │ ├── FindAllAnagramsinaString_438.cs │ ├── FindUniqueBinaryString_1980.cs │ ├── LetterCasePermutation_784.cs │ ├── LexicographicallySmallestStringAfterSubstringOperation_2734.cs │ ├── LongestPalindromicSubstring_5.cs │ ├── LongestRepeatingCharacterReplacement_424.cs │ ├── LongestSubstringWithoutRepeatingCharacters_3.cs │ ├── OptimalPartitionofString_2405.cs │ ├── PermutationinString_567.cs │ ├── RemovingStarsFromaString_2390.cs │ ├── ShiftingLetters_848.cs │ └── SimplifyPath_71.cs │ └── Trees │ ├── BinaryTreeLevelOrderTraversal_102.cs │ ├── CheckCompletenessofaBinaryTree_958.cs │ ├── ConvertSortedListtoBinarySearchTree_109.cs │ ├── KthLargestSuminaBinaryTree_2583.cs │ ├── SumRootToLeafNumbers_129.cs │ └── ValidateBinarySearchTree_98.cs └── README.md /.github/workflows/dotnet-desktop.yml: -------------------------------------------------------------------------------- 1 | name: Dotnet format 2 | 3 | on: push 4 | 5 | jobs: 6 | check-format: 7 | runs-on: windows-2019 8 | 9 | steps: 10 | - uses: actions/checkout@v3 11 | 12 | - name: Setup .NET 13 | uses: actions/setup-dotnet@v3 14 | with: 15 | dotnet-version: 6.0.x 16 | 17 | - name: Setup NuGet 18 | uses: NuGet/setup-nuget@v1.0.5 19 | 20 | - name: Restore dependencies 21 | run: dotnet restore 22 | 23 | - name: Build 24 | run: dotnet build --configuration Release 25 | 26 | - name: Test 27 | run: dotnet test --logger "trx;LogFileName=test-results.trx" || true 28 | 29 | - name: Test Report 30 | uses: dorny/test-reporter@v1 31 | if: always() 32 | with: 33 | name: DotNET Tests 34 | path: "**/test-results.trx" 35 | reporter: dotnet-trx 36 | fail-on-error: true 37 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/Array/BinarySearch_704_test.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using LeetCodeNet.Easy.Array; 3 | 4 | namespace LeetCodeNet.Tests.Easy.Array 5 | { 6 | public sealed class BinarySearch_704_test 7 | { 8 | [Theory, ClassData(typeof(BinarySearchTestData))] 9 | public void Check(int[] inputData1, int inputData2, int expected) 10 | { 11 | var solver = new BinarySearch_704(); 12 | var result = solver.Search(inputData1, inputData2); 13 | 14 | Assert.Equal(expected, result); 15 | } 16 | } 17 | 18 | public sealed class BinarySearchTestData : IEnumerable 19 | { 20 | public IEnumerator GetEnumerator() 21 | { 22 | //// Explanation: 9 exists in nums and its index is 4 23 | yield return new object[] 24 | { 25 | new []{-1,0,3,5,9,12}, 26 | 9, 27 | 4 28 | }; 29 | 30 | //// Explanation: 2 does not exist in nums so return -1 31 | yield return new object[] 32 | { 33 | new []{-1,0,3,5,9,12}, 34 | 2, 35 | -1 36 | }; 37 | 38 | yield return new object[] 39 | { 40 | new []{5}, 41 | 5, 42 | 0 43 | }; 44 | } 45 | 46 | IEnumerator IEnumerable.GetEnumerator() 47 | { 48 | return GetEnumerator(); 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/Array/CheckIfNandItsDoubleExist_1346_test.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using LeetCodeNet.Easy.Array; 3 | 4 | namespace LeetCodeNet.Tests.Easy.Array 5 | { 6 | public sealed class CheckIfNandItsDoubleExist_1346_test 7 | { 8 | [Theory, ClassData(typeof(CheckIfNandItsDoubleExistTestData))] 9 | public void CheckHash(int[] inputData, bool expected) 10 | { 11 | var solver = new CheckIfNandItsDoubleExist_1346(); 12 | 13 | Assert.Equal(expected, solver.CheckIfExistHash(inputData)); 14 | } 15 | } 16 | 17 | public sealed class CheckIfNandItsDoubleExistTestData : IEnumerable 18 | { 19 | public IEnumerator GetEnumerator() 20 | { 21 | yield return new object[] 22 | { 23 | new []{10,2,5,3}, 24 | true 25 | }; 26 | 27 | yield return new object[] 28 | { 29 | new []{ 3,1,7,11 }, 30 | false 31 | }; 32 | 33 | yield return new object[] 34 | { 35 | new []{ 7,1,14,11 }, 36 | true 37 | }; 38 | 39 | yield return new object[] 40 | { 41 | new []{ 4,-7,11,4,18 }, 42 | false 43 | }; 44 | 45 | yield return new object[] 46 | { 47 | new []{ 2,3,3,0,0 }, 48 | true 49 | }; 50 | 51 | 52 | } 53 | 54 | IEnumerator IEnumerable.GetEnumerator() 55 | { 56 | return GetEnumerator(); 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/Array/DuplicateZeros_1089_test.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using LeetCodeNet.Easy.Array; 3 | 4 | namespace LeetCodeNet.Tests.Easy.Array 5 | { 6 | public sealed class DuplicateZeros_1089_test 7 | { 8 | [Theory, ClassData(typeof(DuplicateZerosTestData))] 9 | public void Check(int[] inputData, int[] expected) 10 | { 11 | var solver = new DuplicateZeros_1089(); 12 | solver.DuplicateZeros(inputData); 13 | 14 | Assert.True(inputData.SequenceEqual(expected)); 15 | } 16 | } 17 | 18 | public sealed class DuplicateZerosTestData : IEnumerable 19 | { 20 | public IEnumerator GetEnumerator() 21 | { 22 | yield return new object[] 23 | { 24 | new []{1,0,2,3,0,4,5,0}, 25 | new[] { 1, 0, 0, 2, 3, 0, 0, 4 } 26 | }; 27 | 28 | yield return new object[] 29 | { 30 | new []{ 1, 2, 3 }, 31 | new []{ 1, 2, 3 }, 32 | }; 33 | 34 | yield return new object[] 35 | { 36 | new []{ 8,5,0,9,0,3,4,7}, 37 | new[] { 8,5,0,0,9,0,0,3 } 38 | }; 39 | } 40 | 41 | IEnumerator IEnumerable.GetEnumerator() 42 | { 43 | return GetEnumerator(); 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/Array/FindAllNumbersDisappearedinanArray_448_test.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using LeetCodeNet.Easy.Array; 3 | 4 | namespace LeetCodeNet.Tests.Easy.Array 5 | { 6 | public sealed class FindAllNumbersDisappearedinanArray_448_test 7 | { 8 | [Theory, ClassData(typeof(FindAllNumbersDisappearedinanArrayTestData))] 9 | public void CheckSimple(int[] inputData, int[] expected) 10 | { 11 | var solver = new FindAllNumbersDisappearedinanArray_448(); 12 | 13 | Assert.True(expected.SequenceEqual(solver.FindDisappearedNumbersSimple(inputData))); 14 | } 15 | 16 | [Theory, ClassData(typeof(FindAllNumbersDisappearedinanArrayTestData))] 17 | public void CheckSort(int[] inputData, int[] expected) 18 | { 19 | var solver = new FindAllNumbersDisappearedinanArray_448(); 20 | 21 | Assert.True(expected.SequenceEqual(solver.FindDisappearedNumbersSort(inputData))); 22 | } 23 | } 24 | 25 | public sealed class FindAllNumbersDisappearedinanArrayTestData : IEnumerable 26 | { 27 | public IEnumerator GetEnumerator() 28 | { 29 | yield return new object[] 30 | { 31 | new []{4,3,2,7,8,2,3,1}, 32 | new []{5,6}, 33 | }; 34 | 35 | yield return new object[] 36 | { 37 | new []{1, 1}, 38 | new []{2}, 39 | }; 40 | } 41 | 42 | IEnumerator IEnumerable.GetEnumerator() 43 | { 44 | return GetEnumerator(); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/Array/FindLuckyIntegerinanArray_1394_test.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using LeetCodeNet.Easy.Array; 3 | 4 | namespace LeetCodeNet.Tests.Easy.Array 5 | { 6 | public sealed class FindLuckyIntegerinanArray_1394_test 7 | { 8 | [Theory, ClassData(typeof(FindLuckyIntegerinanArrayTestData))] 9 | public void CheckArray(int[] inputData, int expected) 10 | { 11 | var solver = new FindLuckyIntegerinanArray_1394(); 12 | var result = solver.FindLuckyArray(inputData); 13 | 14 | Assert.Equal(expected, result); 15 | } 16 | 17 | [Theory, ClassData(typeof(FindLuckyIntegerinanArrayTestData))] 18 | public void CheckDict(int[] inputData, int expected) 19 | { 20 | var solver = new FindLuckyIntegerinanArray_1394(); 21 | var result = solver.FindLuckyDict(inputData); 22 | 23 | Assert.Equal(expected, result); 24 | } 25 | } 26 | 27 | public sealed class FindLuckyIntegerinanArrayTestData : IEnumerable 28 | { 29 | public IEnumerator GetEnumerator() 30 | { 31 | yield return new object[] 32 | { 33 | new []{2,2,3,4}, 34 | 2 35 | }; 36 | 37 | yield return new object[] 38 | { 39 | new []{1,2,2,3,3,3}, 40 | 3 41 | }; 42 | 43 | yield return new object[] 44 | { 45 | new []{2,2,2,3,3}, 46 | -1 47 | }; 48 | } 49 | 50 | IEnumerator IEnumerable.GetEnumerator() 51 | { 52 | return GetEnumerator(); 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/Array/FindNumberswithEvenNumberofDigits_1295_test.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using LeetCodeNet.Easy.Array; 3 | 4 | namespace LeetCodeNet.Tests.Easy.Array 5 | { 6 | public sealed class FindNumberswithEvenNumberofDigits_1295_test 7 | { 8 | [Theory, ClassData(typeof(FindNumberswithEvenNumberofDigitsTestData))] 9 | public void Check(int[] inputData, int expected) 10 | { 11 | var solver = new FindNumberswithEvenNumberofDigits_1295(); 12 | var result = solver.FindNumbers(inputData); 13 | 14 | Assert.Equal(expected, result); 15 | } 16 | } 17 | 18 | public sealed class FindNumberswithEvenNumberofDigitsTestData : IEnumerable 19 | { 20 | public IEnumerator GetEnumerator() 21 | { 22 | yield return new object[] 23 | { 24 | new []{12,345,2,6,7896}, 25 | 2 26 | }; 27 | 28 | yield return new object[] 29 | { 30 | new []{555,901,482,1771}, 31 | 1 32 | }; 33 | } 34 | 35 | IEnumerator IEnumerable.GetEnumerator() 36 | { 37 | return GetEnumerator(); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/Array/FindtheHighestAltitude_1732_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Easy.Array; 2 | using System.Collections; 3 | 4 | namespace LeetCodeNet.Tests.Easy.Array 5 | { 6 | public sealed class FindtheHighestAltitude_1732_test 7 | { 8 | [Theory, ClassData(typeof(FindtheHighestAltitudeTestData))] 9 | public void Check(int[] inputData, int expected) 10 | { 11 | var solver = new FindtheHighestAltitude_1732(); 12 | var result = solver.LargestAltitude(inputData); 13 | 14 | Assert.Equal(expected, result); 15 | } 16 | } 17 | 18 | public sealed class FindtheHighestAltitudeTestData : IEnumerable 19 | { 20 | public IEnumerator GetEnumerator() 21 | { 22 | //// Explanation: The altitudes are [0,-5,-4,1,1,-6]. The highest is 1. 23 | yield return new object[] 24 | { 25 | new []{-5,1,5,0,-7}, 26 | 1 27 | }; 28 | 29 | //// Explanation: The altitudes are [0,-4,-7,-9,-10,-6,-3,-1]. The highest is 0. 30 | yield return new object[] 31 | { 32 | new []{-4,-3,-2,-1,4,3,2}, 33 | 0 34 | }; 35 | } 36 | 37 | IEnumerator IEnumerable.GetEnumerator() 38 | { 39 | return GetEnumerator(); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/Array/HeightChecker_1051_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Easy.Array; 2 | using System.Collections; 3 | 4 | namespace LeetCodeNet.Tests.Easy.Array 5 | { 6 | public sealed class HeightChecker_1051_test 7 | { 8 | [Theory, ClassData(typeof(HeightCheckerTestData))] 9 | public void CheckSorting(int[] inputData, double expected) 10 | { 11 | var solver = new HeightChecker_1051(); 12 | 13 | Assert.Equal(expected, solver.HeightCheckerSort(inputData)); 14 | } 15 | 16 | [Theory, ClassData(typeof(HeightCheckerTestData))] 17 | public void CheckBucket(int[] inputData, double expected) 18 | { 19 | var solver = new HeightChecker_1051(); 20 | 21 | Assert.Equal(expected, solver.HeightCheckerBucket(inputData)); 22 | } 23 | } 24 | 25 | public sealed class HeightCheckerTestData : IEnumerable 26 | { 27 | public IEnumerator GetEnumerator() 28 | { 29 | yield return new object[] 30 | { 31 | new[] { 1,1,4,2,1,3 }, 32 | 3 33 | }; 34 | 35 | yield return new object[] 36 | { 37 | new[] { 5,1,2,3,4 }, 38 | 5 39 | }; 40 | 41 | yield return new object[] 42 | { 43 | new[] { 1,2,3,4,5 }, 44 | 0 45 | }; 46 | } 47 | 48 | IEnumerator IEnumerable.GetEnumerator() 49 | { 50 | return GetEnumerator(); 51 | } 52 | } 53 | } -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/Array/MajorityElement_169_test.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using LeetCodeNet.Easy.Array; 3 | 4 | namespace LeetCodeNet.Tests.Easy.Array 5 | { 6 | public sealed class MajorityElement_169_test 7 | { 8 | [Theory, ClassData(typeof(MajorityElementTestData))] 9 | public void Check(int[] inputData, int expected) 10 | { 11 | var solver = new MajorityElement_169(); 12 | var result = solver.MajorityElement(inputData); 13 | 14 | Assert.Equal(expected, result); 15 | } 16 | } 17 | 18 | public sealed class MajorityElementTestData : IEnumerable 19 | { 20 | public IEnumerator GetEnumerator() 21 | { 22 | //// Explanation: 9 exists in nums and its index is 4 23 | yield return new object[] 24 | { 25 | new []{3,2,3}, 26 | 3 27 | }; 28 | 29 | //// Explanation: 2 does not exist in nums so return -1 30 | yield return new object[] 31 | { 32 | new []{2,2,1,1,1,2,2}, 33 | 2 34 | }; 35 | } 36 | 37 | IEnumerator IEnumerable.GetEnumerator() 38 | { 39 | return GetEnumerator(); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/Array/MatrixDiagonalSum_1572_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Easy.Array; 2 | using System.Collections; 3 | 4 | namespace LeetCodeNet.Tests.Easy.Array 5 | { 6 | public sealed class MatrixDiagonalSum_1572_test 7 | { 8 | [Theory, ClassData(typeof(MatrixDiagonalSumTestData))] 9 | public void Check(int[][] inputData, int expected) 10 | { 11 | var solver = new MatrixDiagonalSum_1572(); 12 | 13 | Assert.Equal(expected, solver.DiagonalSum(inputData)); 14 | } 15 | 16 | [Theory, ClassData(typeof(MatrixDiagonalSumTestData))] 17 | public void CheckOptimized(int[][] inputData, int expected) 18 | { 19 | var solver = new MatrixDiagonalSum_1572(); 20 | 21 | Assert.Equal(expected, solver.DiagonalSumOptimized(inputData)); 22 | } 23 | } 24 | 25 | public sealed class MatrixDiagonalSumTestData : IEnumerable 26 | { 27 | public IEnumerator GetEnumerator() 28 | { 29 | //// Diagonals sum: 1 + 5 + 9 + 3 + 7 = 25 30 | /// Notice that element mat[1][1] = 5 is counted only once. 31 | yield return new object[] 32 | { 33 | new[] { new[] { 1, 2, 3 }, new[] { 4, 5, 6 }, new[] { 7, 8, 9 } }, 34 | 25 35 | }; 36 | 37 | yield return new object[] 38 | { 39 | new[] { new[] { 1, 1, 1, 1 }, new[] { 1, 1, 1, 1 }, new[] { 1, 1, 1, 1 }, new[] { 1, 1, 1, 1 } }, 40 | 8 41 | }; 42 | } 43 | 44 | IEnumerator IEnumerable.GetEnumerator() 45 | { 46 | return GetEnumerator(); 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/Array/MaxConsecutiveOnes_485_test.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using LeetCodeNet.Easy.Array; 3 | 4 | namespace LeetCodeNet.Tests.Easy.Array 5 | { 6 | public sealed class MaxConsecutiveOnes_485_test 7 | { 8 | [Theory, ClassData(typeof(MaxConsecutiveOnesTestData))] 9 | public void CheckSimple(int[] inputData, int expected) 10 | { 11 | var solver = new MaxConsecutiveOnes_485(); 12 | var result = solver.FindMaxConsecutiveOnesSimple(inputData); 13 | 14 | Assert.Equal(expected, result); 15 | } 16 | 17 | [Theory, ClassData(typeof(MaxConsecutiveOnesTestData))] 18 | public void CheckShort(int[] inputData, int expected) 19 | { 20 | var solver = new MaxConsecutiveOnes_485(); 21 | var result = solver.FindMaxConsecutiveOnesShort(inputData); 22 | 23 | Assert.Equal(expected, result); 24 | } 25 | } 26 | 27 | public sealed class MaxConsecutiveOnesTestData : IEnumerable 28 | { 29 | public IEnumerator GetEnumerator() 30 | { 31 | yield return new object[] 32 | { 33 | new []{1,1,0,1,1,1}, 34 | 3 35 | }; 36 | 37 | yield return new object[] 38 | { 39 | new []{1,0,1,1,0,1}, 40 | 2 41 | }; 42 | } 43 | 44 | IEnumerator IEnumerable.GetEnumerator() 45 | { 46 | return GetEnumerator(); 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/Array/MoveZeroes_283_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Easy.Array; 2 | using System.Collections; 3 | 4 | namespace LeetCodeNet.Tests.Easy.Array 5 | { 6 | public sealed class MoveZeroes_283_test 7 | { 8 | [Theory, ClassData(typeof(MoveZeroesTestData))] 9 | public void Check(int[] inputData, int[] expected) 10 | { 11 | var solver = new MoveZeroes_283(); 12 | 13 | solver.MoveZeroesOptimization(inputData); 14 | Assert.Equal(expected, inputData); 15 | } 16 | 17 | [Theory, ClassData(typeof(MoveZeroesTestData))] 18 | public void CheckOpimal(int[] inputData, int[] expected) 19 | { 20 | var solver = new MoveZeroes_283(); 21 | 22 | solver.MoveZeroesOptimization(inputData); 23 | Assert.Equal(expected, inputData); 24 | } 25 | } 26 | 27 | public sealed class MoveZeroesTestData : IEnumerable 28 | { 29 | public IEnumerator GetEnumerator() 30 | { 31 | //// Note that you must do this in-place without making a copy of the array. 32 | yield return new object[] 33 | { 34 | new[] { 0, 1, 0, 3, 12 }, 35 | new[] { 1, 3, 12, 0, 0 } 36 | }; 37 | 38 | yield return new object[] 39 | { 40 | new[] { 1 }, 41 | new[] { 1 } 42 | }; 43 | } 44 | 45 | IEnumerator IEnumerable.GetEnumerator() 46 | { 47 | return GetEnumerator(); 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/Array/PlusOne_66_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Easy.Array; 2 | using System.Collections; 3 | 4 | namespace LeetCodeNet.Tests.Easy.Array 5 | { 6 | public sealed class PlusOne_66_test 7 | { 8 | [Theory, ClassData(typeof(PlusOneTestData))] 9 | public void Check(int[] inputData, int[] expected) 10 | { 11 | var solver = new PlusOne_66(); 12 | 13 | Assert.Equal(expected, solver.PlusOne(inputData)); 14 | } 15 | } 16 | 17 | public sealed class PlusOneTestData : IEnumerable 18 | { 19 | public IEnumerator GetEnumerator() 20 | { 21 | //// Explanation: The array represents the integer 123. Incrementing by one gives 123 + 1 = 124. 22 | /// Thus, the result should be[1, 2, 4]. 23 | yield return new object[] 24 | { 25 | new[] { 1, 2, 3 }, 26 | new[] { 1, 2, 4 } 27 | }; 28 | 29 | yield return new object[] 30 | { 31 | new[] { 4, 3, 2, 1 }, 32 | new[] { 4, 3, 2, 2 } 33 | }; 34 | 35 | yield return new object[] 36 | { 37 | new[] { 9 }, 38 | new[] { 1, 0 } 39 | }; 40 | } 41 | 42 | IEnumerator IEnumerable.GetEnumerator() 43 | { 44 | return GetEnumerator(); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/Array/RemoveDuplicatesFromSortedArray_26_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Easy.Array; 2 | using System.Collections; 3 | 4 | namespace LeetCodeNet.Tests.Easy.Array 5 | { 6 | public sealed class RemoveDuplicatesFromSortedArray_26_test 7 | { 8 | [Theory, ClassData(typeof(RemoveDuplicatesFromSortedArrayTestData))] 9 | public void Check(int[] inputData, int expectedCount, int[] expectedArray) 10 | { 11 | var solver = new RemoveDuplicatesFromSortedArray_26(); 12 | var result = solver.RemoveDuplicates(inputData); 13 | 14 | Assert.Equal(expectedCount, result); 15 | Assert.Equal(expectedArray, inputData); 16 | } 17 | } 18 | 19 | public sealed class RemoveDuplicatesFromSortedArrayTestData : IEnumerable 20 | { 21 | public IEnumerator GetEnumerator() 22 | { 23 | yield return new object[] 24 | { 25 | new[] { 1, 1, 2 }, 26 | 2, 27 | new[] { 1, 2, -101 } 28 | }; 29 | 30 | yield return new object[] 31 | { 32 | new[] { 0, 0, 1, 1, 1, 2, 2, 3, 3, 4 }, 33 | 5, 34 | new[] { 0, 1, 2, 3, 4, -101, -101, -101, -101, -101 } 35 | }; 36 | 37 | yield return new object[] 38 | { 39 | new[] { 1 }, 40 | 1, 41 | new[] { 1 } 42 | }; 43 | 44 | } 45 | 46 | IEnumerator IEnumerable.GetEnumerator() 47 | { 48 | return GetEnumerator(); 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/Array/RemoveElement_27_test.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using LeetCodeNet.Easy.Array; 3 | 4 | namespace LeetCodeNet.Tests.Easy.Array 5 | { 6 | public sealed class RemoveElement_27_test 7 | { 8 | [Theory, ClassData(typeof(RemoveElementTestData))] 9 | public void Check(int[] inputData1, int inputData2, int expected) 10 | { 11 | var solver = new RemoveElement_27(); 12 | var result = solver.RemoveElement(inputData1, inputData2); 13 | 14 | Assert.Equal(expected, result); 15 | } 16 | } 17 | 18 | public sealed class RemoveElementTestData : IEnumerable 19 | { 20 | public IEnumerator GetEnumerator() 21 | { 22 | yield return new object[] 23 | { 24 | new []{3,2,2,3}, 25 | 3, 26 | 2 27 | }; 28 | 29 | yield return new object[] 30 | { 31 | new []{0,1,2,2,3,0,4,2}, 32 | 2, 33 | 5 34 | }; 35 | } 36 | 37 | IEnumerator IEnumerable.GetEnumerator() 38 | { 39 | return GetEnumerator(); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/Array/ReshapeTheMatrix_566_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Easy.Array; 2 | using System.Collections; 3 | 4 | namespace LeetCodeNet.Tests.Easy.Array 5 | { 6 | public sealed class ReshapeTheMatrix_566_test 7 | { 8 | [Theory, ClassData(typeof(ReshapeTheMatrixTestData))] 9 | public void Check(int[][] inputData, int inputR, int inputC, int[][] expected) 10 | { 11 | var solver = new ReshapeTheMatrix_566(); 12 | 13 | Assert.Equal(expected, solver.MatrixReshape(inputData, inputR, inputC)); 14 | } 15 | } 16 | 17 | public sealed class ReshapeTheMatrixTestData : IEnumerable 18 | { 19 | public IEnumerator GetEnumerator() 20 | { 21 | yield return new object[] 22 | { 23 | new[] { new[] { 1, 2 }, new[] { 3, 4 } }, 24 | 1, 25 | 4, 26 | new[] { new[] { 1, 2, 3, 4 } } 27 | }; 28 | 29 | } 30 | 31 | IEnumerator IEnumerable.GetEnumerator() 32 | { 33 | return GetEnumerator(); 34 | } 35 | } 36 | } -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/Array/RowWithMaximumOnes_2643_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Easy.Array; 2 | using System.Collections; 3 | 4 | namespace LeetCodeNet.Tests.Easy.Array 5 | { 6 | public sealed class RowWithMaximumOnes_2643_test 7 | { 8 | [Theory, ClassData(typeof(RowWithMaximumOnesTestData))] 9 | public void Check(int[][] inputData, int[] expected) 10 | { 11 | var solver = new RowWithMaximumOnes_2643(); 12 | var result = solver.RowAndMaximumOnes(inputData); 13 | 14 | Assert.True(expected.SequenceEqual(result)); 15 | } 16 | } 17 | 18 | public sealed class RowWithMaximumOnesTestData : IEnumerable 19 | { 20 | public IEnumerator GetEnumerator() 21 | { 22 | yield return new object[] 23 | { 24 | new int[][]{new int[]{0,1}, new int[] {1,0}}, 25 | new int[] {0, 1} 26 | }; 27 | 28 | yield return new object[] 29 | { 30 | new int[][]{new int[]{0, 0, 0}, new int[] {0, 1, 1}}, 31 | new int[] {1 ,2} 32 | }; 33 | } 34 | 35 | IEnumerator IEnumerable.GetEnumerator() 36 | { 37 | return GetEnumerator(); 38 | } 39 | } 40 | } -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/Array/RunningSumof1dArray_1480_test.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using LeetCodeNet.Easy.Array; 3 | 4 | namespace LeetCodeNet.Tests.Easy.Array 5 | { 6 | public sealed class RunningSumof1dArray_1480_test 7 | { 8 | [Theory, ClassData(typeof(RunningSumof1dArrayTestData))] 9 | public void Check(int[] inputData, int[] expected) 10 | { 11 | var solver = new RunningSumof1dArray_1480(); 12 | 13 | Assert.True(expected.SequenceEqual(solver.RunningSum(inputData))); 14 | } 15 | } 16 | 17 | public sealed class RunningSumof1dArrayTestData : IEnumerable 18 | { 19 | public IEnumerator GetEnumerator() 20 | { 21 | //// Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4]. 22 | yield return new object[] 23 | { 24 | new []{1,2,3,4}, 25 | new []{1,3,6,10} 26 | }; 27 | 28 | //// Explanation: Running sum is obtained as follows: [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1]. 29 | yield return new object[] 30 | { 31 | new []{1,1,1,1,1}, 32 | new []{1,2,3,4,5} 33 | }; 34 | 35 | yield return new object[] 36 | { 37 | new []{ 3,1,2,10,1 }, 38 | new []{ 3,4,6,16,17 }, 39 | }; 40 | } 41 | 42 | IEnumerator IEnumerable.GetEnumerator() 43 | { 44 | return GetEnumerator(); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/Array/SearchInsertPosition_35_test.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using LeetCodeNet.Easy.Array; 3 | 4 | namespace LeetCodeNet.Tests.Easy.Array 5 | { 6 | public sealed class SearchInsertPosition_35_test 7 | { 8 | [Theory, ClassData(typeof(SearchInsertPositionTestData))] 9 | public void CheckBFS(int[] inputData1, int inputData2, int expected) 10 | { 11 | var solver = new SearchInsertPosition_35(); 12 | var result = solver.SearchInsert(inputData1, inputData2); 13 | 14 | Assert.Equal(expected, result); 15 | } 16 | } 17 | 18 | public sealed class SearchInsertPositionTestData : IEnumerable 19 | { 20 | public IEnumerator GetEnumerator() 21 | { 22 | yield return new object[] 23 | { 24 | new []{1,3,5,6}, 25 | 5, 26 | 2 27 | }; 28 | 29 | yield return new object[] 30 | { 31 | new []{1,3,5,6}, 32 | 2, 33 | 1 34 | }; 35 | 36 | yield return new object[] 37 | { 38 | new []{1,3,5,6}, 39 | 7, 40 | 4 41 | }; 42 | } 43 | 44 | IEnumerator IEnumerable.GetEnumerator() 45 | { 46 | return GetEnumerator(); 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/Array/Semi-OrderedPermutation_2717_test.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using LeetCodeNet.Easy.Array; 3 | 4 | namespace LeetCodeNet.Tests.Easy.Array 5 | { 6 | public sealed class SemiOrderedPermutation_2717_test 7 | { 8 | [Theory, ClassData(typeof(Semi_OrderedPermutationTestData))] 9 | public void Check(int[] inputData, int expected) 10 | { 11 | var solver = new Semi_OrderedPermutation_2717(); 12 | var result = solver.SemiOrderedPermutation(inputData); 13 | 14 | Assert.Equal(expected, result); 15 | } 16 | } 17 | 18 | public sealed class Semi_OrderedPermutationTestData : IEnumerable 19 | { 20 | public IEnumerator GetEnumerator() 21 | { 22 | yield return new object[] 23 | { 24 | new[] {2,1,4,3}, 25 | 2 26 | }; 27 | 28 | yield return new object[] 29 | { 30 | new[] {2,4,1,3}, 31 | 3 32 | }; 33 | 34 | yield return new object[] 35 | { 36 | new[] {1,3,4,2,5}, 37 | 0 38 | }; 39 | } 40 | 41 | IEnumerator IEnumerable.GetEnumerator() 42 | { 43 | return GetEnumerator(); 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/Array/SpecialArrayWithXElementsGreaterThanorEqualX_1608_test.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using LeetCodeNet.Easy.Array; 3 | 4 | namespace LeetCodeNet.Tests.Easy.Array 5 | { 6 | public sealed class SpecialArrayWithXElementsGreaterThanorEqualX_1608_test 7 | { 8 | [Theory, ClassData(typeof(SpecialArrayWithXElementsGreaterThanorEqualXTestData))] 9 | public void Check(int[] inputData, int expected) 10 | { 11 | var solver = new SpecialArrayWithXElementsGreaterThanorEqualX_1608(); 12 | var result = solver.SpecialArray(inputData); 13 | 14 | Assert.Equal(expected, result); 15 | } 16 | } 17 | 18 | public sealed class SpecialArrayWithXElementsGreaterThanorEqualXTestData : IEnumerable 19 | { 20 | public IEnumerator GetEnumerator() 21 | { 22 | yield return new object[] 23 | { 24 | new []{3, 5}, 25 | 2 26 | }; 27 | 28 | yield return new object[] 29 | { 30 | new []{0, 0}, 31 | -1 32 | }; 33 | 34 | yield return new object[] 35 | { 36 | new []{0,4,3,0,4}, 37 | 3 38 | }; 39 | } 40 | 41 | IEnumerator IEnumerable.GetEnumerator() 42 | { 43 | return GetEnumerator(); 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/Array/SquaresofaSortedArray_977_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Easy.Array; 2 | using System.Collections; 3 | 4 | namespace LeetCodeNet.Tests.Easy.Array 5 | { 6 | public sealed class SquaresofaSortedArray_977_test 7 | { 8 | [Theory, ClassData(typeof(SquaresofaSortedArrayTestData))] 9 | public void CheckBruteForce(int[] inputData, int[] expected) 10 | { 11 | var solver = new SquaresofaSortedArray_977(); 12 | 13 | Assert.Equal(expected, solver.SortedSquaresBruteForce(inputData)); 14 | } 15 | 16 | [Theory, ClassData(typeof(SquaresofaSortedArrayTestData))] 17 | public void CheckPointers(int[] inputData, int[] expected) 18 | { 19 | var solver = new SquaresofaSortedArray_977(); 20 | 21 | Assert.Equal(expected, solver.SortedSquaresPointers(inputData)); 22 | } 23 | } 24 | 25 | public sealed class SquaresofaSortedArrayTestData : IEnumerable 26 | { 27 | public IEnumerator GetEnumerator() 28 | { 29 | //// Explanation: After squaring, the array becomes [16,1,0,9,100]. After sorting, it becomes[0, 1, 9, 16, 100]. 30 | yield return new object[] 31 | { 32 | new[] { -4, -1, 0, 3, 10 }, 33 | new[] { 0, 1, 9, 16, 100 } 34 | }; 35 | 36 | yield return new object[] 37 | { 38 | new[] { -7, -3, 2, 3, 11 }, 39 | new[] { 4, 9, 9, 49, 121 } 40 | }; 41 | } 42 | 43 | IEnumerator IEnumerable.GetEnumerator() 44 | { 45 | return GetEnumerator(); 46 | } 47 | } 48 | } -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/Array/ThirdMaximumNumber_414_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Easy.Array; 2 | using System.Collections; 3 | 4 | namespace LeetCodeNet.Tests.Easy.Array 5 | { 6 | public sealed class ThirdMaximumNumber_414_test 7 | { 8 | [Theory, ClassData(typeof(ThirвMaximumNumberTestData))] 9 | public void CheckLinq(int[] inputData, int expected) 10 | { 11 | var solver = new ThirdMaximumNumber_414(); 12 | 13 | Assert.Equal(expected, solver.ThirdMaxLinq(inputData)); 14 | } 15 | 16 | [Theory, ClassData(typeof(ThirвMaximumNumberTestData))] 17 | public void CheckMax(int[] inputData, double expected) 18 | { 19 | var solver = new ThirdMaximumNumber_414(); 20 | 21 | Assert.Equal(expected, solver.ThirdMaxByValue(inputData)); 22 | } 23 | } 24 | 25 | public sealed class ThirвMaximumNumberTestData : IEnumerable 26 | { 27 | public IEnumerator GetEnumerator() 28 | { 29 | yield return new object[] 30 | { 31 | new[] { 3,2,1 }, 32 | 1 33 | }; 34 | 35 | yield return new object[] 36 | { 37 | new[] { 1,2 }, 38 | 2 39 | }; 40 | 41 | yield return new object[] 42 | { 43 | new[] { 2,2,3,1 }, 44 | 1 45 | }; 46 | } 47 | 48 | IEnumerator IEnumerable.GetEnumerator() 49 | { 50 | return GetEnumerator(); 51 | } 52 | } 53 | } -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/Array/TransposeMatrix_867_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Easy.Array; 2 | using System.Collections; 3 | 4 | namespace LeetCodeNet.Tests.Easy.Array 5 | { 6 | public sealed class TransposeMatrix_867_test 7 | { 8 | [Theory, ClassData(typeof(TransposeMatrixTestData))] 9 | public void Check(int[][] inputData, int[][] expected) 10 | { 11 | var solver = new TransposeMatrix_867(); 12 | 13 | Assert.Equal(expected, solver.Transpose(inputData)); 14 | } 15 | 16 | [Fact] 17 | public void CheckInPlace() 18 | { 19 | var solver = new TransposeMatrix_867(); 20 | 21 | Assert.Equal(new[] { new[] { 1, 4, 7 }, new[] { 2, 5, 8 }, new[] { 3, 6, 9 } }, solver.TransposeInPlace(new[] { new[] { 1, 2, 3 }, new[] { 4, 5, 6 }, new[] { 7, 8, 9 } })); 22 | } 23 | } 24 | 25 | public sealed class TransposeMatrixTestData : IEnumerable 26 | { 27 | public IEnumerator GetEnumerator() 28 | { 29 | yield return new object[] 30 | { 31 | new[] { new[] { 1, 2, 3 }, new[] { 4, 5, 6 }, new[] { 7, 8, 9 } }, 32 | new[] { new[] { 1, 4, 7 }, new[] { 2, 5, 8 }, new[] { 3, 6, 9 } } 33 | }; 34 | 35 | yield return new object[] 36 | { 37 | new[] { new[] { 1, 2, 3 }, new[] { 4, 5, 6 } }, 38 | new[] { new[] { 1, 4 }, new[] { 2, 5 }, new[] { 3, 6 } } 39 | }; 40 | } 41 | 42 | IEnumerator IEnumerable.GetEnumerator() 43 | { 44 | return GetEnumerator(); 45 | } 46 | } 47 | } -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/Array/TwoSum_1_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Easy.Array; 2 | using System.Collections; 3 | 4 | namespace LeetCodeNet.Tests.Easy.Array 5 | { 6 | public sealed class TwoSum_1_test 7 | { 8 | [Theory, ClassData(typeof(TwoSumTestData))] 9 | public void Check(int[] inputData, int target, int[] expected) 10 | { 11 | var solver = new TwoSum_1(); 12 | 13 | Assert.Equal(expected, solver.TwoSum(inputData, target)); 14 | } 15 | } 16 | 17 | public sealed class TwoSumTestData : IEnumerable 18 | { 19 | public IEnumerator GetEnumerator() 20 | { 21 | //// Explanation: Because nums[0] + nums[1] == 9, we return [0, 1]. 22 | yield return new object[] 23 | { 24 | new[] { 2, 7, 11, 15 }, 25 | 9, 26 | new[] { 0, 1 } 27 | }; 28 | 29 | yield return new object[] 30 | { 31 | new[] { 3, 2, 4 }, 32 | 6, 33 | new[] { 1, 2 } 34 | }; 35 | 36 | yield return new object[] 37 | { 38 | new[] { 3, 3 }, 39 | 6, 40 | new[] { 0, 1 } 41 | }; 42 | } 43 | 44 | IEnumerator IEnumerable.GetEnumerator() 45 | { 46 | return GetEnumerator(); 47 | } 48 | } 49 | } -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/Array/ValidMountainArray_941_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Easy.Array; 2 | using System.Collections; 3 | 4 | namespace LeetCodeNet.Tests.Easy.Array 5 | { 6 | public sealed class ValidMountainArray_941_test 7 | { 8 | [Theory, ClassData(typeof(ValidMountainArrayTestData))] 9 | public void CheckLinq(int[] inputData, bool expected) 10 | { 11 | var solver = new ValidMountainArray_941(); 12 | 13 | Assert.Equal(expected, solver.ValidMountainArray(inputData)); 14 | } 15 | } 16 | 17 | public sealed class ValidMountainArrayTestData : IEnumerable 18 | { 19 | public IEnumerator GetEnumerator() 20 | { 21 | yield return new object[] 22 | { 23 | new[] { 2, 1 }, 24 | false 25 | }; 26 | 27 | yield return new object[] 28 | { 29 | new[] { 3,5,5 }, 30 | false 31 | }; 32 | 33 | yield return new object[] 34 | { 35 | new[] { 0,3,2,1 }, 36 | true 37 | }; 38 | } 39 | 40 | IEnumerator IEnumerable.GetEnumerator() 41 | { 42 | return GetEnumerator(); 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/BitManipulation/ConvertBinaryNumberinaLinkedListtoInteger_1290_test.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using LeetCodeNet.DataStructs; 3 | using LeetCodeNet.Easy.LeetMath; 4 | 5 | namespace LeetCodeNet.Tests.Easy.BitManipulation 6 | { 7 | public sealed class ConvertBinaryNumberinaLinkedListtoInteger_1290_test 8 | { 9 | [Theory, ClassData(typeof(ConvertBinaryNumberinaLinkedListtoIntegerTestData))] 10 | public void CheckShift(ListNode inputData, int expected) 11 | { 12 | var solver = new ConvertBinaryNumberinaLinkedListtoInteger_1290(); 13 | var result = solver.GetDecimalValueSimple(inputData); 14 | 15 | Assert.Equal(expected, result); 16 | } 17 | 18 | [Theory, ClassData(typeof(ConvertBinaryNumberinaLinkedListtoIntegerTestData))] 19 | public void CheckBit(ListNode inputData, int expected) 20 | { 21 | var solver = new ConvertBinaryNumberinaLinkedListtoInteger_1290(); 22 | var result = solver.GetDecimalValueBinary(inputData); 23 | 24 | Assert.Equal(expected, result); 25 | } 26 | } 27 | 28 | public sealed class ConvertBinaryNumberinaLinkedListtoIntegerTestData : IEnumerable 29 | { 30 | public IEnumerator GetEnumerator() 31 | { 32 | //// (101) in base 2 = (5) in base 10 33 | yield return new object[] 34 | { 35 | new ListNode(1) {next = new ListNode(0) { next = new ListNode(1)}}, 36 | 5 37 | }; 38 | } 39 | 40 | IEnumerator IEnumerable.GetEnumerator() 41 | { 42 | return GetEnumerator(); 43 | } 44 | } 45 | } -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/BitManipulation/HammingDistance_461_test.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using LeetCodeNet.Easy.BitManipulation; 3 | 4 | namespace LeetCodeNet.Tests.Easy.BitManipulation 5 | { 6 | public sealed class HammingDistance_461_test 7 | { 8 | [Theory, ClassData(typeof(HammingDistanceTestData))] 9 | public void Check(int inputData1, int inputData2, int expected) 10 | { 11 | var solver = new HammingDistance_461(); 12 | var result = solver.HammingDistance(inputData1, inputData2); 13 | 14 | Assert.Equal(expected, result); 15 | } 16 | } 17 | 18 | public sealed class HammingDistanceTestData : IEnumerable 19 | { 20 | public IEnumerator GetEnumerator() 21 | { 22 | //// Explanation: 23 | /// 1(0 0 0 1) 24 | /// 4(0 1 0 0) 25 | /// ↑ ↑ 26 | /// The above arrows point to positions where the corresponding bits are different. 27 | yield return new object[] 28 | { 29 | 1, 30 | 4, 31 | 2 32 | }; 33 | 34 | yield return new object[] 35 | { 36 | 3, 37 | 1, 38 | 1 39 | }; 40 | } 41 | 42 | IEnumerator IEnumerable.GetEnumerator() 43 | { 44 | return GetEnumerator(); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/BitManipulation/Numberof1Bits_191_test.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using LeetCodeNet.Easy.BitManipulation; 3 | 4 | namespace LeetCodeNet.Tests.Easy.BitManipulation 5 | { 6 | public sealed class Numberof1Bits_191_test 7 | { 8 | [Theory, ClassData(typeof(Numberof1BitsTestData))] 9 | public void CheckShift(uint inputData, int expected) 10 | { 11 | var solver = new Numberof1Bits_191(); 12 | var result = solver.HammingWeightShift(inputData); 13 | 14 | Assert.Equal(expected, result); 15 | } 16 | 17 | [Theory, ClassData(typeof(Numberof1BitsTestData))] 18 | public void CheckBit(uint inputData, int expected) 19 | { 20 | var solver = new Numberof1Bits_191(); 21 | var result = solver.HammingWeightBit(inputData); 22 | 23 | Assert.Equal(expected, result); 24 | } 25 | } 26 | 27 | public sealed class Numberof1BitsTestData : IEnumerable 28 | { 29 | public IEnumerator GetEnumerator() 30 | { 31 | //// Explanation: The input binary string 00000000000000000000000000001011 has a total of three '1' bits. 32 | yield return new object[] 33 | { 34 | 11, 35 | 3 36 | }; 37 | 38 | yield return new object[] 39 | { 40 | 64, 41 | 1 42 | }; 43 | 44 | yield return new object[] 45 | { 46 | 4294967293, 47 | 31 48 | }; 49 | } 50 | 51 | IEnumerator IEnumerable.GetEnumerator() 52 | { 53 | return GetEnumerator(); 54 | } 55 | } 56 | } -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/BitManipulation/PowerofTwo_231_test.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using LeetCodeNet.Easy.BitManipulation; 3 | 4 | namespace LeetCodeNet.Tests.Easy.BitManipulation 5 | { 6 | public sealed class PowerofTwo_231_test 7 | { 8 | [Theory, ClassData(typeof(PowerofTwoTestData))] 9 | public void CheckMath(int inputData, bool expected) 10 | { 11 | var solver = new PowerofTwo_231(); 12 | var result = solver.IsPowerOfTwoMath(inputData); 13 | 14 | Assert.Equal(expected, result); 15 | } 16 | 17 | [Theory, ClassData(typeof(PowerofTwoTestData))] 18 | public void CheckBit(int inputData, bool expected) 19 | { 20 | var solver = new PowerofTwo_231(); 21 | var result = solver.IsPowerOfTwoBit(inputData); 22 | 23 | Assert.Equal(expected, result); 24 | } 25 | } 26 | 27 | public sealed class PowerofTwoTestData : IEnumerable 28 | { 29 | public IEnumerator GetEnumerator() 30 | { 31 | yield return new object[] 32 | { 33 | 1, 34 | true 35 | }; 36 | 37 | yield return new object[] 38 | { 39 | 16, 40 | true 41 | }; 42 | 43 | yield return new object[] 44 | { 45 | 3, 46 | false 47 | }; 48 | } 49 | 50 | IEnumerator IEnumerable.GetEnumerator() 51 | { 52 | return GetEnumerator(); 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/BitManipulation/ReverseBits_190_test.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using LeetCodeNet.Easy.BitManipulation; 3 | 4 | namespace LeetCodeNet.Tests.Easy.BitManipulation 5 | { 6 | public sealed class ReverseBits_190_test 7 | { 8 | [Theory, ClassData(typeof(ReverseBitsTestData))] 9 | public void CheckBit(uint inputData, uint expected) 10 | { 11 | var solver = new ReverseBits_190(); 12 | var result = solver.reverseBits(inputData); 13 | 14 | Assert.Equal(expected, result); 15 | } 16 | } 17 | 18 | public sealed class ReverseBitsTestData : IEnumerable 19 | { 20 | public IEnumerator GetEnumerator() 21 | { 22 | yield return new object[] 23 | { 24 | 43261596, 25 | 964176192 26 | }; 27 | 28 | yield return new object[] 29 | { 30 | 4294967293, 31 | 3221225471 32 | }; 33 | } 34 | 35 | IEnumerator IEnumerable.GetEnumerator() 36 | { 37 | return GetEnumerator(); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/BitManipulation/SignleNumber_136_test.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using LeetCodeNet.Easy.BitManipulation; 3 | 4 | namespace LeetCodeNet.Tests.Easy.BitManipulation 5 | { 6 | public sealed class SingleNumber_136_test 7 | { 8 | [Theory, ClassData(typeof(SingleNumberTestData))] 9 | public void CheckBit(int[] inputData, int expected) 10 | { 11 | var solver = new SingleNumber_136(); 12 | var result = solver.SingleNumber(inputData); 13 | 14 | Assert.Equal(expected, result); 15 | } 16 | } 17 | 18 | public sealed class SingleNumberTestData : IEnumerable 19 | { 20 | public IEnumerator GetEnumerator() 21 | { 22 | yield return new object[] 23 | { 24 | new[]{2,2,1}, 25 | 1 26 | }; 27 | 28 | yield return new object[] 29 | { 30 | new[] {4,1,2,1,2}, 31 | 4 32 | }; 33 | 34 | yield return new object[] 35 | { 36 | new[] {1}, 37 | 1 38 | }; 39 | } 40 | 41 | IEnumerator IEnumerable.GetEnumerator() 42 | { 43 | return GetEnumerator(); 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/DP/BestTimetoBuyandSellStock_121_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Easy.DP; 2 | using System.Collections; 3 | 4 | namespace LeetCodeNet.Tests.Easy.DP 5 | { 6 | public sealed class BestTimetoBuyandSellStock_121_test 7 | { 8 | [Theory, ClassData(typeof(BestTimetoBuyandSellStockTestData))] 9 | public void Check(int[] inputData, int expected) 10 | { 11 | var solver = new BestTimetoBuyandSellStock_121(); 12 | 13 | Assert.Equal(expected, solver.MaxProfit(inputData)); 14 | } 15 | } 16 | 17 | public sealed class BestTimetoBuyandSellStockTestData : IEnumerable 18 | { 19 | public IEnumerator GetEnumerator() 20 | { 21 | //// Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5. 22 | /// Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell. 23 | yield return new object[] 24 | { 25 | new[] { 7, 1, 5, 3, 6, 4 }, 26 | 5 27 | }; 28 | 29 | //// Explanation: In this case, no transactions are done and the max profit = 0. 30 | yield return new object[] 31 | { 32 | new[] {7,6,4,3,1}, 33 | 0 34 | }; 35 | } 36 | 37 | IEnumerator IEnumerable.GetEnumerator() 38 | { 39 | return GetEnumerator(); 40 | } 41 | } 42 | } -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/DP/PascalsTriangle_118_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Easy.DP; 2 | using System.Collections; 3 | 4 | namespace LeetCodeNet.Tests.Easy.DP 5 | { 6 | public sealed class PascalsTriangle_118_test 7 | { 8 | [Theory, ClassData(typeof(PascalsTriangleTestData))] 9 | public void CheckLinq(int inputData, int[][] expected) 10 | { 11 | var solver = new PascalsTriangle_118(); 12 | 13 | Assert.Equal(expected, solver.Generate(inputData)); 14 | } 15 | } 16 | 17 | public sealed class PascalsTriangleTestData : IEnumerable 18 | { 19 | public IEnumerator GetEnumerator() 20 | { 21 | yield return new object[] 22 | { 23 | 5, 24 | new[] { new[] { 1 }, new[] { 1, 1 }, new[] { 1, 2, 1 }, new[] { 1, 3, 3, 1 }, new[] { 1, 4, 6, 4, 1 } } 25 | }; 26 | 27 | yield return new object[] 28 | { 29 | 1, 30 | new[] { new[] { 1 } } 31 | }; 32 | } 33 | 34 | IEnumerator IEnumerable.GetEnumerator() 35 | { 36 | return GetEnumerator(); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/Design/DesignHashMap_706_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Easy.Design; 2 | 3 | namespace LeetCodeNet.Tests.Easy.Design 4 | { 5 | public sealed class DesignHashMap_706_test 6 | { 7 | [Fact] 8 | public void Check() 9 | { 10 | var solver = new MyHashMap(); 11 | 12 | solver.Put(1, 1); 13 | solver.Put(2, 2); 14 | Assert.Equal(1, solver.Get(1)); 15 | Assert.Equal(-1, solver.Get(3)); 16 | solver.Put(2,1); 17 | Assert.Equal(1, solver.Get(2)); 18 | solver.Remove(2); 19 | Assert.Equal(-1, solver.Get(2)); 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/Design/DesignHashSet_705_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Easy.Design; 2 | 3 | namespace LeetCodeNet.Tests.Easy.Design 4 | { 5 | public sealed class DesignHashSet_705_test 6 | { 7 | [Fact] 8 | public void CheckSimple() 9 | { 10 | var solver = new MyHashSet_Easy(); 11 | 12 | solver.Add(1); 13 | solver.Add(2); 14 | Assert.True(solver.Contains(1)); 15 | Assert.False(solver.Contains(3)); 16 | solver.Remove(2); 17 | Assert.False(solver.Contains(2)); 18 | solver.Add(1000000); 19 | Assert.True(solver.Contains(1000000)); 20 | } 21 | 22 | [Fact] 23 | public void CheckOptimal() 24 | { 25 | var solver = new MyHashSet_Optimal(); 26 | 27 | solver.Add(1); 28 | solver.Add(2); 29 | Assert.True(solver.Contains(1)); 30 | Assert.False(solver.Contains(3)); 31 | solver.Remove(2); 32 | Assert.False(solver.Contains(2)); 33 | solver.Add(1000000); 34 | Assert.True(solver.Contains(1000000)); 35 | } 36 | } 37 | } -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/Design/DesignParkingSystem_1603_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Easy.Design; 2 | 3 | namespace LeetCodeNet.Tests.Easy.Design 4 | { 5 | public sealed class DesignParkingSystem_1603 6 | { 7 | [Fact] 8 | public void Check() 9 | { 10 | var solver = new ParkingSystem(1, 1, 0); 11 | 12 | Assert.True(solver.AddCar(1)); 13 | Assert.True(solver.AddCar(2)); 14 | Assert.False(solver.AddCar(3)); 15 | Assert.False(solver.AddCar(1)); 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/Design/ImplementQueueusingStacks_232_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Easy.Design; 2 | 3 | namespace LeetCodeNet.Tests.Easy.Design 4 | { 5 | public sealed class ImplementQueueusingStacks_232_test 6 | { 7 | [Fact] 8 | public void Check() 9 | { 10 | var solver = new MyStack(); 11 | 12 | solver.Push(1); 13 | solver.Push(2); 14 | Assert.Equal(solver.Top(), 2); 15 | Assert.Equal(solver.Pop(), 2); 16 | Assert.False(solver.Empty()); 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/Design/ImplementStackusingQueues_225_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Easy.Design; 2 | 3 | namespace LeetCodeNet.Tests.Easy.Design 4 | { 5 | public sealed class ImplementStackusingQueues_225_test 6 | { 7 | [Fact] 8 | public void Check() 9 | { 10 | var solver = new MyQueue(); 11 | 12 | solver.Push(1); 13 | solver.Push(2); 14 | Assert.Equal(solver.Peek(), 1); 15 | Assert.Equal(solver.Pop(), 1); 16 | Assert.False(solver.Empty()); 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/Design/MinStack_155_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Easy.Design; 2 | 3 | namespace LeetCodeNet.Tests.Easy.Design 4 | { 5 | public sealed class MinStack_155_test 6 | { 7 | [Fact] 8 | public void Check() 9 | { 10 | var solver = new MinStack(); 11 | 12 | solver.Push(-2); 13 | solver.Push(0); 14 | solver.Push(-3); 15 | 16 | Assert.Equal(solver.GetMin(), -3); 17 | solver.Pop(); 18 | Assert.Equal(solver.Top(), 0); 19 | Assert.Equal(solver.GetMin(), -2); 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/Design/RangeSumQuery_Immutable_303_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Easy.Design; 2 | 3 | namespace LeetCodeNet.Tests.Easy.Design 4 | { 5 | public sealed class RangeSumQuery_Immutable_303_test 6 | { 7 | [Fact] 8 | public void Check() 9 | { 10 | var solver = new NumArray(new[] { -2, 0, 3, -5, 2, -1 }); 11 | 12 | Assert.Equal(solver.SumRange(0, 2), 1); 13 | Assert.Equal(solver.SumRange(2, 5), -1); 14 | Assert.Equal(solver.SumRange(0, 5), -3); 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/LeetMath/CanMakeArithmeticProgressionFromSequence_1502_Test.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using LeetCodeNet.Easy.LeetMath; 3 | 4 | namespace LeetCodeNet.Tests.Easy.LeetMath 5 | { 6 | public sealed class CanMakeArithmeticProgressionFromSequence_1502_Test 7 | { 8 | [Theory, ClassData(typeof(CanMakeArithmeticProgressionFromSequenceTestData))] 9 | public void CheckSimple(int[] inputData, bool expected) 10 | { 11 | var solver = new CanMakeArithmeticProgressionFromSequence_1502(); 12 | var result = solver.CanMakeArithmeticProgressionSimple(inputData); 13 | 14 | Assert.Equal(expected, result); 15 | } 16 | 17 | [Theory, ClassData(typeof(CanMakeArithmeticProgressionFromSequenceTestData))] 18 | public void CheckMath(int[] inputData, bool expected) 19 | { 20 | var solver = new CanMakeArithmeticProgressionFromSequence_1502(); 21 | var result = solver.CanMakeArithmeticProgressionMath(inputData); 22 | 23 | Assert.Equal(expected, result); 24 | } 25 | } 26 | 27 | public sealed class CanMakeArithmeticProgressionFromSequenceTestData : IEnumerable 28 | { 29 | public IEnumerator GetEnumerator() 30 | { 31 | //// Explanation: We can reorder the elements as [1,3,5] or [5,3,1] with differences 2 and -2 respectively, between each consecutive elements. 32 | yield return new object[] 33 | { 34 | new []{3,5,1}, 35 | true 36 | }; 37 | } 38 | 39 | IEnumerator IEnumerable.GetEnumerator() 40 | { 41 | return GetEnumerator(); 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/LeetMath/CheckIfItIsaStraightLine_1232_test.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using LeetCodeNet.Easy.LeetMath; 3 | 4 | namespace LeetCodeNet.Tests.Easy.LeetMath 5 | { 6 | public sealed class CheckIfItIsaStraightLine_1232_test 7 | { 8 | [Theory, ClassData(typeof(CheckIfItIsaStraightLineTestData))] 9 | public void CheckSimple(int[][] inputData, bool expected) 10 | { 11 | var solver = new CheckIfItIsaStraightLine_1232(); 12 | var result = solver.CheckStraightLine(inputData); 13 | 14 | Assert.Equal(expected, result); 15 | } 16 | } 17 | 18 | public sealed class CheckIfItIsaStraightLineTestData : IEnumerable 19 | { 20 | public IEnumerator GetEnumerator() 21 | { 22 | yield return new object[] 23 | { 24 | new[] { new[] { 1,2 }, new[] {2,3 }, new[] { 3,4 } , new[] { 4,5 }, new[] { 5,6 }, new[] { 6,7 }}, 25 | true 26 | }; 27 | 28 | yield return new object[] 29 | { 30 | new[] { new[] { 1,1 }, new[] {2,2 }, new[] { 3,4 } , new[] { 4,5 }, new[] { 5,6 }, new[] { 7,7 }}, 31 | false 32 | }; 33 | } 34 | 35 | IEnumerator IEnumerable.GetEnumerator() 36 | { 37 | return GetEnumerator(); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/LeetMath/CountOddNumbersinanIntervalRange_1523_test.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using LeetCodeNet.Easy.LeetMath; 3 | 4 | namespace LeetCodeNet.Tests.Easy.LeetMath 5 | { 6 | public sealed class CountOddNumbersinanIntervalRange_1523_test 7 | { 8 | [Theory, ClassData(typeof(CountOddNumbersinanIntervalRangeTestData))] 9 | public void CheckSimple(int low, int high, int expected) 10 | { 11 | var solver = new CountOddNumbersinanIntervalRange_1523(); 12 | var result = solver.CountOdds(low,high); 13 | 14 | Assert.Equal(expected, result); 15 | } 16 | } 17 | 18 | public sealed class CountOddNumbersinanIntervalRangeTestData : IEnumerable 19 | { 20 | public IEnumerator GetEnumerator() 21 | { 22 | yield return new object[] 23 | { 24 | 3, 7, 3 25 | }; 26 | 27 | yield return new object[] 28 | { 29 | 8, 10, 1 30 | }; 31 | } 32 | 33 | IEnumerator IEnumerable.GetEnumerator() 34 | { 35 | return GetEnumerator(); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/LeetMath/FizzBuzz_412_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Easy.LeetMath; 2 | using System.Collections; 3 | 4 | namespace LeetCodeNet.Tests.Easy.LeetMath 5 | { 6 | public sealed class FizzBuzz_412_test 7 | { 8 | [Theory, ClassData(typeof(FizzBuzzTestData))] 9 | public void CheckSimple(int inputData, List expected) 10 | { 11 | var solver = new FizzBuzz_412(); 12 | 13 | Assert.True(Enumerable.SequenceEqual(expected, solver.FizzBuzzSimple(inputData))); 14 | } 15 | 16 | [Theory, ClassData(typeof(FizzBuzzTestData))] 17 | public void CheckDelegate(int inputData, List expected) 18 | { 19 | var solver = new FizzBuzz_412(); 20 | 21 | Assert.True(Enumerable.SequenceEqual(expected, solver.FizzBuzzDelegate(inputData))); 22 | } 23 | } 24 | 25 | public sealed class FizzBuzzTestData : IEnumerable 26 | { 27 | public IEnumerator GetEnumerator() 28 | { 29 | yield return new object[] 30 | { 31 | 3, 32 | new List{ "1", "2", "Fizz" } 33 | }; 34 | 35 | yield return new object[] 36 | { 37 | 5, 38 | new List{ "1", "2", "Fizz", "4", "Buzz" } 39 | }; 40 | 41 | yield return new object[] 42 | { 43 | 15, 44 | new List{ "1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz" } 45 | }; 46 | } 47 | 48 | IEnumerator IEnumerable.GetEnumerator() 49 | { 50 | return GetEnumerator(); 51 | } 52 | } 53 | } -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/LeetMath/HappyNumber_202_test.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using LeetCodeNet.Easy.LeetMath; 3 | 4 | namespace LeetCodeNet.Tests.Easy.LeetMath 5 | { 6 | public sealed class HappyNumber_202_test 7 | { 8 | [Theory, ClassData(typeof(HappyNumberTestData))] 9 | public void CheckSimple(int inputData, bool expected) 10 | { 11 | var solver = new HappyNumber_202(); 12 | var result = solver.IsHappy(inputData); 13 | 14 | Assert.Equal(expected, result); 15 | } 16 | } 17 | 18 | public sealed class HappyNumberTestData : IEnumerable 19 | { 20 | public IEnumerator GetEnumerator() 21 | { 22 | //// Explanation: 23 | /// 12 + 92 = 82 24 | /// 82 + 22 = 68 25 | /// 62 + 82 = 100 26 | /// 12 + 02 + 02 = 1 27 | yield return new object[] 28 | { 29 | 19, 30 | true 31 | }; 32 | 33 | yield return new object[] 34 | { 35 | 2, 36 | false 37 | }; 38 | } 39 | 40 | IEnumerator IEnumerable.GetEnumerator() 41 | { 42 | return GetEnumerator(); 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/LeetMath/LargestPerimeterTriangle_976_test.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using LeetCodeNet.Easy.LeetMath; 3 | 4 | namespace LeetCodeNet.Tests.Easy.LeetMath 5 | { 6 | public sealed class LargestPerimeterTriangle_976_test 7 | { 8 | [Theory, ClassData(typeof(LargestPerimeterTriangleTestData))] 9 | public void CheckSimple(int[] inputData, int expected) 10 | { 11 | var solver = new LargestPerimeterTriangle_976(); 12 | var result = solver.LargestPerimeter(inputData); 13 | 14 | Assert.Equal(expected, result); 15 | } 16 | } 17 | 18 | public sealed class LargestPerimeterTriangleTestData : IEnumerable 19 | { 20 | public IEnumerator GetEnumerator() 21 | { 22 | //// Explanation: You can form a triangle with three side lengths: 1, 2, and 2. 23 | yield return new object[] 24 | { 25 | new[] {2,1,2}, 26 | 5 27 | }; 28 | 29 | ////You cannot use the side lengths 1, 1, and 2 to form a triangle. 30 | /// You cannot use the side lengths 1, 1, and 10 to form a triangle. 31 | /// You cannot use the side lengths 1, 2, and 10 to form a triangle. 32 | /// As we cannot use any three side lengths to form a triangle of non-zero area, we return 0. 33 | yield return new object[] 34 | { 35 | new[] {1,2,1,10}, 36 | 0 37 | }; 38 | } 39 | 40 | IEnumerator IEnumerable.GetEnumerator() 41 | { 42 | return GetEnumerator(); 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/LeetMath/PalindromeNumber_9_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Easy.LeetMath; 2 | using System.Collections; 3 | 4 | namespace LeetCodeNet.Tests.Easy.LeetMath 5 | { 6 | public sealed class PalindromeNumber_9_test 7 | { 8 | [Theory, ClassData(typeof(PalindromeNumberTestData))] 9 | public void CheckSimple(int inputData, bool expected) 10 | { 11 | var solver = new PalindromeNumber_9(); 12 | 13 | Assert.Equal(expected, solver.IsPalindrome(inputData)); 14 | } 15 | } 16 | 17 | public sealed class PalindromeNumberTestData : IEnumerable 18 | { 19 | public IEnumerator GetEnumerator() 20 | { 21 | //// Explanation: 121 reads as 121 from left to right and from right to left. 22 | yield return new object[] 23 | { 24 | 121, 25 | true 26 | }; 27 | 28 | //// Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. 29 | yield return new object[] 30 | { 31 | -121, 32 | false 33 | }; 34 | 35 | //// Explanation: Reads 01 from right to left. Therefore it is not a palindrome. 36 | yield return new object[] 37 | { 38 | 10, 39 | false 40 | }; 41 | } 42 | 43 | IEnumerator IEnumerable.GetEnumerator() 44 | { 45 | return GetEnumerator(); 46 | } 47 | } 48 | } -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/LeetMath/RomanToInteger_13_test.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using LeetCodeNet.Easy.LeetMath; 3 | 4 | namespace LeetCodeNet.Tests.Easy.LeetMath 5 | { 6 | public sealed class RomanToInteger_13_test 7 | { 8 | [Theory, ClassData(typeof(RomanToIntegerTestData))] 9 | public void CheckSimple(string inputData, int expected) 10 | { 11 | var solver = new RomanToInteger_13(); 12 | var result = solver.RomanToInt(inputData); 13 | 14 | Assert.Equal(expected, result); 15 | } 16 | } 17 | 18 | public sealed class RomanToIntegerTestData : IEnumerable 19 | { 20 | public IEnumerator GetEnumerator() 21 | { 22 | yield return new object[] 23 | { 24 | "III", 25 | 3 26 | }; 27 | 28 | yield return new object[] 29 | { 30 | "IV", 31 | 4 32 | }; 33 | 34 | //// Explanation: L = 50, V= 5, III = 3. 35 | yield return new object[] 36 | { 37 | "LVIII", 38 | 58 39 | }; 40 | 41 | //// Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. 42 | yield return new object[] 43 | { 44 | "MCMXCIV", 45 | 1994 46 | }; 47 | } 48 | 49 | IEnumerator IEnumerable.GetEnumerator() 50 | { 51 | return GetEnumerator(); 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/LeetMath/SignoftheProductofanArray_1822_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Easy.LeetMath; 2 | using System.Collections; 3 | 4 | namespace LeetCodeNet.Tests.Easy.LeetMath 5 | { 6 | public sealed class SignoftheProductofanArray_1822_test 7 | { 8 | [Theory, ClassData(typeof(SignoftheProductofanArrayTestData))] 9 | public void CheckSimple(int[] inputData, int expected) 10 | { 11 | var solver = new SignoftheProductofanArray_1822(); 12 | 13 | Assert.Equal(expected, solver.ArraySign(inputData)); 14 | } 15 | } 16 | 17 | public sealed class SignoftheProductofanArrayTestData : IEnumerable 18 | { 19 | public IEnumerator GetEnumerator() 20 | { 21 | yield return new object[] 22 | { 23 | new[]{ -1, -2, -3, -4, 3, 2, 1 }, 24 | 1 25 | }; 26 | 27 | yield return new object[] 28 | { 29 | new[]{ 1,5,0,2,-3 }, 30 | 0 31 | }; 32 | 33 | yield return new object[] 34 | { 35 | new[]{ -1,1,-1,1,-1 }, 36 | -1 37 | }; 38 | } 39 | 40 | IEnumerator IEnumerable.GetEnumerator() 41 | { 42 | return GetEnumerator(); 43 | } 44 | } 45 | } -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/LeetMath/SqrtX_69_test.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using LeetCodeNet.Easy.LeetMath; 3 | 4 | namespace LeetCodeNet.Tests.Easy.LeetMath 5 | { 6 | public sealed class SqrtX_69_test 7 | { 8 | [Theory, ClassData(typeof(SqrtXTestData))] 9 | public void Check(int inputData, int expected) 10 | { 11 | var solver = new SqrtX_69(); 12 | var result = solver.MySqrt(inputData); 13 | 14 | Assert.Equal(expected, result); 15 | } 16 | } 17 | 18 | public sealed class SqrtXTestData : IEnumerable 19 | { 20 | public IEnumerator GetEnumerator() 21 | { 22 | yield return new object[] 23 | { 24 | 4, 25 | 2 26 | }; 27 | 28 | yield return new object[] 29 | { 30 | 0, 31 | 0 32 | }; 33 | 34 | //// Explanation: The square root of 8 is 2.82842..., and since we round it down to the nearest integer, 2 is returned. 35 | yield return new object[] 36 | { 37 | 8, 38 | 2 39 | }; 40 | 41 | yield return new object[] 42 | { 43 | 9, 44 | 3 45 | }; 46 | 47 | yield return new object[] 48 | { 49 | 2147395599, 50 | 46339 51 | }; 52 | } 53 | 54 | IEnumerator IEnumerable.GetEnumerator() 55 | { 56 | return GetEnumerator(); 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/LeetMath/SubtracttheProductandSumofDigitsofanInteger_1281_test.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using LeetCodeNet.Easy.LeetMath; 3 | 4 | namespace LeetCodeNet.Tests.Easy.LeetMath 5 | { 6 | public sealed class SubtracttheProductandSumofDigitsofanInteger_1281_test 7 | { 8 | [Theory, ClassData(typeof(SubtracttheProductandSumofDigitsofanIntegerTestData))] 9 | public void CheckSimple(int inputData, int expected) 10 | { 11 | var solver = new SubtracttheProductandSumofDigitsofanInteger_1281(); 12 | var result = solver.SubtractProductAndSum(inputData); 13 | 14 | Assert.Equal(expected, result); 15 | } 16 | } 17 | 18 | public sealed class SubtracttheProductandSumofDigitsofanIntegerTestData : IEnumerable 19 | { 20 | public IEnumerator GetEnumerator() 21 | { 22 | //// Product of digits = 2 * 3 * 4 = 24 23 | /// Sum of digits = 2 + 3 + 4 = 9 24 | /// Result = 24 - 9 = 15 25 | yield return new object[] 26 | { 27 | 234, 28 | 15 29 | }; 30 | 31 | //// Product of digits = 4 * 4 * 2 * 1 = 32 32 | /// Sum of digits = 4 + 4 + 2 + 1 = 11 33 | /// Result = 32 - 11 = 21 34 | yield return new object[] 35 | { 36 | 4421, 37 | 21 38 | }; 39 | } 40 | 41 | IEnumerator IEnumerable.GetEnumerator() 42 | { 43 | return GetEnumerator(); 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/LeetMath/ValidPerfectSquare_367_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Easy.LeetMath; 2 | using System.Collections; 3 | 4 | namespace LeetCodeNet.Tests.Easy.LeetMath 5 | { 6 | public sealed class ValidPerfectSquare_367_test 7 | { 8 | [Theory, ClassData(typeof(ValidPerfectSquareTestData))] 9 | public void CheckSimple(int inputData, bool expected) 10 | { 11 | var solver = new ValidPerfectSquare_367(); 12 | 13 | Assert.Equal(expected, solver.IsPerfectSquare(inputData)); 14 | } 15 | } 16 | 17 | public sealed class ValidPerfectSquareTestData : IEnumerable 18 | { 19 | public IEnumerator GetEnumerator() 20 | { 21 | yield return new object[] 22 | { 23 | 16, 24 | true 25 | }; 26 | 27 | yield return new object[] 28 | { 29 | 14, 30 | false 31 | }; 32 | 33 | yield return new object[] 34 | { 35 | 808201, 36 | true 37 | }; 38 | } 39 | 40 | IEnumerator IEnumerable.GetEnumerator() 41 | { 42 | return GetEnumerator(); 43 | } 44 | } 45 | } -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/LinkedList/MiddleoftheLinkedList_876_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.DataStructs; 2 | using LeetCodeNet.Easy.LinkedList; 3 | using System.Collections; 4 | 5 | namespace LeetCodeNet.Tests.Easy.LinkedList 6 | { 7 | public sealed class MiddleoftheLinkedList_876_test 8 | { 9 | [Theory, ClassData(typeof(MiddleoftheLinkedListTestData))] 10 | public void Check(ListNode inputData, int expected) 11 | { 12 | var solver = new MiddleoftheLinkedList_876(); 13 | 14 | Assert.Equal(expected, solver.MiddleNode(inputData)?.val); 15 | } 16 | } 17 | 18 | public sealed class MiddleoftheLinkedListTestData : IEnumerable 19 | { 20 | public IEnumerator GetEnumerator() 21 | { 22 | yield return new object[] 23 | { 24 | new ListNode(1){next = new ListNode(2){next = new ListNode(3){next = new ListNode(4){next = new ListNode(5)}}}}, 25 | 3 26 | }; 27 | 28 | yield return new object[] 29 | { 30 | new ListNode(1){next = new ListNode(2){next = new ListNode(3){next = new ListNode(4){next = new ListNode(5) { next = new ListNode(6)} }}}}, 31 | 4 32 | }; 33 | } 34 | 35 | IEnumerator IEnumerable.GetEnumerator() 36 | { 37 | return GetEnumerator(); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/LinkedList/ReverseLinkedList_206_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.DataStructs; 2 | using LeetCodeNet.Easy.LinkedList; 3 | using System.Collections; 4 | 5 | namespace LeetCodeNet.Tests.Easy.LinkedList 6 | { 7 | public sealed class ReverseLinkedList_206_test 8 | { 9 | [Theory, ClassData(typeof(ReverseLinkedListTestData))] 10 | public void Check(ListNode inputData, ListNode expected) 11 | { 12 | var solver = new ReverseLinkedList_206(); 13 | var result = solver.ReverseList(inputData); 14 | 15 | Assert.True(result.Equals(expected)); 16 | } 17 | } 18 | 19 | public sealed class ReverseLinkedListTestData : IEnumerable 20 | { 21 | public IEnumerator GetEnumerator() 22 | { 23 | yield return new object[] 24 | { 25 | new ListNode(1){next = new ListNode(2){next = new ListNode(3){next = new ListNode(4){next = new ListNode(5)}}}}, 26 | new ListNode(5){next = new ListNode(4){next = new ListNode(3){next = new ListNode(2){next = new ListNode(1)}}}} 27 | }; 28 | 29 | yield return new object[] 30 | { 31 | new ListNode(1){next = new ListNode(2)}, 32 | new ListNode(2){next = new ListNode(1)} 33 | }; 34 | 35 | yield return new object[] 36 | { 37 | new ListNode(1), 38 | new ListNode(1) 39 | }; 40 | } 41 | 42 | IEnumerator IEnumerable.GetEnumerator() 43 | { 44 | return GetEnumerator(); 45 | } 46 | } 47 | } -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/Strings/CountBinarySubstrings_696_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Easy.Strings; 2 | using System.Collections; 3 | 4 | namespace LeetCodeNet.Tests.Easy.Strings 5 | { 6 | public sealed class LetterCasePermutation_784_test 7 | { 8 | [Theory, ClassData(typeof(CountBinarySubstringsTestData))] 9 | public void Check(string inputData, int expected) 10 | { 11 | var solver = new CountBinarySubstrings_696(); 12 | 13 | Assert.Equal(expected, solver.CountBinarySubstrings(inputData)); 14 | } 15 | } 16 | 17 | public sealed class CountBinarySubstringsTestData : IEnumerable 18 | { 19 | public IEnumerator GetEnumerator() 20 | { 21 | //// Explanation: There are 6 substrings that have equal number of consecutive 1's and 0's: "0011", "01", "1100", "10", "0011", and "01". 22 | /// Notice that some of these substrings repeat and are counted the number of times they occur. 23 | /// Also, "00110011" is not a valid substring because all the 0's (and 1's) are not grouped together. 24 | yield return new object[] 25 | { 26 | "00110011", 27 | 6 28 | }; 29 | 30 | //// Explanation: There are 4 substrings: "10", "01", "10", "01" that have equal number of consecutive 1's and 0's. 31 | yield return new object[] 32 | { 33 | "10101", 34 | 4 35 | }; 36 | } 37 | 38 | IEnumerator IEnumerable.GetEnumerator() 39 | { 40 | return GetEnumerator(); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/Strings/DecryptStringfromAlphabettoIntegerMapping_1309_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Easy.Strings; 2 | using System.Collections; 3 | 4 | namespace LeetCodeNet.Tests.Easy.Strings 5 | { 6 | public sealed class DecryptStringfromAlphabettoIntegerMapping_1309_test 7 | { 8 | [Theory, ClassData(typeof(DecryptStringfromAlphabettoIntegerMappingTestData))] 9 | public void Check(string inputData, string expected) 10 | { 11 | var solver = new DecryptStringfromAlphabettoIntegerMapping_1309(); 12 | 13 | Assert.Equal(expected, solver.FreqAlphabets(inputData)); 14 | } 15 | } 16 | 17 | public sealed class DecryptStringfromAlphabettoIntegerMappingTestData : IEnumerable 18 | { 19 | public IEnumerator GetEnumerator() 20 | { 21 | yield return new object[] 22 | { 23 | "10#11#12", 24 | "jkab" 25 | }; 26 | 27 | yield return new object[] 28 | { 29 | "1326#", 30 | "acz" 31 | }; 32 | } 33 | 34 | IEnumerator IEnumerable.GetEnumerator() 35 | { 36 | return GetEnumerator(); 37 | } 38 | } 39 | } -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/Strings/FirstUniqueCharacterinaString_387_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Easy.Strings; 2 | using System.Collections; 3 | 4 | namespace LeetCodeNet.Tests.Easy.Strings 5 | { 6 | public sealed class FirstUniqueCharacterinaString_387_test 7 | { 8 | [Theory, ClassData(typeof(FirstUniqueCharacterinaStringTestData))] 9 | public void CheckArray(string inputData, int expected) 10 | { 11 | var solver = new FirstUniqueCharacterinaString_387(); 12 | 13 | Assert.Equal(expected, solver.FirstUniqCharArray(inputData)); 14 | } 15 | } 16 | 17 | public sealed class FirstUniqueCharacterinaStringTestData : IEnumerable 18 | { 19 | public IEnumerator GetEnumerator() 20 | { 21 | yield return new object[] 22 | { 23 | "leetcode", 24 | 0 25 | }; 26 | 27 | yield return new object[] 28 | { 29 | "loveleetcode", 30 | 2 31 | }; 32 | 33 | yield return new object[] 34 | { 35 | "aabb", 36 | -1 37 | }; 38 | } 39 | 40 | IEnumerator IEnumerable.GetEnumerator() 41 | { 42 | return GetEnumerator(); 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/Strings/IsSubsequence_392_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Easy.Strings; 2 | using System.Collections; 3 | 4 | namespace LeetCodeNet.Tests.Easy.Strings 5 | { 6 | public sealed class IsSubsequence_392_test 7 | { 8 | [Theory, ClassData(typeof(IsSubsequenceTestData))] 9 | public void Check(string inputData1, string inputData2, bool expected) 10 | { 11 | var solver = new IsSubsequence_392(); 12 | 13 | Assert.Equal(expected, solver.IsSubsequence(inputData1, inputData2)); 14 | } 15 | } 16 | 17 | public sealed class IsSubsequenceTestData : IEnumerable 18 | { 19 | public IEnumerator GetEnumerator() 20 | { 21 | yield return new object[] 22 | { 23 | "abc", 24 | "ahbgdc", 25 | true 26 | }; 27 | 28 | yield return new object[] 29 | { 30 | "axc", 31 | "ahbgdc", 32 | false 33 | }; 34 | 35 | yield return new object[] 36 | { 37 | "aaaaaa", 38 | "bbaaaa", 39 | false 40 | }; 41 | 42 | } 43 | 44 | IEnumerator IEnumerable.GetEnumerator() 45 | { 46 | return GetEnumerator(); 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/Strings/IsomorphicStrings_205_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Easy.Strings; 2 | using System.Collections; 3 | 4 | namespace LeetCodeNet.Tests.Easy.Strings 5 | { 6 | public sealed class IsomorphicStrings_205_test 7 | { 8 | [Theory, ClassData(typeof(IsomorphicStringsTestData))] 9 | public void Check(string inputData1, string inputData2, bool expected) 10 | { 11 | var solver = new IsomorphicStrings_205(); 12 | 13 | Assert.Equal(expected, solver.IsIsomorphic(inputData1, inputData2)); 14 | } 15 | } 16 | 17 | public sealed class IsomorphicStringsTestData : IEnumerable 18 | { 19 | public IEnumerator GetEnumerator() 20 | { 21 | yield return new object[] 22 | { 23 | "egg", 24 | "add", 25 | true 26 | }; 27 | 28 | yield return new object[] 29 | { 30 | "foo", 31 | "bar", 32 | false 33 | }; 34 | 35 | yield return new object[] 36 | { 37 | "paper", 38 | "title", 39 | true 40 | }; 41 | 42 | yield return new object[] 43 | { 44 | "badc", 45 | "baba", 46 | false 47 | }; 48 | 49 | yield return new object[] 50 | { 51 | "13", 52 | "42", 53 | true 54 | }; 55 | } 56 | 57 | IEnumerator IEnumerable.GetEnumerator() 58 | { 59 | return GetEnumerator(); 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/Strings/LexicographicallySmallestPalindrome_2697_test.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using LeetCodeNet.Easy.Strings; 3 | 4 | namespace LeetCodeNet.Tests.Easy.Strings 5 | { 6 | public sealed class LexicographicallySmallestPalindrome_2697_test 7 | { 8 | [Theory, ClassData(typeof(LexicographicallySmallestPalindromeTestData))] 9 | public void Check(string inputData, int expected) 10 | { 11 | var solver = new LexicographicallySmallestPalindrome_2697(); 12 | var result = solver.MinLength(inputData); 13 | 14 | Assert.Equal(expected, result); 15 | } 16 | } 17 | 18 | public sealed class LexicographicallySmallestPalindromeTestData : IEnumerable 19 | { 20 | public IEnumerator GetEnumerator() 21 | { 22 | yield return new object[] 23 | { 24 | "ABFCACDB", 25 | 2 26 | }; 27 | 28 | yield return new object[] 29 | { 30 | "ACBBD", 31 | 5 32 | }; 33 | 34 | 35 | } 36 | 37 | IEnumerator IEnumerable.GetEnumerator() 38 | { 39 | return GetEnumerator(); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/Strings/LongestCommonPrefix_14_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Easy.Strings; 2 | using System.Collections; 3 | 4 | namespace LeetCodeNet.Tests.Easy.Strings 5 | { 6 | public sealed class LongestCommonPrefix_14_test 7 | { 8 | [Theory, ClassData(typeof(LongestCommonPrefixTestData))] 9 | public void CheckLoop(string[] inputData, string expected) 10 | { 11 | var solver = new LongestCommonPrefix_14(); 12 | 13 | Assert.Equal(expected, solver.LongestCommonPrefixLoop(inputData)); 14 | } 15 | 16 | [Theory, ClassData(typeof(LongestCommonPrefixTestData))] 17 | public void CheckSort(string[] inputData, string expected) 18 | { 19 | var solver = new LongestCommonPrefix_14(); 20 | 21 | Assert.Equal(expected, solver.LongestCommonPrefixLoop(inputData)); 22 | } 23 | } 24 | 25 | public sealed class LongestCommonPrefixTestData : IEnumerable 26 | { 27 | public IEnumerator GetEnumerator() 28 | { 29 | yield return new object[] 30 | { 31 | new[] { "flower","flow","flight"}, 32 | "fl" 33 | }; 34 | 35 | //// Explanation: There is no common prefix among the input strings. 36 | yield return new object[] 37 | { 38 | new[] { "dog","racecar","car"}, 39 | string.Empty 40 | }; 41 | } 42 | 43 | IEnumerator IEnumerable.GetEnumerator() 44 | { 45 | return GetEnumerator(); 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/Strings/LongestPalindrome_409_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Easy.Strings; 2 | using System.Collections; 3 | 4 | namespace LeetCodeNet.Tests.Easy.Strings 5 | { 6 | public sealed class LongestPalindrome_409_test 7 | { 8 | [Theory, ClassData(typeof(LongestPalindromeTestData))] 9 | public void CheckSimple(string inputData, int expected) 10 | { 11 | var solver = new LongestPalindrome_409(); 12 | 13 | Assert.Equal(expected, solver.LongestPalindromeSimple(inputData)); 14 | } 15 | 16 | [Theory, ClassData(typeof(LongestPalindromeTestData))] 17 | public void CheckOptimized(string inputData, int expected) 18 | { 19 | var solver = new LongestPalindrome_409(); 20 | 21 | Assert.Equal(expected, solver.LongestPalindromeOptimized(inputData)); 22 | } 23 | } 24 | 25 | public sealed class LongestPalindromeTestData : IEnumerable 26 | { 27 | public IEnumerator GetEnumerator() 28 | { 29 | //// Explanation: One longest palindrome that can be built is "dccaccd", whose length is 7. 30 | yield return new object[] 31 | { 32 | "abccccdd", 33 | 7 34 | }; 35 | 36 | yield return new object[] 37 | { 38 | "a", 39 | 1 40 | }; 41 | } 42 | 43 | IEnumerator IEnumerable.GetEnumerator() 44 | { 45 | return GetEnumerator(); 46 | } 47 | } 48 | } -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/Strings/MergeStringsAlternately_1768_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Easy.Strings; 2 | using System.Collections; 3 | 4 | namespace LeetCodeNet.Tests.Easy.Strings 5 | { 6 | public sealed class MergeStringsAlternately_1768_test 7 | { 8 | [Theory, ClassData(typeof(MergeStringsAlternatelyTestData))] 9 | public void Check(string inputData1, string inputData2, string expected) 10 | { 11 | var solver = new MergeStringsAlternately_1768(); 12 | 13 | Assert.Equal(expected, solver.MergeAlternately(inputData1, inputData2)); 14 | } 15 | } 16 | 17 | public sealed class MergeStringsAlternatelyTestData : IEnumerable 18 | { 19 | public IEnumerator GetEnumerator() 20 | { 21 | //// Explanation: The merged string will be merged as so: 22 | /// word1: a b c 23 | /// word2: p q r 24 | /// merged: a p b q c r 25 | yield return new object[] 26 | { 27 | "abc", 28 | "pqr", 29 | "apbqcr" 30 | }; 31 | 32 | //// Notice that as word2 is longer, "rs" is appended to the end. 33 | yield return new object[] 34 | { 35 | "ab", 36 | "pqrs", 37 | "apbqrs" 38 | }; 39 | 40 | yield return new object[] 41 | { 42 | "abcd", 43 | "pq", 44 | "apbqcd" 45 | }; 46 | } 47 | 48 | IEnumerator IEnumerable.GetEnumerator() 49 | { 50 | return GetEnumerator(); 51 | } 52 | } 53 | } -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/Strings/MinimizeStringLength_2716_test.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using LeetCodeNet.Easy.Strings; 3 | 4 | namespace LeetCodeNet.Tests.Easy.Strings 5 | { 6 | public sealed class MinimizeStringLength_2716_test 7 | { 8 | [Theory, ClassData(typeof(MinimizeStringLengthTestData))] 9 | public void CheckHash(string inputData, int expected) 10 | { 11 | var solver = new MinimizeStringLength_2716(); 12 | 13 | Assert.Equal(expected, solver.MinimizedStringLengthHash(inputData)); 14 | } 15 | 16 | [Theory, ClassData(typeof(MinimizeStringLengthTestData))] 17 | public void CheckLinq(string inputData, int expected) 18 | { 19 | var solver = new MinimizeStringLength_2716(); 20 | 21 | Assert.Equal(expected, solver.MinimizedStringLengthLinq(inputData)); 22 | } 23 | } 24 | 25 | public sealed class MinimizeStringLengthTestData : IEnumerable 26 | { 27 | public IEnumerator GetEnumerator() 28 | { 29 | yield return new object[] 30 | { 31 | "aaabc", 32 | 3 33 | }; 34 | 35 | yield return new object[] 36 | { 37 | "cbbd", 38 | 3 39 | }; 40 | 41 | yield return new object[] 42 | { 43 | "dddaaa", 44 | 2 45 | }; 46 | } 47 | 48 | IEnumerator IEnumerable.GetEnumerator() 49 | { 50 | return GetEnumerator(); 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/Strings/MinimumStringLengthAfterRemovingSubstrings_2696_test.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using LeetCodeNet.Easy.Strings; 3 | 4 | namespace LeetCodeNet.Tests.Easy.Strings 5 | { 6 | public sealed class MinimumStringLengthAfterRemovingSubstrings_2696_test 7 | { 8 | [Theory, ClassData(typeof(MinimumStringLengthAfterRemovingSubstringsTestData))] 9 | public void Check(string inputData, string expected) 10 | { 11 | var solver = new MinimumStringLengthAfterRemovingSubstrings_2696(); 12 | var result = solver.MakeSmallestPalindrome(inputData); 13 | 14 | Assert.Equal(expected, result); 15 | } 16 | } 17 | 18 | public sealed class MinimumStringLengthAfterRemovingSubstringsTestData : IEnumerable 19 | { 20 | public IEnumerator GetEnumerator() 21 | { 22 | yield return new object[] 23 | { 24 | "egcfe", 25 | "efcfe" 26 | }; 27 | 28 | yield return new object[] 29 | { 30 | "abcd", 31 | "abba" 32 | }; 33 | 34 | yield return new object[] 35 | { 36 | "seven", 37 | "neven" 38 | }; 39 | } 40 | 41 | IEnumerator IEnumerable.GetEnumerator() 42 | { 43 | return GetEnumerator(); 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/Strings/MinimumTimetoTypeWordUsingSpecialTypewriter_1974_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Easy.Strings; 2 | using System.Collections; 3 | 4 | namespace LeetCodeNet.Tests.Easy.Strings 5 | { 6 | public sealed class MinimumTimetoTypeWordUsingSpecialTypewriter_1974_test 7 | { 8 | [Theory, ClassData(typeof(MinimumTimetoTypeWordUsingSpecialTypewriterTestData))] 9 | public void Check(string inputData, int expected) 10 | { 11 | var solver = new MinimumTimetoTypeWordUsingSpecialTypewriter_1974(); 12 | 13 | Assert.Equal(expected, solver.MinTimeToType(inputData)); 14 | } 15 | } 16 | 17 | public sealed class MinimumTimetoTypeWordUsingSpecialTypewriterTestData : IEnumerable 18 | { 19 | public IEnumerator GetEnumerator() 20 | { 21 | //// Explanation: For example, swap the first character with the last character of s2 to make "bank". 22 | yield return new object[] 23 | { 24 | "abc", 25 | 5 26 | }; 27 | 28 | //// Explanation: It is impossible to make them equal with one string swap. 29 | yield return new object[] 30 | { 31 | "bza", 32 | 7 33 | }; 34 | 35 | //// Explanation: The two strings are already equal, so no string swap operation is required. 36 | yield return new object[] 37 | { 38 | "zjpc", 39 | 34 40 | }; 41 | } 42 | 43 | IEnumerator IEnumerable.GetEnumerator() 44 | { 45 | return GetEnumerator(); 46 | } 47 | } 48 | } -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/Strings/RansomNote_383_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Easy.Strings; 2 | using System.Collections; 3 | 4 | namespace LeetCodeNet.Tests.Easy.Strings 5 | { 6 | public sealed class RansomNote_383_test 7 | { 8 | [Theory, ClassData(typeof(RansomNoteTestData))] 9 | public void CheckHash(string inputData1, string inputData2, bool expected) 10 | { 11 | var solver = new RansomNote_383(); 12 | 13 | Assert.Equal(expected, solver.CanConstructHash(inputData1, inputData2)); 14 | } 15 | 16 | [Theory, ClassData(typeof(RansomNoteTestData))] 17 | public void CheckArray(string inputData1, string inputData2, bool expected) 18 | { 19 | var solver = new RansomNote_383(); 20 | 21 | Assert.Equal(expected, solver.CanConstructArray(inputData1, inputData2)); 22 | } 23 | } 24 | 25 | public sealed class RansomNoteTestData : IEnumerable 26 | { 27 | public IEnumerator GetEnumerator() 28 | { 29 | yield return new object[] 30 | { 31 | "a", 32 | "b", 33 | false 34 | }; 35 | 36 | yield return new object[] 37 | { 38 | "aa", 39 | "ab", 40 | false 41 | }; 42 | 43 | yield return new object[] 44 | { 45 | "aa", 46 | "aab", 47 | true 48 | }; 49 | } 50 | 51 | IEnumerator IEnumerable.GetEnumerator() 52 | { 53 | return GetEnumerator(); 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/Strings/ReplaceAllDigitswithCharacters_1844_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Easy.Strings; 2 | using System.Collections; 3 | 4 | namespace LeetCodeNet.Tests.Easy.Strings 5 | { 6 | public sealed class ReplaceAllDigitswithCharacters_1844_test 7 | { 8 | [Theory, ClassData(typeof(ReplaceAllDigitswithCharactersTestData))] 9 | public void Check(string inputData, string expected) 10 | { 11 | var solver = new ReplaceAllDigitswithCharacters_1844(); 12 | 13 | Assert.Equal(expected, solver.ReplaceDigitsWithOriginal(inputData)); 14 | Assert.Equal(expected, solver.ReplaceDigitsWithExceed(inputData)); 15 | } 16 | 17 | [Fact] 18 | public void CheckExceed() 19 | { 20 | var solver = new ReplaceAllDigitswithCharacters_1844(); 21 | 22 | Assert.Equal("zacdef", solver.ReplaceDigitsWithExceed("z1c1e1")); 23 | } 24 | } 25 | 26 | public sealed class ReplaceAllDigitswithCharactersTestData : IEnumerable 27 | { 28 | public IEnumerator GetEnumerator() 29 | { 30 | //// Explanation: The digits are replaced as follows: 31 | /// -s[1]->shift('a', 1) = 'b' 32 | /// - s[3]->shift('c', 1) = 'd' 33 | /// - s[5]->shift('e', 1) = 'f' 34 | yield return new object[] 35 | { 36 | "a1c1e1", 37 | "abcdef" 38 | }; 39 | 40 | yield return new object[] 41 | { 42 | "a1b2c3d4e", 43 | "abbdcfdhe" 44 | }; 45 | } 46 | 47 | IEnumerator IEnumerable.GetEnumerator() 48 | { 49 | return GetEnumerator(); 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/Strings/ReverseString_344_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Easy.Strings; 2 | using System.Collections; 3 | 4 | namespace LeetCodeNet.Tests.Easy.Strings 5 | { 6 | public sealed class ReverseString_344_test 7 | { 8 | [Theory, ClassData(typeof(ReverseStringTestData))] 9 | public void Check(char[] inputData, char[] expected) 10 | { 11 | var solver = new ReverseString_344(); 12 | 13 | solver.ReverseString(inputData); 14 | Assert.Equal(expected, inputData); 15 | } 16 | } 17 | 18 | public sealed class ReverseStringTestData : IEnumerable 19 | { 20 | public IEnumerator GetEnumerator() 21 | { 22 | yield return new object[] 23 | { 24 | new[] {'h','e','l','l','o'}, 25 | new[] {'o','l','l','e','h'}, 26 | }; 27 | 28 | yield return new object[] 29 | { 30 | new[] {'H','a','n','n','a','h'}, 31 | new[] {'h','a','n','n','a','H'}, 32 | }; 33 | } 34 | 35 | IEnumerator IEnumerable.GetEnumerator() 36 | { 37 | return GetEnumerator(); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/Strings/ReverseVowelsofaString_345_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Easy.Strings; 2 | using System.Collections; 3 | 4 | namespace LeetCodeNet.Tests.Easy.Strings 5 | { 6 | public sealed class ReverseVowelsofaString_345_test 7 | { 8 | [Theory, ClassData(typeof(ReverseVowelsofaStringTestData))] 9 | public void CheckTwoPass(string inputData, string expected) 10 | { 11 | var solver = new ReverseVowelsofaString_345(); 12 | 13 | Assert.Equal(expected, solver.ReverseVowelsTwoPass(inputData)); 14 | } 15 | 16 | [Theory, ClassData(typeof(ReverseVowelsofaStringTestData))] 17 | public void CheckTwoPointers(string inputData, string expected) 18 | { 19 | var solver = new ReverseVowelsofaString_345(); 20 | 21 | Assert.Equal(expected, solver.ReverseVowelsTwoPointers(inputData)); 22 | } 23 | } 24 | 25 | public sealed class ReverseVowelsofaStringTestData : IEnumerable 26 | { 27 | public IEnumerator GetEnumerator() 28 | { 29 | yield return new object[] 30 | { 31 | "hello", 32 | "holle", 33 | }; 34 | 35 | yield return new object[] 36 | { 37 | "leetcode", 38 | "leotcede" 39 | }; 40 | } 41 | 42 | IEnumerator IEnumerable.GetEnumerator() 43 | { 44 | return GetEnumerator(); 45 | } 46 | } 47 | } -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/Strings/ReverseWordsinaStringIII_557_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Easy.Strings; 2 | using System.Collections; 3 | 4 | namespace LeetCodeNet.Tests.Easy.Strings 5 | { 6 | public sealed class ReverseWordsinaStringIII_557_test 7 | { 8 | [Theory, ClassData(typeof(ReverseWordsinaStringIIITestData))] 9 | public void CheckLinq(string inputData, string expected) 10 | { 11 | var solver = new ReverseWordsinaStringIII_557(); 12 | 13 | Assert.Equal(expected, solver.ReverseWordsLinq(inputData)); 14 | } 15 | 16 | [Theory, ClassData(typeof(ReverseWordsinaStringIIITestData))] 17 | public void CheckTwoPointers(string inputData, string expected) 18 | { 19 | var solver = new ReverseWordsinaStringIII_557(); 20 | 21 | Assert.Equal(expected, solver.ReverseStringTwoPointers(inputData)); 22 | } 23 | } 24 | 25 | public sealed class ReverseWordsinaStringIIITestData : IEnumerable 26 | { 27 | public IEnumerator GetEnumerator() 28 | { 29 | yield return new object[] 30 | { 31 | "Let's take LeetCode contest", 32 | "s'teL ekat edoCteeL tsetnoc" 33 | }; 34 | 35 | yield return new object[] 36 | { 37 | "God Ding", 38 | "doG gniD" 39 | }; 40 | } 41 | 42 | IEnumerator IEnumerable.GetEnumerator() 43 | { 44 | return GetEnumerator(); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/Strings/ToLowerCase_709_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Easy.Strings; 2 | using System.Collections; 3 | 4 | namespace LeetCodeNet.Tests.Easy.Strings 5 | { 6 | public sealed class ToLowerCase_709_test 7 | { 8 | [Theory, ClassData(typeof(ToLowerCaseTestData))] 9 | public void CheckHash(string inputData, string expected) 10 | { 11 | var solver = new ToLowerCase_709(); 12 | 13 | Assert.Equal(expected, solver.ToLowerCaseSimple(inputData)); 14 | } 15 | 16 | [Theory, ClassData(typeof(ToLowerCaseTestData))] 17 | public void CheckSorting(string inputData, string expected) 18 | { 19 | var solver = new ToLowerCase_709(); 20 | 21 | Assert.Equal(expected, solver.ToLowerCaseChar(inputData)); 22 | } 23 | } 24 | 25 | public sealed class ToLowerCaseTestData : IEnumerable 26 | { 27 | public IEnumerator GetEnumerator() 28 | { 29 | yield return new object[] 30 | { 31 | "Hello", 32 | "hello" 33 | }; 34 | 35 | yield return new object[] 36 | { 37 | "here", 38 | "here" 39 | }; 40 | 41 | yield return new object[] 42 | { 43 | "LOVELY", 44 | "lovely" 45 | }; 46 | } 47 | 48 | IEnumerator IEnumerable.GetEnumerator() 49 | { 50 | return GetEnumerator(); 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/Strings/ValidAnagram_242_test.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | 3 | namespace LeetCodeNet.Tests.Easy.Strings 4 | { 5 | public sealed class ValidAnagram_242_test 6 | { 7 | [Theory, ClassData(typeof(ValidAnagramTestData))] 8 | public void CheckHash(string inputWord1, string inputWord2, bool expected) 9 | { 10 | var solver = new ValidAnagram_242(); 11 | 12 | Assert.Equal(expected, solver.IsAnagramHash(inputWord1, inputWord2)); 13 | } 14 | 15 | [Theory, ClassData(typeof(ValidAnagramTestData))] 16 | public void CheckSorting(string inputWord1, string inputWord2, bool expected) 17 | { 18 | var solver = new ValidAnagram_242(); 19 | 20 | Assert.Equal(expected, solver.IsAnagramSorting(inputWord1, inputWord2)); 21 | } 22 | } 23 | 24 | public sealed class ValidAnagramTestData : IEnumerable 25 | { 26 | public IEnumerator GetEnumerator() 27 | { 28 | yield return new object[] 29 | { 30 | "anagram", 31 | "nagaram", 32 | true 33 | }; 34 | 35 | yield return new object[] 36 | { 37 | "rat", 38 | "car", 39 | false 40 | }; 41 | } 42 | 43 | IEnumerator IEnumerable.GetEnumerator() 44 | { 45 | return GetEnumerator(); 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/Strings/ValidPalindrome_125_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Easy.Strings; 2 | using System.Collections; 3 | 4 | namespace LeetCodeNet.Tests.Easy.Strings 5 | { 6 | public sealed class ValidPalindrome_125_test 7 | { 8 | [Theory, ClassData(typeof(ValidPalindromeTestData))] 9 | public void CheckLinqMethod(string inputData, bool expected) 10 | { 11 | var solver = new ValidPalindrome_125(); 12 | 13 | Assert.Equal(expected, solver.IsPalindromeLinq(inputData)); 14 | } 15 | 16 | [Theory, ClassData(typeof(ValidPalindromeTestData))] 17 | public void CheckPointersMethod(string inputData, bool expected) 18 | { 19 | var solver = new ValidPalindrome_125(); 20 | 21 | Assert.Equal(expected, solver.IsPalindromePointers(inputData)); 22 | } 23 | } 24 | 25 | public sealed class ValidPalindromeTestData : IEnumerable 26 | { 27 | public IEnumerator GetEnumerator() 28 | { 29 | //// Explanation: "amanaplanacanalpanama" is a palindrome. 30 | yield return new object[] 31 | { 32 | "A man, a plan, a canal: Panama", 33 | true 34 | }; 35 | 36 | //// Explanation: "raceacar" is not a palindrome. 37 | yield return new object[] 38 | { 39 | "race a car", 40 | false 41 | }; 42 | 43 | yield return new object[] 44 | { 45 | " ", 46 | true 47 | }; 48 | } 49 | 50 | IEnumerator IEnumerable.GetEnumerator() 51 | { 52 | return GetEnumerator(); 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/Strings/ValidParentheses_20_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Easy.Strings; 2 | using System.Collections; 3 | 4 | namespace LeetCodeNet.Tests.Easy.Strings 5 | { 6 | public sealed class ValidParentheses_20_test 7 | { 8 | [Theory, ClassData(typeof(ValidParenthesesTestData))] 9 | public void CheckSimple(string inputData, bool expected) 10 | { 11 | var solver = new ValidParentheses_20(); 12 | 13 | Assert.Equal(expected, solver.IsValidSimple(inputData)); 14 | } 15 | 16 | [Theory, ClassData(typeof(ValidParenthesesTestData))] 17 | public void CheckAnyChar(string inputData, bool expected) 18 | { 19 | var solver = new ValidParentheses_20(); 20 | 21 | Assert.Equal(expected, solver.IsValidAnyCharacters(inputData)); 22 | } 23 | } 24 | 25 | public sealed class ValidParenthesesTestData : IEnumerable 26 | { 27 | public IEnumerator GetEnumerator() 28 | { 29 | yield return new object[] 30 | { 31 | "()", 32 | true 33 | }; 34 | 35 | yield return new object[] 36 | { 37 | "()[]{}", 38 | true 39 | }; 40 | 41 | yield return new object[] 42 | { 43 | "(]", 44 | false 45 | }; 46 | 47 | yield return new object[] 48 | { 49 | "]", 50 | false 51 | }; 52 | } 53 | 54 | IEnumerator IEnumerable.GetEnumerator() 55 | { 56 | return GetEnumerator(); 57 | } 58 | } 59 | } -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/Trees/AverageofLevelsinBinaryTree_637_test.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using LeetCodeNet.DataStructs; 3 | using LeetCodeNet.Easy.Trees; 4 | 5 | namespace LeetCodeNet.Tests.Easy.Trees 6 | { 7 | public sealed class AverageofLevelsinBinaryTree_637_test 8 | { 9 | [Theory, ClassData(typeof(AverageofLevelsinBinaryTreeTestData))] 10 | public void Check(TreeNode inputData, List expected) 11 | { 12 | var solver = new AverageofLevelsinBinaryTree_637(); 13 | var result = solver.AverageOfLevels(inputData); 14 | 15 | Assert.True(expected.SequenceEqual(result)); 16 | } 17 | } 18 | 19 | public sealed class AverageofLevelsinBinaryTreeTestData : IEnumerable 20 | { 21 | public IEnumerator GetEnumerator() 22 | { 23 | //// Explanation: The average value of nodes on level 0 is 3, on level 1 is 14.5, and on level 2 is 11. 24 | /// Hence return [3, 14.5, 11]. 25 | yield return new object[] 26 | { 27 | new TreeNode(3, new TreeNode(9), new TreeNode(20, new TreeNode(15), new TreeNode(7))), 28 | new List(){3, 14.5, 11} 29 | }; 30 | 31 | yield return new object[] 32 | { 33 | new TreeNode(3, new TreeNode(9, new TreeNode(15), new TreeNode(7)), new TreeNode(20)), 34 | new List() {3, 14.5, 11} 35 | }; 36 | } 37 | 38 | IEnumerator IEnumerable.GetEnumerator() 39 | { 40 | return GetEnumerator(); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/Trees/ConvertSortedArraytoBinarySearchTree_108_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.DataStructs; 2 | using LeetCodeNet.Easy.Trees; 3 | using System.Collections; 4 | 5 | namespace LeetCodeNet.Tests.Easy.Trees 6 | { 7 | public sealed class ConvertSortedArraytoBinarySearchTree_108_test 8 | { 9 | [Theory, ClassData(typeof(ConvertSortedArraytoBinarySearchTreeTestData))] 10 | public void CheckRecursive(int[] inputData, TreeNode expected) 11 | { 12 | var solver = new ConvertSortedArraytoBinarySearchTree_108(); 13 | var result = solver.SortedArrayToBST(inputData); 14 | 15 | Assert.True(expected.Equals(result)); 16 | } 17 | } 18 | 19 | public sealed class ConvertSortedArraytoBinarySearchTreeTestData : IEnumerable 20 | { 21 | public IEnumerator GetEnumerator() 22 | { 23 | yield return new object[] 24 | { 25 | new[]{ -10, -3, 0, 5, 9 }, 26 | new TreeNode(0, new TreeNode(-10, right: new TreeNode(-3)), new TreeNode(5, right: new TreeNode(9))) 27 | }; 28 | } 29 | 30 | IEnumerator IEnumerable.GetEnumerator() 31 | { 32 | return GetEnumerator(); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/Trees/MaximumDepthofBinaryTree_104_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.DataStructs; 2 | using LeetCodeNet.Easy.Trees; 3 | using System.Collections; 4 | 5 | namespace LeetCodeNet.Tests.Easy.Trees 6 | { 7 | public sealed class MaximumDepthofBinaryTree_104_test 8 | { 9 | [Theory, ClassData(typeof(MaximumDepthofBinaryTreeTestData))] 10 | public void CheckRecursive(TreeNode inputData, int expected) 11 | { 12 | var solver = new MaximumDepthofBinaryTree_104(); 13 | var result = solver.MaxDepthRecursive(inputData); 14 | 15 | Assert.True(expected.Equals(result)); 16 | } 17 | 18 | [Theory, ClassData(typeof(MaximumDepthofBinaryTreeTestData))] 19 | public void CheckIterative(TreeNode inputData, int expected) 20 | { 21 | var solver = new MaximumDepthofBinaryTree_104(); 22 | var result = solver.MaxDepthIterative(inputData); 23 | 24 | Assert.True(expected.Equals(result)); 25 | } 26 | } 27 | 28 | public sealed class MaximumDepthofBinaryTreeTestData : IEnumerable 29 | { 30 | public IEnumerator GetEnumerator() 31 | { 32 | yield return new object[] 33 | { 34 | new TreeNode(3, left: new TreeNode(9), right: new TreeNode(20, left: new TreeNode(15), right: new TreeNode(9))), 35 | 3 36 | }; 37 | 38 | yield return new object[] 39 | { 40 | new TreeNode(1, right: new TreeNode(3)), 41 | 2 42 | }; 43 | } 44 | 45 | IEnumerator IEnumerable.GetEnumerator() 46 | { 47 | return GetEnumerator(); 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Easy/Trees/MergeTwoBinaryTrees_617_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.DataStructs; 2 | using LeetCodeNet.Easy.Trees; 3 | using System.Collections; 4 | 5 | namespace LeetCodeNet.Tests.Easy.Trees 6 | { 7 | public sealed class MergeTwoBinaryTrees_617_test 8 | { 9 | [Theory, ClassData(typeof(MergeTwoBinaryTreesTestData))] 10 | public void CheckRecursive(TreeNode inputData1, TreeNode inputData2, TreeNode expected) 11 | { 12 | var solver = new MergeTwoBinaryTrees_617(); 13 | var result = solver.MergeTreesRecursive(inputData1, inputData2); 14 | 15 | Assert.True(expected.Equals(result)); 16 | } 17 | } 18 | 19 | public sealed class MergeTwoBinaryTreesTestData : IEnumerable 20 | { 21 | public IEnumerator GetEnumerator() 22 | { 23 | yield return new object[] 24 | { 25 | new TreeNode(1, left: new TreeNode(3, left: new TreeNode(5)), right: new TreeNode(2)), 26 | new TreeNode(2, left: new TreeNode(1, right: new TreeNode(4)), right: new TreeNode(3, right: new TreeNode(7))), 27 | new TreeNode(3, left: new TreeNode(4, left: new TreeNode(5), new TreeNode(4)), right: new TreeNode(5, right: new TreeNode(7))) 28 | }; 29 | 30 | yield return new object[] 31 | { 32 | new TreeNode(1), 33 | new TreeNode(1, left:new TreeNode(2)), 34 | new TreeNode(2, left: new TreeNode(2)) 35 | }; 36 | } 37 | 38 | IEnumerator IEnumerable.GetEnumerator() 39 | { 40 | return GetEnumerator(); 41 | } 42 | } 43 | } -------------------------------------------------------------------------------- /LeetCodeNet.Tests/LeetCodeNet.Tests.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | net6.0 5 | enable 6 | enable 7 | 8 | false 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | runtime; build; native; contentfiles; analyzers; buildtransitive 17 | all 18 | 19 | 20 | runtime; build; native; contentfiles; analyzers; buildtransitive 21 | all 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Medium/Array/FindMinimuminRotatedSortedArray_153_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Medium.Array; 2 | using System.Collections; 3 | 4 | namespace LeetCodeNet.Tests.Medium.Array 5 | { 6 | public sealed class FindMinimuminRotatedSortedArray_153_test 7 | { 8 | [Theory, ClassData(typeof(FindMinimuminRotatedSortedArrayTestData))] 9 | public void Check(int[] inputData, int expected) 10 | { 11 | var solver = new FindMinimuminRotatedSortedArray_153(); 12 | 13 | Assert.Equal(expected, solver.FindMin(inputData)); 14 | } 15 | } 16 | 17 | public sealed class FindMinimuminRotatedSortedArrayTestData : IEnumerable 18 | { 19 | public IEnumerator GetEnumerator() 20 | { 21 | yield return new object[] 22 | { 23 | new[] { 3,4,5,1,2 }, 24 | 1 25 | }; 26 | 27 | yield return new object[] 28 | { 29 | new[] { 4,5,6,7,0,1,2 }, 30 | 0 31 | }; 32 | } 33 | 34 | IEnumerator IEnumerable.GetEnumerator() 35 | { 36 | return GetEnumerator(); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Medium/Array/FindtheValueofthePartition_2740_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Medium.Array; 2 | using System.Collections; 3 | 4 | namespace LeetCodeNet.Tests.Medium.Array 5 | { 6 | public sealed class FindtheValueofthePartition_2740_test 7 | { 8 | [Theory, ClassData(typeof(FindtheValueofthePartitionTestData))] 9 | public void Check(int[] inputData, int expected) 10 | { 11 | var solver = new FindtheValueofthePartition_2740(); 12 | 13 | Assert.Equal(expected, solver.MinPartitionValue(inputData)); 14 | } 15 | } 16 | 17 | public sealed class FindtheValueofthePartitionTestData : IEnumerable 18 | { 19 | public IEnumerator GetEnumerator() 20 | { 21 | yield return new object[] 22 | { 23 | new int[] {1,2,3,4}, 24 | 1 25 | }; 26 | 27 | yield return new object[] 28 | { 29 | new int[] {100, 1, 10}, 30 | 9 31 | }; 32 | 33 | yield return new object[] 34 | { 35 | new int[] {-2, -1, 10, 20 , 30}, 36 | 1 37 | }; 38 | } 39 | 40 | IEnumerator IEnumerable.GetEnumerator() 41 | { 42 | return GetEnumerator(); 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Medium/Array/FirstCompletelyPaintedRoworColumn_2661_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Medium.Array; 2 | using System.Collections; 3 | 4 | namespace LeetCodeNet.Tests.Medium.Array 5 | { 6 | public sealed class FirstCompletelyPaintedRoworColumn_2661_test 7 | { 8 | [Theory, ClassData(typeof(FirstCompletelyPaintedRoworColumnTestData))] 9 | public void Check(int[] inputData1, int[][] inputData2, int expected) 10 | { 11 | var solver = new FirstCompletelyPaintedRoworColumn_2661(); 12 | 13 | Assert.Equal(expected, solver.FirstCompleteIndex(inputData1, inputData2)); 14 | } 15 | } 16 | 17 | public sealed class FirstCompletelyPaintedRoworColumnTestData : IEnumerable 18 | { 19 | public IEnumerator GetEnumerator() 20 | { 21 | //// Explanation: The moves are shown in order, and both the first row and second column of the matrix become fully painted at arr[2]. 22 | yield return new object[] 23 | { 24 | new int[] { 1,3,4,2}, 25 | new int[][] { new[] {1, 4 }, new[]{ 2,3} }, 26 | 2 27 | }; 28 | 29 | yield return new object[] 30 | { 31 | new int[] { 2,8,7,4,1,3,5,6,9}, 32 | new int[][] { new[] { 3, 2, 5 }, new[]{ 1, 4 ,6 }, new[]{ 8, 7, 9 } }, 33 | 3 34 | }; 35 | } 36 | 37 | IEnumerator IEnumerable.GetEnumerator() 38 | { 39 | return GetEnumerator(); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Medium/Array/MaxConsecutiveOnes III_1004_test.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using LeetCodeNet.Medium.Array; 3 | 4 | namespace LeetCodeNet.Tests.Medium.Array 5 | { 6 | public sealed class MaxConsecutiveOnesIII_1004_test 7 | { 8 | [Theory, ClassData(typeof(MaxConsecutiveOnesIIITestData))] 9 | public void CheckSimple(int[] inputData1, int inputData2, int expected) 10 | { 11 | var solver = new MaxConsecutiveOnesIII_1004(); 12 | var result = solver.LongestOnesSimple(inputData1, inputData2); 13 | 14 | Assert.Equal(expected, result); 15 | } 16 | 17 | [Theory, ClassData(typeof(MaxConsecutiveOnesIIITestData))] 18 | public void CheckOptimal(int[] inputData1, int inputData2, int expected) 19 | { 20 | var solver = new MaxConsecutiveOnesIII_1004(); 21 | var result = solver.LongestOnesOptimal(inputData1, inputData2); 22 | 23 | Assert.Equal(expected, result); 24 | } 25 | } 26 | 27 | public sealed class MaxConsecutiveOnesIIITestData : IEnumerable 28 | { 29 | public IEnumerator GetEnumerator() 30 | { 31 | yield return new object[] 32 | { 33 | new []{1,1,1,0,0,0,1,1,1,1,0}, 34 | 2, 35 | 6 36 | }; 37 | 38 | yield return new object[] 39 | { 40 | new []{0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1}, 41 | 3, 42 | 10 43 | }; 44 | } 45 | 46 | IEnumerator IEnumerable.GetEnumerator() 47 | { 48 | return GetEnumerator(); 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Medium/Array/MaximumSubarray_53_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Medium.Array; 2 | using System.Collections; 3 | 4 | namespace LeetCodeNet.Tests.Medium.Array 5 | { 6 | public sealed class MaximumSubarray_53_test 7 | { 8 | [Theory, ClassData(typeof(MaximumSubarrayTestData))] 9 | public void CheckBruteForse(int[] inputData, int expected) 10 | { 11 | var solver = new MaximumSubarray_53(); 12 | 13 | Assert.Equal(expected, solver.MaxSubArrayBruteForce(inputData)); 14 | } 15 | 16 | [Theory, ClassData(typeof(MaximumSubarrayTestData))] 17 | public void CheckKadane(int[] inputData, int expected) 18 | { 19 | var solver = new MaximumSubarray_53(); 20 | 21 | Assert.Equal(expected, solver.MaxSubArrayKadane(inputData)); 22 | } 23 | } 24 | 25 | public sealed class MaximumSubarrayTestData : IEnumerable 26 | { 27 | public IEnumerator GetEnumerator() 28 | { 29 | //// Explanation: [4,-1,2,1] has the largest sum = 6. 30 | yield return new object[] 31 | { 32 | new[] { -2, 1, -3, 4, -1, 2, 1, -5, 4 }, 33 | 6 34 | }; 35 | 36 | yield return new object[] 37 | { 38 | new[] { 1 }, 39 | 1 40 | }; 41 | 42 | yield return new object[] 43 | { 44 | new[] { 5, 4, -1, 7, 8 }, 45 | 23 46 | }; 47 | } 48 | 49 | IEnumerator IEnumerable.GetEnumerator() 50 | { 51 | return GetEnumerator(); 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Medium/Array/MinimizeMaximumofArray_2439_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Medium.Array; 2 | using System.Collections; 3 | 4 | namespace LeetCodeNet.Tests.Medium.Array 5 | { 6 | public sealed class MinimizeMaximumofArray_2439_test 7 | { 8 | [Theory, ClassData(typeof(MinimizeMaximumofArrayTestData))] 9 | public void Check(int[] inputData, int expected) 10 | { 11 | var solver = new MinimizeMaximumofArray_2439(); 12 | 13 | Assert.Equal(expected, solver.MinimizeArrayValue(inputData)); 14 | } 15 | } 16 | 17 | public sealed class MinimizeMaximumofArrayTestData : IEnumerable 18 | { 19 | public IEnumerator GetEnumerator() 20 | { 21 | //// Explanation: 22 | /// One set of optimal operations is as follows: 23 | /// 1.Choose i = 1, and nums becomes[4, 6, 1, 6]. 24 | /// 2.Choose i = 3, and nums becomes[4, 6, 2, 5]. 25 | /// 3.Choose i = 1, and nums becomes[5, 5, 2, 5]. 26 | /// The maximum integer of nums is 5.It can be shown that the maximum number cannot be less than 5. 27 | /// Therefore, we return 5. 28 | yield return new object[] 29 | { 30 | new[] { 3,7,1,6 }, 31 | 5 32 | }; 33 | 34 | //// Explanation: It is optimal to leave nums as is, and since 10 is the maximum value, we return 10. 35 | yield return new object[] 36 | { 37 | new[] { 10, 1 }, 38 | 10 39 | }; 40 | } 41 | 42 | IEnumerator IEnumerable.GetEnumerator() 43 | { 44 | return GetEnumerator(); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Medium/Array/NumberofEnclaves_1020_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Medium.Array; 2 | using System.Collections; 3 | 4 | namespace LeetCodeNet.Tests.Medium.Array 5 | { 6 | public sealed class NumberofEnclaves_1020_test 7 | { 8 | [Theory, ClassData(typeof(NumberofEnclavesTestData))] 9 | public void Check(int[][] inputData, int expected) 10 | { 11 | var solver = new NumberofEnclaves_1020(); 12 | 13 | Assert.Equal(expected, solver.NumEnclaves(inputData)); 14 | } 15 | } 16 | 17 | public sealed class NumberofEnclavesTestData : IEnumerable 18 | { 19 | public IEnumerator GetEnumerator() 20 | { 21 | //// Explanation: There are three 1s that are enclosed by 0s, and one 1 that is not enclosed because its on the boundary. 22 | yield return new object[] 23 | { 24 | new[] { new[] { 0, 0, 0, 0 }, new[] { 1, 0, 1, 0 }, new[] { 0, 1, 1, 0 }, new int[] {0, 0, 0, 0} }, 25 | 3 26 | }; 27 | 28 | yield return new object[] 29 | { 30 | new[] { new[] { 0, 1, 1, 0 }, new[] { 0, 0, 1, 0 }, new[] { 0, 0, 1, 0 }, new int[] {0, 0, 0, 0} }, 31 | 0 32 | }; 33 | } 34 | 35 | IEnumerator IEnumerable.GetEnumerator() 36 | { 37 | return GetEnumerator(); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Medium/Array/NumberofIslands_200_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Medium.Array; 2 | using System.Collections; 3 | 4 | namespace LeetCodeNet.Tests.Medium.Array 5 | { 6 | public sealed class NumberofIslands_200_test 7 | { 8 | [Theory, ClassData(typeof(NumberofIslandsTestData))] 9 | public void Check(char[][] inputData, int expected) 10 | { 11 | var solver = new NumberofIslands_200(); 12 | 13 | Assert.Equal(expected, solver.NumIslands(inputData)); 14 | } 15 | } 16 | 17 | public sealed class NumberofIslandsTestData : IEnumerable 18 | { 19 | public IEnumerator GetEnumerator() 20 | { 21 | yield return new object[] 22 | { 23 | new[] { new[] { '1', '1', '1', '1', '0' }, new[] { '1', '1', '0', '1', '0' }, new[] { '1', '1', '0', '0', '0' }, new[] { '0', '0', '0', '0', '0' } }, 24 | 1 25 | }; 26 | 27 | yield return new object[] 28 | { 29 | new[] { new[] { '1', '1', '0', '0', '0' }, new[] { '1', '1', '0', '0', '0' }, new[] { '0', '0', '1', '0', '0' }, new[] { '0', '0', '0', '1', '1' } }, 30 | 3 31 | }; 32 | } 33 | 34 | IEnumerator IEnumerable.GetEnumerator() 35 | { 36 | return GetEnumerator(); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Medium/Array/PeakIndexinaMountainArray_852_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Medium.Array; 2 | using System.Collections; 3 | 4 | namespace LeetCodeNet.Tests.Medium.Array 5 | { 6 | public sealed class PeakIndexinaMountainArray_852_test 7 | { 8 | [Theory, ClassData(typeof(PeakIndexinaMountainArrayTestData))] 9 | public void Check(int[] inputData, int expected) 10 | { 11 | var solver = new PeakIndexinaMountainArray_852(); 12 | 13 | Assert.Equal(expected, solver.PeakIndexInMountainArray(inputData)); 14 | } 15 | } 16 | 17 | public sealed class PeakIndexinaMountainArrayTestData : IEnumerable 18 | { 19 | public IEnumerator GetEnumerator() 20 | { 21 | yield return new object[] 22 | { 23 | new[] { 0,1,0 }, 24 | 1 25 | }; 26 | 27 | yield return new object[] 28 | { 29 | new[] { 0,2,1,0 }, 30 | 1 31 | }; 32 | 33 | yield return new object[] 34 | { 35 | new[] { 0,10,5,2 }, 36 | 1 37 | }; 38 | } 39 | 40 | IEnumerator IEnumerable.GetEnumerator() 41 | { 42 | return GetEnumerator(); 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Medium/Array/RottingOranges_994_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Medium.Array; 2 | using System.Collections; 3 | 4 | namespace LeetCodeNet.Tests.Medium.Array 5 | { 6 | public sealed class RottingOranges_994_test 7 | { 8 | [Theory, ClassData(typeof(RottingOranges_994TestData))] 9 | public void Check(int[][] inputData, int expected) 10 | { 11 | var solver = new RottingOranges_994(); 12 | 13 | Assert.Equal(expected, solver.OrangesRotting(inputData)); 14 | } 15 | } 16 | 17 | public sealed class RottingOranges_994TestData : IEnumerable 18 | { 19 | public IEnumerator GetEnumerator() 20 | { 21 | //// Explanation: [4,-1,2,1] has the largest sum = 6. 22 | yield return new object[] 23 | { 24 | new[] { new[] { 2, 1, 1 }, new[] { 1, 1, 0 }, new[] { 0, 1, 1 } }, 25 | 4 26 | }; 27 | 28 | yield return new object[] 29 | { 30 | new[] { new[] { 0, 2 } }, 31 | 0 32 | }; 33 | } 34 | 35 | IEnumerator IEnumerable.GetEnumerator() 36 | { 37 | return GetEnumerator(); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Medium/Array/Searcha2DMatrix_74_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Medium.Array; 2 | using System.Collections; 3 | 4 | namespace LeetCodeNet.Tests.Medium.Array 5 | { 6 | public sealed class Searcha2DMatrix_74_test 7 | { 8 | [Theory, ClassData(typeof(Searcha2DMatrixTestData))] 9 | public void Check(int[][] inputData1, int inputData2, bool expected) 10 | { 11 | var solver = new Searcha2DMatrix_74(); 12 | 13 | Assert.Equal(expected, solver.SearchMatrix(inputData1, inputData2)); 14 | } 15 | } 16 | 17 | public sealed class Searcha2DMatrixTestData : IEnumerable 18 | { 19 | public IEnumerator GetEnumerator() 20 | { 21 | yield return new object[] 22 | { 23 | new[] {new[] { 1, 3, 5, 7 }, new [] { 10, 11, 16, 20 }, new[] { 23, 30, 34, 60 } }, 24 | 3, 25 | true 26 | }; 27 | 28 | yield return new object[] 29 | { 30 | new[] {new[] { 1, 3, 5, 7 }, new [] { 10, 11, 16, 20 }, new[] { 23, 30, 34, 60 } }, 31 | 13, 32 | false 33 | }; 34 | 35 | yield return new object[] 36 | { 37 | new[] {new[] { 1 } }, 38 | 1, 39 | true 40 | }; 41 | 42 | yield return new object[] 43 | { 44 | new[] {new[] { 1, 1 } }, 45 | 2, 46 | false 47 | }; 48 | } 49 | 50 | IEnumerator IEnumerable.GetEnumerator() 51 | { 52 | return GetEnumerator(); 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Medium/Array/SpiralMatrix_54_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Medium.Array; 2 | using System.Collections; 3 | 4 | namespace LeetCodeNet.Tests.Medium.Array 5 | { 6 | public sealed class SpiralMatrix_54_test 7 | { 8 | [Theory, ClassData(typeof(SpiralMatrixTestData))] 9 | public void Check(int[][] inputData, List expected) 10 | { 11 | var solver = new SpiralMatrix_54(); 12 | 13 | Assert.True(expected.SequenceEqual(solver.SpiralOrder(inputData))); 14 | } 15 | } 16 | 17 | public sealed class SpiralMatrixTestData : IEnumerable 18 | { 19 | public IEnumerator GetEnumerator() 20 | { 21 | yield return new object[] 22 | { 23 | new[] { new [] {1, 2, 3}, new [] {4, 5, 6}, new [] {7, 8 ,9}}, 24 | new List{ 1, 2, 3, 6, 9, 8, 7, 4, 5 } 25 | }; 26 | 27 | yield return new object[] 28 | { 29 | new[] { new [] {1, 2, 3, 4}, new [] {5, 6, 7, 8}, new [] {9, 10, 11, 12,}}, 30 | new List{ 1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7 } 31 | }; 32 | } 33 | 34 | IEnumerator IEnumerable.GetEnumerator() 35 | { 36 | return GetEnumerator(); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Medium/Array/SumofMatrixAfterQueries_2718_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Medium.Array; 2 | using System.Collections; 3 | 4 | namespace LeetCodeNet.Tests.Medium.Array 5 | { 6 | public sealed class SumofMatrixAfterQueries_2718_test 7 | { 8 | [Theory, ClassData(typeof(SumofMatrixAfterQueriesTestData))] 9 | public void CheckTwoPass(int input1, int[][] input2, long expected) 10 | { 11 | var solver = new SumofMatrixAfterQueries_2718(); 12 | 13 | Assert.Equal(expected, solver.MatrixSumQueries(input1, input2)); 14 | } 15 | } 16 | 17 | public sealed class SumofMatrixAfterQueriesTestData : IEnumerable 18 | { 19 | public IEnumerator GetEnumerator() 20 | { 21 | yield return new object[] 22 | { 23 | 3, 24 | new [] {new[] { 0,0,1 }, new [] {1,2,2 }, new[] { 0,2,3}, new[] { 1,0,4} }, 25 | 23 26 | }; 27 | 28 | yield return new object[] 29 | { 30 | 3, 31 | new [] {new[] { 0, 0, 4 }, new [] { 0,1,2 }, new[] {1,0,1}, new[] { 0,2,3}, new[] { 1,2,1} }, 32 | 17 33 | }; 34 | } 35 | 36 | IEnumerator IEnumerable.GetEnumerator() 37 | { 38 | return GetEnumerator(); 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Medium/Array/TopKFrequentElements_347_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Medium.Array; 2 | using System.Collections; 3 | 4 | namespace LeetCodeNet.Tests.Medium.Array 5 | { 6 | public sealed class TopKFrequentElements_347_test 7 | { 8 | [Theory, ClassData(typeof(TopKFrequentElementsTestData))] 9 | public void CheckLinq(int[] input1, int input2, int[] expected) 10 | { 11 | var solver = new TopKFrequentElements_347(); 12 | 13 | var result = solver.TopKFrequentLinq(input1, input2); 14 | 15 | Assert.True(expected.SequenceEqual(result)); 16 | } 17 | 18 | [Theory, ClassData(typeof(TopKFrequentElementsTestData))] 19 | public void CheckSort(int[] input1, int input2, int[] expected) 20 | { 21 | var solver = new TopKFrequentElements_347(); 22 | 23 | var result = solver.TopKFrequentSort(input1, input2); 24 | 25 | Assert.True(expected.SequenceEqual(result)); 26 | } 27 | } 28 | 29 | public sealed class TopKFrequentElementsTestData : IEnumerable 30 | { 31 | public IEnumerator GetEnumerator() 32 | { 33 | yield return new object[] 34 | { 35 | new[] {1,1,1,2,2,3}, 36 | 2, 37 | new[] {1,2} 38 | }; 39 | 40 | yield return new object[] 41 | { 42 | new[] {1}, 43 | 1, 44 | new[] {1} 45 | }; 46 | 47 | } 48 | 49 | IEnumerator IEnumerable.GetEnumerator() 50 | { 51 | return GetEnumerator(); 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Medium/Array/TwoSumII_167_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Medium.Array; 2 | using System.Collections; 3 | 4 | namespace LeetCodeNet.Tests.Medium.Array 5 | { 6 | public sealed class TwoSumII_167_test 7 | { 8 | [Theory, ClassData(typeof(TwoSumIITestData))] 9 | public void Check(int[] inputData1, int inputData2, int[] expected) 10 | { 11 | var solver = new TwoSumII_167(); 12 | 13 | Assert.Equal(expected, solver.TwoSum(inputData1, inputData2)); 14 | } 15 | } 16 | 17 | public sealed class TwoSumIITestData : IEnumerable 18 | { 19 | public IEnumerator GetEnumerator() 20 | { 21 | //// Explanation: The sum of 2 and 7 is 9. Therefore, index1 = 1, index2 = 2. We return [1, 2]. 22 | yield return new object[] 23 | { 24 | new[] {2,7,11,15}, 25 | 9, 26 | new[] {1,2} 27 | }; 28 | 29 | //// Explanation: The sum of 2 and 4 is 6. Therefore index1 = 1, index2 = 3. We return [1, 3]. 30 | yield return new object[] 31 | { 32 | new[] {2,3,4}, 33 | 6, 34 | new[] {1,3} 35 | }; 36 | 37 | //// Explanation: The sum of -1 and 0 is -1. Therefore index1 = 1, index2 = 2. We return [1, 2]. 38 | yield return new object[] 39 | { 40 | new[] {-1, 0}, 41 | -1, 42 | new[] {1,2} 43 | }; 44 | } 45 | 46 | IEnumerator IEnumerable.GetEnumerator() 47 | { 48 | return GetEnumerator(); 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Medium/DP/FindthePunishmentNumberofanInteger_2698_test.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using LeetCodeNet.Medium.DP; 3 | 4 | namespace LeetCodeNet.Tests.Medium.DP 5 | { 6 | public sealed class FindthePunishmentNumberofanInteger_2698_test 7 | { 8 | [Theory, ClassData(typeof(FindthePunishmentNumberofanIntegerTestData))] 9 | public void Check(int inputData, int expected) 10 | { 11 | var solver = new FindthePunishmentNumberofanInteger_2698(); 12 | var result = solver.PunishmentNumber(inputData); 13 | 14 | Assert.Equal(expected, result); 15 | } 16 | } 17 | 18 | public sealed class FindthePunishmentNumberofanIntegerTestData : IEnumerable 19 | { 20 | public IEnumerator GetEnumerator() 21 | { 22 | yield return new object[] 23 | { 24 | 10, 25 | 182 26 | }; 27 | 28 | yield return new object[] 29 | { 30 | 37, 31 | 1478 32 | }; 33 | 34 | yield return new object[] 35 | { 36 | 1, 37 | 1 38 | }; 39 | } 40 | 41 | IEnumerator IEnumerable.GetEnumerator() 42 | { 43 | return GetEnumerator(); 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Medium/Design/DesignBrowserHistory_1472_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Medium.Design; 2 | 3 | namespace LeetCodeNet.Tests.Medium.Design 4 | { 5 | public sealed class DesignBrowserHistory_1472_test 6 | { 7 | [Fact] 8 | public void CheckDLL() 9 | { 10 | var history = new BrowserHistoryDLL("leetcode.com"); 11 | 12 | history.Visit("google.com"); 13 | history.Visit("facebook.com"); 14 | history.Visit("youtube.com"); 15 | 16 | Assert.Equal("facebook.com", history.Back(1)); 17 | Assert.Equal("google.com", history.Back(1)); 18 | Assert.Equal("facebook.com", history.Forward(1)); 19 | 20 | history.Visit("linkedin.com"); 21 | 22 | Assert.Equal("linkedin.com", history.Forward(2)); 23 | Assert.Equal("google.com", history.Back(2)); 24 | Assert.Equal("leetcode.com", history.Back(7)); 25 | } 26 | 27 | [Fact] 28 | public void CheckArray() 29 | { 30 | var history = new BrowserHistorArray("leetcode.com"); 31 | 32 | history.Visit("google.com"); 33 | history.Visit("facebook.com"); 34 | history.Visit("youtube.com"); 35 | 36 | Assert.Equal("facebook.com", history.Back(1)); 37 | Assert.Equal("google.com", history.Back(1)); 38 | Assert.Equal("facebook.com", history.Forward(1)); 39 | 40 | history.Visit("linkedin.com"); 41 | 42 | Assert.Equal("linkedin.com", history.Forward(2)); 43 | Assert.Equal("google.com", history.Back(2)); 44 | Assert.Equal("leetcode.com", history.Back(7)); 45 | } 46 | } 47 | } -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Medium/Design/FrequencyTracker_2671_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.Medium.Design; 2 | 3 | namespace LeetCodeNet.Tests.Medium.Design 4 | { 5 | public sealed class FrequencyTracker_2671_test 6 | { 7 | [Fact] 8 | public void Check() 9 | { 10 | var searcher = new FrequencyTracker(); 11 | 12 | searcher.Add(3); 13 | searcher.Add(3); 14 | 15 | Assert.True(searcher.HasFrequency(2)); 16 | } 17 | 18 | [Fact] 19 | public void Check1() 20 | { 21 | var searcher = new FrequencyTracker(); 22 | 23 | searcher.Add(1); 24 | searcher.DeleteOne(1); 25 | 26 | Assert.False(searcher.HasFrequency(1)); 27 | } 28 | 29 | [Fact] 30 | public void Check3() 31 | { 32 | var searcher = new FrequencyTracker(); 33 | 34 | Assert.False(searcher.HasFrequency(2)); 35 | 36 | searcher.Add(3); 37 | 38 | Assert.True(searcher.HasFrequency(1)); 39 | } 40 | } 41 | } -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Medium/Graph/CloneGraph_133_test.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.DataStructs; 2 | using LeetCodeNet.Medium.Graph; 3 | 4 | namespace LeetCodeNet.Tests.Medium.Graph 5 | { 6 | public sealed class CloneGraph_133_test 7 | { 8 | [Fact] 9 | public void Check() 10 | { 11 | var node1 = new Node(1); 12 | var node2 = new Node(2); 13 | var node3 = new Node(3); 14 | var node4 = new Node(4); 15 | 16 | node1.neighbors.Add(node2); 17 | node1.neighbors.Add(node4); 18 | 19 | node2.neighbors.Add(node1); 20 | node2.neighbors.Add(node3); 21 | 22 | node3.neighbors.Add(node2); 23 | node3.neighbors.Add(node4); 24 | 25 | node4.neighbors.Add(node3); 26 | node4.neighbors.Add(node1); 27 | 28 | var solver = new CloneGraph_133(); 29 | 30 | var result = solver.CloneGraph(node1); 31 | 32 | Assert.Equal(1, result.val); 33 | Assert.NotSame(node1, result); 34 | } 35 | } 36 | } -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Medium/Graph/NumberofOperationstoMakeNetworkConnected_1319_test.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using LeetCodeNet.Medium.Graph; 3 | 4 | namespace LeetCodeNet.Tests.Medium.Graph 5 | { 6 | public sealed class NumberofOperationstoMakeNetworkConnected_1319_test 7 | { 8 | [Theory, ClassData(typeof(NumberofOperationstoMakeNetworkConnectedTestData))] 9 | public void Check(int[][] inputData2, int inputData1, int expected) 10 | { 11 | var solver = new NumberofOperationstoMakeNetworkConnected_1319(); 12 | 13 | Assert.Equal(expected, solver.MakeConnected(inputData1, inputData2)); 14 | } 15 | } 16 | 17 | public sealed class NumberofOperationstoMakeNetworkConnectedTestData : IEnumerable 18 | { 19 | public IEnumerator GetEnumerator() 20 | { 21 | //// Explanation: Remove cable between computer 1 and 2 and place between computers 1 and 3. 22 | yield return new object[] 23 | { 24 | new[] { new[] { 0, 1 }, new[] { 1, 2 }, new[] { 0, 2 } }, 25 | 4, 26 | 1 27 | }; 28 | 29 | yield return new object[] 30 | { 31 | new[] { new[] { 0, 1 }, new[] { 0, 2 }, new[] {0, 3}, new[] { 1, 2 }, new[] { 1, 3 }}, 32 | 6, 33 | 2 34 | }; 35 | 36 | yield return new object[] 37 | { 38 | new[] { new[] { 0, 1 }, new[] { 0, 2 }, new[] { 0, 3 }, new[] { 1, 2 } }, 39 | 6, 40 | -1 41 | }; 42 | } 43 | 44 | IEnumerator IEnumerable.GetEnumerator() 45 | { 46 | return GetEnumerator(); 47 | } 48 | } 49 | } -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Medium/Graph/ReorderRoutestoMakeAllPathsLeadtotheCityZero_1466_test.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using LeetCodeNet.Medium.Graph; 3 | 4 | namespace LeetCodeNet.Tests.Medium.Graph 5 | { 6 | public sealed class ReorderRoutestoMakeAllPathsLeadtotheCityZero_1466_test 7 | { 8 | [Theory, ClassData(typeof(ReorderRoutestoMakeAllPathsLeadtotheCityZeroTestData))] 9 | public void Check(int[][] inputData2, int inputData1, int expected) 10 | { 11 | var solver = new ReorderRoutestoMakeAllPathsLeadtotheCityZero_1466(); 12 | 13 | Assert.Equal(expected, solver.MinReorder(inputData1, inputData2)); 14 | } 15 | } 16 | 17 | public sealed class ReorderRoutestoMakeAllPathsLeadtotheCityZeroTestData : IEnumerable 18 | { 19 | public IEnumerator GetEnumerator() 20 | { 21 | yield return new object[] 22 | { 23 | new[] { new[] { 0, 1 }, new[] { 1, 3 }, new[] { 2, 3 }, new[] { 4, 0 }, new[] { 4,5 } }, 24 | 6, 25 | 3 26 | }; 27 | 28 | yield return new object[] 29 | { 30 | new[] { new[] { 1, 0 }, new[] { 1, 2}, new[] { 3, 2 }, new[] { 3, 4 }}, 31 | 5, 32 | 2 33 | }; 34 | 35 | yield return new object[] 36 | { 37 | new[] { new[] { 1, 0 }, new[] { 2, 0} }, 38 | 3, 39 | 0 40 | }; 41 | } 42 | 43 | IEnumerator IEnumerable.GetEnumerator() 44 | { 45 | return GetEnumerator(); 46 | } 47 | } 48 | } -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Medium/LeetMath/ReverseInteger_7_test.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using LeetCodeNet.Medium.LeetMath; 3 | 4 | namespace LeetCodeNet.Tests.Medium.LeetMath 5 | { 6 | public sealed class ReverseInteger_7_test 7 | { 8 | [Theory, ClassData(typeof(ReverseIntegerTestData))] 9 | public void CheckSimple(int inputData, int expected) 10 | { 11 | var solver = new ReverseInteger_7(); 12 | var result = solver.ReverseSimple(inputData); 13 | 14 | Assert.Equal(expected, result); 15 | } 16 | 17 | [Theory, ClassData(typeof(ReverseIntegerTestData))] 18 | public void CheckWithoutLong(int inputData, int expected) 19 | { 20 | var solver = new ReverseInteger_7(); 21 | var result = solver.ReverseWithoutLong(inputData); 22 | 23 | Assert.Equal(expected, result); 24 | } 25 | } 26 | 27 | public sealed class ReverseIntegerTestData : IEnumerable 28 | { 29 | public IEnumerator GetEnumerator() 30 | { 31 | yield return new object[] 32 | { 33 | 123, 34 | 321 35 | }; 36 | 37 | yield return new object[] 38 | { 39 | -123, 40 | -321 41 | }; 42 | 43 | yield return new object[] 44 | { 45 | 120, 46 | 21 47 | }; 48 | 49 | yield return new object[] 50 | { 51 | 1534236469, 52 | 0 53 | }; 54 | } 55 | 56 | IEnumerator IEnumerable.GetEnumerator() 57 | { 58 | return GetEnumerator(); 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Medium/LinkedList/LinkedListCycleII_142_test.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using LeetCodeNet.DataStructs; 3 | using LeetCodeNet.Medium.LinkedList; 4 | 5 | namespace LeetCodeNet.Tests.Medium.LinkedList 6 | { 7 | public sealed class LinkedListCycleII_142_test 8 | { 9 | [Theory, ClassData(typeof(LinkedListCycleIITestData))] 10 | public void Check(ListNode inputData, ListNode expected) 11 | { 12 | var solver = new LinkedListCycleII_142(); 13 | var result = solver.DetectCycle(inputData); 14 | 15 | Assert.True(result.Equals(expected)); 16 | } 17 | } 18 | 19 | public sealed class LinkedListCycleIITestData : IEnumerable 20 | { 21 | public IEnumerator GetEnumerator() 22 | { 23 | var node1 = new ListNode(2); 24 | var node3 = new ListNode(-4) { next = node1 }; 25 | var node2 = new ListNode(0) { next = node3 }; 26 | node1.next = node2; 27 | var node = new ListNode(3) { next = node1 }; 28 | 29 | yield return new object[] 30 | { 31 | node, 32 | node1 33 | }; 34 | 35 | node = new ListNode(0); 36 | node1 = new ListNode(1) {next = node}; 37 | node.next = node1; 38 | 39 | yield return new object[] 40 | { 41 | node, 42 | node 43 | }; 44 | } 45 | 46 | IEnumerator IEnumerable.GetEnumerator() 47 | { 48 | return GetEnumerator(); 49 | } 50 | } 51 | } -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Medium/Strings/CountAndSay_38_test.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using LeetCodeNet.Medium.Strings; 3 | 4 | namespace LeetCodeNet.Tests.Medium.Strings 5 | { 6 | public sealed class CountAndSay_38_test 7 | { 8 | [Theory, ClassData(typeof(CountAndSayTestData))] 9 | public void CheckRecursive(int inputData, string expected) 10 | { 11 | var solver = new CountAndSay_38(); 12 | 13 | Assert.Equal(expected, solver.CountAndSayRecursive(inputData)); 14 | } 15 | } 16 | 17 | public sealed class CountAndSayTestData : IEnumerable 18 | { 19 | public IEnumerator GetEnumerator() 20 | { 21 | yield return new object[] 22 | { 23 | 1, 24 | "1" 25 | }; 26 | 27 | yield return new object[] 28 | { 29 | 4, 30 | "1211" 31 | }; 32 | } 33 | 34 | IEnumerator IEnumerable.GetEnumerator() 35 | { 36 | return GetEnumerator(); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Medium/Strings/DecodeString_394_test.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using LeetCodeNet.Medium.Strings; 3 | 4 | namespace LeetCodeNet.Tests.Medium.Strings 5 | { 6 | public sealed class DecodeString_394_test 7 | { 8 | [Theory, ClassData(typeof(DecodeStringTestData))] 9 | public void CheckRecursive(string inputData, string expected) 10 | { 11 | var solver = new DecodeString_394(); 12 | 13 | Assert.Equal(expected, solver.DecodeString(inputData)); 14 | } 15 | } 16 | 17 | public sealed class DecodeStringTestData : IEnumerable 18 | { 19 | public IEnumerator GetEnumerator() 20 | { 21 | yield return new object[] 22 | { 23 | "3[a]2[bc]", 24 | "aaabcbc" 25 | }; 26 | 27 | yield return new object[] 28 | { 29 | "3[a2[c]]", 30 | "accaccacc" 31 | }; 32 | 33 | yield return new object[] 34 | { 35 | "2[abc]3[cd]ef", 36 | "abcabccdcdcdef" 37 | }; 38 | } 39 | 40 | IEnumerator IEnumerable.GetEnumerator() 41 | { 42 | return GetEnumerator(); 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Medium/Strings/FindAllAnagramsinaString_438_test.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using LeetCodeNet.Medium.Strings; 3 | 4 | namespace LeetCodeNet.Tests.Medium.Strings 5 | { 6 | public sealed class FindAllAnagramsinaString_438_test 7 | { 8 | [Theory, ClassData(typeof(FindAllAnagramsinaStringTestData))] 9 | public void Check(string inputData1, string inputData2, List expected) 10 | { 11 | var solver = new FindAllAnagramsinaString_438(); 12 | 13 | Assert.True(expected.OrderBy(x => x).SequenceEqual(solver.FindAnagrams(inputData1, inputData2).OrderBy(x => x))); 14 | } 15 | } 16 | 17 | public sealed class FindAllAnagramsinaStringTestData : IEnumerable 18 | { 19 | public IEnumerator GetEnumerator() 20 | { 21 | yield return new object[] 22 | { 23 | "cbaebabacd", 24 | "abc", 25 | new List{0,6} 26 | }; 27 | 28 | yield return new object[] 29 | { 30 | "abab", 31 | "ab", 32 | new List{0,1,2} 33 | }; 34 | } 35 | 36 | IEnumerator IEnumerable.GetEnumerator() 37 | { 38 | return GetEnumerator(); 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Medium/Strings/LetterCasePermutation_784_test.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using LeetCodeNet.Medium.Strings; 3 | 4 | namespace LeetCodeNet.Tests.Medium.Strings 5 | { 6 | public sealed class LetterCasePermutation_784_test 7 | { 8 | [Theory, ClassData(typeof(LetterCasePermutationTestData))] 9 | public void Check(string inputData, List expected) 10 | { 11 | var solver = new LetterCasePermutation_784(); 12 | 13 | Assert.True(expected.OrderBy(x=>x).SequenceEqual(solver.LetterCasePermutation(inputData).OrderBy(x=>x))); 14 | } 15 | } 16 | 17 | public sealed class LetterCasePermutationTestData : IEnumerable 18 | { 19 | public IEnumerator GetEnumerator() 20 | { 21 | yield return new object[] 22 | { 23 | "a1b2", 24 | new List(){ "a1b2", "a1B2", "A1b2", "A1B2" } 25 | }; 26 | 27 | yield return new object[] 28 | { 29 | "3z4", 30 | new List { "3z4", "3Z4" } 31 | }; 32 | } 33 | 34 | IEnumerator IEnumerable.GetEnumerator() 35 | { 36 | return GetEnumerator(); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Medium/Strings/LongestPalindromicSubstring_5_test.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using LeetCodeNet.Medium.Strings; 3 | 4 | namespace LeetCodeNet.Tests.Medium.Strings 5 | { 6 | public sealed class LongestPalindromicSubstring_5_test 7 | { 8 | [Theory, ClassData(typeof(LongestPalindromicSubstringTestData))] 9 | public void CheckRecursive(string inputData, string expected) 10 | { 11 | var solver = new LongestPalindromicSubstring_5(); 12 | 13 | Assert.Equal(expected, solver.LongestPalindrome(inputData)); 14 | } 15 | } 16 | 17 | public sealed class LongestPalindromicSubstringTestData : IEnumerable 18 | { 19 | public IEnumerator GetEnumerator() 20 | { 21 | yield return new object[] 22 | { 23 | "babad", 24 | "bab" 25 | }; 26 | 27 | yield return new object[] 28 | { 29 | "cbbd", 30 | "bb" 31 | }; 32 | } 33 | 34 | IEnumerator IEnumerable.GetEnumerator() 35 | { 36 | return GetEnumerator(); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Medium/Strings/LongestRepeatingCharacterReplacement_424_test.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using LeetCodeNet.Medium.Strings; 3 | 4 | namespace LeetCodeNet.Tests.Medium.Strings 5 | { 6 | public sealed class LongestRepeatingCharacterReplacement_424_test 7 | { 8 | [Theory, ClassData(typeof(LongestRepeatingCharacterReplacementTestData))] 9 | public void Check(string inputData1, int inputData2, int expected) 10 | { 11 | var solver = new LongestRepeatingCharacterReplacement_424(); 12 | 13 | Assert.Equal(expected, solver.CharacterReplacement(inputData1, inputData2)); 14 | } 15 | } 16 | 17 | public sealed class LongestRepeatingCharacterReplacementTestData : IEnumerable 18 | { 19 | public IEnumerator GetEnumerator() 20 | { 21 | yield return new object[] 22 | { 23 | "ABAB", 24 | 2, 25 | 4 26 | }; 27 | 28 | yield return new object[] 29 | { 30 | "AABABBA", 31 | 1, 32 | 4 33 | }; 34 | yield return new object[] 35 | { 36 | "ABAA", 37 | 0, 38 | 2 39 | }; 40 | } 41 | 42 | IEnumerator IEnumerable.GetEnumerator() 43 | { 44 | return GetEnumerator(); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Medium/Strings/PermutationinString_567_test.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using LeetCodeNet.Medium.Strings; 3 | 4 | namespace LeetCodeNet.Tests.Medium.Strings 5 | { 6 | public sealed class PermutationinString_567_test 7 | { 8 | [Theory, ClassData(typeof(PermutationinStringTestData))] 9 | public void Check(string inputData1, string inputData2, bool expected) 10 | { 11 | var solver = new PermutationinString_567(); 12 | 13 | Assert.Equal(expected, solver.CheckInclusion(inputData1, inputData2)); 14 | } 15 | } 16 | 17 | public sealed class PermutationinStringTestData : IEnumerable 18 | { 19 | public IEnumerator GetEnumerator() 20 | { 21 | yield return new object[] 22 | { 23 | "ab", 24 | "eidbaooo", 25 | true 26 | }; 27 | 28 | yield return new object[] 29 | { 30 | "ab", 31 | "eidboaoo", 32 | false 33 | }; 34 | 35 | yield return new object[] 36 | { 37 | "adc", 38 | "dcda", 39 | true 40 | }; 41 | 42 | yield return new object[] 43 | { 44 | "ab", 45 | "a", 46 | false 47 | }; 48 | } 49 | 50 | IEnumerator IEnumerable.GetEnumerator() 51 | { 52 | return GetEnumerator(); 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Medium/Strings/RemovingStarsFromaString_2390_test.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using LeetCodeNet.Medium.Strings; 3 | 4 | namespace LeetCodeNet.Tests.Medium.Strings 5 | { 6 | public sealed class RemovingStarsFromaString_2390_test 7 | { 8 | [Theory, ClassData(typeof(RemovingStarsFromaStringTestData))] 9 | public void Check(string inputData, string expected) 10 | { 11 | var solver = new RemovingStarsFromaString_2390(); 12 | 13 | Assert.Equal(expected, solver.RemoveStars(inputData)); 14 | } 15 | } 16 | 17 | public sealed class RemovingStarsFromaStringTestData : IEnumerable 18 | { 19 | //// Explanation: Performing the removals from left to right: 20 | /// - The closest character to the 1st star is 't' in "leet**cod*e". s becomes "lee*cod*e". 21 | /// - The closest character to the 2nd star is 'e' in "lee*cod*e". s becomes "lecod*e". 22 | /// - The closest character to the 3rd star is 'd' in "lecod*e". s becomes "lecoe". 23 | /// There are no more stars, so we return "lecoe". 24 | public IEnumerator GetEnumerator() 25 | { 26 | yield return new object[] 27 | { 28 | "leet**cod*e", 29 | "lecoe" 30 | }; 31 | 32 | yield return new object[] 33 | { 34 | "erase*****", 35 | string.Empty 36 | }; 37 | } 38 | 39 | IEnumerator IEnumerable.GetEnumerator() 40 | { 41 | return GetEnumerator(); 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Medium/Strings/ShiftingLetters_848_test.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using LeetCodeNet.Medium.Strings; 3 | 4 | namespace LeetCodeNet.Tests.Medium.Strings 5 | { 6 | public sealed class ShiftingLetters_848_test 7 | { 8 | [Theory, ClassData(typeof(ShiftingLettersTestData))] 9 | public void CheckRecursive(string inputData1, int[] inputData2, string expected) 10 | { 11 | var solver = new ShiftingLetters_848(); 12 | 13 | Assert.Equal(expected, solver.ShiftingLetters(inputData1, inputData2)); 14 | } 15 | } 16 | 17 | public sealed class ShiftingLettersTestData : IEnumerable 18 | { 19 | public IEnumerator GetEnumerator() 20 | { 21 | yield return new object[] 22 | { 23 | "abc", 24 | new [] { 3, 5, 9 }, 25 | "rpl" 26 | }; 27 | 28 | yield return new object[] 29 | { 30 | "aaa", 31 | new [] { 1,2,3 }, 32 | "gfd" 33 | }; 34 | 35 | yield return new object[] 36 | { 37 | "mkgfzkkuxownxvfvxasy", 38 | new [] { 505870226,437526072,266740649,224336793,532917782,311122363,567754492,595798950,81520022,684110326,137742843,275267355,856903962,148291585,919054234,467541837,622939912,116899933,983296461,536563513 }, 39 | "wqqwlcjnkphhsyvrkdod" 40 | }; 41 | } 42 | 43 | IEnumerator IEnumerable.GetEnumerator() 44 | { 45 | return GetEnumerator(); 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Medium/Trees/BinaryTreeLevelOrderTraversal_102_test.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using LeetCodeNet.DataStructs; 3 | using LeetCodeNet.Medium.Trees; 4 | 5 | namespace LeetCodeNet.Tests.Medium.Trees 6 | { 7 | public sealed class BinaryTreeLevelOrderTraversal_102_test 8 | { 9 | [Theory, ClassData(typeof(BinaryTreeLevelOrderTraversalTestData))] 10 | public void Check(TreeNode input, List> expected) 11 | { 12 | var solver = new BinaryTreeLevelOrderTraversal_102(); 13 | var result = solver.LevelOrder(input); 14 | 15 | Assert.Equal(expected.Count, result.Count); 16 | 17 | for (var i = 0; i < expected.Count; i++) 18 | { 19 | Assert.True(expected[i].SequenceEqual(result[i])); 20 | } 21 | } 22 | } 23 | 24 | public sealed class BinaryTreeLevelOrderTraversalTestData : IEnumerable 25 | { 26 | public IEnumerator GetEnumerator() 27 | { 28 | yield return new object[] 29 | { 30 | new TreeNode(3, new TreeNode(9), new TreeNode(20, new TreeNode(15), new TreeNode(7))), 31 | new List> {new() {3}, new() {9, 20}, new() {15,7}} 32 | }; 33 | 34 | yield return new object[] 35 | { 36 | new TreeNode(1), 37 | new List> {new() {1}} 38 | }; 39 | } 40 | 41 | IEnumerator IEnumerable.GetEnumerator() 42 | { 43 | return GetEnumerator(); 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Medium/Trees/CheckCompletenessofaBinaryTree_958_test.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using LeetCodeNet.DataStructs; 3 | using LeetCodeNet.Medium.Trees; 4 | 5 | namespace LeetCodeNet.Tests.Medium.Trees 6 | { 7 | public sealed class CheckCompletenessofaBinaryTree_958_test 8 | { 9 | [Theory, ClassData(typeof(CheckCompletenessofaBinaryTreeTestData))] 10 | public void CheckSimple(TreeNode input, bool expected) 11 | { 12 | var solver = new CheckCompletenessofaBinaryTree_958(); 13 | 14 | Assert.Equal(expected, solver.IsCompleteTreeFirstAttempt(input)); 15 | } 16 | 17 | [Theory, ClassData(typeof(CheckCompletenessofaBinaryTreeTestData))] 18 | public void CheckOptimized(TreeNode input, bool expected) 19 | { 20 | var solver = new CheckCompletenessofaBinaryTree_958(); 21 | 22 | Assert.Equal(expected, solver.IsCompleteTreeOptimized(input)); 23 | } 24 | } 25 | 26 | public sealed class CheckCompletenessofaBinaryTreeTestData : IEnumerable 27 | { 28 | public IEnumerator GetEnumerator() 29 | { 30 | yield return new object[] 31 | { 32 | new TreeNode(1, new TreeNode(2, new TreeNode(4), new TreeNode(5)), new TreeNode(3, new TreeNode(6))), 33 | true 34 | }; 35 | 36 | yield return new object[] 37 | { 38 | new TreeNode(1, new TreeNode(2, new TreeNode(4), new TreeNode(5)), new TreeNode(3, null, new TreeNode(6))), 39 | false 40 | }; 41 | } 42 | 43 | IEnumerator IEnumerable.GetEnumerator() 44 | { 45 | return GetEnumerator(); 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Medium/Trees/SumRootToLeafNumbers_129_test.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using LeetCodeNet.DataStructs; 3 | using LeetCodeNet.Medium.Trees; 4 | 5 | namespace LeetCodeNet.Tests.Medium.Trees 6 | { 7 | public sealed class SumRootToLeafNumbers_129_test 8 | { 9 | [Theory, ClassData(typeof(SumRootToLeafNumbersTestData))] 10 | public void CheckRecursive(TreeNode inputData, int expected) 11 | { 12 | var solver = new SumRootToLeafNumbers_129(); 13 | 14 | Assert.Equal(expected, solver.SumNumbersRecursive(inputData)); 15 | } 16 | 17 | [Theory, ClassData(typeof(SumRootToLeafNumbersTestData))] 18 | public void CheckDFS(TreeNode inputData, int expected) 19 | { 20 | var solver = new SumRootToLeafNumbers_129(); 21 | 22 | Assert.Equal(expected, solver.SumNumberDFS(inputData)); 23 | } 24 | } 25 | 26 | public sealed class SumRootToLeafNumbersTestData : IEnumerable 27 | { 28 | public IEnumerator GetEnumerator() 29 | { 30 | yield return new object[] 31 | { 32 | new TreeNode(1, new TreeNode(2), new TreeNode(3)), 33 | 25 34 | }; 35 | 36 | yield return new object[] 37 | { 38 | new TreeNode(4, new TreeNode(9, new TreeNode(5), new TreeNode(1)), new TreeNode(0)), 39 | 1026 40 | }; 41 | } 42 | 43 | IEnumerator IEnumerable.GetEnumerator() 44 | { 45 | return GetEnumerator(); 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Medium/Trees/ValidateBinarySearchTree_98_test.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using LeetCodeNet.DataStructs; 3 | using LeetCodeNet.Medium.Trees; 4 | 5 | namespace LeetCodeNet.Tests.Medium.Trees 6 | { 7 | public sealed class ValidateBinarySearchTree_98_test 8 | { 9 | [Theory, ClassData(typeof(ValidateBinarySearchTreeTestData))] 10 | public void CheckRecursive(TreeNode inputData, bool expected) 11 | { 12 | var solver = new ValidateBinarySearchTree_98(); 13 | 14 | Assert.Equal(expected, solver.IsValidBST(inputData)); 15 | } 16 | } 17 | 18 | public sealed class ValidateBinarySearchTreeTestData : IEnumerable 19 | { 20 | public IEnumerator GetEnumerator() 21 | { 22 | yield return new object[] 23 | { 24 | new TreeNode(2, new TreeNode(1), new TreeNode(3)), 25 | true 26 | }; 27 | 28 | yield return new object[] 29 | { 30 | new TreeNode(5, new TreeNode(1), new TreeNode(4, new TreeNode(3), new TreeNode(6))), 31 | false 32 | }; 33 | 34 | yield return new object[] 35 | { 36 | new TreeNode(5, new TreeNode(4), new TreeNode(6, new TreeNode(3), new TreeNode(7))), 37 | false 38 | }; 39 | } 40 | 41 | IEnumerator IEnumerable.GetEnumerator() 42 | { 43 | return GetEnumerator(); 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /LeetCodeNet.Tests/Usings.cs: -------------------------------------------------------------------------------- 1 | global using Xunit; -------------------------------------------------------------------------------- /LeetCodeNet.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 17 4 | VisualStudioVersion = 17.2.32519.379 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LeetCodeNet", "LeetCodeNet\LeetCodeNet.csproj", "{C458316C-98D3-4BB0-AFA7-687ED1D45602}" 7 | EndProject 8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LeetCodeNet.Tests", "LeetCodeNet.Tests\LeetCodeNet.Tests.csproj", "{350DF3FF-CBCF-41DE-812F-990CEA69E977}" 9 | EndProject 10 | Global 11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 12 | Debug|Any CPU = Debug|Any CPU 13 | Release|Any CPU = Release|Any CPU 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {C458316C-98D3-4BB0-AFA7-687ED1D45602}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 17 | {C458316C-98D3-4BB0-AFA7-687ED1D45602}.Debug|Any CPU.Build.0 = Debug|Any CPU 18 | {C458316C-98D3-4BB0-AFA7-687ED1D45602}.Release|Any CPU.ActiveCfg = Release|Any CPU 19 | {C458316C-98D3-4BB0-AFA7-687ED1D45602}.Release|Any CPU.Build.0 = Release|Any CPU 20 | {350DF3FF-CBCF-41DE-812F-990CEA69E977}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 21 | {350DF3FF-CBCF-41DE-812F-990CEA69E977}.Debug|Any CPU.Build.0 = Debug|Any CPU 22 | {350DF3FF-CBCF-41DE-812F-990CEA69E977}.Release|Any CPU.ActiveCfg = Release|Any CPU 23 | {350DF3FF-CBCF-41DE-812F-990CEA69E977}.Release|Any CPU.Build.0 = Release|Any CPU 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | GlobalSection(ExtensibilityGlobals) = postSolution 29 | SolutionGuid = {40DA28EF-475C-4822-9254-7D2959A45C20} 30 | EndGlobalSection 31 | EndGlobal 32 | -------------------------------------------------------------------------------- /LeetCodeNet/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Runtime.CompilerServices; 2 | using System.Runtime.InteropServices; 3 | 4 | // In SDK-style projects such as this one, several assembly attributes that were historically 5 | // defined in this file are now automatically added during build and populated with 6 | // values defined in project properties. For details of which attributes are included 7 | // and how to customise this process see: https://aka.ms/assembly-info-properties 8 | 9 | // Setting ComVisible to false makes the types in this assembly not visible to COM 10 | // components. If you need to access a type in this assembly from COM, set the ComVisible 11 | // attribute to true on that type. 12 | 13 | [assembly: ComVisible(false)] 14 | 15 | // The following GUID is for the ID of the typelib if this project is exposed to COM. 16 | 17 | [assembly: Guid("24914151-68bd-9691-21c5-e2ddd0042276")] 18 | 19 | [assembly: InternalsVisibleTo("LeetCodeNet.Tests")] 20 | [assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")] -------------------------------------------------------------------------------- /LeetCodeNet/DataStructs/ListNode.cs: -------------------------------------------------------------------------------- 1 | namespace LeetCodeNet.DataStructs 2 | { 3 | /// 4 | /// Class of node for LinkedList tasks 5 | /// 6 | public sealed class ListNode : IEquatable 7 | { 8 | /// 9 | /// Value 10 | /// 11 | public int val; 12 | 13 | /// 14 | /// Link to next node 15 | /// 16 | public ListNode next; 17 | 18 | /// 19 | /// Constructor 20 | /// 21 | /// Value 22 | public ListNode(int x) 23 | { 24 | val = x; 25 | } 26 | 27 | /// 28 | /// Equal lists by values 29 | /// 30 | /// Second list head 31 | /// Lists are equal 32 | public bool Equals(ListNode? other) 33 | { 34 | var node1 = this; 35 | var node2 = other; 36 | 37 | while (node1 != null) 38 | { 39 | if (node2 == null || node1.val != node2.val) 40 | { 41 | return false; 42 | } 43 | 44 | node1 = node1.next; 45 | node2 = node2.next; 46 | } 47 | 48 | return node2 == null; 49 | } 50 | } 51 | } -------------------------------------------------------------------------------- /LeetCodeNet/DataStructs/NaryTreeNode.cs: -------------------------------------------------------------------------------- 1 | namespace LeetCodeNet.DataStructs 2 | { 3 | /// 4 | /// Definition for a N-ary Tree node 5 | /// 6 | public sealed class NaryTreeNode 7 | { 8 | /// 9 | /// Value 10 | /// 11 | public int val; 12 | 13 | /// 14 | /// Children nodes 15 | /// 16 | public IList children; 17 | 18 | /// 19 | /// Parameterless constructor 20 | /// 21 | public NaryTreeNode() 22 | { 23 | } 24 | 25 | /// 26 | /// Constructor with parameters 27 | /// 28 | /// Value 29 | public NaryTreeNode(int _val) 30 | { 31 | val = _val; 32 | } 33 | 34 | /// 35 | /// Constructor with parameters 36 | /// 37 | /// Value 38 | /// Children nodes 39 | public NaryTreeNode(int _val, IList _children) 40 | { 41 | val = _val; 42 | children = _children; 43 | } 44 | } 45 | } -------------------------------------------------------------------------------- /LeetCodeNet/DataStructs/Node.cs: -------------------------------------------------------------------------------- 1 | namespace LeetCodeNet.DataStructs 2 | { 3 | /// 4 | /// Graph node 5 | /// 6 | public class Node 7 | { 8 | public int val; 9 | public IList neighbors; 10 | 11 | public Node() 12 | { 13 | val = 0; 14 | neighbors = new List(); 15 | } 16 | 17 | public Node(int _val) 18 | { 19 | val = _val; 20 | neighbors = new List(); 21 | } 22 | 23 | public Node(int _val, List _neighbors) 24 | { 25 | val = _val; 26 | neighbors = _neighbors; 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /LeetCodeNet/Easy/Array/CheckIfNandItsDoubleExist_1346.cs: -------------------------------------------------------------------------------- 1 | namespace LeetCodeNet.Easy.Array 2 | { 3 | /// 4 | /// https://leetcode.com/problems/check-if-n-and-its-double-exist/ 5 | /// 6 | /// 7 | /// Given an array arr of integers, check if there exist two indices i and j such that : 8 | /// i != j 9 | /// 0 <= i, j 12 | internal sealed class CheckIfNandItsDoubleExist_1346 13 | { 14 | /// 15 | /// HashSet provides O(1) lookup, so it ideal for this question. For each number in the array we check, if we have already seen it's half or it's double. 16 | /// NOTE: When checking half, we need to ensure that the current number is even, else we will get wrong answer like in the case of 3 and 7 being in the input. 17 | /// Here for 7, 7/2 would give 3 (not 3.5) which is present in the HashSet but not what we need. 18 | /// 19 | /// Input 20 | /// True, if exists 21 | /// 22 | /// Time complexity: O(N) 23 | /// Space complexity: O(N) 24 | /// 25 | public bool CheckIfExistHash(int[] arr) 26 | { 27 | var hash = new HashSet(arr.Length); 28 | 29 | foreach (var item in arr) 30 | { 31 | if (hash.Contains(item * 2) || (item % 2 == 0 && hash.Contains(item / 2))) 32 | { 33 | return true; 34 | } 35 | 36 | hash.Add(item); 37 | } 38 | 39 | return false; 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /LeetCodeNet/Easy/Array/FindtheHighestAltitude_1732.cs: -------------------------------------------------------------------------------- 1 | namespace LeetCodeNet.Easy.Array 2 | { 3 | /// 4 | /// https://leetcode.com/problems/find-the-highest-altitude 5 | /// 6 | /// 7 | /// There is a biker going on a road trip. The road trip consists of n + 1 points at different altitudes. The biker starts his trip on point 0 with altitude equal 0. 8 | /// You are given an integer array gain of length n where gain[i] is the net gain in altitude between points i​​​​​​ and i + 1 for all(0 <= i 10 | internal sealed class FindtheHighestAltitude_1732 11 | { 12 | /// 13 | /// ... 14 | /// 15 | /// Net gain 16 | /// Max altitude at the point 17 | /// 18 | /// Time complexity: O(N) 19 | /// Space complexity: O(1) 20 | /// 21 | public int LargestAltitude(int[] gain) 22 | { 23 | var currentPoint = 0; 24 | var result = 0; 25 | 26 | for (var index = 0; index < gain.Length; index++) 27 | { 28 | var item = gain[index]; 29 | 30 | currentPoint += item; 31 | 32 | result = Math.Max(result, currentPoint); 33 | } 34 | 35 | return result; 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /LeetCodeNet/Easy/Array/KthMissingPositiveNumber_1539.cs: -------------------------------------------------------------------------------- 1 | namespace LeetCodeNet.Easy.Array 2 | { 3 | /// 4 | /// https://leetcode.com/problems/kth-missing-positive-number 5 | /// 6 | /// 7 | /// Given an array arr of positive integers sorted in a strictly increasing order, and an integer k. 8 | /// Return the kth positive integer that is missing from this array. 9 | /// 10 | internal sealed class KthMissingPositiveNumber_1539 11 | { 12 | /// 13 | /// Simple and elegant. Keep pointer to the array and calc missing numbers 14 | /// 15 | /// Input array 16 | /// Kth missing 17 | /// Kth missing number 18 | /// 19 | /// Time complexity: O(n) 20 | /// Space complexity: O(1) 21 | /// 22 | public int FindKthPositive(int[] arr, int k) 23 | { 24 | var missingNumbers = 0; 25 | var arrIndex = 0; 26 | 27 | for (var i = 1; i <= arr.Length +k + 1; ++i) 28 | { 29 | if (arrIndex < arr.Length && arr[arrIndex] == i) 30 | { 31 | ++arrIndex; 32 | } 33 | else 34 | { 35 | ++missingNumbers; 36 | if (missingNumbers == k) 37 | { 38 | return i; 39 | } 40 | } 41 | } 42 | 43 | return -1; 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /LeetCodeNet/Easy/Array/ReplaceElementswithGreatestElementonRightSide_1299.cs: -------------------------------------------------------------------------------- 1 | namespace LeetCodeNet.Easy.Array 2 | { 3 | /// 4 | /// https://leetcode.com/problems/replace-elements-with-greatest-element-on-right-side/ 5 | /// 6 | /// 7 | /// Given an array arr, replace every element in that array with the greatest element among the elements to its right, and replace the last element with -1. 8 | /// After doing so, return the array. 9 | /// 10 | internal sealed class ReplaceElementswithGreatestElementonRightSide_1299 11 | { 12 | /// 13 | /// Straight forward. We should iterate from back to start. First max element will be initialized as -1, according to the condition. 14 | /// 15 | /// Input array 16 | /// Converted array 17 | /// 18 | /// Time complexity: O(n) 19 | /// Space complexity: O(1), as we change array in-place 20 | /// 21 | public int[] ReplaceElements(int[] arr) 22 | { 23 | var maxElement = -1; 24 | 25 | for (var i = arr.Length - 1; i >= 0; --i) 26 | { 27 | var temp = arr[i]; 28 | arr[i] = maxElement; 29 | maxElement = Math.Max(maxElement, temp); 30 | } 31 | 32 | return arr; 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /LeetCodeNet/Easy/Array/RunningSumof1dArray_1480.cs: -------------------------------------------------------------------------------- 1 | namespace LeetCodeNet.Easy.Array 2 | { 3 | /// 4 | /// https://leetcode.com/problems/running-sum-of-1d-array 5 | /// 6 | /// 7 | /// Given an array nums. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]). 8 | /// Return the running sum of nums. 9 | /// 10 | internal sealed class RunningSumof1dArray_1480 11 | { 12 | /// 13 | /// Use prefix sum approach to calc sum for previous values 14 | /// 15 | /// Input array 16 | /// Summed array 17 | /// 18 | /// Time complexity: O(N) 19 | /// Space complexity: O(N) 20 | /// 21 | public int[] RunningSum(int[] nums) 22 | { 23 | var sum = 0; 24 | var result = new int[nums.Length]; 25 | 26 | for (var i = 0; i < nums.Length; i++) 27 | { 28 | sum += nums[i]; 29 | 30 | result[i] = sum; 31 | } 32 | 33 | return result; 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /LeetCodeNet/Easy/Array/SortArrayByParity_905.cs: -------------------------------------------------------------------------------- 1 | namespace LeetCodeNet.Easy.Array 2 | { 3 | /// 4 | /// https://leetcode.com/problems/sort-array-by-parity/ 5 | /// 6 | /// 7 | /// Given an integer array nums, move all the even integers at the beginning of the array followed by all the odd integers. 8 | /// Return any array that satisfies this condition. 9 | /// 10 | internal sealed class SortArrayByParity_905 11 | { 12 | /// 13 | /// Two-pointers approach. Left pointer for the even integers, right pointer for the odd integers. 14 | /// 15 | /// Input array 16 | /// Ordered array 17 | /// 18 | /// Time complexity: O(n) 19 | /// Space complexity: O(n) 20 | /// 21 | public int[] SortArrayByParity(int[] nums) 22 | { 23 | var leftIndex = 0; 24 | var rightIndex = nums.Length - 1; 25 | var result = new int[nums.Length]; 26 | 27 | foreach (var num in nums) 28 | { 29 | if (num % 2 == 0) 30 | { 31 | result[leftIndex] = num; 32 | 33 | ++leftIndex; 34 | } 35 | else 36 | { 37 | result[rightIndex] = num; 38 | 39 | --rightIndex; 40 | } 41 | } 42 | 43 | return result; 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /LeetCodeNet/Easy/Array/TwoSum_1.cs: -------------------------------------------------------------------------------- 1 | namespace LeetCodeNet.Easy.Array 2 | { 3 | /// 4 | /// https://leetcode.com/problems/two-sum/ 5 | /// 6 | /// Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. 7 | internal sealed class TwoSum_1 8 | { 9 | /// 10 | /// The idea is to use Hash for storing: difference between target number and current number for Key; and index as value 11 | /// 12 | /// List of nums 13 | /// Value we need to find 14 | /// Indices of the two numbers 15 | /// 16 | /// Time complexity: O(n) 17 | /// Space complexity: O(n) 18 | /// 19 | public int[] TwoSum(int[] nums, int target) 20 | { 21 | var result = new Dictionary(nums.Length); 22 | 23 | for (var i = 0; i < nums.Length; ++i) 24 | { 25 | var diff = target - nums[i]; 26 | 27 | if (result.ContainsKey(diff)) 28 | { 29 | return new int[2] { result[diff], i }; 30 | } 31 | 32 | result.Add(nums[i], i); 33 | } 34 | 35 | throw new ArgumentException("Input parameters not valid"); 36 | } 37 | } 38 | } -------------------------------------------------------------------------------- /LeetCodeNet/Easy/Array/ValidMountainArray_941.cs: -------------------------------------------------------------------------------- 1 | namespace LeetCodeNet.Easy.Array 2 | { 3 | /// 4 | /// https://leetcode.com/problems/valid-mountain-array 5 | /// 6 | /// 7 | /// Given an array of integers arr, return true if and only if it is a valid mountain array. 8 | /// 9 | internal sealed class ValidMountainArray_941 10 | { 11 | /// 12 | /// Great explanation with pictures is here 13 | /// 14 | /// Input array 15 | /// True, if mountain 16 | /// 17 | /// Time complexity: O(N) 18 | /// Space complexity: O(1) 19 | /// 20 | public bool ValidMountainArray(int[] arr) 21 | { 22 | if (arr.Length < 3) 23 | { 24 | return false; 25 | } 26 | 27 | var leftIndex = 0; 28 | var rightIndex = arr.Length - 1; 29 | 30 | while (leftIndex < arr.Length - 1 && arr[leftIndex] < arr[leftIndex + 1]) 31 | { 32 | ++leftIndex; 33 | } 34 | 35 | while (rightIndex > 0 && arr[rightIndex] < arr[rightIndex - 1]) 36 | { 37 | --rightIndex; 38 | } 39 | 40 | return leftIndex != 0 && rightIndex != arr.Length - 1 && leftIndex == rightIndex; 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /LeetCodeNet/Easy/BitManipulation/ReverseBits_190.cs: -------------------------------------------------------------------------------- 1 | namespace LeetCodeNet.Easy.BitManipulation 2 | { 3 | /// 4 | /// https://leetcode.com/problems/reverse-bits/ 5 | /// 6 | /// Reverse bits of a given 32 bits unsigned integer. 7 | internal sealed class ReverseBits_190 8 | { 9 | /// 10 | /// Bit solution 11 | /// 12 | /// Number 13 | /// Reversed number 14 | /// 15 | /// Time complexity: O(1) 16 | /// Space complexity: O(1) 17 | /// 18 | public uint reverseBits(uint n) 19 | { 20 | uint result = 0; 21 | 22 | // We have 32 bit number, so to not to miss spaces - we will iterate through all digits 23 | for (var i = 0; i < 32; ++i) 24 | { 25 | result <<= 1; 26 | result |= (n & 1); 27 | n >>= 1; 28 | } 29 | 30 | return result; 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /LeetCodeNet/Easy/BitManipulation/SingleNumber_136.cs: -------------------------------------------------------------------------------- 1 | namespace LeetCodeNet.Easy.BitManipulation 2 | { 3 | /// 4 | /// https://leetcode.com/problems/single-number/ 5 | /// 6 | /// 7 | /// Given a non-empty array of integers nums, every element appears twice except for one. Find that single one. 8 | /// You must implement a solution with a linear runtime complexity and use only constant extra space. 9 | /// 10 | internal sealed class SingleNumber_136 11 | { 12 | /// 13 | /// Bit solution 14 | /// 15 | /// Array of numbers 16 | /// Unique number 17 | /// 18 | /// Time complexity: O(1) 19 | /// Space complexity: O(1) 20 | /// 21 | public int SingleNumber(int[] nums) 22 | { 23 | //// The key idea here is to use XOR. So, the simular numbers will eliminate themselves and we get only unpaired element. 24 | var result = 0; 25 | 26 | foreach (var num in nums) 27 | { 28 | result ^= num; 29 | } 30 | 31 | return result; 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /LeetCodeNet/Easy/DP/BestTimetoBuyandSellStock_121.cs: -------------------------------------------------------------------------------- 1 | namespace LeetCodeNet.Easy.DP 2 | { 3 | /// 4 | /// https://leetcode.com/problems/best-time-to-buy-and-sell-stock/ 5 | /// 6 | /// 7 | /// You are given an array prices where prices[i] is the price of a given stock on the ith day. 8 | /// You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock. 9 | /// Return the maximum profit you can achieve from this transaction.If you cannot achieve any profit, return 0. 10 | /// 11 | internal sealed class BestTimetoBuyandSellStock_121 12 | { 13 | /// 14 | /// The idea is to find a contiguous sub-array giving maximum profit. The common algorithm to find maximum sub-array is Kadane 15 | /// 16 | /// Set of prices 17 | /// Profit 18 | /// 19 | /// Time complexity: O(n) 20 | /// Space complexity: O(1) 21 | /// 22 | public int MaxProfit(int[] prices) 23 | { 24 | var result = 0; 25 | var tempSum = 0; 26 | 27 | for (var i = 1; i < prices.Length; ++i) 28 | { 29 | var diff = prices[i] - prices[i - 1]; 30 | 31 | tempSum += diff; 32 | if (tempSum < 0) 33 | { 34 | tempSum = 0; 35 | } 36 | else 37 | { 38 | result = Math.Max(result, tempSum); 39 | } 40 | } 41 | 42 | return result; 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /LeetCodeNet/Easy/DP/PascalsTriangle_118.cs: -------------------------------------------------------------------------------- 1 | namespace LeetCodeNet.Easy.DP 2 | { 3 | /// 4 | /// Given an integer numRows, return the first numRows of Pascal's triangle. 5 | /// In Pascal's triangle, each number is the sum of the two numbers directly above it as shown: 6 | /// 7 | internal sealed class PascalsTriangle_118 8 | { 9 | /// 10 | /// No tricks here. Fill first two rows and then calculate a current row using a previous row values. 11 | /// 12 | /// Number of rows in a triangle 13 | /// Pascal's triangle 14 | /// 15 | /// Time complexity: O(i*log(j)) 16 | /// Space complexity: O(i*log(j)) 17 | /// 18 | public IList> Generate(int numRows) 19 | { 20 | var result = new List> 21 | { 22 | new List() { 1 } 23 | }; 24 | 25 | if (numRows == 1) 26 | { 27 | return result; 28 | } 29 | 30 | result.Add(new List() { 1, 1 }); 31 | for (var i = 2; i < numRows; ++i) 32 | { 33 | var subResult = new List(); 34 | 35 | subResult.Add(1); 36 | 37 | var prevRow = result[i - 1]; 38 | 39 | for (var j = 1; j < i; ++j) 40 | { 41 | subResult.Add(prevRow[j - 1] + prevRow[j]); 42 | } 43 | 44 | subResult.Add(1); 45 | result.Add(subResult); 46 | } 47 | 48 | return result; 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /LeetCodeNet/Easy/LeetMath/CountOddNumbersinanIntervalRange_1523.cs: -------------------------------------------------------------------------------- 1 | namespace LeetCodeNet.Easy.LeetMath 2 | { 3 | /// 4 | /// https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/ 5 | /// 6 | /// 7 | /// Given two non-negative integers low and high. Return the count of odd numbers between low and high (inclusive). 8 | /// 9 | internal sealed class CountOddNumbersinanIntervalRange_1523 10 | { 11 | /// 12 | /// In the array half of the values will always be odd values. 13 | /// The only exception when both limits are even. 14 | /// 15 | /// Low limit 16 | /// High limit 17 | /// Odd amount 18 | /// 19 | /// Time complexity: O(n) 20 | /// Space complexity: O(1) 21 | /// 22 | public int CountOdds(int low, int high) 23 | { 24 | var isEvenLow = low % 2 == 0; 25 | var isEvenHigh = high % 2 == 0; 26 | 27 | var halfValue = (high - low) / 2; 28 | 29 | return halfValue + (isEvenLow && isEvenHigh ? 0 : 1); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /LeetCodeNet/Easy/LeetMath/LargestPerimeterTriangle_976.cs: -------------------------------------------------------------------------------- 1 | namespace LeetCodeNet.Easy.LeetMath 2 | { 3 | /// 4 | /// https://leetcode.com/problems/largest-perimeter-triangle/description/ 5 | /// 6 | /// 7 | /// Given an integer array nums, return the largest perimeter of a triangle with a non-zero area, formed from three of these lengths. 8 | /// If it is impossible to form any triangle of a non-zero area, return 0. 9 | /// 10 | internal sealed class LargestPerimeterTriangle_976 11 | { 12 | /// 13 | /// We need to calc the perimeter which is sum of three sides of the triangle. We also need to check, that it's a correct formed triangle (where side1 < side2 + side3). 14 | /// To detect the maxium perimeter - it's a good idea to sort all sides by descending and then check for triangle exist. 15 | /// 16 | /// Possible triangle sides 17 | /// Largest perimeter 18 | /// 19 | /// Time complexity: O(n+log(n)) 20 | /// Space complexity: O(n) 21 | /// 22 | public int LargestPerimeter(int[] nums) 23 | { 24 | var sortedNums = nums.OrderByDescending(x => x).ToList(); 25 | 26 | for (var i = 2; i < sortedNums.Count; ++i) 27 | { 28 | if (sortedNums[i - 2] < sortedNums[i - 1] + sortedNums[i]) 29 | { 30 | return sortedNums[i] + sortedNums[i - 1] + sortedNums[i - 2]; 31 | } 32 | } 33 | 34 | return 0; 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /LeetCodeNet/Easy/LeetMath/PalindromeNumber_9.cs: -------------------------------------------------------------------------------- 1 | namespace LeetCodeNet.Easy.LeetMath 2 | { 3 | /// 4 | /// https://leetcode.com/problems/palindrome-number/description/ 5 | /// 6 | /// Given an integer x, return true if x is a palindrome, and false otherwise. 7 | internal sealed class PalindromeNumber_9 8 | { 9 | /// 10 | /// The idea is to reverse the initial number and just compare original with reversed 11 | /// For reverse we will use the standard operations: /, % 12 | /// 13 | /// 14 | /// 15 | public bool IsPalindrome(int x) 16 | { 17 | if (x < 0) 18 | { 19 | return false; 20 | } 21 | 22 | var tempX = x; 23 | var reversedX = 0; 24 | 25 | while (tempX != 0) 26 | { 27 | reversedX = reversedX * 10 + tempX % 10; 28 | 29 | tempX /= 10; 30 | } 31 | 32 | return reversedX == x; 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /LeetCodeNet/Easy/LeetMath/SignoftheProductofanArray_1822.cs: -------------------------------------------------------------------------------- 1 | namespace LeetCodeNet.Easy.LeetMath 2 | { 3 | /// 4 | /// https://leetcode.com/problems/sign-of-the-product-of-an-array/ 5 | /// 6 | /// 7 | /// There is a function signFunc(x) that returns: 8 | /// 1 if x is positive. 9 | /// -1 if x is negative. 10 | /// 0 if x is equal to 0. 11 | /// You are given an integer array nums.Let product be the product of all values in the array nums. 12 | /// Return signFunc(product). 13 | /// 14 | internal sealed class SignoftheProductofanArray_1822 15 | { 16 | /// 17 | /// The total production will negative, if we have odd amount of negative values. Because if the even, than -1*-1 = 1. 18 | /// So, we just need to calc amount of negative values and find is it odd or even. 19 | /// 20 | /// Array 21 | /// - 1 if -, 1 if + 22 | /// 23 | /// Time complexity: O(n) 24 | /// Space complexity: O(1) 25 | /// 26 | public int ArraySign(int[] nums) 27 | { 28 | var negativesCount = 0; 29 | 30 | foreach (var num in nums) 31 | { 32 | //// We have to stop counting, if we have at least one zero in the input array. 33 | if (num == 0) 34 | { 35 | return 0; 36 | } 37 | 38 | negativesCount += num < 0 ? 1 : 0; 39 | } 40 | 41 | return negativesCount % 2 == 0 ? 1 : -1; 42 | 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /LeetCodeNet/Easy/LeetMath/SqrtX_69.cs: -------------------------------------------------------------------------------- 1 | namespace LeetCodeNet.Easy.LeetMath 2 | { 3 | /// 4 | /// Given a non-negative integer x, return the square root of x rounded down to the nearest integer. The returned integer should be non-negative as well. 5 | /// You must not use any built-in exponent function or operator. 6 | /// 7 | internal sealed class SqrtX_69 8 | { 9 | /// 10 | /// The same approach of binary search with small modifications like in 11 | /// 12 | /// Input in int 13 | /// Square 14 | public int MySqrt(int x) 15 | { 16 | if (x == 0) 17 | { 18 | return 0; 19 | } 20 | 21 | long leftIndex = 1; 22 | long rightIndex = x - 1; 23 | 24 | while (leftIndex <= rightIndex) 25 | { 26 | var middleIndex = leftIndex + (rightIndex - leftIndex) / 2; 27 | 28 | if (middleIndex * middleIndex <= x && (middleIndex + 1) * (middleIndex + 1) > x) 29 | { 30 | return (int)middleIndex; 31 | } 32 | 33 | if (middleIndex * middleIndex < x) 34 | { 35 | leftIndex = middleIndex + 1; 36 | } 37 | else 38 | { 39 | rightIndex = middleIndex; 40 | } 41 | } 42 | 43 | return (int)leftIndex; 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /LeetCodeNet/Easy/LeetMath/SubtracttheProductandSumofDigitsofanInteger_1281.cs: -------------------------------------------------------------------------------- 1 | namespace LeetCodeNet.Easy.LeetMath 2 | { 3 | /// 4 | /// https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/ 5 | /// 6 | /// Given an integer number n, return the difference between the product of its digits and the sum of its digits. 7 | internal sealed class SubtracttheProductandSumofDigitsofanInteger_1281 8 | { 9 | /// 10 | /// Keep in mind the '%' operator which return a modulo. This is enough to solve the task 11 | /// 12 | /// Source number 13 | /// Diff between product and sum of source digits 14 | /// 15 | /// Time complexity: O(1) 16 | /// Space complexity: O(1) 17 | /// 18 | public int SubtractProductAndSum(int n) 19 | { 20 | if (n == 0) 21 | { 22 | return 0; 23 | } 24 | 25 | var prod = 1; 26 | var sum = 0; 27 | 28 | while (n != 0) 29 | { 30 | var temp = n % 10; 31 | 32 | prod *= temp; 33 | sum += temp; 34 | n /= 10; 35 | } 36 | 37 | return prod - sum; 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /LeetCodeNet/Easy/LinkedList/DeleteNodeinaLinkedList_237.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.DataStructs; 2 | 3 | namespace LeetCodeNet.Easy.LinkedList 4 | { 5 | /// 6 | /// https://leetcode.com/problems/delete-node-in-a-linked-list/ 7 | /// 8 | /// 9 | /// Write a function to delete a node in a singly-linked list.You will not be given access to the head of the list, instead you will be given access to the node to be deleted directly. 10 | /// It is guaranteed that the node to be deleted is not a tail node in the list. 11 | /// 12 | internal sealed class DeleteNodeinaLinkedList_237 13 | { 14 | /// 15 | /// Copy the next node to the current node. 16 | /// 17 | /// Node to remove 18 | /// 19 | /// Time complexity: O(1) 20 | /// Space complexity: O(1) 21 | /// 22 | public void DeleteNode(ListNode node) 23 | { 24 | node.val = node.next.val; 25 | node.next = node.next.next; 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /LeetCodeNet/Easy/LinkedList/MiddleoftheLinkedList_876.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.DataStructs; 2 | 3 | namespace LeetCodeNet.Easy.LinkedList 4 | { 5 | /// 6 | /// https://leetcode.com/problems/middle-of-the-linked-list/ 7 | /// 8 | /// 9 | /// Given the head of a singly linked list, return the middle node of the linked list. 10 | /// If there are two middle nodes, return the second middle node. 11 | /// 12 | internal sealed class MiddleoftheLinkedList_876 13 | { 14 | /// 15 | /// Use slow/fast approach, where one pointer is moving slow (one step per iteration) 16 | /// And one pointer is moving fast (two steps per iteration) 17 | /// 18 | /// Head of the list 19 | /// Middle of the list 20 | /// 21 | /// Time complexity: O(n) 22 | /// Space complexity: O(1) 23 | /// 24 | public ListNode MiddleNode(ListNode head) 25 | { 26 | var slowPointer = head; 27 | var fastPointer = head; 28 | //// Find the middle with slow/fast pointers 29 | /// Slow (s), fast (f) pointers during the cycle: 30 | /// 1 2 3 4 5 31 | /// sf 32 | /// 1 2 3 4 5 33 | /// s f 34 | /// 1 2 3 4 5 35 | /// s f 36 | while (fastPointer != null && fastPointer.next != null) 37 | { 38 | slowPointer = slowPointer.next; 39 | fastPointer = fastPointer.next.next; 40 | } 41 | 42 | return slowPointer; 43 | } 44 | } 45 | } -------------------------------------------------------------------------------- /LeetCodeNet/Easy/LinkedList/ReverseLinkedList_206.cs: -------------------------------------------------------------------------------- 1 | using LeetCodeNet.DataStructs; 2 | 3 | namespace LeetCodeNet.Easy.LinkedList 4 | { 5 | /// 6 | /// https://leetcode.com/problems/reverse-linked-list/ 7 | /// 8 | /// Given the head of a singly linked list, reverse the list, and return the reversed list. 9 | internal sealed class ReverseLinkedList_206 10 | { 11 | /// 12 | /// 1. Create a new head 13 | /// 2. Update all pointers to the next element in the reverse order 14 | /// 15 | /// Current head 16 | /// New head 17 | /// 18 | /// Time complexity: O(n) 19 | /// Space complexity: O(1) 20 | /// 21 | public ListNode ReverseList(ListNode head) 22 | { 23 | var newHead = default(ListNode); 24 | 25 | while (head != null) 26 | { 27 | var temp = newHead; 28 | newHead = new ListNode(head.val) { next = temp }; 29 | head = head.next; 30 | } 31 | 32 | return newHead; 33 | } 34 | } 35 | } -------------------------------------------------------------------------------- /LeetCodeNet/Easy/Strings/IsSubsequence_392.cs: -------------------------------------------------------------------------------- 1 | namespace LeetCodeNet.Easy.Strings 2 | { 3 | /// 4 | /// https://leetcode.com/problems/is-subsequence 5 | /// 6 | /// 7 | ///Given two strings s and t, return true if s is a subsequence of t, or false otherwise. 8 | /// A subsequence of a string is a new string that is formed from the original string by deleting some(can be none) of the characters without disturbing the relative positions of the remaining characters. 9 | /// (i.e., "ace" is a subsequence of "abcde" while "aec" is not). 10 | /// 11 | internal sealed class IsSubsequence_392 12 | { 13 | /// 14 | /// Use two pointers approach where we skip all characters in T to preserve S chars order 15 | /// 16 | /// Input S 17 | /// Input T 18 | /// True if S is subsequence of T 19 | /// 20 | /// Time complexity: O(N) 21 | /// Space complexity: O(1) 22 | /// 23 | public bool IsSubsequence(string s, string t) 24 | { 25 | var tPointer = 0; 26 | 27 | for (var sPointer = 0; sPointer < s.Length; sPointer++) 28 | { 29 | while (tPointer < t.Length && t[tPointer] != s[sPointer]) 30 | { 31 | ++tPointer; 32 | } 33 | 34 | if (tPointer == t.Length) 35 | { 36 | return false; 37 | } 38 | 39 | ++tPointer; 40 | } 41 | 42 | return true; 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /LeetCodeNet/Easy/Strings/MinimumTimetoTypeWordUsingSpecialTypewriter_1974.cs: -------------------------------------------------------------------------------- 1 | namespace LeetCodeNet.Easy.Strings 2 | { 3 | /// 4 | /// https://leetcode.com/problems/minimum-time-to-type-word-using-special-typewriter/description/ 5 | /// 6 | /// 7 | /// There is a special typewriter with lowercase English letters 'a' to 'z' arranged in a circle with a pointer. 8 | /// A character can only be typed if the pointer is pointing to that character. The pointer is initially pointing to the character 'a'. 9 | /// 10 | internal sealed class MinimumTimetoTypeWordUsingSpecialTypewriter_1974 11 | { 12 | /// 13 | /// Native approach. Check the minimum between going clockwise or counterclockwise. 14 | /// 15 | /// Word 16 | /// Count to type 17 | /// 18 | /// Time complexity: O(N) 19 | /// Space complexity: O(1) 20 | /// 21 | public int MinTimeToType(string word) 22 | { 23 | var result = word.Length; // Time to type each symbol 24 | var prevChar = 'a'; 25 | 26 | for (var i = 0; i < word.Length; i++) 27 | { 28 | var ch = word[i]; 29 | var diff = Math.Abs(prevChar - ch); 30 | 31 | result += Math.Min(diff, 26 - diff); 32 | 33 | prevChar = ch; 34 | } 35 | 36 | return result; 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /LeetCodeNet/Easy/Strings/ReverseString_344.cs: -------------------------------------------------------------------------------- 1 | namespace LeetCodeNet.Easy.Strings 2 | { 3 | /// 4 | /// https://leetcode.com/problems/reverse-string/ 5 | /// 6 | /// 7 | /// Write a function that reverses a string. The input string is given as an array of characters s. 8 | /// You must do this by modifying the input array in-place with O(1) extra memory. 9 | /// 10 | internal sealed class ReverseString_344 11 | { 12 | /// 13 | /// Two pointers approach. One pointer is pointing at the start of the string, another - at the end. Swapping each element and change pointers position 14 | /// 15 | /// Input string in char array 16 | /// 17 | /// Time complexity: O(n) 18 | /// Space complexity: O(1) 19 | /// 20 | public void ReverseString(char[] s) 21 | { 22 | var leftPointer = 0; 23 | var rightPointer = s.Length - 1; 24 | 25 | while (leftPointer < rightPointer) 26 | { 27 | (s[leftPointer], s[rightPointer]) = (s[rightPointer], s[leftPointer]); 28 | 29 | ++leftPointer; 30 | --rightPointer; 31 | } 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /LeetCodeNet/LeetCodeNet.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net6.0 5 | enable 6 | enable 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /LeetCodeNet/Medium/Array/MinimizeMaximumofArray_2439.cs: -------------------------------------------------------------------------------- 1 | namespace LeetCodeNet.Medium.Array 2 | { 3 | /// 4 | /// https://leetcode.com/problems/minimize-maximum-of-array/ 5 | /// 6 | /// 7 | /// You are given a 0-indexed array nums comprising of n non-negative integers. 8 | /// In one operation, you must: 9 | /// Choose an integer i such that 1 <= i 0. 10 | /// Decrease nums[i] by 1. 11 | /// Increase nums[i - 1] by 1. 12 | /// Return the minimum possible value of the maximum integer of nums after performing any number of operations. 13 | /// 14 | internal sealed class MinimizeMaximumofArray_2439 15 | { 16 | /// 17 | /// We calculate the prefix sum arrray and their average. The average is the lower bound of the result, 18 | /// and it's reachable lower bound by the process in intuition, so this average is the result : (sum + i) / (i + 1) 19 | /// 20 | /// Input array 21 | /// Minimum possible value 22 | /// 23 | /// Time complexity: O(N) 24 | /// Space complexity: O(1) 25 | /// 26 | public int MinimizeArrayValue(int[] nums) 27 | { 28 | var sum = 0m; 29 | var result = 0m; 30 | 31 | for (int i = 0; i < nums.Length; ++i) 32 | { 33 | sum += nums[i]; 34 | 35 | result = Math.Max(result, (sum + i) / (i + 1)); 36 | } 37 | return (int)result; 38 | } 39 | } 40 | } 41 | --------------------------------------------------------------------------------