Class Solution
java.lang.Object
g2101_2200.s2170_minimum_operations_to_make_the_array_alternating.Solution
2170 - Minimum Operations to Make the Array Alternating.<p>Medium</p>
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> consisting of <code>n</code> positive integers.</p>
<p>The array <code>nums</code> is called <strong>alternating</strong> if:</p>
<ul>
<li><code>nums[i - 2] == nums[i]</code>, where <code>2 <= i <= n - 1</code>.</li>
<li><code>nums[i - 1] != nums[i]</code>, where <code>1 <= i <= n - 1</code>.</li>
</ul>
<p>In one <strong>operation</strong> , you can choose an index <code>i</code> and <strong>change</strong> <code>nums[i]</code> into <strong>any</strong> positive integer.</p>
<p>Return <em>the <strong>minimum number of operations</strong> required to make the array alternating</em>.</p>
<p><strong>Example 1:</strong></p>
<p><strong>Input:</strong> nums = [3,1,3,2,4,3]</p>
<p><strong>Output:</strong> 3</p>
<p><strong>Explanation:</strong></p>
<p>One way to make the array alternating is by converting it to [3,1,3, <strong>1</strong> , <strong>3</strong> , <strong>1</strong> ].</p>
<p>The number of operations required in this case is 3.</p>
<p>It can be proven that it is not possible to make the array alternating in less than 3 operations.</p>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> nums = [1,2,2,2,2]</p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>One way to make the array alternating is by converting it to [1,2, <strong>1</strong> ,2, <strong>1</strong> ].</p>
<p>The number of operations required in this case is 2.</p>
<p>Note that the array cannot be converted to [<strong>2</strong> ,2,2,2,2] because in this case nums[0] == nums[1] which violates the conditions of an alternating array.</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
minimumOperations
public int minimumOperations(int[] nums)
-