diff --git a/leetcode/1501-1600/1526.Minimum-Number-of-Increments-on-Subarrays-to-Form-a-Target-Array/README.md b/leetcode/1501-1600/1526.Minimum-Number-of-Increments-on-Subarrays-to-Form-a-Target-Array/README.md index 05c1a0ab9..6b97c2237 100755 --- a/leetcode/1501-1600/1526.Minimum-Number-of-Increments-on-Subarrays-to-Form-a-Target-Array/README.md +++ b/leetcode/1501-1600/1526.Minimum-Number-of-Increments-on-Subarrays-to-Form-a-Target-Array/README.md @@ -1,28 +1,41 @@ # [1526.Minimum Number of Increments on Subarrays to Form a Target Array][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 +ou are given an integer array `target`. You have an integer array `initial` of the same size as `target` with all elements initially zeros. + +In one operation you can choose **any** subarray from `initial` and increment each value by one. + +Return the minimum number of operations to form a `target` array from `initial`. + +The test cases are generated so that the answer fits in a 32-bit integer. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: target = [1,2,3,2,1] +Output: 3 +Explanation: We need at least 3 operations to form the target array from the initial array. +[0,0,0,0,0] increment 1 from index 0 to 4 (inclusive). +[1,1,1,1,1] increment 1 from index 1 to 3 (inclusive). +[1,2,2,2,1] increment 1 at index 2. +[1,2,3,2,1] target array is formed. ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -Minimum Number of Increments on Subarrays to Form a Target Array -```go ``` +Input: target = [3,1,1,2] +Output: 4 +Explanation: [0,0,0,0] -> [1,1,1,1] -> [1,1,1,2] -> [2,1,1,2] -> [3,1,1,2] +``` + +**Example 3:** +``` +Input: target = [3,1,5,4,2] +Output: 7 +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]. +``` ## 结语 diff --git a/leetcode/1501-1600/1526.Minimum-Number-of-Increments-on-Subarrays-to-Form-a-Target-Array/Solution.go b/leetcode/1501-1600/1526.Minimum-Number-of-Increments-on-Subarrays-to-Form-a-Target-Array/Solution.go index d115ccf5e..64faff70f 100644 --- a/leetcode/1501-1600/1526.Minimum-Number-of-Increments-on-Subarrays-to-Form-a-Target-Array/Solution.go +++ b/leetcode/1501-1600/1526.Minimum-Number-of-Increments-on-Subarrays-to-Form-a-Target-Array/Solution.go @@ -1,5 +1,10 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(target []int) int { + n := len(target) + ans := target[0] + for i := 1; i < n; i++ { + ans += max(target[i]-target[i-1], 0) + } + return ans } diff --git a/leetcode/1501-1600/1526.Minimum-Number-of-Increments-on-Subarrays-to-Form-a-Target-Array/Solution_test.go b/leetcode/1501-1600/1526.Minimum-Number-of-Increments-on-Subarrays-to-Form-a-Target-Array/Solution_test.go index 14ff50eb4..383d2d162 100644 --- a/leetcode/1501-1600/1526.Minimum-Number-of-Increments-on-Subarrays-to-Form-a-Target-Array/Solution_test.go +++ b/leetcode/1501-1600/1526.Minimum-Number-of-Increments-on-Subarrays-to-Form-a-Target-Array/Solution_test.go @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs []int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{1, 2, 3, 2, 1}, 3}, + {"TestCase2", []int{3, 1, 1, 2}, 4}, + {"TestCase3", []int{3, 1, 5, 4, 2}, 7}, } // 开始测试 @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }