Leetcode-playground

September 21, 2020 ยท View on GitHub

Leetcode ๐Ÿ‘จโ€๐Ÿ’ป

Leetcode-playground

.NET Core codecov GitHub License

Leetcode url: https://leetcode.com/nikiforovall/

#TitleSolutionBasic idea (One line)
1Two SumC#1. Hash O(n) and O(n) space.
2. Sort O(n * log n) and search with two points O(n) and O(1) space.
2AddTwoNumbersC#Traverse single-linked list and do modulo 10 sum
153SumC#Sort initial array, loop over array and pick current element, solve two-sum problem based on two pointers method for array to the right of current element with target equals to negated value of current element
26RemoveDuplicatesFromSortedArrayC#1. Two pointers, slow/fast runner approach
27RemoveElementC#Two pointers, skip element at the end and swap at the start.
35SerachInsertPositionC#1. Two pointers, skip element at the end and swap at the start.
2. Two pointers, both start at the beginning result-sub-list pointer and full-scan pointer
69SqrtXC#Bisection, lazy curr element calculation f(mid) < x < f(mid+1), use division instead of multiplication to speed up calculation
75SortColorsC#Two pointers approach, fill the rest of the array with 1
88MergeSortedArrayC#1. Two pointers solution, store pointers to latest not processed in nums1, nums2 (=p,q). Shift nums1[pos..pos+m-p]->1 when item from nums2 is written and update p,q 2. Two pointers solution, start from the end to avoid shift operation
96UniqueBinarySearchTreesC#Dynamic programming based on catalan number calculation. The goal is to fix one number as root element and calculate number of unique binary trees based on multiplied combinations of subtrees T[i]*T[i - j -1]
130SurroundedRegionsC#Traverse inner part of board and use dfs to check potential candidates for checking, commit if it is a valid group
189RotateArrayC#1. Shifting with additional O(k) memory
2. Reverse in-place based on pivot element
3. Cyclic replacement
200NumberOfIslandsC#In-place uncheck of not visited islands via DFS/BFS
222CountCompleteTreeNodesC#Usage of property of complete tree, depending on height of subtree we could determine the part of tree under construction
231PowerOfTwoC#Bit manipulation
275HIndexIIC#Binary search for first A[mid] >= N - mind, return number of elements to the right of the pointer
278FirstBadVersionC#Bisection
380InsertDeleteGetRandom1C#Maintain map(val -> ind in array) + list, removal is based on swap of last element
414ThirdMaximumNumberC#Three consecutive counters, stored element shift
485ValidateIPAddressC#Validation pipeline with the list of heuristics
485MaxConsecutiveOnesC#Sliding window for non-zero part of array and current sum calculation as index diff
518CoinChange2C#Classic DP problem, cache total combinations of previous subproblems in a list
547FriendCirclesC#Checked DFS nodes
904FruitIntoBasketsC#Sliding window, a bunch of pointers to store consecutive sequence of previous latest element
977SquaresOfASortedArrayC#1. Merge sort, array with negative numbers is traversed in a reversed manner
2. Two pointers
1004MaxConsecutiveOnesIIIC#Sliding window, remove elements from the tail
1004TwoCitySchedulingC#Greedy solution based on costs difference heuristic + sorting
1114DuplicateZerosC#Implement array shift starting from the end for each zero element, don't forget to skip updated zero
1114FirstInOrderC#Event-based blocking ManualResetEventSlim
1115PrintFooBarAlternatelyC#Event-based blocking ManualResetEventSlim
1295FindNumbersWithEvenNumberOfDigitsC#Reduce arr item to double digit by dividing by 100 and count

Other Leetcode Repos

  1. https://github.com/tangweikun/awesome-leetcode
  2. haoel's leetcode
  3. soulmachine's leetcode
  4. gouthampradhan's leetcode
  5. qiyuangong's leetcode