diff --git a/Problem1.cs b/Problem1.cs new file mode 100644 index 00000000..d1e5e421 --- /dev/null +++ b/Problem1.cs @@ -0,0 +1,38 @@ +// Time Complexity : O(m*n) where m is the total number of coins and n is the total amount +// Space Complexity : O(n) where n is the total amount +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + + +public class Solution +{ + public int CoinChange(int[] coins, int amount) + { + int rows = coins.Length + 1; + int columns = amount + 1; + + int[] dp = new int[columns]; + + // Fill all but first element with amount + 1 + + for (int j = 1; j < columns; j++) + { + dp[j] = amount + 1; + } + + // D.P + + for (int i = 1; i < rows; i++) + { + for (int j = 1; j < columns; j++) + { + if (j >= coins[i - 1]) + { + dp[j] = Math.Min(dp[j], 1 + dp[j - coins[i - 1]]); + } + } + } + + return dp[columns - 1] <= amount ? dp[columns - 1] : -1; + } +} \ No newline at end of file diff --git a/Problem2.cs b/Problem2.cs new file mode 100644 index 00000000..90f0a0a2 --- /dev/null +++ b/Problem2.cs @@ -0,0 +1,21 @@ +// Time Complexity : O(n) where m is the total length of nums array +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + + +public class Solution +{ + public int Rob(int[] nums) + { + for (int i = 0; i < nums.Length; i++) + { + int secondPrev = (i - 2) >= 0 ? nums[i - 2] : 0; + int firstPrev = (i - 1) >= 0 ? nums[i - 1] : 0; + + nums[i] = Math.Max(secondPrev + nums[i], firstPrev); + } + + return nums[nums.Length - 1]; + } +} \ No newline at end of file