Skip to content
Open
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
27 changes: 27 additions & 0 deletions Exercise_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Time Complexity : O(amount * n), where n is the number of coin types.
# Space Complexity : O(amount)
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : I had to initialize unreachable amounts clearly so they do not look like valid answers.
# Approach: I used a one-dimensional DP array where dp[i] stores the fewest coins needed for amount i.
# For each amount, I tried every coin that can fit and used the best previously solved amount.
# If the final amount is still unreachable, I returned -1.


class Solution:

def coinChange(self, coins, amount):
dp = [amount + 1] * (amount + 1)
dp[0] = 0

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

if dp[amount] == amount + 1:
return -1

return dp[amount]
21 changes: 21 additions & 0 deletions Exercise_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Time Complexity : O(n)
# Space Complexity : O(1)
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : The main decision was keeping only the previous two states instead of a full DP array.
# Approach: At each house, I choose between robbing it with the best value from two houses back or skipping it.
# Two variables are enough because the current answer only depends on the previous two answers.
# After processing all houses, the latest value is the maximum money that can be robbed.


class Solution:

def rob(self, nums):
two_back = 0
one_back = 0

for money in nums:
current = max(one_back, two_back + money)
two_back = one_back
one_back = current

return one_back