802. Find Eventual Safe States
https://leetcode.com/problems/find-eventual-safe-states/description/
class Solution {
public:
vector<bool> visited;
vector<int> mem; // -1 unknown, 0 unsafe, 1 safe.
int n;
vector<int> eventualSafeNodes(vector<vector<int>>& graph) {
n = graph.size();
mem = vector<int>(n, -1);
visited = vector<bool>(n, false);
for (int i = 0; i < n; i++)
dfs(graph, i);
vector<int> res;
for (int i = 0; i < n; i++)
if (mem[i] == 1)
res.push_back(i);
return res;
}
bool dfs(vector<vector<int>>& graph, int i) {
// check if i is evetually safe
if (mem[i] != -1) return mem[i] == 1;
bool res = true;
if (visited[i]) { // A loop
res = false;
}
else {
visited[i] = true;
for (auto j : graph[i]) {
if (!dfs(graph, j)) {
res = false;
break;
}
}
// visited[i] = false; // This line is optional, since we've cached the result for i in mem.
}
mem[i] = res;
return res;
}
};
802. Find Eventual Safe States的更多相关文章
- 【LeetCode】802. Find Eventual Safe States 解题报告(Python)
[LeetCode]802. Find Eventual Safe States 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemi ...
- LC 802. Find Eventual Safe States
In a directed graph, we start at some node and every turn, walk along a directed edge of the graph. ...
- LeetCode 802. Find Eventual Safe States
原题链接在这里:https://leetcode.com/problems/find-eventual-safe-states/ 题目: In a directed graph, we start a ...
- [LeetCode] 802. Find Eventual Safe States 找到最终的安全状态
In a directed graph, we start at some node and every turn, walk along a directed edge of the graph. ...
- 【leetcode】802. Find Eventual Safe States
题目如下: 解题思路:本题大多数人采用DFS的方法,这里我用的是另一种方法.我的思路是建立一次初始值为空的safe数组,然后遍历graph,找到graph[i]中所有元素都在safe中的元素,把i加入 ...
- [Swift]LeetCode802. 找到最终的安全状态 | Find Eventual Safe States
In a directed graph, we start at some node and every turn, walk along a directed edge of the graph. ...
- [LeetCode] Find Eventual Safe States 找到最终的安全状态
In a directed graph, we start at some node and every turn, walk along a directed edge of the graph. ...
- 算法与数据结构基础 - 图(Graph)
图基础 图(Graph)应用广泛,程序中可用邻接表和邻接矩阵表示图.依据不同维度,图可以分为有向图/无向图.有权图/无权图.连通图/非连通图.循环图/非循环图,有向图中的顶点具有入度/出度的概念. 面 ...
- 算法与数据结构基础 - 深度优先搜索(DFS)
DFS基础 深度优先搜索(Depth First Search)是一种搜索思路,相比广度优先搜索(BFS),DFS对每一个分枝路径深入到不能再深入为止,其应用于树/图的遍历.嵌套关系处理.回溯等,可以 ...
随机推荐
- Redis中的LRU淘汰策略分析
Redis作为缓存使用时,一些场景下要考虑内存的空间消耗问题.Redis会删除过期键以释放空间,过期键的删除策略有两种: 惰性删除:每次从键空间中获取键时,都检查取得的键是否过期,如果过期的话,就删除 ...
- htaccess转换httpd.ini方法及案例参考
案例1:httpd.ini适合IIS使用,.htaccess适合Apache使用,nginx.conf适合Nginx使用 转换前:httpd.ini [ISAPI_Rewrite] # 3600 = ...
- php函数超实用
DateTime DateTime::addDateTime::diffDateTime::formatDateTime::modifyDateTime::sub... * DateInterval ...
- java 基础 03 运算符 分支结构 循环结构
今天内容: (1)运算符 (2)分支结构 (3)循环结构 1运算符 1.1赋值运算符 (1)简单赋值 = 表示赋值运算符,用于将=右边的数据赋值给=左边的变量来覆盖原来的数值. 笔试题: ia == ...
- VMware Workstation Pro 14注册码,亲测可用
** VMware Workstation Pro 14注册码 ** 作者网上搜集整理 作者使用的密钥是: AC5XK-0ZD4H-088HP-9NQZV-ZG2R4 亲测可用 以下密钥未测试 CG5 ...
- SpringBoot的异步调用介绍
参考博客: https://www.cnblogs.com/jebysun/p/9675345.html https://blog.csdn.net/weixin_38399962/article/d ...
- js对jsonArray的操作
上一篇写了关于java中Gson对jsonArray的排序处理,这里介绍js中对jsonArray处理, <!DOCTYPE html> <html> <head> ...
- JS浏览器获取宽高
screen.availHeight is the height the browser's window can have if it is maximized. (including all th ...
- newCoder在线编程---(1)
二维数组查找 题目描述: 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. 1.暴 ...
- 【Python音乐生成】可能有用的一些Python库
1,Python-MIDI,很多操作库的前置库.作者提供了一个python3的branch.git clone下来之后注意切换到这个branch之后再运行setup.py. 实际使用的时候,使用 im ...