Skip to content

Commit b17f772

Browse files
add 2017
1 parent 93ad4de commit b17f772

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ _If you like this project, please leave me a star._ ★
99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
1111
|2018|[Check if Word Can Be Placed In Crossword](https://leetcode.com/problems/check-if-word-can-be-placed-in-crossword/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2018.java) ||Medium||
12+
|2017|[Grid Game](https://leetcode.com/problems/grid-game/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2017.java) ||Medium|Array, Matrix, Prefix Sum|
1213
|2016|[Maximum Difference Between Increasing Elements](https://leetcode.com/problems/maximum-difference-between-increasing-elements/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2016.java) ||Easy||
1314
|2012|[Sum of Beauty in the Array](https://leetcode.com/problems/sum-of-beauty-in-the-array/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2012.java) ||Medium||
1415
|2011|[Final Value of Variable After Performing Operations](https://leetcode.com/problems/final-value-of-variable-after-performing-operations/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2011.java) ||Easy||
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _2017 {
4+
public static class Solution1 {
5+
/**
6+
* credit: https://leetcode.com/uwi/
7+
*/
8+
public long gridGame(int[][] grid) {
9+
int n = grid[0].length;
10+
long[] cum0 = new long[n + 1];
11+
long[] cum1 = new long[n + 1];
12+
for (int i = 0; i < n; i++) {
13+
cum0[i + 1] = cum0[i] + grid[0][i];
14+
cum1[i + 1] = cum1[i] + grid[1][i];
15+
}
16+
long ans = Long.MAX_VALUE;
17+
for (int i = 0; i < n; i++) {
18+
ans = Math.min(ans, Math.max(cum0[n] - cum0[i + 1], cum1[i]));
19+
}
20+
return ans;
21+
}
22+
}
23+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.utils.CommonUtils;
4+
import com.fishercoder.solutions._1;
5+
import com.fishercoder.solutions._2017;
6+
import org.junit.BeforeClass;
7+
import org.junit.Test;
8+
9+
import static org.junit.Assert.assertArrayEquals;
10+
import static org.junit.Assert.assertEquals;
11+
12+
public class _2017Test {
13+
private static _2017.Solution1 solution1;
14+
private static int[][] grid;
15+
16+
@BeforeClass
17+
public static void setup() {
18+
solution1 = new _2017.Solution1();
19+
}
20+
21+
@Test
22+
public void test1() {
23+
grid = CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[2,5,4],[1,5,1]");
24+
assertEquals(4, solution1.gridGame(grid));
25+
}
26+
27+
@Test
28+
public void test2() {
29+
grid = CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[3,3,1],[8,5,2]");
30+
assertEquals(4, solution1.gridGame(grid));
31+
}
32+
33+
@Test
34+
public void test3() {
35+
grid = CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[1,3,1,15],[1,3,3,1]");
36+
assertEquals(7, solution1.gridGame(grid));
37+
}
38+
39+
@Test
40+
public void test4() {
41+
grid = CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[20,3,20,17,2,12,15,17,4,15],[20,10,13,14,15,5,2,3,14,3]");
42+
assertEquals(63, solution1.gridGame(grid));
43+
}
44+
45+
}

0 commit comments

Comments
 (0)