Skip to content

Commit 3e3b7e2

Browse files
committed
feat: add solutions to lc problems: No.2287~2290
* No.2287.Rearrange Characters to Make Target String * No.2288.Apply Discount to Prices * No.2289.Steps to Make Array Non-decreasing * No.2290.Minimum Obstacle Removal to Reach Corner
1 parent 27610b1 commit 3e3b7e2

File tree

45 files changed

+2533
-18
lines changed

Some content is hidden

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

45 files changed

+2533
-18
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
- [访问所有节点的最短路径](/solution/0800-0899/0847.Shortest%20Path%20Visiting%20All%20Nodes/README.md) - `BFS``最小步数模型``A* 算法`
8787
- [为高尔夫比赛砍树](/solution/0600-0699/0675.Cut%20Off%20Trees%20for%20Golf%20Event/README.md) - `BFS``A* 算法`
8888
- [使网格图至少有一条有效路径的最小代价](/solution/1300-1399/1368.Minimum%20Cost%20to%20Make%20at%20Least%20One%20Valid%20Path%20in%20a%20Grid/README.md) - `双端队列 BFS`
89+
- [到达角落需要移除障碍物的最小数目](/solution/2200-2299/2290.Minimum%20Obstacle%20Removal%20to%20Reach%20Corner/README.md) - `双端队列 BFS`
8990
- [迷宫](/solution/0400-0499/0490.The%20Maze/README.md) - `DFS``连通性模型``Flood Fill 算法`
9091
- [单词搜索](/solution/0000-0099/0079.Word%20Search/README.md) - `DFS``搜索顺序``回溯`
9192
- [黄金矿工](/solution/1200-1299/1219.Path%20with%20Maximum%20Gold/README.md) - `DFS``搜索顺序``回溯`

README_EN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ Complete solutions to [LeetCode](https://leetcode.com/problemset/all/), [LCOF](h
8484
- [Shortest Path Visiting All Nodes](/solution/0800-0899/0847.Shortest%20Path%20Visiting%20All%20Nodes/README_EN.md) - `BFS`, `Minimum steps model`, `A* search`
8585
- [Cut Off Trees for Golf Event](/solution/0600-0699/0675.Cut%20Off%20Trees%20for%20Golf%20Event/README_EN.md) - `BFS`, `A* search`
8686
- [Minimum Cost to Make at Least One Valid Path in a Grid](/solution/1300-1399/1368.Minimum%20Cost%20to%20Make%20at%20Least%20One%20Valid%20Path%20in%20a%20Grid/README_EN.md) - `BFS using deque`
87+
- [Minimum Cost to Make at Least One Valid Path in a Grid](/solution/2200-2299/2290.Minimum%20Obstacle%20Removal%20to%20Reach%20Corner/README_EN.md) - `BFS using deque`
8788
- [The Maze](/solution/0400-0499/0490.The%20Maze/README_EN.md) - `DFS, Flood fill`
8889
- [Word Search](/solution/0000-0099/0079.Word%20Search/README_EN.md) - `DFS`, `Backtracking`
8990
- [Path with Maximum Gold](/solution/1200-1299/1219.Path%20with%20Maximum%20Gold/README_EN.md) - `DFS`, `Backtracking`

solution/1300-1399/1368.Minimum Cost to Make at Least One Valid Path in a Grid/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@
7979

8080
<!-- 这里可写通用的实现逻辑 -->
8181

82-
双端队列 BFS。本题实际上也是最短路模型,只不过求解的是改变方向的最小次数。
82+
**方法一:双端队列 BFS**
83+
84+
本题实际上也是最短路模型,只不过求解的是改变方向的最小次数。
8385

8486
在一个边权只有 0、1 的无向图中搜索最短路径可以使用双端队列进行 BFS。其原理是当前可以扩展到的点的权重为 0 时,将其加入队首;权重为 1 时,将其加入队尾。
8587

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# [2282. Number of People That Can Be Seen in a Grid](https://leetcode.cn/problems/number-of-people-that-can-be-seen-in-a-grid)
2+
3+
[English Version](/solution/2200-2299/2282.Number%20of%20People%20That%20Can%20Be%20Seen%20in%20a%20Grid/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>You are given an <code>m x n</code> <strong>0-indexed</strong> 2D array of positive integers <code>heights</code> where <code>heights[i][j]</code> is the height of the person standing at position <code>(i, j)</code>.</p>
10+
11+
<p>A person standing at position <code>(row<sub>1</sub>, col<sub>1</sub>)</code> can see a person standing at position <code>(row<sub>2</sub>, col<sub>2</sub>)</code> if:</p>
12+
13+
<ul>
14+
<li>The person at <code>(row<sub>2</sub>, col<sub>2</sub>)</code> is to the right <strong>or</strong> below the person at <code>(row<sub>1</sub>, col<sub>1</sub>)</code>. More formally, this means that either <code>row<sub>1</sub> == row<sub>2</sub></code> and <code>col<sub>1</sub> &lt; col<sub>2</sub></code> <strong>or</strong> <code>row<sub>1</sub> &lt; row<sub>2</sub></code> and <code>col<sub>1</sub> == col<sub>2</sub></code>.</li>
15+
<li>Everyone in between them is shorter than <strong>both</strong> of them.</li>
16+
</ul>
17+
18+
<p>Return<em> an </em><code>m x n</code><em> 2D array of integers </em><code>answer</code><em> where </em><code>answer[i][j]</code><em> is the number of people that the person at position </em><code>(i, j)</code><em> can see.</em></p>
19+
20+
<p>&nbsp;</p>
21+
<p><strong>Example 1:</strong></p>
22+
<img src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2200-2299/2282.Number%20of%20People%20That%20Can%20Be%20Seen%20in%20a%20Grid/images/image-20220524180458-1.png" style="width: 700px; height: 164px;" />
23+
<pre>
24+
<strong>Input:</strong> heights = [[3,1,4,2,5]]
25+
<strong>Output:</strong> [[2,1,2,1,0]]
26+
<strong>Explanation:</strong>
27+
- The person at (0, 0) can see the people at (0, 1) and (0, 2).
28+
Note that he cannot see the person at (0, 4) because the person at (0, 2) is taller than him.
29+
- The person at (0, 1) can see the person at (0, 2).
30+
- The person at (0, 2) can see the people at (0, 3) and (0, 4).
31+
- The person at (0, 3) can see the person at (0, 4).
32+
- The person at (0, 4) cannot see anybody.
33+
</pre>
34+
35+
<p><strong>Example 2:</strong></p>
36+
<img src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2200-2299/2282.Number%20of%20People%20That%20Can%20Be%20Seen%20in%20a%20Grid/images/image-20220523113533-2.png" style="width: 400px; height: 249px;" />
37+
<pre>
38+
<strong>Input:</strong> heights = [[5,1],[3,1],[4,1]]
39+
<strong>Output:</strong> [[3,1],[2,1],[1,0]]
40+
<strong>Explanation:</strong>
41+
- The person at (0, 0) can see the people at (0, 1), (1, 0) and (2, 0).
42+
- The person at (0, 1) can see the person at (1, 1).
43+
- The person at (1, 0) can see the people at (1, 1) and (2, 0).
44+
- The person at (1, 1) can see the person at (2, 1).
45+
- The person at (2, 0) can see the person at (2, 1).
46+
- The person at (2, 1) cannot see anybody.
47+
</pre>
48+
49+
<p>&nbsp;</p>
50+
<p><strong>Constraints:</strong></p>
51+
52+
<ul>
53+
<li><code>1 &lt;= heights.length &lt;= 400</code></li>
54+
<li><code>1 &lt;= heights[i].length &lt;= 400</code></li>
55+
<li><code>1 &lt;= heights[i][j] &lt;= 10<sup>5</sup></code></li>
56+
</ul>
57+
58+
59+
## 解法
60+
61+
<!-- 这里可写通用的实现逻辑 -->
62+
63+
<!-- tabs:start -->
64+
65+
### **Python3**
66+
67+
<!-- 这里可写当前语言的特殊实现逻辑 -->
68+
69+
```python
70+
71+
```
72+
73+
### **Java**
74+
75+
<!-- 这里可写当前语言的特殊实现逻辑 -->
76+
77+
```java
78+
79+
```
80+
81+
### **TypeScript**
82+
83+
```ts
84+
85+
```
86+
87+
### **...**
88+
89+
```
90+
91+
```
92+
93+
<!-- tabs:end -->
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# [2282. Number of People That Can Be Seen in a Grid](https://leetcode.com/problems/number-of-people-that-can-be-seen-in-a-grid)
2+
3+
[中文文档](/solution/2200-2299/2282.Number%20of%20People%20That%20Can%20Be%20Seen%20in%20a%20Grid/README.md)
4+
5+
## Description
6+
7+
<p>You are given an <code>m x n</code> <strong>0-indexed</strong> 2D array of positive integers <code>heights</code> where <code>heights[i][j]</code> is the height of the person standing at position <code>(i, j)</code>.</p>
8+
9+
<p>A person standing at position <code>(row<sub>1</sub>, col<sub>1</sub>)</code> can see a person standing at position <code>(row<sub>2</sub>, col<sub>2</sub>)</code> if:</p>
10+
11+
<ul>
12+
<li>The person at <code>(row<sub>2</sub>, col<sub>2</sub>)</code> is to the right <strong>or</strong> below the person at <code>(row<sub>1</sub>, col<sub>1</sub>)</code>. More formally, this means that either <code>row<sub>1</sub> == row<sub>2</sub></code> and <code>col<sub>1</sub> &lt; col<sub>2</sub></code> <strong>or</strong> <code>row<sub>1</sub> &lt; row<sub>2</sub></code> and <code>col<sub>1</sub> == col<sub>2</sub></code>.</li>
13+
<li>Everyone in between them is shorter than <strong>both</strong> of them.</li>
14+
</ul>
15+
16+
<p>Return<em> an </em><code>m x n</code><em> 2D array of integers </em><code>answer</code><em> where </em><code>answer[i][j]</code><em> is the number of people that the person at position </em><code>(i, j)</code><em> can see.</em></p>
17+
18+
<p>&nbsp;</p>
19+
<p><strong>Example 1:</strong></p>
20+
<img src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2200-2299/2282.Number%20of%20People%20That%20Can%20Be%20Seen%20in%20a%20Grid/images/image-20220524180458-1.png" style="width: 700px; height: 164px;" />
21+
<pre>
22+
<strong>Input:</strong> heights = [[3,1,4,2,5]]
23+
<strong>Output:</strong> [[2,1,2,1,0]]
24+
<strong>Explanation:</strong>
25+
- The person at (0, 0) can see the people at (0, 1) and (0, 2).
26+
Note that he cannot see the person at (0, 4) because the person at (0, 2) is taller than him.
27+
- The person at (0, 1) can see the person at (0, 2).
28+
- The person at (0, 2) can see the people at (0, 3) and (0, 4).
29+
- The person at (0, 3) can see the person at (0, 4).
30+
- The person at (0, 4) cannot see anybody.
31+
</pre>
32+
33+
<p><strong>Example 2:</strong></p>
34+
<img src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2200-2299/2282.Number%20of%20People%20That%20Can%20Be%20Seen%20in%20a%20Grid/images/image-20220523113533-2.png" style="width: 400px; height: 249px;" />
35+
<pre>
36+
<strong>Input:</strong> heights = [[5,1],[3,1],[4,1]]
37+
<strong>Output:</strong> [[3,1],[2,1],[1,0]]
38+
<strong>Explanation:</strong>
39+
- The person at (0, 0) can see the people at (0, 1), (1, 0) and (2, 0).
40+
- The person at (0, 1) can see the person at (1, 1).
41+
- The person at (1, 0) can see the people at (1, 1) and (2, 0).
42+
- The person at (1, 1) can see the person at (2, 1).
43+
- The person at (2, 0) can see the person at (2, 1).
44+
- The person at (2, 1) cannot see anybody.
45+
</pre>
46+
47+
<p>&nbsp;</p>
48+
<p><strong>Constraints:</strong></p>
49+
50+
<ul>
51+
<li><code>1 &lt;= heights.length &lt;= 400</code></li>
52+
<li><code>1 &lt;= heights[i].length &lt;= 400</code></li>
53+
<li><code>1 &lt;= heights[i][j] &lt;= 10<sup>5</sup></code></li>
54+
</ul>
55+
56+
## Solutions
57+
58+
<!-- tabs:start -->
59+
60+
### **Python3**
61+
62+
```python
63+
64+
```
65+
66+
### **Java**
67+
68+
```java
69+
70+
```
71+
72+
### **TypeScript**
73+
74+
```ts
75+
76+
```
77+
78+
### **...**
79+
80+
```
81+
82+
```
83+
84+
<!-- tabs:end -->
20.3 KB
Loading
10.8 KB
Loading
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# [2283. 判断一个数的数字计数是否等于数位的值](https://leetcode.cn/problems/check-if-number-has-equal-digit-count-and-digit-value)
2+
3+
[English Version](/solution/2200-2299/2283.Check%20if%20Number%20Has%20Equal%20Digit%20Count%20and%20Digit%20Value/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个下标从 <strong>0</strong>&nbsp;开始长度为 <code>n</code>&nbsp;的字符串&nbsp;<code>num</code>&nbsp;,它只包含数字。</p>
10+
11+
<p>如果对于 <strong>每个</strong><em>&nbsp;</em><code>0 &lt;= i &lt; n</code>&nbsp;的下标&nbsp;<code>i</code>&nbsp;,都满足数位<em>&nbsp;</em><code>i</code>&nbsp;在 <code>num</code>&nbsp;中出现了&nbsp;<code>num[i]</code>次,那么请你返回&nbsp;<code>true</code>&nbsp;,否则返回&nbsp;<code>false</code>&nbsp;。</p>
12+
13+
<p>&nbsp;</p>
14+
15+
<p><strong>示例 1:</strong></p>
16+
17+
<pre><b>输入:</b>num = "1210"
18+
<b>输出:</b>true
19+
<strong>解释:</strong>
20+
num[0] = '1' 。数字 0 在 num 中出现了一次。
21+
num[1] = '2' 。数字 1 在 num 中出现了两次。
22+
num[2] = '1' 。数字 2 在 num 中出现了一次。
23+
num[3] = '0' 。数字 3 在 num 中出现了零次。
24+
"1210" 满足题目要求条件,所以返回 true 。
25+
</pre>
26+
27+
<p><strong>示例 2:</strong></p>
28+
29+
<pre><b>输入:</b>num = "030"
30+
<b>输出:</b>false
31+
<strong>解释:</strong>
32+
num[0] = '0' 。数字 0 应该出现 0 次,但是在 num 中出现了一次。
33+
num[1] = '3' 。数字 1 应该出现 3 次,但是在 num 中出现了零次。
34+
num[2] = '0' 。数字 2 在 num 中出现了 0 次。
35+
下标 0 和 1 都违反了题目要求,所以返回 false 。
36+
</pre>
37+
38+
<p>&nbsp;</p>
39+
40+
<p><strong>提示:</strong></p>
41+
42+
<ul>
43+
<li><code>n == num.length</code></li>
44+
<li><code>1 &lt;= n &lt;= 10</code></li>
45+
<li><code>num</code>&nbsp;只包含数字。</li>
46+
</ul>
47+
48+
49+
## 解法
50+
51+
<!-- 这里可写通用的实现逻辑 -->
52+
53+
<!-- tabs:start -->
54+
55+
### **Python3**
56+
57+
<!-- 这里可写当前语言的特殊实现逻辑 -->
58+
59+
```python
60+
61+
```
62+
63+
### **Java**
64+
65+
<!-- 这里可写当前语言的特殊实现逻辑 -->
66+
67+
```java
68+
69+
```
70+
71+
### **TypeScript**
72+
73+
```ts
74+
75+
```
76+
77+
### **...**
78+
79+
```
80+
81+
```
82+
83+
<!-- tabs:end -->
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# [2283. Check if Number Has Equal Digit Count and Digit Value](https://leetcode.com/problems/check-if-number-has-equal-digit-count-and-digit-value)
2+
3+
[中文文档](/solution/2200-2299/2283.Check%20if%20Number%20Has%20Equal%20Digit%20Count%20and%20Digit%20Value/README.md)
4+
5+
## Description
6+
7+
<p>You are given a <strong>0-indexed</strong> string <code>num</code> of length <code>n</code> consisting of digits.</p>
8+
9+
<p>Return <code>true</code> <em>if for <strong>every</strong> index </em><code>i</code><em> in the range </em><code>0 &lt;= i &lt; n</code><em>, the digit </em><code>i</code><em> occurs </em><code>num[i]</code><em> times in </em><code>num</code><em>, otherwise return </em><code>false</code>.</p>
10+
11+
<p>&nbsp;</p>
12+
<p><strong>Example 1:</strong></p>
13+
14+
<pre>
15+
<strong>Input:</strong> num = &quot;1210&quot;
16+
<strong>Output:</strong> true
17+
<strong>Explanation:</strong>
18+
num[0] = &#39;1&#39;. The digit 0 occurs once in num.
19+
num[1] = &#39;2&#39;. The digit 1 occurs twice in num.
20+
num[2] = &#39;1&#39;. The digit 2 occurs once in num.
21+
num[3] = &#39;0&#39;. The digit 3 occurs zero times in num.
22+
The condition holds true for every index in &quot;1210&quot;, so return true.
23+
</pre>
24+
25+
<p><strong>Example 2:</strong></p>
26+
27+
<pre>
28+
<strong>Input:</strong> num = &quot;030&quot;
29+
<strong>Output:</strong> false
30+
<strong>Explanation:</strong>
31+
num[0] = &#39;0&#39;. The digit 0 should occur zero times, but actually occurs twice in num.
32+
num[1] = &#39;3&#39;. The digit 1 should occur three times, but actually occurs zero times in num.
33+
num[2] = &#39;0&#39;. The digit 2 occurs zero times in num.
34+
The indices 0 and 1 both violate the condition, so return false.
35+
</pre>
36+
37+
<p>&nbsp;</p>
38+
<p><strong>Constraints:</strong></p>
39+
40+
<ul>
41+
<li><code>n == num.length</code></li>
42+
<li><code>1 &lt;= n &lt;= 10</code></li>
43+
<li><code>num</code> consists of digits.</li>
44+
</ul>
45+
46+
47+
## Solutions
48+
49+
<!-- tabs:start -->
50+
51+
### **Python3**
52+
53+
```python
54+
55+
```
56+
57+
### **Java**
58+
59+
```java
60+
61+
```
62+
63+
### **TypeScript**
64+
65+
```ts
66+
67+
```
68+
69+
### **...**
70+
71+
```
72+
73+
```
74+
75+
<!-- tabs:end -->

0 commit comments

Comments
 (0)