-
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
ea7c1a3
commit 8e7ab0c
Showing
4 changed files
with
83 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,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; | ||
} | ||
} |
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(n) |
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(m * n * n) | ||
Space complexity - O(m * n * n) |
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,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]; | ||
} | ||
} |