For recursion, it’s too easy. 3 BT traversals using stack(iterative way to solve it).
- 就記得pre-order和in-order用幾乎百分之99一樣的模板, 差別是一個 root node first另外一個是left order first
- post-order最tricky, 就記得我們最後會reverse postorder, 所以root node就直接給他放進去postorder.
Code:
For recursion, it’s too easy. 3 BT traversals using stack(iterative way to solve it).
Code:
Usually, when it comes to combination problems, the most naive way to solve the problem is generate all possible solutions. And, we use depth-first-search(DFS) or called backtracking technique.
For backtracking, when we do time and space complexity analysis, they follow a certain pattern:
T: O(n**m), n raise to power of m, where n represents at most we have n branches can search for current state and m is maximum depth of recursion tree.
Note: O(n**m) will be upper bound of time complexity. For some cases, if we generate all possible combinations or partitions , given an integer or a string, running time will be O(2**n), lower than upper bound.
S:O(m), where m is maximum depth of recursion tree. (if we don’t consider space of storing answer)
Given 一個string expression, 我們要去計算result of expression.
有三個題型:
最基本的計算機題型
1.是string裡面只有+-(), 沒有乘除. (See LC 224)
2.再來是有+-*/但沒有() (See LC 227)
3.最難版本是+-*/和()都有(See LC 772)
Note:
Related Problems in LC:
224. Basic Calculator
227. Basic Calculator II
772. Basic Calculator III
2021–2022刷題重點:
一道题多做
重溫經典系列:
Why do you need to be familiar with evaluation metric?
Some pattern about continuous subarray related problem. Usually, naive solution is generate all possible subarray, which takes O(n²) times at least. And optimal solution is the one with special data structure such as prefix sum, monotone stack, in most of cases. And, this is tricky. So, I try to summarize what I learned in this post.
Note
1.包含當前element cur, 有多少個subarray的公式?
2.做subarray要注意的edge case: