From 14b0e14cd631bfa9d6719875a315026676dbd547 Mon Sep 17 00:00:00 2001 From: Niharika M Date: Thu, 27 Jun 2024 13:27:48 -0600 Subject: [PATCH] Completed DP-10 --- BurstBalloon.java | 49 +++++++++++++++++++++++++++++++++++++++++++++++ SuperEggDrop.java | 45 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 BurstBalloon.java create mode 100644 SuperEggDrop.java diff --git a/BurstBalloon.java b/BurstBalloon.java new file mode 100644 index 0000000..1ac8a5f --- /dev/null +++ b/BurstBalloon.java @@ -0,0 +1,49 @@ +// Time Complexity : O(N^3) +// Space Complexity : O(N) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + +class Solution { + public int maxCoins(int[] nums) { + int n = nums.length; + int[][] dp = new int[n][n]; + + for(int le=1;le <= n;le++){ + for(int i=0; i <=n-le;i++){ // start range + int j= i+le-1; //end range + for(int k=i;k<=j;k++){ // bursting balloon at the last + // before value + balloon itself value + after alue + + int before = 0; + int after = 0; + + if(k != i){ + before = dp[i][k-1]; + } + + if(k != j){ + after = dp[k+1][j]; + } + + int prev = 1; + int next =1; + int curr = nums[k]; + + if( i != 0){ + prev = nums[i-1]; + } + + if(j != n-1){ + next = nums[j+1]; + } + + int balloonItself = prev * curr * next; + + dp[i][j] = Math.max(dp[i][j],before+balloonItself+after); + } + } + } + + return dp[0][n-1]; + } +} \ No newline at end of file diff --git a/SuperEggDrop.java b/SuperEggDrop.java new file mode 100644 index 0000000..034835e --- /dev/null +++ b/SuperEggDrop.java @@ -0,0 +1,45 @@ +// Time Complexity : O(NK) +// Space Complexity : O(NK) +// Did this code successfully run on Leetcode : TLE +// Any problem you faced while coding this : TLE +class Solution { + public int superEggDrop(int k, int n) { + int[][] dp = new int[n+1][k+1]; + + int attempts =0; + while(dp[attempts][k] < n) { + attempts++; + + for(int j=1;j<=k;j++){ + dp[attempts][j] = 1 + dp[attempts-1][j-1] + dp[attempts-1][j]; + } + } + + return attempts; + } +} + +// Time Complexity : O(N^ * KK) +// Space Complexity : O(N) +// Did this code successfully run on Leetcode : TLE +// Any problem you faced while coding this : TLE +class Solution { + public int superEggDrop(int k, int n) { + int[][] dp = new int[k+1][n+1]; + + for(int j=1;j<=n;j++){ + dp[1][j] = j; + } + + for(int i=2;i<=k;i++){ + for(int j=1;j<=n;j++){ + dp[i][j] = Integer.MAX_VALUE; + for(int f=1;f<=j;f++){ + dp[i][j] = Math.min(dp[i][j],1 + Math.max(dp[i-1][f-1], dp[i][j-f])); + } + } + } + + return dp[k][n]; + } +} \ No newline at end of file