-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bf613b6
commit f0d1593
Showing
4 changed files
with
87 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,63 @@ | ||
//{ Driver Code Starts | ||
import java.util.*; | ||
import java.io.*; | ||
import java.lang.*; | ||
|
||
class OptimalStrategy | ||
{ | ||
public static void main (String[] args) { | ||
|
||
//taking input using Scanner class | ||
Scanner sc = new Scanner(System.in); | ||
|
||
//taking total number of testcases | ||
int t = sc.nextInt(); | ||
|
||
while(t-- > 0) | ||
{ | ||
//taking number of elements | ||
int n = sc.nextInt(); | ||
int arr[] = new int[n]; | ||
|
||
//inserting the elements | ||
for(int i = 0; i < n; i++) | ||
arr[i] = sc.nextInt(); | ||
|
||
//calling the countMaximum() method of class solve | ||
System.out.println(new solve().countMaximum(n, arr)); | ||
} | ||
} | ||
|
||
|
||
} | ||
// } Driver Code Ends | ||
|
||
|
||
|
||
class solve | ||
{ | ||
//Function to find the maximum possible amount of money we can win. | ||
static long countMaximum(int n, int arr[]) | ||
{ | ||
// Your code here | ||
int[][] dp = new int[n][n]; | ||
|
||
for(int m=0; m<n; m++) | ||
{ | ||
for(int i=0, j=m; j<n; i++, j++) | ||
{ | ||
if(m==0) | ||
dp[i][j]=arr[i]; | ||
|
||
else if(m==1) | ||
dp[i][j]=Math.max(arr[i], arr[j]); | ||
|
||
else | ||
dp[i][j]=Math.max(arr[i] + Math.min(dp[i+2][j], dp[i+1][j-1]), | ||
arr[j] + Math.min(dp[i+1][j-1],dp[i][j-2])); | ||
} | ||
} | ||
|
||
return dp[0][n-1]; | ||
} | ||
} |
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,2 @@ | ||
Time complexity - O(n^2) | ||
Space complexity - O(n^2) |
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,2 @@ | ||
Time complexity - O(n) | ||
Space complexity - O(1) |
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,20 @@ | ||
class Solution | ||
{ | ||
public int countStudents(int[] students, int[] sandwiches) | ||
{ | ||
int[] count = new int[2]; | ||
|
||
for (int student : students) | ||
++count[student]; | ||
|
||
for (int i = 0; i < sandwiches.length; ++i) | ||
{ | ||
if (count[sandwiches[i]] == 0) | ||
return sandwiches.length - i; | ||
|
||
--count[sandwiches[i]]; | ||
} | ||
|
||
return 0; | ||
} | ||
} |