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
38 changes: 38 additions & 0 deletions Problem1.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
21 changes: 21 additions & 0 deletions Problem2.cs
Original file line number Diff line number Diff line change
@@ -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];
}
}