Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Completed DP-10 #222

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
Completed DP-10
  • Loading branch information
Niharika28 committed Jun 27, 2024
commit 14b0e14cd631bfa9d6719875a315026676dbd547
49 changes: 49 additions & 0 deletions BurstBalloon.java
Original file line number Diff line number Diff line change
@@ -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];
}
}
45 changes: 45 additions & 0 deletions SuperEggDrop.java
Original file line number Diff line number Diff line change
@@ -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];
}
}