java.lang.Object
g2601_2700.s2602_minimum_operations_to_make_all_array_elements_equal.Solution

public class Solution extends Object
2602 - Minimum Operations to Make All Array Elements Equal.<p>Medium</p> <p>You are given an array <code>nums</code> consisting of positive integers.</p> <p>You are also given an integer array <code>queries</code> of size <code>m</code>. For the <code>i<sup>th</sup></code> query, you want to make all of the elements of <code>nums</code> equal to <code>queries[i]</code>. You can perform the following operation on the array <strong>any</strong> number of times:</p> <ul> <li><strong>Increase</strong> or <strong>decrease</strong> an element of the array by <code>1</code>.</li> </ul> <p>Return <em>an array</em> <code>answer</code> <em>of size</em> <code>m</code> <em>where</em> <code>answer[i]</code> <em>is the <strong>minimum</strong> number of operations to make all elements of</em> <code>nums</code> <em>equal to</em> <code>queries[i]</code>.</p> <p><strong>Note</strong> that after each query the array is reset to its original state.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> nums = [3,1,6,8], queries = [1,5]</p> <p><strong>Output:</strong> [14,10]</p> <p><strong>Explanation:</strong> For the first query we can do the following operations:</p> <ul> <li>Decrease nums[0] 2 times, so that nums = [1,1,6,8].</li> <li>Decrease nums[2] 5 times, so that nums = [1,1,1,8].</li> <li>Decrease nums[3] 7 times, so that nums = [1,1,1,1].</li> </ul> <p>So the total number of operations for the first query is 2 + 5 + 7 = 14.</p> <p>For the second query we can do the following operations:</p> <ul> <li>Increase nums[0] 2 times, so that nums = [5,1,6,8].</li> <li>Increase nums[1] 4 times, so that nums = [5,5,6,8].</li> <li>Decrease nums[2] 1 time, so that nums = [5,5,5,8].</li> <li>Decrease nums[3] 3 times, so that nums = [5,5,5,5].</li> </ul> <p>So the total number of operations for the second query is 2 + 4 + 1 + 3 = 10.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> nums = [2,9,6,3], queries = [10]</p> <p><strong>Output:</strong> [20]</p> <p><strong>Explanation:</strong> We can increase each value in the array to 10. The total number of operations will be 8 + 1 + 4 + 7 = 20.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>m == queries.length</code></li> <li><code>1 <= n, m <= 10<sup>5</sup></code></li> <li><code>1 <= nums[i], queries[i] <= 10<sup>9</sup></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • minOperations

      public List<Long> minOperations(int[] nums, int[] queries)