当前位置:网站首页>565. Array nesting: regular simulation questions
565. Array nesting: regular simulation questions
2022-07-19 11:27:00 【Gong Shui Sanye's Diary of writing questions】
Title Description
This is a LeetCode Upper 565. Array nesting , The difficulty is secondary .
Tag : 「 simulation 」
Index from Start length is N Array of A, contain To All integers of . Find the largest set S And return its size , among And abide by the following rules .
Suppose the selection index is i The elements of by S The first element of ,S The next element of should be , And then ... And so on , Keep adding until S Duplicate elements appear .
Example 1:
Input : A = [5,4,0,3,1,6,2]
Output : 4
explain :
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}
Tips :
Nyes Integer between .AThere are no duplicate elements in the .AThe size of the element in Between .
simulation
take And There is a directed edge between , Because all numbers range in , And not repeated , So there is at least one ring , The essence of the problem is to find the maximum length of all rings .
Directly simulate according to the meaning of the title , Deal with each... From front to back , And try to from Start and traverse the ring in which it is located , In order to prevent some rings from being processed repeatedly , For the currently passed Marked as , In this way, each number can be accessed no more than Time , The overall complexity is .
Java Code :
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 Code :
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
};
Time complexity : Each number is accessed a constant number of times , The complexity is Spatial complexity :
Last
This is us. 「 Brush through LeetCode」 The first of the series No.565 piece , The series begins with 2021/01/01, As of the start date LeetCode I have in common 1916 questions , Part of it is a locked question , We will finish all the questions without lock first .
In this series , In addition to explaining the idea of solving problems , And give the most concise code possible . If the general solution is involved, there will be corresponding code templates .
In order to facilitate the students to debug and submit code on the computer , I've built a warehouse :https://github.com/SharingSource/LogicStack-LeetCode .
In the warehouse address , You can see the links to the series 、 The corresponding code of the series 、LeetCode Links to the original problem and other preferred solutions .
More, more, more popular 「 written examination / interview 」 Relevant information can be found in the beautifully arranged Gather new bases
边栏推荐
- The type of MySQL index (single column index, combined index, BTREE index, clustered index, etc.)
- 翻墙后看什么?最热门的国外网站——翻墙网址导航
- Similarities and differences between OA system and MES system
- Resources for Physics based simulation in Computer Graphics 图形学中物理模拟的资源整理
- Set the interface language of CMD command prompt window to English
- LeetCode 745. Prefix and suffix search
- SAP S4 material management inventory module mard database table reading technical details
- Introduction to virtualization troubleshooting
- 要想组建敏捷团队,这些方法不可少
- Synchronized lock upgrade
猜你喜欢

Mysql 自增id、uuid与雪花id

Powercli script performance optimization

学习笔记3--规划控制中的机器学习基本思想

委派雙親之類加載器

SPI服务发现机制

JVM钩子hooks函数

E-commerce sales data analysis and prediction (date data statistics, daily statistics, monthly statistics)

Use and principle of ThreadLocal variable

LeetCode 745. Prefix and suffix search

Mysql优化系列之limit查询
随机推荐
Model comparison of material inventory management between sap ECC and s4hana material
Resources for physics based simulation in computer graphics
Daily question brushing record (26)
Cmake common commands (V)
E-commerce sales data analysis and prediction (date data statistics, daily statistics, monthly statistics)
公网连接MySQL实例的解决方案
Set the interface language of CMD command prompt window to English
Mpu9250 ky9250 attitude, angle module and mpu9250 MPL DMA comparison
PowerCLI 脚本性能优化
Mysql 自增id、uuid与雪花id
After summarizing the surface based knowledge of the database
Huawei machine test: Message decompression
CodeForces - 587E(线性基+线段树+差分)
Detailed explanation of Euler angle, axis angle, quaternion and rotation matrix
Leetcode 1328. 破坏回文串(可以,已解决)
开发那些事儿:如何解决RK芯片视频处理编解码耗时很长的问题?
8.固定收益投资
A curated list of awesome Qt and QML
From prediction to decision-making, Chapter 9 Yunji datacanvas launched the ylearn causal learning open source project
QT two overloaded qlistwidget control objects implement selectitem drag drag