Skip to content

Commit c14d016

Browse files
committed
Add solution and test-cases for problem 3355
1 parent a01e89c commit c14d016

File tree

3 files changed

+63
-26
lines changed

3 files changed

+63
-26
lines changed

leetcode/3301-3400/3355.Zero-Array-Transformation-I/README.md

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,47 @@
11
# [3355.Zero Array Transformation I][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given an integer array `nums` of length n and a 2D array `queries`, where `queries[i] = [li, ri]`.
5+
6+
For each `queries[i]`:
7+
8+
- Select a subset of indices within the range `[li, ri]` in nums.
9+
- Decrement the values at the selected indices by 1.
10+
11+
A **Zero Array** is an array where all elements are equal to 0.
12+
13+
Return `true` if it is possible to transform `nums` into a **Zero Array** after processing all the queries sequentially, otherwise return `false`.
714

815
**Example 1:**
916

1017
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
18+
Input: nums = [1,0,1], queries = [[0,2]]
1419
15-
## 题意
16-
> ...
20+
Output: true
1721
18-
## 题解
22+
Explanation:
1923
20-
### 思路1
21-
> ...
22-
Zero Array Transformation I
23-
```go
24+
For i = 0:
25+
Select the subset of indices as [0, 2] and decrement the values at these indices by 1.
26+
The array will become [0, 0, 0], which is a Zero Array.
2427
```
2528

29+
**Example 2:**
30+
31+
```
32+
Input: nums = [4,3,2,1], queries = [[1,3],[0,2]]
33+
34+
Output: false
35+
36+
Explanation:
37+
38+
For i = 0:
39+
Select the subset of indices as [1, 2, 3] and decrement the values at these indices by 1.
40+
The array will become [4, 2, 1, 0].
41+
For i = 1:
42+
Select the subset of indices as [0, 1, 2] and decrement the values at these indices by 1.
43+
The array will become [3, 1, 0, 0], which is not a Zero Array.
44+
```
2645

2746
## 结语
2847

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int, queries [][]int) bool {
4+
deltaArray := make([]int, len(nums)+1)
5+
for _, query := range queries {
6+
left := query[0]
7+
right := query[1]
8+
deltaArray[left] += 1
9+
deltaArray[right+1] -= 1
10+
}
11+
operationCounts := make([]int, len(deltaArray))
12+
currentOperations := 0
13+
for i, delta := range deltaArray {
14+
currentOperations += delta
15+
operationCounts[i] = currentOperations
16+
}
17+
for i := 0; i < len(nums); i++ {
18+
if operationCounts[i] < nums[i] {
19+
return false
20+
}
21+
}
22+
return true
523
}

leetcode/3301-3400/3355.Zero-Array-Transformation-I/Solution_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,31 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
inputs []int
14+
queries [][]int
15+
expect bool
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{1, 0, 1}, [][]int{{0, 2}}, true},
18+
{"TestCase2", []int{4, 3, 2, 1}, [][]int{{1, 3}, {0, 2}}, false},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.inputs, c.queries)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
27+
c.expect, got, c.inputs, c.queries)
2828
}
2929
})
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)