原题链接在这里:https://leetcode.com/problems/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 array elements equal, where a move is incrementing n - 1 elements by 1.

Example:

Input:
[1,2,3] Output:
3 Explanation:
Only three moves are needed (remember each move increments two elements): [1,2,3] => [2,3,3] => [3,4,3] => [4,4,4]

题解:

Think on the other way, increasing n - 1 elements == decreasing 1 element.

Decrease all num to min(nums).

Time Complexity: O(n). n = nums.length.

Space: O(1).

AC Java:

 public class Solution {
public int minMoves(int[] nums) {
int min = Integer.MAX_VALUE;
for(int num : nums){
min = Math.min(min, num);
} int moves = 0;
for(int num: nums){
moves += (num - min);
}
return moves;
}
}

Could be one pass.

Time Complexity: O(n).

Space: O(1).

AC Java:

 class Solution {
public int minMoves(int[] nums) {
if(nums == null || nums.length < 2){
return 0;
} int min = Integer.MAX_VALUE;
int sum = 0;
for(int num : nums){
min = Math.min(min, num);
sum += num;
} return sum - min * nums.length;
}
}

跟上Minimum Moves to Equal Array Elements II.

LeetCode Minimum Moves to Equal Array Elements的更多相关文章

  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 最少移动次数使数组元素相等

    Given a non-empty integer array of size n, find the minimum number of moves required to make all arr ...

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

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

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

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

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

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

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

  9. 【LeetCode】453. Minimum Moves to Equal Array Elements 解题报告(Java & Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:模拟过程 方法二:求和-n*最小值 方法三: ...

随机推荐

  1. springBoot专题3---->springBoot与多数据源的配置

    最近有点忙,更新有点慢.今天进来说说一说springBoot中如何配置多数据源. 第一,新建一个名为springBoot-mutidata的maven项目,完整的pom.xml配置如下: <?x ...

  2. java 注解 + 自定义注解的使用

    java中元注解有四个: @Retention @Target @Document @Inherited:  @Retention:注解的保留位置 @Retention(RetentionPolicy ...

  3. idapython在样本分析中的使用-字符解密

    最近接手的一个样本,样本中使用了大量的xor加密,由于本身样本不全,无法运行(好吧我最稀饭的动态调试没了,样本很有意思,以后有时间做票大的分析),这个时候就只好拜托idapython大法了(当然用id ...

  4. MSSQL跨服务器插入

    跨服器插入数据  其中服务器的IP为源数据地址 还要注意的是目标库中不能存在对应表. select * INTO compMaintype from openrowset('SQLOLEDB' , ' ...

  5. cxf webservice简单应用

    Server端 server部署到一个端口号为80的tomcat中 CXFController.java package com.lwj.controller; import java.io.IOEx ...

  6. sqlserver 性能优化常用方法

    查看被锁表: select request_session_id spid,OBJECT_NAME(resource_associated_entity_id) tableName from sys. ...

  7. Java中&&和&的区别

    Java中&&和&都是表示与的逻辑运算符,都表示逻辑运输符and,当两边的表达式都为true的时候,整个运算结果才为true,否则为false. &&的短路功能 ...

  8. 2.线性表-Linked list

    fatal.h #include <stdio.h> #include <stdlib.h> #define Error( Str ) FatalError( Str ) #d ...

  9. centos6.5新增加硬盘挂载并实现开机时自动挂载

    在内网主机新增一个2T硬盘,先关机断电再连接硬盘数据线和电源线! 查看当前磁盘设备信息: [root@tb ~]# fdisk -lWARNING: GPT (GUID Partition Table ...

  10. Blog 公用部分结构与class定义

    /*博客文章公用部分class与结构 common*/ /* 1.title-block //标题块 ├── border-danger //危险红 ├── border-info //普通蓝 └── ...