2022-03-27 20:37:52 +08:00
|
|
|
<p>Given an array <code>nums</code> of integers, a <em>move</em> consists of choosing any element and <strong>decreasing it by 1</strong>.</p>
|
|
|
|
|
|
|
|
<p>An array <code>A</code> is a <em>zigzag array</em> if either:</p>
|
|
|
|
|
|
|
|
<ul>
|
|
|
|
<li>Every even-indexed element is greater than adjacent elements, ie. <code>A[0] > A[1] < A[2] > A[3] < A[4] > ...</code></li>
|
|
|
|
<li>OR, every odd-indexed element is greater than adjacent elements, ie. <code>A[0] < A[1] > A[2] < A[3] > A[4] < ...</code></li>
|
|
|
|
</ul>
|
|
|
|
|
|
|
|
<p>Return the minimum number of moves to transform the given array <code>nums</code> into a zigzag array.</p>
|
|
|
|
|
|
|
|
<p> </p>
|
2023-12-09 18:42:21 +08:00
|
|
|
<p><strong class="example">Example 1:</strong></p>
|
2022-03-27 20:37:52 +08:00
|
|
|
|
|
|
|
<pre>
|
|
|
|
<strong>Input:</strong> nums = [1,2,3]
|
|
|
|
<strong>Output:</strong> 2
|
|
|
|
<strong>Explanation:</strong> We can decrease 2 to 0 or 3 to 1.
|
|
|
|
</pre>
|
|
|
|
|
2023-12-09 18:42:21 +08:00
|
|
|
<p><strong class="example">Example 2:</strong></p>
|
2022-03-27 20:37:52 +08:00
|
|
|
|
|
|
|
<pre>
|
|
|
|
<strong>Input:</strong> nums = [9,6,1,6,2]
|
|
|
|
<strong>Output:</strong> 4
|
|
|
|
</pre>
|
|
|
|
|
|
|
|
<p> </p>
|
|
|
|
<p><strong>Constraints:</strong></p>
|
|
|
|
|
|
|
|
<ul>
|
|
|
|
<li><code>1 <= nums.length <= 1000</code></li>
|
|
|
|
<li><code>1 <= nums[i] <= 1000</code></li>
|
|
|
|
</ul>
|