Skip to content

Commit 02b256d

Browse files
committed
Add solution and test-cases for problem 1526
1 parent f03226b commit 02b256d

File tree

3 files changed

+40
-22
lines changed

3 files changed

+40
-22
lines changed

leetcode/1501-1600/1526.Minimum-Number-of-Increments-on-Subarrays-to-Form-a-Target-Array/README.md

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,41 @@
11
# [1526.Minimum Number of Increments on Subarrays to Form a Target Array][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+
ou are given an integer array `target`. You have an integer array `initial` of the same size as `target` with all elements initially zeros.
5+
6+
In one operation you can choose **any** subarray from `initial` and increment each value by one.
7+
8+
Return the minimum number of operations to form a `target` array from `initial`.
9+
10+
The test cases are generated so that the answer fits in a 32-bit integer.
711

812
**Example 1:**
913

1014
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
15+
Input: target = [1,2,3,2,1]
16+
Output: 3
17+
Explanation: We need at least 3 operations to form the target array from the initial array.
18+
[0,0,0,0,0] increment 1 from index 0 to 4 (inclusive).
19+
[1,1,1,1,1] increment 1 from index 1 to 3 (inclusive).
20+
[1,2,2,2,1] increment 1 at index 2.
21+
[1,2,3,2,1] target array is formed.
1322
```
1423

15-
## 题意
16-
> ...
17-
18-
## 题解
24+
**Example 2:**
1925

20-
### 思路1
21-
> ...
22-
Minimum Number of Increments on Subarrays to Form a Target Array
23-
```go
2426
```
27+
Input: target = [3,1,1,2]
28+
Output: 4
29+
Explanation: [0,0,0,0] -> [1,1,1,1] -> [1,1,1,2] -> [2,1,1,2] -> [3,1,1,2]
30+
```
31+
32+
**Example 3:**
2533

34+
```
35+
Input: target = [3,1,5,4,2]
36+
Output: 7
37+
Explanation: [0,0,0,0,0] -> [1,1,1,1,1] -> [2,1,1,1,1] -> [3,1,1,1,1] -> [3,1,2,2,2] -> [3,1,3,3,2] -> [3,1,4,4,2] -> [3,1,5,4,2].
38+
```
2639

2740
## 结语
2841

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(target []int) int {
4+
n := len(target)
5+
ans := target[0]
6+
for i := 1; i < n; i++ {
7+
ans += max(target[i]-target[i-1], 0)
8+
}
9+
return ans
510
}

leetcode/1501-1600/1526.Minimum-Number-of-Increments-on-Subarrays-to-Form-a-Target-Array/Solution_test.go

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

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

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

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

0 commit comments

Comments
 (0)