565. Array Nesting Medium

@problem@discussion
#Array#Depth-First Search



1/**
2 * [565] Array Nesting
3 *
4 * You are given an integer array nums of length n where nums is a permutation of the numbers in the range [0, n - 1].
5 * You should build a set s[k] = {nums[k], nums[nums[k]], nums[nums[nums[k]]], ... } subjected to the following rule:
6 * 
7 * 	The first element in s[k] starts with the selection of the element nums[k] of index = k.
8 * 	The next element in s[k] should be nums[nums[k]], and then nums[nums[nums[k]]], and so on.
9 * 	We stop adding right before a duplicate element occurs in s[k].
10 * 
11 * Return the longest length of a set s[k].
12 *  
13 * Example 1:
14 * 
15 * Input: nums = [5,4,0,3,1,6,2]
16 * Output: 4
17 * Explanation: 
18 * nums[0] = 5, nums[1] = 4, nums[2] = 0, nums[3] = 3, nums[4] = 1, nums[5] = 6, nums[6] = 2.
19 * One of the longest sets s[k]:
20 * s[0] = {nums[0], nums[5], nums[6], nums[2]} = {5, 6, 2, 0}
21 * 
22 * Example 2:
23 * 
24 * Input: nums = [0,1,2]
25 * Output: 1
26 * 
27 *  
28 * Constraints:
29 * 
30 * 	1 <= nums.length <= 10^5
31 * 	0 <= nums[i] < nums.length
32 * 	All the values of nums are unique.
33 * 
34 */
35pub struct Solution {}
36
37// problem: https://leetcode.com/problems/array-nesting/
38// discuss: https://leetcode.com/problems/array-nesting/discuss/?currentPage=1&orderBy=most_votes&query=
39
40// submission codes start here
41
42impl Solution {
43    pub fn array_nesting(nums: Vec<i32>) -> i32 {
44        0
45    }
46}
47
48// submission codes end
49
50#[cfg(test)]
51mod tests {
52    use super::*;
53
54    #[test]
55    fn test_565() {
56    }
57}
58


Back
© 2025 bowen.ge All Rights Reserved.