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. LeetCode 045 Jump Game II

    题目要求:Jump Game II Given an array of non-negative integers, you are initially positioned at the first ...

  2. 20200520_windows2012安装python和django环境

    http://httpd.apache.org/download.cgi#apache24 配置文件修改后, 记得去阿里云开放端口 ServerName 172.18.196.189:9080 →不能 ...

  3. MySQL——一致性非锁定读(快照读)&MVCC

    MySQL--一致性非锁定读(快照读) MySQL数据库中读分为一致性非锁定读.一致性锁定读 一致性非锁定读(快照读),普通的SELECT,通过多版本并发控制(MVCC)实现. 一致性锁定读(当前读) ...

  4. django(django学习) 两张表创建 插入数据

      pycharm中直接创建django项目 输入创建项目名(如first_django) 在此输入应用名(如g_tu) 此为项目总目录 将first_django中settings.py中第58行修 ...

  5. 保姆级别的RabbitMQ教程!包括Java和Golang两种客户端

    目录 什么是AMQP 和 JMS? 常见的MQ产品 安装RabbitMQ 启动RabbitMQ 什么是Authentication(认证) 指定RabbitMQ的启动配置文件 如何让guest用户远程 ...

  6. Abp(net core)+easyui+efcore实现仓储管理系统——出库管理之七(五十六)

    abp(net core)+easyui+efcore实现仓储管理系统目录 abp(net core)+easyui+efcore实现仓储管理系统--ABP总体介绍(一) abp(net core)+ ...

  7. 第8.31节 Python中使用__delattr__清除属性数据

    一. 引言 在前面几节我们介绍了__ getattribute__方法和__setattr__方法,分别实现了实例属性的查询和修改(含定义即新增),作为Python中数据操作必不可少的三剑客get.s ...

  8. Kubernetes-21:Apiserver等证书修改使用年限

    Kubernetes证书使用年限修改方法   Kubernetes的apiservice.crt证书默认只有一年的使用期限,查看方法: cd /etc/kubernetes/pki [root@Cen ...

  9. 团队作业5_测试与发布(Alpha版本)

    Alpha版本测试报告 1.测试找出的bug(N个): (1)修复的Bug:很多个,主要是一些疏忽造成的,比如请求url写错导致数据加载不了.比较有意义的bug是因为使用redux,但是用户刷新后数据 ...

  10. js setTimeout运行机制

    在开始之前先看个面试例子 为什么会是0 1 2 2,而不是 0 0 1 1 再来看个例子 输出结果是4个undefined,为何不是1,2,3,4? 这是为什么呢,这是因为setTimeout是异步的 ...