Skip to content

Commit 53c1d50

Browse files
committed
Merge branch 'master' of https://github.com/cychiu8/Leetcode
2 parents 892b1a0 + 5d99390 commit 53c1d50

File tree

7 files changed

+232
-0
lines changed

7 files changed

+232
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# 0017. Letter Combinations of a Phone Number
2+
3+
* Difficulty: medium
4+
* Link: https://leetcode.com/problems/letter-combinations-of-a-phone-number/
5+
* Topics: Backtracking
6+
7+
# Clarification
8+
9+
1. Check the inputs and outputs
10+
- INPUT: string
11+
- OUTPUT: List[String]
12+
13+
# Naive Solution
14+
15+
### Thought Process
16+
17+
- 建立數字對應的 map
18+
- Backtracking
19+
- 停止條件
20+
- len(substring) == len(inputstring)
21+
- 非停止條件
22+
- forloop
23+
- 對每個letter backtrack
24+
- Implement
25+
26+
```python
27+
class Solution:
28+
def letterCombinations(self, digits: str) -> List[str]:
29+
digit_map = {
30+
"2" : "abc",
31+
"3":"def",
32+
"4":"ghi",
33+
"5":"jkl",
34+
"6":"mno",
35+
"7":"pqrs",
36+
"8":"tuv",
37+
"9":"wxyz"
38+
}
39+
if len(digits) == 0:
40+
return []
41+
chars = list(digits)
42+
result = []
43+
def backtrack(substr, res):
44+
if len(substr) == len(digits):
45+
return result.append(substr)
46+
for i in range(len(res)):
47+
letters = list(digit_map.get(res[i]))
48+
for letter in letters:
49+
backtrack(substr+letter, res[i+1:])
50+
51+
backtrack("", chars)
52+
return result
53+
```
54+
55+
56+
### Complexity
57+
58+
- Time complexity:
59+
- Space complexity: $O(N)$
60+
- N: len(digits)
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# 0131. Palindrome Partitioning
2+
3+
* Difficulty: medium
4+
* Link: https://leetcode.com/problems/palindrome-partitioning/
5+
* Topics: Backtracking
6+
7+
# Clarification
8+
9+
1. Check the inputs and outputs
10+
- INPUT: String
11+
- OUTPUT: List[List[String]]
12+
13+
# Naive Solution
14+
15+
### Thought Process
16+
17+
- 停止條件
18+
- len(res) == 0
19+
- 非停止條件
20+
- substr = res[:i+1]
21+
- if substr is palindrome
22+
- backtrack
23+
- subset = subset + substr
24+
- res = res[i+1:]
25+
26+
![Untitled](./Untitled.png)
27+
28+
![Untitled](./Untitled%201.png)
29+
30+
- Implement
31+
32+
```python
33+
class Solution:
34+
def partition(self, s: str) -> List[List[str]]:
35+
result = []
36+
chars = list(s)
37+
def isPalindrome(substr):
38+
if len(substr) == 1:
39+
return True
40+
forward_idx = 0
41+
backward_idx = len(substr) - 1
42+
while forward_idx < backward_idx:
43+
if substr[forward_idx] != substr[backward_idx]:
44+
return False
45+
forward_idx += 1
46+
backward_idx -=1
47+
return True
48+
49+
def backtrack(subset, res):
50+
if len(res) == 0:
51+
return result.append(subset)
52+
for idx in range(len(res)):
53+
substr = "".join(res[:idx+1])
54+
if isPalindrome(substr):
55+
backtrack(subset + [substr], res[idx+1:])
56+
57+
backtrack([],chars)
58+
return result
59+
```
60+
61+
- 額外需準備的空間:string 長度的空間
62+
63+
### Complexity
64+
65+
- Time complexity: $O(N^N)$ ?!!!
66+
67+
![Untitled](./Untitled%202.png)
68+
69+
- 每個 backtrack 需要呼叫 len(res) 次 的 backtrack
70+
- N * (res - 1 次 backtrack)
71+
72+
![Untitled](./Untitled%203.png)
73+
74+
```python
75+
len of input string = 3
76+
call backtrack = 6
77+
loop time = 7
78+
79+
==== backtrack ==== 1
80+
[]
81+
['a', 'a', 'b']
82+
---- forloop -- 1 -- idx:0 ---- a
83+
==== backtrack ==== 2
84+
['a']
85+
['a', 'b']
86+
---- forloop -- 2 -- idx:0 ---- a
87+
==== backtrack ==== 3
88+
['a', 'a']
89+
['b']
90+
---- forloop -- 3 -- idx:0 ---- b
91+
==== backtrack ==== 4
92+
['a', 'a', 'b']
93+
[]
94+
---- forloop -- 4 -- idx:1 ---- ab
95+
---- forloop -- 5 -- idx:1 ---- aa
96+
==== backtrack ==== 5
97+
['aa']
98+
['b']
99+
---- forloop -- 6 -- idx:0 ---- b
100+
==== backtrack ==== 6
101+
['aa', 'b']
102+
[]
103+
---- forloop -- 7 -- idx:2 ---- aab
104+
```
105+
106+
- Space complexity: $O(N)$
107+
- N : len(input string)
108+
109+
### Problems & Improvement
110+
111+
- isPalindrome 的 function
112+
- [::-1] 順序相反操作
113+
- substr[::-1] 把整個 string 反過來
114+
115+
```python
116+
117+
def isPalindrome(substr):
118+
return substr == substr[::-1]
119+
```
174 KB
Loading
16.2 KB
Loading
260 KB
Loading
178 KB
Loading
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# 0704. Binary Search
2+
3+
* Difficulty: easy
4+
* Link: https://leetcode.com/problems/binary-search/
5+
* Topics: Binary-Search
6+
7+
# Clarification
8+
9+
1. Check the inputs and outputs
10+
- INPUT: List[int]
11+
- OUTPUT: index of list
12+
2. Check the main goal
13+
- the list is sorted
14+
- time complexity in O(log n)
15+
16+
# Naive Solution
17+
18+
### Thought Process
19+
20+
- 建立兩個指標
21+
- left pointer: 陣列開頭的位置、值的下限)
22+
- right pointer: 陣列結束的位置、值的上限
23+
1. 找中間指標對應的值
24+
- = target ⇒ return
25+
- < target ⇒ 調高下限 left pointer ++
26+
- > target ⇒ 調低上限 right pointer - -
27+
- Implement
28+
29+
```python
30+
class Solution:
31+
def search(self, nums: List[int], target: int) -> int:
32+
left = 0
33+
right = len(nums) - 1
34+
while left <= right:
35+
mid = int((left + right) / 2)
36+
if nums[mid] == target:
37+
return mid
38+
if nums[mid] < target:
39+
left += 1
40+
elif nums[mid] > target:
41+
right -= 1
42+
return -1
43+
```
44+
45+
46+
### Complexity
47+
48+
- Time complexity: O(log n)
49+
- Space complexity: O(1)
50+
51+
# Reference
52+
53+
- ****[初學者學演算法|從時間複雜度認識常見演算法](https://medium.com/appworks-school/%E5%88%9D%E5%AD%B8%E8%80%85%E5%AD%B8%E6%BC%94%E7%AE%97%E6%B3%95-%E5%BE%9E%E6%99%82%E9%96%93%E8%A4%87%E9%9B%9C%E5%BA%A6%E8%AA%8D%E8%AD%98%E5%B8%B8%E8%A6%8B%E6%BC%94%E7%AE%97%E6%B3%95-%E4%B8%80-b46fece65ba5)****

0 commit comments

Comments
 (0)