Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions leetcode/1201-1300/1262.Greatest-Sum-Divisible-by-Three/README.md
Original file line number Diff line number Diff line change
@@ -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).
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -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]
}
Original file line number Diff line number Diff line change
Expand Up @@ -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},
}

// 开始测试
Expand All @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading