diff --git a/problem-1.py b/problem-1.py new file mode 100644 index 00000000..6f9837dd --- /dev/null +++ b/problem-1.py @@ -0,0 +1,40 @@ +# LEETCODE PROBLEM 322 COIN CHANGE +# TIME COMPLEXITY:O(N*M) where N is the number of coins and M is the amount +# SPACE COMPLEXITY: O(M) +# Any problem you faced while coding this: None + + +class Solution(object): + def coinChange(self, coins, amount): + """ + :type coins: List[int] + :type amount: int + :rtype: int + """ + #Approach 1 + # n=len(coins)+1 + # m=amount+1 + # matrix=[[0]*m for _ in range(n)] + # #(matrix) + # for i in range(1,m): + # matrix[0][i]=float('inf') + + # for i in range(1,n): + # for j in range(1,m): + # if (j>=coins[i-1]): + # #matrix[i][j]=min(matrix[i-1][j-coins[i-1]]+1,matrix[i-1][j]) + # matrix[i][j] = min(matrix[i][j-coins[i-1]] + 1, matrix[i-1][j]) + # else: + # matrix[i][j]=matrix[i-1][j] + # ans=matrix[n-1][m-1] + # return ans if ans !=float('inf') else -1 + + #Approach 2 + + m=amount+1 + matrix=[float('inf')]*m + matrix[0]=0 + for i in coins: + for j in range(i,m): + matrix[j]=min(matrix[j],matrix[j-i]+1) + return -1 if matrix[m-1]==float('inf') else matrix[m-1] \ No newline at end of file diff --git a/problem-2.py b/problem-2.py new file mode 100644 index 00000000..a8fe415d --- /dev/null +++ b/problem-2.py @@ -0,0 +1,23 @@ +# LEETCODE PROBLEM 198. HOUSE ROBBER +# TIME COMPLEXITY: O(N) where N denotes the number of elements in an array +# SPACE COMPLEXITY: O(1) +# Any problem you faced while coding this: None + + +class Solution(object): + def rob(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + n = len(nums) + if n == 1: + return nums[0] + + prev = nums[0] + current= max(nums[0], nums[1]) + for i in range(2,len(nums)): + temp=current + current=max(temp,prev+nums[i]) + prev=temp + return current \ No newline at end of file