Skip to content

Commit

Permalink
Added codes for 11 Feb
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Feb 11, 2024
1 parent ea7c1a3 commit 8e7ab0c
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
54 changes: 54 additions & 0 deletions GeeksForGeeks/February/11-2-24/GFG.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//{ Driver Code Starts
//Initial Template for Java

import java.io.*;
import java.util.*;
import java.lang.*;

class GFG{
public static void main(String args[])throws IOException
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(in.readLine());
while(t-- > 0){
int n = Integer.parseInt(in.readLine());

Solution ob = new Solution();
ArrayList<Integer> ans = ob.recamanSequence(n);
for(int i = 0;i < n;i++)
System.out.print(ans.get(i)+" ");
System.out.println();
}
}
}
// } Driver Code Ends


//User function Template for Java

class Solution{
static ArrayList<Integer> recamanSequence(int n)
{
// code here
ArrayList<Integer>list=new ArrayList<>();
list.add(0);

HashSet<Integer>set=new HashSet<>();
set.add(0);

for(int i=1;i<=n;i++)
{
int val = list.get(i-1)-i;

if(val<0||set.contains(val))
{
val=list.get(i-1)+i;
}

list.add(val);
set.add(val);
}

return list;
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/February/11-2-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n)
Space complexity - O(n)
2 changes: 2 additions & 0 deletions LeetCode/February/11-2-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(m * n * n)
Space complexity - O(m * n * n)
25 changes: 25 additions & 0 deletions LeetCode/February/11-2-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution
{
public int cherryPickup(int[][] grid)
{
final int m = grid.length;
final int n = grid[0].length;

// dp[x][y1][y2] := the maximum cherries we can collect
// where the robot #1 is on (x, y1) and the robot #2 is on (x, y2)

int[][][] dp = new int[m + 1][n][n];

for (int x = m - 1; x >= 0; --x)
for (int y1 = 0; y1 < n; ++y1)
for (int y2 = 0; y2 < n; ++y2)
{
int currRow = grid[x][y1] + (y1 == y2 ? 0 : 1) * grid[x][y2];
for (int d1 = Math.max(0, y1 - 1); d1 < Math.min(n, y1 + 2); ++d1)
for (int d2 = Math.max(0, y2 - 1); d2 < Math.min(n, y2 + 2); ++d2)
dp[x][y1][y2] = Math.max(dp[x][y1][y2], currRow + dp[x + 1][d1][d2]);
}

return dp[0][0][n - 1];
}
}

0 comments on commit 8e7ab0c

Please sign in to comment.