Skip to content

Commit 22263f4

Browse files
committed
Add solution and test-cases for problem 3225
1 parent 7dc324d commit 22263f4

5 files changed

Lines changed: 98 additions & 22 deletions

File tree

37 KB
Loading
40 KB
Loading

leetcode/3201-3300/3225.Maximum-Score-From-Grid-Operations/README.md

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,39 @@
11
# [3225.Maximum Score From Grid Operations][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given a 2D matrix `grid` of size `n x n`. Initially, all cells of the grid are colored white. In one operation, you can select any cell of indices `(i, j)`, and color black all the cells of the `jth` column starting from the top row down to the `ith` row.
5+
6+
The grid score is the sum of all `grid[i][j]` such that cell `(i, j)` is white and it has a horizontally adjacent black cell.
7+
8+
Return the **maximum** score that can be achieved after some number of operations.
79

8-
**Example 1:**
10+
**Example 1:**
11+
12+
![1](./1.png)
913

1014
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
15+
Input: grid = [[0,0,0,0,0],[0,0,3,0,0],[0,1,0,0,0],[5,0,0,3,0],[0,0,0,0,2]]
16+
17+
Output: 11
18+
19+
Explanation:
20+
21+
In the first operation, we color all cells in column 1 down to row 3, and in the second operation, we color all cells in column 4 down to the last row. The score of the resulting grid is grid[3][0] + grid[1][2] + grid[3][3] which is equal to 11.
1322
```
1423

15-
## 题意
16-
> ...
24+
**Example 2:**
1725

18-
## 题解
26+
![2](./2.png)
1927

20-
### 思路1
21-
> ...
22-
Maximum Score From Grid Operations
23-
```go
2428
```
29+
Input: grid = [[10,9,0,0,15],[7,1,0,8,0],[5,20,0,11,0],[0,0,0,1,2],[8,12,1,10,3]]
30+
31+
Output: 94
2532
33+
Explanation:
34+
35+
We perform operations on 1, 2, and 3 down to rows 1, 4, and 0, respectively. The score of the resulting grid is grid[0][0] + grid[1][0] + grid[2][1] + grid[4][1] + grid[1][3] + grid[2][3] + grid[3][3] + grid[4][3] + grid[0][4] which is equal to 94.
36+
```
2637

2738
## 结语
2839

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,71 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(grid [][]int) int64 {
4+
n := len(grid[0])
5+
if n == 1 {
6+
return 0
7+
}
8+
9+
dp := make([][][]int64, n)
10+
for i := range dp {
11+
dp[i] = make([][]int64, n+1)
12+
for j := range dp[i] {
13+
dp[i][j] = make([]int64, n+1)
14+
}
15+
}
16+
17+
prevMax := make([][]int64, n+1)
18+
for i := range prevMax {
19+
prevMax[i] = make([]int64, n+1)
20+
}
21+
prevSuffixMax := make([][]int64, n+1)
22+
for i := range prevSuffixMax {
23+
prevSuffixMax[i] = make([]int64, n+1)
24+
}
25+
26+
colSum := make([][]int64, n)
27+
for c := 0; c < n; c++ {
28+
colSum[c] = make([]int64, n+1)
29+
for r := 1; r <= n; r++ {
30+
colSum[c][r] = colSum[c][r-1] + int64(grid[r-1][c])
31+
}
32+
}
33+
34+
for i := 1; i < n; i++ {
35+
for currH := 0; currH <= n; currH++ {
36+
for prevH := 0; prevH <= n; prevH++ {
37+
if currH <= prevH {
38+
extraScore := colSum[i][prevH] - colSum[i][currH]
39+
dp[i][currH][prevH] = max(dp[i][currH][prevH], prevSuffixMax[prevH][0]+extraScore)
40+
} else {
41+
extraScore := colSum[i-1][currH] - colSum[i-1][prevH]
42+
dp[i][currH][prevH] = max(dp[i][currH][prevH],
43+
max(prevSuffixMax[prevH][currH], prevMax[prevH][currH]+extraScore))
44+
}
45+
}
46+
}
47+
48+
for currH := 0; currH <= n; currH++ {
49+
prevMax[currH][0] = dp[i][currH][0]
50+
for prevH := 1; prevH <= n; prevH++ {
51+
var penalty int64 = 0
52+
if prevH > currH {
53+
penalty = colSum[i][prevH] - colSum[i][currH]
54+
}
55+
prevMax[currH][prevH] = max(prevMax[currH][prevH-1], dp[i][currH][prevH]-penalty)
56+
}
57+
58+
prevSuffixMax[currH][n] = dp[i][currH][n]
59+
for prevH := n - 1; prevH >= 0; prevH-- {
60+
prevSuffixMax[currH][prevH] = max(prevSuffixMax[currH][prevH+1], dp[i][currH][prevH])
61+
}
62+
}
63+
}
64+
65+
var ans int64 = 0
66+
for k := 0; k <= n; k++ {
67+
ans = max(ans, max(dp[n-1][n][k], dp[n-1][0][k]))
68+
}
69+
70+
return ans
571
}

leetcode/3201-3300/3225.Maximum-Score-From-Grid-Operations/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs [][]int
14+
expect int64
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", [][]int{{0, 0, 0, 0, 0}, {0, 0, 3, 0, 0}, {0, 1, 0, 0, 0}, {5, 0, 0, 3, 0}, {0, 0, 0, 0, 2}}, 11},
17+
{"TestCase2", [][]int{{10, 9, 0, 0, 15}, {7, 1, 0, 8, 0}, {5, 20, 0, 11, 0}, {0, 0, 0, 1, 2}, {8, 12, 1, 10, 3}}, 94},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)