diff --git a/leetcode/3301-3400/3355.Zero-Array-Transformation-I/README.md b/leetcode/3301-3400/3355.Zero-Array-Transformation-I/README.md index ed9347af..615c8a82 100755 --- a/leetcode/3301-3400/3355.Zero-Array-Transformation-I/README.md +++ b/leetcode/3301-3400/3355.Zero-Array-Transformation-I/README.md @@ -1,28 +1,47 @@ # [3355.Zero Array Transformation I][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` of length n and a 2D array `queries`, where `queries[i] = [li, ri]`. + +For each `queries[i]`: + +- Select a subset of indices within the range `[li, ri]` in nums. +- Decrement the values at the selected indices by 1. + +A **Zero Array** is an array where all elements are equal to 0. + +Return `true` if it is possible to transform `nums` into a **Zero Array** after processing all the queries sequentially, otherwise return `false`. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" -``` +Input: nums = [1,0,1], queries = [[0,2]] -## 题意 -> ... +Output: true -## 题解 +Explanation: -### 思路1 -> ... -Zero Array Transformation I -```go +For i = 0: +Select the subset of indices as [0, 2] and decrement the values at these indices by 1. +The array will become [0, 0, 0], which is a Zero Array. ``` +**Example 2:** + +``` +Input: nums = [4,3,2,1], queries = [[1,3],[0,2]] + +Output: false + +Explanation: + +For i = 0: +Select the subset of indices as [1, 2, 3] and decrement the values at these indices by 1. +The array will become [4, 2, 1, 0]. +For i = 1: +Select the subset of indices as [0, 1, 2] and decrement the values at these indices by 1. +The array will become [3, 1, 0, 0], which is not a Zero Array. +``` ## 结语 diff --git a/leetcode/3301-3400/3355.Zero-Array-Transformation-I/Solution.go b/leetcode/3301-3400/3355.Zero-Array-Transformation-I/Solution.go index d115ccf5..eda1d2ad 100644 --- a/leetcode/3301-3400/3355.Zero-Array-Transformation-I/Solution.go +++ b/leetcode/3301-3400/3355.Zero-Array-Transformation-I/Solution.go @@ -1,5 +1,23 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(nums []int, queries [][]int) bool { + deltaArray := make([]int, len(nums)+1) + for _, query := range queries { + left := query[0] + right := query[1] + deltaArray[left] += 1 + deltaArray[right+1] -= 1 + } + operationCounts := make([]int, len(deltaArray)) + currentOperations := 0 + for i, delta := range deltaArray { + currentOperations += delta + operationCounts[i] = currentOperations + } + for i := 0; i < len(nums); i++ { + if operationCounts[i] < nums[i] { + return false + } + } + return true } diff --git a/leetcode/3301-3400/3355.Zero-Array-Transformation-I/Solution_test.go b/leetcode/3301-3400/3355.Zero-Array-Transformation-I/Solution_test.go index 14ff50eb..4c83e372 100644 --- a/leetcode/3301-3400/3355.Zero-Array-Transformation-I/Solution_test.go +++ b/leetcode/3301-3400/3355.Zero-Array-Transformation-I/Solution_test.go @@ -9,31 +9,31 @@ import ( func TestSolution(t *testing.T) { // 测试用例 cases := []struct { - name string - inputs bool - expect bool + name string + inputs []int + queries [][]int + expect bool }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{1, 0, 1}, [][]int{{0, 2}}, true}, + {"TestCase2", []int{4, 3, 2, 1}, [][]int{{1, 3}, {0, 2}}, false}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.inputs, c.queries) 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.inputs, c.queries) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }