Skip to content

Commit c2a4479

Browse files
authored
feat: update lc problems (#3089)
1 parent d7cbf26 commit c2a4479

File tree

43 files changed

+453
-200
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+453
-200
lines changed

solution/0100-0199/0130.Surrounded Regions/README_EN.md

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,36 @@ tags:
2020

2121
<!-- description:start -->
2222

23-
<p>Given an <code>m x n</code> matrix <code>board</code> containing <code>&#39;X&#39;</code> and <code>&#39;O&#39;</code>, <em>capture all regions that are 4-directionally&nbsp;surrounded by</em> <code>&#39;X&#39;</code>.</p>
23+
<p>You are given an <code>m x n</code> matrix <code>board</code> containing <strong>letters</strong> <code>&#39;X&#39;</code> and <code>&#39;O&#39;</code>, <strong>capture regions</strong> that are <strong>surrounded</strong>:</p>
2424

25-
<p>A region is <strong>captured</strong> by flipping all <code>&#39;O&#39;</code>s into <code>&#39;X&#39;</code>s in that surrounded region.</p>
25+
<ul>
26+
<li><strong>Connect</strong>: A cell is connected to adjacent cells horizontally or vertically.</li>
27+
<li><strong>Region</strong>: To form a region <strong>connect every</strong> <code>&#39;O&#39;</code> cell.</li>
28+
<li><strong>Surround</strong>: The region is surrounded with <code>&#39;X&#39;</code> cells if you can <strong>connect the region </strong>with <code>&#39;X&#39;</code> cells and none of the region cells are on the edge of the <code>board</code>.</li>
29+
</ul>
30+
31+
<p>A <strong>surrounded region is captured</strong> by replacing all <code>&#39;O&#39;</code>s with <code>&#39;X&#39;</code>s in the input matrix <code>board</code>.</p>
2632

2733
<p>&nbsp;</p>
2834
<p><strong class="example">Example 1:</strong></p>
29-
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0100-0199/0130.Surrounded%20Regions/images/xogrid.jpg" style="width: 550px; height: 237px;" />
30-
<pre>
31-
<strong>Input:</strong> board = [[&quot;X&quot;,&quot;X&quot;,&quot;X&quot;,&quot;X&quot;],[&quot;X&quot;,&quot;O&quot;,&quot;O&quot;,&quot;X&quot;],[&quot;X&quot;,&quot;X&quot;,&quot;O&quot;,&quot;X&quot;],[&quot;X&quot;,&quot;O&quot;,&quot;X&quot;,&quot;X&quot;]]
32-
<strong>Output:</strong> [[&quot;X&quot;,&quot;X&quot;,&quot;X&quot;,&quot;X&quot;],[&quot;X&quot;,&quot;X&quot;,&quot;X&quot;,&quot;X&quot;],[&quot;X&quot;,&quot;X&quot;,&quot;X&quot;,&quot;X&quot;],[&quot;X&quot;,&quot;O&quot;,&quot;X&quot;,&quot;X&quot;]]
33-
<strong>Explanation:</strong> Notice that an &#39;O&#39; should not be flipped if:
34-
- It is on the border, or
35-
- It is adjacent to an &#39;O&#39; that should not be flipped.
36-
The bottom &#39;O&#39; is on the border, so it is not flipped.
37-
The other three &#39;O&#39; form a surrounded region, so they are flipped.
38-
</pre>
35+
36+
<div class="example-block">
37+
<p><strong>Input:</strong> <span class="example-io">board = [[&quot;X&quot;,&quot;X&quot;,&quot;X&quot;,&quot;X&quot;],[&quot;X&quot;,&quot;O&quot;,&quot;O&quot;,&quot;X&quot;],[&quot;X&quot;,&quot;X&quot;,&quot;O&quot;,&quot;X&quot;],[&quot;X&quot;,&quot;O&quot;,&quot;X&quot;,&quot;X&quot;]]</span></p>
38+
39+
<p><strong>Output:</strong> <span class="example-io">[[&quot;X&quot;,&quot;X&quot;,&quot;X&quot;,&quot;X&quot;],[&quot;X&quot;,&quot;X&quot;,&quot;X&quot;,&quot;X&quot;],[&quot;X&quot;,&quot;X&quot;,&quot;X&quot;,&quot;X&quot;],[&quot;X&quot;,&quot;O&quot;,&quot;X&quot;,&quot;X&quot;]]</span></p>
40+
41+
<p><strong>Explanation:</strong></p>
42+
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0100-0199/0130.Surrounded%20Regions/images/xogrid.jpg" style="width: 367px; height: 158px;" />
43+
<p>In the above diagram, the bottom region is not captured because it is on the edge of the board and cannot be surrounded.</p>
44+
</div>
3945

4046
<p><strong class="example">Example 2:</strong></p>
4147

42-
<pre>
43-
<strong>Input:</strong> board = [[&quot;X&quot;]]
44-
<strong>Output:</strong> [[&quot;X&quot;]]
45-
</pre>
48+
<div class="example-block">
49+
<p><strong>Input:</strong> <span class="example-io">board = [[&quot;X&quot;]]</span></p>
50+
51+
<p><strong>Output:</strong> <span class="example-io">[[&quot;X&quot;]]</span></p>
52+
</div>
4653

4754
<p>&nbsp;</p>
4855
<p><strong>Constraints:</strong></p>

solution/0400-0499/0419.Battleships in a Board/README.md

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,13 @@ tags:
5959

6060
<!-- solution:start -->
6161

62-
### 方法一
62+
### 方法一:直接遍历
63+
64+
我们可以遍历矩阵,找到每个战舰的左上角,即当前位置为 `X` 且上方和左方都不是 `X` 的位置,将答案加一。
65+
66+
遍历结束后,返回答案即可。
67+
68+
时间复杂度 $O(m \times n)$,其中 $m$ 和 $n$ 分别是矩阵的行数和列数。空间复杂度 $O(1)$。
6369

6470
<!-- tabs:start -->
6571

@@ -118,9 +124,15 @@ public:
118124
int ans = 0;
119125
for (int i = 0; i < m; ++i) {
120126
for (int j = 0; j < n; ++j) {
121-
if (board[i][j] == '.') continue;
122-
if (i > 0 && board[i - 1][j] == 'X') continue;
123-
if (j > 0 && board[i][j - 1] == 'X') continue;
127+
if (board[i][j] == '.') {
128+
continue;
129+
}
130+
if (i > 0 && board[i - 1][j] == 'X') {
131+
continue;
132+
}
133+
if (j > 0 && board[i][j - 1] == 'X') {
134+
continue;
135+
}
124136
++ans;
125137
}
126138
}
@@ -132,12 +144,10 @@ public:
132144
#### Go
133145
134146
```go
135-
func countBattleships(board [][]byte) int {
136-
m, n := len(board), len(board[0])
137-
ans := 0
138-
for i := 0; i < m; i++ {
139-
for j := 0; j < n; j++ {
140-
if board[i][j] == '.' {
147+
func countBattleships(board [][]byte) (ans int) {
148+
for i, row := range board {
149+
for j, c := range row {
150+
if c == '.' {
141151
continue
142152
}
143153
if i > 0 && board[i-1][j] == 'X' {
@@ -149,7 +159,32 @@ func countBattleships(board [][]byte) int {
149159
ans++
150160
}
151161
}
152-
return ans
162+
return
163+
}
164+
```
165+
166+
#### TypeScript
167+
168+
```ts
169+
function countBattleships(board: string[][]): number {
170+
const m = board.length;
171+
const n = board[0].length;
172+
let ans = 0;
173+
for (let i = 0; i < m; ++i) {
174+
for (let j = 0; j < n; ++j) {
175+
if (board[i][j] === '.') {
176+
continue;
177+
}
178+
if (i && board[i - 1][j] === 'X') {
179+
continue;
180+
}
181+
if (j && board[i][j - 1] === 'X') {
182+
continue;
183+
}
184+
++ans;
185+
}
186+
}
187+
return ans;
153188
}
154189
```
155190

solution/0400-0499/0419.Battleships in a Board/README_EN.md

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,13 @@ tags:
5656

5757
<!-- solution:start -->
5858

59-
### Solution 1
59+
### Solution 1: Direct Iteration
60+
61+
We can iterate through the matrix, find the top-left corner of each battleship, i.e., the position where the current position is `X` and both the top and left are not `X`, and increment the answer by one.
62+
63+
After the iteration ends, return the answer.
64+
65+
The time complexity is $O(m \times n)$, where $m$ and $n$ are the number of rows and columns of the matrix, respectively. The space complexity is $O(1)$.
6066

6167
<!-- tabs:start -->
6268

@@ -115,9 +121,15 @@ public:
115121
int ans = 0;
116122
for (int i = 0; i < m; ++i) {
117123
for (int j = 0; j < n; ++j) {
118-
if (board[i][j] == '.') continue;
119-
if (i > 0 && board[i - 1][j] == 'X') continue;
120-
if (j > 0 && board[i][j - 1] == 'X') continue;
124+
if (board[i][j] == '.') {
125+
continue;
126+
}
127+
if (i > 0 && board[i - 1][j] == 'X') {
128+
continue;
129+
}
130+
if (j > 0 && board[i][j - 1] == 'X') {
131+
continue;
132+
}
121133
++ans;
122134
}
123135
}
@@ -129,12 +141,10 @@ public:
129141
#### Go
130142
131143
```go
132-
func countBattleships(board [][]byte) int {
133-
m, n := len(board), len(board[0])
134-
ans := 0
135-
for i := 0; i < m; i++ {
136-
for j := 0; j < n; j++ {
137-
if board[i][j] == '.' {
144+
func countBattleships(board [][]byte) (ans int) {
145+
for i, row := range board {
146+
for j, c := range row {
147+
if c == '.' {
138148
continue
139149
}
140150
if i > 0 && board[i-1][j] == 'X' {
@@ -146,7 +156,32 @@ func countBattleships(board [][]byte) int {
146156
ans++
147157
}
148158
}
149-
return ans
159+
return
160+
}
161+
```
162+
163+
#### TypeScript
164+
165+
```ts
166+
function countBattleships(board: string[][]): number {
167+
const m = board.length;
168+
const n = board[0].length;
169+
let ans = 0;
170+
for (let i = 0; i < m; ++i) {
171+
for (let j = 0; j < n; ++j) {
172+
if (board[i][j] === '.') {
173+
continue;
174+
}
175+
if (i && board[i - 1][j] === 'X') {
176+
continue;
177+
}
178+
if (j && board[i][j - 1] === 'X') {
179+
continue;
180+
}
181+
++ans;
182+
}
183+
}
184+
return ans;
150185
}
151186
```
152187

solution/0400-0499/0419.Battleships in a Board/Solution.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,15 @@ class Solution {
55
int ans = 0;
66
for (int i = 0; i < m; ++i) {
77
for (int j = 0; j < n; ++j) {
8-
if (board[i][j] == '.') continue;
9-
if (i > 0 && board[i - 1][j] == 'X') continue;
10-
if (j > 0 && board[i][j - 1] == 'X') continue;
8+
if (board[i][j] == '.') {
9+
continue;
10+
}
11+
if (i > 0 && board[i - 1][j] == 'X') {
12+
continue;
13+
}
14+
if (j > 0 && board[i][j - 1] == 'X') {
15+
continue;
16+
}
1117
++ans;
1218
}
1319
}
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
func countBattleships(board [][]byte) int {
2-
m, n := len(board), len(board[0])
3-
ans := 0
4-
for i := 0; i < m; i++ {
5-
for j := 0; j < n; j++ {
6-
if board[i][j] == '.' {
1+
func countBattleships(board [][]byte) (ans int) {
2+
for i, row := range board {
3+
for j, c := range row {
4+
if c == '.' {
75
continue
86
}
97
if i > 0 && board[i-1][j] == 'X' {
@@ -15,5 +13,5 @@ func countBattleships(board [][]byte) int {
1513
ans++
1614
}
1715
}
18-
return ans
16+
return
1917
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function countBattleships(board: string[][]): number {
2+
const m = board.length;
3+
const n = board[0].length;
4+
let ans = 0;
5+
for (let i = 0; i < m; ++i) {
6+
for (let j = 0; j < n; ++j) {
7+
if (board[i][j] === '.') {
8+
continue;
9+
}
10+
if (i && board[i - 1][j] === 'X') {
11+
continue;
12+
}
13+
if (j && board[i][j - 1] === 'X') {
14+
continue;
15+
}
16+
++ans;
17+
}
18+
}
19+
return ans;
20+
}

solution/0800-0899/0853.Car Fleet/README_EN.md

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,48 +19,59 @@ tags:
1919

2020
<!-- description:start -->
2121

22-
<p>There are <code>n</code> cars going to the same destination along a one-lane road. The destination is <code>target</code> miles away.</p>
22+
<p>There are <code>n</code> cars at given miles away from the starting mile 0, traveling to reach the mile <code>target</code>.</p>
2323

24-
<p>You are given two integer array <code>position</code> and <code>speed</code>, both of length <code>n</code>, where <code>position[i]</code> is the position of the <code>i<sup>th</sup></code> car and <code>speed[i]</code> is the speed of the <code>i<sup>th</sup></code> car (in miles per hour).</p>
24+
<p>You are given two integer array <code>position</code> and <code>speed</code>, both of length <code>n</code>, where <code>position[i]</code> is the starting mile of the <code>i<sup>th</sup></code> car and <code>speed[i]</code> is the speed of the <code>i<sup>th</sup></code> car in miles per hour.</p>
2525

26-
<p>A car can never pass another car ahead of it, but it can catch up to it&nbsp;and drive bumper to bumper <strong>at the same speed</strong>. The faster car will <strong>slow down</strong> to match the slower car&#39;s speed. The distance between these two cars is ignored (i.e., they are assumed to have the same position).</p>
26+
<p>A car cannot pass another car, but it can catch up and then travel next to it at the speed of the slower car.</p>
2727

28-
<p>A <strong>car fleet</strong> is some non-empty set of cars driving at the same position and same speed. Note that a single car is also a car fleet.</p>
28+
<p>A <strong>car fleet</strong> is a car or cars driving next to each other. The speed of the car fleet is the <strong>minimum</strong> speed of any car in the fleet.</p>
2929

30-
<p>If a car catches up to a car fleet right at the destination point, it will still be considered as one car fleet.</p>
30+
<p>If a car catches up to a car fleet at the mile <code>target</code>, it will still be considered as part of the car fleet.</p>
3131

32-
<p>Return <em>the <strong>number of car fleets</strong> that will arrive at the destination</em>.</p>
32+
<p>Return the number of car fleets that will arrive at the destination.</p>
3333

3434
<p>&nbsp;</p>
3535
<p><strong class="example">Example 1:</strong></p>
3636

37-
<pre>
38-
<strong>Input:</strong> target = 12, position = [10,8,0,5,3], speed = [2,4,1,1,3]
39-
<strong>Output:</strong> 3
40-
<strong>Explanation:</strong>
41-
The cars starting at 10 (speed 2) and 8 (speed 4) become a fleet, meeting each other at 12.
42-
The car starting at 0 does not catch up to any other car, so it is a fleet by itself.
43-
The cars starting at 5 (speed 1) and 3 (speed 3) become a fleet, meeting each other at 6. The fleet moves at speed 1 until it reaches target.
44-
Note that no other cars meet these fleets before the destination, so the answer is 3.
45-
</pre>
37+
<div class="example-block">
38+
<p><strong>Input:</strong> <span class="example-io">target = 12, position = [10,8,0,5,3], speed = [2,4,1,1,3]</span></p>
39+
40+
<p><strong>Output:</strong> <span class="example-io">3</span></p>
41+
42+
<p><strong>Explanation:</strong></p>
43+
44+
<ul>
45+
<li>The cars starting at 10 (speed 2) and 8 (speed 4) become a fleet, meeting each other at 12. The fleet forms at <code>target</code>.</li>
46+
<li>The car starting at 0 (speed 1) does not catch up to any other car, so it is a fleet by itself.</li>
47+
<li>The cars starting at 5 (speed 1) and 3 (speed 3) become a fleet, meeting each other at 6. The fleet moves at speed 1 until it reaches <code>target</code>.</li>
48+
</ul>
49+
</div>
4650

4751
<p><strong class="example">Example 2:</strong></p>
4852

49-
<pre>
50-
<strong>Input:</strong> target = 10, position = [3], speed = [3]
51-
<strong>Output:</strong> 1
52-
<strong>Explanation:</strong> There is only one car, hence there is only one fleet.
53-
</pre>
53+
<div class="example-block">
54+
<p><strong>Input:</strong> <span class="example-io">target = 10, position = [3], speed = [3]</span></p>
55+
56+
<p><strong>Output:</strong> <span class="example-io">1</span></p>
57+
58+
<p><strong>Explanation:</strong></p>
59+
There is only one car, hence there is only one fleet.</div>
5460

5561
<p><strong class="example">Example 3:</strong></p>
5662

57-
<pre>
58-
<strong>Input:</strong> target = 100, position = [0,2,4], speed = [4,2,1]
59-
<strong>Output:</strong> 1
60-
<strong>Explanation:</strong>
61-
The cars starting at 0 (speed 4) and 2 (speed 2) become a fleet, meeting each other at 4. The fleet moves at speed 2.
62-
Then, the fleet (speed 2) and the car starting at 4 (speed 1) become one fleet, meeting each other at 6. The fleet moves at speed 1 until it reaches target.
63-
</pre>
63+
<div class="example-block">
64+
<p><strong>Input:</strong> <span class="example-io">target = 100, position = [0,2,4], speed = [4,2,1]</span></p>
65+
66+
<p><strong>Output:</strong> <span class="example-io">1</span></p>
67+
68+
<p><strong>Explanation:</strong></p>
69+
70+
<ul>
71+
<li>The cars starting at 0 (speed 4) and 2 (speed 2) become a fleet, meeting each other at 4. The car starting at 4 (speed 1) travels to 5.</li>
72+
<li>Then, the fleet at 4 (speed 2) and the car at position 5 (speed 1) become one fleet, meeting each other at 6. The fleet moves at speed 1 until it reaches <code>target</code>.</li>
73+
</ul>
74+
</div>
6475

6576
<p>&nbsp;</p>
6677
<p><strong>Constraints:</strong></p>

0 commit comments

Comments
 (0)