diff --git a/Problem20.java b/Problem20.java new file mode 100644 index 00000000..e2d2a01a --- /dev/null +++ b/Problem20.java @@ -0,0 +1,25 @@ +class Solution { + public int coinChange(int[] coins, int amount) { + int m = coins.length; + int n = amount; + + int[] dp = new int[n+1]; + + for (int j = 1; j <= n; j++) { + dp[j] = 99999; + } + + for (int i = 1; i <= m; i++) { + for (int j = 0; j <=n; j++) { + if (j < coins[i - 1]) { + dp[j] = dp[j]; + } else { + dp[j] = Math.min(dp[j], 1 + (dp[j - coins[i-1]])); + } + } + } + + if (dp[n] == 99999) return -1; + return dp[n]; + } +} \ No newline at end of file diff --git a/Problem21.java b/Problem21.java new file mode 100644 index 00000000..62c06900 --- /dev/null +++ b/Problem21.java @@ -0,0 +1,17 @@ +class Solution { + public int rob(int[] nums) { + int n = nums.length; + if (n == 1) return nums[0]; + + int prev = nums[0]; + int curr = Math.max(nums[0], nums[1]); + + for (int i = 2; i < n; i++) { + int temp = curr; + curr = Math.max(temp, prev+nums[i]); + prev = temp; + } + + return curr; + } +} \ No newline at end of file diff --git a/Sample.java b/Sample.java deleted file mode 100644 index 1739a9cb..00000000 --- a/Sample.java +++ /dev/null @@ -1,7 +0,0 @@ -// Time Complexity : -// Space Complexity : -// Did this code successfully run on Leetcode : -// Any problem you faced while coding this : - - -// Your code here along with comments explaining your approach