MCPcopy Create free account

hub / github.com/FreeTymeKiyan/LeetCode-Sol-Res / functions

Functions1,974 in github.com/FreeTymeKiyan/LeetCode-Sol-Res

↓ 1 callersMethodisStrobogrammatic
Math. Take a look at all digits from 0 to 9. 0,1,8 are strobogrammatic no matter what. 6 and 9 can form a strobogrammatic pair, which means they must
src/main/java/com/freetymekiyan/algorithms/level/easy/StrobogrammaticNumber.java:25
↓ 1 callersMethodisValid
src/main/cpp/020_Valid_Parentheses.cpp:18
↓ 1 callersMethodisValid
Use an integer counter as a Stack. Make sure there are more left parens than right ones. Increase the counter when there is a left paren. If there is
src/main/java/com/freetymekiyan/algorithms/level/hard/RemoveInvalidParentheses.java:92
↓ 1 callersMethodisValid
(int i, int j, int k, int num)
src/main/java/com/freetymekiyan/algorithms/level/hard/SudokuSolver.java:91
↓ 1 callersMethodisValid
Stack. Use stack to check pair. Whenever there is a valid pair, pop from stack since it is already valid. Quick check: If string length not even, retu
src/main/java/com/freetymekiyan/algorithms/level/easy/ValidParentheses.java:34
↓ 1 callersMethodisValid2
Stack. Push closing parens onto stack instead of opening ones. This way match function can be saved. When it is a right paren: | If the stack is empty
src/main/java/com/freetymekiyan/algorithms/level/easy/ValidParentheses.java:67
↓ 1 callersMethodisValidSudoku
src/main/cpp/036_Valid_Sudoku.cpp:37
↓ 1 callersMethodisValidSudoku
(char[][] board)
src/main/java/com/freetymekiyan/algorithms/level/easy/ValidSudoku.java:38
↓ 1 callersMethodisWord
(String s)
src/main/java/com/freetymekiyan/algorithms/other/FindWords.java:39
↓ 1 callersMethodkSum
Sort num array first
src/main/java/com/freetymekiyan/algorithms/other/KSum.java:26
↓ 1 callersMethodkeysWithPrefix
(String pre)
src/main/java/com/freetymekiyan/datastructures/TrieST.java:113
↓ 1 callersMethodladderLength
src/main/cpp/127_Word_Ladder.cpp:35
↓ 1 callersMethodladderLength
BFS. Level-order Traversal. Search from begin word to end word. Use an integer to track ladder length. Add begin word to queue. Remove begin word from
src/main/java/com/freetymekiyan/algorithms/level/medium/WordLadder.java:49
↓ 1 callersMethodladderLengthB
BFS, search from both ends.
src/main/java/com/freetymekiyan/algorithms/level/medium/WordLadder.java:90
↓ 1 callersMethodlargestNumber
Create a comparator for sorting Convert num to String and compare the concatenated result of them Note {0, 0} is a special case
src/main/java/com/freetymekiyan/algorithms/level/medium/LargestNum.java:29
↓ 1 callersMethodlargestRectangleArea
Only height is smaller do update happens Stack for indices add a zero height into the group
src/main/java/com/freetymekiyan/algorithms/level/hard/LargestRectangleInHist.java:33
↓ 1 callersMethodlengthOfLIS
DP. O(nlogn) time. O(n) space. Store the tail of a LIS at a specific length. dp[0] means the tail value of array at length 1. Current number can eithe
src/main/java/com/freetymekiyan/algorithms/level/medium/LongestIncreasingSubsequence.java:34
↓ 1 callersMethodlengthOfLIS2
DP. O(n^2) time, O(n) space. We can store the longest increasing subseqence lengths of [0, i-1]. Now at index i, if nums[i] is larger than nums[x], it
src/main/java/com/freetymekiyan/algorithms/level/medium/LongestIncreasingSubsequence.java:65
↓ 1 callersMethodlengthOfList
(ListNode node)
src/main/java/com/freetymekiyan/algorithms/level/medium/ConvertSortedListToBST.java:46
↓ 1 callersMethodlengthOfLongestSubstring
src/main/cpp/003_Longest_Substring_Without_Repeating_Characters.cpp:20
↓ 1 callersMethodlengthOfLongestSubstring
Traverse the string Get current character Update start point Update max Put current char in map
src/main/java/com/freetymekiyan/algorithms/level/easy/LongestSubstring.java:31
↓ 1 callersMethodlengthOfLongestSubstringTwoDistinct
(String s)
src/main/java/com/freetymekiyan/algorithms/level/hard/LongestSubstringWithAtMostTwoDistinctCharacters.java:33
↓ 1 callersMethodletterCombinations
src/main/cpp/017_Letter_Combinations_of_a_Phone_Number.cpp:65
↓ 1 callersMethodlistLength
(ListNode head)
src/main/java/com/freetymekiyan/algorithms/level/medium/RotateList.java:68
↓ 1 callersMethodlongest
DP d[i][j] = max{d[i+1][j], d[i-1][j], d[i][j+1], d[i][j-1]} + 1
src/main/java/com/freetymekiyan/algorithms/other/LongestIncreasingSequenceInMat.java:36
↓ 1 callersMethodlongestConsecutive
Set. Add numbers into a set first. Then for each number i: 1) Make sure it's the start of a sequence by check i - 1. 2) If it's the start, check i + 1
src/main/java/com/freetymekiyan/algorithms/level/hard/LongestConsecutiveSeq.java:33
↓ 1 callersMethodlongestConsecutive2
Union Find. O(n) Time. View consecutive sequence as connected component. Use a map to store value to connected component id mapping. For each number n
src/main/java/com/freetymekiyan/algorithms/level/hard/LongestConsecutiveSeq.java:63
↓ 1 callersMethodlongestPalindrome
src/main/cpp/005_Longest_Palindromic_Substring.cpp:19
↓ 1 callersMethodlongestPalindrome
Manacher's Algorithm, O(n) Time. Insert a special character between each character in a string, and also both at the start and the end. S = “abba” =>
src/main/java/com/freetymekiyan/algorithms/level/medium/LongestPalindromicSubstring.java:24
↓ 1 callersMethodlongestPath
Backtracking. Maintain a max length, a current length and current values. When at a leaf node, compare with existing max to see if the path is longest
src/main/java/com/freetymekiyan/algorithms/other/BinaryTreeLongestPath.java:17
↓ 1 callersMethodlowestCommonAncestor
Recursive. Recurrence relation: Search for p and q in left and right subtrees. If both are found, it means the two nodes are in different subtrees, ro
src/main/java/com/freetymekiyan/algorithms/level/medium/LowestCommonAncestorOfABinaryTree.java:42
↓ 1 callersMethodlowestCommonAncestor2
Iterative. Two stacks. First, DFS for p or q. If we found anyone of them, copy current stack to a new stack, which keeps all its ancestors. Then we tr
src/main/java/com/freetymekiyan/algorithms/level/medium/LowestCommonAncestorOfABinaryTree.java:61
↓ 1 callersMethodmatchTree
Check if both trees are the same
src/main/java/com/freetymekiyan/algorithms/other/IsSubtree.java:36
↓ 1 callersMethodmaxArea
src/main/cpp/011_Container_With_Most_Water.cpp:19
↓ 1 callersMethodmaxDepth
src/main/cpp/104_Maximum_Depth_of_Binary_Tree.cpp:30
↓ 1 callersMethodmaxDepth
Recursive. By definition we should compare the height of each node. Tree's height range from -1 to h. -1 is null, 0 is root's height. So instead of he
src/main/java/com/freetymekiyan/algorithms/level/easy/BalancedBinaryTree.java:36
↓ 1 callersMethodmaxKadane
Find max sum contiguous subsequences
src/main/java/com/freetymekiyan/algorithms/other/MaxSubseqDifference.java:31
↓ 1 callersMethodmaxProfit
src/main/cpp/122_Best_Time_to_Buy_and_Sell_Stock_II.cpp:24
↓ 1 callersMethodmaxProfit
DP. Bottom-up. Space optimized. O(n) Time, O(1) Space. Recurrence Relation: max profit of ith day = a[n] - min > max(i-1) ? a[n] - min : max(i-1). To
src/main/java/com/freetymekiyan/algorithms/level/medium/BestTimeStock.java:40
↓ 1 callersMethodmaxProfit
DP. Goes forward to build single transaction max profit Then goes backward to build max since day i profit Find the max of the sum of these two
src/main/java/com/freetymekiyan/algorithms/level/medium/BestTimeToBuyAndSellStock3.java:30
↓ 1 callersMethodmaxProfit
Sell whenever there is profit. If next value is bigger, add the difference up to the profit
src/main/java/com/freetymekiyan/algorithms/level/medium/BestTimeStock2.java:26
↓ 1 callersMethodmaxProfit
DP. Bottom-up. Optimized. O(n) Time, O(1) Space. Maximum profit is the maximum price - minimum price. So loop from second day to last day, track min p
src/main/java/com/freetymekiyan/algorithms/level/easy/BestTimeToBuyAndSellStock.java:44
↓ 1 callersMethodmaxProfit
(int[] prices)
src/main/java/com/freetymekiyan/algorithms/level/easy/BestTimetoBuyandSellStock1.java:18
↓ 1 callersMethodmaxProfit2
DP. Bottom-up. O(n) Time, O(n) Space. Store max profit of each day using an integer array. Profit of next day can only be same as last day or update b
src/main/java/com/freetymekiyan/algorithms/level/medium/BestTimeStock.java:57
↓ 1 callersMethodmaxProfit2
DP. More compact version. sell is skipped on day 1 since we don't have any share. hold is initialized as MIN since we have negative values, 0 will be
src/main/java/com/freetymekiyan/algorithms/level/medium/BestTimeToBuyAndSellStockWithTransactionFee.java:71
↓ 1 callersMethodmaxProfitB
DP. Bottom-up. O(n) Time. O(n) Space. Keep track of minimum price of the stock. Keep track of the maximum profit before today. Get the maximum profit
src/main/java/com/freetymekiyan/algorithms/level/easy/BestTimeToBuyAndSellStock.java:69
↓ 1 callersMethodmaxSubArray
DP. O(n) Time, O(1) Space. State: the max subarray sum ending at i. Recurrence Relation: Since the sub array is contiguous, nums[i] must be included.
src/main/java/com/freetymekiyan/algorithms/level/medium/MaximumSubarray.java:30
↓ 1 callersMethodmaxSumOfThreeSubarrays
A: [1,2,1,2,6,7,6,1] -> B: [3,3,3,8,13,13,7] B is the k-interval sum. The index of element in B is the starting index of the subarray in A. It makes t
src/main/java/com/freetymekiyan/algorithms/level/hard/MaximumSumOf3NonOverlappingSubarrays.java:40
↓ 1 callersMethodmaximumGap
(int[] nums)
others/MaximumGap_shuna.java:21
↓ 1 callersMethodmaximumGap
O(n) Time, O(n) Space Find max and min in one traverse Calculate bucket length and divide numbers into buckets Traverse buckets to find max gap
src/main/java/com/freetymekiyan/algorithms/level/hard/MaximumGap.java:30
↓ 1 callersMethodmaximumSwap
Math. Swap the digit that is: 1. larger. 2. max amongst all larger ones. Build a digit to last index of the digit mapping to get the last position of
src/main/java/com/freetymekiyan/algorithms/level/medium/MaximumSwap.java:30
↓ 1 callersMethodmerge
src/main/cpp/088_Merge_Sorted_Array.cpp:17
↓ 1 callersMethodmerge
Copy items from low to high to a helper array Init 2 pointers Compare value of 2 pointers, and overwrite original array, and move on Stop when left re
src/main/java/com/freetymekiyan/algorithms/other/MergeSort.java:42
↓ 1 callersMethodmergeSort
Cut into two halves Sort left half first, move to right half's pre head and sort right Merge two halves Insert node in latter part if its smaller than
src/main/java/com/freetymekiyan/algorithms/level/medium/SortList.java:33
↓ 1 callersMethodmergeTwoLists
src/main/cpp/021_Merge_Two_Sorted_List.cpp:64
↓ 1 callersMethodminCut
src/main/cpp/132_Palindrome_Partitioning_II.cpp:24
↓ 1 callersMethodminDistance
src/main/cpp/072_Edit_Distance.cpp:23
↓ 1 callersMethodminInsertionsDP
DP, bottom-up Fill a table in diagonal direction
src/main/java/com/freetymekiyan/algorithms/other/MinInsertionsToFormPalindrome.java:20
↓ 1 callersMethodminKadane
Modification of maxKadane to find min sum contiguous subsequence
src/main/java/com/freetymekiyan/algorithms/other/MaxSubseqDifference.java:58
↓ 1 callersMethodminPathSum
DP. bottom-up row by row, use an array to store values
src/main/java/com/freetymekiyan/algorithms/level/medium/MinPathSum.java:23
↓ 1 callersMethodminSwap
Dynamic Programming. The result is guaranteed to be possible. So either both are already increasing, or the increasing can be fixed with swapping. Mod
src/main/java/com/freetymekiyan/algorithms/level/medium/MinimumSwapsToMakeSequencesIncreasing.java:53
↓ 1 callersMethodminWindow
(String s, String t)
others/MinimumWindowSubstring.java:31
↓ 1 callersMethodminWindow
Two Pointers. Hash Table. Two pointers to represent a moving window. Hash table to record count of letters in S and T. To know whether all T's letters
src/main/java/com/freetymekiyan/algorithms/level/hard/MinimumWindowSubstring.java:39
↓ 1 callersMethodminWindow
DP. State: The starting index (+1) of the valid window in S where T has length i and S has length j. Base case: T is empty, starting index is always j
src/main/java/com/freetymekiyan/algorithms/level/hard/MinimumWindowSubsequence.java:51
↓ 1 callersMethodminWindow2
Hash Table. Two Pointers. <p> 1. Use two pointers: start and end to represent a window. 2. Move end to find a valid window. 3. When a valid window is
src/main/java/com/freetymekiyan/algorithms/level/hard/MinimumWindowSubstring.java:88
↓ 1 callersMethodminWindow2
Hash Table. Pre-computing. Find the index of the next character in T in S. Instead of searching in S every time, we may iterate S once to pre-compute
src/main/java/com/freetymekiyan/algorithms/level/hard/MinimumWindowSubsequence.java:100
↓ 1 callersMethodminimumTotal
DP Math.min(result.get(i), result.get(i + 1)) + triangle.get(curLv).get(i) Pick the smaller one of next row and add it up to current level
src/main/java/com/freetymekiyan/algorithms/level/medium/Triangle.java:39
↓ 1 callersMethodmorrisInorder
Morris Traversal, link null left node to its inorder predecessor link null right node to its inorder successor Initialize current as root While curren
src/main/java/com/freetymekiyan/algorithms/level/hard/RecoverBinarySearchTree.java:46
↓ 1 callersMethodmostFrequent
(int[] array)
src/main/java/com/freetymekiyan/algorithms/other/MostFrequentElementInArray.java:14
↓ 1 callersMethodmoveZeroes
Two Pointers. One-pass. Track the end of non-zero elements with a pointer. Swap non-zero elements to the front. For each number n in the array: | If n
src/main/java/com/freetymekiyan/algorithms/level/easy/MoveZeroes.java:32
↓ 1 callersMethodmoveZeroes2
Two pointers. One pointer goes through the array and find positive numbers. The other tracks the next position to be filled up. Move all positive numb
src/main/java/com/freetymekiyan/algorithms/level/easy/MoveZeroes.java:53
↓ 1 callersMethodmultiply
src/main/cpp/043_Multiply_Strings.cpp:22
↓ 1 callersMethodmySqrt
src/main/cpp/069_Sqrt(x).cpp:22
↓ 1 callersMethodmySqrt
Binary Search. Validate input first. If x < 0 , invalid. Special cases: If x = 0 or 1, return x. <p> Binary Search from 1 ~ x/2. While lo < hi: | Roun
src/main/java/com/freetymekiyan/algorithms/level/medium/Sqrt.java:46
↓ 1 callersMethodmySqrt2
Why return hi not lo in the end? The loop breaks when lo > hi. So the previous loop lo = hi = mid. Now, mid != t, because it didn't return. If mid < t
src/main/java/com/freetymekiyan/algorithms/level/medium/Sqrt.java:74
↓ 1 callersMethodmySqrt3
(int x)
src/main/java/com/freetymekiyan/algorithms/level/medium/Sqrt.java:88
↓ 1 callersMethodnext
()
src/main/java/com/freetymekiyan/algorithms/other/IntegerIterator.java:47
↓ 1 callersMethodnext
()
src/main/java/com/freetymekiyan/algorithms/level/medium/PeekingIterator.java:66
↓ 1 callersMethodnextCoordinates
(int curCol, int curRow)
src/main/java/com/freetymekiyan/algorithms/other/IntegerIterator.java:38
↓ 1 callersMethodnextPermutation
src/main/cpp/031_Next_Permutation.cpp:29
↓ 1 callersMethodnextPermutation
e.g.: 1234 -> 1243, 1243 -> 1324 Traverse backward to get 3 Then traverse forward to get furthest number bigger than 3 Swap these two digits and rever
src/main/java/com/freetymekiyan/algorithms/level/hard/Permutations2.java:44
↓ 1 callersMethodnormalize
use ax + by = c to represent a line and a|b|c as a key for that line a, b, c should be normalized, how? <p> special case, vertical, horizontal
src/main/java/com/freetymekiyan/algorithms/level/hard/MaxPointsOnALine.java:66
↓ 1 callersMethodnormalize
use ax + by = c to represent a line and a|b|c as a key for that line a, b, c should be normalized, how? special case, vertical, horizontal
src/main/java/com/freetymekiyan/algorithms/level/hard/MaxPoints.java:61
↓ 1 callersMethodnumDecodings
DP, Bottom Up. State: # of ways to decode string's substring from [0, i], ways[i]. Base case: ways[0] = 1. Only 1 way to decode empty string. ways[1]
src/main/java/com/freetymekiyan/algorithms/level/hard/DecodeWays2.java:53
↓ 1 callersMethodnumDecodings2
DP, Optimized Space. Reduce space usage from O(n) array to O(1) variables since we are only using the previous two values to calculate.
src/main/java/com/freetymekiyan/algorithms/level/hard/DecodeWays2.java:69
↓ 1 callersMethodnumDistinct
(String s, String t)
src/main/java/com/freetymekiyan/algorithms/level/hard/DistinctSubsequences_shuna.java:16
↓ 1 callersMethodnumIslands
BFS. DFS. Start from the top-left corner of the grid. Go through each grid row by row and check if it is a land. If it is not, skip. If it is, BFS / D
src/main/java/com/freetymekiyan/algorithms/level/medium/NumberOfIslands.java:48
↓ 1 callersMethodnumIslandsUnionFind
Union find. Find the # of connected components. Union find can mark the grids. But to find out # of connected components, we must understand that: 1.
src/main/java/com/freetymekiyan/algorithms/level/medium/NumberOfIslands.java:144
↓ 1 callersMethodnumSquares1
A natural number is... <p> 1. a square if and only if each prime factor occurs to an even power in the number's prime factorization. 2. a sum of two s
src/main/java/com/freetymekiyan/algorithms/level/medium/PerfectSquares.java:37
↓ 1 callersMethodnumSquares2
DP, bottom-up
src/main/java/com/freetymekiyan/algorithms/level/medium/PerfectSquares.java:54
↓ 1 callersMethodnumberOfArithmeticSlices
If we find an arithmetic array of length n, the number of slices it has is: (n - 2) (n - 1) / 2 E.g. [1, 2, 3, 4], n = 4, # of arithmetic slices = (4-
src/main/java/com/freetymekiyan/algorithms/level/medium/ArithmeticSlices.java:50
↓ 1 callersMethodnumberOfArithmeticSlices2
Same idea. Slightly different implementation. We skip the first 2 integers in A and start directly from the 3rd. Record the count of integers that can
src/main/java/com/freetymekiyan/algorithms/level/medium/ArithmeticSlices.java:79
↓ 1 callersMethodnumberOfArithmeticSlices3
DP. Space Optimized. State: dp[i]: # of arithmetic slices with integer A[i]. Recurrence Relation: Suppose we have x arithmetic slices in A[0...i-1]. I
src/main/java/com/freetymekiyan/algorithms/level/medium/ArithmeticSlices.java:112
↓ 1 callersMethodnumberToWords
Math. String. Try to find the pattern first. The numbers less than 1000, e.g. xyz, can be x Hundred y"ty" z. The numbers larger than 1000, we need to
src/main/java/com/freetymekiyan/algorithms/level/hard/IntegerToEnglishWords.java:48
↓ 1 callersMethodoutput
(String s)
src/main/java/com/freetymekiyan/algorithms/other/CharFrequencyOrderOfAString.java:18
↓ 1 callersMethodpalindromePairs
Hash Table. Several cases: 1. If s1 is a blank string, then for any string s2 that is a palindrome, s1+s2 and s2+s1 are palindrome. 2. If s2 is the re
src/main/java/com/freetymekiyan/algorithms/level/hard/PalindromePairs.java:42
↓ 1 callersMethodpalindromePairs2
Trie. Checking whether 2 words form a palindrome can be optimized. By checking whether the current word's prefix is some others' suffix. Adding the re
src/main/java/com/freetymekiyan/algorithms/level/hard/PalindromePairs.java:112
↓ 1 callersMethodpancakeSort
Find max from from start to end If max is not at the end, filp it to first and flip it to end Reduce array size by one Stop till size reduced to 1
src/main/java/com/freetymekiyan/algorithms/other/PancakeSorting.java:24
↓ 1 callersMethodpartition
src/main/cpp/131_Palindrome_Partitioning.cpp:73
↓ 1 callersMethodpartition
Choose mid value as pivot Move two pointers Swap and move on Return left pointer
src/main/java/com/freetymekiyan/algorithms/other/KthLargest.java:68
← previousnext →501–600 of 1,974, ranked by callers