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 ...
随机推荐
- Linux桌面进化史
自 20 世纪 90 年代初期以来,Linux 桌面也已从简单的窗口管理器发展为成熟.完整的桌面.那么它究竟是如何一步步发展至今的呢?作为从 1993 年就开始使用 Linux 的资深用户,FreeD ...
- oracle 12c数据库在Windows环境下的安装
因为菜鸟小白之前做着一些数据库审计产品的测试,接下来我会分享一些关于数据库安装和通过python的访问数据库的知识 安装 首先我们需要下载一个oracle 12c的安装程序,解压后右键点击“ ...
- java中AQS源码分析
AQS内部采用CLH队列.CLH队列是由节点组成.内部的Node节点包含的状态有 static final int CANCELLED = 1; static final int SIGNAL ...
- OSCP Learning Notes - Post Exploitation(4)
Pivoting 1. Edit the virtual network settings of the Vmware. 2. Set the Network Adapter(s) of Kali L ...
- docker环境部署mysql
参考文档 docker官方:https://hub.docker.com/_/mysql/?tab=description 部署步骤 1. 拉取镜像 这里我拉取了tag为5.7的镜像 docker p ...
- ES6标准中的import和export
在ES6前, 前端使用RequireJS或者seaJS实现模块化, requireJS是基于AMD规范的模块化库, 而像seaJS是基于CMD规范的模块化库, 两者都是为了为了推广前端模块化的工具 ...
- NCRE-Python考点
NCRE-Python考点 作者:封亚飞本文不含 文件处理.面向对象程序设计.公共基础.计算生态希望各位可以批评指正Qq 64761294 由于图片上传不方便,需要真题的朋友可以加我的qq找我要pdf ...
- 阿里云如何使用二次验证码/虚拟MFA/两步验证/谷歌验证器?
阿里云如何使用二次验证码/虚拟MFA/两步验证/谷歌验证器? 见如上链接中视频
- Python 什么时候会被取代?
以下是译文: Python经过了几十年的努力才得到了编程社区的赏识.自2010年以来,Python得到了蓬勃发展,并最终超越了C.C#.Java和JavaScript. 但是,这种趋势将持续到什么 ...
- HTTP的实体数据
数据类型表示实体数据的内容是什么,使用的是MIME type,相关的头字段是Accept和Content-Type: text:即文本格式的可读数据,我们最熟悉的应该就是text/html ...