Skip to content

Commit

Permalink
update 565, cheathseet, progress
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Feb 9, 2022
1 parent ff4730d commit 95a06bc
Show file tree
Hide file tree
Showing 6 changed files with 211 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@
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)
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*
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*)
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)
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)
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**
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*
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*
Expand Down
1 change: 1 addition & 0 deletions data/progress.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
20220210: 565
20220208: 670,406,1010,525,523
20220207: 026,080,257,11,128
20220206: 69,207,210
Expand Down
17 changes: 9 additions & 8 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
2022-04-06 -> ['565']
2022-04-04 -> ['670,406,1010,525,523']
2022-04-03 -> ['026,080,257,11,128']
2022-04-02 -> ['69,207,210']
Expand All @@ -11,7 +12,7 @@
2022-03-19 -> ['640']
2022-03-18 -> ['553,606,662,295']
2022-03-17 -> ['536,532']
2022-03-16 -> ['532']
2022-03-16 -> ['565', '532']
2022-03-15 -> ['100,572,508']
2022-03-14 -> ['670,406,1010,525,523']
2022-03-13 -> ['026,080,257,11,128', '189,617']
Expand All @@ -24,27 +25,27 @@
2022-03-06 -> ['190', '046,969,128']
2022-03-05 -> ['235,236,1268', '658,005']
2022-03-04 -> ['875,646,435,055']
2022-03-03 -> ['1167', '508,663,725,278,035,875']
2022-03-03 -> ['565', '1167', '508,663,725,278,035,875']
2022-03-02 -> ['529,508']
2022-03-01 -> ['670,406,1010,525,523', '406', '073,199']
2022-02-28 -> ['026,080,257,11,128', '005,026,080,011']
2022-02-27 -> ['69,207,210', '791']
2022-02-26 -> ['640', '937,1041,994,523,525,204']
2022-02-25 -> ['111,165,57,56,355', '553,606,662,295', '090,852,1131']
2022-02-24 -> ['536,532']
2022-02-23 -> ['532']
2022-02-23 -> ['565', '532']
2022-02-22 -> ['100,572,508']
2022-02-21 -> ['670,406,1010,525,523', '190']
2022-02-20 -> ['026,080,257,11,128', '235,236,1268', '189,617']
2022-02-19 -> ['69,207,210', '560,264,703']
2022-02-18 -> ['1167', '1010,134,089']
2022-02-18 -> ['565', '1167', '1010,134,089']
2022-02-17 -> ['111,165,57,56,355', '647']
2022-02-16 -> ['670,406,1010,525,523', '406', '323,008,538']
2022-02-15 -> ['026,080,257,11,128', '005,026,080,011', '716,136,163,725,621']
2022-02-15 -> ['565', '026,080,257,11,128', '005,026,080,011', '716,136,163,725,621']
2022-02-14 -> ['69,207,210', '791', '57,56,227,224']
2022-02-13 -> ['670,406,1010,525,523', '190', '640', '046,969,128']
2022-02-12 -> ['026,080,257,11,128', '111,165,57,56,355', '235,236,1268', '553,606,662,295', '658,005']
2022-02-11 -> ['670,406,1010,525,523', '69,207,210', '536,532', '875,646,435,055']
2022-02-13 -> ['565', '670,406,1010,525,523', '190', '640', '046,969,128']
2022-02-12 -> ['565', '026,080,257,11,128', '111,165,57,56,355', '235,236,1268', '553,606,662,295', '658,005']
2022-02-11 -> ['565', '670,406,1010,525,523', '69,207,210', '536,532', '875,646,435,055']
2022-02-10 -> ['670,406,1010,525,523', '026,080,257,11,128', '1167', '532', '508,663,725,278,035,875']
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']
2022-02-08 -> ['026,080,257,11,128', '69,207,210', '190', '406', '073,199']
Expand Down
22 changes: 22 additions & 0 deletions doc/cheatsheet/union_find.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,26 @@ class Solution(object):
### 2-5) Friend Circles
```python
# LC 547 Friend Circles
```

### 2-6) Array Nesting
```python
# LC 565. Array Nesting
# 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
```
4 changes: 2 additions & 2 deletions doc/progress.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# Progress

# 2022-02-09
# 2022-02-10
- DOING:
- LC (appl_)
- ref : https://github.com/yennanliu/CS_basics/blob/master/doc/leetcode_company_V1/Apple%20-%20LeetCode.pdf
- (pdf : p.2)
- 269
- 565
- 565 (ok, but again!!)
- ================
- LC (LC discuss sharing)
- https://leetcode.com/discuss/interview-question/1367526/apple-uber-flipkart-swiggy-interview-questions-july-2020-july-2021
Expand Down
193 changes: 176 additions & 17 deletions leetcode_python/Array/array-nesting.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,28 +37,25 @@
"""

# V0

# V1
# https://blog.csdn.net/fuxuemingzhu/article/details/79460546
# IDEA : UNION FIND
class Solution(object):
def arrayNesting(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
visited = [False] * len(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 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)
for x in range(len(nums)):
if nums[x] >= 0:
ans = max(ans, search(x))
return ans

# V1'
# V1
# IDEA : UNION FIND
# http://bookshadow.com/weblog/2017/05/28/leetcode-array-nesting/
class Solution(object):
def arrayNesting(self, nums):
Expand All @@ -80,6 +77,168 @@ def search(idx):
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)
Expand Down

0 comments on commit 95a06bc

Please sign in to comment.