原题链接https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/

题目描述

Given a non-empty integer array, find the minimum number of moves required to make all array elements equal, where a move is incrementing a selected element by 1 or decrementing a selected element by 1.

You may assume the array’s length is at most 10,000.

Example:

Input:
[1,2,3]

Output:
2

Explanation:
Only two moves are needed (remember each move increments or decrements one element):

[1,2,3] => [2,2,3] => [2,2,2]

思路分析

给定非空整数数组,每次可把数字加一或减一,返回把数组元素变为相等的做少操作数

  1. 直接法:
    找到数组中位数,其余元素向其靠近,累加操作次数 bingo
  2. ……//懒惰如我

源码附录

class Solution {
public int minMoves2(int[] nums) {
if(nums.length<2)
{
return 0;
}
Arrays.sort(nums); int result = 0;
for(int i=0;i<nums.length;i++)
{
result = result + Math.abs(nums[i]-nums[nums.length/2]);
} return result;
}
}

462.Minimum Moves to Equal Array Elements II——LeetCode进阶路的更多相关文章

  1. 【LeetCode】462. 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. 462. 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】462. Minimum Moves to Equal Array Elements II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:排序 方法二:直接找中位数 日期 题目地址: ...

  4. 462 Minimum Moves to Equal Array Elements II 最少移动次数使数组元素相等 II

    给定一个非空整数数组,找到使所有数组元素相等所需的最小移动数,其中每次移动可将选定的一个元素加1或减1. 您可以假设数组的长度最多为10000.例如:输入:[1,2,3]输出:2说明:只有两个动作是必 ...

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

  7. LeetCode Minimum Moves to Equal Array Elements II

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

  8. [Swift]LeetCode462. 最少移动次数使数组元素相等 II | 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 ...

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

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

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

随机推荐

  1. Linux - centos6.6不使用ssh如何在服务器之间传输文件?

    根据上一篇的设定,如果升级openssh失败之后,又无法使用ssh,该怎么传输文件呢? 可以使用busybox进行文件传输,首先需要准备两台centos6.6服务器:ctos66-01和ctos66- ...

  2. 从汇编层解读Golang的闭包实现:逃逸分析与性能影响

    本文精心梳理了一系列面试中具有一定难度的高频Golang问题,其中部分知识点可能你之前未曾深入探究,然而它们却在面试和实际工作中至关重要. 包括:Golang的基础语法.并发模型.内存管理等核心知识点 ...

  3. 仿京东短信验证码UI效果(鸿蒙)

    整体思路: 外层Stack布局,里面TextInput组件用来调起键盘,Row布局中循环出四个Text组件,Row布局覆盖在TextInput组件上,用来展示输入的数字. 定义两个参数,code用来接 ...

  4. Trae AI 工具使用记录--0手写代码创建桌面代办事项软件

    使用的AI工具是最近字节跳动出品的Trae工具. 第一步 下载IDE,Trae(官网链接 https://www.trae.ai) 安装完成后注册.登录,可以直接使用github账号.第一个坑就是目前 ...

  5. [解决方案]git pull : error: cannot lock ref 'refs/remotes/origin/*' (unable to update local ref)

    错误 git pull 报错不能更新本地分支 错误分析 本地分支跟远程分支不匹配 导致更新失败 解决方案 备份自己修改的代码 .git\refs\remotes (文件路径)对应删除你报错的分支 gi ...

  6. 震惊!AI 编程竟然让程序员 “失业” 了?真相让人意外

    在科技飞速发展的当下,AI 编程的异军突起无疑成为了整个编程领域乃至社会各界热议的焦点. 去年,全球首个AI程序员Devin横空出世,不仅能独立完成代码开发.修复Bug,甚至能通过阅读技术文档自主学习 ...

  7. RealityCapture重建试验

    一.使用已有数据集 (一)小型物件(官网) 输入:Camera_Lubitel2_studio "Lubitel Camera" consisting of 72 images 地 ...

  8. 使用PySide6/PyQt6实现Python跨平台表格数据分页打印预览处理

    我曾经在前面使用WxPython开发跨平台应用程序的时候,写了一篇<WxPython跨平台开发框架之列表数据的通用打印处理>,介绍在WxPython下实现表格数据分页打印处理的过程,在Wi ...

  9. ubuntu 刷新 hosts 命令

    systemd-resolved 服务 sudo systemctl restart systemd-resolved 这个命令将重启 systemd-resolved 服务,该服务负责 DNS 解析 ...

  10. SetupFactory-脚本

    1.写注册表 lResult = Registry.DoesKeyExist(HKEY_LOCAL_MACHINE, "SOFTWARE\\MapInfo\\MapX\\5.0") ...