https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/

package com.company;

import java.util.*;

// https://discuss.leetcode.com/topic/68736/java-just-like-meeting-point-problem
// 以上是整体解法,获得中位数中位数就可以获得结果 (just like meeting point problem) // https://discuss.leetcode.com/topic/68758/java-o-n-time-using-quickselect/2
// 以上解法是 O(n) 获得中位数的方法 (其实是获得第K位结果的方法, Quick-select) class Solution {
// 对应上面的第一种方法
public int minMoves2_old(int[] nums) {
Arrays.sort(nums);
int i = , j = nums.length - ;
int ret = ;
while (i < j) {
ret += nums[j] - nums[i];
i++;
j--;
}
return ret;
} // 对应上面的第二种方法
public int minMoves2(int[] nums) {
int mid = getPos(nums, nums.length/+, , nums.length-);
int ret = ;
for (int i=; i<nums.length; i++) {
ret += Math.abs(nums[i]-mid);
}
return ret;
} private int getPos(int[] nums, int k, int start, int end) {
int pivot = nums[end];
int left = start;
int right = end; while (left <= right) {
while (left <= right && nums[left] < pivot) left++;
while (left <= right && nums[right] >= pivot) right--;
if (left > right) {
break;
}
swap(nums, left, right);
}
swap(nums, left, end);
if (k == left+) {
return nums[left];
}
if (k < left+) {
return getPos(nums, k, start, left-);
}
else {
return getPos(nums, k, left+, end);
} } private void swap(int[] nums, int a, int b) {
int tmp = nums[a];
nums[a] = nums[b];
nums[b] = tmp;
} } public class Main { public static void main(String[] args) throws InterruptedException { System.out.println("Hello!");
Solution solution = new Solution(); // Your Codec object will be instantiated and called as such:
int[] nums = {,,};
int ret = solution.minMoves2(nums);
System.out.printf("ret:%d\n", ret); System.out.println(); } }

minimum-moves-to-equal-array-elements-ii(好)的更多相关文章

  1. Leetcode-462 Minimum Moves to Equal Array Elements II

    #462.   Minimum Moves to Equal Array Elements II Given a non-empty integer array, find the minimum n ...

  2. [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 element ...

  3. LeetCode Minimum Moves to Equal Array Elements II

    原题链接在这里:https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/ 题目: Given a non-empt ...

  4. 【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 element ...

  5. [Swift]LeetCode462. 最少移动次数使数组元素相等 II | Minimum Moves to Equal Array Elements II

    Given a non-empty integer array, find the minimum number of moves required to make all array element ...

  6. 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 element ...

  7. 【LeetCode】462. Minimum Moves to Equal Array Elements II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:排序 方法二:直接找中位数 日期 题目地址: ...

  8. 462 Minimum Moves to Equal Array Elements II 最少移动次数使数组元素相等 II

    给定一个非空整数数组,找到使所有数组元素相等所需的最小移动数,其中每次移动可将选定的一个元素加1或减1. 您可以假设数组的长度最多为10000.例如:输入:[1,2,3]输出:2说明:只有两个动作是必 ...

  9. LeetCode Minimum Moves to Equal Array Elements

    原题链接在这里:https://leetcode.com/problems/minimum-moves-to-equal-array-elements/ 题目: Given a non-empty i ...

  10. 453. Minimum Moves to Equal Array Elements 一次改2个数,变成统一的

    [抄题]: Given a non-empty integer array of size n, find the minimum number of moves required to make a ...

随机推荐

  1. BZOJ 5334: [Tjoi2018]数学计算

    线段树裸题 难度在于认识到这个没法线性做 #include<cstdio> using namespace std; int n,mod,tr[400005]; void insert(i ...

  2. managed unmanaged

    Enable function-level control for compiling functions as managed or unmanaged.     #pragma managed # ...

  3. 编程哲学之 C# 篇:007——如何创造万物

    上帝拥有创建万物的能力,本文介绍创造万物的道,让你也拥有上帝般创造万物的能力! 道 中国哲学家,道家学派创始人--老子,在<道德经>写到: 道生一,一生二,二生三,三生万物 那么,是什么 ...

  4. linux随笔4

    vim编辑器: 启动vim编辑器,只需键入vim 和希望编辑的文件:vim mongo.sh 如果文件存在,将显示整个内容显示到进行编辑的缓冲区,如果文件不存在,打开一个新的缓冲区进行编辑. 内容未占 ...

  5. [git 学习篇] --创建git创库

    http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/0013743256916071d ...

  6. [python篇][其他] python博客学习汇总

    http://blog.csdn.net/zhangxinrun/article/details/8141913

  7. DS博客作业-05--树

    1.本周学习总结  1.1思维导图  1.2学习体会 1.课堂上的知识也很难听懂,打代码就更难听懂了,真的需要不断练习代码. 2.在学习本章的内容中,一开始只是理解了概念,在真正做题中,一点思路都没有 ...

  8. hihoCoder #1157 建造金字塔

    这道题我想了一天才想清楚做法.AC 了之后去看别人写的题解,都是三言两语意识流式描述,我并不能读懂.我觉得很自卑,为何人家解这道题如此轻松.不过,我还是决定把我的解法写下来,并且一定要写清楚. 思路 ...

  9. Spring Boot 必须先说说 Spring 框架!

    现在 Spring Boot 非常火,各种技术文章,各种付费教程,多如牛毛,可能还有些不知道 Spring Boot 的,那它到底是什么呢?有什么用?今天给大家详细介绍一下. Spring Boot ...

  10. 开源编辑器ueditor

    http://ueditor.baidu.com/website/onlinedemo.html