java.lang.Object
g2001_2100.s2009_minimum_number_of_operations_to_make_array_continuous.Solution

public class Solution extends Object
2009 - Minimum Number of Operations to Make Array Continuous.<p>Hard</p> <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p> <p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p> <ul> <li>All elements in <code>nums</code> are <strong>unique</strong>.</li> <li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li> </ul> <p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong> , but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p> <p>Return <em>the <strong>minimum</strong> number of operations to make</em> <code>nums</code> <strong><em>continuous</em></strong>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> nums = [4,2,5,3]</p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong> nums is already continuous.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> nums = [1,2,3,5,6]</p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong> One possible solution is to change the last element to 4. The resulting array is [1,2,3,5,4], which is continuous.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> nums = [1,10,100,1000]</p> <p><strong>Output:</strong> 3</p> <p><strong>Explanation:</strong> One possible solution is to:</p> <ul> <li> <p>Change the second element to 2.</p> </li> <li> <p>Change the third element to 3.</p> </li> <li> <p>Change the fourth element to 4.</p> </li> </ul> <p>The resulting array is [1,2,3,4], which is continuous.</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>9</sup></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • minOperations

      public int minOperations(int[] nums)