Skip to content

Commit 03bcf12

Browse files
committed
4.5 done
1 parent fc364be commit 03bcf12

File tree

10 files changed

+295
-1
lines changed

10 files changed

+295
-1
lines changed

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,8 @@
4343
- [3.5 순환 검출](LinkedList/3_5_cycle_detect/README.md)
4444
- [3.6 두 수 더하기](LinkedList/3_6_add_two_num/README.md)
4545
### 스택
46-
- [4.3 유효한 괄호 검증](Stack/4_3_valid_parentheses/README.md)
46+
- [4.3 유효한 괄호 검증](Stack/4_3_valid_parentheses/README.md)
47+
- [4.5.1 계단 오르기](Stack/4_5_1_stairs/README.md)
48+
- [4.5.2 모든 문자열 치환(permutation)](Stack/4_5_2_permutation/README.md)
49+
- [4.5.3 동전 교환](Stack/4_5_3_coin_change/README.md)
50+
- [4.5.4 배열의 두 부분집합의 최소 차이만들기](Stack/4_5_4_min_diff/README.md)

Stack/4_5_1_stairs/README.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
## 계단 오르기
2+
- Page 227
3+
### Brute-force
4+
- [stair_bf.py](stair_bf.py)
5+
6+
### LeetCode
7+
- https://leetcode.com/problems/climbing-stairs/
8+
9+
10+
11+
12+
13+

Stack/4_5_1_stairs/stair_bf.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from typing import List
2+
3+
tests = {
4+
1: 2,
5+
2: 3,
6+
3: 4,
7+
}
8+
9+
res = {
10+
1: 2,
11+
2: 3,
12+
3: 5,
13+
}
14+
15+
def check_result(index: int, output: int):
16+
if index > len(tests):
17+
raise RuntimeError(f'Failed to get {index}th case')
18+
return res.get(index, -1) == output
19+
20+
def climbStairs(n: int) -> int:
21+
def climb(n, i):
22+
if n == i:
23+
return 1
24+
if n < i:
25+
return 0
26+
27+
return climb(n, i + 1) + climb(n, i + 2)
28+
29+
return climb(n, 0)
30+
31+
def main():
32+
for index, input_val in tests.items():
33+
res = climbStairs(input_val)
34+
35+
if check_result(index, res):
36+
print(f'Test case {index} is correct: value {res}')
37+
else:
38+
print(f'Test case {index} is failed: value {res}')
39+
40+
if __name__ == '__main__':
41+
main()

Stack/4_5_2_permutation/README.md

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
## 모든 문자열 치환(permutation)
2+
- Page 229
3+
### Brute-force 1
4+
- [perm_bf.py](perm_bf1.py)
5+
- code 에 ```s_perm(s)``` 부분을 ```find_permutation(s)```으로 변경해야 합니다.
6+
### Brute-force 2
7+
- [perm_bf.py](perm_bf1.py)
8+
9+
10+
### LeetCode
11+
- https://leetcode.com/problems/permutations/
12+
### GeeksForGeeks
13+
- https://practice.geeksforgeeks.org/problems/permutations-of-a-given-string/0
14+
15+
16+
17+
18+

Stack/4_5_2_permutation/perm_bf1.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from typing import List
2+
import collections
3+
4+
tests = {
5+
1: "abc"
6+
}
7+
8+
res = {
9+
1: ["abc", "acb", "bac", "bca", "cab", "cba"]
10+
}
11+
12+
def is_same_list(alist: List, blist: List):
13+
return collections.Counter(alist) == collections.Counter(blist)
14+
15+
def check_result(index: int, output: List):
16+
if index > len(tests):
17+
raise RuntimeError(f'Failed to get {index}th case')
18+
19+
return is_same_list(output, res.get(index, []))
20+
21+
def find_permutation(s):
22+
if len(s) == 1:
23+
return list(s)
24+
25+
ans = []
26+
curr = s[0]
27+
s = s[1:]
28+
29+
words = find_permutation(s)
30+
31+
for sub in words:
32+
for i in range(len(sub) + 1):
33+
ans.append("".join([sub[:i], curr, sub[i:]]))
34+
35+
return ans
36+
37+
def main():
38+
for index, input_str in tests.items():
39+
res = find_permutation(input_str)
40+
41+
if check_result(index, res):
42+
print(f'Test case {index} is correct: value {res}')
43+
else:
44+
print(f'Test case {index} is failed: value {res}')
45+
46+
if __name__ == '__main__':
47+
main()

Stack/4_5_2_permutation/perm_bf2.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from typing import List
2+
import collections
3+
4+
tests = {
5+
1: "abc"
6+
}
7+
8+
res = {
9+
1: ["abc", "acb", "bac", "bca", "cab", "cba"]
10+
}
11+
12+
def is_same_list(alist: List, blist: List):
13+
return collections.Counter(alist) == collections.Counter(blist)
14+
15+
def check_result(index: int, output: List):
16+
if index > len(tests):
17+
raise RuntimeError(f'Failed to get {index}th case')
18+
19+
return is_same_list(output, res.get(index, []))
20+
21+
def find_permutation(res, chs, s, e):
22+
if s == e - 1:
23+
res.append("".join(chs))
24+
else:
25+
for i in range(s, e):
26+
chs[s], chs[i] = chs[i], chs[s]
27+
find_permutation(res, chs, s + 1, e)
28+
chs[s], chs[i] = chs[i], chs[s]
29+
30+
def main():
31+
for index, input_str in tests.items():
32+
res = []
33+
find_permutation(res, list(input_str), 0, len(input_str))
34+
35+
if check_result(index, res):
36+
print(f'Test case {index} is correct: value {res}')
37+
else:
38+
print(f'Test case {index} is failed: value {res}')
39+
40+
if __name__ == '__main__':
41+
main()

Stack/4_5_3_coin_change/README.md

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
## 동전 교환
2+
- Page 232
3+
### Brute-force 1
4+
- [coin_change_bf.py](coin_change_bf.py)
5+
6+
### LeetCode
7+
- https://leetcode.com/problems/coin-change/
8+
### GeeksforGeeks
9+
- https://practice.geeksforgeeks.org/problems/coin-change/0
10+
11+
12+
13+
14+
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
from typing import List
2+
import sys
3+
4+
tests = {
5+
1: ([1,2,5], 11),
6+
2: ([2], 3),
7+
3: ([1], 0),
8+
4: ([1], 1),
9+
5: ([1], 2)
10+
}
11+
12+
res = {
13+
1: 3,
14+
2: -1,
15+
3: 0,
16+
4: 1,
17+
5: 2,
18+
}
19+
20+
def check_result(index: int, output: int):
21+
if index > len(tests):
22+
raise RuntimeError(f'Failed to get {index}th case')
23+
return res.get(index, -1) == output
24+
25+
def coinChange(coins: List[int], value: int) -> int:
26+
def change(v:int):
27+
if v == 0:
28+
return 0
29+
30+
min_coin_cnt = sys.maxsize
31+
for c in coins:
32+
if (v - c) >= 0:
33+
change_cnt = change(v - c)
34+
if change_cnt < min_coin_cnt:
35+
min_coin_cnt = change_cnt
36+
37+
return min_coin_cnt + 1
38+
39+
ans = change(value)
40+
41+
return ans if ans != sys.maxsize + 1 else -1
42+
43+
def main():
44+
for index, data in tests.items():
45+
coins = data[0]
46+
value = data[1]
47+
res = coinChange(coins, value)
48+
49+
if check_result(index, res):
50+
print(f'Test case {index} is correct: value {res}')
51+
else:
52+
print(f'Test case {index} is failed: value {res}')
53+
54+
if __name__ == '__main__':
55+
main()

Stack/4_5_4_min_diff/README.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
## 배열의 두 부분집합의 최소 차이만들기
2+
- Page 234
3+
### Brute-force 1
4+
- [min_diff_bf.py](min_diff_bf.py)
5+
6+
### LeetCode
7+
- https://leetcode.com/problems/partition-equal-subset-sum/
8+
### GeeksForGeeks
9+
- https://practice.geeksforgeeks.org/problems/minimum-sum-partition/0
10+
11+
12+
13+
14+
15+

Stack/4_5_4_min_diff/min_diff_bf.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from typing import List
2+
import sys
3+
4+
tests = {
5+
1: [1,5,11,5],
6+
2: [1,2,3,5],
7+
3: [3, 2, 4, 7, 1]
8+
}
9+
10+
res = {
11+
1: 0,
12+
2: 1,
13+
3: 1
14+
}
15+
16+
def check_result(index: int, output: int):
17+
if index > len(tests):
18+
raise RuntimeError(f'Failed to get {index}th case')
19+
return res.get(index, -1) == output
20+
21+
min_diff = float('inf')
22+
total = 0
23+
24+
def subset_diff(index: int, nums: List[int], subsum: int):
25+
global total, min_diff
26+
if index == len(nums):
27+
min_diff = min(min_diff, abs(((total - subsum) - subsum)))
28+
return
29+
30+
subset_diff(index + 1, nums, subsum + nums[index])
31+
subset_diff(index + 1, nums, subsum)
32+
33+
def main():
34+
global total, min_diff
35+
for index, arr in tests.items():
36+
min_diff = float('inf')
37+
total = sum(arr)
38+
subset_diff(0, arr, 0)
39+
40+
if check_result(index, min_diff):
41+
print(f'Test case {index} is correct: value {min_diff}')
42+
else:
43+
print(f'Test case {index} is failed: value {min_diff}')
44+
45+
if __name__ == '__main__':
46+
main()

0 commit comments

Comments
 (0)