java.lang.Object
g2301_2400.s2366_minimum_replacements_to_sort_the_array.Solution

public class Solution extends Object
2366 - Minimum Replacements to Sort the Array.<p>Hard</p> <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. In one operation you can replace any element of the array with <strong>any two</strong> elements that <strong>sum</strong> to it.</p> <ul> <li>For example, consider <code>nums = [5,6,7]</code>. In one operation, we can replace <code>nums[1]</code> with <code>2</code> and <code>4</code> and convert <code>nums</code> to <code>[5,2,4,7]</code>.</li> </ul> <p>Return <em>the minimum number of operations to make an array that is sorted in <strong>non-decreasing</strong> order</em>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> nums = [3,9,3]</p> <p><strong>Output:</strong> 2</p> <p><strong>Explanation:</strong> Here are the steps to sort the array in non-decreasing order:</p> <ul> <li> <p>From [3,9,3], replace the 9 with 3 and 6 so the array becomes [3,3,6,3]</p> </li> <li> <p>From [3,3,6,3], replace the 6 with 3 and 3 so the array becomes [3,3,3,3,3]</p> </li> </ul> <p>There are 2 steps to sort the array in non-decreasing order. Therefore, we return 2.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> nums = [1,2,3,4,5]</p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong> The array is already in non-decreasing order. Therefore, we return 0.</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

    • minimumReplacement

      public long minimumReplacement(int[] nums)