Skip to content

[EGON] Week 03 Solutions #377

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
46 changes: 28 additions & 18 deletions coin-change/EGON.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
from typing import List
from unittest import TestCase, main
from collections import defaultdict


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

# Unbounded Knapsack Problem
def solve_with_dp(self, coins: List[int], amount: int) -> int:
"""
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

Expand All @@ -17,22 +28,21 @@ def solve_with_dp(self, coins: List[int], amount: int) -> int:
if amount < coins[0]:
return -1

dp = [[0] * (amount + 1) for _ in range(len(coins) + 1)]
for curr_r in range(1, len(coins) + 1):
coin_index = curr_r - 1
curr_coin = coins[coin_index]
if amount < curr_coin:
continue
dp = [float('inf')] * (amount + 1)

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

dp[curr_r][curr_coin] += 1
for curr_amount in range(curr_coin + 1, amount + 1):
for coin in coins:
if 0 < dp[curr_r][curr_amount - coin]:
dp[curr_r][curr_amount] = max(dp[curr_r - 1][curr_amount], dp[curr_r][curr_amount - coin] + 1)
else:
dp[curr_r][curr_amount] = dp[curr_r - 1][curr_amount]
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][-1] if 0 < dp[-1][-1] else -1
return dp[-1] if dp[-1] != float('inf') else -1


class _LeetCodeTestCases(TestCase):
Expand All @@ -57,7 +67,7 @@ def test_3(self):
def test_4(self):
coins = [1, 2147483647]
amount = 2
output = -1
output = 2
self.assertEqual(Solution.coinChange(Solution(), coins, amount), output)


Expand Down
13 changes: 9 additions & 4 deletions combination-sum/EGON.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@

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

"""
Runtime: 2039 ms (Beats 5.01%)
Time Complexity: ?

Memory: 16.81 MB (Beats 11.09%)
Space Complexity: ?
"""
def solve_with_dfs(self, candidates: List[int], target: int) -> List[List[int]]:
def solveWithDFS(self, candidates: List[int], target: int) -> List[List[int]]:
result = []
stack = []
visited = defaultdict(bool)
Expand All @@ -38,6 +36,13 @@ def solve_with_dfs(self, candidates: List[int], target: int) -> List[List[int]]:

return result

"""
Runtime: ?

Memory: ?
"""
def solveWithBackTracking(self, candidates: List[int], target: int) -> List[List[int]]:
return []

class _LeetCodeTestCases(TestCase):
def test_1(self):
Expand Down
5 changes: 3 additions & 2 deletions two-sum/EGON.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ def twoSum(self, nums: List[int], target: int) -> List[int]:
"""
Runtime: 3762 ms (Beats 5.00%)
Time Complexity: O(n ** 2)
- 크기가 n인 nums 배열을 2중으로 조회하므로 O(n ** 2)
> 크기가 n인 nums 배열을 2중으로 조회하므로 O(n ** 2)

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