Skip to content

Commit 962e6af

Browse files
authored
Merge pull request #1373 from 0xff-dev/1292
Add solution and test-cases for problem 1292
2 parents d5358c4 + 595ba28 commit 962e6af

File tree

3 files changed

+37
-33
lines changed

3 files changed

+37
-33
lines changed

leetcode/1201-1300/1292.Maximum-Side-Length-of-a-Square-with-Sum-Less-than-or-Equal-to-Threshold/README.md

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,6 @@ Input: mat = [[2,2,2,2,2],[2,2,2,2,2],[2,2,2,2,2],[2,2,2,2,2],[2,2,2,2,2]], thre
1818
Output: 0
1919
```
2020

21-
**Tags:** Math, String
22-
23-
## 题意
24-
> 求2数之和
25-
26-
## 题解
27-
28-
### 思路1
29-
> 。。。。
30-
31-
```go
32-
33-
```
34-
35-
### 思路2
36-
> 思路2
37-
```go
38-
39-
```
40-
4121
## 结语
4222

4323
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(mat [][]int, threshold int) int {
4+
rows, cols := len(mat), len(mat[0])
5+
cache := make([][]int, rows+1)
6+
for i := 0; i <= rows; i++ {
7+
cache[i] = make([]int, cols+1)
8+
}
9+
10+
for i := rows - 1; i >= 0; i-- {
11+
for j := cols - 1; j >= 0; j-- {
12+
cache[i][j] = mat[i][j] + cache[i+1][j] + cache[i][j+1] - cache[i+1][j+1]
13+
}
14+
}
15+
16+
var ret, ml int
17+
for i := 0; i < rows; i++ {
18+
for j := 0; j < cols; j++ {
19+
ml = min(rows-i, cols-j)
20+
for k := 1; k <= ml; k++ {
21+
sum := cache[i][j] - cache[i+k][j] - cache[i][j+k] + cache[i+k][j+k]
22+
if sum <= threshold {
23+
ret = max(ret, k)
24+
}
25+
}
26+
}
27+
}
28+
return ret
529
}

leetcode/1201-1300/1292.Maximum-Side-Length-of-a-Square-with-Sum-Less-than-or-Equal-to-Threshold/Solution_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,31 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
inputs [][]int
14+
threshold int
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", [][]int{{1, 1, 2, 3, 4, 3, 2}, {1, 1, 2, 3, 4, 3, 2}, {1, 1, 2, 3, 4, 3, 2}}, 4, 2},
18+
{"TestCase2", [][]int{{2, 2, 2, 2, 2}, {2, 2, 2, 2, 2}, {2, 2, 2, 2, 2}, {2, 2, 2, 2, 2}, {2, 2, 2, 2, 2}}, 1, 0},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.inputs, c.threshold)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
27+
c.expect, got, c.inputs, c.threshold)
2828
}
2929
})
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)