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

Posted Asurudo Jyo の 倉 庫

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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             else
14             {
15                 for(auto d:nums)
16                     result += abs(d-nums[nums.size()/2]);
17                 int tmp = result;
18                 result = 0;
19                 for(auto d:nums)
20                     result += abs(d-nums[nums.size()/2-1]);
21                 result = min(result,tmp);
22             }
23             return result;
24         }
25 };

 

以上是关于Leetcode-462 Minimum Moves to Equal Array Elements II(最少移动次数使数组元素相等 II)的主要内容,如果未能解决你的问题,请参考以下文章

leetcode462

LeetCode --- 1217. Minimum Cost to Move Chips to The Same Position 解题报告

LeetCode --- 1217. Minimum Cost to Move Chips to The Same Position 解题报告

Leetcode 462.最少移动次数使数组元素相等

Leetcode 1769. Minimum Number of Operations to Move All Balls to Each Box

vue-transition-move