Leetcode: Minimum Moves to Equal Array Elements II

Given a non-empty integer array, find the minimum number of moves required to make all array elements equal, where a move is incrementing a selected element by 1 or decrementing a selected element by 1.

You may assume the array‘s length is at most 10,000.

Example:

Input:
[1,2,3]

Output:
2

Explanation:
Only two moves are needed (remember each move increments or decrements one element):

[1,2,3]  =>  [2,2,3]  =>  [2,2,2]

Just like meeting point problem, find the median elements in the array after sorting, so

Solution 1: Sort, find the median: O(NlogN)

 1 public class Solution {
 2     public int minMoves2(int[] nums) {
 3         Arrays.sort(nums);
 4         int i = 0, j = nums.length-1;
 5         int count = 0;
 6         while(i < j){
 7             count += nums[j]-nums[i];
 8             i++;
 9             j--;
10         }
11         return count;
12     }
13 }
 1 public class Solution {
 2     public int minMoves2(int[] nums) {
 3         Arrays.sort(nums);
 4         int median = nums[nums.length/2];
 5         int res = 0;
 6         for (int num : nums) {
 7             res += Math.max(num-median, median-num);
 8         }
 9         return res;
10     }
11 }

Solution 2: Quick Select

This solution relies on the fact that if we increment/decrement each element to the median of all the elements, the optimal number of moves is necessary. The median of all elements can be found in expected O(n) time using QuickSelect (or O(n)

 1 public class Solution {
 2     public int minMoves2(int[] nums) {
 3         int median = findMedian(nums, 0, nums.length-1, nums.length/2+1);
 4         int res = 0;
 5         for (int num : nums) {
 6             res += Math.abs(num-median);
 7         }
 8         return res;
 9     }
10
11     public int findMedian(int[] nums, int l, int r, int k) {
12         int start = l;
13         int end = r;
14         int pivot = r;
15         while (start < end) {
16             while (start < end && nums[start] < nums[pivot]) {
17                 start++;
18             }
19             while (start < end && nums[end] >= nums[pivot]) {
20                 end--;
21             }
22             if (start == end) break;
23             swap(nums, start, end);
24         }
25         swap(nums, start, pivot);
26         if (start+1 == k) return nums[start];
27         else if (k < start+1) return findMedian(nums, l, start-1, k);
28         else return findMedian(nums, start+1, r, k);
29     }
30
31     public void swap(int[] nums, int i, int j) {
32         int temp = nums[i];
33         nums[i] = nums[j];
34         nums[j] = temp;
35     }
36 }
时间: 2024-10-13 20:24:08

Leetcode: Minimum Moves to Equal Array Elements II的相关文章

[Math_Medium]462. Minimum Moves to Equal Array Elements II

原题462. Minimum Moves to Equal Array Elements II 题目大意: 给你一串数字,对于每个数字,一次性可以移动一步(数值增加1或者减小1),请问如何在最小的步伐数内使所有的数字都相等 Example Input: [1,2,3] Output: 2 Explanation: Only two moves are needed (remember each move increments or decrements one element): [1,2,3]

LeetCode 462. Minimum Moves to Equal Array Elements II

Given a non-empty integer array, find the minimum number of moves required to make all array elements equal, where a move is incrementing a selected element by 1 or decrementing a selected element by 1. You may assume the array's length is at most 10

【LeetCode】462. Minimum Moves to Equal Array Elements II

Given a non-empty integer array, find the minimum number of moves required to make all array elements equal, where a move is incrementing a selected element by 1 or decrementing a selected element by 1. You may assume the array's length is at most 10

[LeetCode] Minimum Moves to Equal Array Elements

Given a non-empty integer array of size n, find the minimum number of moves required to make all array elements equal, where a move is incrementing n - 1 elements by 1. Example: Input: [1,2,3] Output: 3 Explanation: Only three moves are needed (remem

462. Minimum Moves to Equal Array Elements II

Given a non-empty integer array, find the minimum number of moves required to make all array elements equal, where a move is incrementing a selected element by 1 or decrementing a selected element by 1. You may assume the array's length is at most 10

[LeetCode] Minimum Moves to Equal Array Elements 最少移动次数使数组元素相等

Given a non-empty integer array of size n, find the minimum number of moves required to make all array elements equal, where a move is incrementing n - 1 elements by 1. Example: Input: [1,2,3] Output: 3 Explanation: Only three moves are needed (remem

Leetcode-462 Minimum Moves to Equal Array Elements II(最少移动次数使数组元素相等 II)

1 class Solution 2 { 3 public: 4 int minMoves2(vector<int>& nums) 5 { 6 sort(nums.begin(),nums.end(),less<int>()); 7 int result = 0; 8 if((nums.size()&0x1)==1) 9 { 10 for(auto d:nums) 11 result += abs(d-nums[nums.size()/2]); 12 } 13 el

453. Minimum Moves to Equal Array Elements(LeetCode)

Given a non-empty integer array of size n, find the minimum number of moves required to make all array elements equal, where a move is incrementing n - 1 elements by 1. Example: Input: [1,2,3] Output: 3 Explanation: Only three moves are needed (remem

LeetCode 453. Minimum Moves to Equal Array Elements C#

Given a non-empty integer array of size n, find the minimum number of moves required to make all array elements equal, where a move is incrementing n - 1 elements by 1. Example: Input: [1,2,3] Output: 3 Explanation: Only three moves are needed (remem