Skip to content

feat: add solutions to lc problem: No.2360 #4298

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 42 additions & 4 deletions solution/2300-2399/2360.Longest Cycle in a Graph/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,23 +208,23 @@ func longestCycle(edges []int) int {
```ts
function longestCycle(edges: number[]): number {
const n = edges.length;
const vis = new Array(n).fill(false);
const vis: boolean[] = Array(n).fill(false);
let ans = -1;
for (let i = 0; i < n; ++i) {
if (vis[i]) {
continue;
}
let j = i;
const cycle: number[] = [];
for (; j != -1 && !vis[j]; j = edges[j]) {
for (; j !== -1 && !vis[j]; j = edges[j]) {
vis[j] = true;
cycle.push(j);
}
if (j == -1) {
if (j === -1) {
continue;
}
for (let k = 0; k < cycle.length; ++k) {
if (cycle[k] == j) {
if (cycle[k] === j) {
ans = Math.max(ans, cycle.length - k);
break;
}
Expand All @@ -234,6 +234,44 @@ function longestCycle(edges: number[]): number {
}
```

#### Rust

```rust
impl Solution {
pub fn longest_cycle(edges: Vec<i32>) -> i32 {
let n = edges.len();
let mut vis = vec![false; n];
let mut ans = -1;

for i in 0..n {
if vis[i] {
continue;
}
let mut j = i as i32;
let mut cycle = Vec::new();

while j != -1 && !vis[j as usize] {
vis[j as usize] = true;
cycle.push(j);
j = edges[j as usize];
}

if j == -1 {
continue;
}

for k in 0..cycle.len() {
if cycle[k] == j {
ans = ans.max((cycle.len() - k) as i32);
break;
}
}
}
ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
46 changes: 42 additions & 4 deletions solution/2300-2399/2360.Longest Cycle in a Graph/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,23 +202,23 @@ func longestCycle(edges []int) int {
```ts
function longestCycle(edges: number[]): number {
const n = edges.length;
const vis = new Array(n).fill(false);
const vis: boolean[] = Array(n).fill(false);
let ans = -1;
for (let i = 0; i < n; ++i) {
if (vis[i]) {
continue;
}
let j = i;
const cycle: number[] = [];
for (; j != -1 && !vis[j]; j = edges[j]) {
for (; j !== -1 && !vis[j]; j = edges[j]) {
vis[j] = true;
cycle.push(j);
}
if (j == -1) {
if (j === -1) {
continue;
}
for (let k = 0; k < cycle.length; ++k) {
if (cycle[k] == j) {
if (cycle[k] === j) {
ans = Math.max(ans, cycle.length - k);
break;
}
Expand All @@ -228,6 +228,44 @@ function longestCycle(edges: number[]): number {
}
```

#### Rust

```rust
impl Solution {
pub fn longest_cycle(edges: Vec<i32>) -> i32 {
let n = edges.len();
let mut vis = vec![false; n];
let mut ans = -1;

for i in 0..n {
if vis[i] {
continue;
}
let mut j = i as i32;
let mut cycle = Vec::new();

while j != -1 && !vis[j as usize] {
vis[j as usize] = true;
cycle.push(j);
j = edges[j as usize];
}

if j == -1 {
continue;
}

for k in 0..cycle.len() {
if cycle[k] == j {
ans = ans.max((cycle.len() - k) as i32);
break;
}
}
}
ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
33 changes: 33 additions & 0 deletions solution/2300-2399/2360.Longest Cycle in a Graph/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
impl Solution {
pub fn longest_cycle(edges: Vec<i32>) -> i32 {
let n = edges.len();
let mut vis = vec![false; n];
let mut ans = -1;

for i in 0..n {
if vis[i] {
continue;
}
let mut j = i as i32;
let mut cycle = Vec::new();

while j != -1 && !vis[j as usize] {
vis[j as usize] = true;
cycle.push(j);
j = edges[j as usize];
}

if j == -1 {
continue;
}

for k in 0..cycle.len() {
if cycle[k] == j {
ans = ans.max((cycle.len() - k) as i32);
break;
}
}
}
ans
}
}
8 changes: 4 additions & 4 deletions solution/2300-2399/2360.Longest Cycle in a Graph/Solution.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
function longestCycle(edges: number[]): number {
const n = edges.length;
const vis = new Array(n).fill(false);
const vis: boolean[] = Array(n).fill(false);
let ans = -1;
for (let i = 0; i < n; ++i) {
if (vis[i]) {
continue;
}
let j = i;
const cycle: number[] = [];
for (; j != -1 && !vis[j]; j = edges[j]) {
for (; j !== -1 && !vis[j]; j = edges[j]) {
vis[j] = true;
cycle.push(j);
}
if (j == -1) {
if (j === -1) {
continue;
}
for (let k = 0; k < cycle.length; ++k) {
if (cycle[k] == j) {
if (cycle[k] === j) {
ans = Math.max(ans, cycle.length - k);
break;
}
Expand Down