-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create Day 8: Optimal Strategy For A Game.java
- Loading branch information
1 parent
bdebe40
commit 4732b84
Showing
1 changed file
with
31 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
class solve | ||
{ | ||
//Function to find the maximum possible amount of money we can win. | ||
static long countMaximum(int n, int arr[]){ | ||
int[] sum = new int[n+1]; | ||
sum[0] = 0; | ||
for(int i=0; i<n; i++){ | ||
sum[i+1] = arr[i] + sum[i]; | ||
} | ||
int[][] dp = new int[n][n]; | ||
for(int i=0; i<n; i++){ | ||
dp[i][i] = arr[i]; | ||
} | ||
for(int d=1; d<n; d++){ | ||
for(int r=0; r<n-d; r++){ | ||
int c = d+r; | ||
//option 1 | ||
//take arr[c] | ||
//sum from r to c-1 inclusive = sum till c-1 - sum till r-1 | ||
// = sum[c] - sum[r] | ||
dp[r][c] = arr[c] + (sum[c] - sum[r]) - dp[r][c-1]; | ||
//option 2 | ||
//take arr[r] | ||
//sum from r+1 to c inclusive = sum till c - sum till r | ||
// = sum[c+1] - sum[r+1]; | ||
dp[r][c] = Math.max(dp[r][c], arr[r] + (sum[c+1] - sum[r+1]) - dp[r+1][c]); | ||
} | ||
} | ||
return dp[0][n-1]; | ||
} | ||
} |