MCPcopy Create free account

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

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

MethodminDistance
DP, O(nm) Time, O(nm) Space Searching for a path (sequence of edits) from the start string to the final string For two strings, X of length n, Y of le
src/main/java/com/freetymekiyan/algorithms/level/hard/EditDistance.java:34
MethodminDistanceOptimal
Optimal DP. Reduce table to a row.
src/main/java/com/freetymekiyan/algorithms/level/hard/EditDistance.java:55
MethodminInsertions
Recursive Let the input string be str[l……h]. The problem can be broken down into 3 parts: 1. Find the minimum # of insertions in the substring str[l+1
src/main/java/com/freetymekiyan/algorithms/other/MinInsertionsToFormPalindrome.java:45
MethodminMeetingRooms
Sort. Heap. Greedy. Always put the next starting meeting after the first ending meeting. If the start time overlaps with the nearest end time, need a
src/main/java/com/freetymekiyan/algorithms/level/medium/MeetingRooms2.java:35
MethodminMeetingRooms2
Sort. Sort all starts and ends separately. Then merge them with 2 pointers. When a new meeting starts earlier than the most recent old meeting ends, i
src/main/java/com/freetymekiyan/algorithms/level/medium/MeetingRooms2.java:57
MethodminPatches
Build the largest range possible and find the smallest missing number if missing >= nums[i], range can be extended to missing + nums[i] else add missi
src/main/java/com/freetymekiyan/algorithms/level/medium/PatchingArray.java:36
MethodminSubArrayLen
Two Pointers. O(n) Time. Use start and end index to represent a window. Store the window sum and min length. Move the end index and update window sum.
src/main/java/com/freetymekiyan/algorithms/level/medium/MinSizeSubarraySum.java:30
MethodminSubArrayLen2
Binary Search. O(nlogn) Time. Binary search for a window size. Use a helper function to check whether this window exists. If it exists, then we can ha
src/main/java/com/freetymekiyan/algorithms/level/medium/MinSizeSubarraySum.java:56
MethodminWindow3
Two pointers. Avoid substring when there is a new minimum. Store the starting position with an integer instead.
src/main/java/com/freetymekiyan/algorithms/level/hard/MinimumWindowSubstring.java:131
MethodmissingNumber1
Math Considering that the n numbers are distinct, we can get the sum of the array Then substract it from the sum of 0 ~ n
src/main/java/com/freetymekiyan/algorithms/level/medium/MissingNumber.java:23
MethodmissingNumber2
avoid overflow
src/main/java/com/freetymekiyan/algorithms/level/medium/MissingNumber.java:32
MethodmissingNumber3
Bit Manipulation xor all numbers in the array and from 0 to n, the result is the missing number
src/main/java/com/freetymekiyan/algorithms/level/medium/MissingNumber.java:44
Methodmove
Player {player} makes a move at ({row}, {col}). @param row The row of the board. @param col The column of the board. @param player The player,
src/main/java/com/freetymekiyan/algorithms/level/medium/DesignTicTacToe.java:85
Methodmove
(String direction)
src/main/java/com/freetymekiyan/algorithms/level/medium/DesignSnakeGame.java:90
Methodmultiply
Math. String. How to do multiplication? Start from right to left, multiply each pair of digits, and add them together. Result num1[i] num2[j] will be
src/main/java/com/freetymekiyan/algorithms/level/medium/MultiplyStrings.java:29
Methodmultiply
Matrix multiplication is row in A multiply column in B. When implement, for A[i][j], multiply a row B[j][k], 0 <= k < nB. Then add result to res[i][k]
src/main/java/com/freetymekiyan/algorithms/level/medium/SparseMatrixMultiplication.java:44
Methodmultiply2
A sparse matrix can be represented as a sequence of rows. Each row is a sequence of (column-number, value) pairs of the nonzero values in the row. Cre
src/main/java/com/freetymekiyan/algorithms/level/medium/SparseMatrixMultiplication.java:82
MethodmyDeleteDuplicates
(ListNode head)
src/main/java/com/freetymekiyan/algorithms/level/easy/RemoveDuplicatesFromSortedList.java:32
MethodmyPow
Binary Search. Math. Decompose the exponent into powers of 2, so that you can keep dividing the problem in half. N = 9 = 2^3 + 2^0 = 1001 in binary. T
src/main/java/com/freetymekiyan/algorithms/level/medium/Pow.java:21
MethodmySqrt
src/main/cpp/069_Sqrt(x).cpp:61
MethodmySqrt
src/main/cpp/069_Sqrt(x).cpp:86
MethodnegBeforePos
Two-pass O(n) For the first pass just get the count of negative numbers For the second pass, initialize negIndex as 0, posIndex as negCount If A[negIn
src/main/java/com/freetymekiyan/algorithms/other/NegBeforePos.java:32
Methodnext
@return the next smallest number */
src/main/cpp/173_Binary_Search_Tree_Iterator.cpp:56
Methodnext
()
src/main/java/com/freetymekiyan/datastructures/LinkedListBag.java:31
Methodnext
()
src/main/java/com/freetymekiyan/datastructures/ArrayStack.java:117
Methodnext
Imagine all left subtree of a node is popped out. The next will be itself. And then the next will be its right subtree. The right subtree repeats the
src/main/java/com/freetymekiyan/algorithms/level/medium/BinarySearchTreeIterator.java:49
Methodnext
()
src/main/java/com/freetymekiyan/algorithms/level/medium/ZigzagIterator.java:64
Methodnext
()
src/main/java/com/freetymekiyan/algorithms/level/medium/FlattenNestedListIterator.java:75
Methodnext
(int val)
src/main/java/com/freetymekiyan/algorithms/level/easy/MovingAverageFromDataStream.java:34
MethodnextPermutation
O(n) Time Move pointer to second last element of ascending order From that index, find the farthest element that is bigger Swap these two elements Rev
src/main/java/com/freetymekiyan/algorithms/level/medium/NextPermutation.java:32
MethodnthSuperUglyNumber
Refer to ugly number 2 Use an array to keep all indices for primes
src/main/java/com/freetymekiyan/algorithms/level/medium/SuperUglyNumber.java:27
MethodnthUglyNumber
(int n)
src/main/java/com/freetymekiyan/algorithms/level/medium/UglyNumber2.java:32
MethodnumDecodings
DP. Bottom-up. Space Optimized. O(n) Time, O(1) Space. Don't need an array since we only need previous two results. Must keep the previous two results
src/main/java/com/freetymekiyan/algorithms/level/medium/DecodeWays.java:30
MethodnumDecodings2
DP. Bottom-up. O(n) Time, O(n) Space. Suppose the length of the String is n. Recurrence relation: number of decode ways at length n related to number
src/main/java/com/freetymekiyan/algorithms/level/medium/DecodeWays.java:58
MethodnumDistinct
DP, 2d array as table, Time O(mn), Space O(mn) We keep a m n matrix and scanning through string S, while m = T.length() + 1 and n = S.length() + 1 and
src/main/java/com/freetymekiyan/algorithms/level/hard/DistinctSubsequences.java:38
MethodnumDistinctOptimal
Space optimized, 1D array, build row by row
src/main/java/com/freetymekiyan/algorithms/level/hard/DistinctSubsequences.java:57
MethodnumIslandsUnionFind2
Union Find without a separate class. Number of islands = Number of one's - Number of unions.
src/main/java/com/freetymekiyan/algorithms/level/medium/NumberOfIslands.java:254
MethodnumTrees
DP, Bottom-up approach. a BST can be destruct to root, left subtree and right subtree. if the root is fixed, every combination of unique left/right su
src/main/java/com/freetymekiyan/algorithms/level/medium/UniqueBST.java:35
MethodnumTrees2
Catalan Number
src/main/java/com/freetymekiyan/algorithms/level/medium/UniqueBST.java:50
MethodnumWays
(int n, int k)
src/main/java/com/freetymekiyan/algorithms/level/easy/PaintFence.java:38
MethodorderOfLargestPlusSign
DP. Pre-compute the 4 arms of a position in grid. Store all zero coordinates in a set to check later. For each row, count number of ones from left and
src/main/java/com/freetymekiyan/algorithms/level/medium/LargestPlusSign.java:85
MethodparseTernary
Stack. Use a stack to help with evaluation. Push possible values into stack. Whenever there is a '?', pop two values from stack. Then get the char bef
src/main/java/com/freetymekiyan/algorithms/level/medium/TernaryExpressionParser.java:72
MethodparseTernaryB
Recursive. The key is to find the correct colon to split the expression. Note that one '?' match with one ':' for a non-nested expression. So just nee
src/main/java/com/freetymekiyan/algorithms/level/medium/TernaryExpressionParser.java:98
Methodpartition
(ListNode head, int x)
others/PartitionList_86.java:23
Methodpartition
src/main/cpp/131_Palindrome_Partitioning.cpp:114
Methodpartition
Build left and right lists and concatenate
src/main/java/com/freetymekiyan/algorithms/level/medium/PartitionList.java:21
Methodpartition2
Move greater and equal value nodes to tail
src/main/java/com/freetymekiyan/algorithms/level/medium/PartitionList.java:48
MethodpathSum
(TreeNode root, int sum)
src/main/java/com/freetymekiyan/algorithms/level/medium/PathSum2.java:39
Methodpeek
()
src/main/java/com/freetymekiyan/algorithms/other/PeekIterator.java:43
Methodpermute
(int[] num)
src/main/java/com/freetymekiyan/algorithms/level/medium/Permutations.java:72
MethodpermuteHelper
src/main/cpp/047_Permutations_II.cpp:35
MethodpermuteHelper
src/main/cpp/046_Permutations.cpp:34
MethodpermuteUnique
Same idea as Permutation 1 except we skip if duplicate of current element is found in previous sequence
src/main/java/com/freetymekiyan/algorithms/level/hard/Permutations2.java:78
MethodpermuteUniqueB
Lexicography Order next permutation Find the next permutation in lexicographic order. http://en.wikipedia.org/wiki/Permutation#Generation_in_lexicogra
src/main/java/com/freetymekiyan/algorithms/level/hard/Permutations2.java:25
Methodpick
Reservoir Sampling. Keep a count for the number of target number. Replace index with (1/count) probability. How to prove that the number is equal dist
src/main/java/com/freetymekiyan/algorithms/level/medium/RandomPickIndex.java:59
MethodplusOne
(ListNode head)
src/main/java/com/freetymekiyan/algorithms/level/medium/PlusOneLinkedList.java:27
MethodplusOne
(int[] digits)
src/main/java/com/freetymekiyan/algorithms/level/easy/PlusOne.java:14
MethodplusOne2
(ListNode head)
src/main/java/com/freetymekiyan/algorithms/level/medium/PlusOneLinkedList.java:58
MethodplusOneB
(int[] digits)
src/main/java/com/freetymekiyan/algorithms/level/easy/PlusOne.java:32
Methodpop
()
src/main/java/com/freetymekiyan/algorithms/level/easy/MinStack.java:73
Methodpop
()
src/main/java/com/freetymekiyan/algorithms/level/easy/ImplementStackUsingQueues.java:45
MethodpostTweet
(int userId, int tweetId)
src/main/java/com/freetymekiyan/algorithms/level/medium/DesignTwitter.java:61
MethodpostorderTraversal
(TreeNode root)
src/main/java/com/freetymekiyan/algorithms/level/medium/BinaryTreePostorderTraversal.java:32
MethodpreorderTraversal
Iterative Use a stack Pop current top, and push right first then push left
src/main/java/com/freetymekiyan/algorithms/level/medium/BinaryTreePreorderTraversal.java:30
MethodpreorderTraversalB
Recursive
src/main/java/com/freetymekiyan/algorithms/level/medium/BinaryTreePreorderTraversal.java:47
MethodprintMatrix
(int[][] grid)
src/main/java/com/freetymekiyan/algorithms/level/medium/MinPathSum.java:40
Methodpush
(int x)
src/main/java/com/freetymekiyan/algorithms/level/easy/MinStack.java:67
Methodpush
(int x)
src/main/java/com/freetymekiyan/algorithms/level/easy/ImplementStackUsingQueues.java:37
MethodpushAllNodes
push all left children onto the stack
src/main/cpp/173_Binary_Search_Tree_Iterator.cpp:38
Methodput
insert an entry to the trie
src/main/java/com/freetymekiyan/datastructures/TrieST.java:61
Methodrank
(Key key)
src/main/java/com/freetymekiyan/datastructures/BST.java:86
Methodread
Call multiple times, storing states The difference between single time multiple times is: There can be some characters return by read4 in the buffer W
src/main/java/com/freetymekiyan/algorithms/other/ReadNMultipleRead4.java:37
Methodread
Call only 1 time Can reach n or end of file if read4 returns size smaller than 4, it means end of file the # of bytes read is the minimum of n - readB
src/main/java/com/freetymekiyan/algorithms/other/ReadNGivenRead4.java:28
Methodread
Read from the file into an intermediate buffer. Write from that buffer to the outside buffer. If that buffer is used up, read from file again. If that
src/main/java/com/freetymekiyan/algorithms/level/hard/ReadNCharactersGivenRead42.java:48
Methodread
@param buf Destination buffer @param n Maximum number of characters to read @return The number of characters read
src/main/java/com/freetymekiyan/algorithms/level/hard/ReadNCharactersGivenRead42.java:85
Methodread
Read with read4 to an intermediate buffer. Copy characters in read4 buffer to output buffer while making sure don't exceed n. Update the bytes read. I
src/main/java/com/freetymekiyan/algorithms/level/easy/ReadNCharactersGivenRead4.java:39
MethodreadBinaryWatch
(int num)
src/main/java/com/freetymekiyan/algorithms/level/easy/BinaryWatch.java:39
MethodreadBinaryWatch2
(int num)
src/main/java/com/freetymekiyan/algorithms/level/easy/BinaryWatch.java:73
MethodrearrangeString
(String str, int k)
src/main/java/com/freetymekiyan/algorithms/level/hard/RearrangeStringKDistanceApart.java:36
MethodreconstructQueue
Greedy. Assuming there's always a solution.To fit the requirements: People have smaller height should go to the tail as much as possible. People have
src/main/java/com/freetymekiyan/algorithms/level/medium/QueueReconstructionByHeight.java:42
MethodreconstructQueue2
Same idea. But comparator is faster than lambda in execution.
src/main/java/com/freetymekiyan/algorithms/level/medium/QueueReconstructionByHeight.java:55
MethodrecoverTree
O(n) space solution
src/main/cpp/099_Recover_Binary_Search_Tree.cpp:29
MethodrecoverTree
Do morris traversal to find those swapped nodes
src/main/java/com/freetymekiyan/algorithms/level/hard/RecoverBinarySearchTree.java:23
MethodrecoverTree_O1
O(1) space solution
src/main/cpp/099_Recover_Binary_Search_Tree.cpp:49
Methodrelease
Recycle or release a number.
src/main/java/com/freetymekiyan/algorithms/level/medium/DesignPhoneDirectory.java:81
MethodremoveComments
Two kinds of comments: single-line and multi-line. Create two boolean flags for them. For i from 0 to code.length() - 1: | If hasSingle && reach the e
src/main/java/com/freetymekiyan/algorithms/other/RemoveCommentsFromCode.java:31
MethodremoveDupStandard
Use count to remember current position
src/main/java/com/freetymekiyan/algorithms/level/easy/RemoveDuplicates.java:26
MethodremoveDuplicates
Two Pointers. Duplicates are allowed at most twice. It means that the number can be the same as the last number. Instead of comparing with the last nu
src/main/java/com/freetymekiyan/algorithms/level/medium/RemoveDuplicatesFromSortedArr2.java:30
MethodremoveDuplicates
Two Pointers. When there are less than 2 elements, there cannot be more than 2 duplicates. So just return length. Compare current element with second
src/main/java/com/freetymekiyan/algorithms/level/medium/RemoveDuplicatesFromSortedArray2.java:25
MethodremoveDuplicates
Two Pointers. Use the length of the distinct elements as a pointer to the next position to be filled. For each number n in nums: | If length is 0 or n
src/main/java/com/freetymekiyan/algorithms/level/easy/RemoveDuplicatesFromSortedArray.java:31
MethodremoveDuplicates2
(int[] nums)
src/main/java/com/freetymekiyan/algorithms/level/easy/RemoveDuplicatesFromSortedArray.java:41
MethodremoveEldestEntry
(Map.Entry eldest)
src/main/java/com/freetymekiyan/algorithms/level/hard/LRUCacheLinkedHashMap.java:47
MethodremoveElementsRecursive
Recursive version. Base case: if head is null, just return null. Relation: remove elements from current linked list can be divided into two parts: 1.
src/main/java/com/freetymekiyan/algorithms/level/easy/RemoveLinkedListElements.java:53
MethodremoveInvalidParentheses
BFS. Generate all possible next strings by removing one paren from current string. First add the original string to a queue and a set to start BFS. Wh
src/main/java/com/freetymekiyan/algorithms/level/hard/RemoveInvalidParentheses.java:47
MethodremoveInvalidParentheses2
DFS. https://leetcode.com/discuss/81478/easy-short-concise-and-fast-java-dfs-3-ms-solution
src/main/java/com/freetymekiyan/algorithms/level/hard/RemoveInvalidParentheses.java:110
MethodremoveNthFromEnd
src/main/cpp/019_Remove_Nth_Node_From_End_of_List.cpp:27
MethodremoveNthFromEnd
Dummy head and Runner's technique
src/main/java/com/freetymekiyan/algorithms/level/easy/RemoveNthNodeFromEnd.java:27
MethodreorderList
(ListNode head)
src/main/java/com/freetymekiyan/algorithms/level/medium/ReorderList.java:24
Methodreset
Resets the array to its original configuration and return it.
src/main/java/com/freetymekiyan/algorithms/level/medium/ShuffleAnArray.java:38
MethodrestoreIpAddresses
(String s)
src/main/java/com/freetymekiyan/algorithms/level/medium/RestoreIpAddresses.java:21
← previousnext →1,501–1,600 of 1,974, ranked by callers