第 394 场 LeetCode 周赛题解

embedded/2024/9/22 11:06:29/

A leetcode.cn/problems/count-the-number-of-special-characters-i/" rel="nofollow">统计特殊字母的数量 I

在这里插入图片描述

哈希:遍历然后枚举

class Solution {public:int numberOfSpecialChars(string word) {unordered_map<char, int> m;for (auto ch : word)m[ch] = 1;int res = 0;for (char ch = 'a'; ch <= 'z'; ch++)if (m.count(ch) && m.count('A' + ch - 'a'))res++;return res;}
};

B leetcode.cn/problems/count-the-number-of-special-characters-ii/" rel="nofollow">count-the-number-of-special-characters-ii/

在这里插入图片描述

哈希:遍历记录各小写字母的最后出现下标,及各大写字母的第一次出现的下标,然后枚举

class Solution {public:int numberOfSpecialChars(string word) {unordered_map<char, int> m;for (int i = 0; i < word.size(); i++)if (word[i] >= 'a' && word[i] <= 'z' || m.find(word[i]) == m.end())m[word[i]] = i;int res = 0;for (char ch = 'a'; ch <= 'z'; ch++)if (m.count(ch) && m.count('A' + ch - 'a') && m[ch] < m['A' + ch - 'a'])res++;return res;}
};

C leetcode.cn/problems/minimum-number-of-operations-to-satisfy-conditions/" rel="nofollow">使矩阵满足条件的最少操作次数

在这里插入图片描述

动态规划:设 p [ i ] [ j ] p[i][j] p[i][j] 为使 g r i d grid grid 的前 i + 1 i+1 i+1 列行成的子矩阵满足条件的且最后一列都为 j j j 的最少操作数,最终答案为 m i n { p [ n − 1 ] [ j ] ∣ 0 ≤ j ≤ 9 } min\{ p[n-1][j] \;|\; 0\le j\le 9 \} min{p[n1][j]0j9}

class Solution {public:int minimumOperations(vector<vector<int>>& grid) {int m = grid.size(), n = grid[0].size();vector<vector<int>> col(n, vector<int>(10));//col[i][j]: 第i列中j的数目for (int i = 0; i < m; i++)for (int j = 0; j < n; j++)col[j][grid[i][j]]++;vector<vector<int>> p(n, vector<int>(10));for (int j = 0; j < 10; j++)p[0][j] = m - col[0][j];for (int j = 1; j < n; j++) {for (int cur = 0; cur < 10; cur++) {p[j][cur] = INT32_MAX;for (int last = 0; last < 10; last++)//枚举前一行if (last != cur)p[j][cur] = min(p[j][cur], p[j - 1][last] + m - col[j][cur]);}}int res = INT32_MAX;for (int v = 0; v < 10; v++)res = min(res, p[n - 1][v]);return res;}
};

D leetcode.cn/problems/find-edges-in-shortest-paths/" rel="nofollow">最短路径中的边

在这里插入图片描述

最短路 + b f s bfs bfs:设 d [ i ] d[i] d[i] 0 0 0 i i i最短路的长度,先用最 d i j k s t r a dijkstra dijkstra 0 0 0 n − 1 n-1 n1最短路。如果 0 0 0 n − 1 n-1 n1 连通,则从 n − 1 n-1 n1 开始 b f s bfs bfs:设 b f s bfs bfs 当前遍历到的节点为 i i i,若 i i i 的邻接点 j j j 满足 d [ j ] + w { j , i } = d [ i ] d[j]+w_{\{j,i\}}=d[i] d[j]+w{j,i}=d[i] ,则将 a n s w e r answer answer 数组中 ( j , i ) (j,i) (j,i) 边对应的下标位置置为 t r u e true true ,同时将 j j j 加入 b f s bfs bfs 队列。

class Solution {public:using ll = long long;vector<bool> findAnswer(int n, vector<vector<int>>& edges) {vector<vector<pair<int, int>>> e(n);map<pair<int, int>, int> id;//记录各表在edges中的下标for (int i = 0; i < edges.size(); i++) {//建图auto& edge = edges[i];e[edge[0]].push_back({edge[1], edge[2]});e[edge[1]].push_back({edge[0], edge[2]});id[{min(edge[0], edge[1]), max(edge[0], edge[1])}] = i;}vector<ll> d(n, INT64_MAX);d[0] = 0;priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> q;//最小堆q.emplace(0, 0);while (!q.empty()) {//求最短路auto [di, i] = q.top();q.pop();for (auto [j, w] : e[i]) {if (d[j] > di + w) {d[j] = di + w;q.emplace(d[j], j);}}}vector<int> vis(n);queue<int> qu;vector<bool> res(edges.size());if (d[n - 1] != INT64_MAX)qu.push(n - 1);while (!qu.empty()) {//bfsauto i = qu.front();qu.pop();for (auto [j, w] : e[i]) {if (d[j] != INT64_MAX && d[j] + w == d[i]) {res[id[make_pair(min(i, j), max(i, j))]] = true;if (!vis[j]) {vis[j] = 1;//入队标记qu.push(j);}}}}return res;}
};

http://www.ppmy.cn/embedded/13211.html

相关文章

Laravel 6 - 第十五章 验证器

​ 文章目录 Laravel 6 - 第一章 简介 Laravel 6 - 第二章 项目搭建 Laravel 6 - 第三章 文件夹结构 Laravel 6 - 第四章 生命周期 Laravel 6 - 第五章 控制反转和依赖注入 Laravel 6 - 第六章 服务容器 Laravel 6 - 第七章 服务提供者 Laravel 6 - 第八章 门面 Laravel 6 - …

【图片格式转换】ICO、JPG、JPEG、PNG图片格式在线免费转换

ICO、JPG、JPEG、PNG图片格式转换 图片格式转换 https://orcc.online/image 支持ICO、JPG、JPEG、PNG等 主页 https://www.orcc.online 其他工具 pdf在线免费转word文档 https://orcc.online/pdf 时间戳转换 https://orcc.online/timestamp Base64 编码解码 https://orcc…

QT中的OpenGL学习-----3D图形

一、3D坐标系 记住V_clip M_projection * M_view * M_model * V_local就行&#xff0c;可以在顶点着色器里面添加位置信息&#xff1a; #version 330 core layout (location 2) in vec3 aPos;//location属性位置有16个 layout (location 3) in vec3 aColor; layout (locati…

Python 异常处理与日志记录

&#x1f47d;发现宝藏 前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住分享一下给大家。【点击进入巨牛的人工智能学习网站】。 异常处理是任何编程语言中的重要组成部分&#xff0c;Python 也不例外。Python 提供了丰富的…

详解数据结构:栈

一、顺序栈 顺序栈的存储方式如下&#xff1a; 从图中可以看出&#xff0c;顺序栈需要两个指针&#xff0c;base指向栈底&#xff0c;top指向栈顶。 typedef struct SqStack {ElemType *base; //栈底指针ElemType *top; //栈顶指针}SqStack; 说明&#xff1a; ElemType是元…

数仓建模—物理数据模型

数仓建模—物理数据模型 前面我们讲了数据模型和逻辑数据模型,你可以参考前面的文章,这一节我们介绍一下物理数据模型 数仓建模—数据模型 数仓建模—逻辑数据模型 什么是物理数据模型 物理数据模型指定如何在数据库中构建数据模型。它概述了所有表结构,包括列名、数据类…

Qt 运行 Android 程序时找不到 Toou2D 库闪退

问题描述 程序闪退&#xff0c;错误信息如下&#xff0c;找不到库。 W libAndroid10_armeabi-v7a.so: QQmlApplicationEngine failed to load component W libAndroid10_armeabi-v7a.so: qrc:/main.qml:3:1: plugin cannot be loaded for module "Toou2D": Cannot …

【一些神金】怎么缓解工作压力?使用VS-code彩虹屁插件

怎么缓解工作压力&#xff1f; 其实吃点好的&#xff0c;多睡一会儿&#xff0c;再锻炼锻炼身体就好。 但我只是想炫耀一下这个彩虹屁插件。 原版插件&#xff1a;VS-code-Rainbowfart 我的版本&#xff1a;RainbowFart-Oberon 基于 MIT 开源&#xff0c;包括所有设计资源及音…