From 595ba2836d52a7c5ba2c6e49710049789891d010 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Sun, 16 Nov 2025 16:38:23 +0800 Subject: [PATCH] Add solution and test-cases for problem 1292 --- .../README.md | 20 ------------- .../Solution.go | 28 +++++++++++++++++-- .../Solution_test.go | 22 +++++++-------- 3 files changed, 37 insertions(+), 33 deletions(-) diff --git a/leetcode/1201-1300/1292.Maximum-Side-Length-of-a-Square-with-Sum-Less-than-or-Equal-to-Threshold/README.md b/leetcode/1201-1300/1292.Maximum-Side-Length-of-a-Square-with-Sum-Less-than-or-Equal-to-Threshold/README.md index 27e6c8f53..00d4f4fff 100644 --- a/leetcode/1201-1300/1292.Maximum-Side-Length-of-a-Square-with-Sum-Less-than-or-Equal-to-Threshold/README.md +++ b/leetcode/1201-1300/1292.Maximum-Side-Length-of-a-Square-with-Sum-Less-than-or-Equal-to-Threshold/README.md @@ -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 Output: 0 ``` -**Tags:** Math, String - -## 题意 -> 求2数之和 - -## 题解 - -### 思路1 -> 。。。。 - -```go - -``` - -### 思路2 -> 思路2 -```go - -``` - ## 结语 如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me] diff --git a/leetcode/1201-1300/1292.Maximum-Side-Length-of-a-Square-with-Sum-Less-than-or-Equal-to-Threshold/Solution.go b/leetcode/1201-1300/1292.Maximum-Side-Length-of-a-Square-with-Sum-Less-than-or-Equal-to-Threshold/Solution.go index d115ccf5e..0332e86f0 100644 --- a/leetcode/1201-1300/1292.Maximum-Side-Length-of-a-Square-with-Sum-Less-than-or-Equal-to-Threshold/Solution.go +++ b/leetcode/1201-1300/1292.Maximum-Side-Length-of-a-Square-with-Sum-Less-than-or-Equal-to-Threshold/Solution.go @@ -1,5 +1,29 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(mat [][]int, threshold int) int { + rows, cols := len(mat), len(mat[0]) + cache := make([][]int, rows+1) + for i := 0; i <= rows; i++ { + cache[i] = make([]int, cols+1) + } + + for i := rows - 1; i >= 0; i-- { + for j := cols - 1; j >= 0; j-- { + cache[i][j] = mat[i][j] + cache[i+1][j] + cache[i][j+1] - cache[i+1][j+1] + } + } + + var ret, ml int + for i := 0; i < rows; i++ { + for j := 0; j < cols; j++ { + ml = min(rows-i, cols-j) + for k := 1; k <= ml; k++ { + sum := cache[i][j] - cache[i+k][j] - cache[i][j+k] + cache[i+k][j+k] + if sum <= threshold { + ret = max(ret, k) + } + } + } + } + return ret } diff --git a/leetcode/1201-1300/1292.Maximum-Side-Length-of-a-Square-with-Sum-Less-than-or-Equal-to-Threshold/Solution_test.go b/leetcode/1201-1300/1292.Maximum-Side-Length-of-a-Square-with-Sum-Less-than-or-Equal-to-Threshold/Solution_test.go index 14ff50eb4..53aae164d 100644 --- a/leetcode/1201-1300/1292.Maximum-Side-Length-of-a-Square-with-Sum-Less-than-or-Equal-to-Threshold/Solution_test.go +++ b/leetcode/1201-1300/1292.Maximum-Side-Length-of-a-Square-with-Sum-Less-than-or-Equal-to-Threshold/Solution_test.go @@ -9,31 +9,31 @@ import ( func TestSolution(t *testing.T) { // 测试用例 cases := []struct { - name string - inputs bool - expect bool + name string + inputs [][]int + threshold int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"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}, + {"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}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.inputs, c.threshold) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.inputs, c.threshold) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }