From 8716858e7b651357421796802079fb0ff35a608a Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Tue, 28 Oct 2025 22:49:30 +0800 Subject: [PATCH] Add solution and test-cases for problem 3354 --- .../README.md | 50 ++++++++++++++----- .../Solution.go | 25 +++++++++- .../Solution_test.go | 13 +++-- 3 files changed, 66 insertions(+), 22 deletions(-) diff --git a/leetcode/3301-3400/3354.Make-Array-Elements-Equal-to-Zero/README.md b/leetcode/3301-3400/3354.Make-Array-Elements-Equal-to-Zero/README.md index c47f9d4f9..4091a8f7d 100755 --- a/leetcode/3301-3400/3354.Make-Array-Elements-Equal-to-Zero/README.md +++ b/leetcode/3301-3400/3354.Make-Array-Elements-Equal-to-Zero/README.md @@ -1,28 +1,52 @@ # [3354.Make Array Elements Equal 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 integer array `nums`. + +Start by selecting a starting position `curr` such that `nums[curr] == 0`, and choose a movement **direction** of either left or right. + +After that, you repeat the following process: + +- If `curr` is out of the range `[0, n - 1]`, this process ends. +- If `nums[curr] == 0`, move in the current direction by **incrementing** `curr` if you are moving right, or **decrementing** `curr` if you are moving left. +- Else if `nums[curr] > 0`: + + - Decrement `nums[curr]` by 1. + - **Reverse** your movement direction (left becomes right and vice versa). + - Take a step in your new direction. + +A selection of the initial position `curr` and movement direction is considered **valid** if every element in `nums` becomes 0 by the end of the process. + +Return the number of possible **valid** selections. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" -``` +Input: nums = [1,0,2,0,3] -## 题意 -> ... +Output: 2 -## 题解 +Explanation: + +The only possible valid selections are the following: + +Choose curr = 3, and a movement direction to the left. +[1,0,2,0,3] -> [1,0,2,0,3] -> [1,0,1,0,3] -> [1,0,1,0,3] -> [1,0,1,0,2] -> [1,0,1,0,2] -> [1,0,0,0,2] -> [1,0,0,0,2] -> [1,0,0,0,1] -> [1,0,0,0,1] -> [1,0,0,0,1] -> [1,0,0,0,1] -> [0,0,0,0,1] -> [0,0,0,0,1] -> [0,0,0,0,1] -> [0,0,0,0,1] -> [0,0,0,0,0]. +Choose curr = 3, and a movement direction to the right. +[1,0,2,0,3] -> [1,0,2,0,3] -> [1,0,2,0,2] -> [1,0,2,0,2] -> [1,0,1,0,2] -> [1,0,1,0,2] -> [1,0,1,0,1] -> [1,0,1,0,1] -> [1,0,0,0,1] -> [1,0,0,0,1] -> [1,0,0,0,0] -> [1,0,0,0,0] -> [1,0,0,0,0] -> [1,0,0,0,0] -> [0,0,0,0,0]. +``` + +**Example 2:** -### 思路1 -> ... -Make Array Elements Equal to Zero -```go ``` +Input: nums = [2,3,4,0,4,1,0] +Output: 0 + +Explanation: + +There are no possible valid selections. +``` ## 结语 diff --git a/leetcode/3301-3400/3354.Make-Array-Elements-Equal-to-Zero/Solution.go b/leetcode/3301-3400/3354.Make-Array-Elements-Equal-to-Zero/Solution.go index d115ccf5e..77ea1c3ba 100644 --- a/leetcode/3301-3400/3354.Make-Array-Elements-Equal-to-Zero/Solution.go +++ b/leetcode/3301-3400/3354.Make-Array-Elements-Equal-to-Zero/Solution.go @@ -1,5 +1,26 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(nums []int) int { + l := len(nums) + left, right := make([]int, l+2), make([]int, l+2) + for i := 1; i <= l; i++ { + left[i] = left[i-1] + nums[i-1] + } + for i := l; i >= 1; i-- { + right[i] = right[i+1] + nums[i-1] + } + var ret int + for index := 1; index <= l; index++ { + if nums[index-1] != 0 { + continue + } + diff := left[index-1] - right[index+1] + if diff == 0 { + ret += 2 + } + if diff == 1 || diff == -1 { + ret++ + } + } + return ret } diff --git a/leetcode/3301-3400/3354.Make-Array-Elements-Equal-to-Zero/Solution_test.go b/leetcode/3301-3400/3354.Make-Array-Elements-Equal-to-Zero/Solution_test.go index 14ff50eb4..8ebd3e5dd 100644 --- a/leetcode/3301-3400/3354.Make-Array-Elements-Equal-to-Zero/Solution_test.go +++ b/leetcode/3301-3400/3354.Make-Array-Elements-Equal-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{1, 0, 2, 0, 3}, 2}, + {"TestCase2", []int{2, 3, 4, 0, 4, 1, 0}, 0}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }