Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions climbing-stairs/EGON.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from unittest import TestCase, main


class Solution:
def climbStairs(self, n: int) -> int:
return self.solveWithDP(n)

"""
Runtime: 30 ms (Beats 83.62%)
Time Complexity: O(n)
> 3에서 n + 1 까지 range를 조회하였으므로 O((n + 1) - 3) ~= O(n)

Memory: 16.39 MB (Beats 90.15%)
Space Complexity: O(n)
> 크기가 n + 1인 dp를 선언하여 사용했으므로 O(n + 1) ~= O(n)
"""
def solveWithDP(self, n: int) -> int:
if n <= 2:
return n

dp = [0] * (n + 1)
dp[0], dp[1], dp[2] = 0, 1, 2
for stair in range(3, n + 1):
dp[stair] = dp[stair - 1] + dp[stair - 2]

return dp[n]


class _LeetCodeTestCases(TestCase):
def test_1(self):
n = 2
output = 2
self.assertEqual(Solution.climbStairs(Solution(), n), output)

def test_2(self):
n = 3
output = 3
self.assertEqual(Solution.climbStairs(Solution(), n), output)

def test_3(self):
n = 1
output = 1
self.assertEqual(Solution.climbStairs(Solution(), n), output)


if __name__ == '__main__':
main()
75 changes: 75 additions & 0 deletions coin-change/EGON.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
from typing import List
from unittest import TestCase, main


class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
return self.solveWithDP(coins, amount)

# Unbounded Knapsack Problem
"""
Runtime: 801 ms (Beats 48.54%)
Time Complexity: O(n)
- coins 길이를 c, amount의 크기를 a라고 하면
- coins를 정렬하는데 O(log c)
- dp 배열 조회에 O((n + 1) * c)
> c의 최대 크기는 12라서 무시가능하므로 O((n + 1) * c) ~= O(n * c) ~= O(n)

Memory: 16.94 MB (Beats 50.74%)
Space Complexity: O(n)
> 크기가 n + 1인 dp를 선언하여 사용했으므로 O(n + 1) ~= O(n)
"""
def solveWithDP(self, coins: List[int], amount: int) -> int:
if amount == 0:
return 0

coins.sort()

if amount < coins[0]:
return -1

dp = [float('inf')] * (amount + 1)

for coin in coins:
if coin <= amount:
dp[coin] = 1

for curr_amount in range(amount + 1):
for coin in coins:
if 0 <= curr_amount - coin:
dp[curr_amount] = min(
dp[curr_amount],
dp[curr_amount - coin] + 1
)

return dp[-1] if dp[-1] != float('inf') else -1


class _LeetCodeTestCases(TestCase):
def test_1(self):
coins = [1, 2, 5]
amount = 11
output = 3
self.assertEqual(Solution.coinChange(Solution(), coins, amount), output)

def test_2(self):
coins = [2]
amount = 3
output = -1
self.assertEqual(Solution.coinChange(Solution(), coins, amount), output)

def test_3(self):
coins = [1]
amount = 0
output = 0
self.assertEqual(Solution.coinChange(Solution(), coins, amount), output)

def test_4(self):
coins = [1, 2147483647]
amount = 2
output = 2
self.assertEqual(Solution.coinChange(Solution(), coins, amount), output)


if __name__ == '__main__':
main()
103 changes: 103 additions & 0 deletions combination-sum/EGON.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
from typing import List
from unittest import TestCase, main
from collections import defaultdict


class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
return self.solveWithBackTracking(candidates, target)

"""
Runtime: 2039 ms (Beats 5.01%)
Time Complexity: O(c * c * log c)
- 처음 stack의 크기는 c에 비례 O(c)
- 중복 제거에 사용하는 변수인 curr_visited_checker 생성에 O(c' log c')
- stack의 내부 로직에서 c에 비례한 for문을 순회하는데 O(c)
> O(c) * O(c' log c') + O(c) * O(c) ~= O(c * c * log c)

Memory: 16.81 MB (Beats 11.09%)
Space Complexity: O(c * c)
- curr_combination의 크기가 c에 비례
- stack의 크기는 curr_combination의 크기와 c에 비례
> O(c * c)
"""
def solveWithDFS(self, candidates: List[int], target: int) -> List[List[int]]:
result = []
stack = []
visited = defaultdict(bool)
for candidate in candidates:
stack.append([[candidate], candidate])

while stack:
curr_combination, curr_sum = stack.pop()
curr_visited_checker = tuple(sorted(curr_combination))

if curr_sum == target and visited[curr_visited_checker] is False:
visited[curr_visited_checker] = True
result.append(curr_combination)

if target < curr_sum:
continue

for candidate in candidates:
post_combination, post_sum = curr_combination + [candidate], curr_sum + candidate
stack.append([post_combination, post_sum])

return result

"""
Runtime: 58 ms (Beats 32.30%)
Time Complexity: O(c * c)
- candidates 정렬에 O(log c)
- 첫 depte에서 dfs 함수 호출에 O(c)
- 그 후 candidates의 길이에 비례해서 재귀적으로 dfs를 호출하는데 O(c)
- lower_bound_idx에 따라 range가 감소하기는 하나 일단은 비례 O(c')
> O(log c) + O(c * c') ~= O(c * c), 단 c' <= c 이므로 이 복잡도는 upper bound
Memory: 16.59 MB (Beats 75.00%)
Space Complexity: O(c)
- result를 제외하고 모두 nonlocal 변수를 call by reference로 참조
- dfs 함수 호출마다 메모리가 증가하는데, 호출횟수는 candidates의 길이에 비례 O(c)
- lower_bound_idx에 따라 range가 감소하기는 하나 일단은 비례
> O(c), 단 이 복잡도는 upper bound
"""
def solveWithBackTracking(self, candidates: List[int], target: int) -> List[List[int]]:
def dfs(stack: List[int], sum: int, lower_bound_idx: int):
nonlocal result, candidates, target

if target < sum:
return
elif sum < target:
for idx in range(lower_bound_idx, len(candidates)):
dfs(stack + [candidates[idx]], sum + candidates[idx], idx)
else: # target == sum
result.append(stack)
return

result = []
candidates.sort()
dfs([], 0, 0)
return result


class _LeetCodeTestCases(TestCase):
def test_1(self):
candidates = [2, 3, 6, 7]
target = 7
output = [[2, 2, 3], [7]]
self.assertEqual(Solution.combinationSum(Solution(), candidates, target), output)

def test_2(self):
candidates = [2, 3, 5]
target = 8
output = [[2, 2, 2, 2], [2, 3, 3], [3, 5]]
self.assertEqual(Solution.combinationSum(Solution(), candidates, target), output)

def test_3(self):
candidates = [2]
target = 1
output = []
self.assertEqual(Solution.combinationSum(Solution(), candidates, target), output)


if __name__ == '__main__':
main()
45 changes: 45 additions & 0 deletions product-of-array-except-self/EGON.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from typing import List
from unittest import TestCase, main


class Solution:
def productExceptSelf(self, nums: List[int]) -> List[int]:
return self.solve_with_no_extra_space(nums)

"""
Runtime: 290 ms (Beats 12.23%)
Time Complexity: O(n)
- 1부터 n까지 순회했으므로 O(n - 1)
- 1부터 n + 1 까지 순회했으므로 O(n)
> O(n - 1) + O(n) ~= O(2n) ~= O(n)

Memory: 25.68 MB (Beats 83.82%)
Space Complexity: O(n)
> 크기가 n + 2인 배열 2개를 사용했으므로 O(2 * (n + 2)) ~= O(n)
"""
def solve_with_prefix_and_suffix(self, nums: List[int]) -> List[int]:
forward_product = nums[:]
reverse_product = nums[:]
for i in range(1, len(nums)):
forward_product[i] *= forward_product[i - 1]
reverse_product[len(nums) - i - 1] *= reverse_product[len(nums) - i]
forward_product = [1] + forward_product + [1]
reverse_product = [1] + reverse_product + [1]

return [forward_product[i - 1] * reverse_product[i + 1] for i in range(1, len(nums) + 1)]


class _LeetCodeTestCases(TestCase):
def test_1(self):
nums = [1, 2, 3, 4]
output = [24, 12, 8, 6]
self.assertEqual(Solution.productExceptSelf(Solution(), nums), output)

def test_2(self):
nums = [-1, 1, 0, -3, 3]
output = [0, 0, 9, 0, 0]
self.assertEqual(Solution.productExceptSelf(Solution(), nums), output)


if __name__ == '__main__':
main()
88 changes: 88 additions & 0 deletions two-sum/EGON.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
from typing import List
from unittest import TestCase, main
from collections import defaultdict


class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
return self.solveWithMemoization(nums, target)

"""
Runtime: 3762 ms (Beats 5.00%)
Time Complexity: O(n ** 2)
> 크기가 n인 nums 배열을 2중으로 조회하므로 O(n ** 2)

Memory: 17.42 MB (Beats 61.58%)
Space Complexity: O(1)
> 딱히 저장하는 변수 없음 (반환하는 list 제외)
"""
def solveWithBruteForce(self, nums: List[int], target: int) -> List[int]:
for i in range(len(nums)):
for j in range(len(nums)):
if i != j and nums[i] + nums[j] == target:
return [i, j]

"""
Runtime: 52 ms (Beats 89.73%)
Time Complexity: O(n)
1. nums 배열을 돌며 idx를 저장하는 dict 생성에 O(n)
2. 첫 숫자를 선택하기 위해 len(nums)를 for문으로 조회하는데 O(n)
> O(2n) ~= O(n)

Memory: 19.96 MB (Beats 8.42%)
Space Complexity: O(n)
- 크기가 n인 defaultdict 변수 사용
"""
def solveWithMemoization(self, nums: List[int], target: int) -> List[int]:
num_to_idx_dict = defaultdict(list)
for idx, num in enumerate(nums):
num_to_idx_dict[num].append(idx)

for i in range(len(nums)):
first_num = nums[i]
second_num = target - nums[i]

if first_num != second_num:
if not (len(num_to_idx_dict[first_num]) and len(num_to_idx_dict[second_num])):
continue
else:
if not (2 <= len(num_to_idx_dict[first_num])):
continue

first_idx = num_to_idx_dict[first_num].pop()
second_idx = num_to_idx_dict[second_num].pop()

if first_num != second_num:
return [first_idx, second_idx]
else:
return [second_idx, first_idx]


class _LeetCodeTestCases(TestCase):
def test_1(self):
nums = [2, 7, 11, 15]
target = 9
output = [0, 1]
self.assertEqual(Solution.twoSum(Solution(), nums, target), output)

def test_2(self):
nums = [3,2,4]
target = 6
output = [1, 2]
self.assertEqual(Solution.twoSum(Solution(), nums, target), output)

def test_3(self):
nums = [3, 3]
target = 6
output = [0, 1]
self.assertEqual(Solution.twoSum(Solution(), nums, target), output)

def test_4(self):
nums = [3, 2, 3]
target = 6
output = [0, 2]
self.assertEqual(Solution.twoSum(Solution(), nums, target), output)


if __name__ == '__main__':
main()