Skip to content

Commit

Permalink
Added codes for 8 April
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Apr 8, 2024
1 parent bf613b6 commit f0d1593
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
63 changes: 63 additions & 0 deletions GeeksForGeeks/April/08-4-24/GFG.java
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];
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/April/08-4-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n^2)
Space complexity - O(n^2)
2 changes: 2 additions & 0 deletions LeetCode/April/08-4-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n)
Space complexity - O(1)
20 changes: 20 additions & 0 deletions LeetCode/April/08-4-24/Solution.java
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;
}
}

0 comments on commit f0d1593

Please sign in to comment.