Skip to content

Commit 46c598e

Browse files
2022-05-19 - LeetHub
1 parent 0ca6d13 commit 46c598e

File tree

6 files changed

+182
-5
lines changed

6 files changed

+182
-5
lines changed

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## LeetCode
44

5-
**Problem Solved: 145** (<span style="color:green">Easy: 28</span>, <span style="color:orange">Medium: 101</span>, <span style="color:red">Hard: 16</span>)
5+
**Problem Solved: 147** (<span style="color:green">Easy: 28</span>, <span style="color:orange">Medium: 103</span>, <span style="color:red">Hard: 16</span>)
66

77
| # | Title | Solutions | Difficulty |
88
| - | - | - | - |
@@ -19,7 +19,7 @@
1919
| 23 | [Merge k Sorted Lists](https://leetcode.com/problems/merge-k-sorted-lists/) | <a href="leetcode/00023_merge-k-sorted-lists/merge-k-sorted-lists.py"><img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/python/python-original.svg" width="20" height="20"></a> | Hard |
2020
| 24 | [Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/) | <a href="leetcode/00024_swap-nodes-in-pairs/24-swap-nodes-in-pairs.py"><img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/python/python-original.svg" width="20" height="20"></a> | Medium |
2121
| 28 | [Implement strStr()](https://leetcode.com/problems/implement-strstr/) | <a href="leetcode/00028_implement-strstr/implement-strstr.py"><img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/python/python-original.svg" width="20" height="20"></a> | Easy |
22-
| 31 | [Next Permutation](https://leetcode.com/problems/next-permutation/) | <a href="leetcode/00031_next-permutation/next-permutation.py"><img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/python/python-original.svg" width="20" height="20"></a> | Medium |
22+
| 31 | [Next Permutation](https://leetcode.com/problems/next-permutation/) | <a href="leetcode/00031_next-permutation/31-next-permutation.py"><img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/python/python-original.svg" width="20" height="20"></a> | Medium |
2323
| 32 | [Longest Valid Parentheses](https://leetcode.com/problems/longest-valid-parentheses/) | <a href="leetcode/00032_longest-valid-parentheses/longest-valid-parentheses.py"><img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/python/python-original.svg" width="20" height="20"></a> | Hard |
2424
| 33 | [Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/) | <a href="leetcode/00033_search-in-rotated-sorted-array/search-in-rotated-sorted-array.java"><img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/java/java-original.svg" width="20" height="20"></a> | Medium |
2525
| 34 | [Find First and Last Position of Element in Sorted Array](https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/) | <a href="leetcode/00034_find-first-and-last-position-of-element-in-sorted-array/34-find-first-and-last-position-of-element-in-sorted-array.py"><img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/python/python-original.svg" width="20" height="20"></a> | Medium |
@@ -73,9 +73,9 @@
7373
| 236 | [Lowest Common Ancestor of a Binary Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/) | <a href="leetcode/00236_lowest-common-ancestor-of-a-binary-tree/236-lowest-common-ancestor-of-a-binary-tree.py"><img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/python/python-original.svg" width="20" height="20"></a> | Medium |
7474
| 238 | [Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/) | <a href="leetcode/00238_product-of-array-except-self/product-of-array-except-self.py"><img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/python/python-original.svg" width="20" height="20"></a> | Medium |
7575
| 247 | [Strobogrammatic Number II](https://leetcode.com/problems/strobogrammatic-number-ii/) | <a href="leetcode/00247_strobogrammatic-number-ii/247-strobogrammatic-number-ii.py"><img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/python/python-original.svg" width="20" height="20"></a> | Medium |
76-
| 249 | [Group Shifted Strings](https://leetcode.com/problems/group-shifted-strings/) | | Medium |
76+
| 249 | [Group Shifted Strings](https://leetcode.com/problems/group-shifted-strings/) | <a href="leetcode/00249_group-shifted-strings/249-group-shifted-strings.py"><img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/python/python-original.svg" width="20" height="20"></a> | Medium |
7777
| 252 | [Meeting Rooms](https://leetcode.com/problems/meeting-rooms/) | <a href="leetcode/00252_meeting-rooms/meeting-rooms.py"><img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/python/python-original.svg" width="20" height="20"></a> | Easy |
78-
| 253 | [Meeting Rooms II](https://leetcode.com/problems/meeting-rooms-ii/) | <a href="leetcode/00253_meeting-rooms-ii/253-meeting-rooms-ii.py"><img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/python/python-original.svg" width="20" height="20"></a><a href="leetcode/00253_meeting-rooms-ii/253-meeting-rooms-ii.java"><img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/java/java-original.svg" width="20" height="20"></a> | Medium |
78+
| 253 | [Meeting Rooms II](https://leetcode.com/problems/meeting-rooms-ii/) | <a href="leetcode/00253_meeting-rooms-ii/253-meeting-rooms-ii.java"><img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/java/java-original.svg" width="20" height="20"></a><a href="leetcode/00253_meeting-rooms-ii/253-meeting-rooms-ii.py"><img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/python/python-original.svg" width="20" height="20"></a> | Medium |
7979
| 259 | [3Sum Smaller](https://leetcode.com/problems/3sum-smaller/) | <a href="leetcode/00259_3sum-smaller/259-3sum-smaller.py"><img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/python/python-original.svg" width="20" height="20"></a> | Medium |
8080
| 263 | [Ugly Number](https://leetcode.com/problems/ugly-number/) | <a href="leetcode/00263_ugly-number/ugly-number.py"><img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/python/python-original.svg" width="20" height="20"></a><a href="leetcode/00263_ugly-number/ugly-number.cpp"><img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/cplusplus/cplusplus-original.svg" width="20" height="20"></a> | Easy |
8181
| 286 | [Walls and Gates](https://leetcode.com/problems/walls-and-gates/) | <a href="leetcode/00286_walls-and-gates/286-walls-and-gates.cpp"><img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/cplusplus/cplusplus-original.svg" width="20" height="20"></a><a href="leetcode/00286_walls-and-gates/286-walls-and-gates.py"><img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/python/python-original.svg" width="20" height="20"></a> | Medium |
@@ -107,8 +107,9 @@
107107
| 657 | [Robot Return to Origin](https://leetcode.com/problems/robot-return-to-origin/) | <a href="leetcode/00657_robot-return-to-origin/robot-return-to-origin.cpp"><img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/cplusplus/cplusplus-original.svg" width="20" height="20"></a> | Easy |
108108
| 660 | [Remove 9](https://leetcode.com/problems/remove-9/) | <a href="leetcode/00660_remove-9/660-remove-9.py"><img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/python/python-original.svg" width="20" height="20"></a> | Hard |
109109
| 692 | [Top K Frequent Words](https://leetcode.com/problems/top-k-frequent-words/) | <a href="leetcode/00692_top-k-frequent-words/692-top-k-frequent-words.py"><img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/python/python-original.svg" width="20" height="20"></a> | Medium |
110+
| 694 | [Number of Distinct Islands](https://leetcode.com/problems/number-of-distinct-islands/) | <a href="leetcode/00694_number-of-distinct-islands/694-number-of-distinct-islands.py"><img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/python/python-original.svg" width="20" height="20"></a> | Medium |
110111
| 695 | [Max Area of Island](https://leetcode.com/problems/max-area-of-island/) | <a href="leetcode/00695_max-area-of-island/695-max-area-of-island.py"><img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/python/python-original.svg" width="20" height="20"></a> | Medium |
111-
| 701 | [Insert into a Binary Search Tree](https://leetcode.com/problems/insert-into-a-binary-search-tree/) | <a href="leetcode/00701_insert-into-a-binary-search-tree/701-insert-into-a-binary-search-tree.java"><img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/java/java-original.svg" width="20" height="20"></a><a href="leetcode/00701_insert-into-a-binary-search-tree/701-insert-into-a-binary-search-tree.py"><img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/python/python-original.svg" width="20" height="20"></a><a href="leetcode/00701_insert-into-a-binary-search-tree/701-insert-into-a-binary-search-tree.go"><img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/go/go-original.svg" width="20" height="20"></a> | Medium |
112+
| 701 | [Insert into a Binary Search Tree](https://leetcode.com/problems/insert-into-a-binary-search-tree/) | <a href="leetcode/00701_insert-into-a-binary-search-tree/701-insert-into-a-binary-search-tree.go"><img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/go/go-original.svg" width="20" height="20"></a><a href="leetcode/00701_insert-into-a-binary-search-tree/701-insert-into-a-binary-search-tree.java"><img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/java/java-original.svg" width="20" height="20"></a><a href="leetcode/00701_insert-into-a-binary-search-tree/701-insert-into-a-binary-search-tree.py"><img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/python/python-original.svg" width="20" height="20"></a> | Medium |
112113
| 706 | [Design HashMap](https://leetcode.com/problems/design-hashmap/) | <a href="leetcode/00706_design-hashmap/706-design-hashmap.py"><img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/python/python-original.svg" width="20" height="20"></a> | Easy |
113114
| 728 | [Self Dividing Numbers](https://leetcode.com/problems/self-dividing-numbers/) | <a href="leetcode/00728_self-dividing-numbers/self-dividing-numbers.py"><img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/python/python-original.svg" width="20" height="20"></a> | Easy |
114115
| 735 | [Asteroid Collision](https://leetcode.com/problems/asteroid-collision/) | <a href="leetcode/00735_asteroid-collision/735-asteroid-collision.py"><img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/python/python-original.svg" width="20" height="20"></a> | Medium |
@@ -135,6 +136,7 @@
135136
| 1029 | [Two City Scheduling](https://leetcode.com/problems/two-city-scheduling/) | <a href="leetcode/01029_two-city-scheduling/1029-two-city-scheduling.py"><img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/python/python-original.svg" width="20" height="20"></a> | Medium |
136137
| 1041 | [Robot Bounded In Circle](https://leetcode.com/problems/robot-bounded-in-circle/) | <a href="leetcode/01041_robot-bounded-in-circle/1041-robot-bounded-in-circle.py"><img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/python/python-original.svg" width="20" height="20"></a> | Medium |
137138
| 1044 | [Longest Duplicate Substring](https://leetcode.com/problems/longest-duplicate-substring/) | <a href="leetcode/01044_longest-duplicate-substring/longest-duplicate-substring.py"><img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/python/python-original.svg" width="20" height="20"></a> | Hard |
139+
| 1091 | [Shortest Path in Binary Matrix](https://leetcode.com/problems/shortest-path-in-binary-matrix/) | <a href="leetcode/01091_shortest-path-in-binary-matrix/1091-shortest-path-in-binary-matrix.py"><img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/python/python-original.svg" width="20" height="20"></a> | Medium |
138140
| 1094 | [Car Pooling](https://leetcode.com/problems/car-pooling/) | <a href="leetcode/01094_car-pooling/1094-car-pooling.py"><img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/python/python-original.svg" width="20" height="20"></a> | Medium |
139141
| 1100 | [Find K-Length Substrings With No Repeated Characters](https://leetcode.com/problems/find-k-length-substrings-with-no-repeated-characters/) | <a href="leetcode/01100_find-k-length-substrings-with-no-repeated-characters/1100-find-k-length-substrings-with-no-repeated-characters.py"><img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/python/python-original.svg" width="20" height="20"></a> | Medium |
140142
| 1143 | [Longest Common Subsequence](https://leetcode.com/problems/longest-common-subsequence/) | <a href="leetcode/01143_longest-common-subsequence/1143-longest-common-subsequence.py"><img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/python/python-original.svg" width="20" height="20"></a> | Medium |
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from collections import defaultdict
2+
3+
4+
class Solution:
5+
def groupStrings(self, strings: List[str]) -> List[List[str]]:
6+
memo = defaultdict(list)
7+
for string in strings:
8+
if len(string) == 1:
9+
memo[tuple()].append(string)
10+
continue
11+
12+
keys = []
13+
for idx, char in enumerate(string[:-1]):
14+
diff = ord(string[idx+1]) - ord(char)
15+
if diff < 0:
16+
diff += 26
17+
keys.append(diff)
18+
19+
memo[tuple(keys)].append(string)
20+
return list(memo.values())
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class Solution:
2+
def numDistinctIslands(self, grid: List[List[int]]) -> int:
3+
R, C = len(grid), len(grid[0])
4+
5+
unique_encoded = set()
6+
7+
offsets = (
8+
(0, 1),
9+
(0, -1),
10+
(1, 0),
11+
(-1, 0),
12+
)
13+
14+
def bfs(start_r, start_c):
15+
nodes = deque([(start_r, start_c)])
16+
encoded = list()
17+
18+
while nodes:
19+
r, c = nodes.popleft()
20+
if not (0 <= r < R) or \
21+
not (0 <= c < C) or \
22+
grid[r][c] == 0:
23+
continue
24+
25+
encoded.append((r - start_r, c - start_c))
26+
grid[r][c] = 0
27+
28+
for dr, dc in offsets:
29+
nodes.append((r + dr, c + dc))
30+
31+
return ','.join(str(e) for e in encoded)
32+
33+
for r in range(R):
34+
for c in range(C):
35+
if grid[r][c] == 0:
36+
continue
37+
38+
encoded = bfs(r, c)
39+
unique_encoded.add(encoded)
40+
41+
return len(unique_encoded)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<h2><a href="https://leetcode.com/problems/number-of-distinct-islands/">694. Number of Distinct Islands</a></h2><h3>Medium</h3><hr><div><p>You are given an <code>m x n</code> binary matrix <code>grid</code>. An island is a group of <code>1</code>'s (representing land) connected <strong>4-directionally</strong> (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.</p>
2+
3+
<p>An island is considered to be the same as another if and only if one island can be translated (and not rotated or reflected) to equal the other.</p>
4+
5+
<p>Return <em>the number of <b>distinct</b> islands</em>.</p>
6+
7+
<p>&nbsp;</p>
8+
<p><strong>Example 1:</strong></p>
9+
<img alt="" src="https://assets.leetcode.com/uploads/2021/05/01/distinctisland1-1-grid.jpg" style="width: 413px; height: 334px;">
10+
<pre><strong>Input:</strong> grid = [[1,1,0,0,0],[1,1,0,0,0],[0,0,0,1,1],[0,0,0,1,1]]
11+
<strong>Output:</strong> 1
12+
</pre>
13+
14+
<p><strong>Example 2:</strong></p>
15+
<img alt="" src="https://assets.leetcode.com/uploads/2021/05/01/distinctisland1-2-grid.jpg" style="width: 413px; height: 334px;">
16+
<pre><strong>Input:</strong> grid = [[1,1,0,1,1],[1,0,0,0,0],[0,0,0,0,1],[1,1,0,1,1]]
17+
<strong>Output:</strong> 3
18+
</pre>
19+
20+
<p>&nbsp;</p>
21+
<p><strong>Constraints:</strong></p>
22+
23+
<ul>
24+
<li><code>m == grid.length</code></li>
25+
<li><code>n == grid[i].length</code></li>
26+
<li><code>1 &lt;= m, n &lt;= 50</code></li>
27+
<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
28+
</ul>
29+
</div>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class Solution:
2+
def shortestPathBinaryMatrix(self, grid: List[List[int]]) -> int:
3+
if grid[0][0] == 1:
4+
return -1
5+
6+
R, C = len(grid), len(grid[0])
7+
8+
if grid[R - 1][C - 1] == 1:
9+
return -1
10+
11+
offsets = (
12+
(1, -1),
13+
(1, 0),
14+
(1, 1),
15+
(0, 1),
16+
(0, -1),
17+
(-1, -1),
18+
(-1, 0),
19+
(-1, 1),
20+
)
21+
22+
visited = [[sys.maxsize] * C for _ in range(R)]
23+
24+
nodes = deque([(0, 0, 1)])
25+
while nodes:
26+
r, c, steps = nodes.popleft()
27+
28+
if not (0 <= r < R) or \
29+
not (0 <= c < C):
30+
continue
31+
32+
if grid[r][c] == 1:
33+
continue
34+
35+
if steps >= visited[r][c]:
36+
continue
37+
38+
visited[r][c] = steps
39+
40+
for dr, dc in offsets:
41+
nodes.append((r + dr, c + dc, steps + 1))
42+
43+
if visited[R - 1][C - 1] == sys.maxsize:
44+
return -1
45+
return visited[R - 1][C - 1]
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<h2><a href="https://leetcode.com/problems/shortest-path-in-binary-matrix/">1091. Shortest Path in Binary Matrix</a></h2><h3>Medium</h3><hr><div><p>Given an <code>n x n</code> binary matrix <code>grid</code>, return <em>the length of the shortest <strong>clear path</strong> in the matrix</em>. If there is no clear path, return <code>-1</code>.</p>
2+
3+
<p>A <strong>clear path</strong> in a binary matrix is a path from the <strong>top-left</strong> cell (i.e., <code>(0, 0)</code>) to the <strong>bottom-right</strong> cell (i.e., <code>(n - 1, n - 1)</code>) such that:</p>
4+
5+
<ul>
6+
<li>All the visited cells of the path are <code>0</code>.</li>
7+
<li>All the adjacent cells of the path are <strong>8-directionally</strong> connected (i.e., they are different and they share an edge or a corner).</li>
8+
</ul>
9+
10+
<p>The <strong>length of a clear path</strong> is the number of visited cells of this path.</p>
11+
12+
<p>&nbsp;</p>
13+
<p><strong>Example 1:</strong></p>
14+
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/example1_1.png" style="width: 500px; height: 234px;">
15+
<pre><strong>Input:</strong> grid = [[0,1],[1,0]]
16+
<strong>Output:</strong> 2
17+
</pre>
18+
19+
<p><strong>Example 2:</strong></p>
20+
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/example2_1.png" style="height: 216px; width: 500px;">
21+
<pre><strong>Input:</strong> grid = [[0,0,0],[1,1,0],[1,1,0]]
22+
<strong>Output:</strong> 4
23+
</pre>
24+
25+
<p><strong>Example 3:</strong></p>
26+
27+
<pre><strong>Input:</strong> grid = [[1,0,0],[1,1,0],[1,1,0]]
28+
<strong>Output:</strong> -1
29+
</pre>
30+
31+
<p>&nbsp;</p>
32+
<p><strong>Constraints:</strong></p>
33+
34+
<ul>
35+
<li><code>n == grid.length</code></li>
36+
<li><code>n == grid[i].length</code></li>
37+
<li><code>1 &lt;= n &lt;= 100</code></li>
38+
<li><code>grid[i][j] is 0 or 1</code></li>
39+
</ul>
40+
</div>

0 commit comments

Comments
 (0)