当前位置:网站首页>565. 数组嵌套 : 常规模拟题
565. 数组嵌套 : 常规模拟题
2022-07-17 14:32:00 【宫水三叶的刷题日记】
题目描述
这是 LeetCode 上的 565. 数组嵌套 ,难度为 中等。
Tag : 「模拟」
索引从 开始长度为 N 的数组 A,包含 到 的所有整数。找到最大的集合 S 并返回其大小,其中 且遵守以下的规则。
假设选择索引为 i 的元素 为 S 的第一个元素,S 的下一个元素应该是 ,之后是 ... 以此类推,不断添加直到 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是 之间的整数。A中不含有重复的元素。A中的元素大小在 之间。
模拟
将 与 之间看作存在一条有向边,由于所有数范围都在 ,且不重复,因此至少存在一个环,而问题本质是求所有环的最大长度。
直接根据题意记进行模拟即可,从前往后处理每个 ,并尝试从 出发遍历其所在的环,为了防止某些环被重复处理,对于当前经过的 标记为 ,这样每个数被访问的次数最多不超过 次,整体复杂度为 。
Java 代码:
class Solution {
public int arrayNesting(int[] nums) {
int n = nums.length, ans = 0;
for (int i = 0; i < n; i++) {
int cur = i, cnt = 0;
while (nums[cur] != -1) {
cnt++;
int c = cur;
cur = nums[cur];
nums[c] = -1;
}
ans = Math.max(ans, cnt);
}
return ans;
}
}
TypeScript 代码:
function arrayNesting(nums: number[]): number {
let n = nums.length, ans = 0
for (let i = 0; i < n; i++) {
let cur = i, cnt = 0
while (nums[cur] != -1) {
cnt++
const c = cur
cur = nums[cur]
nums[c] = -1
}
ans = Math.max(ans, cnt)
}
return ans
};
时间复杂度:每个数字被访问的次数为常数次,复杂度为 空间复杂度:
最后
这是我们「刷穿 LeetCode」系列文章的第 No.565 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
更多更全更热门的「笔试/面试」相关资料可访问排版精美的 合集新基地
边栏推荐
- Sword finger offer II 041 Average value of sliding window
- input number 純數字輸入 限制長度 限制 最大值
- SPI服务发现机制
- JVM钩子hooks函数
- QT two overloaded qlistwidget control objects implement selectitem drag drag
- Learning outline of the column "MySQL DBA's magic road"
- Unity Dropdown(可编辑,可输入)下拉选择框,带文本联想
- Unity dropdown (editable, inputable) drop-down selection box with Text Association
- Powercli script performance optimization
- How does unity3d use the asset store to download some useful resource packages
猜你喜欢

性能优化之@Contended减少伪共享

Un modèle de détection par défaut basé sur le réseau neuronal évolutif rapide dans le contrôle de la qualité des produits - lire les notes

A fastandrobust convolutionalneuralnetwork-based defect detection model inproductqualitycontrol-閱讀筆記

PowerCLI 脚本性能优化

NPC, Microsoft, etc. proposed inclusivefl: inclusive federal learning on heterogeneous devices

Win10 install Apache Jena 3.17

开发那些事儿:如何解决RK芯片视频处理编解码耗时很长的问题?

Introduction to common distributed locks

Avi Deployment Guide (2): overview of AVI architecture

Antd drop-down multiple options to transfer values to the background for query operations
随机推荐
Delegate parents and other loaders
Hello JSON Schema
ThreadLocal变量使用及原理
Introduction of database lock, shared with InnoDB, exclusive lock
IP SAN has an independent file system. After the application server accesses the IP SAN through the network sharing protocol, it can read and write the files in the file system
火箭大机动运动欧拉角解算的探讨
Environment variable configuration of win10
OA系统与MES系统的异同点
动态内存分配问题
Unity high version returned low version error
Resources for Physics based simulation in Computer Graphics 图形学中物理模拟的资源整理
NPC, Microsoft, etc. proposed inclusivefl: inclusive federal learning on heterogeneous devices
常用getshell工具的下载
Category imbalance in classification tasks
Tier defect detection using full revolutionary network
CodeForces - 587E(线性基+线段树+差分)
Configuration of vscode+unity3d
Detailed explanation of multiple linear regression
pjudge#21652-[PR #4]到底有没有九【数位dp】
XSS. haozi. Me brush questions