565. Array Nesting

Problem statement:

A zero-indexed array A consisting of N different integers is given. The array contains all integers in the range [0, N - 1].

Sets S[K] for 0 <= K < N are defined as follows:

S[K] = { A[K], A[A[K]], A[A[A[K]]], ... }.

Sets S[K] are finite for each K and should NOT contain duplicates.

Write a function that given an array A consisting of N integers, return the size of the largest set S[K] for this array.

Example 1:

Input: A = [5,4,0,3,1,6,2]
Output: 4
Explanation:
A[0] = 5, A[1] = 4, A[2] = 0, A[3] = 3, A[4] = 1, A[5] = 6, A[6] = 2.
One of the longest S[K]:
S[0] = {A[0], A[5], A[6], A[2]} = {5, 6, 2, 0}

Note:

  1. N is an integer within the range [1, 20,000].
  2. The elements of A are all distinct.
  3. Each element of array A is an integer within the range [0, N-1].

Solution:

This is a DFS solution. I solved it by employing the DFS template with a returned length of S[k].

For each S[k], it forms a circle. It means any element in this circle returns the same length.

For the purpose of pruning, we set a visited array to denote whether current element has been visited before.

Time complexity is O(n).

Space complexity is O(n).

class Solution {
public:
    int arrayNesting(vector<int>& nums) {
        int largest = 0;
        vector<int> visited(nums.size(), 0);
        for(int i = 0; i < nums.size(); i++){
            largest = max(largest, largest_nesting(nums, visited, nums[i], 0));
        }
        return largest;
    }
    int largest_nesting(vector<int>& nums, vector<int>& visited, int idx, int size){
        if(visited[nums[idx]] == 0){
            visited[nums[idx]] = 1;
            return largest_nesting(nums, visited, nums[idx], size + 1);
        } else {
            return size;
        }
    }
};
时间: 2024-10-13 04:52:29

565. Array Nesting的相关文章

[LeetCode] Array Nesting 数组嵌套

A zero-indexed array A consisting of N different integers is given. The array contains all integers in the range [0, N - 1]. Sets S[K] for 0 <= K < N are defined as follows: S[K] = { A[K], A[A[K]], A[A[A[K]]], ... }. Sets S[K] are finite for each K

[Swift]LeetCode565. 数组嵌套 | Array Nesting

A zero-indexed array A of length N contains all integers from 0 to N-1. Find and return the longest length of set S, where S[i] = {A[i], A[A[i]], A[A[A[i]]], ... } subjected to the rule below. Suppose the first element in S starts with the selection

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

【LeetCode】数组

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [1]Two Sum [4]Median of Two Sorted Arrays [11]Container With Most Water [15]3Sum [16]3Sum Closest [18]4Sum [26]Remove Duplicates from Sorted Array [27]Remove Element [31]Next Permutatio

ES6之Array.from()方法

Array.from()方法就是将一个类数组对象或者可遍历对象转换成一个真正的数组. 那么什么是类数组对象呢?所谓类数组对象,最基本的要求就是具有length属性的对象. 1.将类数组对象转换为真正数组: let arrayLike = { 0: 'tom', 1: '65', 2: '男', 3: ['jane','john','Mary'], 'length': 4 } let arr = Array.from(arrayLike) console.log(arr) // ['tom','6

JavaScript Array --&gt;map()、filter()、reduce()、forEach()函数的使用

题目: 1.得到 3000 到 3500 之内工资的人. 2.增加一个年龄的字段,并且计算其年龄. 3.打印出每个人的所在城市 4.计算所有人的工资的总和. 测试数据: function getData() { var arr = [{ id: 1, name: 'ohzri', birth: '1999.09.09', city: '湖北', salary: 9379 }, { id: 2, name: 'rqgfd', birth: '1999.10.28', city: '湖北', sal

Array.from()方法就是将一个类数组对象或者可遍历对象转换成一个真正的数组。

详解:https://www.cnblogs.com/jf-67/p/8440758.html Array.from()方法就是将一个类数组对象或者可遍历对象转换成一个真正的数组. 1. let arrayLike = { 0: 'tom', 1: '65', 2: '男', 3: ['jane','john','Mary'], 'length': 4 } let arr = Array.from(arrayLike) console.log(arr) // ['tom','65','男',['

PHP Warning: array_multisort(): Array sizes are inconsistent

array_multisort() 函数返回排序数组.您可以输入一个或多个数组.函数先对第一个数组进行排序,接着是其他数组,如果两个或多个值相同,它将对下一个数组进行排序. 遇到这报错是两个数组对比不一致导致的, 如果是一维数组与二维数组进行排序可以用以下方法解决: 使用这个方法,会比较麻烦些,要将age提取出来存储到一维数组里,然后按照age升序排列.具体代码如下: 复制代码代码如下: $ages = array();foreach ($users as $user) {    $ages[]

详解go语言的array和slice 【二】

上一篇  详解go语言的array和slice [一]已经讲解过,array和slice的一些基本用法,使用array和slice时需要注意的地方,特别是slice需要注意的地方比较多.上一篇的最后讲解到创建新的slice时使用第三个索引来限制slice的容量,在操作新slice时,如果新slice的容量大于长度时,添加新元素依然后使源的相应元素改变.这一篇里我会讲解到如何避免这些问题,以及迭代.和做为方法参数方面的知识点. slice的长度和容量设置为同一个值 如果在创建新的slice时我们把