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:
- N is an integer within the range [1, 20,000].
- The elements of A are all distinct.
- 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的更多相关文章
- 【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 ...
- [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 ...
- 【LeetCode】565. Array Nesting 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [LeetCode] Array Nesting 数组嵌套
A zero-indexed array A consisting of N different integers is given. The array contains all integers ...
- [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 ...
- leetcode array解题思路
Array *532. K-diff Pairs in an Array 方案一:暴力搜索, N平方的时间复杂度,空间复杂度N 数组长度为10000,使用O(N平方)的解法担心TLE,不建议使用,尽管 ...
- Leetcode Tags(2)Array
一.448. Find All Numbers Disappeared in an Array 给定一个范围在 1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了 ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
随机推荐
- Castle.net
using System; using System.Collections.Generic; using System.Linq;using System.Text;using Castle.Act ...
- SSM-WebMVC(三)
SSM-WebMVC(三) 一.Annotated Controllers 应用程序控制器 handlerMethod(处理方法) ㈠方法入参 (springmvc针对于在controller ...
- new操作符具体干了什么
function Func(){ }; var newFunc=new Func (); new共经过了4个阶段 1.创建一个空对象 var obj=new Object(); 2.设置原型链 把 o ...
- 开发原生安卓cordova插件(有原生界面)
上文开发的插件没有调用原生界面,本文介绍开发带有activity的插件 本文很多操作与上文重复,重复部分会省略 首先打开plug1,先开发插件的原生代码 在以下命名空间创建一个activity 名称为 ...
- spring Existing transaction found for transaction marked with propagation 'never' 解决
先在申明事务中配置了所有的事务 <!--配置事物传播策略,以及隔离级别--> <tx:advice id="txAdvice" transaction-manag ...
- centos 6.2用yum安装中文输入法
centos 6.2用yum安装中文输入法 1.su root 2.yum install "@Chinese Support" 3.exit 4.回到桌面,system-> ...
- 四次元新浪微博客户端Android源码
四次元新浪微博客户端Android源码 源码下载:http://code.662p.com/list/11_1.html [/td][td] [/td][td] [/td][td] 详细说明:http ...
- MySQL系列(三)--数据库结构优化
良好的数据库逻辑设计和物理设计是数据库高性能的基础,所以对于数据库结构优化是很有必要的 数据库结构优化目的: 1.减少数据的冗余 2.尽量避免在数据插入.删除和更新异常 例如:有一张设计不得当的学生选 ...
- luogu 1113 杂务--啥?最长路?抱歉,我不会
P1113 杂务 题目描述 John的农场在给奶牛挤奶前有很多杂务要完成,每一项杂务都需要一定的时间来完成它.比如:他们要将奶牛集合起来,将他们赶进牛棚,为奶牛清洗乳房以及一些其它工作.尽早将所有杂务 ...
- 万能的搜索--之BFS(三)
接着(一)start (二)广度优先搜索(BFS) 广度优先搜索(又称宽度优先搜索算法)是最简便的图的搜索算法之一,这一算法也是很多重要的图的算法的原型. Dijkstra单源最短路径算法和Pri ...