LeetCode第347场周赛

news/2024/7/7 19:55:16

2023.5.28LeetCode第347场周赛

A. 移除字符串中的尾随零

思路

从最后一位开始遍历,为0则跳过

代码

class Solution {
public:
    string removeTrailingZeros(string num) {
        int i = num.size() - 1;
        while (i >= 0 && num[i] == '0') i -- ;
        return num.substr(0, i + 1);
    }
};

B. 对角线上不同值的数量差

思路

暴力模拟

代码

class Solution {
public:
    vector<vector<int>> differenceOfDistinctValues(vector<vector<int>>& g) {
        int n = g.size(), m = g[0].size();
        vector<vector<int>> ans, tl, br;
        ans.resize(n, vector<int>(m));
        tl.resize(n, vector<int>(m));
        br.resize(n, vector<int>(m));
        for (int i = 0; i < n; i ++ )
            for (int j = 0; j < m; j ++ ) {
                //tl
                int x = i - 1, y = j - 1;
                unordered_set<int> se;
                while (x >= 0 && y >= 0) {
                    se.insert(g[x][y]);
                    x -- , y -- ;
                }
                tl[i][j] = se.size();
                //br
                se.clear();
                x = i + 1, y = j + 1;
                while (x < n && y < m) {
                    se.insert(g[x][y]);
                    x ++ , y ++ ;
                }
                br[i][j] = se.size();
            }
        for (int i = 0; i < n; i ++ )
            for (int j = 0; j < m; j ++ )
                ans[i][j] = abs(tl[i][j] - br[i][j]);
        return ans;
    }
};

[C. 使所有字符相等的最小成本

](https://leetcode.cn/problems/minimum-cost-to-make-all-characters-equal/)

思路

对于每个s[i] != s[i - 1],要使其相等
有两种选择,翻转前i个,或者翻转后n - i个,选择代价最小的方案

代码

class Solution {
public:
    long long minimumCost(string s) {
        long long ans = 0;
        int n = s.size();
        for (int i = 1; i < n; i ++ ) {
            if (s[i] != s[i - 1])
                ans += min(i, n - i);
        }
        return ans;
    }
};

D. 矩阵中严格递增的单元格数

思路

动态规划
从小到大枚举所有值,每个值一定是从更小的数转移而来
定义动态规划数组f,f[i][j]表示到(i,j)经过的最多数量
维护每行每列最大的f值
f[i][j] = max(row[i], col[j]) + 1
计算后更新row和col数组

代码

class Solution {
public:
    typedef pair<int, int> PII;

    int maxIncreasingCells(vector<vector<int>>& mat) {
        int n = mat.size(), m = mat[0].size();
        vector<int> row(n), col(m); // 存放每一行列最大的f值
        map<int, vector<PII>> mp; // 从小到大记录每个值的位置
        for (int i = 0; i < n; i ++ )
            for (int j = 0; j < m; j ++ )
                mp[mat[i][j]].push_back({i, j});
        int ans = 0;
        for (auto [k, v] : mp) {
            vector<int> p;
            for (auto i : v) {
                int x = i.first, y = i.second;
                p.push_back(max(row[x], col[y]) + 1);
            }
            for (int i = 0; i < v.size(); i ++ ) {
                ans = max(ans, p[i]);
                int x = v[i].first, y = v[i].second;
                row[x] = max(row[x], p[i]);
                col[y] = max(col[y], p[i]);
            }
        }
        return ans;
    }
};

http://lihuaxi.xjx100.cn/news/1178881.html

相关文章

【JAVAWEB】HTML的常见标签

目录 1.HTML结构 1.1认识HTML标签 1.2HTML文件基本结构 1.3标签层次结构 1.4快速生成代码框架 2.HTML常见标签 注释标签 标题标签&#xff1a;h1-h6 段落标签:p 换行标签&#xff1a;br 格式化标签 图片标签 超链接标签&#xff1a;a 表格标签 列表标签 表单标…

如何在 ubuntu 下安装英伟达 GPU 的驱动程序?

在 Ubuntu 下安装 NVIDIA GPU 驱动程序的方法如下&#xff1a; 打开终端&#xff0c;并检查您的 GPU 型号&#xff1a;lspci | grep -i nvidia。如果您已经知道您的 GPU 型号&#xff0c;可以跳过此步。 添加 NVIDIA 的软件源。 首先&#xff0c;确认您的系统已经安装了 Secur…

Python代码写好了怎么运行

Python代码写好了怎么运行&#xff1f;相信问这样问题的朋友一定是刚刚入门Python的初学者。本文就来为大家详细讲讲如何运行Python代码。 一般来讲&#xff0c;运行Python代码的方式有两种&#xff0c;一是在Python交互式命令行下运行&#xff1b;另一种是使用文本编辑器&…

用ChatGPT一分钟自动产出一份高质量PPT

如何用ChatGPT一分钟自动产出一份高质量PPT&#xff0c;节约时间摸鱼呢&#xff1f;废话少说&#xff0c;直接上案例。 一.用ChatGPT做一下提问&#xff0c;这里我用的小程序万事知天下&#xff0c;根据自己PPT的需求&#xff0c;制作chatgpt的prompt就行了。 请帮我创建一个以…

Mysql面试必知的知识点-干货分享

文章目录 底层索引为什么使用B树,而不用B树?为什么Innodb索引建议必须建主键?为什么主键推荐使用整形自增?Mysql底层索引只有B树吗?联合索引底层长什么样子?数据库隔离级别中串行化是怎么实现的?查询方法需要加事务吗?大事务有什么影响? 底层索引为什么使用B树,而不用B…

海思芯片pcie启动——pcie_mcc驱动框架的booter程序分析

1、booter程序介绍 (1)源码目录:pcie_mcc/multi_boot/example/boot_test.c; (2)调用命令:./booter start_device; (3)booter程序的作用:在主片将pcie启动相关的驱动加载完成后,调用booter来引导从片pcie启动; 2、主片引导从片启动的过程 (1)调用pcie启动相关驱动,知道当…

PCA主成分分析 | 机器学习

1、概述(Principal componet analysis,PCA) 是一种无监督学习方法&#xff0c;是为了降低特征的维度。将原始高维数据转化为低维度的数据&#xff0c;高维数据指的是数据的特征维度较多&#xff0c;找到一个坐标系&#xff0c;使得这些数据特征映射到一个二维或三维的坐标系中…

【2023】某python语言程序设计跟学第八周内容【完】

目录 1.从数据处理到人工智能1.1.python库之数据分析1.2.python库之数据可视化1.3.python库之文本处理1.4.python库之机器学习 2.实例&#xff1a;霍兰德人格分析雷达图3.从web解析到网络空间3.1.python库之网络爬虫3.2.python库之web信息提取3.3.Python库之web网站开发3.4.pyt…