Skip to content

Commit

Permalink
Merge pull request #999 from 0xff-dev/1590
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 1590
  • Loading branch information
6boris authored Oct 30, 2024
2 parents e8ac188 + 430294c commit 8be7d40
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 25 deletions.
33 changes: 20 additions & 13 deletions leetcode/1501-1600/1590.Make-Sum-Divisible-by-P/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
# [1590.Make Sum Divisible by P][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 array of positive integers `nums`, remove the **smallest** subarray (possibly **empty**) such that the **sum** of the remaining elements is divisible by `p`. It is **not** allowed to remove the whole array.

Return the length of the smallest subarray that you need to remove, or `-1` if it's impossible.

A **subarray** is defined as a contiguous block of elements in the array.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: nums = [3,1,4,2], p = 6
Output: 1
Explanation: The sum of the elements in nums is 10, which is not divisible by 6. We can remove the subarray [4], and the sum of the remaining elements is 6, which is divisible by 6.
```

## 题意
> ...
## 题解
**Example 2:**

### 思路1
> ...
Make Sum Divisible by P
```go
```
Input: nums = [6,3,5,2], p = 9
Output: 2
Explanation: We cannot remove a single element to get a sum divisible by 9. The best way is to remove the subarray [5,2], leaving us with [6,3] with sum 9.
```

**Example 3:**

```
Input: nums = [1,2,3], p = 3
Output: 0
Explanation: Here the sum is 6. which is already divisible by 3. Thus we do not need to remove anything.
```

## 结语

Expand Down
34 changes: 32 additions & 2 deletions leetcode/1501-1600/1590.Make-Sum-Divisible-by-P/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,35 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(nums []int, p int) int {
n := len(nums)
sum := 0
for _, num := range nums {
sum = (sum + num) % p
}

target := sum % p
if target == 0 {
return 0
}

cache := make(map[int]int)
cache[0] = -1
tmpSum := 0
ans := n

for i := 0; i < n; i++ {
tmpSum = (tmpSum + nums[i]) % p
needed := (tmpSum - target + p) % p

if idx, found := cache[needed]; found {
ans = min(ans, i-idx)
}

cache[tmpSum] = i
}

if ans == n {
return -1
}
return ans
}
21 changes: 11 additions & 10 deletions leetcode/1501-1600/1590.Make-Sum-Divisible-by-P/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,31 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
nums []int
p int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{3, 1, 4, 2}, 6, 1},
{"TestCase2", []int{6, 3, 5, 2}, 9, 2},
{"TestCase3", []int{1, 2, 3}, 3, 0},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.nums, c.p)
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.nums, c.p)
}
})
}
}

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

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

0 comments on commit 8be7d40

Please sign in to comment.