Skip to content

Commit 515d71e

Browse files
authored
Merge pull request #1305 from 0xff-dev/3495
Add solution and test-cases for problem 3495
2 parents 23166d0 + 06c9536 commit 515d71e

File tree

3 files changed

+73
-22
lines changed

3 files changed

+73
-22
lines changed

leetcode/3401-3500/3495.Minimum-Operations-to-Make-Array-Elements-Zero/README.md

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,57 @@
11
# [3495.Minimum Operations to Make Array Elements Zero][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 a 2D array `queries`, where `queries[i]` is of the form `[l, r]`. Each `queries[i]` defines an array of integers `nums` consisting of elements ranging from `l` to `r`, both **inclusive**.
5+
6+
In one operation, you can:
7+
8+
- Select two integers `a` and `b` from the array.
9+
- Replace them with `floor(a / 4)` and `floor(b / 4)`.
10+
11+
Your task is to determine the **minimum** number of operations required to reduce all elements of the array to zero for each query. Return the sum of the results for all queries.
712

813
**Example 1:**
914

1015
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
16+
Input: queries = [[1,2],[2,4]]
17+
18+
Output: 3
19+
20+
Explanation:
1421
15-
## 题意
16-
> ...
22+
For queries[0]:
1723
18-
## 题解
24+
The initial array is nums = [1, 2].
25+
In the first operation, select nums[0] and nums[1]. The array becomes [0, 0].
26+
The minimum number of operations required is 1.
27+
For queries[1]:
1928
20-
### 思路1
21-
> ...
22-
Minimum Operations to Make Array Elements Zero
23-
```go
29+
The initial array is nums = [2, 3, 4].
30+
In the first operation, select nums[0] and nums[2]. The array becomes [0, 3, 1].
31+
In the second operation, select nums[1] and nums[2]. The array becomes [0, 0, 0].
32+
The minimum number of operations required is 2.
33+
The output is 1 + 2 = 3.
2434
```
2535

36+
**Example 2:**
37+
38+
```
39+
Input: queries = [[2,6]]
40+
41+
Output: 4
42+
43+
Explanation:
44+
45+
For queries[0]:
46+
47+
The initial array is nums = [2, 3, 4, 5, 6].
48+
In the first operation, select nums[0] and nums[3]. The array becomes [0, 3, 4, 1, 6].
49+
In the second operation, select nums[2] and nums[4]. The array becomes [0, 3, 1, 1, 1].
50+
In the third operation, select nums[1] and nums[2]. The array becomes [0, 0, 0, 1, 1].
51+
In the fourth operation, select nums[3] and nums[4]. The array becomes [0, 0, 0, 0, 0].
52+
The minimum number of operations required is 4.
53+
The output is 4.
54+
```
2655

2756
## 结语
2857

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

3-
func Solution(x bool) bool {
4-
return x
3+
func get(num int) int64 {
4+
var cnt int64
5+
i := 1
6+
base := 1
7+
8+
for base <= num {
9+
end := base*2 - 1
10+
if end > num {
11+
end = num
12+
}
13+
cnt += int64((i+1)/2) * int64(end-base+1)
14+
i++
15+
base *= 2
16+
}
17+
return cnt
18+
}
19+
20+
func Solution(queries [][]int) int64 {
21+
var res int64
22+
for _, q := range queries {
23+
count1 := get(q[1])
24+
count2 := get(q[0] - 1)
25+
res += (count1 - count2 + 1) / 2
26+
}
27+
return res
528
}

leetcode/3401-3500/3495.Minimum-Operations-to-Make-Array-Elements-Zero/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs [][]int
14+
expect int64
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", [][]int{{1, 2}, {2, 4}}, 3},
17+
{"TestCase2", [][]int{{2, 6}}, 4},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)