Skip to content

Commit 5b107ef

Browse files
committed
feat: update solutions to lc problems
* No.1162.As Far from Land as Possible * No.2373.Largest Local Values in a Matrix * No.2379.Minimum Recolors to Get K Consecutive Black Blocks * No.2386.Find the K-Sum of an Array * No.2387.Median of a Row Wise Sorted Matrix * No.2390.Removing Stars From a String
1 parent 50f9a5d commit 5b107ef

File tree

7 files changed

+24
-17
lines changed

7 files changed

+24
-17
lines changed

solution/1100-1199/1162.As Far from Land as Possible/README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,20 +75,21 @@
7575
class Solution:
7676
def maxDistance(self, grid: List[List[int]]) -> int:
7777
n = len(grid)
78-
q = deque([(i, j) for i in range(n) for j in range(n) if grid[i][j] == 1])
78+
q = deque((i, j) for i in range(n) for j in range(n) if grid[i][j])
7979
ans = -1
80-
valid = False
80+
if len(q) in (0, n * n):
81+
return ans
82+
dirs = (-1, 0, 1, 0, -1)
8183
while q:
82-
ans += 1
8384
for _ in range(len(q)):
8485
i, j = q.popleft()
85-
for a, b in [[0, 1], [0, -1], [1, 0], [-1, 0]]:
86-
x, y = i + a, b + j
86+
for a, b in pairwise(dirs):
87+
x, y = i + a, j + b
8788
if 0 <= x < n and 0 <= y < n and grid[x][y] == 0:
88-
valid = True
8989
grid[x][y] = 1
9090
q.append((x, y))
91-
return ans if valid else -1
91+
ans += 1
92+
return ans
9293
```
9394

9495
### **Java**

solution/2300-2399/2373.Largest Local Values in a Matrix/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@
5454

5555
<!-- 这里可写通用的实现逻辑 -->
5656

57-
**方法一:模拟**
57+
**方法一:枚举**
5858

59-
枚举每个 $3 \times 3$ 的矩阵,求出最大值,放入答案矩阵中
59+
我们可以枚举每个 $3 \times 3$ 的矩阵,求出每个 $3 \times 3$ 的矩阵中的最大值,然后将这些最大值放入答案矩阵中
6060

61-
时间复杂度 $O(n^2)$,空间复杂度 $O(n^2)$。其中 $n$ 为矩阵的边长
61+
时间复杂度 $O(n^2)$,其中 $n$ 是矩阵的边长。忽略答案矩阵的空间消耗,空间复杂度 $O(1)$
6262

6363
<!-- tabs:start -->
6464

solution/2300-2399/2379.Minimum Recolors to Get K Consecutive Black Blocks/README.md

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

5858
遍历 $blocks$,找出 $k$ 大小的窗口中的白色块个数的最小值。
5959

60-
时间复杂度 $O(n)$,空间复杂度 $O(1)$。
60+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为 $blocks$ 的长度。
6161

6262
<!-- tabs:start -->
6363

@@ -73,7 +73,7 @@ class Solution:
7373
i, n = k, len(blocks)
7474
while i < n:
7575
cnt += blocks[i] == 'W'
76-
cnt -= blocks[i-k] == 'W'
76+
cnt -= blocks[i - k] == 'W'
7777
ans = min(ans, cnt)
7878
i += 1
7979
return ans

solution/2300-2399/2379.Minimum Recolors to Get K Consecutive Black Blocks/README_EN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class Solution:
5959
i, n = k, len(blocks)
6060
while i < n:
6161
cnt += blocks[i] == 'W'
62-
cnt -= blocks[i-k] == 'W'
62+
cnt -= blocks[i - k] == 'W'
6363
ans = min(ans, cnt)
6464
i += 1
6565
return ans

solution/2300-2399/2386.Find the K-Sum of an Array/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161

6262
由于数组是从小到大排序,可以证明,这种方式能够不重不漏地按序遍历完所有的子序列和。
6363

64-
时间复杂度 $O(nlogn+klogk)$。其中 $n$ 是数组 `nums` 的长度。
64+
时间复杂度 $O(n \times \log n + k \times \log k)$。其中 $n$ 是数组 `nums` 的长度,而 $k$ 是题目中给定的 $k$
6565

6666
<!-- tabs:start -->
6767

solution/2300-2399/2387.Median of a Row Wise Sorted Matrix/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@
4747

4848
**方法一:两次二分查找**
4949

50-
中位数实际上是排序后第 $target = \left \lceil \frac{m\times n}{2} \right \rceil $ 个数。
50+
中位数实际上是排序后第 $target = \left \lceil \frac{m\times n}{2} \right \rceil$ 个数。
5151

5252
我们二分枚举矩阵的元素 $x$,统计网格中大于该元素的个数 $cnt$,如果 $cnt \ge target$,说明中位数在 $x$ 的左侧(包含 $x$),否则在右侧。
5353

54-
时间复杂度 $O(m\times \log n \times log M)$,其中 $M$ 为矩阵中的最大值
54+
时间复杂度 $O(m\times \log n \times log M)$,空间复杂度 $O(1)$。其中 $m$ 和 $n$ 分别为网格的行数和列数;而 $M$ 为网格中的最大元素
5555

5656
<!-- tabs:start -->
5757

solution/2300-2399/2390.Removing Stars From a String/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,13 @@
6161

6262
**方法一:栈模拟**
6363

64-
时间复杂度 $O(n)$,空间复杂度 $O(n)$。
64+
我们可以使用栈模拟操作过程。
65+
66+
遍历字符串 $s$,如果当前字符不是星号,则将其入栈;如果当前字符是星号,则将栈顶元素出栈。
67+
68+
最后我们将栈中元素拼接成字符串返回即可。
69+
70+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 $s$ 的长度。
6571

6672
<!-- tabs:start -->
6773

0 commit comments

Comments
 (0)