★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-uabtmyzi-ks.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
Given an array nums
of integers, a move consists of choosing any element and decreasing it by 1.git
An array A
is a zigzag array if either:github
A[0] > A[1] < A[2] > A[3] < A[4] > ...
A[0] < A[1] > A[2] < A[3] > A[4] < ...
Return the minimum number of moves to transform the given array nums
into a zigzag array. 數組
Example 1:微信
Input: nums = [1,2,3] Output: 2 Explanation: We can decrease 2 to 0 or 3 to 1.
Example 2:ide
Input: nums = [9,6,1,6,2] Output: 4
Constraints:spa
1 <= nums.length <= 1000
1 <= nums[i] <= 1000
給你一個整數數組 nums
,每次 操做 會從中選擇一個元素並 將該元素的值減小 1。code
若是符合下列狀況之一,則數組 A
就是 鋸齒數組:orm
A[0] > A[1] < A[2] > A[3] < A[4] > ...
A[0] < A[1] > A[2] < A[3] > A[4] < ...
返回將數組 nums
轉換爲鋸齒數組所需的最小操做次數。 htm
示例 1:
輸入:nums = [1,2,3] 輸出:2 解釋:咱們能夠把 2 遞減到 0,或把 3 遞減到 1。
示例 2:
輸入:nums = [9,6,1,6,2] 輸出:4
提示:
1 <= nums.length <= 1000
1 <= nums[i] <= 1000
1 class Solution { 2 func movesToMakeZigzag(_ nums: [Int]) -> Int { 3 if nums.count < 3 { 4 return 0 5 } 6 7 let n = nums.count 8 var result1 = 0 9 var result2 = 0 10 11 var i = 1 12 while i < n { 13 var minValue = min(nums[i-1], i+1 < n ? nums[i+1] : 1000) 14 if minValue <= nums[i] { 15 result1 += (nums[i]-minValue+1) 16 } 17 i += 2 18 } 19 20 i = 0 21 while i < n { 22 var minValue = min(i > 0 ? nums[i-1] : 1000, i+1 < n ? nums[i+1] : 1000) 23 if minValue <= nums[i] { 24 result2 += (nums[i]-minValue+1) 25 } 26 i += 2 27 } 28 29 return min(result1, result2) 30 } 31 }
8ms
1 class Solution { 2 func movesToMakeZigzag(_ nums: [Int]) -> Int { 3 var result = [0, 0] 4 let n = nums.count 5 var left = 0 6 var right = nums.count - 1 7 8 for i in 0..<n { 9 left = i > 0 ? nums[i-1] : 1001 10 right = i + 1 < n ? nums[i+1] : 1001 11 result[i%2] += max(0, nums[i] - min(left, right) + 1) 12 } 13 return result.min()! 14 } 15 }
Runtime: 8 ms
1 class Solution { 2 func movesToMakeZigzag(_ nums: [Int]) -> Int { 3 var n:Int = nums.count 4 var j:Int = 0 5 var s:Int = 0 6 var t:Int = 0 7 for i in stride(from:0,to:n,by:2) 8 { 9 j = 0 10 if i != 0 11 { 12 j = max(j,nums[i]-nums[i-1]+1) 13 } 14 if i + 1 < n 15 { 16 j = max(j,nums[i]-nums[i+1]+1) 17 } 18 s += j 19 } 20 for i in stride(from:1,to:n,by:2) 21 { 22 j = 0 23 if i != 0 24 { 25 j = max(j,nums[i]-nums[i-1]+1) 26 } 27 if i + 1 < n 28 { 29 j = max(j,nums[i]-nums[i+1]+1) 30 } 31 t += j 32 } 33 return min(s,t) 34 } 35 }
1 class Solution { 2 func movesToMakeZigzag(_ nums: [Int]) -> Int { 3 var movesA = 0 4 var movesB = 0 5 var array = nums 6 for i in stride(from: 1, to: array.count, by: 2) { 7 while array[i - 1] <= array[i] { 8 movesA += 1 9 array[i] -= 1 10 } 11 12 guard (i + 1) < array.count else { break } 13 14 while array[i + 1] <= array[i] { 15 movesA += 1 16 array[i] -= 1 17 } 18 } 19 array = nums 20 for i in stride(from: 1, to: array.count, by: 2) { 21 while array[i - 1] >= array[i] { 22 movesB += 1 23 array[i - 1] -= 1 24 } 25 26 guard (i + 1) < array.count else { break } 27 28 while array[i + 1] >= array[i] { 29 movesB += 1 30 array[i + 1] -= 1 31 } 32 } 33 34 return movesA < movesB ? movesA : movesB 35 } 36 }