[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 ...
随机推荐
- Android中Application的意义及使用方法
首先,在一个Android程序中,有且只有一个Application对象,在程序启动的时候,首先执行Application的onCreate方法,这是一个Android应用的入口,在开发中,我们常常自 ...
- K 均值算法-如何让数据自动分组
公号:码农充电站pro 主页:https://codeshellme.github.io 之前介绍到的一些机器学习算法都是监督学习算法.所谓监督学习,就是既有特征数据,又有目标数据. 而本篇文章要介绍 ...
- 部署完的Django项目升级为HTTPS
1.阿里云上申请免费ssl证书--->提交各种资料--->等待审核--->下载证书. 2.远程连接阿里云服务器,将下载下来的证书内容复制到Nginx安装目录下的cert目录(需要新建 ...
- Mybatis报错invalid comparison: java.util.Date and java.lang.String
请求参数中两个属性确实都是date类型,数据库也确认是data类型,这个错误是因为 在这里把date类型的参数与单引号做了比较出现的,删除就可以正常运行了.
- Mybatis04
title: Mybatis学习04 date: 2020-01-20 21:48:00 tags: 这次的笔记主要是mybatis中的注解 1.实体类的注解 实体类的注解在mybati的XML文件中 ...
- WindowsServerU盘系统盘制作
一.工具及安装包准备: 1.UltraISO软碟通 下载:链接:https://pan.baidu.com/s/1gixSdpEjvh6I31rGeh1-Gg 提取码:9zbx (大学期间无意间找到一 ...
- 解析php sprintf函数漏洞
php sprintf函数漏洞 0x01 了解sprintf()函数 1,sprintf(),函数是php中的函数 2,作用是将格式化字符串写入变量中 3,函数形式为sprintf(format,ar ...
- MyBatis if 标签的坑,居然被我踩到了。。。
事件的原因是这样的,需求是按条件查数据然后给前端展示就行了,写的时候想着挺简单的,不就是使用 MyBatis 动态 SQL 去查询数据吗? 现实还是很残酷的,等我写完上完 UAT 后,前端同学说根据s ...
- 安卓11配置谷歌FCM推送报错
2020-12-11 11:57:50.872 15404-15464/com.sp.notify E/FirebaseInstanceId: Failed to get FIS auth token ...
- 十、TestNG分组测试
使用 groups 属性 package com.lc.tesgFenZu; import org.testng.annotations.AfterGroups; import org.testng. ...