interview
str[i:j]
是否為 palindrom 記錄在 memo[i, j]
迴圈最後要加的東西 沒有加進
記得迴圈用的區域變數不要跟其他變數衝突
如果 dfs(left)==False
,dfs(right)
就不會執行:
如果有些邏輯是不管怎樣都要對每個 node 執行,務必寫成這樣:
invalid number string:
leading zero: 000012
negative zero: -0
n! –> 2^n –> polynomial
n! permutation
T(n) = nT(n-1)
2^n combination
T(n) = 2T(n-1)
polynomial combination (reuse subset of combination)
O(n)
O(nlogn)
O(logn)
O(n2)
O(n) / O(1)
得到 k-th 大/小的元素
https://leetcode.com/problems/kth-largest-element-in-an-array/
L 指向 0 後的第一個 idx
M 指向第一個 ?
R 指向最後一個 ?
https://leetcode.com/problems/majority-element-ii/
https://leetcode.com/problems/linked-list-cycle-ii/
在 loop 的過程中將 arr[i] 加進 map,在 map 尋找 target - arr[i] 的 idx/count。
可搭配 prefix,將 prefix 存進 map,便能在 map 尋找某區間的和是否等於 target。
https://leetcode.com/problems/longest-well-performing-interval/
https://leetcode.com/problems/subarray-sum-equals-k/
https://leetcode.com/problems/maximum-size-subarray-sum-equals-k/
https://leetcode.com/problems/contiguous-array/
https://leetcode.com/problems/binary-subarrays-with-sum/
用 left[i] 紀錄往左邊看第一個比 arr[i] 還大/小的 idx
用 right[i] 紀錄往右邊看第一個比 arr[i] 還大/小的 idx
便能計算以 arr[i] 為最大/小值的最大 subarray,搭配 prefix sum 能進一步算該 subarray 的和。
e.g.
https://leetcode.com/problems/rotate-array/
https://leetcode.com/problems/reverse-words-in-a-string-iii/
https://leetcode.com/problems/design-most-recently-used-queue/
Use extra field to mark element as "removed".
https://leetcode.com/problems/exam-room/discuss/139941/Python-O(log-n)-time-for-both-seat()-and-leave()-with-heapq-and-dicts-Detailed-explanation
improvement (using bucket):
https://codingcompetitions.withgoogle.com/kickstart/round/0000000000436140/000000000068cb14
-> bucket O(1) popmax / insert
if the value range is limited, use each value as key, store the elements with that value in array or hashset.
https://leetcode.com/problems/cat-and-mouse/submissions/
84. Largest Rectangle in Histogram
221. Maximal Square
85. Maximal Rectangle
1727. Largest Submatrix With Rearrangements
1673. Find the Most Competitive Subsequence
1425. Constrained Subsequence Sum
239. Sliding Window Maximum
https://leetcode.com/problems/shortest-unsorted-continuous-subarray/
https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k/
https://leetcode.com/problems/next-greater-element-i/
https://leetcode.com/problems/largest-rectangle-in-histogram/
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/
TODO:
907. Sum of Subarray Minimums
901. Online Stock Span
856. Score of Parentheses
503. Next Greater Element II
496. Next Greater Element I
1334. Find the City With the Smallest Number of Neighbors at a Threshold Distance
pattern: subarray
that match some condition
template:
map::lower_bound
, map::upper_bound
map::lower_bound(k)
gives the iterator that its key that >= k
map::upper_bound(k)
gives the iterator that its key > k
root is dmy
check if next node exist
if not exist: return
else: check if "next" node is endNode and add to ans
421. Maximum XOR of Two Numbers in an Array
1707. Maximum XOR With an Element From Array
Trie search with limitation
11. Container With Most Water
42. Trapping Rain Water
算裝水的問題可以用stack或two pointer來做
用stack的缺點是你不知道從左到右的路上
遇到小水坑你只能蒐集這個水坑裡的水(x的部分)
但水坑上方的的空間你不確定有沒有水(?的部分)
儘管左邊有更高的山(1)
但你不知道右邊有沒有高山可以困住(?)的水
要直到遇到了才能蒐集(?)
two pointer則很巧妙的用這個問題greedy的性質
從兩側往中間逼近
總是移動矮的那一邊,所以你可以保證另一邊一定有比這邊更高的山
所以每次蒐集雨水時可以直接算出每個位置會淹到哪裡
713. Subarray Product Less Than K
1574. Shortest Subarray to be Removed to Make Array Sorted
Reverse
Slow-Fast pointers
https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/discuss/35476/Share-my-JAVA-solution-1ms-very-short-and-concise.
220. Contains Duplicate III
a solution for 36. Valid Sudoku
table[n-1][m] == Cn取m
1569. Number of Ways to Reorder Array to Get Same BST
gfg
1239. Maximum Length of a Concatenated String with Unique Characters
https://leetcode.com/tag/segment-tree/
Need to be 1-based
range query + node 會swap位置
1505. Minimum Possible Integer After at Most K Adjacent Swaps On Digits
只加下一個 parent
假設要建segment tree的資料有n筆
如果n是2的power,tree size = 2n-1
如果不是,tree size是2x-1,x是比n大的最小的2的power
所以一定可以涵蓋的array大小會是n<<2 (4n)
當要做range update時,可用lazy update
要另外管理一個跟tree
一樣大的array: lazy
470. Implement Rand10() Using Rand7()
316. Remove Duplicate Letters
Greedy + string
youtube
template:
28. Implement strStr()
796. Rotate String
214. Shortest Palindrome
看回文 abc#bca
5. Longest Palindromic Substring
336. Palindrome Pairs
power 的部分可以用%prime來防止overflow
1044. Longest Duplicate Substring
hash = (hash - ((S[i-m] - 'a') * (long long)power[m-1]) % prime) % prime;
hash - ((S[i-m] - 'a') * (long long)power[m-1]) % prime
變成負數,所以要再加一個prime讓他變回正數hash = (hash - ((S[i-m] - 'a') * (long long)power[m-1]) % prime + prime) % prime;
template:
- Selecting the intervals that start earliest is not an optimal solution, because if the earliest interval happens to be very long, accepting it would make us reject many other shorter requests.
- Selecting the shortest intervals or selecting intervals with the fewest conflicts is also not optimal.
435. Non-overlapping Intervals
…
56. Merge Intervals
252. Meeting Rooms
253. Meeting Rooms II
452. Minimum Number of Arrows to Burst Balloons
1605. Find Valid Matrix Given Row and Column Sums
切割子問題
dp[i] = 範圍從0到i的子問題 且選擇i
dp[i]
存1~i並且選擇i最大的解dp[n]
而是max(dp[0...n])
template:
1
638. Shopping Offers
1553. Minimum Number of Days to Eat N Oranges
1575. Count All Possible Routes
Top down
Bottom up
如果遞迴關係只固定用到前N個,例如
可以不用存整個input size的memo
只需要存N個,退化成像是prefix sum的結構。
template:
1552. Magnetic Force Between Two Balls
1482. Minimum Number of Days to Make m Bouquets
1300. Sum of Mutated Array Closest to Target valid() 難寫
…
410. Split Array Largest Sum (Hard)
1044. Longest Duplicate Substring (Hard)
668. Kth Smallest Number in Multiplication Table (Hard)
719. Find K-th Smallest Pair Distance (Hard)
33. Search in Rotated Sorted Array
多個gcd:
lcm: