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的更多相关文章

  1. 【LeetCode】851. Loud and Rich 解题报告(Python)

    [LeetCode]851. Loud and Rich 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:// ...

  2. 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 ...

  3. 849. Maximize Distance to Closest Person ——weekly contest 87

    849. Maximize Distance to Closest Person 题目链接:https://leetcode.com/problems/maximize-distance-to-clo ...

  4. 848.Shifting Letters——weekly contest 87

    848. Shifting Letters 题目链接:https://leetcode.com/problems/shifting-letters/description/ 思路:O(N^2)复杂度过 ...

  5. LeetCode Weekly Contest 8

    LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...

  6. Leetcode Weekly Contest 86

    Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...

  7. leetcode weekly contest 43

    leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...

  8. LeetCode Weekly Contest 23

    LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...

  9. LeetCode之Weekly Contest 91

    第一题:柠檬水找零 问题: 在柠檬水摊上,每一杯柠檬水的售价为 5 美元. 顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯. 每位顾客只买一杯柠檬水,然后向你付 5 美元.10  ...

随机推荐

  1. 08 . Jenkins之SpringCloud微服务+Vue+Docker持续集成

    简介 大致流程 /* 1.开发人员每天把代码提交到Gitlab代码仓库 2.jenkins从gitlab中拉取项目源码,编译并打包成war包,然后构建Docker镜像,将镜像上传到Harbor私有仓库 ...

  2. 03 sublime text3下配置Java的编译运行环境

    参考如下文章,加入了自己的干货: https://blog.csdn.net/qq_38295511/article/details/81140069 https://blog.csdn.net/qq ...

  3. 02 C语言最简单快速上手的IDE

    不要让开发环境 成为学习路上的拦路虎,先培养学习兴趣和学习路上的成就感,这个对于激励自己持续学习很重要. 等真正从小白入门了,甚至成为大牛了,能解决诸多困难问题了,安装个开发环境还会再是个什么难事吗? ...

  4. Matlab .asv文件

    参考: https://blog.csdn.net/u013152895/article/details/44724199 有时在存放m文件的文件夹中会出现*.asv asv 就是auto save的 ...

  5. MyEclipse 设置打开 jsp 文件的默认编辑器

    MyEclipse 版本信息 MyEclipse Enterprise Workbench Version: 2014 Build id: 12.0.0-20131202 菜单 Window > ...

  6. Magicodes.IE 2.4版本发布

    今天我们发布了2.4版本,这离不开大家对Magicodes.IE的支持,我们也对大家的意见以及需求不断的进行更新迭代,目前我们的发布频率平均在一周一个beta版本,一个月一个正式版本的更新,我们欢迎更 ...

  7. ORA-28001: the password has expired 密码已过期

    ORA-28001: the password has expiredORA-28001: 密码已过期 Cause:       The user's account has expired and ...

  8. gitlab介绍

    1. GitLab简介    GitLab是一个利用 Ruby on Rails 开发的开源应用程序,实现一个自托管的Git项目仓库,可通过Web界面进行访问公开的或者私人项目. GitLab拥有与G ...

  9. Pythonic【15个代码示例】

    Python由于语言的简洁性,让我们以人类思考的方式来写代码,新手更容易上手,老鸟更爱不释手. 要写出 Pythonic(优雅的.地道的.整洁的)代码,还要平时多观察那些大牛代码,Github 上有很 ...

  10. 好用的C语言编程软件!工具都没有,怎么用技术改变世界呢!

    好用的C语言编程软件 1.VS(Visual Studio)   VS(Visual Studio) VS是目前最流行的windows平台应用程序的集成开发环境,由于大部分同学使用的都是Windows ...