Skip to content

Commit 2805b40

Browse files
Chris WuChris Wu
Chris Wu
authored and
Chris Wu
committed
no message
1 parent 2180589 commit 2805b40

File tree

3 files changed

+92
-3
lines changed

3 files changed

+92
-3
lines changed

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,11 @@ These are the interview resources I personally used and only if it is really hel
165165
<https://youtu.be/YJZCUhxNCv8>
166166

167167
3. CS DoJo on "How I Got a Job at Google as a Software Engineer". There are also lots of technique on coding interview in his channel.
168-
<https://www.youtube.com/watch?v=UPO-9iMjBpc>
168+
<https://www.youtube.com/watch?v=UPO-9iMjBpc>
169+
170+
4. The #1 Daily Habit of Those Who Dominate with Andy Frisella (Also on Spotify or Youtube, just google it.)
171+
<https://podcasts.apple.com/tw/podcast/the-mfceo-project/id1012570406?i=1000412624447>
172+
169173

170174
## Prepare in a Structural Way
171175
1. <https://www.quora.com/How-should-I-prepare-for-my-Google-interview-if-I-have-1-month-left-and-I%E2%80%99m-applying-for-a-software-engineer-role/answer/Anthony-D-Mays?ch=10&share=5c488000&srid=W0jqp>
@@ -182,8 +186,8 @@ Basic data structure and algorithm online course taught in Python. This course i
182186
<https://classroom.udacity.com/courses/ud513>
183187

184188
## Interview Question Survey
185-
<https://www.glassdoor.com/index.htm>
186-
<https://www.careercup.com/>
189+
1. <https://www.glassdoor.com/index.htm>
190+
2. <https://www.careercup.com/>
187191

188192

189193
# Interview Knowledge Base Quesion
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""
2+
I put all the posible answer in the `ans`.
3+
For every digit in the input, we got whole new sets of answers, which is generated from the previous input.
4+
"""
5+
class Solution(object):
6+
def letterCombinations(self, digits):
7+
def helper(A, digit):
8+
if not A: return memo[digit]
9+
10+
opt = []
11+
for letter in memo[digit]:
12+
for string in A:
13+
opt.append(string+letter)
14+
return opt
15+
16+
ans = []
17+
memo = {
18+
'2': ['a', 'b', 'c'],
19+
'3': ['d', 'e', 'f'],
20+
'4': ['g', 'h', 'i'],
21+
'5': ['j', 'k', 'l'],
22+
'6': ['m', 'n', 'o'],
23+
'7': ['p', 'q', 'r', 's'],
24+
'8': ['t', 'u', 'v'],
25+
'9': ['w', 'x', 'y', 'z']
26+
}
27+
28+
for digit in digits:
29+
ans = helper(ans, digit)
30+
return ans

problems/score-of-parentheses.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""
2+
We parse the content in the parentheses and evaluate it.
3+
If the content is empty string then the value is 1.
4+
Otherwise, the value is the value of the content multiply by 2
5+
And we use the exact the same function to evaluate the value of the content (recursion).
6+
We can know the start and the end of the parentheses (so we can extract the content) by `depth`, which is the level of parentheses.
7+
8+
Even though this looks efficient the time complexity is high. O(N^depth).
9+
You can think of a case like this
10+
```
11+
(((((((((( ... content ... ))))))))))
12+
```
13+
Where in every level you have to go through the whole thing again.
14+
15+
The Space complexity is O(depth).
16+
Even we only use O(1) in each function, but the recursion takes stack memory of O(depth).
17+
"""
18+
class Solution(object):
19+
def scoreOfParentheses(self, S):
20+
depth = 0
21+
start = 0
22+
score = 0
23+
for i, s in enumerate(S):
24+
if s=='(': depth+=1
25+
if s==')': depth-=1
26+
if depth==0:
27+
content = S[start+1:i]
28+
if content == '':
29+
score+=1
30+
else:
31+
score+=self.scoreOfParentheses(content)*2
32+
start = i+1
33+
return score
34+
35+
"""
36+
If we take a closer look, we will notice that `()` are the only structure that provides value, the outer parentheses just add some multiplier.
37+
So we only need to be concerned with `depth`.
38+
For level we multiply the inner content by 2, so for each `()`, its value is `1 * 2**depth`
39+
40+
The time complexity is O(N).
41+
The space complexity is O(1).
42+
"""
43+
class Solution(object):
44+
def scoreOfParentheses(self, S):
45+
score = 0
46+
depth = 0
47+
48+
for i, s in enumerate(S):
49+
if s=='(':
50+
depth+=1
51+
else:
52+
depth-=1
53+
if S[i-1]=='(':
54+
score+=2**depth
55+
return score

0 commit comments

Comments
 (0)