第 376 场 LeetCode 周赛题解

news/2024/5/18 23:03:33 标签: leetcode, 算法, 枚举, 二分, 滑动窗口, 前缀和

A leetcode.cn/problems/find-missing-and-repeated-values" rel="nofollow">找出缺失和重复的数字

在这里插入图片描述

模拟

class Solution {
public:
    vector<int> findMissingAndRepeatedValues(vector<vector<int>> &grid) {
        int n = grid.size();
        vector<int> vis(n * n + 1);
        for (auto &r: grid)
            for (auto &c: r)
                vis[c]++;
        vector<int> res(2);
        for (int i = 1; i <= n * n; i++)
            if (vis[i] == 2)
                res[0] = i;
            else if (vis[i] == 0)
                res[1] = i;
        return res;
    }
};

B leetcode.cn/problems/divide-array-into-arrays-with-max-difference" rel="nofollow">划分数组并满足最大差限制

在这里插入图片描述

贪心:为了使组内元素之差尽可能小,所以只将排序后相邻的三个元素依次划分为一个子数组

class Solution {
public:
    vector<vector<int>> divideArray(vector<int> &nums, int k) {
        sort(nums.begin(), nums.end());
        vector<vector<int>> res;
        for (int i = 0; i < nums.size(); i += 3)
            if (nums[i + 2] - nums[i] > k)
                return {};
            else {
                res.push_back({nums[i], nums[i + 1], nums[i + 2]});
            }
        return res;
    }
};

C leetcode.cn/problems/minimum-cost-to-make-array-equalindromic" rel="nofollow">使数组成为等数数组的最小代价

在这里插入图片描述
在这里插入图片描述

枚举:首先对 n u m s nums nums 排序,若 n u m s nums nums 的中位数 m i d mid mid 为回文数则将 n u m s nums nums 中各元素变为 m i d mid mid 即可产生最小代价,否则 y y y 应该是小于 m i d mid mid 的且最接近 m i d mid mid 的回文数 或 大于 m i d mid mid 的且最接近 m i d mid mid 的回文数

class Solution {
public:
    using ll = long long;

    bool ispow10(int x) {
        while (x % 10 == 0)
            x /= 10;
        return x == 1;
    }

    vector<int> find_val(int x) {//找到最接近x的几个回文数
        vector<int> res;
        string s = to_string(x);
        if (ispow10(x)) {
            if (x == 1e9)
                return {(int) 1e9 - 1};
            else if (x == 1)
                return {x};
            else
                return {x + 1, x - 1};
        } else if (ispow10(x + 1) || ispow10(x - 1))
            return {x};

        if (s.size() % 2 == 0) {
            string left = s.substr(0, s.size() / 2);
            vector<int> d{1, 0, -1};
            for (auto di: d) {
                string nleft = to_string(stol(left) + di);
                string r = string(nleft.rbegin(), nleft.rend());
                ll t = stol(nleft + r);
                if (t < 1e9) {
                    res.push_back(t);
                }
            }
        } else {
            string left = s.substr(0, s.size() / 2 + 1);
            vector<int> d{1, 0, -1};
            for (auto di: d) {
                string nleft = to_string(stol(left) + di);
                string r = string(nleft.rbegin() + 1, nleft.rend());
                ll t = stol(nleft + r);
                if (t < 1e9) {
                    res.push_back(t);
                }
            }
        }
        return res;
    }

    long long minimumCost(vector<int> &nums) {
        int n = nums.size();
        sort(nums.begin(), nums.end());
        int x = n % 2 == 0 ? (nums[(n - 1) / 2] + nums[n / 2]) / 2 : nums[n / 2];
        auto tar = find_val(x);
        ll res = INT64_MAX;
        for (auto ti: tar) {
            ll t = 0;
            for (auto it: nums)
                t += abs(it - ti);
            res = min(res, t);
        }
        return res;
    }
};


D leetcode.cn/problems/apply-operations-to-maximize-frequency-score" rel="nofollow">执行操作使频率分数最大

在这里插入图片描述

二分+枚举二分答案 m i d mid mid,在排序后的 n u m s nums nums枚举长为 m i d mid mid 的子数组,判断该将该子数组变为同一元素的最少操作(变为该子数组的中位数即可产生最少操作数)数是否不超过 k k k

class Solution {
public:
    using ll = long long;

    int maxFrequencyScore(vector<int> &nums, long long k) {
        int n = nums.size();
        sort(nums.begin(), nums.end());
        vector<ll> ps(n + 1);//前缀和
        for (int i = 0; i < n; i++)
            ps[i + 1] = ps[i] + nums[i];
        int l = 1, r = n;
        while (l < r) {
            int mid = (l + r + 1) / 2;
            int find = 0;
            for (int l = 0, r = mid - 1; r < n; l++, r++) {//子数组nums[l,r]
                int cen = (l + r) / 2;
                int tar = nums[cen];//将子数组各元素变为tar产生最少操作数
                ll t = 1LL * tar * (cen - l + 1) - (ps[cen + 1] - ps[l]) + ps[r + 1] - ps[cen + 1] - 1LL * tar * (r - cen);
                if (k >= t) {
                    find = 1;
                    break;
                }
            }
            if (find)
                l = mid;
            else
                r = mid - 1;
        }
        return l;
    }
};


http://www.niftyadmin.cn/n/5269783.html

相关文章

服务器数据恢复-raid5多块磁盘掉线导致上层卷无法挂载的数据恢复案例

服务器数据恢复环境&#xff1a; 一台服务器中有一组由24块FC硬盘组建的raid5磁盘阵列&#xff0c;linux操作系统ext3文件系统&#xff0c;服务器上层部署有oracle数据库。 服务器故障&检测&#xff1a; raid5阵列中有两块硬盘出现故障掉线&#xff0c;导致服务器上层卷无法…

Saas 中 用默认的值,不初始化给商户值,sql 查询 group by中,指定字段 倒序

在saas 项目中&#xff0c;有些商户没有设定某些值&#xff0c;则用系统默认的值&#xff0c;不需要初始化给商户 SELECT * FROM app_public_config WHERE (name, merchant_id) IN (SELECT name, MAX(merchant_id)FROM app_public_configGROUP BY name ) and merchant_id IN …

HTML5+CSS3小实例:3D发光切换按钮效果

目录 一、运行效果 图片效果 二、项目概述 三、开发环境 四、实现步骤及代码 1.创建空文件夹 2.完成页面内容 3.完成css样式 五、项目总结 六、源码获取 一、运行效果 图片效果 二、项目概述 这个项目是一个演示3D发光切换按钮效果的网页。按钮由一个开关和一个指…

电子元器件介绍——电阻(一)

电子元器件 文章目录 电子元器件前言1.1电阻基本知识1.2电阻的作用1.3电阻的分类1.4 贴片电阻贴片电阻的规范、尺寸、封装 1.5 技术参数噪声&#xff1a; 1.6 电阻的失效 总结 前言 接下来我们就把常用的电子元器件全部介绍给大家&#xff0c;这一节是电阻&#xff0c;电容电感…

什么是PHP中的变量作用域?

PHP中的变量作用域&#xff08;variable scope&#xff09;指的是变量在代码中可访问的区域或范围。PHP支持多种变量作用域&#xff0c;包括以下几种主要类型&#xff1a; 全局作用域&#xff08;Global Scope&#xff09;&#xff1a; 在全局作用域中声明的变量可以在脚本的任…

冷热字段分离提升程序局部性

突然想起了上学期课堂上的一个提升程序局部性的案例&#xff0c;我觉得非常有意思&#xff0c;写篇博客记录一下。 1 场景 案例场景非常简单&#xff0c;就是遍历访问大结构体数组的某一字段。对应到下图&#xff0c;funcA要访问a[N]的fld3字段&#xff0c;funcB中要访问b[N]…

西工大计院计算机系统基础实验二(配置gdb插件)

第二次实验是二进制炸弹实验&#xff0c;为了简化操作&#xff0c;并且让大家接下来能够按照作者之前已经为网安院写好的博客西工大网络空间安全学院计算机系统基础实验二&#xff08;清楚实验框架及phase_1&#xff09;-CSDN博客来走&#xff0c;大家需要下载一款好用的gdb插件…

【docker 】Compose 使用介绍

Docker Compose Docker Compose文档 Docker Compose GitHub地址 Docker Compose 是用于定义和运行多容器 Docker 应用程序的工具。通过 Compose&#xff0c;您可以使用 YML 文件来配置应用程序需要的所有服务。然后&#xff0c;使用一个命令&#xff0c;就可以从 YML 文件配…