当前位置:网站首页>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
n
nodes, nodes numbered0
ton - 1
, where each node has at most one outgoing edge.The graph is represented by an array
edges
of sizen
with subscripts starting at 0, nodesi
toThere is a directed edge between the nodesedges[i]
.If nodei
has 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.length
2 <= n <= 1e5
-1 <= edges[i] < n
edges[i] != i
Source: 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;}}
边栏推荐
- Golang:go模版引擎的使用
- 22 Niu Ke Duo School 1 I. Chiitoitsu (Probability dp)
- The use of Golang: go template engine
- What do the values 1, 2, and 3 in nodetype mean?
- 日志导致线程Block的这些坑,你不得不防
- Go 支持 OOP: 用 struct 代替 class
- pytest接口自动化测试框架 | parametrize叠加使用
- 22 Grab the Seat 1 C.Grab the Seat (Geometry + Violence)
- [Tear AHB-APB Bridge by hand]~ Why aren't the lower two bits of the AHB address bus used to represent the address?
- 支付宝如何生成及配置公钥证书
猜你喜欢
随机推荐
22牛客多校1 I. Chiitoitsu (概率dp)
2022.7.31-----leetcode.1161
C语言学习概览(一)
LeetCode240+312+394
22 Grab the Seat 1 C.Grab the Seat (Geometry + Violence)
pytest interface automation testing framework | skip test classes
Chapter 9 of Huawei Deep Learning Course - Convolutional Neural Network and Case Practice
pytest接口自动化测试框架 | 跳过测试类
Leetcode - 6135: the longest part of the figure
Golang: go to connect and use mysql
pytest接口自动化测试框架 | 单个/多个参数
2022杭电中超杯(1)个人题解
USB Protocol (2) Terminology
Delphi MDI appliction 文档最大化显示、去掉最大化最小化等按钮
POJ2421道路建设题解
gethostbyname \ getaddrinfo 解析域名IP地址不安全的原因
Mysql database deployment and initialization steps
pytest接口自动化测试框架 | parametrize中ids的用法
聊一聊ICMP协议以及ping的过程
The use of Golang: go template engine