首页 > 编程语言 >算法杂记 2023/02/15

算法杂记 2023/02/15

时间:2023-02-15 18:23:36浏览次数:48  
标签:02 15 nums int 元素 ans 2023 return array

算法杂记 2023/02/15

目录

今天分享的是两个力扣上的题,都是关于如何最后使数组中所有的元素相等

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

题目大意是每次操作可以使数组中 \(n-1\) 个元素 +1

假设我们需要 \(m\) 次操作,最后得到使所有的元素都等于 \(tar\)。如果我们每次操作是让所有元素都 \(+1\),那么所有元素初始值都应该为 \(tar - m\)。但是我们每次可以让一个元素不加一,反过来想,意思是每次操作让一个元素减\(1\),(因为最后只是要求相等,数值是没有意义的),最后使得所有元素相等。

那么就很好考虑了,我们只需要知道最小的元素是什么,并且统计答案即可,\(O(n)\)。

class Solution {
public:
    int minMoves(vector<int>& nums) {
        int n = nums.size();
        int mn = 0x3f3f3f3f;
        for (int i = 0; i < n; ++ i)
            mn = min(mn, nums[i]);
        int ans = 0;
        for (int i = 0; i < n; ++ i)
            ans += nums[i] - mn;
        return ans;
    }
};

462. Minimum Moves to Equal Array Elements II

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 or decrement an element of the array by 1.

Test cases are designed so that the answer will fit in a 32-bit integer.

Example 1:

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

Example 2:

Input: nums = [1,10,2,9]
Output: 16

Constraints:

  • n == nums.length
  • 1 <= nums.length <= 105
  • -109 <= nums[i] <= 109

本题是每个元素可以加一或者减一,最后使得所有元素相等,求问最小操作数。

直觉来说,肯定是一个介于中间的值,这样最大值,最小值都往这个中间值逼近肯定是最好的。

这题可以直接转化为最小曼哈顿距离,结论就是直接求中位数就好了。

这里,可以用 sort 求,当然也不能忘了快速选择算法。

这样就可以在 \(O(n)\) 的时间内解决。

这里的快速选择写的非常简洁,但实际上里面的原理分析大有道理,这里留下一个关于该写法的分析

class Solution {
public:

    int qsort(vector<int>& nums, int l, int r, int k){
        if (l == r) return nums[l];
        int i = l - 1, j = r + 1;
        int x = nums[(i + j) / 2]; // 中位数
        
        while (i < j){
            do ++i; while (nums[i] < x);
            do --j; while (x < nums[j]);
            if (i < j) swap(nums[i], nums[j]);
        }
        // 最后得到 [l, j] 是 <= x 的
        int Len = j - l + 1;
        if (Len >= k)
            return qsort(nums, l, j, k);
        return qsort(nums, j+1, r, k - Len);
    }

    int minMoves2(vector<int>& nums) {
        int n = nums.size(), ans = 0;
        int mid = qsort(nums, 0, n - 1, n / 2 + 1);
        for (int i = 0; i < n; ++ i)
            ans += abs(nums[i] - mid);
        return ans;
    }
};

标签:02,15,nums,int,元素,ans,2023,return,array
From: https://www.cnblogs.com/Last--Whisper/p/17124213.html

相关文章

  • 西湖论剑2023复现
    MessageBoard​​​​格式字符串漏洞获取栈地址,下一处读入利用栈地址获取libc基址payload=p64(stack_addr+0xb0+0x28)#rbppayload+=p64(pop_rdi)+p64(elf.got["......
  • hgame2023
    hgame2023week1‍test_ncnc签到‍easy_overflow​​有后门pl=b'a'*0x18+p64(0x40117E)p.sendline(pl)​​close(1)关闭了标准输出首先我们要明白什么是标准输......
  • day02
    上节课复习: 人---------编程语言--------》计算机 去包子店 付款 把包子送回来 1、计算机硬件 (运算器,控制器)=》CPU 负责运行人类程......
  • ON1 NoNoise AI 2023 for mac(ai摄影降噪软件)v17.1.0.13508中文版
    ON1NoNoiseAIMac破解版是一款去除图像噪点的应用,特别对于摄影师来说,它是比较专业的摄影降噪软件。使用AI人工智能驱动的NoNoiseAI快速去除噪点并获得照片中最清晰......
  • 15. CTFshow 萌新 web1
    一、代码<html><head><title>ctf.show萌新计划web1</title><metacharset="utf-8"></head><body><?php#包含数据库连接文件include("config.php");#判......
  • 与AI对话 -- 20230215 -- linux 启动参数与控制台
    linux启动参数console=ttyS0,115200n8console=tty0说明console=ttyS0,115200n8:指定系统使用ttyS0(ttyS1、ttyS2以此类推)串口作为主控台,115200n8意思是以115200即......
  • RComputer电阻计算器2024下载download
    2024版更新记录: 2024EditionupdateRecord: 1.增加了电阻电压电流功率计算的能力。 1.IncreasedthecalculatingabilityoftheR/V/I/P. 2.增加了计算......
  • 2023.2.11-2.12
    zroi2524命题挺简单的,就是一开始以为顺序无关考虑高维前缀和去了。用\(\text{Trie}\)树的做法比较显然,总状态数是\(2^n\log2^n\)的。zroi2526题目手玩一下发现......
  • 「解题报告」[NOI2022] 冒泡排序
    前排膜拜happyguy感觉这种特殊性质给的很多的题就应该把特殊性质挨个进行分析。特殊性质A首先容易发现:\(V_i\in[0,1]\),那么\(a_i\in[0,1]\)。显然这样不劣。......
  • vue-day02插值语法、文本指令、属性指令、事件指令、class和style、条件渲染、列表渲
    目录一、插值语法1.1mvvm演示1.2插值语法二、文本指令三、属性指令四、事件指令五、class和style六、条件渲染七、列表渲染八、补充:九、作业一、插值语法1.1mvvm演示......