From 430294cfa7d0612865efd1c6ed5f081a6b09964f Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Thu, 3 Oct 2024 16:14:44 +0800 Subject: [PATCH] Add solution and test-cases for problem 1590 --- .../1590.Make-Sum-Divisible-by-P/README.md | 33 +++++++++++------- .../1590.Make-Sum-Divisible-by-P/Solution.go | 34 +++++++++++++++++-- .../Solution_test.go | 21 ++++++------ 3 files changed, 63 insertions(+), 25 deletions(-) diff --git a/leetcode/1501-1600/1590.Make-Sum-Divisible-by-P/README.md b/leetcode/1501-1600/1590.Make-Sum-Divisible-by-P/README.md index 0aab8efee..47a03e25a 100755 --- a/leetcode/1501-1600/1590.Make-Sum-Divisible-by-P/README.md +++ b/leetcode/1501-1600/1590.Make-Sum-Divisible-by-P/README.md @@ -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. +``` ## 结语 diff --git a/leetcode/1501-1600/1590.Make-Sum-Divisible-by-P/Solution.go b/leetcode/1501-1600/1590.Make-Sum-Divisible-by-P/Solution.go index d115ccf5e..f0f474744 100644 --- a/leetcode/1501-1600/1590.Make-Sum-Divisible-by-P/Solution.go +++ b/leetcode/1501-1600/1590.Make-Sum-Divisible-by-P/Solution.go @@ -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 } diff --git a/leetcode/1501-1600/1590.Make-Sum-Divisible-by-P/Solution_test.go b/leetcode/1501-1600/1590.Make-Sum-Divisible-by-P/Solution_test.go index 14ff50eb4..df90da69b 100644 --- a/leetcode/1501-1600/1590.Make-Sum-Divisible-by-P/Solution_test.go +++ b/leetcode/1501-1600/1590.Make-Sum-Divisible-by-P/Solution_test.go @@ -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() { }