forked from Srinivas11789/AlgorithmNuggets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharr.py
More file actions
40 lines (36 loc) · 1.06 KB
/
arr.py
File metadata and controls
40 lines (36 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class Solution(object):
def arrayNesting(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
# Optimized Logic -
# * Remove the List operation/lookup of temp
# * Arrive at j = nums[j] in a different fashion by sub and add
maxi = 0
n = len(nums)
for i in range(n):
temp = 0
j = i
while nums[j] >= 0:
temp += 1
nums[j] -= n
j = nums[j] + n
if temp > maxi:
maxi = temp
return maxi
"""
# Direct List Logic
# Correct Logic but Time Limit Exceeded
# All pass except performance cases
maxi = 0
n = len(nums)
for i in range(n):
temp = []
temp.append(nums[i])
while nums[temp[-1]] not in temp and nums[temp[-1]] in nums:
temp.append(nums[temp[-1]])
if len(temp) > maxi:
maxi = len(temp)
return maxi
"""