第 387 场 LeetCode 周赛题解

news/2024/5/18 5:21:23 标签: leetcode, 算法, 枚举, 前缀和, 离散化, 树状数组, 模拟

A leetcode.cn/problems/distribute-elements-into-two-arrays-i/description/" rel="nofollow">3069. 将元素分配到两个数组中 I

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

模拟

class Solution {
public:
    vector<int> resultArray(vector<int> &nums) {
        vector<int> r1{nums[0]}, r2{nums[1]};
        for (int i = 2; i < nums.size(); i++) {
            if (r1.back() > r2.back())
                r1.push_back(nums[i]);
            else
                r2.push_back(nums[i]);
        }
        for (auto x: r2)
            r1.push_back(x);
        return r1;
    }
};

B leetcode.cn/problems/count-submatrices-with-top-left-element-and-sum-less-than-k/description/" rel="nofollow">元素和小于等于 k 的子矩阵的数目

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

前缀和:先计算二维前缀和,再枚举包含左上角元素的子矩阵

class Solution {
public:
    int countSubmatrices(vector<vector<int>> &grid, int k) {
        int m = grid.size(), n = grid[0].size();
        for (int i = 0; i < m; i++)
            for (int j = 1; j < n; j++)
                grid[i][j] += grid[i][j - 1];
        for (int j = 0; j < n; j++)
            for (int i = 1; i < m; i++)
                grid[i][j] += grid[i - 1][j];
        int res = 0;
        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++)
                if (grid[i][j] <= k)
                    res++;
        return res;
    }
};

C leetcode.cn/problems/minimum-operations-to-write-the-letter-y-on-a-grid/description/" rel="nofollow">在矩阵上写出字母 Y 所需的最少操作次数

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

枚举枚举属于 Y 和不属于 Y 的单元格的颜色

class Solution {
public:
    int minimumOperationsToWriteY(vector<vector<int>> &grid) {
        int n = grid.size();
        int res = INT32_MAX;
        for (int cy = 0; cy <= 2; cy++)//属于Y的单元格的颜色
            for (int cny = 0; cny <= 2; cny++)//不属于Y的单元格的颜色
                if (cy != cny) {
                    int t = 0;
                    for (int i = 0; i < n; i++)
                        for (int j = 0; j < n; j++)
                            if (i <= n / 2 && (i == j || i + j == n - 1) || i > n / 2 && j == n / 2)//属于Y的单元格
                                t += grid[i][j] != cy ? 1 : 0;
                            else
                                t += grid[i][j] != cny ? 1 : 0;
                    res = min(res, t);
                }
        return res;
    }
};

D leetcode.cn/problems/distribute-elements-into-two-arrays-ii/description/" rel="nofollow">将元素分配到两个数组中 II

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

离散化 + 树状数组:先将 nums 离散化,然后利用树状数组来维护两个数组,并查询数组中严格大于某个数的元素数量

class Solution {
public:
    vector<int> resultArray(vector<int> &nums) {
        vector<int> li = nums, o = li;
        sort(o.begin(), o.end());
        o.erase(unique(o.begin(), o.end()), o.end());
        for (auto &i: li)//li为nums离散化后的数组
            i = lower_bound(o.begin(), o.end(), i) - o.begin() + 1;
        int n = nums.size();
        int m = li.size();
        vector<int> r1{nums[0]}, r2{nums[1]};
        BinaryIndexedTree t1(m), t2(m);//两个树状数组
        t1.add(li[0], 1);
        t2.add(li[1], 1);
        for (int i = 2; i < n; i++) {
            int c1 = t1.query(m) - t1.query(li[i]);
            int c2 = t2.query(m) - t2.query(li[i]);
            if (c1 > c2) {
                r1.push_back(nums[i]);
                t1.add(li[i], 1);
            } else if (c1 < c2) {
                r2.push_back(nums[i]);
                t2.add(li[i], 1);
            } else {
                if (r1.size() <= r2.size()) {
                    r1.push_back(nums[i]);
                    t1.add(li[i], 1);
                } else {
                    r2.push_back(nums[i]);
                    t2.add(li[i], 1);
                }
            }
        }
        for (auto x: r2)
            r1.push_back(x);
        return r1;
    }

    class BinaryIndexedTree {//树状数组模板
    public:
        int N;
        vector<int> a;

        BinaryIndexedTree(int n) {
            N = n;
            a = vector<int>(N + 1);
        }

        inline int lowbit(int x) {
            return x & -x;
        }

        void add(int loc, int val) {// li[loc]+=val;
            for (; loc <= N; loc += lowbit(loc))
                a[loc] += val;
        }

        int query(int loc) {// sum{li[k] | 1<=k<=loc}
            int res = 0;
            for (; loc > 0; loc -= lowbit(loc))
                res += a[loc];
            return res;
        }
    };
};

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

相关文章

UDF学习重要视频资料

一、学习资料&#xff1a;&#xff08;视频资料目录如下&#xff0c;该专栏为学习笔记&#xff0c;详见订阅后视频资料&#xff09; C语言所需基础及内容&#xff1b;数据结构-UDF常见循环宏、几何宏UDF重要宏4-DEFINE_PROFILE宏学习及应用5-1DENFIN宏学习及应用5-2DEFINE宏6-…

JWT的是什么

session共享 什么是session共享 Session共享是指在分布式系统中&#xff0c;在多个服务器之间共享同一个用户的会话数据。在传统的Web应用中&#xff0c;用户的会话信息通常存储在服务器端的Session中&#xff0c;而每个用户的请求在同一个服务器上处理&#xff0c;因此可以轻…

设计模式大题做题记录

设计模式大题 09年 上半年&#xff1a; 09年下半年 10年上半年 10年下半年 11年上半年 11年下半年 12年上半年 12年下半年 13年上半年 13年下半年

Leetcode 3071. Minimum Operations to Write the Letter Y on a Grid

Leetcode 3071. Minimum Operations to Write the Letter Y on a Grid 1. 解题思路2. 代码实现 题目链接&#xff1a;3071. Minimum Operations to Write the Letter Y on a Grid 1. 解题思路 这一题思路上也是比较直接的&#xff0c;就是首先找到这个Y字符&#xff0c;然后…

《汇编语言》第3版 (王爽)检测点11.1解析

第11章 检测点11.1解析 在Debug中各标志位定义如下: 写出下面每条指令执行后&#xff0c;ZF、PF、SF等标志位的值。 sub al,al ZF1 PF1 SF0 ;执行完此指令后&#xff0c;结果为0&#xff0c;所以ZF1;1的个数为0是偶数个&#xff0c;所以PF1;0非负&#xff0c;所以SF0。各标…

ES: ES+Kibana 环境部署

ESKibana 部署 机器信息 10.10.8.62 10.10.8.63 10.10.8.64版本选择&#xff1a;6.8.1 基础环境优化 所有节点 # 关闭防火墙 systemctl stop firewalld.service systemctl disable firewalld.service# 查看selinux getenforce # 关闭selinux setenforce 0 # 永久关闭se…

Java零基础 - 数组的定义和声明

哈喽&#xff0c;各位小伙伴们&#xff0c;你们好呀&#xff0c;我是喵手。 今天我要给大家分享一些自己日常学习到的一些知识点&#xff0c;并以文字的形式跟大家一起交流&#xff0c;互相学习&#xff0c;一个人虽可以走的更快&#xff0c;但一群人可以走的更远。 我是一名后…

一周学会Django5 Python Web开发-Django5删除视图DeleteView

锋哥原创的Python Web开发 Django5视频教程&#xff1a; 2024版 Django5 Python web开发 视频教程(无废话版) 玩命更新中~_哔哩哔哩_bilibili2024版 Django5 Python web开发 视频教程(无废话版) 玩命更新中~共计31条视频&#xff0c;包括&#xff1a;2024版 Django5 Python we…