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
31 changes: 31 additions & 0 deletions Problem-1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Time Complexity :O(n)
// Space Complexity :O(1)
// Did this code successfully run on Leetcode :No
// Any problem you faced while coding this :No


// Your code here along with comments explaining your approach
//Keep a DP array of size 3 to store the minimum cost to paint the previous house in each color.
// For each house, update each color’s cost by adding its paint cost to the minimum of the other two colors from the previous house.
// After processing all houses, return the minimum value among the three colors, which gives the overall minimum painting cost.

class Solution {
public int minCost(int[][] costs) {
int n = costs.length;
if( n==0){
return 0;
}
int[] dp = new int[3];
dp[0] = costs[0][0];
dp[1] = costs[0][1];
dp[2] = costs[0][2];

for( int i =1; i < n; i++){
int temp = dp[0];
dp[0] = costs[i][0] + Math.min(dp[1],dp[2]);
int temp_dp1 = dp[1];
dp[1] = costs[i][1] + Math.min(temp,dp[2]);
dp[2] = costs[i][2]+ Math.min(temp,temp_dp1);
}
return Math.min(dp[0], Math.min(dp[1],dp[2]));
}}
24 changes: 24 additions & 0 deletions Problem-1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Time Complexity :O(n)
# Space Complexity :O(1)
# Did this code successfully run on Leetcode :No
# Any problem you faced while coding this :No


# Your code here along with comments explaining your approach
#Keep a DP array of size 3 to store the minimum cost to paint the previous house in each color.
# For each house, update each color’s cost by adding its paint cost to the minimum of the other two colors from the previous house.
# After processing all houses, return the minimum value among the three colors, which gives the overall minimum painting cost.

class Solution:
def minCost(self, costs):
n = len(costs)
m = len(costs[0])
dp = costs[0][:]
if n ==0:
return 0
for i in range(1,n):
prev_dp = dp[:]
dp[0] = costs[i][0] + min(prev_dp[1], prev_dp[2])
dp[1] = costs[i][1] + min(prev_dp[0], prev_dp[2])
dp[2] = costs[i][2] + min(prev_dp[0], prev_dp[1])
return min(dp[0],dp[1],dp[2])
27 changes: 27 additions & 0 deletions Problem-2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Time Complexity :O(mn)
// Space Complexity :O(n)
// Did this code successfully run on Leetcode :Yes
// Any problem you faced while coding this :No


// Your code here along with comments explaining your approach
//I used a DP array where dp[i] stores the number of ways to make amount i.
// For each coin, I iterate through all amounts ≥ coin value and update dp[j] += dp[j - coin] to add combinations including this coin.
// At the end, dp[amount] contains the total number of combinations to form the target amount.

class Solution {
public int change(int amount, int[] coins) {
int n = coins.length;
int[] dp = new int[amount+1];
dp[0] = 1;
if (amount <0 || n == 0){
return 0;
}
for (int i = 0; i < n; i++){
for (int j = coins[i]; j < amount+1; j++){
dp[j] = dp[j] + dp[j-coins[i]];
}
}
return dp[amount];
}
}
21 changes: 21 additions & 0 deletions Problem-2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Time Complexity :O(mn)
# Space Complexity :O(n)
# Did this code successfully run on Leetcode :Yes
# Any problem you faced while coding this :No


# Your code here along with comments explaining your approach
#I used a DP array where dp[i] stores the number of ways to make amount i.
# For each coin, I iterate through all amounts ≥ coin value and update dp[j] += dp[j - coin] to add combinations including this coin.
# At the end, dp[amount] contains the total number of combinations to form the target amount.

class Solution:
def change(self, amount: int, coins: List[int]) -> int:
dp = [0]* (amount+1)
dp[0] = 1
if len(coins) == 0 or amount < 0:
return 0
for coin in coins:
for j in range(coin,amount+1):
dp[j] = dp[j]+ dp[j-coin]
return dp[amount]