From 20ab8f01b868275656e4e7ec1426e71b6e83418a Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Mon, 10 Nov 2025 23:03:50 +0800 Subject: [PATCH] Add solution and test-cases for problem 3542 --- .../README.md | 51 ++++++++++++++----- .../Solution.go | 18 ++++++- .../Solution_test.go | 13 +++-- 3 files changed, 61 insertions(+), 21 deletions(-) diff --git a/leetcode/3501-3600/3542.Minimum-Operations-to-Convert-All-Elements-to-Zero/README.md b/leetcode/3501-3600/3542.Minimum-Operations-to-Convert-All-Elements-to-Zero/README.md index e674601f0..32e2fcc31 100755 --- a/leetcode/3501-3600/3542.Minimum-Operations-to-Convert-All-Elements-to-Zero/README.md +++ b/leetcode/3501-3600/3542.Minimum-Operations-to-Convert-All-Elements-to-Zero/README.md @@ -1,28 +1,55 @@ # [3542.Minimum Operations to Convert All Elements to Zero][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 +You are given an array `nums` of size `n`, consisting of **non-negative** integers. Your task is to apply some (possibly zero) operations on the array so that **all** elements become 0. + +In one operation, you can select a subarray `[i, j]` (where `0 <= i <= j < n`) and set all occurrences of the **minimum non-negative** integer in that subarray to 0. + +Return the **minimum** number of operations required to make all elements in the array 0. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: nums = [0,2] + +Output: 1 + +Explanation: + +Select the subarray [1,1] (which is [2]), where the minimum non-negative integer is 2. Setting all occurrences of 2 to 0 results in [0,0]. +Thus, the minimum number of operations required is 1. ``` -## 题意 -> ... +**Example 2:** + +``` +Input: nums = [3,1,2,1] -## 题解 +Output: 3 + +Explanation: + +Select subarray [1,3] (which is [1,2,1]), where the minimum non-negative integer is 1. Setting all occurrences of 1 to 0 results in [3,0,2,0]. +Select subarray [2,2] (which is [2]), where the minimum non-negative integer is 2. Setting all occurrences of 2 to 0 results in [3,0,0,0]. +Select subarray [0,0] (which is [3]), where the minimum non-negative integer is 3. Setting all occurrences of 3 to 0 results in [0,0,0,0]. +Thus, the minimum number of operations required is 3. +``` + +**Example 3:** -### 思路1 -> ... -Minimum Operations to Convert All Elements to Zero -```go ``` +Input: nums = [1,2,1,2,1,2] +Output: 4 + +Explanation: + +Select subarray [0,5] (which is [1,2,1,2,1,2]), where the minimum non-negative integer is 1. Setting all occurrences of 1 to 0 results in [0,2,0,2,0,2]. +Select subarray [1,1] (which is [2]), where the minimum non-negative integer is 2. Setting all occurrences of 2 to 0 results in [0,0,0,2,0,2]. +Select subarray [3,3] (which is [2]), where the minimum non-negative integer is 2. Setting all occurrences of 2 to 0 results in [0,0,0,0,0,2]. +Select subarray [5,5] (which is [2]), where the minimum non-negative integer is 2. Setting all occurrences of 2 to 0 results in [0,0,0,0,0,0]. +Thus, the minimum number of operations required is 4. +``` ## 结语 diff --git a/leetcode/3501-3600/3542.Minimum-Operations-to-Convert-All-Elements-to-Zero/Solution.go b/leetcode/3501-3600/3542.Minimum-Operations-to-Convert-All-Elements-to-Zero/Solution.go index d115ccf5e..3e3b1d8de 100644 --- a/leetcode/3501-3600/3542.Minimum-Operations-to-Convert-All-Elements-to-Zero/Solution.go +++ b/leetcode/3501-3600/3542.Minimum-Operations-to-Convert-All-Elements-to-Zero/Solution.go @@ -1,5 +1,19 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(nums []int) int { + s := []int{} + res := 0 + for _, a := range nums { + for len(s) > 0 && s[len(s)-1] > a { + s = s[:len(s)-1] + } + if a == 0 { + continue + } + if len(s) == 0 || s[len(s)-1] < a { + res++ + s = append(s, a) + } + } + return res } diff --git a/leetcode/3501-3600/3542.Minimum-Operations-to-Convert-All-Elements-to-Zero/Solution_test.go b/leetcode/3501-3600/3542.Minimum-Operations-to-Convert-All-Elements-to-Zero/Solution_test.go index 14ff50eb4..bf8264129 100644 --- a/leetcode/3501-3600/3542.Minimum-Operations-to-Convert-All-Elements-to-Zero/Solution_test.go +++ b/leetcode/3501-3600/3542.Minimum-Operations-to-Convert-All-Elements-to-Zero/Solution_test.go @@ -10,12 +10,11 @@ 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{0, 2}, 1}, + {"TestCase2", []int{3, 1, 2, 1}, 3}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }