├── LeetCode ├── settings.gradle ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── src │ ├── main │ │ └── java │ │ │ └── org │ │ │ └── redquark │ │ │ └── onlinejudges │ │ │ └── leetcode │ │ │ ├── database │ │ │ └── RecyclableAndLowFatProducts.sql │ │ │ ├── math │ │ │ ├── BulbSwitcher.java │ │ │ ├── AddTwoIntegers.java │ │ │ ├── ArrangingCoins.java │ │ │ ├── AddDigits.java │ │ │ ├── PalindromeNumber.java │ │ │ ├── NumberOfStepsToReduceANumberToZero.java │ │ │ ├── SignOfTheProductOfAnArray.java │ │ │ └── CountSortedVowelStrings.java │ │ │ ├── common │ │ │ ├── ListNode.java │ │ │ ├── TreeNode.java │ │ │ ├── ListNodeWithRandom.java │ │ │ ├── TrieNode.java │ │ │ ├── Node.java │ │ │ └── TreeNodeWithNext.java │ │ │ ├── bitmagic │ │ │ ├── PowerOfTwo.java │ │ │ ├── BitwiseANDOfNumbersRange.java │ │ │ ├── NumberOf1Bits.java │ │ │ ├── ComplementOfBase10Integer.java │ │ │ ├── HammingDistance.java │ │ │ ├── SingleNumber.java │ │ │ └── ReverseBits.java │ │ │ ├── tree │ │ │ ├── RootEqualsSumOfChildren.java │ │ │ ├── MaximumDepthOfBinaryTree.java │ │ │ ├── InsertIntoABinarySearchTree.java │ │ │ ├── SumOfLeftLeaves.java │ │ │ ├── ValidateBinarySearchTree.java │ │ │ └── MergeTwoBinaryTrees.java │ │ │ ├── array │ │ │ ├── RemoveElement.java │ │ │ ├── MissingNumber.java │ │ │ ├── RunningSumOf1DArray.java │ │ │ ├── BuildArrayFromPermutation.java │ │ │ ├── ConcatenationOfArray.java │ │ │ ├── MinimizeMaximumOfArray.java │ │ │ ├── MinimumValueToGetPositiveStepByStepSum.java │ │ │ ├── ContainsDuplicate.java │ │ │ └── RichestCustomerWealth.java │ │ │ ├── dp │ │ │ ├── FibonacciNumber.java │ │ │ ├── NthTribonacciNumber.java │ │ │ ├── UniqueBinarySearchTrees.java │ │ │ ├── ClimbingStairs.java │ │ │ └── CoinChangeII.java │ │ │ └── string │ │ │ ├── PartitioningIntoMinimumNumberOfDeciBinaryNumbers.java │ │ │ ├── CheckIfAStringContainsAllBinaryCodesOfSizeK.java │ │ │ ├── DefangingAnIPAddress.java │ │ │ ├── FinalValueOfVariableAfterPerformingOperations.java │ │ │ └── ReverseString.java │ └── test │ │ └── java │ │ └── org │ │ └── redquark │ │ └── onlinejudges │ │ └── leetcode │ │ ├── backtracking │ │ ├── NQueensIITest.java │ │ ├── FairDistributionOfCookiesTest.java │ │ ├── GenerateParenthesesTest.java │ │ ├── GeneralizedAbbreviationTest.java │ │ ├── LetterCasePermutationTest.java │ │ └── PartitionToKEqualSumSubsetsTest.java │ │ ├── array │ │ ├── FirstBadVersionTest.java │ │ ├── StoneGameTest.java │ │ ├── FindTheDuplicateNumberTest.java │ │ ├── CandyTest.java │ │ ├── ContiguousArrayTest.java │ │ ├── KthSmallestNumberInMultiplicationTableTest.java │ │ ├── FindPeakElementTest.java │ │ ├── MajorityElementTest.java │ │ ├── ContainerWithMostWaterTest.java │ │ ├── MinimizeMaximumOfArrayTest.java │ │ ├── MaxConsecutiveOnesTest.java │ │ ├── SubarraySumEqualsKTest.java │ │ ├── BinarySearchTest.java │ │ ├── MaximumErasureValueTest.java │ │ ├── RemoveElementTest.java │ │ ├── NonDecreasingArrayTest.java │ │ ├── MaxNumberOfKSumPairsTest.java │ │ ├── BoatsToSavePeopleTest.java │ │ ├── LongestConsecutiveSequenceTest.java │ │ ├── MoveZeroesTest.java │ │ ├── RemoveDuplicatesFromSortedArrayTest.java │ │ ├── SingleElementInASortedArrayTest.java │ │ ├── GasStationTest.java │ │ ├── RemoveDuplicatesFromSortedArrayIITest.java │ │ ├── SortColorsTest.java │ │ ├── MissingNumberTest.java │ │ ├── SortAnArrayTest.java │ │ ├── KthLargestElementInAnArrayTest.java │ │ ├── MaximumSubarrayTest.java │ │ ├── CountOfRangeSumTest.java │ │ ├── SortArrayByParityTest.java │ │ ├── ThreeSumWithMultiplicityTest.java │ │ ├── CarPoolingTest.java │ │ ├── MaxConsecutiveOnesIIITest.java │ │ ├── AverageSalaryExcludingTheMinimumAndMaximumSalaryTest.java │ │ ├── MedianOfTwoSortedArraysTest.java │ │ ├── FirstMissingPositiveTest.java │ │ ├── SpiralMatrixIITest.java │ │ ├── RotateArrayTest.java │ │ ├── ConcatenationOfArrayTest.java │ │ ├── SearchInRotatedSortedArrayIITest.java │ │ ├── SquaresOfASortedArrayTest.java │ │ ├── BuildArrayFromPermutationTest.java │ │ ├── FindTheSmallestDivisorGivenAThresholdTest.java │ │ ├── SearchInRotatedSortedArrayTest.java │ │ ├── KDiffPairsInAnArrayTest.java │ │ ├── MaximizeDistanceToClosestPersonTest.java │ │ ├── PeakIndexInAMountainArrayTest.java │ │ ├── MinimumValueToGetPositiveStepByStepSumTest.java │ │ ├── ContainsDuplicateTest.java │ │ ├── KokoEatingBananasTest.java │ │ ├── ShortestUnsortedContinuousSubarrayTest.java │ │ ├── MinimumDominoRotationsForEqualRowTest.java │ │ └── SummaryRangesTest.java │ │ ├── dp │ │ ├── ClimbingStairsTest.java │ │ ├── UglyNumberIITest.java │ │ ├── NthTribonacciNumberTest.java │ │ ├── FibonacciNumberTest.java │ │ ├── UniqueBinarySearchTreesTest.java │ │ ├── HouseRobberTest.java │ │ ├── JumpGameIITest.java │ │ ├── BurstBalloonsTest.java │ │ ├── UniquePathsTest.java │ │ ├── PalindromicSubstringsTest.java │ │ ├── DeleteAndEarnTest.java │ │ ├── StoneGameIVTest.java │ │ ├── JumpGameTest.java │ │ ├── TwoKeysKeyboardTest.java │ │ ├── MaximumProductSubarrayTest.java │ │ ├── PerfectSquaresTest.java │ │ ├── TrappingRainWaterTest.java │ │ ├── ArithmeticSlicesTest.java │ │ ├── CountAllValidPickupAndDeliveryOptionsTest.java │ │ ├── BestTimeToBuyAndSellStocksTest.java │ │ ├── EditDistanceTest.java │ │ ├── BestSightSeeingPairTest.java │ │ ├── TargetSumTest.java │ │ ├── CountingBitsTest.java │ │ ├── HouseRobberIITest.java │ │ ├── RangeSumQueryImmutableTest.java │ │ ├── LongestPalindromicSubsequenceTest.java │ │ ├── BestTimeToBuyAndSellStockWithCooldownTest.java │ │ ├── CombinationSumIVTest.java │ │ ├── MinCostClimbingStairsTest.java │ │ ├── PartitionEqualSubsetSumTest.java │ │ ├── BestTimeToBuyAndSellStockIVTest.java │ │ ├── SuperEggDropTest.java │ │ ├── NumberOfLongestIncreasingSubsequenceTest.java │ │ ├── DeleteOperationForTwoStringsTest.java │ │ ├── DecodeWaysTest.java │ │ ├── DungeonGameTest.java │ │ ├── RestoreTheArrayTest.java │ │ ├── LastStoneWeightIITest.java │ │ ├── CoinChangeIITest.java │ │ ├── MinimumPathSumTest.java │ │ ├── BestTimeToBuyAndSellStockIITest.java │ │ ├── MinimumCostForTicketsTest.java │ │ ├── BestTimeToBuyAndSellStockWithTransactionFeeTest.java │ │ ├── MinimumInsertionStepsToMakeAStringPalindromeTest.java │ │ ├── SplitArrayLargestSumTest.java │ │ ├── LongestIncreasingSubsequenceTest.java │ │ ├── UniquePathsIITest.java │ │ ├── LongestStringChainTest.java │ │ └── NumberOfWaysToFormATargetStringGivenADictionaryTest.java │ │ ├── bitmagic │ │ ├── ReverseBitsTest.java │ │ ├── HammingDistanceTest.java │ │ ├── PowerOfTwoTest.java │ │ ├── ComplementOfBase10IntegerTest.java │ │ ├── BitwiseANDOfNumbersRangeTest.java │ │ ├── DivideTwoIntegersTest.java │ │ ├── NumberOf1BitsTest.java │ │ ├── SingleNumberTest.java │ │ └── MaximumXOROfTwoNumbersInAnArrayTest.java │ │ ├── math │ │ ├── HappyNumberTest.java │ │ ├── NextGreaterElementIIITest.java │ │ ├── ArrangingCoinsTest.java │ │ ├── IntegerToRomanTest.java │ │ ├── BulbSwitcherTest.java │ │ ├── AddBinaryTest.java │ │ ├── AddTwoIntegersTest.java │ │ ├── CountSortedVowelStringsTest.java │ │ ├── PalindromeNumberTest.java │ │ ├── RomanToIntegerTest.java │ │ ├── MinimumMovesToEqualArrayElementsIITest.java │ │ ├── ReverseIntegerTest.java │ │ ├── NumberOfStepsToReduceANumberToZeroTest.java │ │ ├── ExcelSheetColumnNumberTest.java │ │ ├── SignOfTheProductOfAnArrayTest.java │ │ ├── AddDigitsTest.java │ │ ├── BrokenCalculatorTest.java │ │ └── GuessNumberHigherOrLowerTest.java │ │ ├── string │ │ ├── Dota2SenateTest.java │ │ ├── OptimalPartitionOfStringTest.java │ │ ├── SmallestStringWithAGivenNumericValueTest.java │ │ ├── DetectCapitalTest.java │ │ ├── FindTheDifferenceTest.java │ │ ├── RemoveDuplicateCharactersTest.java │ │ ├── MaximumNumberOfBalloonsTest.java │ │ ├── DefangingAnIPAddressTest.java │ │ ├── ReverseWordsInAStringIIITest.java │ │ ├── ValidAnagramTest.java │ │ ├── ShortEncodingOfWordsTest.java │ │ ├── IsSubsequenceTest.java │ │ ├── StringToIntegerAtoiTest.java │ │ ├── LongestCommonPrefixTest.java │ │ ├── LongestPalindromeTest.java │ │ ├── CheckIfAStringContainsAllBinaryCodesOfSizeKTest.java │ │ ├── PermutationInStringTest.java │ │ ├── ValidPalindromeIITest.java │ │ ├── LongestValidParenthesesTest.java │ │ ├── RemovePalindromicSubsequencesTest.java │ │ ├── ReverseOnlyLettersTest.java │ │ ├── ReverseStringTest.java │ │ ├── RobotBoundedInCircleTest.java │ │ ├── ZigzagConversionTest.java │ │ ├── MinimumDeletionsToMakeCharacterFrequenciesUniqueTest.java │ │ ├── LongestPalindromicSubstringTest.java │ │ ├── PartitioningIntoMinimumNumberOfDeciBinaryNumbersTest.java │ │ └── PartitionLabelsTest.java │ │ ├── trie │ │ └── PrefixAndSuffixSearchTest.java │ │ ├── heap │ │ ├── ReorganizeStringTest.java │ │ ├── LastStoneWeightTest.java │ │ ├── FindMedianFromDataStreamTest.java │ │ ├── KthLargestElementInAStreamTest.java │ │ ├── MinimizeDeviationInArrayTest.java │ │ ├── TopKFrequentElementsTest.java │ │ └── SortEvenAndOddIndicesIndependentlyTest.java │ │ ├── stack │ │ ├── RemovingStarsFromAStringTest.java │ │ ├── MinStackTest.java │ │ ├── ScoreOfParenthesesTest.java │ │ ├── SimplifyPathTest.java │ │ ├── LargestRectangleInHistogramTest.java │ │ ├── MinimumRemoveToMakeValidParenthesesTest.java │ │ ├── BaseballGameTest.java │ │ ├── RemoveKDigitsTest.java │ │ ├── NextGreaterElementIITest.java │ │ ├── OneThreeTwoPatternTest.java │ │ └── DecodeStringTest.java │ │ ├── design │ │ ├── FlattenNestedListIteratorTest.java │ │ ├── EncodeAndDecodeTinyURLTest.java │ │ ├── ImplementQueueUsingStacksTest.java │ │ ├── LRUCacheTest.java │ │ ├── ImplementTrieTest.java │ │ ├── DesignHashMapTest.java │ │ ├── TimeBasedKeyValueStoreTest.java │ │ ├── PeekingIteratorTest.java │ │ └── DesignHashSetTest.java │ │ └── graph │ │ ├── SimilarStringGroupsTest.java │ │ └── JumpGameIVTest.java └── build.gradle └── WorkAtTech ├── settings.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── src ├── main │ └── java │ │ └── org │ │ └── redquark │ │ └── onlinejudges │ │ └── workattech │ │ ├── arrays │ │ └── CumulativeSum.java │ │ ├── graph │ │ └── EdgesToAdjacencyMatrix.java │ │ └── dp │ │ ├── ClimbingStairs.java │ │ └── RodCutting.java └── test │ └── java │ └── org │ └── redquark │ └── onlinejudges │ └── workattech │ ├── dp │ ├── ClimbingStairsTest.java │ ├── SubsetSum2Test.java │ ├── SubsetSumTest.java │ ├── EditDistanceTest.java │ ├── RodCuttingTest.java │ ├── CoinChangeTest.java │ ├── LongestCommonSubsequenceTest.java │ ├── LongestIncreasingSubsequenceTest.java │ ├── UniquePathsTest.java │ ├── MaximumProductSubarrayTest.java │ ├── MaximumPathSumInMatrixTest.java │ ├── MaximumSumIncreasingSubsequenceTest.java │ ├── DecodeWaysTest.java │ ├── LongestPalindromicSubstringTest.java │ └── CollectJewelsTest.java │ ├── graph │ ├── NumberOfIslandsTest.java │ └── ValidPathTest.java │ ├── greedy │ ├── TasksForProfitTest.java │ ├── SackOfGrainsTest.java │ └── TrainsAndPlatformsTest.java │ └── arrays │ └── IdenticalTwinsTest.java └── build.gradle /LeetCode/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'LeetCode' 2 | 3 | -------------------------------------------------------------------------------- /WorkAtTech/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'Work At Tech' 2 | 3 | -------------------------------------------------------------------------------- /LeetCode/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ani03sha/OnlineJudges/HEAD/LeetCode/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /WorkAtTech/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ani03sha/OnlineJudges/HEAD/WorkAtTech/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /LeetCode/src/main/java/org/redquark/onlinejudges/leetcode/database/RecyclableAndLowFatProducts.sql: -------------------------------------------------------------------------------- 1 | SELECT PRODUCT_ID FROM PRODUCTS WHERE LOW_FATS = 'Y' AND RECYCLABLE = 'Y'; -------------------------------------------------------------------------------- /LeetCode/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-7.0-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /LeetCode/src/main/java/org/redquark/onlinejudges/leetcode/math/BulbSwitcher.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.math; 2 | 3 | public class BulbSwitcher { 4 | 5 | public int bulbSwitch(int n) { 6 | return (int) Math.sqrt(n); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /WorkAtTech/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /LeetCode/src/main/java/org/redquark/onlinejudges/leetcode/common/ListNode.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.common; 2 | 3 | public class ListNode { 4 | 5 | public int val; 6 | public ListNode next; 7 | 8 | public ListNode(int val) { 9 | this.val = val; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /LeetCode/src/main/java/org/redquark/onlinejudges/leetcode/math/AddTwoIntegers.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.math; 2 | 3 | /** 4 | * @author Anirudh Sharma 5 | */ 6 | public class AddTwoIntegers { 7 | 8 | public int sum(int num1, int num2) { 9 | return num1 + num2; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /LeetCode/src/main/java/org/redquark/onlinejudges/leetcode/common/TreeNode.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.common; 2 | 3 | public class TreeNode { 4 | 5 | public int val; 6 | public TreeNode left; 7 | public TreeNode right; 8 | 9 | public TreeNode(int val) { 10 | this.val = val; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /LeetCode/src/main/java/org/redquark/onlinejudges/leetcode/bitmagic/PowerOfTwo.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.bitmagic; 2 | 3 | public class PowerOfTwo { 4 | 5 | public boolean isPowerOfTwo(int n) { 6 | // Special case 7 | if (n <= 0) { 8 | return false; 9 | } 10 | return (n & (n - 1)) == 0; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /WorkAtTech/src/main/java/org/redquark/onlinejudges/workattech/arrays/CumulativeSum.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.workattech.arrays; 2 | 3 | public class CumulativeSum { 4 | 5 | public int[] getCumulativeSum(int[] nums) { 6 | for (int i = 1; i < nums.length; i++) { 7 | nums[i] += nums[i - 1]; 8 | } 9 | return nums; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /LeetCode/src/main/java/org/redquark/onlinejudges/leetcode/common/ListNodeWithRandom.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.common; 2 | 3 | public class ListNodeWithRandom { 4 | 5 | public int val; 6 | public ListNodeWithRandom next; 7 | public ListNodeWithRandom random; 8 | 9 | public ListNodeWithRandom(int val) { 10 | this.val = val; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /LeetCode/src/main/java/org/redquark/onlinejudges/leetcode/common/TrieNode.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.common; 2 | 3 | public class TrieNode { 4 | 5 | public final char data; 6 | public final TrieNode[] children; 7 | public boolean isLeaf; 8 | 9 | public TrieNode(char data) { 10 | this.data = data; 11 | this.children = new TrieNode[26]; 12 | } 13 | } -------------------------------------------------------------------------------- /LeetCode/src/main/java/org/redquark/onlinejudges/leetcode/common/Node.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.common; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class Node { 7 | 8 | public int val; 9 | public List neighbors; 10 | 11 | public Node(int val) { 12 | this.val = val; 13 | this.neighbors = new ArrayList<>(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /LeetCode/src/main/java/org/redquark/onlinejudges/leetcode/tree/RootEqualsSumOfChildren.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.tree; 2 | 3 | import org.redquark.onlinejudges.leetcode.common.TreeNode; 4 | 5 | /** 6 | * @author Anirudh Sharma 7 | */ 8 | public class RootEqualsSumOfChildren { 9 | 10 | public boolean checkTree(TreeNode root) { 11 | return root.val == root.left.val + root.right.val; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /LeetCode/src/main/java/org/redquark/onlinejudges/leetcode/array/RemoveElement.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | /** 4 | * @author Anirudh Sharma 5 | */ 6 | public class RemoveElement { 7 | 8 | public int removeElement(int[] nums, int val) { 9 | int index = 0; 10 | for (int num : nums) { 11 | if (num != val) { 12 | nums[index++] = num; 13 | } 14 | } 15 | return index; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/backtracking/NQueensIITest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.backtracking; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class NQueensIITest { 8 | 9 | private final NQueensII testObject = new NQueensII(); 10 | 11 | @Test 12 | public void testTotalNQueens() { 13 | assertEquals(2, testObject.totalNQueens(4)); 14 | } 15 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/array/FirstBadVersionTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class FirstBadVersionTest { 8 | 9 | private final FirstBadVersion testObject = new FirstBadVersion(); 10 | 11 | @Test 12 | public void testFirstBadVersion() { 13 | assertEquals(4, testObject.firstBadVersion(5)); 14 | } 15 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/array/StoneGameTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertTrue; 6 | 7 | public class StoneGameTest { 8 | 9 | private final StoneGame testObject = new StoneGame(); 10 | 11 | @Test 12 | public void testStoneGame() { 13 | int[] piles = {5, 3, 4, 5}; 14 | assertTrue(testObject.stoneGame(piles)); 15 | } 16 | } -------------------------------------------------------------------------------- /LeetCode/src/main/java/org/redquark/onlinejudges/leetcode/bitmagic/BitwiseANDOfNumbersRange.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.bitmagic; 2 | 3 | /** 4 | * @author Anirudh Sharma 5 | */ 6 | public class BitwiseANDOfNumbersRange { 7 | 8 | public int rangeBitwiseAnd(int left, int right) { 9 | // The trick is to start from the right and move towards left 10 | while (right > left) { 11 | right &= right - 1; 12 | } 13 | return left & right; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /LeetCode/src/main/java/org/redquark/onlinejudges/leetcode/math/ArrangingCoins.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.math; 2 | 3 | /** 4 | * @author Anirudh Sharma 5 | */ 6 | public class ArrangingCoins { 7 | 8 | public int arrangeCoins(int n) { 9 | // Special case 10 | if (n <= 0) { 11 | return 0; 12 | } 13 | // Using Sridharacharya's formula for solving quadratic equations 14 | return (int) ((-1 + Math.sqrt(1 + 8 * (long) n)) / 2); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode/src/main/java/org/redquark/onlinejudges/leetcode/bitmagic/NumberOf1Bits.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.bitmagic; 2 | 3 | /** 4 | * @author Anirudh Sharma 5 | */ 6 | public class NumberOf1Bits { 7 | 8 | public int hammingWeight(int n) { 9 | // Count of set bits 10 | int count = 0; 11 | // Loop until the number becomes zero 12 | while (n != 0) { 13 | n &= (n - 1); 14 | count++; 15 | } 16 | return count; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /LeetCode/src/main/java/org/redquark/onlinejudges/leetcode/tree/MaximumDepthOfBinaryTree.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.tree; 2 | 3 | import org.redquark.onlinejudges.leetcode.common.TreeNode; 4 | 5 | /** 6 | * @author Anirudh Sharma 7 | */ 8 | public class MaximumDepthOfBinaryTree { 9 | 10 | public int maxDepth(TreeNode root) { 11 | // Special case 12 | if (root == null) { 13 | return 0; 14 | } 15 | return 1 + Math.max(maxDepth(root.left), maxDepth(root.right)); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/dp/ClimbingStairsTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class ClimbingStairsTest { 8 | 9 | private final ClimbingStairs testObject = new ClimbingStairs(); 10 | 11 | @Test 12 | public void testClimbStairs() { 13 | assertEquals(2, testObject.climbStairs(2)); 14 | assertEquals(3, testObject.climbStairs(3)); 15 | } 16 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/dp/UglyNumberIITest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class UglyNumberIITest { 8 | 9 | private final UglyNumberII testObject = new UglyNumberII(); 10 | 11 | @Test 12 | public void testNthUglyNumber() { 13 | assertEquals(12, testObject.nthUglyNumber(10)); 14 | assertEquals(1, testObject.nthUglyNumber(1)); 15 | } 16 | } -------------------------------------------------------------------------------- /LeetCode/src/main/java/org/redquark/onlinejudges/leetcode/array/MissingNumber.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | /** 4 | * @author Anirudh Sharma 5 | */ 6 | public class MissingNumber { 7 | 8 | public int missingNumber(int[] nums) { 9 | // Length of the array 10 | int n = nums.length; 11 | // Sum of all the elements of the array 12 | int sum = 0; 13 | for (int num : nums) { 14 | sum += num; 15 | } 16 | return n * (n + 1) / 2 - sum; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /WorkAtTech/src/test/java/org/redquark/onlinejudges/workattech/dp/ClimbingStairsTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.workattech.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class ClimbingStairsTest { 8 | 9 | private final ClimbingStairs testObject = new ClimbingStairs(); 10 | 11 | @Test 12 | public void testClimbStairs() { 13 | assertEquals(2, testObject.climbStairs(2)); 14 | assertEquals(5, testObject.climbStairs(4)); 15 | } 16 | } -------------------------------------------------------------------------------- /WorkAtTech/src/test/java/org/redquark/onlinejudges/workattech/dp/SubsetSum2Test.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.workattech.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class SubsetSum2Test { 8 | 9 | private final SubsetSum2 testObject = new SubsetSum2(); 10 | 11 | @Test 12 | public void testSubsetSum() { 13 | int[] A = new int[]{3, 0, 4, 9, 5}; 14 | int target = 17; 15 | assertEquals(1, testObject.subsetSum(A, target)); 16 | } 17 | } -------------------------------------------------------------------------------- /WorkAtTech/src/test/java/org/redquark/onlinejudges/workattech/dp/SubsetSumTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.workattech.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertTrue; 6 | 7 | public class SubsetSumTest { 8 | 9 | private final SubsetSum testObject = new SubsetSum(); 10 | 11 | @Test 12 | public void testHasValidSubset() { 13 | int[] A = new int[]{1, 3, 4, 9, 2}; 14 | int target = 13; 15 | assertTrue(testObject.hasValidSubset(A, target)); 16 | } 17 | } -------------------------------------------------------------------------------- /WorkAtTech/src/test/java/org/redquark/onlinejudges/workattech/dp/EditDistanceTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.workattech.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class EditDistanceTest { 8 | 9 | private final EditDistance testObject = new EditDistance(); 10 | 11 | @Test 12 | public void testMinOperations() { 13 | String s1 = "hello"; 14 | String s2 = "seldom"; 15 | assertEquals(3, testObject.minOperations(s1, s2)); 16 | } 17 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/dp/NthTribonacciNumberTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class NthTribonacciNumberTest { 8 | 9 | private final NthTribonacciNumber testObject = new NthTribonacciNumber(); 10 | 11 | @Test 12 | public void testTribonacci() { 13 | assertEquals(4, testObject.tribonacci(4)); 14 | assertEquals(1389537, testObject.tribonacci(25)); 15 | } 16 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/bitmagic/ReverseBitsTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.bitmagic; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class ReverseBitsTest { 8 | 9 | private final ReverseBits testObject = new ReverseBits(); 10 | 11 | @Test 12 | public void testReverseBits() { 13 | int n = Integer.parseInt("00000010100101000001111010011100", 2); 14 | assertEquals(964176192, testObject.reverseBits(n)); 15 | } 16 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/dp/FibonacciNumberTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class FibonacciNumberTest { 8 | 9 | private final FibonacciNumber testObject = new FibonacciNumber(); 10 | 11 | @Test 12 | public void testFib() { 13 | assertEquals(1, testObject.fib(2)); 14 | assertEquals(2, testObject.fib(3)); 15 | assertEquals(3, testObject.fib(4)); 16 | } 17 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/dp/UniqueBinarySearchTreesTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class UniqueBinarySearchTreesTest { 8 | 9 | private final UniqueBinarySearchTrees testObject = new UniqueBinarySearchTrees(); 10 | 11 | @Test 12 | public void testNumTrees() { 13 | assertEquals(5, testObject.numTrees(3)); 14 | assertEquals(1, testObject.numTrees(1)); 15 | } 16 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/bitmagic/HammingDistanceTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.bitmagic; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class HammingDistanceTest { 8 | 9 | private final HammingDistance testObject = new HammingDistance(); 10 | 11 | @Test 12 | public void testHammingDistance() { 13 | assertEquals(2, testObject.hammingDistance(1, 4)); 14 | assertEquals(1, testObject.hammingDistance(1, 3)); 15 | } 16 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/math/HappyNumberTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.math; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertFalse; 6 | import static org.junit.jupiter.api.Assertions.assertTrue; 7 | 8 | public class HappyNumberTest { 9 | 10 | private final HappyNumber testObject = new HappyNumber(); 11 | 12 | @Test 13 | public void testIsHappy() { 14 | assertTrue(testObject.isHappy(19)); 15 | assertFalse(testObject.isHappy(2)); 16 | } 17 | } -------------------------------------------------------------------------------- /WorkAtTech/src/test/java/org/redquark/onlinejudges/workattech/dp/RodCuttingTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.workattech.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class RodCuttingTest { 8 | 9 | private final RodCutting testObject = new RodCutting(); 10 | 11 | @Test 12 | public void testMaximumProfit() { 13 | int n = 8; 14 | int[] prices = new int[]{1, 3, 4, 5, 7, 9, 10, 11}; 15 | assertEquals(12, testObject.maximumProfit(n, prices)); 16 | } 17 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/array/FindTheDuplicateNumberTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class FindTheDuplicateNumberTest { 8 | 9 | private final FindTheDuplicateNumber testObject = new FindTheDuplicateNumber(); 10 | 11 | @Test 12 | public void testFindDuplicate() { 13 | int[] nums = new int[]{1, 3, 4, 2, 2}; 14 | assertEquals(2, testObject.findDuplicate(nums)); 15 | } 16 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/string/Dota2SenateTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.string; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class Dota2SenateTest { 8 | 9 | private final Dota2Senate testObject = new Dota2Senate(); 10 | 11 | @Test 12 | public void testPredictPartyVictory() { 13 | assertEquals("Radiant", testObject.predictPartyVictory("RD")); 14 | assertEquals("Dire", testObject.predictPartyVictory("RDD")); 15 | } 16 | } -------------------------------------------------------------------------------- /WorkAtTech/src/test/java/org/redquark/onlinejudges/workattech/dp/CoinChangeTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.workattech.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class CoinChangeTest { 8 | 9 | private final CoinChange testObject = new CoinChange(); 10 | 11 | @Test 12 | public void testNumberOfCombinations() { 13 | int[] coins = new int[]{5, 2, 4}; 14 | int target = 13; 15 | assertEquals(3, testObject.numberOfCombinations(coins, target)); 16 | } 17 | } -------------------------------------------------------------------------------- /LeetCode/src/main/java/org/redquark/onlinejudges/leetcode/bitmagic/ComplementOfBase10Integer.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.bitmagic; 2 | 3 | /** 4 | * @author Anirudh Sharma 5 | */ 6 | public class ComplementOfBase10Integer { 7 | 8 | public int bitwiseComplement(int n) { 9 | // Special case 10 | if (n == 0) { 11 | return 1; 12 | } 13 | // Number of bits in the number 14 | int bits = (int) (Math.log(n) / Math.log(2) + 1); 15 | // Complement will be 2 ^ bits - 1 - n 16 | return (1 << bits) - 1 - n; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/math/NextGreaterElementIIITest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.math; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class NextGreaterElementIIITest { 8 | 9 | private final NextGreaterElementIII testObject = new NextGreaterElementIII(); 10 | 11 | @Test 12 | public void testNextGreaterElement() { 13 | assertEquals(132, testObject.nextGreaterElement(123)); 14 | assertEquals(-1, testObject.nextGreaterElement(21)); 15 | } 16 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/array/CandyTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class CandyTest { 8 | 9 | private final Candy testObject = new Candy(); 10 | 11 | @Test 12 | public void testCandy() { 13 | int[] ratings = new int[]{1, 0, 2}; 14 | assertEquals(5, testObject.candy(ratings)); 15 | 16 | ratings = new int[]{1, 2, 2}; 17 | assertEquals(4, testObject.candy(ratings)); 18 | } 19 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/dp/HouseRobberTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class HouseRobberTest { 8 | 9 | private final HouseRobber testObject = new HouseRobber(); 10 | 11 | @Test 12 | void testRob() { 13 | int[] nums = new int[]{1, 2, 3, 1}; 14 | assertEquals(4, testObject.rob(nums)); 15 | 16 | nums = new int[]{2, 7, 9, 3, 1}; 17 | assertEquals(12, testObject.rob(nums)); 18 | } 19 | } -------------------------------------------------------------------------------- /LeetCode/src/main/java/org/redquark/onlinejudges/leetcode/array/RunningSumOf1DArray.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | /** 4 | * @author Anirudh Sharma 5 | */ 6 | public class RunningSumOf1DArray { 7 | 8 | public int[] runningSum(int[] nums) { 9 | // Special case 10 | if (nums == null || nums.length == 0) { 11 | return nums; 12 | } 13 | // Loop through the array to calculate running sum 14 | for (int i = 1; i < nums.length; i++) { 15 | nums[i] += nums[i - 1]; 16 | } 17 | return nums; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/dp/JumpGameIITest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class JumpGameIITest { 8 | 9 | private final JumpGameII testObject = new JumpGameII(); 10 | 11 | @Test 12 | public void testJump() { 13 | int[] nums = new int[]{2, 3, 1, 1, 4}; 14 | assertEquals(2, testObject.jump(nums)); 15 | 16 | nums = new int[]{2, 3, 0, 1, 4}; 17 | assertEquals(2, testObject.jump(nums)); 18 | } 19 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/math/ArrangingCoinsTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.math; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class ArrangingCoinsTest { 8 | 9 | private final ArrangingCoins testObject = new ArrangingCoins(); 10 | 11 | @Test 12 | public void testArrangeCoins() { 13 | assertEquals(2, testObject.arrangeCoins(5)); 14 | assertEquals(3, testObject.arrangeCoins(8)); 15 | assertEquals(6864, testObject.arrangeCoins(23563356)); 16 | } 17 | } -------------------------------------------------------------------------------- /WorkAtTech/src/test/java/org/redquark/onlinejudges/workattech/dp/LongestCommonSubsequenceTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.workattech.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class LongestCommonSubsequenceTest { 8 | 9 | private final LongestCommonSubsequence testObject = new LongestCommonSubsequence(); 10 | 11 | @Test 12 | public void testGetLengthOfLCS() { 13 | String s1 = "workattech"; 14 | String s2 = "branch"; 15 | assertEquals(4, testObject.getLengthOfLCS(s1, s2)); 16 | } 17 | } -------------------------------------------------------------------------------- /WorkAtTech/src/test/java/org/redquark/onlinejudges/workattech/dp/LongestIncreasingSubsequenceTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.workattech.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class LongestIncreasingSubsequenceTest { 8 | 9 | private final LongestIncreasingSubsequence testObject = new LongestIncreasingSubsequence(); 10 | 11 | @Test 12 | public void testGetLengthOfLIS() { 13 | int[] A = new int[]{10, 20, 2, 5, 3, 8, 8, 25, 6}; 14 | assertEquals(4, testObject.getLengthOfLIS(A)); 15 | } 16 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/math/IntegerToRomanTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.math; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class IntegerToRomanTest { 8 | 9 | private final IntegerToRoman testObject = new IntegerToRoman(); 10 | 11 | @Test 12 | public void testIntToRoman() { 13 | assertEquals("III", testObject.intToRoman(3)); 14 | assertEquals("LVIII", testObject.intToRoman(58)); 15 | assertEquals("MCMXCIV", testObject.intToRoman(1994)); 16 | } 17 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/trie/PrefixAndSuffixSearchTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.trie; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | import static org.redquark.onlinejudges.leetcode.trie.PrefixAndSuffixSearch.WordFilter; 7 | 8 | public class PrefixAndSuffixSearchTest { 9 | 10 | @Test 11 | public void test() { 12 | String[] words = new String[]{"apple"}; 13 | WordFilter testObject = new WordFilter(words); 14 | assertEquals(0, testObject.f("a", "e")); 15 | } 16 | 17 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/dp/BurstBalloonsTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class BurstBalloonsTest { 8 | 9 | private final BurstBalloons testObject = new BurstBalloons(); 10 | 11 | @Test 12 | public void testMaxCoins() { 13 | int[] nums = new int[]{3, 1, 5, 8}; 14 | assertEquals(167, testObject.maxCoins(nums)); 15 | 16 | nums = new int[]{1, 5}; 17 | assertEquals(10, testObject.maxCoins(nums)); 18 | } 19 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/dp/UniquePathsTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class UniquePathsTest { 8 | 9 | private final UniquePaths testObject = new UniquePaths(); 10 | 11 | @Test 12 | public void testUniquePaths() { 13 | int m = 3; 14 | int n = 7; 15 | assertEquals(28, testObject.uniquePaths(m, n)); 16 | 17 | m = 3; 18 | n = 3; 19 | assertEquals(6, testObject.uniquePaths(m, n)); 20 | } 21 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/heap/ReorganizeStringTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.heap; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class ReorganizeStringTest { 8 | 9 | private final ReorganizeString testObject = new ReorganizeString(); 10 | 11 | @Test 12 | public void testReorganizeString() { 13 | String s = "aab"; 14 | assertEquals("aba", testObject.reorganizeString(s)); 15 | 16 | s = "aaab"; 17 | assertEquals("", testObject.reorganizeString(s)); 18 | } 19 | } -------------------------------------------------------------------------------- /WorkAtTech/src/main/java/org/redquark/onlinejudges/workattech/graph/EdgesToAdjacencyMatrix.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.workattech.graph; 2 | 3 | public class EdgesToAdjacencyMatrix { 4 | 5 | public int[][] edgesToMatrix(int n, int[][] edges) { 6 | // Adjacency matrix 7 | int[][] adjacencyMatrix = new int[n][n]; 8 | // Loop through the edges 9 | for (int[] edge : edges) { 10 | int u = edge[0]; 11 | int v = edge[1]; 12 | adjacencyMatrix[u][v] = 1; 13 | adjacencyMatrix[v][u] = 1; 14 | } 15 | return adjacencyMatrix; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/dp/PalindromicSubstringsTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class PalindromicSubstringsTest { 8 | 9 | private final PalindromicSubstrings testObject = new PalindromicSubstrings(); 10 | 11 | @Test 12 | public void testCountSubstrings() { 13 | String s = "abc"; 14 | assertEquals(3, testObject.countSubstrings(s)); 15 | 16 | s = "aaa"; 17 | assertEquals(6, testObject.countSubstrings(s)); 18 | } 19 | } -------------------------------------------------------------------------------- /LeetCode/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'java' 3 | id 'jacoco' 4 | } 5 | 6 | group 'org.redquark.onlinejudges.leetcode' 7 | version '1.0-SNAPSHOT' 8 | 9 | repositories { 10 | mavenCentral() 11 | } 12 | 13 | jacoco { 14 | toolVersion = "0.8.7" 15 | reportsDir = file("$buildDir/jacoco") 16 | } 17 | 18 | test { 19 | finalizedBy jacocoTestReport 20 | } 21 | jacocoTestReport { 22 | dependsOn test 23 | } 24 | 25 | dependencies { 26 | testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0' 27 | testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0' 28 | } 29 | 30 | test { 31 | useJUnitPlatform() 32 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/math/BulbSwitcherTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.math; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class BulbSwitcherTest { 8 | 9 | private final BulbSwitcher testObject = new BulbSwitcher(); 10 | 11 | @Test 12 | public void testBulbSwitch() { 13 | assertEquals(1, testObject.bulbSwitch(3)); 14 | assertEquals(0, testObject.bulbSwitch(0)); 15 | assertEquals(1, testObject.bulbSwitch(1)); 16 | assertEquals(3, testObject.bulbSwitch(10)); 17 | } 18 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/array/ContiguousArrayTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class ContiguousArrayTest { 8 | 9 | private final ContiguousArray testObject = new ContiguousArray(); 10 | 11 | @Test 12 | public void testFindMaxLength() { 13 | int[] nums = new int[]{0, 1}; 14 | assertEquals(2, testObject.findMaxLength(nums)); 15 | 16 | nums = new int[]{0, 1, 0}; 17 | assertEquals(2, testObject.findMaxLength(nums)); 18 | } 19 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/bitmagic/PowerOfTwoTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.bitmagic; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertFalse; 6 | import static org.junit.jupiter.api.Assertions.assertTrue; 7 | 8 | public class PowerOfTwoTest { 9 | 10 | private final PowerOfTwo testObject = new PowerOfTwo(); 11 | 12 | @Test 13 | public void testIsPowerOfTwo() { 14 | assertTrue(testObject.isPowerOfTwo(1)); 15 | assertTrue(testObject.isPowerOfTwo(16)); 16 | assertFalse(testObject.isPowerOfTwo(3)); 17 | } 18 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/dp/DeleteAndEarnTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class DeleteAndEarnTest { 8 | 9 | private final DeleteAndEarn testObject = new DeleteAndEarn(); 10 | 11 | @Test 12 | public void testDeleteAndEarn() { 13 | int[] nums = new int[]{3, 4, 2}; 14 | assertEquals(6, testObject.deleteAndEarn(nums)); 15 | 16 | nums = new int[]{2, 2, 3, 3, 3, 4}; 17 | assertEquals(9, testObject.deleteAndEarn(nums)); 18 | } 19 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/math/AddBinaryTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.math; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class AddBinaryTest { 8 | 9 | private final AddBinary testObject = new AddBinary(); 10 | 11 | @Test 12 | public void testAddBinary() { 13 | String a = "11"; 14 | String b = "1"; 15 | assertEquals("100", testObject.addBinary(a, b)); 16 | 17 | a = "1010"; 18 | b = "1011"; 19 | assertEquals("10101", testObject.addBinary(a, b)); 20 | } 21 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/math/AddTwoIntegersTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.math; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class AddTwoIntegersTest { 8 | 9 | private final AddTwoIntegers testObject = new AddTwoIntegers(); 10 | 11 | @Test 12 | public void testSum() { 13 | int num1 = 12; 14 | int num2 = 5; 15 | assertEquals(17, testObject.sum(num1, num2)); 16 | 17 | num1 = -10; 18 | num2 = 4; 19 | assertEquals(-6, testObject.sum(num1, num2)); 20 | } 21 | } -------------------------------------------------------------------------------- /WorkAtTech/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'java' 3 | id 'jacoco' 4 | } 5 | 6 | group 'org.redquark.onlinejudges.workattech' 7 | version '1.0-SNAPSHOT' 8 | 9 | repositories { 10 | mavenCentral() 11 | } 12 | 13 | jacoco { 14 | toolVersion = "0.8.7" 15 | reportsDir = file("$buildDir/jacoco") 16 | } 17 | 18 | test { 19 | finalizedBy jacocoTestReport 20 | } 21 | jacocoTestReport { 22 | dependsOn test 23 | } 24 | 25 | dependencies { 26 | testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1' 27 | testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1' 28 | } 29 | 30 | test { 31 | useJUnitPlatform() 32 | } -------------------------------------------------------------------------------- /LeetCode/src/main/java/org/redquark/onlinejudges/leetcode/dp/FibonacciNumber.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.dp; 2 | 3 | /** 4 | * @author Anirudh Sharma 5 | */ 6 | public class FibonacciNumber { 7 | 8 | public int fib(int n) { 9 | // Special cases 10 | if (n == 0) { 11 | return 0; 12 | } 13 | if (n <= 2) { 14 | return 1; 15 | } 16 | int a = 1; 17 | int b = 1; 18 | int c = 0; 19 | for (int i = 3; i <= n; i++) { 20 | c = a + b; 21 | a = b; 22 | b = c; 23 | } 24 | return c; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/dp/StoneGameIVTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertFalse; 6 | import static org.junit.jupiter.api.Assertions.assertTrue; 7 | 8 | public class StoneGameIVTest { 9 | 10 | private final StoneGameIV testObject = new StoneGameIV(); 11 | 12 | @Test 13 | public void testWinnerSquareGame() { 14 | assertTrue(testObject.winnerSquareGame(1)); 15 | assertFalse(testObject.winnerSquareGame(2)); 16 | assertTrue(testObject.winnerSquareGame(4)); 17 | } 18 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/array/KthSmallestNumberInMultiplicationTableTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class KthSmallestNumberInMultiplicationTableTest { 8 | 9 | private final KthSmallestNumberInMultiplicationTable testObject = new KthSmallestNumberInMultiplicationTable(); 10 | 11 | @Test 12 | public void testFindKthNumber() { 13 | assertEquals(3, testObject.findKthNumber(3, 3, 5)); 14 | assertEquals(6, testObject.findKthNumber(2, 3, 6)); 15 | } 16 | } -------------------------------------------------------------------------------- /WorkAtTech/src/test/java/org/redquark/onlinejudges/workattech/dp/UniquePathsTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.workattech.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class UniquePathsTest { 8 | 9 | private final UniquePaths testObject = new UniquePaths(); 10 | 11 | @Test 12 | public void testUniquePaths() { 13 | assertEquals(3, testObject.uniquePaths(3, 2)); 14 | assertEquals(3, testObject.uniquePaths(2, 3)); 15 | assertEquals(6, testObject.uniquePaths(3, 3)); 16 | assertEquals(2, testObject.uniquePaths(2, 2)); 17 | } 18 | } -------------------------------------------------------------------------------- /LeetCode/src/main/java/org/redquark/onlinejudges/leetcode/common/TreeNodeWithNext.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.common; 2 | 3 | public class TreeNodeWithNext { 4 | 5 | public final int val; 6 | public TreeNodeWithNext left; 7 | public TreeNodeWithNext right; 8 | public TreeNodeWithNext next; 9 | 10 | public TreeNodeWithNext(int val) { 11 | this.val = val; 12 | } 13 | 14 | public TreeNodeWithNext(int val, TreeNodeWithNext left, TreeNodeWithNext right, TreeNodeWithNext next) { 15 | this.val = val; 16 | this.left = left; 17 | this.right = right; 18 | this.next = next; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /LeetCode/src/main/java/org/redquark/onlinejudges/leetcode/math/AddDigits.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.math; 2 | 3 | /** 4 | * @author Anirudh Sharma 5 | */ 6 | public class AddDigits { 7 | 8 | public int addDigitsSolutionOne(int num) { 9 | if (num < 9) { 10 | return num; 11 | } 12 | int currentSum = 0; 13 | while (num > 0) { 14 | currentSum += num % 10; 15 | num /= 10; 16 | } 17 | return addDigitsSolutionOne(currentSum); 18 | } 19 | 20 | public int addDigitsSolutionTwo(int num) { 21 | return num == 0 ? 0 : 1 + (num - 1) % 9; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /LeetCode/src/main/java/org/redquark/onlinejudges/leetcode/string/PartitioningIntoMinimumNumberOfDeciBinaryNumbers.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.string; 2 | 3 | /** 4 | * @author Anirudh Sharma 5 | */ 6 | public class PartitioningIntoMinimumNumberOfDeciBinaryNumbers { 7 | 8 | public int minPartitions(String n) { 9 | // Max digit in the number 10 | int max = 0; 11 | // Loop through the string 12 | for (char c : n.toCharArray()) { 13 | max = Math.max(max, c - '0'); 14 | if (max == 9) { 15 | return max; 16 | } 17 | } 18 | return max; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/math/CountSortedVowelStringsTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.math; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class CountSortedVowelStringsTest { 8 | 9 | private final CountSortedVowelStrings testObject = new CountSortedVowelStrings(); 10 | 11 | @Test 12 | public void countVowelStrings() { 13 | assertEquals(5, testObject.countVowelStrings(1)); 14 | assertEquals(15, testObject.countVowelStrings(2)); 15 | assertEquals(66045, testObject.countVowelStrings(33)); 16 | } 17 | } -------------------------------------------------------------------------------- /LeetCode/src/main/java/org/redquark/onlinejudges/leetcode/string/CheckIfAStringContainsAllBinaryCodesOfSizeK.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.string; 2 | 3 | import java.util.HashSet; 4 | import java.util.Set; 5 | 6 | /** 7 | * @author Anirudh Sharma 8 | */ 9 | public class CheckIfAStringContainsAllBinaryCodesOfSizeK { 10 | 11 | public boolean hasAllCodes(String s, int k) { 12 | Set distinctSubstrings = new HashSet<>(); 13 | for (int i = 0; i <= s.length() - k; i++) { 14 | distinctSubstrings.add(s.substring(i, i + k)); 15 | } 16 | return distinctSubstrings.size() >= Math.pow(2, k); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/dp/JumpGameTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertFalse; 6 | import static org.junit.jupiter.api.Assertions.assertTrue; 7 | 8 | public class JumpGameTest { 9 | 10 | private final JumpGame testObject = new JumpGame(); 11 | 12 | @Test 13 | public void testCanJump() { 14 | int[] nums = new int[]{2, 3, 1, 1, 4}; 15 | assertTrue(testObject.canJump(nums)); 16 | 17 | nums = new int[]{3, 2, 1, 0, 4}; 18 | assertFalse(testObject.canJump(nums)); 19 | } 20 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/dp/TwoKeysKeyboardTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class TwoKeysKeyboardTest { 8 | 9 | private final TwoKeysKeyboard testObject = new TwoKeysKeyboard(); 10 | 11 | @Test 12 | public void testMinSteps() { 13 | int n = 3; 14 | assertEquals(3, testObject.minSteps(n)); 15 | 16 | n = 1; 17 | assertEquals(0, testObject.minSteps(n)); 18 | 19 | n = 1000; 20 | assertEquals(21, testObject.minSteps(n)); 21 | } 22 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/heap/LastStoneWeightTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.heap; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class LastStoneWeightTest { 8 | 9 | private final LastStoneWeight testObject = new LastStoneWeight(); 10 | 11 | @Test 12 | public void testLastStoneWeight() { 13 | int[] stones = new int[]{2, 7, 4, 1, 8, 1}; 14 | assertEquals(1, testObject.lastStoneWeight(stones)); 15 | 16 | stones = new int[]{1}; 17 | assertEquals(1, testObject.lastStoneWeight(stones)); 18 | } 19 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/math/PalindromeNumberTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.math; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertFalse; 6 | import static org.junit.jupiter.api.Assertions.assertTrue; 7 | 8 | public class PalindromeNumberTest { 9 | 10 | private final PalindromeNumber testObject = new PalindromeNumber(); 11 | 12 | @Test 13 | public void testIsPalindrome() { 14 | assertTrue(testObject.isPalindrome(121)); 15 | assertFalse(testObject.isPalindrome(-121)); 16 | assertFalse(testObject.isPalindrome(10)); 17 | } 18 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/stack/RemovingStarsFromAStringTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.stack; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class RemovingStarsFromAStringTest { 8 | 9 | private final RemovingStarsFromAString testObject = new RemovingStarsFromAString(); 10 | 11 | @Test 12 | public void testRemoveStars() { 13 | String s = "leet**cod*e"; 14 | assertEquals("lecoe", testObject.removeStars(s)); 15 | 16 | s = "erase*****"; 17 | assertEquals("", testObject.removeStars(s)); 18 | } 19 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/string/OptimalPartitionOfStringTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.string; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class OptimalPartitionOfStringTest { 8 | 9 | private final OptimalPartitionOfString testObject = new OptimalPartitionOfString(); 10 | 11 | @Test 12 | public void testPartitionString() { 13 | String s = "abacaba"; 14 | assertEquals(4, testObject.partitionString(s)); 15 | 16 | s = "ssssss"; 17 | assertEquals(6, testObject.partitionString(s)); 18 | } 19 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/array/FindPeakElementTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class FindPeakElementTest { 8 | 9 | private final FindPeakElement testObject = new FindPeakElement(); 10 | 11 | @Test 12 | public void testFindPeakElement() { 13 | int[] nums = new int[]{1, 2, 3, 1}; 14 | assertEquals(2, testObject.findPeakElement(nums)); 15 | 16 | nums = new int[]{1, 2, 1, 3, 5, 6, 4}; 17 | assertEquals(5, testObject.findPeakElement(nums)); 18 | } 19 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/array/MajorityElementTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class MajorityElementTest { 8 | 9 | private final MajorityElement testObject = new MajorityElement(); 10 | 11 | @Test 12 | public void testMajorityElement() { 13 | int[] nums = new int[]{3, 2, 3}; 14 | assertEquals(3, testObject.majorityElement(nums)); 15 | 16 | nums = new int[]{2, 2, 1, 1, 1, 2, 2}; 17 | assertEquals(2, testObject.majorityElement(nums)); 18 | } 19 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/dp/MaximumProductSubarrayTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class MaximumProductSubarrayTest { 8 | 9 | private final MaximumProductSubarray testObject = new MaximumProductSubarray(); 10 | 11 | @Test 12 | public void testMaxProduct() { 13 | int[] nums = new int[]{2, 3, -2, 4}; 14 | assertEquals(6, testObject.maxProduct(nums)); 15 | 16 | nums = new int[]{-2, 0, -1}; 17 | assertEquals(0, testObject.maxProduct(nums)); 18 | } 19 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/dp/PerfectSquaresTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class PerfectSquaresTest { 8 | 9 | private final PerfectSquares testObject = new PerfectSquares(); 10 | 11 | @Test 12 | public void testNumSquares() { 13 | int n = 12; 14 | assertEquals(3, testObject.numSquares(n)); 15 | 16 | n = 13; 17 | assertEquals(2, testObject.numSquares(n)); 18 | 19 | n = 8765; 20 | assertEquals(2, testObject.numSquares(n)); 21 | } 22 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/dp/TrappingRainWaterTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class TrappingRainWaterTest { 8 | 9 | private final TrappingRainWater testObject = new TrappingRainWater(); 10 | 11 | @Test 12 | public void testTrap() { 13 | int[] height = new int[]{0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1}; 14 | assertEquals(6, testObject.trap(height)); 15 | 16 | height = new int[]{4, 2, 0, 3, 2, 5}; 17 | assertEquals(9, testObject.trap(height)); 18 | } 19 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/string/SmallestStringWithAGivenNumericValueTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.string; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class SmallestStringWithAGivenNumericValueTest { 8 | 9 | private final SmallestStringWithAGivenNumericValue testObject = new SmallestStringWithAGivenNumericValue(); 10 | 11 | @Test 12 | public void testGetSmallestString() { 13 | assertEquals("aay", testObject.getSmallestString(3, 27)); 14 | assertEquals("aaszz", testObject.getSmallestString(5, 73)); 15 | } 16 | } -------------------------------------------------------------------------------- /LeetCode/src/main/java/org/redquark/onlinejudges/leetcode/bitmagic/HammingDistance.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.bitmagic; 2 | 3 | /** 4 | * @author Anirudh Sharma 5 | */ 6 | public class HammingDistance { 7 | 8 | public int hammingDistance(int x, int y) { 9 | // XOR of two numbers will give us set bits at position 10 | // where bits are different in x and y 11 | int z = x ^ y; 12 | // Count to find the set bits 13 | int count = 0; 14 | // Loop until z becomes zero 15 | while (z != 0) { 16 | z &= (z - 1); 17 | count++; 18 | } 19 | return count; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/bitmagic/ComplementOfBase10IntegerTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.bitmagic; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class ComplementOfBase10IntegerTest { 8 | 9 | private final ComplementOfBase10Integer testObject = new ComplementOfBase10Integer(); 10 | 11 | @Test 12 | public void testBitwiseComplement() { 13 | assertEquals(2, testObject.bitwiseComplement(5)); 14 | assertEquals(1, testObject.bitwiseComplement(0)); 15 | assertEquals(5, testObject.bitwiseComplement(10)); 16 | } 17 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/bitmagic/BitwiseANDOfNumbersRangeTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.bitmagic; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class BitwiseANDOfNumbersRangeTest { 8 | 9 | private final BitwiseANDOfNumbersRange testObject = new BitwiseANDOfNumbersRange(); 10 | 11 | @Test 12 | public void testRangeBitwiseAnd() { 13 | assertEquals(4, testObject.rangeBitwiseAnd(5, 7)); 14 | assertEquals(0, testObject.rangeBitwiseAnd(0, 0)); 15 | assertEquals(0, testObject.rangeBitwiseAnd(1, 2147483647)); 16 | } 17 | } -------------------------------------------------------------------------------- /WorkAtTech/src/test/java/org/redquark/onlinejudges/workattech/graph/NumberOfIslandsTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.workattech.graph; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class NumberOfIslandsTest { 8 | 9 | private final NumberOfIslands testObject = new NumberOfIslands(); 10 | 11 | @Test 12 | public void testGetNumberOfIslands() { 13 | int[][] surface = new int[][]{ 14 | {1, 1, 0, 1}, 15 | {1, 0, 1, 1}, 16 | {0, 1, 0, 1} 17 | }; 18 | assertEquals(3, testObject.getNumberOfIslands(surface)); 19 | } 20 | } -------------------------------------------------------------------------------- /LeetCode/src/main/java/org/redquark/onlinejudges/leetcode/string/DefangingAnIPAddress.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.string; 2 | 3 | /** 4 | * @author Anirudh Sharma 5 | */ 6 | public class DefangingAnIPAddress { 7 | 8 | public String defangIPaddress(String address) { 9 | StringBuilder defanged = new StringBuilder(); 10 | // Loop through every character of the string 11 | for (char c : address.toCharArray()) { 12 | if (c == '.') { 13 | defanged.append("[.]"); 14 | } else { 15 | defanged.append(c); 16 | } 17 | } 18 | return defanged.toString(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/array/ContainerWithMostWaterTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class ContainerWithMostWaterTest { 8 | 9 | private final ContainerWithMostWater testObject = new ContainerWithMostWater(); 10 | 11 | @Test 12 | public void testMaxArea() { 13 | int[] height = new int[]{1, 8, 6, 2, 5, 4, 8, 3, 7}; 14 | assertEquals(49, testObject.maxArea(height)); 15 | 16 | height = new int[]{1, 1}; 17 | assertEquals(1, testObject.maxArea(height)); 18 | } 19 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/design/FlattenNestedListIteratorTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.design; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertFalse; 6 | import static org.junit.jupiter.api.Assertions.assertNull; 7 | import static org.redquark.onlinejudges.leetcode.design.FlattenNestedListIterator.NestedIterator; 8 | 9 | public class FlattenNestedListIteratorTest { 10 | 11 | @Test 12 | public void test() { 13 | NestedIterator testObject = new NestedIterator(null); 14 | assertFalse(testObject.hasNext()); 15 | assertNull(testObject.next()); 16 | } 17 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/dp/ArithmeticSlicesTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class ArithmeticSlicesTest { 8 | 9 | private final ArithmeticSlices testObject = new ArithmeticSlices(); 10 | 11 | @Test 12 | public void testNumberOfArithmeticSlices() { 13 | int[] nums = new int[]{1, 2, 3, 4}; 14 | assertEquals(3, testObject.numberOfArithmeticSlices(nums)); 15 | 16 | nums = new int[]{1}; 17 | assertEquals(0, testObject.numberOfArithmeticSlices(nums)); 18 | 19 | } 20 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/dp/CountAllValidPickupAndDeliveryOptionsTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class CountAllValidPickupAndDeliveryOptionsTest { 8 | 9 | private final CountAllValidPickupAndDeliveryOptions testObject = new CountAllValidPickupAndDeliveryOptions(); 10 | 11 | @Test 12 | public void testCountOrders() { 13 | assertEquals(1, testObject.countOrders(1)); 14 | assertEquals(6, testObject.countOrders(2)); 15 | assertEquals(90, testObject.countOrders(3)); 16 | } 17 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/string/DetectCapitalTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.string; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertFalse; 6 | import static org.junit.jupiter.api.Assertions.assertTrue; 7 | 8 | public class DetectCapitalTest { 9 | 10 | private final DetectCapital testObject = new DetectCapital(); 11 | 12 | @Test 13 | public void testDetectCapitalUse() { 14 | assertTrue(testObject.detectCapitalUse("INDIA")); 15 | assertFalse(testObject.detectCapitalUse("FlaG")); 16 | assertTrue(testObject.detectCapitalUse("leetcode")); 17 | } 18 | } -------------------------------------------------------------------------------- /WorkAtTech/src/test/java/org/redquark/onlinejudges/workattech/dp/MaximumProductSubarrayTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.workattech.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class MaximumProductSubarrayTest { 8 | 9 | private final MaximumProductSubarray testObject = new MaximumProductSubarray(); 10 | 11 | @Test 12 | public void testMaxProduct() { 13 | int[] A = new int[]{-1, 3, 2, -1, -2, 3, 0, -2}; 14 | assertEquals(36, testObject.maxProduct(A)); 15 | 16 | A = new int[]{3, 0, -1, -2, 3, 0, -2}; 17 | assertEquals(6, testObject.maxProduct(A)); 18 | } 19 | } -------------------------------------------------------------------------------- /LeetCode/src/main/java/org/redquark/onlinejudges/leetcode/bitmagic/SingleNumber.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.bitmagic; 2 | 3 | /** 4 | * @author Anirudh Sharma 5 | */ 6 | public class SingleNumber { 7 | 8 | public int singleNumber(int[] nums) { 9 | // We will use XOR property which is a ^ a = 0 10 | // and a ^ b = 1 where a != b. 11 | // Since every number except one is repeating twice, 12 | // their XOR will become 0 and only the single number 13 | // will remain 14 | int xor = nums[0]; 15 | for (int i = 1; i < nums.length; i++) { 16 | xor ^= nums[i]; 17 | } 18 | return xor; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/array/MinimizeMaximumOfArrayTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class MinimizeMaximumOfArrayTest { 8 | 9 | private final MinimizeMaximumOfArray testObject = new MinimizeMaximumOfArray(); 10 | 11 | @Test 12 | public void testMinimizeArrayValue() { 13 | int[] nums = new int[]{3, 7, 1, 6}; 14 | assertEquals(5, testObject.minimizeArrayValue(nums)); 15 | 16 | nums = new int[]{10, 1}; 17 | assertEquals(10, testObject.minimizeArrayValue(nums)); 18 | } 19 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/design/EncodeAndDecodeTinyURLTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.design; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | import static org.redquark.onlinejudges.leetcode.design.EncodeAndDecodeTinyURL.Codec; 7 | 8 | public class EncodeAndDecodeTinyURLTest { 9 | 10 | @Test 11 | public void test() { 12 | Codec testObject = new Codec(); 13 | String longUrl = "https://leetcode.com/problems/design-tinyurl"; 14 | String shortUrl = testObject.encode(longUrl); 15 | assertEquals(longUrl, testObject.decode(shortUrl)); 16 | } 17 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/dp/BestTimeToBuyAndSellStocksTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class BestTimeToBuyAndSellStocksTest { 8 | 9 | private final BestTimeToBuyAndSellStock testObject = new BestTimeToBuyAndSellStock(); 10 | 11 | @Test 12 | public void testMaxProfit() { 13 | int[] prices = new int[]{7, 1, 5, 3, 6, 4}; 14 | assertEquals(5, testObject.maxProfit(prices)); 15 | 16 | prices = new int[]{7, 6, 4, 3, 1}; 17 | assertEquals(0, testObject.maxProfit(prices)); 18 | } 19 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/dp/EditDistanceTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class EditDistanceTest { 8 | 9 | private final EditDistance testObject = new EditDistance(); 10 | 11 | @Test 12 | public void testMinDistance() { 13 | String word1 = "horse"; 14 | String word2 = "ros"; 15 | assertEquals(3, testObject.minDistance(word1, word2)); 16 | 17 | word1 = "intention"; 18 | word2 = "execution"; 19 | assertEquals(5, testObject.minDistance(word1, word2)); 20 | } 21 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/math/RomanToIntegerTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.math; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class RomanToIntegerTest { 8 | 9 | private final RomanToInteger testObject = new RomanToInteger(); 10 | 11 | @Test 12 | public void testRomanToInt() { 13 | String s = "III"; 14 | assertEquals(3, testObject.romanToInt(s)); 15 | 16 | s = "LVIII"; 17 | assertEquals(58, testObject.romanToInt(s)); 18 | 19 | s = "MCMXCIV"; 20 | assertEquals(1994, testObject.romanToInt(s)); 21 | } 22 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/stack/MinStackTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.stack; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class MinStackTest { 8 | 9 | private final MinStack testMinStack = new MinStack(); 10 | 11 | @Test 12 | public void test() { 13 | testMinStack.push(-2); 14 | testMinStack.push(0); 15 | testMinStack.push(-3); 16 | assertEquals(-3, testMinStack.getMin()); 17 | testMinStack.pop(); 18 | assertEquals(0, testMinStack.top()); 19 | assertEquals(-2, testMinStack.getMin()); 20 | } 21 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/bitmagic/DivideTwoIntegersTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.bitmagic; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class DivideTwoIntegersTest { 8 | 9 | private final DivideTwoIntegers testObject = new DivideTwoIntegers(); 10 | 11 | @Test 12 | public void testDivide() { 13 | int dividend = 10; 14 | int divisor = 3; 15 | assertEquals(3, testObject.divide(dividend, divisor)); 16 | 17 | dividend = 7; 18 | divisor = -3; 19 | assertEquals(-2, testObject.divide(dividend, divisor)); 20 | } 21 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/dp/BestSightSeeingPairTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class BestSightSeeingPairTest { 8 | 9 | private final BestSightSeeingPair testObject = new BestSightSeeingPair(); 10 | 11 | @Test 12 | public void testMaxScoreSightseeingPair() { 13 | int[] values = new int[]{8, 1, 5, 2, 6}; 14 | assertEquals(11, testObject.maxScoreSightseeingPair(values)); 15 | 16 | values = new int[]{1, 2}; 17 | assertEquals(2, testObject.maxScoreSightseeingPair(values)); 18 | } 19 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/dp/TargetSumTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class TargetSumTest { 8 | 9 | private final TargetSum testObject = new TargetSum(); 10 | 11 | @Test 12 | public void testFindTargetSumWays() { 13 | int[] nums = new int[]{1, 1, 1, 1, 1}; 14 | int target = 3; 15 | assertEquals(5, testObject.findTargetSumWays(nums, target)); 16 | 17 | nums = new int[]{1}; 18 | target = 1; 19 | assertEquals(1, testObject.findTargetSumWays(nums, target)); 20 | } 21 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/string/FindTheDifferenceTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.string; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class FindTheDifferenceTest { 8 | 9 | private final FindTheDifference testObject = new FindTheDifference(); 10 | 11 | @Test 12 | public void testFindTheDifference() { 13 | String s = "abcd"; 14 | String t = "abcde"; 15 | assertEquals('e', testObject.findTheDifference(s, t)); 16 | 17 | s = ""; 18 | t = "y"; 19 | assertEquals('y', testObject.findTheDifference(s, t)); 20 | } 21 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/string/RemoveDuplicateCharactersTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.string; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class RemoveDuplicateCharactersTest { 8 | 9 | private final RemoveDuplicateCharacters testObject = new RemoveDuplicateCharacters(); 10 | 11 | @Test 12 | public void testRemoveDuplicateLetters() { 13 | String s = "bcabc"; 14 | assertEquals("abc", testObject.removeDuplicateLetters(s)); 15 | 16 | s = "cbacdcbc"; 17 | assertEquals("acdb", testObject.removeDuplicateLetters(s)); 18 | } 19 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/array/MaxConsecutiveOnesTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class MaxConsecutiveOnesTest { 8 | 9 | private final MaxConsecutiveOnes testObject = new MaxConsecutiveOnes(); 10 | 11 | @Test 12 | public void testFindMaxConsecutiveOnes() { 13 | int[] nums = new int[]{1, 1, 0, 1, 1, 1}; 14 | assertEquals(3, testObject.findMaxConsecutiveOnes(nums)); 15 | 16 | nums = new int[]{1, 0, 1, 1, 0, 1}; 17 | assertEquals(2, testObject.findMaxConsecutiveOnes(nums)); 18 | } 19 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/array/SubarraySumEqualsKTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class SubarraySumEqualsKTest { 8 | 9 | private final SubarraySumEqualsK testObject = new SubarraySumEqualsK(); 10 | 11 | @Test 12 | public void testSubarraySum() { 13 | int[] nums = new int[]{1, 1, 1}; 14 | int k = 2; 15 | assertEquals(2, testObject.subarraySum(nums, k)); 16 | 17 | nums = new int[]{1, 2, 3}; 18 | k = 3; 19 | assertEquals(2, testObject.subarraySum(nums, k)); 20 | } 21 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/dp/CountingBitsTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 6 | 7 | public class CountingBitsTest { 8 | 9 | private final CountingBits testObject = new CountingBits(); 10 | 11 | @Test 12 | public void testCountBits() { 13 | int n = 2; 14 | int[] expected = new int[]{0, 1, 1}; 15 | assertArrayEquals(expected, testObject.countBits(n)); 16 | 17 | n = 5; 18 | expected = new int[]{0, 1, 1, 2, 1, 2}; 19 | assertArrayEquals(expected, testObject.countBits(n)); 20 | } 21 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/dp/HouseRobberIITest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class HouseRobberIITest { 8 | 9 | private final HouseRobberII testObject = new HouseRobberII(); 10 | 11 | @Test 12 | public void testRob() { 13 | int[] nums = new int[]{2, 3, 2}; 14 | assertEquals(3, testObject.rob(nums)); 15 | 16 | nums = new int[]{1, 2, 3, 1}; 17 | assertEquals(4, testObject.rob(nums)); 18 | 19 | nums = new int[]{1, 2, 3}; 20 | assertEquals(3, testObject.rob(nums)); 21 | } 22 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/dp/RangeSumQueryImmutableTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | import static org.redquark.onlinejudges.leetcode.dp.RangeSumQueryImmutable.NumArray; 7 | 8 | public class RangeSumQueryImmutableTest { 9 | 10 | @Test 11 | public void test() { 12 | int[] nums = {-2, 0, 3, -5, 2, -1}; 13 | NumArray numArray = new NumArray(nums); 14 | assertEquals(1, numArray.sumRange(0, 2)); 15 | assertEquals(-1, numArray.sumRange(2, 5)); 16 | assertEquals(-3, numArray.sumRange(0, 5)); 17 | } 18 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/string/MaximumNumberOfBalloonsTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.string; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class MaximumNumberOfBalloonsTest { 8 | 9 | private final MaximumNumberOfBalloons testObject = new MaximumNumberOfBalloons(); 10 | 11 | @Test 12 | public void testMaxNumberOfBalloons() { 13 | assertEquals(1, testObject.maxNumberOfBalloons("nlaebolko")); 14 | assertEquals(2, testObject.maxNumberOfBalloons("loonbalxballpoon")); 15 | assertEquals(0, testObject.maxNumberOfBalloons("leetcode")); 16 | } 17 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/array/BinarySearchTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class BinarySearchTest { 8 | 9 | private final BinarySearch testObject = new BinarySearch(); 10 | 11 | @Test 12 | public void testSearch() { 13 | int[] nums = new int[]{-1, 0, 3, 5, 9, 12}; 14 | int target = 9; 15 | assertEquals(4, testObject.search(nums, target)); 16 | 17 | nums = new int[]{-1, 0, 3, 5, 9, 12}; 18 | target = 2; 19 | assertEquals(-1, testObject.search(nums, target)); 20 | } 21 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/bitmagic/NumberOf1BitsTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.bitmagic; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class NumberOf1BitsTest { 8 | 9 | private final NumberOf1Bits testObject = new NumberOf1Bits(); 10 | 11 | @Test 12 | public void testHammingWeight() { 13 | int n = Integer.parseInt("00000000000000000000000000001011", 2); 14 | assertEquals(3, testObject.hammingWeight(n)); 15 | 16 | n = Integer.parseInt("00000000000000000000000010000000", 2); 17 | assertEquals(1, testObject.hammingWeight(n)); 18 | } 19 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/dp/LongestPalindromicSubsequenceTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class LongestPalindromicSubsequenceTest { 8 | 9 | private final LongestPalindromicSubsequence testObject = new LongestPalindromicSubsequence(); 10 | 11 | @Test 12 | public void testLongestPalindromeSubsequence() { 13 | String s = "bbbab"; 14 | assertEquals(4, testObject.longestPalindromeSubsequence(s)); 15 | 16 | s = "cbbd"; 17 | assertEquals(2, testObject.longestPalindromeSubsequence(s)); 18 | } 19 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/graph/SimilarStringGroupsTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.graph; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class SimilarStringGroupsTest { 8 | 9 | private final SimilarStringGroups testObject = new SimilarStringGroups(); 10 | 11 | @Test 12 | public void testNumSimilarGroups() { 13 | String[] strs = new String[]{"tars", "rats", "arts", "star"}; 14 | assertEquals(2, testObject.numSimilarGroups(strs)); 15 | 16 | strs = new String[]{"omv", "ovm"}; 17 | assertEquals(1, testObject.numSimilarGroups(strs)); 18 | } 19 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/string/DefangingAnIPAddressTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.string; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class DefangingAnIPAddressTest { 8 | 9 | private final DefangingAnIPAddress testObject = new DefangingAnIPAddress(); 10 | 11 | @Test 12 | public void testDefangIPaddress() { 13 | String address = "1.1.1.1"; 14 | assertEquals("1[.]1[.]1[.]1", testObject.defangIPaddress(address)); 15 | 16 | address = "255.100.50.0"; 17 | assertEquals("255[.]100[.]50[.]0", testObject.defangIPaddress(address)); 18 | } 19 | } -------------------------------------------------------------------------------- /LeetCode/src/main/java/org/redquark/onlinejudges/leetcode/dp/NthTribonacciNumber.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.dp; 2 | 3 | /** 4 | * @author Anirudh Sharma 5 | */ 6 | public class NthTribonacciNumber { 7 | 8 | public int tribonacci(int n) { 9 | // Special cases 10 | if (n == 0) { 11 | return 0; 12 | } 13 | if (n <= 2) { 14 | return 1; 15 | } 16 | int a = 0; 17 | int b = 1; 18 | int c = 1; 19 | int d = 0; 20 | for (int i = 3; i <= n; i++) { 21 | d = a + b + c; 22 | a = b; 23 | b = c; 24 | c = d; 25 | } 26 | return d; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/array/MaximumErasureValueTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class MaximumErasureValueTest { 8 | 9 | private final MaximumErasureValue testObject = new MaximumErasureValue(); 10 | 11 | @Test 12 | public void testMaximumUniqueSubarray() { 13 | int[] nums = new int[]{4, 2, 4, 5, 6}; 14 | assertEquals(17, testObject.maximumUniqueSubarray(nums)); 15 | 16 | nums = new int[]{5, 2, 1, 2, 5, 2, 1, 2, 5}; 17 | assertEquals(8, testObject.maximumUniqueSubarray(nums)); 18 | } 19 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/math/MinimumMovesToEqualArrayElementsIITest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.math; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class MinimumMovesToEqualArrayElementsIITest { 8 | 9 | private final MinimumMovesToEqualArrayElementsII testObject = new MinimumMovesToEqualArrayElementsII(); 10 | 11 | @Test 12 | public void testMinMoves2() { 13 | int[] nums = new int[]{1, 2, 3}; 14 | assertEquals(2, testObject.minMoves2(nums)); 15 | 16 | nums = new int[]{1, 10, 2, 9}; 17 | assertEquals(16, testObject.minMoves2(nums)); 18 | } 19 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/string/ReverseWordsInAStringIIITest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.string; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class ReverseWordsInAStringIIITest { 8 | 9 | private final ReverseWordsInAStringIII testObject = new ReverseWordsInAStringIII(); 10 | 11 | @Test 12 | public void testReverseWords() { 13 | String s = "Let's take LeetCode contest"; 14 | assertEquals("s'teL ekat edoCteeL tsetnoc", testObject.reverseWords(s)); 15 | 16 | s = "God Ding"; 17 | assertEquals("doG gniD", testObject.reverseWords(s)); 18 | } 19 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/string/ValidAnagramTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.string; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertFalse; 6 | import static org.junit.jupiter.api.Assertions.assertTrue; 7 | 8 | public class ValidAnagramTest { 9 | 10 | private final ValidAnagram testObject = new ValidAnagram(); 11 | 12 | @Test 13 | public void testIsAnagram() { 14 | String s = "anagram"; 15 | String t = "nagaram"; 16 | assertTrue(testObject.isAnagram(s, t)); 17 | 18 | s = "rat"; 19 | t = "car"; 20 | assertFalse(testObject.isAnagram(s, t)); 21 | } 22 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/array/RemoveElementTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class RemoveElementTest { 8 | 9 | private final RemoveElement testObject = new RemoveElement(); 10 | 11 | @Test 12 | public void testRemoveElement() { 13 | int[] nums = new int[]{3, 2, 2, 3}; 14 | int val = 3; 15 | assertEquals(2, testObject.removeElement(nums, val)); 16 | 17 | nums = new int[]{0, 1, 2, 2, 3, 0, 4, 2}; 18 | val = 2; 19 | assertEquals(5, testObject.removeElement(nums, val)); 20 | } 21 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/dp/BestTimeToBuyAndSellStockWithCooldownTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class BestTimeToBuyAndSellStockWithCooldownTest { 8 | 9 | private final BestTimeToBuyAndSellStockWithCooldown testObject = new BestTimeToBuyAndSellStockWithCooldown(); 10 | 11 | @Test 12 | void testMaxProfit() { 13 | int[] prices = new int[]{1, 2, 3, 0, 2}; 14 | assertEquals(3, testObject.maxProfit(prices)); 15 | 16 | prices = new int[]{1}; 17 | assertEquals(0, testObject.maxProfit(prices)); 18 | } 19 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/dp/CombinationSumIVTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class CombinationSumIVTest { 8 | 9 | private final CombinationSumIV testObject = new CombinationSumIV(); 10 | 11 | @Test 12 | public void testCombinationSum4() { 13 | int[] nums = new int[]{1, 2, 3}; 14 | int target = 4; 15 | assertEquals(7, testObject.combinationSum4(nums, target)); 16 | 17 | nums = new int[]{9}; 18 | target = 3; 19 | assertEquals(0, testObject.combinationSum4(nums, target)); 20 | } 21 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/string/ShortEncodingOfWordsTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.string; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class ShortEncodingOfWordsTest { 8 | 9 | private final ShortEncodingOfWords testObject = new ShortEncodingOfWords(); 10 | 11 | @Test 12 | public void testMinimumLengthEncoding() { 13 | String[] words = new String[]{"time", "me", "bell"}; 14 | assertEquals(10, testObject.minimumLengthEncoding(words)); 15 | 16 | words = new String[]{"t"}; 17 | assertEquals(2, testObject.minimumLengthEncoding(words)); 18 | } 19 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/array/NonDecreasingArrayTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertFalse; 6 | import static org.junit.jupiter.api.Assertions.assertTrue; 7 | 8 | public class NonDecreasingArrayTest { 9 | 10 | private final NonDecreasingArray testObject = new NonDecreasingArray(); 11 | 12 | @Test 13 | public void testCheckPossibility() { 14 | int[] nums = new int[]{4, 2, 3}; 15 | assertTrue(testObject.checkPossibility(nums)); 16 | 17 | nums = new int[]{4, 2, 1}; 18 | assertFalse(testObject.checkPossibility(nums)); 19 | } 20 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/dp/MinCostClimbingStairsTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class MinCostClimbingStairsTest { 8 | 9 | private final MinCostClimbingStairs testObject = new MinCostClimbingStairs(); 10 | 11 | @Test 12 | public void testMinCostClimbingStairs() { 13 | int[] cost = new int[]{10, 15, 20}; 14 | assertEquals(15, testObject.minCostClimbingStairs(cost)); 15 | 16 | cost = new int[]{1, 100, 1, 1, 1, 100, 1, 1, 100, 1}; 17 | assertEquals(6, testObject.minCostClimbingStairs(cost)); 18 | } 19 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/array/MaxNumberOfKSumPairsTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class MaxNumberOfKSumPairsTest { 8 | 9 | private final MaxNumberOfKSumPairs testObject = new MaxNumberOfKSumPairs(); 10 | 11 | @Test 12 | public void testMaxOperations() { 13 | int[] nums = new int[]{1, 2, 3, 4}; 14 | int k = 5; 15 | assertEquals(2, testObject.maxOperations(nums, k)); 16 | 17 | nums = new int[]{3, 1, 3, 4, 3}; 18 | k = 6; 19 | assertEquals(1, testObject.maxOperations(nums, k)); 20 | } 21 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/math/ReverseIntegerTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.math; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class ReverseIntegerTest { 8 | 9 | private final ReverseInteger testObject = new ReverseInteger(); 10 | 11 | @Test 12 | public void testReverse() { 13 | assertEquals(321, testObject.reverse(123)); 14 | assertEquals(-321, testObject.reverse(-123)); 15 | assertEquals(21, testObject.reverse(120)); 16 | assertEquals(0, testObject.reverse(Integer.MAX_VALUE)); 17 | assertEquals(0, testObject.reverse(Integer.MIN_VALUE)); 18 | } 19 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/string/IsSubsequenceTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.string; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertFalse; 6 | import static org.junit.jupiter.api.Assertions.assertTrue; 7 | 8 | public class IsSubsequenceTest { 9 | 10 | private final IsSubsequence testObject = new IsSubsequence(); 11 | 12 | @Test 13 | public void testIsSubsequence() { 14 | String s = "abc"; 15 | String t = "ahbgdc"; 16 | assertTrue(testObject.isSubsequence(s, t)); 17 | 18 | s = "axc"; 19 | t = "ahbgdc"; 20 | assertFalse(testObject.isSubsequence(s, t)); 21 | } 22 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/string/StringToIntegerAtoiTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.string; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class StringToIntegerAtoiTest { 8 | 9 | private final StringToIntegerAtoi testObject = new StringToIntegerAtoi(); 10 | 11 | @Test 12 | public void testMyAtoi() { 13 | assertEquals(42, testObject.myAtoi("42")); 14 | assertEquals(-42, testObject.myAtoi(" -42")); 15 | assertEquals(4193, testObject.myAtoi("4193 with words")); 16 | assertEquals(Integer.MAX_VALUE, testObject.myAtoi(" 34675434436742146635av")); 17 | } 18 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/array/BoatsToSavePeopleTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class BoatsToSavePeopleTest { 8 | 9 | private final BoatsToSavePeople testObject = new BoatsToSavePeople(); 10 | 11 | @Test 12 | public void testNumRescueBoats() { 13 | int[] people = new int[]{1, 2}; 14 | int limit = 3; 15 | assertEquals(1, testObject.numRescueBoats(people, limit)); 16 | 17 | people = new int[]{3, 5, 3, 4}; 18 | limit = 5; 19 | assertEquals(4, testObject.numRescueBoats(people, limit)); 20 | } 21 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/dp/PartitionEqualSubsetSumTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertFalse; 6 | import static org.junit.jupiter.api.Assertions.assertTrue; 7 | 8 | public class PartitionEqualSubsetSumTest { 9 | 10 | private final PartitionEqualSubsetSum testObject = new PartitionEqualSubsetSum(); 11 | 12 | @Test 13 | public void testCanPartition() { 14 | int[] nums = new int[]{1, 5, 11, 5}; 15 | assertTrue(testObject.canPartition(nums)); 16 | 17 | nums = new int[]{1, 2, 3, 5}; 18 | assertFalse(testObject.canPartition(nums)); 19 | } 20 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/stack/ScoreOfParenthesesTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.stack; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class ScoreOfParenthesesTest { 8 | 9 | private final ScoreOfParentheses testObject = new ScoreOfParentheses(); 10 | 11 | @Test 12 | public void testScoreOfParentheses() { 13 | String s = "()"; 14 | assertEquals(1, testObject.scoreOfParentheses(s)); 15 | 16 | s = "(())"; 17 | assertEquals(2, testObject.scoreOfParentheses(s)); 18 | 19 | s = "()()"; 20 | assertEquals(2, testObject.scoreOfParentheses(s)); 21 | } 22 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/stack/SimplifyPathTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.stack; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class SimplifyPathTest { 8 | 9 | private final SimplifyPath testObject = new SimplifyPath(); 10 | 11 | @Test 12 | public void testSimplifyPath() { 13 | String path = "/home/"; 14 | assertEquals("/home", testObject.simplifyPath(path)); 15 | 16 | path = "/../"; 17 | assertEquals("/", testObject.simplifyPath(path)); 18 | 19 | path = "/home//foo/"; 20 | assertEquals("/home/foo", testObject.simplifyPath(path)); 21 | } 22 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/string/LongestCommonPrefixTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.string; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class LongestCommonPrefixTest { 8 | 9 | private final LongestCommonPrefix testObject = new LongestCommonPrefix(); 10 | 11 | @Test 12 | public void testLongestCommonPrefix() { 13 | String[] strs = new String[]{"flower", "flow", "flight"}; 14 | assertEquals("fl", testObject.longestCommonPrefix(strs)); 15 | 16 | strs = new String[]{"dog", "racecar", "car"}; 17 | assertEquals("", testObject.longestCommonPrefix(strs)); 18 | } 19 | } -------------------------------------------------------------------------------- /WorkAtTech/src/main/java/org/redquark/onlinejudges/workattech/dp/ClimbingStairs.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.workattech.dp; 2 | 3 | public class ClimbingStairs { 4 | 5 | public int climbStairs(int n) { 6 | // Special case 7 | if (n < 3) { 8 | return n; 9 | } 10 | // Lookup table to store the number of 11 | // ways to climb n stairs 12 | int[] lookup = new int[n + 1]; 13 | // Base initialization 14 | lookup[1] = 1; 15 | lookup[2] = 2; 16 | // Populate for remaining stairs 17 | for (int i = 3; i <= n; i++) { 18 | lookup[i] = lookup[i - 1] + lookup[i - 2]; 19 | } 20 | return lookup[n]; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/dp/BestTimeToBuyAndSellStockIVTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class BestTimeToBuyAndSellStockIVTest { 8 | 9 | private final BestTimeToBuyAndSellStockIV testObject = new BestTimeToBuyAndSellStockIV(); 10 | 11 | @Test 12 | void testMaxProfit() { 13 | int k = 2; 14 | int[] prices = new int[]{2, 4, 1}; 15 | assertEquals(2, testObject.maxProfit(k, prices)); 16 | 17 | k = 2; 18 | prices = new int[]{3, 2, 6, 5, 0, 3}; 19 | assertEquals(7, testObject.maxProfit(k, prices)); 20 | } 21 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/dp/SuperEggDropTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class SuperEggDropTest { 8 | 9 | private final SuperEggDrop testObject = new SuperEggDrop(); 10 | 11 | @Test 12 | public void testSuperEggDrop() { 13 | int k = 1; 14 | int n = 2; 15 | assertEquals(2, testObject.superEggDrop(k, n)); 16 | 17 | k = 2; 18 | n = 6; 19 | assertEquals(3, testObject.superEggDrop(k, n)); 20 | 21 | k = 3; 22 | n = 14; 23 | assertEquals(4, testObject.superEggDrop(k, n)); 24 | } 25 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/stack/LargestRectangleInHistogramTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.stack; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class LargestRectangleInHistogramTest { 8 | 9 | private final LargestRectangleInHistogram testObject = new LargestRectangleInHistogram(); 10 | 11 | @Test 12 | public void testLargestRectangleArea() { 13 | int[] heights = new int[]{2, 1, 5, 6, 2, 3}; 14 | assertEquals(10, testObject.largestRectangleArea(heights)); 15 | 16 | heights = new int[]{2, 4}; 17 | assertEquals(4, testObject.largestRectangleArea(heights)); 18 | } 19 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/array/LongestConsecutiveSequenceTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class LongestConsecutiveSequenceTest { 8 | 9 | private final LongestConsecutiveSequence testObject = new LongestConsecutiveSequence(); 10 | 11 | @Test 12 | public void testLongestConsecutive() { 13 | int[] nums = new int[]{200, 4, 100, 1, 3, 2}; 14 | assertEquals(4, testObject.longestConsecutive(nums)); 15 | 16 | nums = new int[]{0, 3, 7, 2, 5, 8, 4, 6, 0, 1}; 17 | assertEquals(9, testObject.longestConsecutive(nums)); 18 | } 19 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/array/MoveZeroesTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 6 | 7 | public class MoveZeroesTest { 8 | 9 | private final MoveZeroes testObject = new MoveZeroes(); 10 | 11 | @Test 12 | public void testMoveZeroes() { 13 | int[] nums = new int[]{0, 1, 0, 3, 12}; 14 | int[] expected = new int[]{1, 3, 12, 0, 0}; 15 | assertArrayEquals(expected, testObject.moveZeroes(nums)); 16 | 17 | nums = new int[]{0}; 18 | expected = new int[]{0}; 19 | assertArrayEquals(expected, testObject.moveZeroes(nums)); 20 | } 21 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/array/RemoveDuplicatesFromSortedArrayTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class RemoveDuplicatesFromSortedArrayTest { 8 | 9 | private final RemoveDuplicatesFromSortedArray testObject = new RemoveDuplicatesFromSortedArray(); 10 | 11 | @Test 12 | public void testRemoveDuplicates() { 13 | int[] nums = new int[]{1, 1, 2}; 14 | assertEquals(2, testObject.removeDuplicates(nums)); 15 | 16 | nums = new int[]{0, 0, 1, 1, 1, 2, 2, 3, 3, 4}; 17 | assertEquals(5, testObject.removeDuplicates(nums)); 18 | } 19 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/array/SingleElementInASortedArrayTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class SingleElementInASortedArrayTest { 8 | 9 | private final SingleElementInASortedArray testObject = new SingleElementInASortedArray(); 10 | 11 | @Test 12 | public void testSingleNonDuplicate() { 13 | int[] nums = new int[]{1, 1, 2, 3, 3, 4, 4, 8, 8}; 14 | assertEquals(2, testObject.singleNonDuplicate(nums)); 15 | 16 | nums = new int[]{3, 3, 7, 7, 10, 11, 11}; 17 | assertEquals(10, testObject.singleNonDuplicate(nums)); 18 | } 19 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/dp/NumberOfLongestIncreasingSubsequenceTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class NumberOfLongestIncreasingSubsequenceTest { 8 | 9 | private final NumberOfLongestIncreasingSubsequence testObject = new NumberOfLongestIncreasingSubsequence(); 10 | 11 | @Test 12 | public void testFindNumberOfLIS() { 13 | int[] nums = new int[]{1, 3, 5, 4, 7}; 14 | assertEquals(2, testObject.findNumberOfLIS(nums)); 15 | 16 | nums = new int[]{2, 2, 2, 2, 2}; 17 | assertEquals(5, testObject.findNumberOfLIS(nums)); 18 | } 19 | } -------------------------------------------------------------------------------- /WorkAtTech/src/test/java/org/redquark/onlinejudges/workattech/dp/MaximumPathSumInMatrixTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.workattech.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class MaximumPathSumInMatrixTest { 8 | 9 | private final MaximumPathSumInMatrix testObject = new MaximumPathSumInMatrix(); 10 | 11 | @Test 12 | public void testFindMaxPath() { 13 | int[][] M = new int[][]{ 14 | {12, 22, 5, 0, 20, 18}, 15 | {0, 2, 5, 25, 18, 17}, 16 | {12, 10, 5, 4, 2, 1}, 17 | {3, 8, 2, 20, 10, 8} 18 | }; 19 | assertEquals(70, testObject.findMaxPath(M)); 20 | } 21 | } -------------------------------------------------------------------------------- /WorkAtTech/src/test/java/org/redquark/onlinejudges/workattech/dp/MaximumSumIncreasingSubsequenceTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.workattech.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class MaximumSumIncreasingSubsequenceTest { 8 | 9 | private final MaximumSumIncreasingSubsequence testObject = new MaximumSumIncreasingSubsequence(); 10 | 11 | @Test 12 | public void testMaxSumSubsequence() { 13 | int[] nums = new int[]{101, 4, 98, 103}; 14 | assertEquals(205, testObject.maxSumSubsequence(nums)); 15 | 16 | nums = new int[]{101, 4, 95, 103}; 17 | assertEquals(204, testObject.maxSumSubsequence(nums)); 18 | } 19 | } -------------------------------------------------------------------------------- /LeetCode/src/main/java/org/redquark/onlinejudges/leetcode/math/PalindromeNumber.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.math; 2 | 3 | /** 4 | * @author Anirudh Sharma 5 | */ 6 | public class PalindromeNumber { 7 | 8 | public boolean isPalindrome(int x) { 9 | // Special case 10 | if (x < 0) { 11 | return false; 12 | } 13 | // Variable to store the original number 14 | int original = x; 15 | // Variable to store the constructed number 16 | long reversed = 0; 17 | // Loop until x is not zero 18 | while (x > 0) { 19 | reversed = reversed * 10 + x % 10; 20 | x /= 10; 21 | } 22 | return original == reversed; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/array/GasStationTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class GasStationTest { 8 | 9 | private final GasStation testObject = new GasStation(); 10 | 11 | @Test 12 | public void testCanCompleteCircuit() { 13 | int[] gas = new int[]{1, 2, 3, 4, 5}; 14 | int[] cost = new int[]{3, 4, 5, 1, 2}; 15 | assertEquals(3, testObject.canCompleteCircuit(gas, cost)); 16 | 17 | gas = new int[]{2, 3, 4}; 18 | cost = new int[]{3, 4, 3}; 19 | assertEquals(-1, testObject.canCompleteCircuit(gas, cost)); 20 | } 21 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/bitmagic/SingleNumberTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.bitmagic; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class SingleNumberTest { 8 | 9 | private final SingleNumber testObject = new SingleNumber(); 10 | 11 | @Test 12 | public void testSingleNumber() { 13 | int[] nums = new int[]{2, 2, 1}; 14 | assertEquals(1, testObject.singleNumber(nums)); 15 | 16 | nums = new int[]{4, 1, 2, 1, 2}; 17 | assertEquals(4, testObject.singleNumber(nums)); 18 | 19 | nums = new int[]{1}; 20 | assertEquals(1, testObject.singleNumber(nums)); 21 | } 22 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/dp/DeleteOperationForTwoStringsTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class DeleteOperationForTwoStringsTest { 8 | 9 | private final DeleteOperationForTwoStrings testObject = new DeleteOperationForTwoStrings(); 10 | 11 | @Test 12 | public void testMinDistance() { 13 | String word1 = "sea"; 14 | String word2 = "eat"; 15 | assertEquals(2, testObject.minDistance(word1, word2)); 16 | 17 | word1 = "leetcode"; 18 | word2 = "etco"; 19 | assertEquals(4, testObject.minDistance(word1, word2)); 20 | } 21 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/heap/FindMedianFromDataStreamTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.heap; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | import static org.redquark.onlinejudges.leetcode.heap.FindMedianFromDataStream.MedianFinder; 7 | 8 | public class FindMedianFromDataStreamTest { 9 | 10 | private final MedianFinder testObject = new MedianFinder(); 11 | 12 | @Test 13 | public void test() { 14 | testObject.addNum(1); 15 | testObject.addNum(2); 16 | assertEquals(1.5, testObject.findMedian()); 17 | testObject.addNum(3); 18 | assertEquals(2.0, testObject.findMedian()); 19 | } 20 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/stack/MinimumRemoveToMakeValidParenthesesTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.stack; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class MinimumRemoveToMakeValidParenthesesTest { 8 | 9 | private final MinimumRemoveToMakeValidParentheses testObject = new MinimumRemoveToMakeValidParentheses(); 10 | 11 | @Test 12 | public void testMinRemoveToMakeValid() { 13 | String s = "lee(t(c)o)de)"; 14 | assertEquals("lee(t(c)o)de", testObject.minRemoveToMakeValid(s)); 15 | 16 | s = "a)b(c)d"; 17 | assertEquals("ab(c)d", testObject.minRemoveToMakeValid(s)); 18 | } 19 | } -------------------------------------------------------------------------------- /LeetCode/src/main/java/org/redquark/onlinejudges/leetcode/array/BuildArrayFromPermutation.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | /** 4 | * @author Anirudh Sharma 5 | */ 6 | public class BuildArrayFromPermutation { 7 | 8 | public int[] buildArray(int[] nums) { 9 | // Special case 10 | if (nums == null || nums.length == 0) { 11 | return nums; 12 | } 13 | // Length of the array 14 | int n = nums.length; 15 | // Array to store the permutation based output 16 | int[] output = new int[n]; 17 | // Populate the array 18 | for (int i = 0; i < n; i++) { 19 | output[i] = nums[nums[i]]; 20 | } 21 | return output; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/dp/DecodeWaysTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class DecodeWaysTest { 8 | 9 | private final DecodeWays testObject = new DecodeWays(); 10 | 11 | @Test 12 | public void testNumDecodings() { 13 | String s = "12"; 14 | assertEquals(2, testObject.numDecodings(s)); 15 | 16 | s = "226"; 17 | assertEquals(3, testObject.numDecodings(s)); 18 | 19 | s = "0"; 20 | assertEquals(0, testObject.numDecodings(s)); 21 | 22 | s = "0"; 23 | assertEquals(0, testObject.numDecodings(s)); 24 | } 25 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/array/RemoveDuplicatesFromSortedArrayIITest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class RemoveDuplicatesFromSortedArrayIITest { 8 | 9 | private final RemoveDuplicatesFromSortedArrayII testObject = new RemoveDuplicatesFromSortedArrayII(); 10 | 11 | @Test 12 | public void testRemoveDuplicates() { 13 | int[] nums = new int[]{1, 1, 1, 2, 2, 3}; 14 | assertEquals(5, testObject.removeDuplicates(nums)); 15 | 16 | nums = new int[]{0, 0, 1, 1, 1, 1, 2, 3, 3}; 17 | assertEquals(7, testObject.removeDuplicates(nums)); 18 | } 19 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/array/SortColorsTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 6 | 7 | public class SortColorsTest { 8 | 9 | private final SortColors testObject = new SortColors(); 10 | 11 | @Test 12 | public void testSortColors() { 13 | int[] nums = new int[]{2, 0, 2, 1, 1, 0}; 14 | int[] expected = new int[]{0, 0, 1, 1, 2, 2}; 15 | assertArrayEquals(expected, testObject.sortColors(nums)); 16 | 17 | nums = new int[]{2, 0, 1}; 18 | expected = new int[]{0, 1, 2}; 19 | assertArrayEquals(expected, testObject.sortColors(nums)); 20 | } 21 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/string/LongestPalindromeTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.string; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class LongestPalindromeTest { 8 | 9 | private final LongestPalindrome longestPalindrome = new LongestPalindrome(); 10 | 11 | @Test 12 | public void testLongestPalindrome() { 13 | String s = "abccccdd"; 14 | assertEquals(7, longestPalindrome.longestPalindrome(s)); 15 | 16 | s = "a"; 17 | assertEquals(1, longestPalindrome.longestPalindrome(s)); 18 | 19 | s = "AAAAAA"; 20 | assertEquals(6, longestPalindrome.longestPalindrome(s)); 21 | } 22 | } -------------------------------------------------------------------------------- /LeetCode/src/main/java/org/redquark/onlinejudges/leetcode/dp/UniqueBinarySearchTrees.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.dp; 2 | 3 | /** 4 | * @author Anirudh Sharma 5 | */ 6 | public class UniqueBinarySearchTrees { 7 | 8 | public int numTrees(int n) { 9 | // Lookup table to store the unique BSTs with i-th number as the node 10 | int[] lookup = new int[n + 1]; 11 | // Base initialization 12 | lookup[0] = 1; 13 | lookup[1] = 1; 14 | // Populate the remaining lookup table 15 | for (int i = 2; i <= n; i++) { 16 | for (int j = 0; j < i; j++) { 17 | lookup[i] += lookup[j] * lookup[i - j - 1]; 18 | } 19 | } 20 | return lookup[n]; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /LeetCode/src/main/java/org/redquark/onlinejudges/leetcode/math/NumberOfStepsToReduceANumberToZero.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.math; 2 | 3 | /** 4 | * @author Anirudh Sharma 5 | */ 6 | public class NumberOfStepsToReduceANumberToZero { 7 | 8 | public int numberOfSteps(int num) { 9 | // Special case 10 | if (num == 0) { 11 | return 0; 12 | } 13 | // Total number of steps 14 | int steps = 0; 15 | // Loop until the num becomes zero 16 | while (num != 0) { 17 | if (num % 2 == 0) { 18 | num /= 2; 19 | } else { 20 | num -= 1; 21 | } 22 | steps++; 23 | } 24 | return steps; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /LeetCode/src/main/java/org/redquark/onlinejudges/leetcode/string/FinalValueOfVariableAfterPerformingOperations.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.string; 2 | 3 | /** 4 | * @author Anirudh Sharma 5 | */ 6 | public class FinalValueOfVariableAfterPerformingOperations { 7 | 8 | public int finalValueAfterOperations(String[] operations) { 9 | // Special case 10 | if (operations == null || operations.length == 0) { 11 | return 0; 12 | } 13 | // Variable to keep track of value of x 14 | int x = 0; 15 | // Loop through the operations array 16 | for (String operation : operations) { 17 | x += operation.charAt(1) == '+' ? 1 : -1; 18 | } 19 | return x; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/array/MissingNumberTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class MissingNumberTest { 8 | 9 | private final MissingNumber testObject = new MissingNumber(); 10 | 11 | @Test 12 | public void testMissingNumber() { 13 | int[] nums = new int[]{3, 0, 1}; 14 | assertEquals(2, testObject.missingNumber(nums)); 15 | 16 | nums = new int[]{0, 1}; 17 | assertEquals(2, testObject.missingNumber(nums)); 18 | 19 | nums = new int[]{9, 6, 4, 2, 3, 5, 7, 0, 1}; 20 | assertEquals(8, testObject.missingNumber(nums)); 21 | } 22 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/design/ImplementQueueUsingStacksTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.design; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | import static org.junit.jupiter.api.Assertions.assertFalse; 7 | import static org.redquark.onlinejudges.leetcode.design.ImplementQueueUsingStacks.MyQueue; 8 | 9 | public class ImplementQueueUsingStacksTest { 10 | 11 | @Test 12 | public void test() { 13 | MyQueue myQueue = new MyQueue(); 14 | myQueue.push(1); 15 | myQueue.push(2); 16 | assertEquals(1, myQueue.peek()); 17 | assertEquals(1, myQueue.pop()); 18 | assertFalse(myQueue.empty()); 19 | } 20 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/dp/DungeonGameTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class DungeonGameTest { 8 | 9 | private final DungeonGame testObject = new DungeonGame(); 10 | 11 | @Test 12 | public void testCalculateMinimumHP() { 13 | int[][] dungeon = new int[][]{ 14 | {-2, -3, 3}, 15 | {-5, -10, 1}, 16 | {10, 30, -5} 17 | }; 18 | assertEquals(7, testObject.calculateMinimumHP(dungeon)); 19 | 20 | dungeon = new int[][]{{0}}; 21 | assertEquals(1, testObject.calculateMinimumHP(dungeon)); 22 | } 23 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/string/CheckIfAStringContainsAllBinaryCodesOfSizeKTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.string; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertTrue; 6 | 7 | public class CheckIfAStringContainsAllBinaryCodesOfSizeKTest { 8 | 9 | private final CheckIfAStringContainsAllBinaryCodesOfSizeK testObject = new CheckIfAStringContainsAllBinaryCodesOfSizeK(); 10 | 11 | @Test 12 | public void testHasAllCodes() { 13 | String s = "00110110"; 14 | int k = 2; 15 | assertTrue(testObject.hasAllCodes(s, k)); 16 | 17 | s = "0110"; 18 | k = 1; 19 | assertTrue(testObject.hasAllCodes(s, k)); 20 | } 21 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/string/PermutationInStringTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.string; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertFalse; 6 | import static org.junit.jupiter.api.Assertions.assertTrue; 7 | 8 | public class PermutationInStringTest { 9 | 10 | private final PermutationInString testObject = new PermutationInString(); 11 | 12 | @Test 13 | public void testCheckInclusion() { 14 | String s1 = "ab"; 15 | String s2 = "eidbaooo"; 16 | assertTrue(testObject.checkInclusion(s1, s2)); 17 | 18 | s1 = "ab"; 19 | s2 = "eidboaoo"; 20 | assertFalse(testObject.checkInclusion(s1, s2)); 21 | } 22 | } -------------------------------------------------------------------------------- /WorkAtTech/src/test/java/org/redquark/onlinejudges/workattech/dp/DecodeWaysTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.workattech.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class DecodeWaysTest { 8 | 9 | private final DecodeWays testObject = new DecodeWays(); 10 | 11 | @Test 12 | public void testNumDecodings() { 13 | String s = "123"; 14 | assertEquals(3, testObject.numDecodings(s)); 15 | 16 | s = "36"; 17 | assertEquals(1, testObject.numDecodings(s)); 18 | 19 | s = "106"; 20 | assertEquals(1, testObject.numDecodings(s)); 21 | 22 | s = "306"; 23 | assertEquals(0, testObject.numDecodings(s)); 24 | } 25 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/array/SortAnArrayTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 6 | 7 | public class SortAnArrayTest { 8 | 9 | private final SortAnArray testObject = new SortAnArray(); 10 | 11 | @Test 12 | public void testSortArray() { 13 | int[] nums = new int[]{5, 2, 3, 1}; 14 | int[] expected = new int[]{1, 2, 3, 5}; 15 | assertArrayEquals(expected, testObject.sortArray(nums)); 16 | 17 | nums = new int[]{5, 1, 1, 2, 0, 0}; 18 | expected = new int[]{0, 0, 1, 1, 2, 5}; 19 | assertArrayEquals(expected, testObject.sortArray(nums)); 20 | } 21 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/string/ValidPalindromeIITest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.string; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertFalse; 6 | import static org.junit.jupiter.api.Assertions.assertTrue; 7 | 8 | public class ValidPalindromeIITest { 9 | 10 | private final ValidPalindromeII testObject = new ValidPalindromeII(); 11 | 12 | @Test 13 | public void testValidPalindrome() { 14 | String s = "aba"; 15 | assertTrue(testObject.validPalindrome(s)); 16 | 17 | s = "abca"; 18 | assertTrue(testObject.validPalindrome(s)); 19 | 20 | s = "abc"; 21 | assertFalse(testObject.validPalindrome(s)); 22 | } 23 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/array/KthLargestElementInAnArrayTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class KthLargestElementInAnArrayTest { 8 | 9 | private final KthLargestElementInAnArray testObject = new KthLargestElementInAnArray(); 10 | 11 | @Test 12 | public void testFindKthLargest() { 13 | int[] nums = new int[]{3, 2, 1, 5, 6, 4}; 14 | int k = 2; 15 | assertEquals(5, testObject.findKthLargest(nums, k)); 16 | 17 | nums = new int[]{3, 2, 3, 1, 2, 4, 5, 5, 6}; 18 | k = 4; 19 | assertEquals(4, testObject.findKthLargest(nums, k)); 20 | } 21 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/array/MaximumSubarrayTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class MaximumSubarrayTest { 8 | 9 | private final MaximumSubarray testObject = new MaximumSubarray(); 10 | 11 | @Test 12 | public void testMaxSubArray() { 13 | int[] nums = new int[]{-2, 1, -3, 4, -1, 2, 1, -5, 4}; 14 | assertEquals(6, testObject.maxSubArray(nums)); 15 | 16 | nums = new int[]{1}; 17 | assertEquals(1, testObject.maxSubArray(nums)); 18 | 19 | nums = new int[]{5, 4, -1, 7, 8}; 20 | assertEquals(23, testObject.maxSubArray(nums)); 21 | } 22 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/bitmagic/MaximumXOROfTwoNumbersInAnArrayTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.bitmagic; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class MaximumXOROfTwoNumbersInAnArrayTest { 8 | 9 | private final MaximumXOROfTwoNumbersInAnArray testObject = new MaximumXOROfTwoNumbersInAnArray(); 10 | 11 | @Test 12 | public void testFindMaximumXOR() { 13 | int[] nums = new int[]{3, 10, 5, 25, 2, 8}; 14 | assertEquals(28, testObject.findMaximumXOR(nums)); 15 | 16 | nums = new int[]{14, 70, 53, 83, 49, 91, 36, 80, 92, 51, 66, 70}; 17 | assertEquals(127, testObject.findMaximumXOR(nums)); 18 | } 19 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/design/LRUCacheTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.design; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class LRUCacheTest { 8 | 9 | @Test 10 | public void test() { 11 | LRUCache testObject = new LRUCache(2); 12 | testObject.put(1, 1); 13 | testObject.put(2, 2); 14 | assertEquals(1, testObject.get(1)); 15 | testObject.put(3, 3); 16 | assertEquals(-1, testObject.get(2)); 17 | testObject.put(4, 4); 18 | assertEquals(-1, testObject.get(1)); 19 | assertEquals(3, testObject.get(3)); 20 | assertEquals(4, testObject.get(4)); 21 | } 22 | 23 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/math/NumberOfStepsToReduceANumberToZeroTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.math; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class NumberOfStepsToReduceANumberToZeroTest { 8 | 9 | private final NumberOfStepsToReduceANumberToZero testObject = new NumberOfStepsToReduceANumberToZero(); 10 | 11 | @Test 12 | public void testNumberOfSteps() { 13 | int num = 14; 14 | assertEquals(6, testObject.numberOfSteps(num)); 15 | 16 | num = 8; 17 | assertEquals(4, testObject.numberOfSteps(num)); 18 | 19 | num = 123; 20 | assertEquals(12, testObject.numberOfSteps(num)); 21 | } 22 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/string/LongestValidParenthesesTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.string; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class LongestValidParenthesesTest { 8 | 9 | private final LongestValidParentheses testObject = new LongestValidParentheses(); 10 | 11 | @Test 12 | public void testLongestValidParentheses() { 13 | String s = "(()"; 14 | assertEquals(2, testObject.longestValidParentheses(s)); 15 | 16 | s = ")()())"; 17 | assertEquals(4, testObject.longestValidParentheses(s)); 18 | 19 | s = ""; 20 | assertEquals(0, testObject.longestValidParentheses(s)); 21 | } 22 | } -------------------------------------------------------------------------------- /LeetCode/src/main/java/org/redquark/onlinejudges/leetcode/bitmagic/ReverseBits.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.bitmagic; 2 | 3 | /** 4 | * @author Anirudh Sharma 5 | */ 6 | public class ReverseBits { 7 | 8 | public int reverseBits(int n) { 9 | // Special case 10 | if (n == 0) { 11 | return 0; 12 | } 13 | // Reverse bit 14 | int m = 0; 15 | // Loop for all the positions in a 32-bit integer 16 | for (int i = 31; i >= 0; i--) { 17 | // If the current bit in n is 1, set the corresponding 18 | // bit in m. 19 | if ((n & 1) != 0) { 20 | m |= (1 << i); 21 | } 22 | n >>= 1; 23 | } 24 | return m; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /LeetCode/src/main/java/org/redquark/onlinejudges/leetcode/tree/InsertIntoABinarySearchTree.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.tree; 2 | 3 | import org.redquark.onlinejudges.leetcode.common.TreeNode; 4 | 5 | /** 6 | * @author Anirudh Sharma 7 | */ 8 | public class InsertIntoABinarySearchTree { 9 | 10 | public TreeNode insertIntoBST(TreeNode root, int val) { 11 | // Special case 12 | if (root == null) { 13 | return new TreeNode(val); 14 | } 15 | // Find the correct place of the node to insert 16 | if (root.val > val) { 17 | root.left = insertIntoBST(root.left, val); 18 | } else { 19 | root.right = insertIntoBST(root.right, val); 20 | } 21 | return root; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/array/CountOfRangeSumTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class CountOfRangeSumTest { 8 | 9 | private final CountOfRangeSum testObject = new CountOfRangeSum(); 10 | 11 | @Test 12 | public void testCountRangeSum() { 13 | int[] nums = new int[]{-2, 5, -1}; 14 | int lower = -2; 15 | int upper = 2; 16 | assertEquals(3, testObject.countRangeSum(nums, lower, upper)); 17 | 18 | nums = new int[]{0}; 19 | lower = 0; 20 | upper = 0; 21 | assertEquals(1, testObject.countRangeSum(nums, lower, upper)); 22 | } 23 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/design/ImplementTrieTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.design; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertFalse; 6 | import static org.junit.jupiter.api.Assertions.assertTrue; 7 | import static org.redquark.onlinejudges.leetcode.design.ImplementTrie.Trie; 8 | 9 | 10 | public class ImplementTrieTest { 11 | 12 | @Test 13 | public void test() { 14 | Trie trie = new Trie(); 15 | trie.insert("apple"); 16 | assertTrue(trie.search("apple")); 17 | assertFalse(trie.search("app")); 18 | assertTrue(trie.startsWith("app")); 19 | trie.insert("app"); 20 | assertTrue(trie.search("app")); 21 | } 22 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/stack/BaseballGameTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.stack; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class BaseballGameTest { 8 | 9 | private final BaseballGame testObject = new BaseballGame(); 10 | 11 | @Test 12 | public void testCalPoints() { 13 | String[] ops = new String[]{"5", "2", "C", "D", "+"}; 14 | assertEquals(30, testObject.calPoints(ops)); 15 | 16 | ops = new String[]{"5", "-2", "4", "C", "D", "9", "+", "+"}; 17 | assertEquals(27, testObject.calPoints(ops)); 18 | 19 | ops = new String[]{"1"}; 20 | assertEquals(1, testObject.calPoints(ops)); 21 | } 22 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/array/SortArrayByParityTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 6 | 7 | public class SortArrayByParityTest { 8 | 9 | private final SortArrayByParity testObject = new SortArrayByParity(); 10 | 11 | @Test 12 | public void testSortArrayByParity() { 13 | int[] nums = new int[]{3, 1, 2, 4}; 14 | int[] expected = new int[]{2, 4, 3, 1}; 15 | assertArrayEquals(expected, testObject.sortArrayByParity(nums)); 16 | 17 | nums = new int[]{0}; 18 | expected = new int[]{0}; 19 | assertArrayEquals(expected, testObject.sortArrayByParity(nums)); 20 | } 21 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/array/ThreeSumWithMultiplicityTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class ThreeSumWithMultiplicityTest { 8 | 9 | private final ThreeSumWithMultiplicity testObject = new ThreeSumWithMultiplicity(); 10 | 11 | @Test 12 | public void testThreeSumMulti() { 13 | int[] arr = new int[]{1, 1, 2, 2, 3, 3, 4, 4, 5, 5}; 14 | int target = 8; 15 | assertEquals(20, testObject.threeSumMulti(arr, target)); 16 | 17 | arr = new int[]{1, 1, 2, 2, 2, 2}; 18 | target = 5; 19 | assertEquals(12, testObject.threeSumMulti(arr, target)); 20 | } 21 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/dp/RestoreTheArrayTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class RestoreTheArrayTest { 8 | 9 | private final RestoreTheArray testObject = new RestoreTheArray(); 10 | 11 | @Test 12 | public void testNumberOfArrays() { 13 | String s = "1000"; 14 | int k = 10000; 15 | assertEquals(1, testObject.numberOfArrays(s, k)); 16 | 17 | s = "1000"; 18 | k = 10; 19 | assertEquals(0, testObject.numberOfArrays(s, k)); 20 | 21 | s = "1317"; 22 | k = 2000; 23 | assertEquals(8, testObject.numberOfArrays(s, k)); 24 | } 25 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/heap/KthLargestElementInAStreamTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.heap; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | import static org.redquark.onlinejudges.leetcode.heap.KthLargestElementInAStream.KthLargest; 7 | 8 | public class KthLargestElementInAStreamTest { 9 | 10 | @Test 11 | public void test() { 12 | KthLargest kthLargest = new KthLargest(3, new int[]{4, 5, 8, 2}); 13 | assertEquals(4, kthLargest.add(3)); 14 | assertEquals(5, kthLargest.add(5)); 15 | assertEquals(5, kthLargest.add(10)); 16 | assertEquals(8, kthLargest.add(9)); 17 | assertEquals(8, kthLargest.add(4)); 18 | } 19 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/math/ExcelSheetColumnNumberTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.math; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class ExcelSheetColumnNumberTest { 8 | 9 | private final ExcelSheetColumnNumber testObject = new ExcelSheetColumnNumber(); 10 | 11 | @Test 12 | public void testTitleToNumber() { 13 | String columnTitle = "A"; 14 | assertEquals(1, testObject.titleToNumber(columnTitle)); 15 | 16 | columnTitle = "AB"; 17 | assertEquals(28, testObject.titleToNumber(columnTitle)); 18 | 19 | columnTitle = "ZY"; 20 | assertEquals(701, testObject.titleToNumber(columnTitle)); 21 | } 22 | } -------------------------------------------------------------------------------- /LeetCode/src/main/java/org/redquark/onlinejudges/leetcode/array/ConcatenationOfArray.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | /** 4 | * @author Anirudh Sharma 5 | */ 6 | public class ConcatenationOfArray { 7 | 8 | public int[] getConcatenation(int[] nums) { 9 | // Special case 10 | if (nums == null || nums.length == 0) { 11 | return nums; 12 | } 13 | // Length of the array 14 | int n = nums.length; 15 | // Concatenated array 16 | int[] concatenated = new int[2 * n]; 17 | // Populate the array 18 | for (int i = 0; i < n; i++) { 19 | concatenated[i] = nums[i]; 20 | concatenated[i + n] = nums[i]; 21 | } 22 | return concatenated; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode/src/main/java/org/redquark/onlinejudges/leetcode/array/MinimizeMaximumOfArray.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | public class MinimizeMaximumOfArray { 4 | 5 | public int minimizeArrayValue(int[] nums) { 6 | // Special case 7 | if (nums == null || nums.length == 0) { 8 | return 0; 9 | } 10 | // Variable to store the minimum maximum element 11 | long minMax = 0; 12 | // Variable to store the prefix sum 13 | long prefixSum = 0; 14 | // Loop through the array 15 | for (int i = 0; i < nums.length; i++) { 16 | prefixSum += nums[i]; 17 | minMax = Math.max(minMax, (prefixSum + i) / (i + 1)); 18 | } 19 | return (int) minMax; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/array/CarPoolingTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertFalse; 6 | import static org.junit.jupiter.api.Assertions.assertTrue; 7 | 8 | public class CarPoolingTest { 9 | 10 | private final CarPooling testObject = new CarPooling(); 11 | 12 | @Test 13 | public void testCarPooling() { 14 | int[][] trips = new int[][]{ 15 | {2, 1, 5}, 16 | {3, 3, 7} 17 | }; 18 | int capacity = 4; 19 | assertFalse(testObject.carPooling(trips, capacity)); 20 | 21 | capacity = 5; 22 | assertTrue(testObject.carPooling(trips, capacity)); 23 | } 24 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/array/MaxConsecutiveOnesIIITest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class MaxConsecutiveOnesIIITest { 8 | 9 | private final MaxConsecutiveOnesIII testObject = new MaxConsecutiveOnesIII(); 10 | 11 | @Test 12 | public void testLongestOnes() { 13 | int[] nums = new int[]{1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0}; 14 | int k = 2; 15 | assertEquals(6, testObject.longestOnes(nums, k)); 16 | 17 | nums = new int[]{0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1}; 18 | k = 3; 19 | assertEquals(10, testObject.longestOnes(nums, k)); 20 | } 21 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/string/RemovePalindromicSubsequencesTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.string; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class RemovePalindromicSubsequencesTest { 8 | 9 | private final RemovePalindromicSubsequences testObject = new RemovePalindromicSubsequences(); 10 | 11 | @Test 12 | public void testRemovePalindromeSub() { 13 | String s = "ababa"; 14 | assertEquals(1, testObject.removePalindromeSub(s)); 15 | 16 | s = "abb"; 17 | assertEquals(2, testObject.removePalindromeSub(s)); 18 | 19 | s = "baabb"; 20 | assertEquals(2, testObject.removePalindromeSub(s)); 21 | } 22 | } -------------------------------------------------------------------------------- /WorkAtTech/src/test/java/org/redquark/onlinejudges/workattech/greedy/TasksForProfitTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.workattech.greedy; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | import static org.redquark.onlinejudges.workattech.greedy.TasksForProfit.Task; 7 | 8 | public class TasksForProfitTest { 9 | 10 | private final TasksForProfit testObject = new TasksForProfit(); 11 | 12 | @Test 13 | public void testGetMaxProfit() { 14 | Task[] tasks = new Task[]{ 15 | new Task(4, 20), 16 | new Task(2, 10), 17 | new Task(2, 40), 18 | new Task(1, 30) 19 | }; 20 | assertEquals(90, testObject.getMaxProfit(tasks)); 21 | } 22 | } -------------------------------------------------------------------------------- /LeetCode/src/main/java/org/redquark/onlinejudges/leetcode/array/MinimumValueToGetPositiveStepByStepSum.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | /** 4 | * @author Anirudh Sharma 5 | */ 6 | public class MinimumValueToGetPositiveStepByStepSum { 7 | 8 | public int minStartValue(int[] nums) { 9 | // Special case 10 | if (nums == null || nums.length == 0) { 11 | return 0; 12 | } 13 | // Minimum prefix sum 14 | int minPrefixSum = 0; 15 | // Cumulative sum 16 | int sum = 0; 17 | // Loop through all the elements 18 | for (int num : nums) { 19 | sum += num; 20 | minPrefixSum = Math.min(minPrefixSum, sum); 21 | } 22 | return 1 - minPrefixSum; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/stack/RemoveKDigitsTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.stack; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class RemoveKDigitsTest { 8 | 9 | private final RemoveKDigits testObject = new RemoveKDigits(); 10 | 11 | @Test 12 | public void testRemoveKdigits() { 13 | String num = "1432219"; 14 | int k = 3; 15 | assertEquals("1219", testObject.removeKdigits(num, k)); 16 | 17 | num = "10200"; 18 | k = 1; 19 | assertEquals("200", testObject.removeKdigits(num, k)); 20 | 21 | num = "10"; 22 | k = 2; 23 | assertEquals("0", testObject.removeKdigits(num, k)); 24 | } 25 | } -------------------------------------------------------------------------------- /WorkAtTech/src/test/java/org/redquark/onlinejudges/workattech/greedy/SackOfGrainsTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.workattech.greedy; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | import static org.redquark.onlinejudges.workattech.greedy.SackOfGrains.Grain; 7 | 8 | public class SackOfGrainsTest { 9 | 10 | private final SackOfGrains testObject = new SackOfGrains(); 11 | 12 | @Test 13 | public void testMaxSackValue() { 14 | Grain[] grains = new Grain[]{ 15 | new Grain(5, 20), 16 | new Grain(8, 20), 17 | new Grain(4, 15), 18 | new Grain(5, 8) 19 | }; 20 | assertEquals(42.5, testObject.maxSackValue(grains, 12)); 21 | } 22 | } -------------------------------------------------------------------------------- /LeetCode/src/main/java/org/redquark/onlinejudges/leetcode/dp/ClimbingStairs.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.dp; 2 | 3 | /** 4 | * @author Anirudh Sharma 5 | */ 6 | public class ClimbingStairs { 7 | 8 | public int climbStairs(int n) { 9 | // Special case 10 | if (n <= 0) { 11 | return 0; 12 | } 13 | // Lookup table to store minimum steps to reach 14 | // a certain stair i.e., lookup[i] => minimum steps 15 | // to reach i-th stair 16 | int[] lookup = new int[n + 1]; 17 | // Base initialization 18 | lookup[0] = 1; 19 | lookup[1] = 1; 20 | for (int i = 2; i <= n; i++) { 21 | lookup[i] += lookup[i - 1] + lookup[i - 2]; 22 | } 23 | return lookup[n]; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /LeetCode/src/main/java/org/redquark/onlinejudges/leetcode/math/SignOfTheProductOfAnArray.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.math; 2 | 3 | public class SignOfTheProductOfAnArray { 4 | 5 | public int arraySign(int[] nums) { 6 | // Special case 7 | if (nums == null || nums.length == 0) { 8 | throw new IllegalArgumentException("Array cannot be null/empty!"); 9 | } 10 | // Count of negative numbers in the array 11 | int count = 0; 12 | // Traverse through the array 13 | for (int num : nums) { 14 | if (num < 0) { 15 | count++; 16 | } 17 | if (num == 0) { 18 | return 0; 19 | } 20 | } 21 | return count % 2 == 0 ? 1 : -1; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/array/AverageSalaryExcludingTheMinimumAndMaximumSalaryTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class AverageSalaryExcludingTheMinimumAndMaximumSalaryTest { 8 | 9 | private final AverageSalaryExcludingTheMinimumAndMaximumSalary testObject = new AverageSalaryExcludingTheMinimumAndMaximumSalary(); 10 | 11 | @Test 12 | public void testAverage() { 13 | int[] salary = new int[]{4000, 3000, 1000, 2000}; 14 | assertEquals(2500.0, testObject.average(salary)); 15 | 16 | salary = new int[]{1000, 2000, 3000}; 17 | assertEquals(2000.0, testObject.average(salary)); 18 | } 19 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/backtracking/FairDistributionOfCookiesTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.backtracking; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class FairDistributionOfCookiesTest { 8 | 9 | private final FairDistributionOfCookies testObject = new FairDistributionOfCookies(); 10 | 11 | @Test 12 | public void testDistributeCookies() { 13 | int[] cookies = new int[]{8, 15, 10, 20, 8}; 14 | int k = 2; 15 | assertEquals(31, testObject.distributeCookies(cookies, k)); 16 | 17 | cookies = new int[]{6, 1, 3, 2, 2, 4, 1, 2}; 18 | k = 3; 19 | assertEquals(7, testObject.distributeCookies(cookies, k)); 20 | } 21 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/dp/LastStoneWeightIITest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class LastStoneWeightIITest { 8 | 9 | private final LastStoneWeightII testObject = new LastStoneWeightII(); 10 | 11 | @Test 12 | public void testLastStoneWeightII() { 13 | int[] stones = new int[]{2, 7, 4, 1, 8, 1}; 14 | assertEquals(1, testObject.lastStoneWeightII(stones)); 15 | 16 | stones = new int[]{31, 26, 33, 21, 40}; 17 | assertEquals(5, testObject.lastStoneWeightII(stones)); 18 | 19 | stones = new int[]{1, 2}; 20 | assertEquals(1, testObject.lastStoneWeightII(stones)); 21 | } 22 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/string/ReverseOnlyLettersTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.string; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class ReverseOnlyLettersTest { 8 | 9 | private final ReverseOnlyLetters testObject = new ReverseOnlyLetters(); 10 | 11 | @Test 12 | public void testReverseOnlyLetters() { 13 | String s = "ab-cd"; 14 | assertEquals("dc-ba", testObject.reverseOnlyLetters(s)); 15 | 16 | s = "a-bC-dEf-ghIj"; 17 | assertEquals("j-Ih-gfE-dCba", testObject.reverseOnlyLetters(s)); 18 | 19 | s = "Test1ng-Leet=code-Q!"; 20 | assertEquals("Qedo1ct-eeLg=ntse-T!", testObject.reverseOnlyLetters(s)); 21 | } 22 | } -------------------------------------------------------------------------------- /LeetCode/src/main/java/org/redquark/onlinejudges/leetcode/tree/SumOfLeftLeaves.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.tree; 2 | 3 | import org.redquark.onlinejudges.leetcode.common.TreeNode; 4 | 5 | /** 6 | * @author Anirudh Sharma 7 | */ 8 | public class SumOfLeftLeaves { 9 | 10 | public int sumOfLeftLeaves(TreeNode root) { 11 | // Base case 12 | if (root == null) { 13 | return 0; 14 | } 15 | // Check if the current node is the left leave node 16 | else if (root.left != null && root.left.left == null && root.left.right == null) { 17 | return root.left.val + sumOfLeftLeaves(root.right); 18 | } 19 | // Exploring the tree further 20 | return sumOfLeftLeaves(root.left) + sumOfLeftLeaves(root.right); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/array/MedianOfTwoSortedArraysTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class MedianOfTwoSortedArraysTest { 8 | 9 | private final MedianOfTwoSortedArrays testObject = new MedianOfTwoSortedArrays(); 10 | 11 | @Test 12 | public void testFindMedianSortedArrays() { 13 | int[] nums1 = new int[]{1, 3}; 14 | int[] nums2 = new int[]{2}; 15 | assertEquals(2.0, testObject.findMedianSortedArrays(nums1, nums2)); 16 | 17 | nums1 = new int[]{1, 2}; 18 | nums2 = new int[]{3, 4}; 19 | assertEquals(2.5, testObject.findMedianSortedArrays(nums1, nums2)); 20 | 21 | } 22 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/dp/CoinChangeIITest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class CoinChangeIITest { 8 | 9 | private final CoinChangeII testObject = new CoinChangeII(); 10 | 11 | @Test 12 | public void testChange() { 13 | int[] coins = new int[]{1, 2, 5}; 14 | int amount = 5; 15 | assertEquals(4, testObject.change(amount, coins)); 16 | 17 | coins = new int[]{2}; 18 | amount = 3; 19 | assertEquals(0, testObject.change(amount, coins)); 20 | 21 | coins = new int[]{10}; 22 | amount = 10; 23 | assertEquals(1, testObject.change(amount, coins)); 24 | } 25 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/dp/MinimumPathSumTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class MinimumPathSumTest { 8 | 9 | private final MinimumPathSum testObject = new MinimumPathSum(); 10 | 11 | @Test 12 | public void testMinPathSum() { 13 | int[][] grid = new int[][]{ 14 | {1, 3, 1}, 15 | {1, 5, 1}, 16 | {4, 2, 1} 17 | }; 18 | assertEquals(7, testObject.minPathSum(grid)); 19 | 20 | grid = new int[][]{ 21 | {1, 2, 3}, 22 | {4, 5, 6} 23 | }; 24 | assertEquals(12, testObject.minPathSum(grid)); 25 | } 26 | } -------------------------------------------------------------------------------- /LeetCode/src/main/java/org/redquark/onlinejudges/leetcode/tree/ValidateBinarySearchTree.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.tree; 2 | 3 | import org.redquark.onlinejudges.leetcode.common.TreeNode; 4 | 5 | /** 6 | * @author Anirudh Sharma 7 | */ 8 | public class ValidateBinarySearchTree { 9 | 10 | public boolean isValidBST(TreeNode root) { 11 | // Base case 12 | if (root == null) { 13 | return true; 14 | } 15 | // Check if BST property is violated 16 | if ((root.left != null && root.left.val > root.val) || (root.right != null && root.right.val < root.val)) { 17 | return false; 18 | } 19 | // Recursively check for left and right subtrees 20 | return isValidBST(root.left) && isValidBST(root.right); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/dp/BestTimeToBuyAndSellStockIITest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class BestTimeToBuyAndSellStockIITest { 8 | 9 | private final BestTimeToBuyAndSellStockII testObject = new BestTimeToBuyAndSellStockII(); 10 | 11 | @Test 12 | public void maxProfit() { 13 | int[] prices = new int[]{7, 1, 5, 3, 6, 4}; 14 | assertEquals(7, testObject.maxProfit(prices)); 15 | 16 | prices = new int[]{1, 2, 3, 4, 5}; 17 | assertEquals(4, testObject.maxProfit(prices)); 18 | 19 | prices = new int[]{7, 6, 4, 3, 1}; 20 | assertEquals(0, testObject.maxProfit(prices)); 21 | } 22 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/dp/MinimumCostForTicketsTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class MinimumCostForTicketsTest { 8 | 9 | private final MinimumCostForTickets testObject = new MinimumCostForTickets(); 10 | 11 | @Test 12 | public void testMinCostTickets() { 13 | int[] days = new int[]{1, 4, 6, 7, 8, 20}; 14 | int[] costs = new int[]{2, 7, 15}; 15 | assertEquals(11, testObject.minCostTickets(days, costs)); 16 | 17 | days = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 30, 31}; 18 | costs = new int[]{2, 7, 15}; 19 | assertEquals(17, testObject.minCostTickets(days, costs)); 20 | } 21 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/math/SignOfTheProductOfAnArrayTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.math; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class SignOfTheProductOfAnArrayTest { 8 | 9 | private final SignOfTheProductOfAnArray testObject = new SignOfTheProductOfAnArray(); 10 | 11 | @Test 12 | public void testArraySign() { 13 | int[] nums = new int[]{-1, -2, -3, -4, 3, 2, 1}; 14 | assertEquals(1, testObject.arraySign(nums)); 15 | 16 | nums = new int[]{1, 5, 0, 2, -3}; 17 | assertEquals(0, testObject.arraySign(nums)); 18 | 19 | nums = new int[]{-1, 1, -1, 1, -1}; 20 | assertEquals(-1, testObject.arraySign(nums)); 21 | } 22 | } -------------------------------------------------------------------------------- /WorkAtTech/src/test/java/org/redquark/onlinejudges/workattech/arrays/IdenticalTwinsTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.workattech.arrays; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class IdenticalTwinsTest { 8 | 9 | private final IdenticalTwins testObject = new IdenticalTwins(); 10 | 11 | @Test 12 | public void testGetIdenticalTwinsCount() { 13 | int[] nums = new int[]{1, 2, 3, 2, 1}; 14 | assertEquals(2, testObject.getIdenticalTwinsCount(nums)); 15 | 16 | nums = new int[]{1, 2, 2, 3, 2, 1}; 17 | assertEquals(4, testObject.getIdenticalTwinsCount(nums)); 18 | 19 | nums = new int[]{1, 1, 1, 1}; 20 | assertEquals(6, testObject.getIdenticalTwinsCount(nums)); 21 | } 22 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/array/FirstMissingPositiveTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class FirstMissingPositiveTest { 8 | 9 | private final FirstMissingPositive testObject = new FirstMissingPositive(); 10 | 11 | @Test 12 | public void testFirstMissingPositive() { 13 | int[] nums = new int[]{1, 2, 0}; 14 | assertEquals(3, testObject.firstMissingPositive(nums)); 15 | 16 | nums = new int[]{3, 4, -1, 1}; 17 | assertEquals(2, testObject.firstMissingPositive(nums)); 18 | 19 | nums = new int[]{7, 8, 9, 11, 12}; 20 | assertEquals(1, testObject.firstMissingPositive(nums)); 21 | } 22 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/array/SpiralMatrixIITest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 6 | 7 | public class SpiralMatrixIITest { 8 | 9 | private final SpiralMatrixII testObject = new SpiralMatrixII(); 10 | 11 | @Test 12 | public void testGenerateMatrix() { 13 | int[][] expected = new int[][]{ 14 | {1, 2, 3}, 15 | {8, 9, 4}, 16 | {7, 6, 5} 17 | }; 18 | assertArrayEquals(expected, testObject.generateMatrix(3)); 19 | 20 | expected = new int[][]{ 21 | {1} 22 | }; 23 | assertArrayEquals(expected, testObject.generateMatrix(1)); 24 | } 25 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/design/DesignHashMapTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.design; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | import static org.redquark.onlinejudges.leetcode.design.DesignHashMap.MyHashMap; 7 | 8 | public class DesignHashMapTest { 9 | 10 | @Test 11 | public void test() { 12 | MyHashMap testObject = new MyHashMap(); 13 | testObject.put(1, 1); 14 | testObject.put(2, 2); 15 | assertEquals(1, testObject.get(1)); 16 | assertEquals(-1, testObject.get(3)); 17 | testObject.put(2, 1); 18 | assertEquals(1, testObject.get(2)); 19 | testObject.remove(2); 20 | assertEquals(-1, testObject.get(2)); 21 | } 22 | 23 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/design/TimeBasedKeyValueStoreTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.design; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | import static org.redquark.onlinejudges.leetcode.design.TimeBasedKeyValueStore.TimeMap; 7 | 8 | public class TimeBasedKeyValueStoreTest { 9 | 10 | @Test 11 | public void test() { 12 | TimeMap timeMap = new TimeMap(); 13 | timeMap.set("foo", "bar", 1); 14 | assertEquals("bar", timeMap.get("foo", 1)); 15 | assertEquals("bar", timeMap.get("foo", 3)); 16 | timeMap.set("foo", "bar2", 4); 17 | assertEquals("bar2", timeMap.get("foo", 4)); 18 | assertEquals("bar2", timeMap.get("foo", 5)); 19 | } 20 | 21 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/dp/BestTimeToBuyAndSellStockWithTransactionFeeTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class BestTimeToBuyAndSellStockWithTransactionFeeTest { 8 | 9 | private final BestTimeToBuyAndSellStockWithTransactionFee testObject = new BestTimeToBuyAndSellStockWithTransactionFee(); 10 | 11 | @Test 12 | void testMaxProfit() { 13 | int[] prices = new int[]{1, 3, 2, 8, 4, 9}; 14 | int fee = 2; 15 | assertEquals(8, testObject.maxProfit(prices, fee)); 16 | 17 | prices = new int[]{1, 3, 7, 5, 10, 3}; 18 | fee = 3; 19 | assertEquals(6, testObject.maxProfit(prices, fee)); 20 | } 21 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/dp/MinimumInsertionStepsToMakeAStringPalindromeTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class MinimumInsertionStepsToMakeAStringPalindromeTest { 8 | 9 | private final MinimumInsertionStepsToMakeAStringPalindrome testObject = new MinimumInsertionStepsToMakeAStringPalindrome(); 10 | 11 | @Test 12 | public void testMinInsertions() { 13 | String s = "zzazz"; 14 | assertEquals(0, testObject.minInsertions(s)); 15 | 16 | s = "mbadm"; 17 | assertEquals(2, testObject.minInsertions(s)); 18 | 19 | s = "leetcode"; 20 | assertEquals(5, testObject.minInsertions(s)); 21 | } 22 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/heap/MinimizeDeviationInArrayTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.heap; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class MinimizeDeviationInArrayTest { 8 | 9 | private final MinimizeDeviationInArray testObject = new MinimizeDeviationInArray(); 10 | 11 | @Test 12 | public void testMinimumDeviation() { 13 | int[] nums = new int[]{1, 2, 3, 4}; 14 | assertEquals(1, testObject.minimumDeviation(nums)); 15 | 16 | nums = new int[]{4, 1, 5, 20, 3}; 17 | assertEquals(3, testObject.minimumDeviation(nums)); 18 | 19 | nums = new int[]{2, 10, 8}; 20 | assertEquals(3, testObject.minimumDeviation(nums)); 21 | } 22 | 23 | } -------------------------------------------------------------------------------- /WorkAtTech/src/test/java/org/redquark/onlinejudges/workattech/dp/LongestPalindromicSubstringTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.workattech.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class LongestPalindromicSubstringTest { 8 | 9 | private final LongestPalindromicSubstring testObject = new LongestPalindromicSubstring(); 10 | 11 | @Test 12 | public void testLps() { 13 | String s = "abcdcab"; 14 | assertEquals("cdc", testObject.lps(s)); 15 | 16 | s = "abcdcba"; 17 | assertEquals("abcdcba", testObject.lps(s)); 18 | 19 | s = "abcd"; 20 | assertEquals("a", testObject.lps(s)); 21 | 22 | s = "abaadcd"; 23 | assertEquals("aba", testObject.lps(s)); 24 | } 25 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/array/RotateArrayTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 6 | 7 | public class RotateArrayTest { 8 | 9 | private final RotateArray testObject = new RotateArray(); 10 | 11 | @Test 12 | public void testRotate() { 13 | int[] nums = new int[]{1, 2, 3, 4, 5, 6, 7}; 14 | int k = 3; 15 | int[] expected = new int[]{5, 6, 7, 1, 2, 3, 4}; 16 | assertArrayEquals(expected, testObject.rotate(nums, k)); 17 | 18 | nums = new int[]{-1, -100, 3, 99}; 19 | k = 2; 20 | expected = new int[]{3, 99, -1, -100}; 21 | assertArrayEquals(expected, testObject.rotate(nums, k)); 22 | } 23 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/backtracking/GenerateParenthesesTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.backtracking; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import java.util.Arrays; 6 | import java.util.List; 7 | 8 | import static org.junit.jupiter.api.Assertions.assertEquals; 9 | 10 | public class GenerateParenthesesTest { 11 | 12 | private final GenerateParentheses testObject = new GenerateParentheses(); 13 | 14 | @Test 15 | public void testGenerateParenthesis() { 16 | List expected = Arrays.asList("((()))", "(()())", "(())()", "()(())", "()()()"); 17 | assertEquals(expected, testObject.generateParenthesis(3)); 18 | 19 | expected = List.of("()"); 20 | assertEquals(expected, testObject.generateParenthesis(1)); 21 | } 22 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/dp/SplitArrayLargestSumTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class SplitArrayLargestSumTest { 8 | 9 | private final SplitArrayLargestSum testObject = new SplitArrayLargestSum(); 10 | 11 | @Test 12 | public void testSplitArray() { 13 | int[] nums = new int[]{7, 2, 5, 10, 8}; 14 | int m = 2; 15 | assertEquals(18, testObject.splitArray(nums, m)); 16 | 17 | nums = new int[]{1, 2, 3, 4, 5}; 18 | assertEquals(9, testObject.splitArray(nums, m)); 19 | 20 | nums = new int[]{1, 4, 4}; 21 | m = 3; 22 | assertEquals(4, testObject.splitArray(nums, m)); 23 | } 24 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/stack/NextGreaterElementIITest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.stack; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 6 | 7 | public class NextGreaterElementIITest { 8 | 9 | private final NextGreaterElementII testObject = new NextGreaterElementII(); 10 | 11 | @Test 12 | public void testNextGreaterElements() { 13 | int[] nums = new int[]{1, 2, 1}; 14 | int[] expected = new int[]{2, -1, 2}; 15 | assertArrayEquals(expected, testObject.nextGreaterElements(nums)); 16 | 17 | nums = new int[]{1, 2, 3, 4, 3}; 18 | expected = new int[]{2, 3, 4, -1, 4}; 19 | assertArrayEquals(expected, testObject.nextGreaterElements(nums)); 20 | } 21 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/array/ConcatenationOfArrayTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 6 | 7 | public class ConcatenationOfArrayTest { 8 | 9 | private final ConcatenationOfArray testObject = new ConcatenationOfArray(); 10 | 11 | @Test 12 | public void testGetConcatenation() { 13 | int[] nums = new int[]{1, 2, 1}; 14 | int[] expected = new int[]{1, 2, 1, 1, 2, 1}; 15 | assertArrayEquals(expected, testObject.getConcatenation(nums)); 16 | 17 | nums = new int[]{1, 3, 2, 1}; 18 | expected = new int[]{1, 3, 2, 1, 1, 3, 2, 1}; 19 | assertArrayEquals(expected, testObject.getConcatenation(nums)); 20 | } 21 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/array/SearchInRotatedSortedArrayIITest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertFalse; 6 | import static org.junit.jupiter.api.Assertions.assertTrue; 7 | 8 | public class SearchInRotatedSortedArrayIITest { 9 | 10 | private final SearchInRotatedSortedArrayII testObject = new SearchInRotatedSortedArrayII(); 11 | 12 | @Test 13 | public void testSearch() { 14 | int[] nums = new int[]{2, 5, 6, 0, 0, 1, 2}; 15 | int target = 0; 16 | assertTrue(testObject.search(nums, target)); 17 | 18 | nums = new int[]{2, 5, 6, 0, 0, 1, 2}; 19 | target = 3; 20 | assertFalse(testObject.search(nums, target)); 21 | } 22 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/backtracking/GeneralizedAbbreviationTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.backtracking; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import java.util.Arrays; 6 | import java.util.List; 7 | 8 | import static org.junit.jupiter.api.Assertions.assertEquals; 9 | 10 | public class GeneralizedAbbreviationTest { 11 | 12 | private final GeneralizedAbbreviation testObject = new GeneralizedAbbreviation(); 13 | 14 | @Test 15 | public void testGenerateAbbreviations() { 16 | String word = "word"; 17 | List expected = Arrays.asList("4", "3d", "2r1", "2rd", "1o2", "1o1d", "1or1", "1ord", "w3", "w2d", "w1r1", "w1rd", "wo2", "wo1d", "wor1", "word"); 18 | assertEquals(expected, testObject.generateAbbreviations(word)); 19 | } 20 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/dp/LongestIncreasingSubsequenceTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class LongestIncreasingSubsequenceTest { 8 | 9 | private final LongestIncreasingSubsequence testObject = new LongestIncreasingSubsequence(); 10 | 11 | @Test 12 | public void lengthOfLIS() { 13 | int[] nums = new int[]{10, 9, 2, 5, 3, 7, 101, 18}; 14 | assertEquals(4, testObject.lengthOfLIS(nums)); 15 | 16 | nums = new int[]{0, 1, 0, 3, 2, 3}; 17 | assertEquals(4, testObject.lengthOfLIS(nums)); 18 | 19 | nums = new int[]{7, 7, 7, 7, 7, 7, 7}; 20 | assertEquals(1, testObject.lengthOfLIS(nums)); 21 | } 22 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/heap/TopKFrequentElementsTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.heap; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 6 | 7 | public class TopKFrequentElementsTest { 8 | 9 | private final TopKFrequentElements testObject = new TopKFrequentElements(); 10 | 11 | @Test 12 | public void testTopKFrequent() { 13 | int[] nums = new int[]{1, 1, 1, 2, 2, 3}; 14 | int k = 2; 15 | int[] expected = new int[]{1, 2}; 16 | assertArrayEquals(expected, testObject.topKFrequent(nums, k)); 17 | 18 | nums = new int[]{1}; 19 | k = 1; 20 | expected = new int[]{1}; 21 | assertArrayEquals(expected, testObject.topKFrequent(nums, k)); 22 | } 23 | } -------------------------------------------------------------------------------- /WorkAtTech/src/test/java/org/redquark/onlinejudges/workattech/greedy/TrainsAndPlatformsTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.workattech.greedy; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | import static org.redquark.onlinejudges.workattech.greedy.TrainsAndPlatforms.Train; 7 | 8 | public class TrainsAndPlatformsTest { 9 | 10 | private final TrainsAndPlatforms testObject = new TrainsAndPlatforms(); 11 | 12 | @Test 13 | public void testMinPlatforms() { 14 | Train[] trains = new Train[]{ 15 | new Train(120, 130), 16 | new Train(130, 150), 17 | new Train(125, 145), 18 | new Train(150, 180) 19 | }; 20 | assertEquals(3, testObject.minPlatforms(trains)); 21 | } 22 | } -------------------------------------------------------------------------------- /LeetCode/src/main/java/org/redquark/onlinejudges/leetcode/array/ContainsDuplicate.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | import java.util.HashSet; 4 | import java.util.Set; 5 | 6 | /** 7 | * @author Anirudh Sharma 8 | */ 9 | public class ContainsDuplicate { 10 | 11 | public boolean containsDuplicate(int[] nums) { 12 | // Base case 13 | if (nums == null || nums.length == 0) { 14 | return false; 15 | } 16 | // Set to store elements in the array 17 | final Set elements = new HashSet<>(); 18 | // Loop through the array 19 | for (int num : nums) { 20 | // Add the element 21 | if (!elements.add(num)) { 22 | return true; 23 | } 24 | } 25 | return false; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /LeetCode/src/main/java/org/redquark/onlinejudges/leetcode/math/CountSortedVowelStrings.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.math; 2 | 3 | /** 4 | * @author Anirudh Sharma 5 | */ 6 | public class CountSortedVowelStrings { 7 | 8 | public int countVowelStrings(int n) { 9 | // Special case 10 | if (n <= 0) { 11 | return 0; 12 | } 13 | // Counts of words that start with each vowel 14 | int a = 1; 15 | int e = 1; 16 | int i = 1; 17 | int o = 1; 18 | int u = 1; 19 | // Loop for n characters 20 | for (int count = 1; count < n; count++) { 21 | a += e + i + o + u; 22 | e += i + o + u; 23 | i += o + u; 24 | o += u; 25 | } 26 | return a + e + i + o + u; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/array/SquaresOfASortedArrayTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 6 | 7 | public class SquaresOfASortedArrayTest { 8 | 9 | private final SquaresOfASortedArray testObject = new SquaresOfASortedArray(); 10 | 11 | @Test 12 | public void testSortedSquares() { 13 | int[] nums = new int[]{-4, -1, 0, 3, 10}; 14 | int[] expected = new int[]{0, 1, 9, 16, 100}; 15 | assertArrayEquals(expected, testObject.sortedSquares(nums)); 16 | 17 | nums = new int[]{-7, -3, 2, 3, 11}; 18 | expected = new int[]{4, 9, 9, 49, 121}; 19 | assertArrayEquals(expected, testObject.sortedSquares(nums)); 20 | } 21 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/math/AddDigitsTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.math; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class AddDigitsTest { 8 | 9 | private final AddDigits testObject = new AddDigits(); 10 | 11 | @Test 12 | public void testAddDigits() { 13 | assertEquals(2, testObject.addDigitsSolutionOne(38)); 14 | assertEquals(2, testObject.addDigitsSolutionTwo(38)); 15 | assertEquals(0, testObject.addDigitsSolutionOne(0)); 16 | assertEquals(0, testObject.addDigitsSolutionTwo(0)); 17 | assertEquals(1, testObject.addDigitsSolutionOne(Integer.MAX_VALUE)); 18 | assertEquals(1, testObject.addDigitsSolutionTwo(Integer.MAX_VALUE)); 19 | } 20 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/math/BrokenCalculatorTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.math; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class BrokenCalculatorTest { 8 | 9 | private final BrokenCalculator testObject = new BrokenCalculator(); 10 | 11 | @Test 12 | public void testBrokenCalc() { 13 | int startValue = 2; 14 | int target = 3; 15 | assertEquals(2, testObject.brokenCalc(startValue, target)); 16 | 17 | startValue = 5; 18 | target = 8; 19 | assertEquals(2, testObject.brokenCalc(startValue, target)); 20 | 21 | startValue = 3; 22 | target = 10; 23 | assertEquals(3, testObject.brokenCalc(startValue, target)); 24 | } 25 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/stack/OneThreeTwoPatternTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.stack; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertFalse; 6 | import static org.junit.jupiter.api.Assertions.assertTrue; 7 | 8 | public class OneThreeTwoPatternTest { 9 | 10 | private final OneThreeTwoPattern testObject = new OneThreeTwoPattern(); 11 | 12 | @Test 13 | public void testFind132pattern() { 14 | int[] nums = new int[]{1, 2, 3, 4}; 15 | assertFalse(testObject.find132pattern(nums)); 16 | 17 | nums = new int[]{3, 1, 4, 2}; 18 | assertTrue(testObject.find132pattern(nums)); 19 | 20 | nums = new int[]{-1, 3, 2, 0}; 21 | assertTrue(testObject.find132pattern(nums)); 22 | } 23 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/string/ReverseStringTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.string; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 6 | 7 | public class ReverseStringTest { 8 | 9 | private final ReverseString testObject = new ReverseString(); 10 | 11 | @Test 12 | public void testReverseString() { 13 | char[] s = new char[]{'h', 'e', 'l', 'l', 'o'}; 14 | char[] expected = new char[]{'o', 'l', 'l', 'e', 'h'}; 15 | assertArrayEquals(expected, testObject.reverseString(s)); 16 | 17 | s = new char[]{'H', 'a', 'n', 'n', 'a', 'h'}; 18 | expected = new char[]{'h', 'a', 'n', 'n', 'a', 'H'}; 19 | assertArrayEquals(expected, testObject.reverseString(s)); 20 | } 21 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/array/BuildArrayFromPermutationTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 6 | 7 | public class BuildArrayFromPermutationTest { 8 | 9 | private final BuildArrayFromPermutation testObject = new BuildArrayFromPermutation(); 10 | 11 | @Test 12 | public void testBuildArray() { 13 | int[] nums = new int[]{5, 0, 1, 2, 3, 4}; 14 | int[] expected = new int[]{4, 5, 0, 1, 2, 3}; 15 | assertArrayEquals(expected, testObject.buildArray(nums)); 16 | 17 | nums = new int[]{0, 2, 1, 5, 3, 4}; 18 | expected = new int[]{0, 1, 2, 4, 5, 3}; 19 | assertArrayEquals(expected, testObject.buildArray(nums)); 20 | } 21 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/array/FindTheSmallestDivisorGivenAThresholdTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class FindTheSmallestDivisorGivenAThresholdTest { 8 | 9 | private final FindTheSmallestDivisorGivenAThreshold testObject = new FindTheSmallestDivisorGivenAThreshold(); 10 | 11 | @Test 12 | public void testSmallestDivisor() { 13 | int[] nums = new int[]{1, 2, 5, 9}; 14 | int threshold = 6; 15 | assertEquals(5, testObject.smallestDivisor(nums, threshold)); 16 | 17 | nums = new int[]{44, 22, 33, 11, 1}; 18 | threshold = 5; 19 | assertEquals(44, testObject.smallestDivisor(nums, threshold)); 20 | } 21 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/heap/SortEvenAndOddIndicesIndependentlyTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.heap; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 6 | 7 | public class SortEvenAndOddIndicesIndependentlyTest { 8 | 9 | private final SortEvenAndOddIndicesIndependently testObject = new SortEvenAndOddIndicesIndependently(); 10 | 11 | @Test 12 | public void testSortEvenOdd() { 13 | int[] nums = new int[]{4, 1, 2, 3}; 14 | int[] expected = new int[]{2, 3, 4, 1}; 15 | assertArrayEquals(expected, testObject.sortEvenOdd(nums)); 16 | 17 | nums = new int[]{2, 1}; 18 | expected = new int[]{2, 1}; 19 | assertArrayEquals(expected, testObject.sortEvenOdd(nums)); 20 | } 21 | } -------------------------------------------------------------------------------- /LeetCode/src/main/java/org/redquark/onlinejudges/leetcode/array/RichestCustomerWealth.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | /** 4 | * @author Anirudh Sharma 5 | */ 6 | public class RichestCustomerWealth { 7 | 8 | public int maximumWealth(int[][] accounts) { 9 | // Variable to store the maximum wealth 10 | int maxWealth = 0; 11 | // Loop for each customer 12 | for (int[] customerAccounts : accounts) { 13 | // Wealth of the current customer 14 | int currentWealth = 0; 15 | for (int account : customerAccounts) { 16 | currentWealth += account; 17 | } 18 | // Update the maxWealth, if needed 19 | maxWealth = Math.max(maxWealth, currentWealth); 20 | } 21 | return maxWealth; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /LeetCode/src/main/java/org/redquark/onlinejudges/leetcode/tree/MergeTwoBinaryTrees.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.tree; 2 | 3 | import org.redquark.onlinejudges.leetcode.common.TreeNode; 4 | 5 | /** 6 | * @author Anirudh Sharma 7 | */ 8 | public class MergeTwoBinaryTrees { 9 | 10 | public TreeNode mergeTrees(TreeNode root1, TreeNode root2) { 11 | // Special cases 12 | if (root1 == null) { 13 | return root2; 14 | } 15 | if (root2 == null) { 16 | return root1; 17 | } 18 | // Add the values in two nodes 19 | root1.val += root2.val; 20 | // Recur for left and right subtrees 21 | root1.left = mergeTrees(root1.left, root2.left); 22 | root1.right = mergeTrees(root1.right, root2.right); 23 | return root1; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/array/SearchInRotatedSortedArrayTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class SearchInRotatedSortedArrayTest { 8 | 9 | private final SearchInRotatedSortedArray testObject = new SearchInRotatedSortedArray(); 10 | 11 | @Test 12 | public void testSearch() { 13 | int[] nums = new int[]{4, 5, 6, 7, 0, 1, 2}; 14 | int target = 0; 15 | assertEquals(4, testObject.search(nums, target)); 16 | 17 | target = 3; 18 | assertEquals(-1, testObject.search(nums, target)); 19 | 20 | nums = new int[]{1}; 21 | target = 0; 22 | assertEquals(-1, testObject.search(nums, target)); 23 | } 24 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/string/RobotBoundedInCircleTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.string; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertFalse; 6 | import static org.junit.jupiter.api.Assertions.assertTrue; 7 | 8 | public class RobotBoundedInCircleTest { 9 | 10 | private final RobotBoundedInCircle testObject = new RobotBoundedInCircle(); 11 | 12 | @Test 13 | public void testIsRobotBounded() { 14 | String instructions = "GGLLGG"; 15 | assertTrue(testObject.isRobotBounded(instructions)); 16 | 17 | instructions = "GG"; 18 | assertFalse(testObject.isRobotBounded(instructions)); 19 | 20 | instructions = "GL"; 21 | assertTrue(testObject.isRobotBounded(instructions)); 22 | } 23 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/string/ZigzagConversionTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.string; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class ZigzagConversionTest { 8 | 9 | private final ZigzagConversion testObject = new ZigzagConversion(); 10 | 11 | @Test 12 | public void testConvert() { 13 | String s = "PAYPALISHIRING"; 14 | int numRows = 3; 15 | assertEquals("PAHNAPLSIIGYIR", testObject.convert(s, numRows)); 16 | 17 | s = "PAYPALISHIRING"; 18 | numRows = 4; 19 | assertEquals("PINALSIGYAHRPI", testObject.convert(s, numRows)); 20 | 21 | s = "A"; 22 | numRows = 1; 23 | assertEquals("A", testObject.convert(s, numRows)); 24 | } 25 | } -------------------------------------------------------------------------------- /WorkAtTech/src/test/java/org/redquark/onlinejudges/workattech/graph/ValidPathTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.workattech.graph; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.redquark.onlinejudges.workattech.graph.ValidPath.Circle; 5 | 6 | import static org.junit.jupiter.api.Assertions.assertTrue; 7 | 8 | public class ValidPathTest { 9 | 10 | private final ValidPath testObject = new ValidPath(); 11 | 12 | @Test 13 | public void testHasValidPath() { 14 | int m = 5; 15 | int n = 4; 16 | int r = 1; 17 | Circle[] circles = new Circle[]{ 18 | new Circle(0, 2), 19 | new Circle(2, 3), 20 | new Circle(3, 0), 21 | new Circle(4, 1) 22 | }; 23 | assertTrue(testObject.hasValidPath(m, n, r, circles)); 24 | } 25 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/array/KDiffPairsInAnArrayTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class KDiffPairsInAnArrayTest { 8 | 9 | private final KDiffPairsInAnArray testObject = new KDiffPairsInAnArray(); 10 | 11 | @Test 12 | public void testFindPairs() { 13 | int[] nums = new int[]{3, 1, 4, 1, 5}; 14 | int k = 2; 15 | assertEquals(2, testObject.findPairs(nums, k)); 16 | 17 | nums = new int[]{1, 2, 3, 4, 5}; 18 | k = 1; 19 | assertEquals(4, testObject.findPairs(nums, k)); 20 | 21 | nums = new int[]{1, 3, 1, 5, 4}; 22 | k = 0; 23 | assertEquals(1, testObject.findPairs(nums, k)); 24 | } 25 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/array/MaximizeDistanceToClosestPersonTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class MaximizeDistanceToClosestPersonTest { 8 | 9 | private final MaximizeDistanceToClosestPerson testObject = new MaximizeDistanceToClosestPerson(); 10 | 11 | @Test 12 | public void testMaxDistToClosest() { 13 | int[] seats = new int[]{1, 0, 0, 0, 1, 0, 1}; 14 | assertEquals(2, testObject.maxDistToClosest(seats)); 15 | 16 | seats = new int[]{1, 0, 0, 0}; 17 | assertEquals(3, testObject.maxDistToClosest(seats)); 18 | 19 | seats = new int[]{0, 1}; 20 | assertEquals(1, testObject.maxDistToClosest(seats)); 21 | } 22 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/array/PeakIndexInAMountainArrayTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class PeakIndexInAMountainArrayTest { 8 | 9 | private final PeakIndexInAMountainArray testObject = new PeakIndexInAMountainArray(); 10 | 11 | @Test 12 | public void testPeakIndexInMountainArray() { 13 | int[] nums = new int[]{0, 1, 0}; 14 | assertEquals(1, testObject.peakIndexInMountainArray(nums)); 15 | 16 | nums = new int[]{0, 2, 1, 0}; 17 | assertEquals(1, testObject.peakIndexInMountainArray(nums)); 18 | 19 | nums = new int[]{0, 10, 5, 2}; 20 | assertEquals(1, testObject.peakIndexInMountainArray(nums)); 21 | } 22 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/backtracking/LetterCasePermutationTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.backtracking; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import java.util.List; 6 | 7 | import static org.junit.jupiter.api.Assertions.assertEquals; 8 | 9 | public class LetterCasePermutationTest { 10 | 11 | private final LetterCasePermutation testObject = new LetterCasePermutation(); 12 | 13 | @Test 14 | public void testLetterCasePermutation() { 15 | String s = "a1b2"; 16 | List expected = List.of("A1B2", "A1b2", "a1B2", "a1b2"); 17 | assertEquals(expected, testObject.letterCasePermutation(s)); 18 | 19 | s = "3z4"; 20 | expected = List.of("3Z4", "3z4"); 21 | assertEquals(expected, testObject.letterCasePermutation(s)); 22 | } 23 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/backtracking/PartitionToKEqualSumSubsetsTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.backtracking; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertFalse; 6 | import static org.junit.jupiter.api.Assertions.assertTrue; 7 | 8 | public class PartitionToKEqualSumSubsetsTest { 9 | 10 | private final PartitionToKEqualSumSubsets testObject = new PartitionToKEqualSumSubsets(); 11 | 12 | @Test 13 | public void testCanPartitionKSubsets() { 14 | int[] nums = new int[]{4, 3, 2, 3, 5, 2, 1}; 15 | int k = 4; 16 | assertTrue(testObject.canPartitionKSubsets(nums, k)); 17 | 18 | nums = new int[]{1, 2, 3, 4}; 19 | k = 3; 20 | assertFalse(testObject.canPartitionKSubsets(nums, k)); 21 | } 22 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/dp/UniquePathsIITest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class UniquePathsIITest { 8 | 9 | private final UniquePathsII testObject = new UniquePathsII(); 10 | 11 | @Test 12 | public void testUniquePathsWithObstacles() { 13 | int[][] obstacleGrid = new int[][]{ 14 | {0, 0, 0}, 15 | {0, 1, 0}, 16 | {0, 0, 0} 17 | }; 18 | assertEquals(2, testObject.uniquePathsWithObstacles(obstacleGrid)); 19 | 20 | obstacleGrid = new int[][]{ 21 | {0, 1}, {0, 0} 22 | }; 23 | assertEquals(1, testObject.uniquePathsWithObstacles(obstacleGrid)); 24 | } 25 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/math/GuessNumberHigherOrLowerTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.math; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class GuessNumberHigherOrLowerTest { 8 | 9 | private final GuessNumberHigherOrLower testObject = new GuessNumberHigherOrLower(); 10 | 11 | @Test 12 | public void testGuessNumber() { 13 | testObject.pick = 6; 14 | assertEquals(6, testObject.guessNumber(10)); 15 | 16 | testObject.pick = 1; 17 | assertEquals(1, testObject.guessNumber(1)); 18 | 19 | testObject.pick = 1; 20 | assertEquals(1, testObject.guessNumber(2)); 21 | 22 | testObject.pick = 2; 23 | assertEquals(2, testObject.guessNumber(2)); 24 | } 25 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/stack/DecodeStringTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.stack; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class DecodeStringTest { 8 | 9 | private final DecodeString testObject = new DecodeString(); 10 | 11 | @Test 12 | public void testDecodeString() { 13 | String s = "3[a]2[bc]"; 14 | assertEquals("aaabcbc", testObject.decodeString(s)); 15 | 16 | s = "3[a2[c]]"; 17 | assertEquals("accaccacc", testObject.decodeString(s)); 18 | 19 | s = "2[abc]3[cd]ef"; 20 | assertEquals("abcabccdcdcdef", testObject.decodeString(s)); 21 | 22 | s = "abc3[cd]xyz"; 23 | assertEquals("abccdcdcdxyz", testObject.decodeString(s)); 24 | } 25 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/array/MinimumValueToGetPositiveStepByStepSumTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class MinimumValueToGetPositiveStepByStepSumTest { 8 | 9 | private final MinimumValueToGetPositiveStepByStepSum testObject = new MinimumValueToGetPositiveStepByStepSum(); 10 | 11 | @Test 12 | public void testMinStartValue() { 13 | int[] nums = new int[]{-3, 2, -3, 4, 2}; 14 | assertEquals(5, testObject.minStartValue(nums)); 15 | 16 | nums = new int[]{1, 2}; 17 | assertEquals(1, testObject.minStartValue(nums)); 18 | 19 | nums = new int[]{1, -2, -3}; 20 | assertEquals(5, testObject.minStartValue(nums)); 21 | } 22 | } -------------------------------------------------------------------------------- /WorkAtTech/src/test/java/org/redquark/onlinejudges/workattech/dp/CollectJewelsTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.workattech.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | import static org.redquark.onlinejudges.workattech.dp.CollectJewels.JewelStone; 7 | 8 | public class CollectJewelsTest { 9 | 10 | private final CollectJewels testObject = new CollectJewels(); 11 | 12 | @Test 13 | public void testGetMaxValue() { 14 | JewelStone[] stones = new JewelStone[]{ 15 | new JewelStone(1, 3), 16 | new JewelStone(2, 4), 17 | new JewelStone(3, 5), 18 | new JewelStone(4, 7) 19 | }; 20 | int capacity = 5; 21 | assertEquals(10, testObject.getMaxValue(stones, capacity)); 22 | } 23 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/array/ContainsDuplicateTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertFalse; 6 | import static org.junit.jupiter.api.Assertions.assertTrue; 7 | 8 | public class ContainsDuplicateTest { 9 | 10 | private final ContainsDuplicate testObject = new ContainsDuplicate(); 11 | 12 | @Test 13 | public void testContainsDuplicate() { 14 | int[] nums = new int[]{1, 2, 3, 1}; 15 | assertTrue(testObject.containsDuplicate(nums)); 16 | 17 | nums = new int[]{1, 2, 3, 4}; 18 | assertFalse(testObject.containsDuplicate(nums)); 19 | 20 | nums = new int[]{1, 1, 1, 3, 3, 4, 3, 2, 4, 2}; 21 | assertTrue(testObject.containsDuplicate(nums)); 22 | } 23 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/design/PeekingIteratorTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.design; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import java.util.Arrays; 6 | import java.util.List; 7 | 8 | import static org.junit.jupiter.api.Assertions.assertEquals; 9 | import static org.junit.jupiter.api.Assertions.assertFalse; 10 | 11 | public class PeekingIteratorTest { 12 | 13 | @Test 14 | public void test() { 15 | List elements = Arrays.asList(1, 2, 3); 16 | PeekingIterator testObject = new PeekingIterator(elements.iterator()); 17 | assertEquals(1, testObject.next()); 18 | assertEquals(2, testObject.peek()); 19 | assertEquals(2, testObject.next()); 20 | assertEquals(3, testObject.next()); 21 | assertFalse(testObject.hasNext()); 22 | } 23 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/dp/LongestStringChainTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class LongestStringChainTest { 8 | 9 | private final LongestStringChain testObject = new LongestStringChain(); 10 | 11 | @Test 12 | public void testLongestStrChain() { 13 | String[] words = new String[]{"a", "b", "ba", "bca", "bda", "bdca"}; 14 | assertEquals(4, testObject.longestStrChain(words)); 15 | 16 | words = new String[]{"xbc", "pcxbcf", "xb", "cxbc", "pcxbc"}; 17 | assertEquals(5, testObject.longestStrChain(words)); 18 | 19 | words = new String[]{"abcd", "dbqca"}; 20 | assertEquals(1, testObject.longestStrChain(words)); 21 | } 22 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/graph/JumpGameIVTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.graph; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class JumpGameIVTest { 8 | 9 | private final JumpGameIV testObject = new JumpGameIV(); 10 | 11 | @Test 12 | public void testMinJumps() { 13 | int[] arr = new int[]{100, -23, -23, 404, 100, 23, 23, 23, 3, 404}; 14 | assertEquals(3, testObject.minJumps(arr)); 15 | 16 | arr = new int[]{7}; 17 | assertEquals(0, testObject.minJumps(arr)); 18 | 19 | arr = new int[]{7, 6, 9, 6, 9, 6, 9, 7}; 20 | assertEquals(1, testObject.minJumps(arr)); 21 | 22 | arr = new int[]{6, 1, 9}; 23 | assertEquals(2, testObject.minJumps(arr)); 24 | } 25 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/string/MinimumDeletionsToMakeCharacterFrequenciesUniqueTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.string; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class MinimumDeletionsToMakeCharacterFrequenciesUniqueTest { 8 | 9 | private final MinimumDeletionsToMakeCharacterFrequenciesUnique testObject = 10 | new MinimumDeletionsToMakeCharacterFrequenciesUnique(); 11 | 12 | @Test 13 | public void testMinDeletions() { 14 | String s = "aab"; 15 | assertEquals(0, testObject.minDeletions(s)); 16 | 17 | s = "aaabbbcc"; 18 | assertEquals(2, testObject.minDeletions(s)); 19 | 20 | s = "ceabaacb"; 21 | assertEquals(2, testObject.minDeletions(s)); 22 | } 23 | } -------------------------------------------------------------------------------- /LeetCode/src/main/java/org/redquark/onlinejudges/leetcode/dp/CoinChangeII.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.dp; 2 | 3 | public class CoinChangeII { 4 | 5 | public int change(int amount, int[] coins) { 6 | // Special case 7 | if (coins == null || coins.length == 0 || amount < 0) { 8 | return 0; 9 | } 10 | // Lookup table to store number of ways to 11 | // make up the target using certain coins 12 | int[] lookup = new int[amount + 1]; 13 | lookup[0] = 1; 14 | // Populate the table from 1 to n. 15 | for (int coin : coins) { 16 | for (int i = 1; i <= amount; i++) { 17 | if (coin <= i) { 18 | lookup[i] += lookup[i - coin]; 19 | } 20 | } 21 | } 22 | return lookup[amount]; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/design/DesignHashSetTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.design; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertFalse; 6 | import static org.junit.jupiter.api.Assertions.assertTrue; 7 | import static org.redquark.onlinejudges.leetcode.design.DesignHashSet.MyHashSet; 8 | 9 | public class DesignHashSetTest { 10 | 11 | @Test 12 | public void test() { 13 | MyHashSet testObject = new MyHashSet(); 14 | testObject.add(1); 15 | testObject.add(2); 16 | assertTrue(testObject.contains(1)); 17 | assertFalse(testObject.contains(3)); 18 | testObject.add(2); 19 | assertTrue(testObject.contains(2)); 20 | testObject.remove(2); 21 | assertFalse(testObject.contains(2)); 22 | } 23 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/dp/NumberOfWaysToFormATargetStringGivenADictionaryTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.dp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class NumberOfWaysToFormATargetStringGivenADictionaryTest { 8 | 9 | private final NumberOfWaysToFormATargetStringGivenADictionary testObject = new NumberOfWaysToFormATargetStringGivenADictionary(); 10 | 11 | @Test 12 | public void testNumWays() { 13 | String[] words = new String[]{"acca", "bbbb", "caca"}; 14 | String target = "aba"; 15 | assertEquals(6, testObject.numWays(words, target)); 16 | 17 | words = new String[]{"abba", "baab"}; 18 | target = "bab"; 19 | assertEquals(4, testObject.numWays(words, target)); 20 | } 21 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/string/LongestPalindromicSubstringTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.string; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class LongestPalindromicSubstringTest { 8 | 9 | private final LongestPalindromicSubstring testObject = new LongestPalindromicSubstring(); 10 | 11 | @Test 12 | public void testLongestPalindrome() { 13 | String s = "babad"; 14 | assertEquals("aba", testObject.longestPalindrome(s)); 15 | 16 | s = "cbbd"; 17 | assertEquals("bb", testObject.longestPalindrome(s)); 18 | 19 | s = "a"; 20 | assertEquals("a", testObject.longestPalindrome(s)); 21 | 22 | s = "ac"; 23 | assertEquals("c", testObject.longestPalindrome(s)); 24 | } 25 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/string/PartitioningIntoMinimumNumberOfDeciBinaryNumbersTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.string; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class PartitioningIntoMinimumNumberOfDeciBinaryNumbersTest { 8 | 9 | private final PartitioningIntoMinimumNumberOfDeciBinaryNumbers testObject = 10 | new PartitioningIntoMinimumNumberOfDeciBinaryNumbers(); 11 | 12 | @Test 13 | public void testMinPartitions() { 14 | String n = "32"; 15 | assertEquals(3, testObject.minPartitions(n)); 16 | 17 | n = "82734"; 18 | assertEquals(8, testObject.minPartitions(n)); 19 | 20 | n = "27346209830709182346"; 21 | assertEquals(9, testObject.minPartitions(n)); 22 | } 23 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/array/KokoEatingBananasTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class KokoEatingBananasTest { 8 | 9 | private final KokoEatingBananas testObject = new KokoEatingBananas(); 10 | 11 | @Test 12 | public void testMinEatingSpeed() { 13 | int[] piles = new int[]{3, 6, 7, 11}; 14 | int h = 8; 15 | assertEquals(4, testObject.minEatingSpeed(piles, h)); 16 | 17 | piles = new int[]{30, 11, 23, 4, 20}; 18 | h = 5; 19 | assertEquals(30, testObject.minEatingSpeed(piles, h)); 20 | 21 | piles = new int[]{30, 11, 23, 4, 20}; 22 | h = 6; 23 | assertEquals(23, testObject.minEatingSpeed(piles, h)); 24 | } 25 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/array/ShortestUnsortedContinuousSubarrayTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class ShortestUnsortedContinuousSubarrayTest { 8 | 9 | private final ShortestUnsortedContinuousSubarray testObject = new ShortestUnsortedContinuousSubarray(); 10 | 11 | @Test 12 | public void testFindUnsortedSubarray() { 13 | int[] nums = new int[]{2, 6, 4, 8, 10, 9, 15}; 14 | assertEquals(5, testObject.findUnsortedSubarray(nums)); 15 | 16 | nums = new int[]{1, 2, 3, 4}; 17 | assertEquals(0, testObject.findUnsortedSubarray(nums)); 18 | 19 | nums = new int[]{1}; 20 | assertEquals(0, testObject.findUnsortedSubarray(nums)); 21 | } 22 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/array/MinimumDominoRotationsForEqualRowTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class MinimumDominoRotationsForEqualRowTest { 8 | 9 | private final MinimumDominoRotationsForEqualRow testObject = new MinimumDominoRotationsForEqualRow(); 10 | 11 | @Test 12 | public void testMinDominoRotations() { 13 | int[] tops = new int[]{2, 1, 2, 4, 2, 2}; 14 | int[] bottoms = new int[]{5, 2, 6, 2, 3, 2}; 15 | assertEquals(2, testObject.minDominoRotations(tops, bottoms)); 16 | 17 | tops = new int[]{3, 5, 1, 2, 3}; 18 | bottoms = new int[]{3, 6, 3, 3, 4}; 19 | assertEquals(-1, testObject.minDominoRotations(tops, bottoms)); 20 | } 21 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/array/SummaryRangesTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.array; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import java.util.Arrays; 6 | import java.util.List; 7 | 8 | import static org.junit.jupiter.api.Assertions.assertEquals; 9 | 10 | public class SummaryRangesTest { 11 | 12 | private final SummaryRanges testObject = new SummaryRanges(); 13 | 14 | @Test 15 | public void testSummaryRanges() { 16 | int[] nums = new int[]{0, 1, 2, 4, 5, 7}; 17 | List expected = Arrays.asList("0->2", "4->5", "7"); 18 | assertEquals(expected, testObject.summaryRanges(nums)); 19 | 20 | nums = new int[]{0, 2, 3, 4, 6, 8, 9}; 21 | expected = Arrays.asList("0", "2->4", "6", "8->9"); 22 | assertEquals(expected, testObject.summaryRanges(nums)); 23 | } 24 | } -------------------------------------------------------------------------------- /LeetCode/src/test/java/org/redquark/onlinejudges/leetcode/string/PartitionLabelsTest.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.string; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import java.util.Arrays; 6 | import java.util.Collections; 7 | import java.util.List; 8 | 9 | import static org.junit.jupiter.api.Assertions.assertEquals; 10 | 11 | public class PartitionLabelsTest { 12 | 13 | private final PartitionLabels testObject = new PartitionLabels(); 14 | 15 | @Test 16 | public void testPartitionLabels() { 17 | String s = "ababcbacadefegdehijhklij"; 18 | List expected = Arrays.asList(9, 7, 8); 19 | assertEquals(expected, testObject.partitionLabels(s)); 20 | 21 | s = "eccbbbbdec"; 22 | expected = Collections.singletonList(10); 23 | assertEquals(expected, testObject.partitionLabels(s)); 24 | } 25 | } -------------------------------------------------------------------------------- /WorkAtTech/src/main/java/org/redquark/onlinejudges/workattech/dp/RodCutting.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.workattech.dp; 2 | 3 | public class RodCutting { 4 | 5 | public int maximumProfit(int n, int[] prices) { 6 | // Special case 7 | if (n == 0) { 8 | return 0; 9 | } 10 | // Lookup table to store the maximum profit 11 | // for lengths from 0 to n. 12 | int[] lookup = new int[n + 1]; 13 | // Loop through the array 14 | for (int i = 1; i <= n; i++) { 15 | // If one piece is of length i then 16 | // the other piece would be of length 17 | // n - i. 18 | for (int j = 1; j <= i; j++) { 19 | lookup[i] = Math.max(lookup[i], prices[j - 1] + lookup[i - j]); 20 | } 21 | } 22 | return lookup[n]; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode/src/main/java/org/redquark/onlinejudges/leetcode/string/ReverseString.java: -------------------------------------------------------------------------------- 1 | package org.redquark.onlinejudges.leetcode.string; 2 | 3 | /** 4 | * @author Anirudh Sharma 5 | */ 6 | public class ReverseString { 7 | 8 | public char[] reverseString(char[] s) { 9 | // Special case 10 | if (s == null || s.length == 0) { 11 | return s; 12 | } 13 | // Left and right pointers 14 | int left = 0; 15 | int right = s.length - 1; 16 | // Loop until the two pointers meet 17 | while (left < right) { 18 | // Swap the characters at left and right pointers 19 | char temp = s[left]; 20 | s[left] = s[right]; 21 | s[right] = temp; 22 | // Update the pointers 23 | left++; 24 | right--; 25 | } 26 | return s; 27 | } 28 | } 29 | --------------------------------------------------------------------------------