当前位置:网站首页>Leicester Weekly 304 6135. The longest ring in the picture Inward base ring tree
Leicester Weekly 304 6135. The longest ring in the picture Inward base ring tree
2022-08-01 08:14:00 【Yu Niangniang】
6135. Longest loop in the figure
Give you a directed graph of
nnodes, nodes numbered0ton - 1, where each node has at most one outgoing edge.The graph is represented by an array
edgesof sizenwith subscripts starting at 0, nodesitoThere is a directed edge between the nodesedges[i].If nodeihas no outgoing edges, thenedges[i] == -1.Please return the longest ring in the figure. If there is no ring, please return
-1.A ring is a path that starts and ends at the same node.
Example 1:
Input:edges = [3,3,4,2,3]Output to:3Explanation: The longest cycle in the graph is: 2 -> 4 -> 3 -> 2 .The length of this ring is 3 , so 3 is returned.Example 2:
Input:edges = [2,-1,3,1]Output:-1Explanation: There are no cycles in the graph.Tip:
n == edges.length2 <= n <= 1e5-1 <= edges[i] < nedges[i] != iSource: LeetCode
Link: https://leetcode.cn/problems/longest-cycle-in-a-graph
The copyright belongs to LeetCode.For commercial reprints, please contact the official authorization, and for non-commercial reprints, please indicate the source.
Results of the questions
Failed in the weekly competition, and I wrote T T after reviewing the game at night.Originally wanted to ask the big guy, and then tried it out
Method: Simulate

A single-pointer graph can eventually form one or more shapes similar to the above, with multiple outer lines connected to the inner ring

When we visit a duplicate node from a pointer, we may get the result corresponding to the red line above. Each point corresponding to the red line should be judged and marked, and then when we visit the node that has been visited at the red position, end directly
1. Access to a duplicate node: end of recording the difference between the current timestamp and the old timestamp, and record the visited point at the same time
2. Visiting an illegal node (a node visited by the old node): end directly, record the visited point
class Solution {public int longestCycle(int[] edges) {int n = edges.length;boolean[] visited = new boolean[n];int ans = -1;for(int i = 0; i < n; i++){if(visited[i]) continue;Map used = new HashMap<>();int pos = i;int size = 0;while (edges[pos]!=-1&&!used.containsKey(edges[pos])&&!visited[edges[pos]]){++size;pos = edges[pos];used.put(pos,size);}if(edges[pos]!=-1&&!visited[edges[pos]]){ans = Math.max(ans,size-used.get(edges[pos])+1);}for(Integer key:used.keySet()){visited[key]=true;}}return ans;}} 边栏推荐
- 七夕来袭——属于程序员的浪漫
- Mysql database deployment and initialization steps
- Self-made a remote control software - VeryControl
- zip打包目录所有文件(含隐藏文件/夹)
- JVM内存模型之深究模型特征
- 配置我的kitty
- special day to remember
- Go supports OOP: use struct instead of class
- Redis 3.2.3 crashed by signal: 11 服务宕机问题排查
- pytest interface automation testing framework | parametrize source code analysis
猜你喜欢
随机推荐
VoLTE基础学习系列 | 企业语音网简述
Image lossless compression software which works: try completely free JPG - C image batch finishing compression reduces weight tools | latest JPG batch dressing tools download
pytest接口自动化测试框架 | 集成Allure测试报告
毕业论文写作技巧
Fist game copyright-free music download, League of Legends copyright-free music, can be used for video creation, live broadcast
372. 超级次方
Pytest | skip module interface test automation framework
HoloView 在 jyputer lab/notebook 不显示总结
LeetCode 415:字符串相加
The use of Golang: go template engine
C语言中编译时出现警告C4013(C语言不加函数原型产生的潜在错误)
拳头游戏免版权音乐下载,英雄联盟无版权音乐,可用于视频创作、直播
GO error handling
Data Analysis 6
Chapters 6 and 7 of Huawei Deep Learning Course
基于tika实现对文件类型进行判断
【杭电多校第四场 B题】最短路图+缩点dp
flink sql-client,怎么处理源端与目标增加端,sql-client包括映射表与JOB如
179. 最大数
app 自动化 打开app (二)











