minimum-moves-to-equal-array-elements-ii(好)
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(好)的更多相关文章
- 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 ...
- [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 ...
- LeetCode Minimum Moves to Equal Array Elements II
原题链接在这里:https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/ 题目: Given a non-empt ...
- 【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 ...
- [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 ...
- 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 ...
- 【LeetCode】462. Minimum Moves to Equal Array Elements II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:排序 方法二:直接找中位数 日期 题目地址: ...
- 462 Minimum Moves to Equal Array Elements II 最少移动次数使数组元素相等 II
给定一个非空整数数组,找到使所有数组元素相等所需的最小移动数,其中每次移动可将选定的一个元素加1或减1. 您可以假设数组的长度最多为10000.例如:输入:[1,2,3]输出:2说明:只有两个动作是必 ...
- LeetCode Minimum Moves to Equal Array Elements
原题链接在这里:https://leetcode.com/problems/minimum-moves-to-equal-array-elements/ 题目: Given a non-empty i ...
- 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 ...
随机推荐
- C++智能指针实现
#include <iostream> #include <string> #define unsigned int size_t using namespace std; / ...
- html5/css3常考面试题
一.HTML5 CSS3 CSS3有哪些新特性? 1. CSS3实现圆角(border-radius),阴影(box-shadow), 2. 对文字加特效(text-shadow.),线性渐变(gra ...
- luogu1736 创意吃鱼法
好的题解使人一下就懂啊-- s1[i][j]表示(i,j)最多向左(或右)延伸多少个格子,使这些格子中的数都是0(不包括(i,j)) s2[i][j]表示(i,j)最多向上延伸多少个格子,使这些格子中 ...
- javascript学习笔记 - 面向对象 理解对象
ECMAScript 中有两种属性:数据属性和访问器属性 一 属性类型 1.数据属性.数据属性有4个描述其行为的特性 [[Configurable]]表示能否通过delete删除属性从而重新定义属性: ...
- Oracle PL/SQL 语言(Procedural Language/SQL)
Oracle PL/SQL 语言(Procedural Language/SQL)是结合了结构化查询与 Oracle 自身过程控制为一体的强大语言,PL/SQL 不但支持更多的数据类型,拥有自身的变量 ...
- Linux Shell系列教程之(二)第一个Shell脚本
本文是Linux Shell系列教程的第(二)篇,更多shell教程请看:Linux Shell系列教程 通过上一篇教程的学习,相信大家已经能够对shell建立起一个大体的印象了,接下来,我们通过一个 ...
- BZOJ4176 Lucas的数论 【莫比乌斯反演 + 杜教筛】
题目 去年的Lucas非常喜欢数论题,但是一年以后的Lucas却不那么喜欢了. 在整理以前的试题时,发现了这样一道题目"求Sigma(f(i)),其中1<=i<=N", ...
- BZOJ1064 [Noi2008]假面舞会 【dfs】
题目 一年一度的假面舞会又开始了,栋栋也兴致勃勃的参加了今年的舞会.今年的面具都是主办方特别定制的.每个参加舞会的人都可以在入场时选择一 个自己喜欢的面具.每个面具都有一个编号,主办方会把此编号告诉拿 ...
- bzoj 1061~1065【Noi2008】解题报告
这次Noi好像格外喜欢树形DpQAQ P.S.好像这次的题都与图有关QAQ bzoj1061[Noi2008]志愿者招募:上下界可行最小费用流 bzoj1062[Noi2008]糖果雨:数形结合&am ...
- Java24种设计模式的优点、缺点和适用环境总结
一.7个常用的面向对象设计原则 1.单一职责原则: 它是实现高内聚.低耦合的指导方针:一个对象应该只包含单一的职责,并且该职责被完整的封装在一个类中: 2.开闭原则: 指软件实体应尽量在不改变原有的代 ...