453. Minimum Moves to Equal Array Elements

Easy

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]
package leetcode.easy;

public class MinimumMovesToEqualArrayElements {
public int minMoves(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int min = nums[0];
for (int i = 0; i < nums.length; i++) {
min = Math.min(min, nums[i]);
}
int moves = 0;
for (int i = 0; i < nums.length; i++) {
moves += nums[i] - min;
}
return moves;
} @org.junit.Test
public void test() {
int[] nums = { 1, 2, 3 };
System.out.println(minMoves(nums));
}
}

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

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

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

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

  3. LeetCode Minimum Moves to Equal Array Elements II

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

  4. LeetCode Minimum Moves to Equal Array Elements

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

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

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

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

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

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

  9. LeetCode 453 Minimum Moves to Equal Array Elements

    Problem: Given a non-empty integer array of size n, find the minimum number of moves required to mak ...

随机推荐

  1. httprunner学习20-跳过用例skip/skipIf/skipUnless

    前言 在实际工作中,我们有时候会需要对测试用例加判断,比如某个接口功能暂时去掉了,我们希望对这个用例skip不去执行. 当其它的接口依赖于登陆接口返回的token时候,如果登陆都失败了,后面的接口,我 ...

  2. 201671010403 陈倩倩 实验十四 团队项目评审&课程学习总结

    一:实验名称:团队项目评审&课程学习总结 二:实验目的与要求 (1)掌握软件项目评审会流程: (2)反思总结课程学习内容. 三:实验步骤 任务一:按照团队项目结对评审名单,由项目组扮演乙方,结 ...

  3. Beta冲刺(3/7)——2019.5.24

    所属课程 软件工程1916|W(福州大学) 作业要求 Beta冲刺(3/7)--2019.5.24 团队名称 待就业六人组 1.团队信息 团队名称:待就业六人组 团队描述:同舟共济扬帆起,乘风破浪万里 ...

  4. VScode 配置 C++ 环境进行编译和调试

    这里记录为 VScode 配置 C++ 环境的简单步骤,实践环境为 Ubuntu 18.04 ,VScode 1.27 .在 Ubuntu 环境下,系统默认安装 gcc 和 g++ 编译器,故而下列步 ...

  5. 数组中有一个数字出现的次数超过数组长度的一半(C、Python)

    C语言 1 /* 2 ----------------------------------- 3 动态分配需要的内存大小 4 输入数组元素的值 5 通过函数调用,传地址对数组排序 6 循环每个元素,当 ...

  6. python Image open读取网络图片本地显示 爬虫必备

    #!/usr/bin/python3 # -*- coding: utf-8 -*- import requests from PIL import Image from io import Byte ...

  7. /bin/bash^M: bad interpreter

    (1)使用linux命令dos2unix filename,直接把文件转换为unix格式: (2)使用sed命令sed -i "s/\r//" filename 或者 sed -i ...

  8. JS的ES5的扩展内容

    JS的ES5 1.严格模式: (1)什么是严格模式: 在全局或函数的第一条语句定义为:  'use strict' 如果浏览器不支持, 只解析为一条简单的语句, 没有任何副作用 (2)严格模式作用: ...

  9. jdk和Tomcat版本之间的关系,jdk尽量要比tomcat低才行

    注:本文来源于:6860 <jdk和Tomcat版本之间的关系,jdk尽量要比tomcat低才行> 用的tomcat是低版本的,但是用的jdk却是高版本的,用Servlet做的项目运行都没 ...

  10. cogs 999. [東方S2]雾雨魔理沙

    二次联通门 : cogs 999. [東方S2]雾雨魔理沙 摸你傻赛高!! /* cogs 999. [東方S2]雾雨魔理沙 原来以为是一道计算几何的题 可是细细一想发现.. 这就是一道dp 由于给定 ...