|
70 | 70 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
71 | 71 |
|
72 | 72 | ```python
|
73 |
| - |
| 73 | +class Solution: |
| 74 | + def gridGame(self, grid: List[List[int]]) -> int: |
| 75 | + ans = inf |
| 76 | + s1, s2 = sum(grid[0]), 0 |
| 77 | + for j, v in enumerate(grid[0]): |
| 78 | + s1 -= v |
| 79 | + ans = min(ans, max(s1, s2)) |
| 80 | + s2 += grid[1][j] |
| 81 | + return ans |
74 | 82 | ```
|
75 | 83 |
|
76 | 84 | ### **Java**
|
77 | 85 |
|
78 | 86 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
79 | 87 |
|
80 | 88 | ```java
|
| 89 | +class Solution { |
| 90 | + public long gridGame(int[][] grid) { |
| 91 | + long ans = Long.MAX_VALUE; |
| 92 | + long s1 = 0, s2 = 0; |
| 93 | + for (int v : grid[0]) { |
| 94 | + s1 += v; |
| 95 | + } |
| 96 | + int n = grid[0].length; |
| 97 | + for (int j = 0; j < n; ++j) { |
| 98 | + s1 -= grid[0][j]; |
| 99 | + ans = Math.min(ans, Math.max(s1, s2)); |
| 100 | + s2 += grid[1][j]; |
| 101 | + } |
| 102 | + return ans; |
| 103 | + } |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +### **C++** |
| 108 | + |
| 109 | +```cpp |
| 110 | +using ll = long long; |
| 111 | + |
| 112 | +class Solution { |
| 113 | +public: |
| 114 | + long long gridGame(vector<vector<int>>& grid) { |
| 115 | + ll ans = LONG_MAX; |
| 116 | + int n = grid[0].size(); |
| 117 | + ll s1 = 0, s2 = 0; |
| 118 | + for (int& v : grid[0]) s1 += v; |
| 119 | + for (int j = 0; j < n; ++j) |
| 120 | + { |
| 121 | + s1 -= grid[0][j]; |
| 122 | + ans = min(ans, max(s1, s2)); |
| 123 | + s2 += grid[1][j]; |
| 124 | + } |
| 125 | + return ans; |
| 126 | + } |
| 127 | +}; |
| 128 | +``` |
81 | 129 |
|
| 130 | +### **Go** |
| 131 | +
|
| 132 | +```go |
| 133 | +func gridGame(grid [][]int) int64 { |
| 134 | + ans := math.MaxInt64 |
| 135 | + s1, s2 := 0, 0 |
| 136 | + for _, v := range grid[0] { |
| 137 | + s1 += v |
| 138 | + } |
| 139 | + for j, v := range grid[0] { |
| 140 | + s1 -= v |
| 141 | + ans = min(ans, max(s1, s2)) |
| 142 | + s2 += grid[1][j] |
| 143 | + } |
| 144 | + return int64(ans) |
| 145 | +} |
| 146 | +
|
| 147 | +func max(a, b int) int { |
| 148 | + if a > b { |
| 149 | + return a |
| 150 | + } |
| 151 | + return b |
| 152 | +} |
| 153 | +
|
| 154 | +func min(a, b int) int { |
| 155 | + if a < b { |
| 156 | + return a |
| 157 | + } |
| 158 | + return b |
| 159 | +} |
82 | 160 | ```
|
83 | 161 |
|
84 | 162 | ### **...**
|
|
0 commit comments