Skip to content

Commit 972e589

Browse files
author
changmuk.im
committed
SAVE
1 parent 816ec49 commit 972e589

File tree

5 files changed

+305
-0
lines changed

5 files changed

+305
-0
lines changed

climbing-stairs/EGON.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from unittest import TestCase, main
2+
3+
4+
class Solution:
5+
def climbStairs(self, n: int) -> int:
6+
return self.solveWithDP(n)
7+
8+
"""
9+
Runtime: 30 ms (Beats 83.62%)
10+
Time Complexity: O(n)
11+
> 3에서 n + 1 까지 range를 조회하였으므로 O((n + 1) - 3) ~= O(n)
12+
13+
Memory: 16.39 MB (Beats 90.15%)
14+
Space Complexity: O(n)
15+
> 크기가 n + 1인 dp를 선언하여 사용했으므로 O(n + 1) ~= O(n)
16+
"""
17+
def solveWithDP(self, n: int) -> int:
18+
if n <= 2:
19+
return n
20+
21+
dp = [0] * (n + 1)
22+
dp[0], dp[1], dp[2] = 0, 1, 2
23+
for stair in range(3, n + 1):
24+
dp[stair] = dp[stair - 1] + dp[stair - 2]
25+
26+
return dp[n]
27+
28+
29+
class _LeetCodeTestCases(TestCase):
30+
def test_1(self):
31+
n = 2
32+
output = 2
33+
self.assertEqual(Solution.climbStairs(Solution(), n), output)
34+
35+
def test_2(self):
36+
n = 3
37+
output = 3
38+
self.assertEqual(Solution.climbStairs(Solution(), n), output)
39+
40+
def test_3(self):
41+
n = 1
42+
output = 1
43+
self.assertEqual(Solution.climbStairs(Solution(), n), output)
44+
45+
46+
if __name__ == '__main__':
47+
main()

coin-change/EGON.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
from typing import List
2+
from unittest import TestCase, main
3+
from collections import defaultdict
4+
5+
6+
class Solution:
7+
def coinChange(self, coins: List[int], amount: int) -> int:
8+
return self.solve_with_dfs(coins, amount)
9+
10+
"""
11+
Runtime: 2039 ms (Beats 5.01%)
12+
Time Complexity: ?
13+
14+
Memory: 16.81 MB (Beats 11.09%)
15+
Space Complexity: ?
16+
"""
17+
18+
# TIME LIMIT
19+
def solve_with_dfs(self, coins: List[int], amount: int) -> int:
20+
if amount == 0:
21+
return 0
22+
23+
result = float('INF')
24+
stack = []
25+
for coin in coins:
26+
stack.append([[coin], coin])
27+
while stack:
28+
curr_coins, curr_amount = stack.pop()
29+
30+
if amount < curr_amount:
31+
continue
32+
33+
if curr_amount == amount:
34+
result = min(result, len(curr_coins))
35+
36+
for coin in coins:
37+
stack.append([curr_coins + [coin], curr_amount + coin])
38+
39+
return -1 if result == float('INF') else result
40+
41+
42+
class _LeetCodeTestCases(TestCase):
43+
def test_1(self):
44+
coins = [1, 2, 5]
45+
amount = 11
46+
output = 3
47+
self.assertEqual(Solution.coinChange(Solution(), coins, amount), output)
48+
49+
def test_2(self):
50+
coins = [2]
51+
amount = 3
52+
output = -1
53+
self.assertEqual(Solution.coinChange(Solution(), coins, amount), output)
54+
55+
def test_3(self):
56+
coins = [1]
57+
amount = 0
58+
output = 0
59+
self.assertEqual(Solution.coinChange(Solution(), coins, amount), output)
60+
61+
62+
if __name__ == '__main__':
63+
main()

combination-sum/EGON.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
from typing import List
2+
from unittest import TestCase, main
3+
from collections import defaultdict
4+
5+
6+
class Solution:
7+
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
8+
return self.solve_with_dfs(candidates, target)
9+
10+
"""
11+
Runtime: 2039 ms (Beats 5.01%)
12+
Time Complexity: ?
13+
14+
Memory: 16.81 MB (Beats 11.09%)
15+
Space Complexity: ?
16+
"""
17+
def solve_with_dfs(self, candidates: List[int], target: int) -> List[List[int]]:
18+
result = []
19+
stack = []
20+
visited = defaultdict(bool)
21+
for candidate in candidates:
22+
stack.append([[candidate], candidate])
23+
24+
while stack:
25+
curr_combination, curr_sum = stack.pop()
26+
curr_visited_checker = tuple(sorted(curr_combination))
27+
28+
if curr_sum == target and visited[curr_visited_checker] is False:
29+
visited[curr_visited_checker] = True
30+
result.append(curr_combination)
31+
32+
if target < curr_sum:
33+
continue
34+
35+
for candidate in candidates:
36+
post_combination, post_sum = curr_combination + [candidate], curr_sum + candidate
37+
stack.append([post_combination, post_sum])
38+
39+
return result
40+
41+
42+
class _LeetCodeTestCases(TestCase):
43+
def test_1(self):
44+
candidates = [2, 3, 6, 7]
45+
target = 7
46+
output = [[2, 2, 3], [7]]
47+
self.assertEqual(Solution.combinationSum(Solution(), candidates, target), output)
48+
49+
def test_2(self):
50+
candidates = [2, 3, 5]
51+
target = 8
52+
output = [[2, 2, 2, 2], [2, 3, 3], [3, 5]]
53+
self.assertEqual(Solution.combinationSum(Solution(), candidates, target), output)
54+
55+
def test_3(self):
56+
candidates = [2]
57+
target = 1
58+
output = []
59+
self.assertEqual(Solution.combinationSum(Solution(), candidates, target), output)
60+
61+
62+
if __name__ == '__main__':
63+
main()
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from typing import List
2+
from unittest import TestCase, main
3+
4+
5+
class Solution:
6+
def productExceptSelf(self, nums: List[int]) -> List[int]:
7+
return self.solve_with_no_extra_space(nums)
8+
9+
"""
10+
Runtime: 290 ms (Beats 12.23%)
11+
Time Complexity: O(n)
12+
- 1부터 n까지 순회했으므로 O(n - 1)
13+
- 1부터 n + 1 까지 순회했으므로 O(n)
14+
> O(n - 1) + O(n) ~= O(2n) ~= O(n)
15+
16+
Memory: 25.68 MB (Beats 83.82%)
17+
Space Complexity: O(n)
18+
> 크기가 n + 2인 배열 2개를 사용했으므로 O(2 * (n + 2)) ~= O(n)
19+
"""
20+
def solve_with_prefix_and_suffix(self, nums: List[int]) -> List[int]:
21+
forward_product = nums[:]
22+
reverse_product = nums[:]
23+
for i in range(1, len(nums)):
24+
forward_product[i] *= forward_product[i - 1]
25+
reverse_product[len(nums) - i - 1] *= reverse_product[len(nums) - i]
26+
forward_product = [1] + forward_product + [1]
27+
reverse_product = [1] + reverse_product + [1]
28+
29+
return [forward_product[i - 1] * reverse_product[i + 1] for i in range(1, len(nums) + 1)]
30+
31+
32+
class _LeetCodeTestCases(TestCase):
33+
def test_1(self):
34+
nums = [1, 2, 3, 4]
35+
output = [24, 12, 8, 6]
36+
self.assertEqual(Solution.productExceptSelf(Solution(), nums), output)
37+
38+
def test_2(self):
39+
nums = [-1, 1, 0, -3, 3]
40+
output = [0, 0, 9, 0, 0]
41+
self.assertEqual(Solution.productExceptSelf(Solution(), nums), output)
42+
43+
44+
if __name__ == '__main__':
45+
main()

two-sum/EGON.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
from typing import List
2+
from unittest import TestCase, main
3+
from collections import defaultdict
4+
5+
6+
class Solution:
7+
def twoSum(self, nums: List[int], target: int) -> List[int]:
8+
return self.solveWithMemoization(nums, target)
9+
10+
"""
11+
Runtime: 3762 ms (Beats 5.00%)
12+
Time Complexity: O(n ** 2)
13+
- 크기가 n인 nums 배열을 2중으로 조회하므로 O(n ** 2)
14+
15+
Memory: 17.42 MB (Beats 61.58%)
16+
Space Complexity: ?
17+
"""
18+
def solveWithBruteForce(self, nums: List[int], target: int) -> List[int]:
19+
for i in range(len(nums)):
20+
for j in range(len(nums)):
21+
if i != j and nums[i] + nums[j] == target:
22+
return [i, j]
23+
24+
"""
25+
Runtime: 52 ms (Beats 89.73%)
26+
Time Complexity: O(n)
27+
1. nums 배열을 돌며 idx를 저장하는 dict 생성에 O(n)
28+
2. 첫 숫자를 선택하기 위해 len(nums)를 for문으로 조회하는데 O(n)
29+
> O(2n) ~= O(n)
30+
31+
Memory: 19.96 MB (Beats 8.42%)
32+
Space Complexity: O(n)
33+
- 크기가 n인 defaultdict 변수 사용
34+
"""
35+
def solveWithMemoization(self, nums: List[int], target: int) -> List[int]:
36+
num_to_idx_dict = defaultdict(list)
37+
for idx, num in enumerate(nums):
38+
num_to_idx_dict[num].append(idx)
39+
40+
for i in range(len(nums)):
41+
first_num = nums[i]
42+
second_num = target - nums[i]
43+
44+
if first_num != second_num:
45+
if not (len(num_to_idx_dict[first_num]) and len(num_to_idx_dict[second_num])):
46+
continue
47+
else:
48+
if not (2 <= len(num_to_idx_dict[first_num])):
49+
continue
50+
51+
first_idx = num_to_idx_dict[first_num].pop()
52+
second_idx = num_to_idx_dict[second_num].pop()
53+
54+
if first_num != second_num:
55+
return [first_idx, second_idx]
56+
else:
57+
return [second_idx, first_idx]
58+
59+
60+
class _LeetCodeTestCases(TestCase):
61+
def test_1(self):
62+
nums = [2, 7, 11, 15]
63+
target = 9
64+
output = [0, 1]
65+
self.assertEqual(Solution.twoSum(Solution(), nums, target), output)
66+
67+
def test_2(self):
68+
nums = [3,2,4]
69+
target = 6
70+
output = [1, 2]
71+
self.assertEqual(Solution.twoSum(Solution(), nums, target), output)
72+
73+
def test_3(self):
74+
nums = [3, 3]
75+
target = 6
76+
output = [0, 1]
77+
self.assertEqual(Solution.twoSum(Solution(), nums, target), output)
78+
79+
def test_4(self):
80+
nums = [3, 2, 3]
81+
target = 6
82+
output = [0, 2]
83+
self.assertEqual(Solution.twoSum(Solution(), nums, target), output)
84+
85+
86+
if __name__ == '__main__':
87+
main()

0 commit comments

Comments
 (0)