当前位置:网站首页>2022.07.18 _ a day
2022.07.18 _ a day
2022-07-31 07:40:00 【No. い】
565. 数组嵌套
题目描述
索引从0开始长度为N的数组A,包含0到N - 1的所有整数.找到最大的集合S并返回其大小,其中 S[i] = {A[i], A[A[i]], A[A[A[i]]], ... }且遵守以下的规则.
假设选择索引为i的元素A[i]为S的第一个元素,S的下一个元素应该是A[A[i]],之后是A[A[A[i]]]... 以此类推,不断添加直到S出现重复的元素.
示例 1:
输入: A = [5,4,0,3,1,6,2]
输出: 4
解释:
A[0] = 5, A[1] = 4, A[2] = 0, A[3] = 3, A[4] = 1, A[5] = 6, A[6] = 2.
其中一种最长的 S[K]:
S[0] = {A[0], A[5], A[6], A[2]} = {5, 6, 2, 0}
提示:
N是[1, 20,000]之间的整数.A中不含有重复的元素.A中的元素大小在[0, N-1]之间.
- 深度优先搜索
- 数组
coding
1. 有向图
n 个不重复数字,范围【0,n - 1】,Can connect one by one or x A ring of directed graph i -> nums[i],Each ring does not exist cross,So once we pass a certain elements,In the future will not be using it again,So I can put it to,You can place to mark it as true.We need to traverse each ring,Get the most ring can be,如果是 isVis = true 的节点, It has been traversal,无需再次遍历,So just considering isVis = false 的节点开始遍历
class Solution {
public int arrayNesting(int[] nums) {
int res = 0;
boolean[] isVis = new boolean[nums.length];
for (int i = 0; i < nums.length; i++) {
int cnt = 0;
while (!isVis[i]) {
isVis[i] = true;
i = nums[i];
cnt ++;
}
res = Math.max(cnt, res);
}
return res;
}
}
2. In situ tag array
class Solution {
public int arrayNesting(int[] nums) {
int ans = 0, n = nums.length;
for (int i = 0; i < n; ++i) {
int cnt = 0;
while (nums[i] < n) {
int num = nums[i];
nums[i] = n;
i = num;
++cnt;
}
ans = Math.max(ans, cnt);
}
return ans;
}
}
3. 深搜 => 超时
class Solution {
public int arrayNesting(int[] nums) {
int max = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] != -1) {
Stack<Integer> stack = new Stack();
max = Math.max(dfs(nums, i, stack), max);
}
}
return max;
}
private int dfs(int[] nums, int index, Stack<Integer> stack) {
if (nums[index] == -1) {
return 0;
}
if (stack.contains(nums[index])) {
return stack.size();
}
stack.push(nums[index]);
int size = dfs(nums, nums[index], stack);
nums[index] = -1;
stack.pop();
return size;
}
}
边栏推荐
猜你喜欢

Log4net 思维导图

科普 | “大姨太”ETH 和 “小姨太”ETC的爱恨情仇

Analysis of pseudo-classes and pseudo-elements

把 VS Code 当游戏机

那些破釜沉舟入局Web3.0的互联网精英都怎么样了?

Automatic translation software - batch batch automatic translation software recommendation
解决win11/win10在登陆界面(解锁界面)点击获取每日壁纸无效的问题 - get Daily Lockscreen and Wallpaper - Win11/10的登录界面背景图片在哪里?

基金投顾业务

tidyverse笔记——dplyr包

【微服务】Nacos集群搭建以及加载文件配置
随机推荐
Financial leasing business
简单谈谈Feign
Install the gstreamer development dependency library to the project sysroot directory
2022.07.12_每日一题
Core Tower Electronics won the championship in the Wuhu Division of the 11th China Innovation and Entrepreneurship Competition
【解决】mysql本地计算机上的MySQL服务启动后停止。某些服务在未由其他服务或程序使用时将自动停止
leetcode 406. Queue Reconstruction by Height
线程中断方法
小实战项目之——吃货联盟订餐系统
批量免费文字翻译
中断及pendSV
【微服务】(十六)—— 分布式事务Seata
HighTec 的安装与配置
Difficulty comparison between high concurrency and multithreading (easy to confuse)
【微服务】Nacos集群搭建以及加载文件配置
文件 - 04 下载文件: 根据文件下载链接下载文件
【并发编程】ReentrantLock的lock()方法源码分析
nohup principle
gstreamer's caps event and new_segment event
Analysis of the implementation principle and detailed knowledge of v-model syntactic sugar and how to make the components you develop support v-model