C#LeetCode刷题之#453-最小移动次数使数组元素相等(Minimum Moves to Equal Array Elements)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3877 访问。
给定一个长度为 n 的非空整数数组,找到让数组所有元素相等的最小移动次数。每次移动可以使 n - 1 个元素增加 1。
输入:[1,2,3]
输出:3
解释:只需要3次移动(注意每次移动会增加两个元素的值):
[1,2,3] => [2,3,3] => [3,4,3] => [4,4,4]
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.
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]
示例
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3877 访问。
public class Program {
public static void Main(string[] args) {
var x = new int[] { 1, 2, 3 };
var res = MinMoves(x);
Console.WriteLine(res);
x = new int[] { 1, 2147483647 };
res = MinMoves2(x);
Console.WriteLine(res);
x = new int[] { 1, 5, 7, 2, 3 };
res = MinMoves3(x);
Console.WriteLine(res);
Console.ReadKey();
}
private static int MinMoves(int[] nums) {
//这个解法未AC,因为nums.Sum()的值有可能会超出整型范围
return nums.Sum() - nums.Length * nums.Min();
}
private static int MinMoves2(int[] nums) {
var min = nums.Min();
var sum = 0L;
foreach (var num in nums) {
sum += num;
}
return (int)(sum - min * nums.Length);
}
private static int MinMoves3(int[] nums) {
var min = nums.Min();
return nums.Sum(n => n - min);
}
}
以上给出3种算法实现,以下是这个案例的输出结果:
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3877 访问。
3
2147483646
13
分析:
该题的解法思路为每次减1个数,使整个数组相等。
显而易见,MinMoves 、MinMoves2 和 MinMoves3 的时间复杂度基于部分函数库的实现,它们的时间复杂度应当均为: 。
C#LeetCode刷题之#453-最小移动次数使数组元素相等(Minimum Moves to Equal Array Elements)的更多相关文章
- LeetCode 453. 最小移动次数使数组元素相等(Minimum Moves to Equal Array Elements) 47
453. 最小移动次数使数组元素相等 453. Minimum Moves to Equal Array Elements 题目描述 给定一个长度为 n 的非空整数数组,找到让数组所有元素相等的最小移 ...
- [Swift]LeetCode453. 最小移动次数使数组元素相等 | 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 ...
- Java实现 LeetCode 453 最小移动次数使数组元素相等
453. 最小移动次数使数组元素相等 给定一个长度为 n 的非空整数数组,找到让数组所有元素相等的最小移动次数.每次移动可以使 n - 1 个元素增加 1. 示例: 输入: [1,2,3] 输出: 3 ...
- LeetCode#453 最小移动次数使数组元素相等
给定一个长度为 n 的非空整数数组,找到让数组所有元素相等的最小移动次数.每次移动可以使 n - 1 个元素增加 1. 示例: 输入: [,,] 输出: 解释: 只需要3次移动(注意每次移动会增加两个 ...
- 力扣(LeetCode)453. 最小移动次数使数组元素相等
给定一个长度为 n 的非空整数数组,找到让数组所有元素相等的最小移动次数.每次移动可以使 n - 1 个元素增加 1. 示例: 输入: [1,2,3] 输出: 3 解释: 只需要3次移动(注意每次移动 ...
- 【leetcode】453. Minimum Moves to Equal Array Elements
problem 453. Minimum Moves to Equal Array Elements 相当于把不等于最小值的数字都减到最小值所需要次数的累加和. solution1: class So ...
- LeetCode Minimum Moves to Equal Array Elements II
原题链接在这里:https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/ 题目: Given a non-empt ...
- LeetCode Minimum Moves to Equal Array Elements
原题链接在这里:https://leetcode.com/problems/minimum-moves-to-equal-array-elements/ 题目: Given a non-empty i ...
- 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 ...
随机推荐
- 一文快速掌握华为云IPv6基础知识及使用指南
随着5G.物联网等新兴技术领域的发展,IP空间需求巨大,IPv6成为万物互联的基础,势在必行:华为云作为IPv6成熟商用开拓者,针对金融.广电.媒资等不同行业推出IPv6解决方案,助力企业平滑升级到I ...
- Burp Suite Repeater Module - 中继模块
目的:节省网页应用分析时间 目标对象:http://10.0.0.15/getboo/login.php 通过调整Request的参数,不断尝试,通过Response查看状态.从而节省在浏览器中操作的 ...
- try-catch- finally块中, finally块唯一不执行的情况是什么?
除非在try块或者catch块中调用了退出虚拟机的方法(即System.exit(1);),否则不管在try块.catch块中执行怎样的代码,出现怎样的情况,异常处理的finally块总是会被执行的 ...
- Python虚拟环境(virtualenv)
python虚拟环境 虚拟环境:一个独立的可以运行的python执行环境,可以创建多个,且相互之间互不影响 使用virtualenv库 pip install virtualenv 用法 # 创建虚拟 ...
- C++语法小记---string类
string类 #include <iostream> #include <string> using namespace std; // 实现字符串右移, 例子hello & ...
- 题解 洛谷 P5311 【[Ynoi2011]成都七中】
每次询问是关于 \(x\) 所在的连通块,所以考虑用点分树来解决本题. 点分树上每个节点所对应的子树,都是原树中的一个连通块.询问中给定 \(x\) 和区间 \([l,r]\),其就已经确定了原树的一 ...
- Flutter日常笔记
factory修饰的构造方法 表示不是每次返回的都是新创建出来的对象, 可以取内存中已有的, 比如单例模式的书写 每次返回的都是一个实例, 这时要使用factory修饰构造方法 flutter不要求显 ...
- scp的使用以及cp的对比
scp是secure copy的简写,用于在Linux下进行远程拷贝文件的命令,和它类似的命令有cp,不过cp只是在本机进行拷贝不能跨服务器,而且scp传输是加密的.可能会稍微影响一下速度.当你服务器 ...
- laravel开发调试工具laravel-debugbar的安装
一.使用 Composer 安装该扩展包 composer require barryvdh/laravel-debugbar --dev 二.(可选)修改配置文件app/config.php Lar ...
- Android集成Zxing
1.在build文件中添加依赖 dependencies { //ZXing implementation 'com.google.zxing:core:3.3.3' implementation(' ...