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. django_orm操作

    查询操作和性能优化 1.基本操作 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23   增   models.Tb1.object ...

  2. 贪心 - [POI2006]ORK-Ploughing

    [POI2006]ORK-Ploughing 描述 Byteasar 想耕种他那块矩形的田,他每次能耕种矩形的一边(上下左右都行),在他每次耕完后,剩下的田也一定是矩形,每块小区域边长为 1,耕地的长 ...

  3. CSS3中的border-radius兼容IE低版本解决方法

    ie-css3.htc先说道说道这斯是弄啥嘞ie-css3.htc是一个可以让IE浏览器支持部份CSS3属性的htc文件,不只是box-shadow,它还可以让你的IE浏览器支持圆角属性border- ...

  4. redhat linux 从/home目录扩展空间至/根目录

    查看分区大小 [root@easdb01 ~]# df -hFilesystem Size Used Avail Use% Mounted on/dev/mapper/vg_easdb01-lv_ro ...

  5. 设计模式(十九)观察者模式 Observer

    模拟场景: 甲方提供了一个气象站的接口,气象站上面装有:温度感应装置.湿度感应装置.气压感应装置. 现在我们是乙方,需要设计一个 WeatherData 对象,从气象站获取数据,并且利用这些数据,更新 ...

  6. [codevs2185]最长公共上升子序列

    [codevs2185]最长公共上升子序列 试题描述 熊大妈的奶牛在小沐沐的熏陶下开始研究信息题目.小沐沐先让奶牛研究了最长上升子序列,再让他们研究了最长公共子序列,现在又让他们要研究最长公共上升子序 ...

  7. Excel VBA基础教程

    https://www.w3cschool.cn/excelvba/excelvba-basics.html Excel VBA语言基础 VBA语言的基础认识 详解VBA编程是什么 excel处理录制 ...

  8. 【python接口自动化】logger

    #! /usr/bin/env python # coding=GBK import logging, os class Logger: def __init__(self, path, clevel ...

  9. 关于后台返回excel文件的问题

    一般情况ajax请求只能获取解析非流文件类型,而excel是流文件类型,这个时候获取到的数据会是一串乱码的字符串 想要下载这个excel文件,通过form表单模拟的方式可以解决 var form = ...

  10. uva 10828 高斯消元求数学期望

    Back to Kernighan-RitchieInput: Standard Input Output: Standard Output You must have heard the name ...