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 = ;
vector<int> visited(nums.size(), );
for(int i = ; i < nums.size(); i++){
largest = max(largest, largest_nesting(nums, visited, nums[i], ));
}
return largest;
}
int largest_nesting(vector<int>& nums, vector<int>& visited, int idx, int size){
if(visited[nums[idx]] == ){
visited[nums[idx]] = ;
return largest_nesting(nums, visited, nums[idx], size + );
} else {
return size;
}
}
};

565. Array Nesting的更多相关文章

  1. 【leetcode】565. Array Nesting

    You are given an integer array nums of length n where nums is a permutation of the numbers in the ra ...

  2. [LeetCode] 565. Array Nesting 数组嵌套

    A zero-indexed array A of length N contains all integers from 0 to N-1. Find and return the longest ...

  3. 【LeetCode】565. Array Nesting 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  4. [LeetCode] Array Nesting 数组嵌套

    A zero-indexed array A consisting of N different integers is given. The array contains all integers ...

  5. [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 ...

  6. leetcode array解题思路

    Array *532. K-diff Pairs in an Array 方案一:暴力搜索, N平方的时间复杂度,空间复杂度N 数组长度为10000,使用O(N平方)的解法担心TLE,不建议使用,尽管 ...

  7. Leetcode Tags(2)Array

    一.448. Find All Numbers Disappeared in an Array 给定一个范围在 1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了 ...

  8. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

  9. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

随机推荐

  1. php数组与字符串转换

    1.将字符串转换成数组的几个函数: (1)explode(separate,string) 示例:$str = "Hello world It's a beautiful day" ...

  2. Java GUI 基础组件

    1.JLabel  标签 构造函数: JLabel() JLabel(String text) JLabel(String text,int align)     //第二个参数设置文本的对齐方式,常 ...

  3. 【java基础】Java锁机制

    在读很多并发文章中,会提及各种各样锁如公平锁,乐观锁等等,这篇文章介绍各种锁的分类.介绍的内容如下: 公平锁/非公平锁 可重入锁 独享锁/共享锁(广义) 互斥锁/读写锁(独享锁/共享锁的实现) 乐观锁 ...

  4. SpringMvc之参数绑定注解详解

    引言: 前段时间项目中用到了REST风格来开发程序,但是当用POST.PUT模式提交数据时,发现服务器端接受不到提交的数据(服务器端参数绑定没有加任何注解),查看了提交方式为application/j ...

  5. UI常用字体定义和继承的实例,ResearchKitCode

    #import <UIKit/UIKit.h> @interface UIFont (APCAppearance) + (UIFont*) appRegularFontWithSize: ...

  6. java语言基础-类型运算细节

    代码一: public class varDemo{ public static void main(String[] args) { byte a2; a2=3+4; System.out.prin ...

  7. dede网站目录权限设置

    如果你的网站数据十分重要(那种两天就能弄好的垃圾站就算了),建议按本文所说的安全步骤进行严格的设置.1.目录权限 我们不建议用户把栏目目录设置在根目录, 原因是这样进行安全设置会十分的麻烦, 在默认的 ...

  8. 在一台电脑上运行两个或多个tomcat

    在一台电脑上运行多个tomcat 在本例中,使用两个tomcat做示例   工具/原料   tomcat 安装好jdk,并且配置好环境变量 方法/步骤     首先去apache下载一个tomcat, ...

  9. Android天天数钱游戏项目源码

    Android天天数钱游戏源码,源码功能,天天数钱,这个游戏现在很多线上的小游戏都有这个了,游戏项目是在基于android游戏代码,大家可以参考一下. 源码下载:http://code.662p.co ...

  10. 我所理解的MVVM

    将UI中的数据适配.交互处理: controller中与UI密切相关的功能: 剥离出来,形成单独的模块: 以增加UI和Controller的灵活性.