Post

2145 Grid Game

2145 Grid Game

Grid Game image

You are given a 0-indexed 2D array grid of size 2 x n, where grid[r][c] represents the number of points at position (r, c) on the matrix. Two robots are playing a game on this matrix.

Both robots initially start at (0, 0) and want to reach (1, n-1). Each robot may only move to the right ((r, c) to (r, c + 1)) or **down **((r, c) to (r + 1, c)).

At the start of the game, the first robot moves from (0, 0) to (1, n-1), collecting all the points from the cells on its path. For all cells (r, c) traversed on the path, grid[r][c] is set to 0. Then, the second robot moves from (0, 0) to (1, n-1), collecting the points on its path. Note that their paths may intersect with one another.

The first robot wants to minimize the number of points collected by the second robot. In contrast, the second **robot wants to **maximize the number of points it collects. If both robots play optimally, return the number of points collected by the second robot.

 

Example 1:

image

1
2
3
4
5
6
7
**Input:** grid = [[2,5,4],[1,5,1]]
**Output:** 4
**Explanation:** The optimal path taken by the first robot is shown in red, and the optimal path taken by the second robot is shown in blue.
The cells visited by the first robot are set to 0.
The second robot will collect 0 + 0 + 4 + 0 = 4 points.

Example 2:

image

1
2
3
4
5
6
7
**Input:** grid = [[3,3,1],[8,5,2]]
**Output:** 4
**Explanation:** The optimal path taken by the first robot is shown in red, and the optimal path taken by the second robot is shown in blue.
The cells visited by the first robot are set to 0.
The second robot will collect 0 + 3 + 1 + 0 = 4 points.

Example 3:

image

1
2
3
4
5
6
7
**Input:** grid = [[1,3,1,15],[1,3,3,1]]
**Output:** 7
**Explanation: **The optimal path taken by the first robot is shown in red, and the optimal path taken by the second robot is shown in blue.
The cells visited by the first robot are set to 0.
The second robot will collect 0 + 1 + 3 + 3 + 0 = 7 points.

 

Constraints:

1
2
3
4
grid.length == 2
n == grid[r].length
1 <= n <= 5 * 104
1 <= grid[r][c] <= 105
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65

/*
dp did not work 
1 2 3 4
5 6 7 8

10,9,7,4
26,21,15,8
https://leetcode.com/problems/grid-game/editorial/comments/2817377

for every n , i to find the pivot and then n to evaluate b's sum n*n*n
- fill prefix and suffix for both rows
- row1 prefix + row0 suffix sum least 

*/

func gridGame(grid [][]int) int64 {
    if len(grid[0]) == 1 {
        return 0
    }
    arr := make([][]int, 2)

    for i := 0 ; i < 2 ; i ++ {
        arr[i] = make([]int, len(grid[0]))
    }

    temp := 0
    for i := 0 ; i < len(grid[0]) ; i ++ {
        temp += grid[1][i]
        arr[1][i] = temp
    }

    temp = 0
    for i := len(grid[0]) - 1 ; i >= 0 ; i -- {
        temp += grid[0][i]
        arr[0][i] = temp
    }

    s := 10000000000000
    for i := 0 ; i < len(grid[0]) ; i ++ {

        if i - 1 >= 0 && i + 1 < len(grid[0]) {
            s = min(s, max(arr[1][i-1] , arr[0][i+1]))
            continue
        }

        if i - 1 < 0 && i + 1 < len(grid[0]){
            s = min(s,  arr[0][i+1])
            continue
        }

        if i - 1 >= 0 && i + 1 > len(grid[0])-1 {
            s = min(s,  arr[1][i-1])
            continue
        }
        
    }
    return int64(s)
    
}




This post is licensed under CC BY 4.0 by the author.