Skip to content

Commit 95a06bc

Browse files
committed
update 565, cheathseet, progress
1 parent ff4730d commit 95a06bc

File tree

6 files changed

+211
-28
lines changed

6 files changed

+211
-28
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@
218218
498 | [Diagonal Traverse](https://leetcode.com/problems/diagonal-traverse/) | [Python](./leetcode_python/Array/diagonal_traverse.py) | _O(n)_ | _O(1)_ | Medium |`matrix`,`google`,`fb`| AGAIN*** (3) (not start)
219219
531 | [Lonely Pixel I](https://leetcode.com/problems/lonely-pixel-i/) | [Python](./leetcode_python/Array/lonely-pixel-i.py) | _O(m * n)_ | _O(m + n)_ | Medium | 🔒, `matrix`, `basic`| AGAIN*
220220
533 | [Lonely Pixel II](https://leetcode.com/problems/lonely-pixel-ii/) | [Python](./leetcode_python/Array/lonely-pixel-ii.py) | _O(m * n)_ | _O(m * n)_ | Medium |🔒| AGAIN (not start*)
221-
565 | [Array Nesting](https://leetcode.com/problems/array-nesting/) | [Python](./leetcode_python/Array/array-nesting.py) | _O(n)_ | _O(1)_ | Medium |`apple`| AGAIN (not start)
221+
565 | [Array Nesting](https://leetcode.com/problems/array-nesting/) | [Python](./leetcode_python/Array/array-nesting.py) | _O(n)_ | _O(1)_ | Medium |`union find`,`dfs`,`apple`| AGAIN**** (1)
222222
566 | [Reshape the Matrix](https://leetcode.com/problems/reshape-the-matrix/) | [Python](./leetcode_python/Array/reshape-the-matrix.py) | _O(m * n)_ | _O(m * n)_ | Easy |`basic`, `matrix`| AGAIN**
223223
581 | [Shortest Unsorted Continuous Subarray](https://leetcode.com/problems/shortest-unsorted-continuous-subarray/) | [Python](./leetcode_python/Array/shortest-unsorted-continuous-subarray.py) | _O(n)_ | _O(1)_ | Easy |`basic`| AGAIN*
224224
605 | [Can Place Flowers](https://leetcode.com/problems/can-place-flowers/) | [Python](./leetcode_python/Array/can-place-flowers.py) | _O(n)_ | _O(1)_ | Easy |`pass vs continue`| OK*

data/progress.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
20220210: 565
12
20220208: 670,406,1010,525,523
23
20220207: 026,080,257,11,128
34
20220206: 69,207,210

data/to_review.txt

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
2022-04-06 -> ['565']
12
2022-04-04 -> ['670,406,1010,525,523']
23
2022-04-03 -> ['026,080,257,11,128']
34
2022-04-02 -> ['69,207,210']
@@ -11,7 +12,7 @@
1112
2022-03-19 -> ['640']
1213
2022-03-18 -> ['553,606,662,295']
1314
2022-03-17 -> ['536,532']
14-
2022-03-16 -> ['532']
15+
2022-03-16 -> ['565', '532']
1516
2022-03-15 -> ['100,572,508']
1617
2022-03-14 -> ['670,406,1010,525,523']
1718
2022-03-13 -> ['026,080,257,11,128', '189,617']
@@ -24,27 +25,27 @@
2425
2022-03-06 -> ['190', '046,969,128']
2526
2022-03-05 -> ['235,236,1268', '658,005']
2627
2022-03-04 -> ['875,646,435,055']
27-
2022-03-03 -> ['1167', '508,663,725,278,035,875']
28+
2022-03-03 -> ['565', '1167', '508,663,725,278,035,875']
2829
2022-03-02 -> ['529,508']
2930
2022-03-01 -> ['670,406,1010,525,523', '406', '073,199']
3031
2022-02-28 -> ['026,080,257,11,128', '005,026,080,011']
3132
2022-02-27 -> ['69,207,210', '791']
3233
2022-02-26 -> ['640', '937,1041,994,523,525,204']
3334
2022-02-25 -> ['111,165,57,56,355', '553,606,662,295', '090,852,1131']
3435
2022-02-24 -> ['536,532']
35-
2022-02-23 -> ['532']
36+
2022-02-23 -> ['565', '532']
3637
2022-02-22 -> ['100,572,508']
3738
2022-02-21 -> ['670,406,1010,525,523', '190']
3839
2022-02-20 -> ['026,080,257,11,128', '235,236,1268', '189,617']
3940
2022-02-19 -> ['69,207,210', '560,264,703']
40-
2022-02-18 -> ['1167', '1010,134,089']
41+
2022-02-18 -> ['565', '1167', '1010,134,089']
4142
2022-02-17 -> ['111,165,57,56,355', '647']
4243
2022-02-16 -> ['670,406,1010,525,523', '406', '323,008,538']
43-
2022-02-15 -> ['026,080,257,11,128', '005,026,080,011', '716,136,163,725,621']
44+
2022-02-15 -> ['565', '026,080,257,11,128', '005,026,080,011', '716,136,163,725,621']
4445
2022-02-14 -> ['69,207,210', '791', '57,56,227,224']
45-
2022-02-13 -> ['670,406,1010,525,523', '190', '640', '046,969,128']
46-
2022-02-12 -> ['026,080,257,11,128', '111,165,57,56,355', '235,236,1268', '553,606,662,295', '658,005']
47-
2022-02-11 -> ['670,406,1010,525,523', '69,207,210', '536,532', '875,646,435,055']
46+
2022-02-13 -> ['565', '670,406,1010,525,523', '190', '640', '046,969,128']
47+
2022-02-12 -> ['565', '026,080,257,11,128', '111,165,57,56,355', '235,236,1268', '553,606,662,295', '658,005']
48+
2022-02-11 -> ['565', '670,406,1010,525,523', '69,207,210', '536,532', '875,646,435,055']
4849
2022-02-10 -> ['670,406,1010,525,523', '026,080,257,11,128', '1167', '532', '508,663,725,278,035,875']
4950
2022-02-09 -> ['670,406,1010,525,523', '026,080,257,11,128', '69,207,210', '111,165,57,56,355', '100,572,508', '529,508']
5051
2022-02-08 -> ['026,080,257,11,128', '69,207,210', '190', '406', '073,199']

doc/cheatsheet/union_find.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,4 +241,26 @@ class Solution(object):
241241
### 2-5) Friend Circles
242242
```python
243243
# LC 547 Friend Circles
244+
```
245+
246+
### 2-6) Array Nesting
247+
```python
248+
# LC 565. Array Nesting
249+
# V0
250+
# IDEA : UNION FIND
251+
class Solution(object):
252+
def arrayNesting(self, nums):
253+
def search(idx):
254+
cnt = 0
255+
while nums[idx] >= 0:
256+
cnt += 1
257+
next = nums[idx]
258+
nums[idx] = -1
259+
idx = next
260+
return cnt
261+
ans = 0
262+
for x in range(len(nums)):
263+
if nums[x] >= 0:
264+
ans = max(ans, search(x))
265+
return ans
244266
```

doc/progress.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# Progress
22

3-
# 2022-02-09
3+
# 2022-02-10
44
- DOING:
55
- LC (appl_)
66
- ref : https://github.com/yennanliu/CS_basics/blob/master/doc/leetcode_company_V1/Apple%20-%20LeetCode.pdf
77
- (pdf : p.2)
88
- 269
9-
- 565
9+
- 565 (ok, but again!!)
1010
- ================
1111
- LC (LC discuss sharing)
1212
- https://leetcode.com/discuss/interview-question/1367526/apple-uber-flipkart-swiggy-interview-questions-july-2020-july-2021

leetcode_python/Array/array-nesting.py

Lines changed: 176 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,28 +37,25 @@
3737
"""
3838

3939
# V0
40-
41-
# V1
42-
# https://blog.csdn.net/fuxuemingzhu/article/details/79460546
40+
# IDEA : UNION FIND
4341
class Solution(object):
4442
def arrayNesting(self, nums):
45-
"""
46-
:type nums: List[int]
47-
:rtype: int
48-
"""
49-
visited = [False] * len(nums)
43+
def search(idx):
44+
cnt = 0
45+
while nums[idx] >= 0:
46+
cnt += 1
47+
next = nums[idx]
48+
nums[idx] = -1
49+
idx = next
50+
return cnt
5051
ans = 0
51-
for i in range(len(nums)):
52-
road = 0
53-
while not visited[i]:
54-
road += 1
55-
# order of below 2 lines of code is unchangeable
56-
visited[i] = True
57-
i = nums[i]
58-
ans = max(ans, road)
52+
for x in range(len(nums)):
53+
if nums[x] >= 0:
54+
ans = max(ans, search(x))
5955
return ans
6056

61-
# V1'
57+
# V1
58+
# IDEA : UNION FIND
6259
# http://bookshadow.com/weblog/2017/05/28/leetcode-array-nesting/
6360
class Solution(object):
6461
def arrayNesting(self, nums):
@@ -80,6 +77,168 @@ def search(idx):
8077
ans = max(ans, search(x))
8178
return ans
8279

80+
# V1'
81+
# IDEA : UNION FIND
82+
# https://blog.csdn.net/fuxuemingzhu/article/details/79460546
83+
class Solution(object):
84+
def arrayNesting(self, nums):
85+
"""
86+
:type nums: List[int]
87+
:rtype: int
88+
"""
89+
visited = [False] * len(nums)
90+
ans = 0
91+
for i in range(len(nums)):
92+
road = 0
93+
while not visited[i]:
94+
road += 1
95+
# order of below 2 lines of code is unchangeable
96+
visited[i] = True
97+
i = nums[i]
98+
ans = max(ans, road)
99+
return ans
100+
101+
# V1''
102+
# IDEA : UNION FIND
103+
# https://leetcode.com/problems/array-nesting/discuss/209108/Python-solution
104+
class Solution(object):
105+
def arrayNesting(self, nums):
106+
seen = [0]*len(nums)
107+
res = 0
108+
for i in range(len(nums)):
109+
if seen[i] != -1:
110+
j = i
111+
count = 0
112+
while seen[j] != -1:
113+
count += 1
114+
seen[j] = -1
115+
j = nums[j]
116+
res = max(res, count)
117+
return res
118+
119+
# V1'''
120+
# IDEA : optimzied (we don't need extra space for seen, just make nums[idx] as -1)
121+
# https://leetcode.com/problems/array-nesting/discuss/209108/Python-solution
122+
class Solution(object):
123+
def arrayNesting(self, nums):
124+
res = 0
125+
for i in range(len(nums)):
126+
if nums[i] != -1:
127+
j = i
128+
count = 0
129+
while nums[j] != -1:
130+
count += 1
131+
tmp = nums[j]
132+
nums[j] = -1
133+
j = tmp
134+
res = max(res, count)
135+
return res
136+
137+
# V1''''
138+
# IDEA : UNION FIND
139+
# https://leetcode.com/problems/array-nesting/discuss/1439139/AA-Python-solution
140+
class Solution:
141+
def arrayNesting(self, nums):
142+
res = 0
143+
seen = set()
144+
for i in range(len(nums)):
145+
if nums[i] not in seen:
146+
tmp = self.build(nums, i)
147+
seen |= tmp
148+
res = max(res, len(tmp))
149+
return res
150+
151+
def build(self, nums, k):
152+
res = set()
153+
current = nums[k]
154+
while current not in res:
155+
res.add(current)
156+
current = nums[current]
157+
return res
158+
159+
# V1'''''
160+
# https://leetcode.com/problems/array-nesting/discuss/314782/python
161+
class Solution:
162+
def arrayNesting(self, nums):
163+
res = 0
164+
visited = [0] * len(nums)
165+
for i in range(len(nums)):
166+
cur, cur_res = i, 0
167+
while not visited[cur]:
168+
visited[cur] = 1
169+
cur_res += 1
170+
cur = nums[cur]
171+
res = max(res, cur_res)
172+
return res
173+
174+
# V1''''''
175+
# IDEA : BRUTE FORCE (time out error)
176+
# https://leetcode.com/problems/array-nesting/solution/
177+
# JAVA
178+
# public class Solution {
179+
# public int arrayNesting(int[] nums) {
180+
# int res = 0;
181+
# for (int i = 0; i < nums.length; i++) {
182+
# int start = nums[i], count = 0;
183+
# do {
184+
# start = nums[start];
185+
# count++;
186+
# }
187+
# while (start != nums[i]);
188+
# res = Math.max(res, count);
189+
#
190+
# }
191+
# return res;
192+
# }
193+
# }
194+
195+
# V1''''''
196+
# IDEA : VISITED ARRAY
197+
# JAVA
198+
# https://leetcode.com/problems/array-nesting/solution/
199+
# public class Solution {
200+
# public int arrayNesting(int[] nums) {
201+
# boolean[] visited = new boolean[nums.length];
202+
# int res = 0;
203+
# for (int i = 0; i < nums.length; i++) {
204+
# if (!visited[i]) {
205+
# int start = nums[i], count = 0;
206+
# do {
207+
# start = nums[start];
208+
# count++;
209+
# visited[start] = true;
210+
# }
211+
# while (start != nums[i]);
212+
# res = Math.max(res, count);
213+
# }
214+
# }
215+
# return res;
216+
# }
217+
# }
218+
219+
# V1''''''''
220+
# IDEA : VISITED ARRAY WITHOUT EXTRA SPACE
221+
# https://leetcode.com/problems/array-nesting/solution/
222+
# JAVA
223+
# public class Solution {
224+
# public int arrayNesting(int[] nums) {
225+
# int res = 0;
226+
# for (int i = 0; i < nums.length; i++) {
227+
# if (nums[i] != Integer.MAX_VALUE) {
228+
# int start = nums[i], count = 0;
229+
# while (nums[start] != Integer.MAX_VALUE) {
230+
# int temp = start;
231+
# start = nums[start];
232+
# count++;
233+
# nums[temp] = Integer.MAX_VALUE;
234+
# }
235+
# res = Math.max(res, count);
236+
# }
237+
# }
238+
# return res;
239+
# }
240+
# }
241+
83242
# V2
84243
# Time: O(n)
85244
# Space: O(1)

0 commit comments

Comments
 (0)