Skip to content

Commit 5eccc40

Browse files
committed
Add solution and test-cases for problem 3362
1 parent a01e89c commit 5eccc40

File tree

3 files changed

+105
-24
lines changed

3 files changed

+105
-24
lines changed

leetcode/3301-3400/3362.Zero-Array-Transformation-III/README.md

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,55 @@
11
# [3362.Zero Array Transformation III][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+
Each `queries[i]` represents the following action on `nums`:
7+
8+
- Decrement the value at each index in the range `[li, ri]` in nums by **at most** `1`.
9+
- The amount by which the value is decremented can be chosen **independently** for each index.
10+
11+
A **Zero Array** is an array with all its elements equal to 0.
12+
13+
Return the **maximum** number of elements that can be removed from `queries`, such that `nums` can still be converted to a **zero array** using the remaining queries. If it is not possible to convert `nums` to a **zero array**, return -1.
714

815
**Example 1:**
916

1017
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
18+
Input: nums = [2,0,2], queries = [[0,2],[0,2],[1,1]]
19+
20+
Output: 1
21+
22+
Explanation:
23+
24+
After removing queries[2], nums can still be converted to a zero array.
25+
26+
Using queries[0], decrement nums[0] and nums[2] by 1 and nums[1] by 0.
27+
Using queries[1], decrement nums[0] and nums[2] by 1 and nums[1] by 0.
28+
```
29+
30+
**Example 2:**
31+
1332
```
33+
Input: nums = [1,1,1,1], queries = [[1,3],[0,2],[1,3],[1,2]]
1434
15-
## 题意
16-
> ...
35+
Output: 2
1736
18-
## 题解
37+
Explanation:
1938
20-
### 思路1
21-
> ...
22-
Zero Array Transformation III
23-
```go
39+
We can remove queries[2] and queries[3].
2440
```
2541

42+
**Example 3:**
43+
44+
```
45+
Input: nums = [1,2,3,4], queries = [[0,3]]
46+
47+
Output: -1
48+
49+
Explanation:
50+
51+
nums cannot be converted to a zero array even after using all the queries.
52+
```
2653

2754
## 结语
2855

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,58 @@
11
package Solution
22

3-
func Solution(x bool) bool {
3+
import (
4+
"container/heap"
5+
"sort"
6+
)
7+
8+
func Solution(nums []int, queries [][]int) int {
9+
sort.Slice(queries, func(i, j int) bool {
10+
return queries[i][0] < queries[j][0]
11+
})
12+
pq := &Heap{}
13+
heap.Init(pq)
14+
deltaArray := make([]int, len(nums)+1)
15+
operations := 0
16+
17+
for i, j := 0, 0; i < len(nums); i++ {
18+
operations += deltaArray[i]
19+
for j < len(queries) && queries[j][0] == i {
20+
heap.Push(pq, queries[j][1])
21+
j++
22+
}
23+
for operations < nums[i] && pq.Len() > 0 && (*pq)[0] >= i {
24+
operations += 1
25+
deltaArray[heap.Pop(pq).(int)+1] -= 1
26+
}
27+
if operations < nums[i] {
28+
return -1
29+
}
30+
}
31+
return pq.Len()
32+
}
33+
34+
type Heap []int
35+
36+
func (h Heap) Len() int {
37+
return len(h)
38+
}
39+
40+
func (h Heap) Less(i, j int) bool {
41+
return h[i] > h[j]
42+
}
43+
44+
func (h Heap) Swap(i, j int) {
45+
h[i], h[j] = h[j], h[i]
46+
}
47+
48+
func (h *Heap) Push(x interface{}) {
49+
*h = append(*h, x.(int))
50+
}
51+
52+
func (h *Heap) Pop() interface{} {
53+
old := *h
54+
n := len(old)
55+
x := old[n-1]
56+
*h = old[0 : n-1]
457
return x
558
}

leetcode/3301-3400/3362.Zero-Array-Transformation-III/Solution_test.go

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

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.nums, c.queries)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
28+
c.expect, got, c.nums, c.queries)
2829
}
2930
})
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)