Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cb7cdaa

Browse files
Create Magic Grid
1 parent f36a3de commit cb7cdaa

File tree

1 file changed

+87
-0
lines changed
  • Course 2 - Data Structures in JAVA/Lecture 19 - Dynamic Programming II

1 file changed

+87
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
You are given a magic grid A with R rows and C columns. In the cells of the grid, you will either get magic juice, which increases your strength by |A[i][j]| points, or poison,
3+
which takes away |A[i][j]| strength points. If at any point of the journey, the strength points become less than or equal to zero, then you will die.
4+
You have to start from the top-left corner cell (1,1) and reach at the bottom-right corner cell (R,C).
5+
From a cell (i,j), you can only move either one cell down or right i.e., to cell (i+1,j) or cell (i,j+1) and you can not move outside the magic grid.
6+
You have to find the minimum number of strength points with which you will be able to reach the destination cell.
7+
8+
Input format:
9+
The first line contains the number of test cases T. T cases follow.
10+
Each test case consists of R C in the first line followed by the description of the grid in R lines, each containing C integers.
11+
Rows are numbered 1 to R from top to bottom and columns are numbered 1 to C from left to right. Cells with A[i][j] < 0 contain poison, others contain magic juice.
12+
13+
Output format:
14+
Output T lines, one for each case containing the minimum strength you should start with from the cell (1,1) to have a positive strength through out his journey to the cell (R,C).
15+
16+
Constraints:
17+
1 ≤ T ≤ 5
18+
2 ≤ R, C ≤ 500
19+
-10^3 ≤ A[i][j] ≤ 10^3
20+
A[1][1] = A[R][C] = 0
21+
Time Limit: 1 second
22+
23+
Sample Input 1:
24+
3
25+
2 3
26+
0 1 -3
27+
1 -2 0
28+
2 2
29+
0 1
30+
2 0
31+
3 4
32+
0 -2 -3 1
33+
-1 4 0 -2
34+
1 -2 -3 0
35+
Sample Output 1:
36+
2
37+
1
38+
2
39+
*/
40+
public class Solution{
41+
42+
43+
public static int getMinimumStrength(int[][] grid) {
44+
45+
/* Your class should be named Solution
46+
* Don't write main().
47+
* Don't read input, it is passed as function argument.
48+
* Return output and don't print it.
49+
* Taking input and printing output is handled automatically.
50+
*/
51+
int row=grid.length;
52+
if (row==0)
53+
return row;
54+
55+
int col=grid[0].length;
56+
if (col==0)
57+
return col;
58+
59+
int[][] dp=new int[row][col];
60+
dp[row-1][col-1]=1;
61+
62+
for (int i=col-2;i>=0;i--)
63+
{
64+
dp[row-1][i]=dp[row-1][i+1]-grid[row-1][i];
65+
66+
}
67+
for (int i=row-2;i>=0;i--)
68+
{
69+
dp[i][col-1]=dp[i+1][col-1]-grid[i][col-1];
70+
}
71+
72+
73+
for(int i=row-2;i>=0;i--)
74+
{
75+
for (int j=col-2;j>=0;j--)
76+
{
77+
int ans1=dp[i+1][j];
78+
int ans2=dp[i][j+1];
79+
80+
dp[i][j]=Math.max(1,Math.min(ans1,ans2)-grid[i][j]);
81+
}
82+
}
83+
84+
return dp[0][0];
85+
86+
}
87+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /