-
Notifications
You must be signed in to change notification settings - Fork 43
/
array-nesting.py
261 lines (236 loc) · 6.99 KB
/
array-nesting.py
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
"""
565. Array Nesting
Medium
You are given an integer array nums of length n where nums is a permutation of the numbers in the range [0, n - 1].
You should build a set s[k] = {nums[k], nums[nums[k]], nums[nums[nums[k]]], ... } subjected to the following rule:
The first element in s[k] starts with the selection of the element nums[k] of index = k.
The next element in s[k] should be nums[nums[k]], and then nums[nums[nums[k]]], and so on.
We stop adding right before a duplicate element occurs in s[k].
Return the longest length of a set s[k].
Example 1:
Input: nums = [5,4,0,3,1,6,2]
Output: 4
Explanation:
nums[0] = 5, nums[1] = 4, nums[2] = 0, nums[3] = 3, nums[4] = 1, nums[5] = 6, nums[6] = 2.
One of the longest sets s[k]:
s[0] = {nums[0], nums[5], nums[6], nums[2]} = {5, 6, 2, 0}
Example 2:
Input: nums = [0,1,2]
Output: 1
Constraints:
1 <= nums.length <= 105
0 <= nums[i] < nums.length
All the values of nums are unique.
"""
# V0
# IDEA : UNION FIND
class Solution(object):
def arrayNesting(self, nums):
def search(idx):
cnt = 0
while nums[idx] >= 0:
cnt += 1
next = nums[idx]
nums[idx] = -1
idx = next
return cnt
ans = 0
for x in range(len(nums)):
if nums[x] >= 0:
ans = max(ans, search(x))
return ans
# V1
# IDEA : UNION FIND
# http://bookshadow.com/weblog/2017/05/28/leetcode-array-nesting/
class Solution(object):
def arrayNesting(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
def search(idx):
cnt = 0
while nums[idx] >= 0:
cnt += 1
next = nums[idx]
nums[idx] = -1
idx = next
return cnt
ans = 0
for x in range(len(nums)):
if nums[x] >= 0:
ans = max(ans, search(x))
return ans
# V1'
# IDEA : UNION FIND
# https://blog.csdn.net/fuxuemingzhu/article/details/79460546
class Solution(object):
def arrayNesting(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
visited = [False] * len(nums)
ans = 0
for i in range(len(nums)):
road = 0
while not visited[i]:
road += 1
# order of below 2 lines of code is unchangeable
visited[i] = True
i = nums[i]
ans = max(ans, road)
return ans
# V1''
# IDEA : UNION FIND
# https://leetcode.com/problems/array-nesting/discuss/209108/Python-solution
class Solution(object):
def arrayNesting(self, nums):
seen = [0]*len(nums)
res = 0
for i in range(len(nums)):
if seen[i] != -1:
j = i
count = 0
while seen[j] != -1:
count += 1
seen[j] = -1
j = nums[j]
res = max(res, count)
return res
# V1'''
# IDEA : optimzied (we don't need extra space for seen, just make nums[idx] as -1)
# https://leetcode.com/problems/array-nesting/discuss/209108/Python-solution
class Solution(object):
def arrayNesting(self, nums):
res = 0
for i in range(len(nums)):
if nums[i] != -1:
j = i
count = 0
while nums[j] != -1:
count += 1
tmp = nums[j]
nums[j] = -1
j = tmp
res = max(res, count)
return res
# V1''''
# IDEA : UNION FIND
# https://leetcode.com/problems/array-nesting/discuss/1439139/AA-Python-solution
class Solution:
def arrayNesting(self, nums):
res = 0
seen = set()
for i in range(len(nums)):
if nums[i] not in seen:
tmp = self.build(nums, i)
seen |= tmp
res = max(res, len(tmp))
return res
def build(self, nums, k):
res = set()
current = nums[k]
while current not in res:
res.add(current)
current = nums[current]
return res
# V1'''''
# https://leetcode.com/problems/array-nesting/discuss/314782/python
class Solution:
def arrayNesting(self, nums):
res = 0
visited = [0] * len(nums)
for i in range(len(nums)):
cur, cur_res = i, 0
while not visited[cur]:
visited[cur] = 1
cur_res += 1
cur = nums[cur]
res = max(res, cur_res)
return res
# V1''''''
# IDEA : BRUTE FORCE (time out error)
# https://leetcode.com/problems/array-nesting/solution/
# JAVA
# public class Solution {
# public int arrayNesting(int[] nums) {
# int res = 0;
# for (int i = 0; i < nums.length; i++) {
# int start = nums[i], count = 0;
# do {
# start = nums[start];
# count++;
# }
# while (start != nums[i]);
# res = Math.max(res, count);
#
# }
# return res;
# }
# }
# V1''''''
# IDEA : VISITED ARRAY
# JAVA
# https://leetcode.com/problems/array-nesting/solution/
# public class Solution {
# public int arrayNesting(int[] nums) {
# boolean[] visited = new boolean[nums.length];
# int res = 0;
# for (int i = 0; i < nums.length; i++) {
# if (!visited[i]) {
# int start = nums[i], count = 0;
# do {
# start = nums[start];
# count++;
# visited[start] = true;
# }
# while (start != nums[i]);
# res = Math.max(res, count);
# }
# }
# return res;
# }
# }
# V1''''''''
# IDEA : VISITED ARRAY WITHOUT EXTRA SPACE
# https://leetcode.com/problems/array-nesting/solution/
# JAVA
# public class Solution {
# public int arrayNesting(int[] nums) {
# int res = 0;
# for (int i = 0; i < nums.length; i++) {
# if (nums[i] != Integer.MAX_VALUE) {
# int start = nums[i], count = 0;
# while (nums[start] != Integer.MAX_VALUE) {
# int temp = start;
# start = nums[start];
# count++;
# nums[temp] = Integer.MAX_VALUE;
# }
# res = Math.max(res, count);
# }
# }
# return res;
# }
# }
# V2
# Time: O(n)
# Space: O(1)
class Solution(object):
def arrayNesting(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
result = 0
for num in nums:
if num is not None:
start, count = num, 0
while nums[start] is not None:
temp = start
start = nums[start]
nums[temp] = None
count += 1
result = max(result, count)
return result