mirror of
				https://gitee.com/coder-xiaomo/leetcode-problemset
				synced 2025-11-04 03:33:12 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			35 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			35 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
<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>
 | 
						|
<p><strong>Example 1:</strong></p>
 | 
						|
 | 
						|
<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>
 | 
						|
 | 
						|
<p><strong>Example 2:</strong></p>
 | 
						|
 | 
						|
<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>
 |