g0501_0600.s0565_array_nesting.Solution.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-kotlin Show documentation
Show all versions of leetcode-in-kotlin Show documentation
Kotlin-based LeetCode algorithm problem solutions, regularly updated
package g0501_0600.s0565_array_nesting
// #Medium #Array #Depth_First_Search #2023_01_21_Time_553_ms_(100.00%)_Space_52.8_MB_(100.00%)
class Solution {
fun arrayNesting(nums: IntArray): Int {
var index: Int
var value: Int
var maxLen = 0
var len: Int
for (i in nums.indices) {
if (nums[i] != -1) {
index = i
len = 0
while (nums[index] != -1) {
value = nums[index]
nums[index] = -1
index = value
len++
}
maxLen = Math.max(len, maxLen)
}
}
return maxLen
}
}