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的更多相关文章

  1. [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 ...

  2. LeetCode Minimum Moves to Equal Array Elements II

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

  3. LeetCode Minimum Moves to Equal Array Elements

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

  4. [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 ...

  5. LeetCode 453. 最小移动次数使数组元素相等(Minimum Moves to Equal Array Elements) 47

    453. 最小移动次数使数组元素相等 453. Minimum Moves to Equal Array Elements 题目描述 给定一个长度为 n 的非空整数数组,找到让数组所有元素相等的最小移 ...

  6. 【leetcode】453. Minimum Moves to Equal Array Elements

    problem 453. Minimum Moves to Equal Array Elements 相当于把不等于最小值的数字都减到最小值所需要次数的累加和. solution1: class So ...

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

  8. 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 ...

  9. 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. C#(一)基础篇—类型与变量

    于今日起学习巩固C#基础 2020-12-01 本随笔用于个人回忆理解,记录当天学习过程,内容多从书中整理与自我学习了解,如有问题麻烦指正 以后有时间会单独分版块叙述 不管什么语言,都从一个Hello ...

  2. 探究 | 如何捕获一个Activity页面上所有的点击行为

    前言 最近逛wanAndroid论坛,发现一个有趣的问题:如何捕获一个Activity页面上所有的点击行为. 一起研究下吧,不想看源码的小伙伴可以直接看文末总结- 准备工作 先得罗列出页面上的一些点击 ...

  3. 20191017_datatable.select() 数据源中没有dataRow

    filterStr =" 记录时间 >= '2019/10/17 00:00:00' and 记录时间 <='2019/10/20 23:59:59' " 代码: dg ...

  4. day4(编写注册接口)

    1.编写注册接口 1.1 user/urls.py中添加路由 urlpatterns = [    path('register/', views.RegisterView.as_view()),  ...

  5. 倾斜摄影实景三维在智慧工厂 Web 3D GIS 数字孪生应用

      数字化推动钢铁工业转型升级 数字时代,随着数字地球,数字中国,数字工厂等数字化建设的不断深入,以地理信息系统(Geographic Information System, GIS)为基础,融合大数 ...

  6. Scrapy简明教程

    本文通过示例简要介绍一下使用Scrapy抓取网站内容的基本方法和流程. 继续阅读之前请确保已安装了scrapy. 基本安装方法为:pip install scrapy 我们已经在之前的文章中初步介绍了 ...

  7. 从零开始的ssti学习(已填)

    前前言: 本文只是接这个机会来梳理一下ssti的知识点.先说一下,本文目前的重点是Flask的ssti,但是之后会填其他框架的坑.(就不该叫ssti学习,ssti太广了) 涉及知识点: 模板注入 前言 ...

  8. IntelliJ IDEA 学习笔记

    之前一直用Eclipse,最近尝试使用IDEA,相较于 Eclipse 而言,IDEA强大的整合能力,比如: Git. Maven. Spring 等:提示功能的快速. 便捷:提示功能的范围广:好用的 ...

  9. 【Codeforces 1097F】Alex and a TV Show(bitset & 莫比乌斯反演)

    Description 你需要维护 \(n\) 个可重集,并执行 \(m\) 次操作: 1 x v:\(X\leftarrow \{v\}\): 2 x y z:\(X\leftarrow Y \cu ...

  10. socket ThreadingTCPServer学习笔记

    文件上传#服务端 while True: conn,address = sk.accept() conn.sendall(bytes('欢迎你小sb',encoding='utf-8')) str_s ...