From bf4007a4372f6947edda7280482dcc3eb75876b2 Mon Sep 17 00:00:00 2001 From: Praniksha123 Date: Mon, 10 Aug 2026 21:41:11 +0530 Subject: [PATCH] Create dp1.java --- dp1.java | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 dp1.java diff --git a/dp1.java b/dp1.java new file mode 100644 index 00000000..7b257951 --- /dev/null +++ b/dp1.java @@ -0,0 +1,38 @@ +//problem1 +class Solution { + public int coinChange(int[] coins, int amount) { + int n=coins.length; + int m=amount; + int[][] dp=new int[n+1][m+1]; + for(int j=1;j<=m;j++){ + dp[0][j]=amount+1; + } + for(int i=1;i<=n;i++){ + for(int j=1;j<=m;j++){ + if(coins[i-1]>j){ + dp[i][j]=dp[i-1][j]; + }else{ + dp[i][j]=Math.min(dp[i-1][j],1+dp[i][j-coins[i-1]]); + } + } + } + int res=dp[n][m]; + return res==amount+1?-1:res; + } +} +//problem2 +class Solution { + public int rob(int[] nums) { + if(nums.length==1) return nums[0]; + int n=nums.length; + int temp; + int prev=nums[0]; + int curr=Math.max(nums[0],nums[1]); + for(int i=2;i