From 487934bbc64512664ea41278e4558e8ef2475719 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Sun, 11 May 2025 18:40:02 +0800 Subject: [PATCH] Add solution and test-cases for problem 1262 --- .../README.md | 29 ++++++++++--------- .../Solution.go | 19 ++++++++++-- .../Solution_test.go | 14 ++++----- 3 files changed, 40 insertions(+), 22 deletions(-) diff --git a/leetcode/1201-1300/1262.Greatest-Sum-Divisible-by-Three/README.md b/leetcode/1201-1300/1262.Greatest-Sum-Divisible-by-Three/README.md index 5f836f831..8b10256a8 100644 --- a/leetcode/1201-1300/1262.Greatest-Sum-Divisible-by-Three/README.md +++ b/leetcode/1201-1300/1262.Greatest-Sum-Divisible-by-Three/README.md @@ -1,28 +1,31 @@ # [1262.Greatest Sum Divisible by Three][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +Given an integer array `nums`, return the **maximum possible sum** of elements of the array such that it is divisible by three. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: nums = [3,6,5,1,8] +Output: 18 +Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -Greatest Sum Divisible by Three -```go +``` +Input: nums = [4] +Output: 0 +Explanation: Since 4 is not divisible by 3, do not pick any number. ``` +**Example 3:** + +``` +Input: nums = [1,2,3,4,4] +Output: 12 +Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3). +``` ## 结语 diff --git a/leetcode/1201-1300/1262.Greatest-Sum-Divisible-by-Three/Solution.go b/leetcode/1201-1300/1262.Greatest-Sum-Divisible-by-Three/Solution.go index d115ccf5e..daad8c336 100644 --- a/leetcode/1201-1300/1262.Greatest-Sum-Divisible-by-Three/Solution.go +++ b/leetcode/1201-1300/1262.Greatest-Sum-Divisible-by-Three/Solution.go @@ -1,5 +1,20 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(nums []int) int { + dp := []int{-1, -1, -1} + dp[0] = 0 + + for _, num := range nums { + t := make([]int, 3) + copy(t, dp) + for _, i := range t { + if i >= 0 { + newR := (i + num) % 3 + if i+num > dp[newR] { + dp[newR] = i + num + } + } + } + } + return dp[0] } diff --git a/leetcode/1201-1300/1262.Greatest-Sum-Divisible-by-Three/Solution_test.go b/leetcode/1201-1300/1262.Greatest-Sum-Divisible-by-Three/Solution_test.go index 14ff50eb4..2e8bfaf25 100644 --- a/leetcode/1201-1300/1262.Greatest-Sum-Divisible-by-Three/Solution_test.go +++ b/leetcode/1201-1300/1262.Greatest-Sum-Divisible-by-Three/Solution_test.go @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs []int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{3, 6, 5, 1, 8}, 18}, + {"TestCase2", []int{4}, 0}, + {"TestCase3", []int{1, 2, 3, 4, 4}, 12}, } // 开始测试 @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }