851. Loud and Rich —— weekly contest 87
851. Loud and Rich
题目链接:https://leetcode.com/problems/loud-and-rich/description/
思路:有向图DFS,记录最小的quiet值
注意点:可优化,记忆性搜索,每次搜到已经记录过的值时可直接比较不需要进一步搜下去。
1 void DFS(vector<int> &visited, vector<vector<int> >& G, int x,vector<int>& quiet, int &res,int &minid){
2 visited[x] = 1;
3 if(res > quiet[x]){
4 res = quiet[x];
5 minid = x;
6 }
7 for(auto i:G[x]){
8 if(visited[i] == 0){
9 DFS(visited,G,i,quiet,res,minid);
10 }
11 }
12 return;
13 }
14 vector<int> loudAndRich(vector<vector<int>>& richer, vector<int>& quiet) {
15 vector<int> ans;
16 int n = quiet.size();
17 vector<vector<int> > G;
18 G.resize(n);
19 for(auto i : richer){
20 G[i[1]].push_back(i[0]);
21 }
22 vector<int> visited;
23
24 for(int i = 0 ; i < n; i++){
25 int res = 100000;
26 int minid = -1;
27 visited.assign(n,0);
28 DFS(visited,G,i,quiet,res,minid);
29 ans.push_back(minid);
30 }
31 return ans;
32 }
851. Loud and Rich —— weekly contest 87的更多相关文章
- 【LeetCode】851. Loud and Rich 解题报告(Python)
[LeetCode]851. Loud and Rich 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:// ...
- LC 851. Loud and Rich
In a group of N people (labelled 0, 1, 2, ..., N-1), each person has different amounts of money, and ...
- 849. Maximize Distance to Closest Person ——weekly contest 87
849. Maximize Distance to Closest Person 题目链接:https://leetcode.com/problems/maximize-distance-to-clo ...
- 848.Shifting Letters——weekly contest 87
848. Shifting Letters 题目链接:https://leetcode.com/problems/shifting-letters/description/ 思路:O(N^2)复杂度过 ...
- LeetCode Weekly Contest 8
LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...
- Leetcode Weekly Contest 86
Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...
- leetcode weekly contest 43
leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...
- LeetCode Weekly Contest 23
LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...
- LeetCode之Weekly Contest 91
第一题:柠檬水找零 问题: 在柠檬水摊上,每一杯柠檬水的售价为 5 美元. 顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯. 每位顾客只买一杯柠檬水,然后向你付 5 美元.10 ...
随机推荐
- 安卓app功能或自动化测试覆盖率统计(不用instrumentation启动app)
一文带你揭秘如何采取非instrumentation启动app,打造实时统计覆盖率,一键触发覆盖率测试报告. 在上篇文章,一文带你解决Android app手工测试或者自动化测试覆盖率统计(撸代码版) ...
- __declspec(dllexport)和__declspec(dllimport) (——declspec方法创建dll的方法已验证ok)
转载:https://www.cnblogs.com/chengbing2011/p/4084125.html __declspec(dllimport)和__declspec(dllexport)经 ...
- 固件(Firmware)
来源:https://baike.baidu.com/item/%E5%9B%BA%E4%BB%B6/627829 固件 固件(Firmware)就是写入EPROM(可擦写可编程只读存储器)或EE ...
- Thymeleaf 异常:Exception processing template "index": An error happened during template parsing (template: "class path resource [templates/index.html]")
Spring Boot 项目,在 Spring Tool Suite 4, Version: 4.4.0.RELEASE 运行没有问题,将项目中的静态资源和页面复制到 IDEA 的项目中,除了 IDE ...
- 史上最全Python快速入门教程,满满都是干货
Python是面向对象,高级语言,解释,动态和多用途编程语言.Python易于学习,而且功能强大,功能多样的脚本语言使其对应用程序开发具有吸引力.Python的语法和动态类型具有其解释性质,使其成为许 ...
- Spark 单机环境配置
概要 Spark 单机环境配置 JDK 环境配置 Spark 环境配置 python 环境配置 Spark 使用示例 示例代码 (order_stat.py) 测试用的 csv 文件内容 (order ...
- 解读JVM虚拟机
概要点: java虚拟机概述和基本概念 堆.栈.方法区 了解虚拟机参数 垃圾回收概念和算法.及对象的分代转换 垃圾收集器 java虚拟机的原理: 所谓虚拟机,就是一台虚拟的机器.它是一款软件,用来执行 ...
- 【人人都懂密码学】一篇最易懂的Java密码学入门教程
密码与我们的生活息息相关,远到国家机密,近到个人账户,我们每天都在跟密码打交道: 那么,密码从何而来?生活中常见的加密是怎么实现的?怎么保证个人信息安全?本文将从这几方面进行浅谈,如有纰漏,敬请各位大 ...
- go 实现websocket推送
index.html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> &l ...
- sql 操作表常用语句,语法
新增列:alter table 表名 add 新列名 数据类型 删除列:alter table 表名 drop column 列名 删除约束:alter table 表名 drop constrain ...