Skip to content

Commit b5d5ff8

Browse files
committed
feat: add solutions to lc problems: No.2535~2538
* No.2535.Difference Between Element Sum and Digit Sum of an Array * No.2536.Increment Submatrices by One * No.2537.Count the Number of Good Subarrays * No.2538.Difference Between Maximum and Minimum Price Sum
1 parent d6e8362 commit b5d5ff8

File tree

41 files changed

+2060
-19
lines changed

Some content is hidden

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

41 files changed

+2060
-19
lines changed

solution/1400-1499/1443.Minimum Time to Collect All Apples in a Tree/README_EN.md

-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
<li><code>edges.length == n - 1</code></li>
4141
<li><code>edges[i].length == 2</code></li>
4242
<li><code>0 &lt;= a<sub>i</sub> &lt; b<sub>i</sub> &lt;= n - 1</code></li>
43-
<li><code>from<sub>i</sub> &lt; to<sub>i</sub></code></li>
4443
<li><code>hasApple.length == n</code></li>
4544
</ul>
4645

solution/1800-1899/1813.Sentence Similarity III/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757

5858
**方法一:双指针**
5959

60-
我们将两个句子分别用空格分割成单词数组 `words1``words2`,假设 `words1``words2` 的长度分别为 $m$ 和 $n$,不妨设 $m \geq n$。
60+
我们将两个句子按照空格分割成两个单词数组 `words1``words2`,假设 `words1``words2` 的长度分别为 $m$ 和 $n$,不妨设 $m \geq n$。
6161

6262
我们使用双指针 $i$ 和 $j$,初始时 $i = j = 0$。接下来,我们循环判断 `words1[i]` 是否等于 `words2[i]`,是则指针 $i$ 继续右移;然后我们循环判断 `words1[m - 1 - j]` 是否等于 `words2[n - 1 - j]`,是则指针 $j$ 继续右移。
6363

@@ -109,7 +109,7 @@ class Solution {
109109
while (j < n && words1[m - 1 - j].equals(words2[n - 1 - j])) {
110110
++j;
111111
}
112-
return i == n || j == n || i + j >= n;
112+
return i + j >= n;
113113
}
114114
}
115115
```

solution/1800-1899/1813.Sentence Similarity III/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class Solution {
8686
while (j < n && words1[m - 1 - j].equals(words2[n - 1 - j])) {
8787
++j;
8888
}
89-
return i == n || j == n || i + j >= n;
89+
return i + j >= n;
9090
}
9191
}
9292
```

solution/1800-1899/1813.Sentence Similarity III/Solution.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ public boolean areSentencesSimilar(String sentence1, String sentence2) {
1515
while (j < n && words1[m - 1 - j].equals(words2[n - 1 - j])) {
1616
++j;
1717
}
18-
return i == n || j == n || i + j >= n;
18+
return i + j >= n;
1919
}
2020
}

solution/2200-2299/2296.Design a Text Editor/README_EN.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -124,17 +124,17 @@ class TextEditor {
124124
public TextEditor() {
125125

126126
}
127-
127+
128128
public void addText(String text) {
129129
left.append(text);
130130
}
131-
131+
132132
public int deleteText(int k) {
133133
k = Math.min(k, left.length());
134134
left.setLength(left.length() - k);
135135
return k;
136136
}
137-
137+
138138
public String cursorLeft(int k) {
139139
k = Math.min(k, left.length());
140140
for (int i = 0; i < k; ++i) {
@@ -143,7 +143,7 @@ class TextEditor {
143143
}
144144
return left.substring(Math.max(left.length() - 10, 0));
145145
}
146-
146+
147147
public String cursorRight(int k) {
148148
k = Math.min(k, right.length());
149149
for (int i = 0; i < k; ++i) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# [2534. 通过门的时间](https://leetcode.cn/problems/time-taken-to-cross-the-door)
2+
3+
[English Version](/solution/2500-2599/2534.Time%20Taken%20to%20Cross%20the%20Door/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p><code>n</code> 个人,按从 <code>0</code> 到 <code>n - 1</code> 编号。现在有一扇门,每个人只能通过门进入或离开一次,耗时一秒。</p>
10+
11+
<p>给你一个 <strong>非递减顺序</strong> 排列的整数数组 <code>arrival</code> ,数组长度为 <code>n</code> ,其中 <code>arrival[i]</code> 是第 <code>i</code> 个人到达门前的时间。另给你一个长度为 <code>n</code> 的数组 <code>state</code> ,其中 <code>state[i]</code> 是 <code>0</code> 则表示第 <code>i</code> 个人希望进入这扇门,是 <code>1</code> 则表示 TA 想要离开这扇门。</p>
12+
13+
<p>如果 <strong>同时</strong> 有两个或更多人想要使用这扇门,则必须遵循以下规则:</p>
14+
15+
<ul>
16+
<li>如果前一秒 <strong>没有</strong> 使用门,那么想要 <strong>离开</strong> 的人会先离开。</li>
17+
<li>如果前一秒使用门 <strong>进入</strong> ,那么想要 <strong>进入</strong> 的人会先进入。</li>
18+
<li>如果前一秒使用门 <strong>离开</strong> ,那么想要 <strong>离开</strong> 的人会先离开。</li>
19+
<li>如果多个人都想朝同一方向走(都进入或都离开),编号最小的人会先通过门。</li>
20+
</ul>
21+
22+
<p>返回一个长度为 <code>n</code> 的数组<em> </em><code>answer</code><em> </em>,其中<em> </em><code>answer[i]</code><em> </em>是第 <code>i</code> 个人通过门的时刻(秒)。</p>
23+
<strong>注意:</strong>
24+
25+
<ul>
26+
<li>每秒只有一个人可以通过门。</li>
27+
<li>为遵循上述规则,一个人可以在到达门附近后等待,而不通过门进入或离开。</li>
28+
</ul>
29+
30+
<p>&nbsp;</p>
31+
32+
<p><strong>示例 1:</strong></p>
33+
34+
<pre>
35+
<strong>输入:</strong>arrival = [0,1,1,2,4], state = [0,1,0,0,1]
36+
<strong>输出:</strong>[0,3,1,2,4]
37+
<strong>解释:</strong>每秒发生的情况如下:
38+
- t = 0 :第 0 个人是唯一一个想要进入的人,所以 TA 可以直接进入。
39+
- t = 1 :第 1 个人想要离开,第 2 个人想要进入。因为前一秒有人使用门进入,所以第 2 个人先进入。
40+
- t = 2 :第 1 个人还是想要离开,第 3 个人想要进入。因为前一秒有人使用门进入,所以第 3 个人先进入。
41+
- t = 3 :第 1 个人是唯一一个想要离开的人,所以 TA 可以直接离开。
42+
- t = 4 :第 4 个人是唯一一个想要进入的人,所以 TA 可以直接离开。
43+
</pre>
44+
45+
<p><strong>示例 2:</strong></p>
46+
47+
<pre>
48+
<strong>输入:</strong>arrival = [0,0,0], state = [1,0,1]
49+
<strong>输出:</strong>[0,2,1]
50+
<strong>解释:</strong>每秒发生的情况如下:
51+
- t = 0 :第 1 个人想要进入,但是第 0 个人和第 2 个人都想要离开。因为前一秒没有使用门,所以想要离开的人会先离开。又因为第 0 个人的编号更小,所以 TA 先离开。
52+
- t = 1 :第 1 个人想要进入,第 2 个人想要离开。因为前一秒有人使用门离开,所以第 2 个人先离开。
53+
- t = 2 :第 1 个人是唯一一个想要进入的人,所以 TA 可以直接进入。
54+
</pre>
55+
56+
<p>&nbsp;</p>
57+
58+
<p><strong>提示:</strong></p>
59+
60+
<ul>
61+
<li><code>n == arrival.length == state.length</code></li>
62+
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
63+
<li><code>0 &lt;= arrival[i] &lt;= n</code></li>
64+
<li><code>arrival</code> 按 <strong>非递减顺序</strong> 排列</li>
65+
<li><code>state[i]</code> 为 <code>0</code> 或 <code>1</code></li>
66+
</ul>
67+
68+
## 解法
69+
70+
<!-- 这里可写通用的实现逻辑 -->
71+
72+
<!-- tabs:start -->
73+
74+
### **Python3**
75+
76+
<!-- 这里可写当前语言的特殊实现逻辑 -->
77+
78+
```python
79+
80+
```
81+
82+
### **Java**
83+
84+
<!-- 这里可写当前语言的特殊实现逻辑 -->
85+
86+
```java
87+
88+
```
89+
90+
### **C++**
91+
92+
```cpp
93+
94+
```
95+
96+
### **Go**
97+
98+
```go
99+
100+
```
101+
102+
### **...**
103+
104+
```
105+
106+
```
107+
108+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# [2534. Time Taken to Cross the Door](https://leetcode.com/problems/time-taken-to-cross-the-door)
2+
3+
[中文文档](/solution/2500-2599/2534.Time%20Taken%20to%20Cross%20the%20Door/README.md)
4+
5+
## Description
6+
7+
<p>There are <code>n</code> persons numbered from <code>0</code> to <code>n - 1</code> and a door. Each person can enter or exit through the door once, taking one second.</p>
8+
9+
<p>You are given a <strong>non-decreasing</strong> integer array <code>arrival</code> of size <code>n</code>, where <code>arrival[i]</code> is the arrival time of the <code>i<sup>th</sup></code> person at the door. You are also given an array <code>state</code> of size <code>n</code>, where <code>state[i]</code> is <code>0</code> if person <code>i</code> wants to enter through the door or <code>1</code> if they want to exit through the door.</p>
10+
11+
<p>If two or more persons want to use the door at the <strong>same</strong> time, they follow the following rules:</p>
12+
13+
<ul>
14+
<li>If the door was <strong>not</strong> used in the previous second, then the person who wants to <strong>exit</strong> goes first.</li>
15+
<li>If the door was used in the previous second for <strong>entering</strong>, the person who wants to enter goes first.</li>
16+
<li>If the door was used in the previous second for <strong>exiting</strong>, the person who wants to <strong>exit</strong> goes first.</li>
17+
<li>If multiple persons want to go in the same direction, the person with the <strong>smallest</strong> index goes first.</li>
18+
</ul>
19+
20+
<p>Return <em>an array </em><code>answer</code><em> of size </em><code>n</code><em> where </em><code>answer[i]</code><em> is the second at which the </em><code>i<sup>th</sup></code><em> person crosses the door</em>.</p>
21+
22+
<p><strong>Note</strong> that:</p>
23+
24+
<ul>
25+
<li>Only one person can cross the door at each second.</li>
26+
<li>A person may arrive at the door and wait without entering or exiting to follow the mentioned rules.</li>
27+
</ul>
28+
29+
<p>&nbsp;</p>
30+
<p><strong>Example 1:</strong></p>
31+
32+
<pre>
33+
<strong>Input:</strong> arrival = [0,1,1,2,4], state = [0,1,0,0,1]
34+
<strong>Output:</strong> [0,3,1,2,4]
35+
<strong>Explanation:</strong> At each second we have the following:
36+
- At t = 0: Person 0 is the only one who wants to enter, so they just enter through the door.
37+
- At t = 1: Person 1 wants to exit, and person 2 wants to enter. Since the door was used the previous second for entering, person 2 enters.
38+
- At t = 2: Person 1 still wants to exit, and person 3 wants to enter. Since the door was used the previous second for entering, person 3 enters.
39+
- At t = 3: Person 1 is the only one who wants to exit, so they just exit through the door.
40+
- At t = 4: Person 4 is the only one who wants to exit, so they just exit through the door.
41+
</pre>
42+
43+
<p><strong>Example 2:</strong></p>
44+
45+
<pre>
46+
<strong>Input:</strong> arrival = [0,0,0], state = [1,0,1]
47+
<strong>Output:</strong> [0,2,1]
48+
<strong>Explanation:</strong> At each second we have the following:
49+
- At t = 0: Person 1 wants to enter while persons 0 and 2 want to exit. Since the door was not used in the previous second, the persons who want to exit get to go first. Since person 0 has a smaller index, they exit first.
50+
- At t = 1: Person 1 wants to enter, and person 2 wants to exit. Since the door was used in the previous second for exiting, person 2 exits.
51+
- At t = 2: Person 1 is the only one who wants to enter, so they just enter through the door.
52+
</pre>
53+
54+
<p>&nbsp;</p>
55+
<p><strong>Constraints:</strong></p>
56+
57+
<ul>
58+
<li><code>n == arrival.length == state.length</code></li>
59+
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
60+
<li><code>0 &lt;= arrival[i] &lt;= n</code></li>
61+
<li><code>arrival</code> is sorted in <strong>non-decreasing</strong> order.</li>
62+
<li><code>state[i]</code> is either <code>0</code> or <code>1</code>.</li>
63+
</ul>
64+
65+
## Solutions
66+
67+
<!-- tabs:start -->
68+
69+
### **Python3**
70+
71+
```python
72+
73+
```
74+
75+
### **Java**
76+
77+
```java
78+
79+
```
80+
81+
### **C++**
82+
83+
```cpp
84+
85+
```
86+
87+
### **Go**
88+
89+
```go
90+
91+
```
92+
93+
### **...**
94+
95+
```
96+
97+
```
98+
99+
<!-- tabs:end -->

0 commit comments

Comments
 (0)