From 36ca19ec61c308686e91d59f2bbca1599f68fead Mon Sep 17 00:00:00 2001 From: allurkarsneha Date: Sat, 8 Aug 2026 22:03:56 -0500 Subject: [PATCH] Completed leetcode 322 and 198 --- CoinChange.py | 19 +++++++++++++++++++ HouseRobber.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 CoinChange.py create mode 100644 HouseRobber.py diff --git a/CoinChange.py b/CoinChange.py new file mode 100644 index 00000000..0105d296 --- /dev/null +++ b/CoinChange.py @@ -0,0 +1,19 @@ +#Time Complexity: O(n*m) where n is the amount and m is the number of coins +#Space Complexity: O(n) where n is the amount + +class Solution(object): + def coinChange(self, coins, amount): + """ + :type coins: List[int] + :type amount: int + :rtype: int + """ + n = amount + dp = [99999] * (n + 1) + dp[0] = 0 + + for coin in coins: + for j in range(coin, n + 1): + dp[j] = min(dp[j], 1 + dp[j - coin]) + + return -1 if dp[n] == 99999 else dp[n] \ No newline at end of file diff --git a/HouseRobber.py b/HouseRobber.py new file mode 100644 index 00000000..aaa62038 --- /dev/null +++ b/HouseRobber.py @@ -0,0 +1,31 @@ +#Time Complexity: O(n) where n is the length of the input list +#Space Complexity: O(1) since we are using constant space + +class Solution(object): + def rob(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + """n = len(nums) + if n == 1: + return nums[0] + dp = [0] * n + dp[0] = nums[0] + dp[1] = max(nums[0], nums[1]) + for i in range(2, len(nums)): + dp[i] = max(dp[i - 1], dp[i - 2] + nums[i]) + + return dp[-1]""" + n = len(nums) + if n == 1: + return nums[0] + prev = nums[0] + curr = max(nums[0], nums[1]) + for i in range(2, len(nums)): + temp = curr + curr = max(curr, prev + nums[i]) + prev = temp + + return curr + \ No newline at end of file