LeetCode453. Minimum Moves to Equal Array Elements

文章目录

一、题目

Given an integer array nums of size n, return the minimum number of moves required to make all array elements equal.

In one move, you can increment n - 1 elements of the array by 1.

Example 1:

Input: nums = [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]
Example 2:

Input: nums = [1,1,1]
Output: 0

Constraints:

n == nums.length
1 <= nums.length <= 105
-109 <= nums[i] <= 109
The answer is guaranteed to fit in a 32-bit integer.

二、题解

class Solution {
   
public:
    int minMoves(vector<int>& nums) {
   
        int n = nums.size();
        int res = 0;
        int minValue = *min_element(nums.begin(),nums.end());
        for(auto x:nums){
   
            res += x - minValue;
        }
        return res;
    }
};

相关推荐

  1. LeetCode 45

    2023-12-27 08:52:02       69 阅读
  2. LeetCode--455.分发饼干

    2023-12-27 08:52:02       56 阅读
  3. leetcode 455.分发饼干

    2023-12-27 08:52:02       38 阅读
  4. LeetCode435. Non-overlapping Intervals

    2023-12-27 08:52:02       50 阅读
  5. LeetCode //C - 443. String Compression

    2023-12-27 08:52:02       74 阅读
  6. Leetcode459:重复的字符串

    2023-12-27 08:52:02       64 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-27 08:52:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-27 08:52:02       101 阅读
  3. 在Django里面运行非项目文件

    2023-12-27 08:52:02       82 阅读
  4. Python语言-面向对象

    2023-12-27 08:52:02       91 阅读

热门阅读

  1. C语言中的结构体和联合体:异同及应用

    2023-12-27 08:52:02       60 阅读
  2. Go语言入门:Go程序的基础结构

    2023-12-27 08:52:02       50 阅读
  3. C++入门【16-C++ 从函数返回数组】

    2023-12-27 08:52:02       62 阅读
  4. Ansible的变量

    2023-12-27 08:52:02       50 阅读
  5. 【EasyExcel】使用技巧

    2023-12-27 08:52:02       53 阅读
  6. Mybatis-Plus基础之Mapper的映射规则

    2023-12-27 08:52:02       42 阅读
  7. C++设计模式:单例模式(饿汉式、懒汉式)

    2023-12-27 08:52:02       61 阅读
  8. 【数据库】postgressql设置数据库执行超时时间

    2023-12-27 08:52:02       62 阅读
  9. 黑客常用端口利用总结

    2023-12-27 08:52:02       42 阅读
  10. python之列表动态生成和重复数据处理

    2023-12-27 08:52:02       60 阅读
  11. VSCode 加Cortex-Debug嵌入式调试方法

    2023-12-27 08:52:02       54 阅读
  12. codeforces 1904B

    2023-12-27 08:52:02       59 阅读