- find pattern in half and build whole
- from top to bottom using:
dp[current] += dp[parent] + current_val
- then find nodes with no child and sum them
- topology sort
- build graph and in_degree
- start from in_degree 0
- if queue, pop left queue to ans, loop neighbour, update in_degree (-1), if down to 0, add neighbour to queue
- return ans
- Group by most frequency task
- store list with coordinates which are related to the starting point representing an Island
- add island list to set and count
- Need O(n^2)
- sort the list, loop each number i,
- set j,k to the remaining smallest and largest
- do while j < k
- if good, than we know the count for fix i and k, make j+1 (try bigger)
- if no good, than make k-1 (try smaller)
- construct 'word1_word2_word3_'
- maintain a pointer
- 3 cases: row ends with space ('_'), row ends with end of a word, row ends in middle of word
left/right = dfs(node)
left/right = [1,2,3]
: each item represents their children's path length to them- update sum with left and right information: if [1,2,3] and [4,5,6], distance = 6, then add 3 pairs (1+4, 2+4, 1+5)
- return [2,3,4]: each children path length+1 for upper level
- two pointer from leftmost and rightmost
- if not equal, sum and keep comparing unitl left meet right
- two pointer (sum 0 or 60) but mod 60 version
- keep track of left count (to represent a stack)
- if left, just add it to stack
- if right:
- but no left in stack, add one left to stack and increase count
- and left in stack, check next one (if exist):
- if still is a right, pop stack (because one left matches two rights)
- if not a right, increase one count and pop one left
- topology sort but initalize queue to supplies only (not all in_degree 0 nodes)
- use stack:
['(', 1, '(', 2]
- match left and right and push score to stack for future compute
- use stack:
['[', 'a', '[', 'c', 'd']
- use another stack to keep track of number:
[2, 4]
- expand when encounter a
]
, pop number stack as frequency:cd
and4
givescdcd
, put back to queue['[', 'a', 'c', 'd', 'c', 'd']
- creat graph using neighbour (dislikes)
- union-find:
- check self and neighbour have same parent (then invalid)
- union neighbour with neighbour (random choice, say just neighbour[0])
- bfs:
- color the graph with 0,1, use a list to keep track of the color (also served as "visited" list)
- put neighbour in queue until all nodes are colored or encounter invalid (node and neighbour have same color)'
- method 1:
dp[i,j] = (dp[i-1,j-1] - 1) / 2 + (dp[i-1, j] - 1) / 2
- method 2:
dp[i+1, j] += (dp[i,j] - 1) / 2
dp[i+1, j+1] += (dp[i,j] - 1) / 2