[LeetCode]Minimum Moves to Equal Array Elements1,2
1.将每次n-1个数+1,转化为每次最大的数-1
public int minMoves(int[] nums) {
/*
看了看答案 ,很巧妙,最后的结果肯定是把数组元素都加到一个相同的值,
也就是每次把n-1个比较小的值+1,这样我们可以转化为每次把最大的数-1
直到减到最小值
*/
int min = Integer.MAX_VALUE;
int sum = 0;
for (int i = 0; i < nums.length; i++) {
sum+=nums[i];
min = Math.min(min,nums[i]);
}
return sum-min*nums.length;
}
2.问题的关键是最后的相同值是多少。将数组调整到相同值最快的方法是:将中位数当做相同值。
public int minMoves2(int[] nums) {
//取中位数为目标值,画图就明白了,当个数是奇数的时候,中位数作为目标值,这样移动的肯定最少,因为关于目标值对称的两个数的移动步数之和是固定的,取中间的数作为目标值时,中间的数就不用移动了。如果是偶数,那么取两个偶数中间的任意一个数都一样。两种情况都是关于中位数对称两数的差值之和
int res = 0;
Arrays.sort(nums);
int i = 0,j = nums.length-1;
while(i<j)
{
res += nums[j] - nums[i];
i++;
j--;
}
return res;
}
[LeetCode]Minimum Moves to Equal Array Elements1,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 ...
- LeetCode Minimum Moves to Equal Array Elements II
原题链接在这里:https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/ 题目: Given a non-empt ...
- LeetCode Minimum Moves to Equal Array Elements
原题链接在这里:https://leetcode.com/problems/minimum-moves-to-equal-array-elements/ 题目: Given a non-empty i ...
- [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 arr ...
- LeetCode 453. 最小移动次数使数组元素相等(Minimum Moves to Equal Array Elements) 47
453. 最小移动次数使数组元素相等 453. Minimum Moves to Equal Array Elements 题目描述 给定一个长度为 n 的非空整数数组,找到让数组所有元素相等的最小移 ...
- 【leetcode】453. Minimum Moves to Equal Array Elements
problem 453. Minimum Moves to Equal Array Elements 相当于把不等于最小值的数字都减到最小值所需要次数的累加和. solution1: class So ...
- LeetCode_453. Minimum Moves to Equal Array Elements
453. Minimum Moves to Equal Array Elements Easy Given a non-empty integer array of size n, find the ...
- 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 ...
- 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 ...
随机推荐
- Potato家族本地提权分析
原文来自SecIN社区-作者:Zeva 0x00 前言 在实际渗透中,我们用到最多的就是Potato家族的提权.本文着重研究Potato家族的提权原理以及本地提权细节 0x01 原理讲解 1.利用Po ...
- Spring Boot 集成 MQTT
本文代码有些许问题,处理方案见:解决 spring-integration-mqtt 频繁报 Lost connection 错误 一.添加配置 spring: mqtt: client: usern ...
- 【面试题】GC Root都有哪些?
那天去面试,面试官问我JVM垃圾回收,我是有备而来,上来就是一个可达性分析算法,然后就是一个复制算法,标记-清理,标记-整理,以及几个常见的垃圾回收器 详情见:https://www.cnblogs. ...
- Social Infrastructure Information Systems Division, Hitachi Programming Contest 2020 D题题解
将题意转换为一开始\(t = 0\),第\(i\)个操作是令\(t \leftarrow (a_i + 1) t + (a_i + b_i + 1)\).记\(A_i = a_i + 1, B_i = ...
- redis学习之——在分布式数据库中CAP原理CAP+BASE
分布式系统 分布式系统(distributed system) 由多台计算机和通信的软件组件通过计算机网络连接(本地网络或广域网)组成.分布式系统是建立在网络之上的软件系统.正是因为软件的特性,所以分 ...
- js实现转盘抽奖
大转盘抽奖,主要通过css3的"transform:rotate(0deg)"属性来控制元素的旋转角度来实现. 通常,抽奖的过程需要渐进的效果,所以直接通过旋转属性写比较繁琐. 这 ...
- 在Nuxt中使用react-id-swiper封装公共的轮播图组件(移动端
首先就是引入swiper import Swiper from 'react-id-swiper': 一个轮播图首先要考虑到一种情况就是当只有一张图的时候是不是需要按轮播图来处理 一般情况下,一张图是 ...
- SecureCRT无法退格删除
SecureCRT无法退格删除 securecrt无法退格删除问题解决: 如果想要全部会话都可以实现退格删除的功能,需要在全局选项设置. 最后选择全局应用即可.
- SpringBoot基于EasyExcel解析Excel实现文件导出导入、读取写入
1. 简介 Java解析.生成Excel比较有名的框架有Apache poi.jxl.但他们都存在一个严重的问题就是非常的耗内存,poi有一套SAX模式的API可以一定程度的解决一些内存溢出的问题 ...
- Linux修改系统时间为东八区北京时间(上海时间)
1. Linux时间 Linux的时间分为 System Clock(系统时间)和 Real Time Clock(硬件时间,简称RTC). 系统时间:指系统内核中的时间. 硬件时间:指主 ...