Skip to content

Commit 6d6efeb

Browse files
authored
chore: update lc problems (#1806)
1 parent d72d9e4 commit 6d6efeb

File tree

19 files changed

+825
-36
lines changed

19 files changed

+825
-36
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# [2899. 上一个遍历的整数](https://leetcode.cn/problems/last-visited-integers)
2+
3+
[English Version](/solution/2800-2899/2899.Last%20Visited%20Integers/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个下标从 <strong>0</strong>&nbsp;开始的字符串数组&nbsp;<code>words</code>&nbsp;,其中&nbsp;<code>words[i]</code>&nbsp;要么是一个字符串形式的正整数,要么是字符串&nbsp;<code>"prev"</code>&nbsp;。</p>
10+
11+
<p>我们从数组的开头开始遍历,对于 <code>words</code>&nbsp;中的每个&nbsp;<code>"prev"</code>&nbsp;字符串,找到 <code>words</code>&nbsp;中的 <strong>上一个遍历的整数</strong>&nbsp;,定义如下:</p>
12+
13+
<ul>
14+
<li><code>k</code>&nbsp;表示到当前位置为止的连续&nbsp;<code>"prev"</code>&nbsp;字符串数目(包含当前字符串),令下标从&nbsp;<strong>0</strong>&nbsp;开始的&nbsp;<strong>整数</strong> 数组&nbsp;<code>nums</code>&nbsp;表示目前为止遍历过的所有整数,同时用&nbsp;<code>nums_reverse</code>&nbsp;表示&nbsp;<code>nums</code>&nbsp;反转得到的数组,那么当前 <code>"prev"</code>&nbsp;对应的 <strong>上一个遍历的整数</strong>&nbsp;是&nbsp;<code>nums_reverse</code>&nbsp;数组中下标为 <code>(k - 1)</code>&nbsp;的整数。</li>
15+
<li>如果&nbsp;<code>k</code>&nbsp;比目前为止遍历过的整数数目 <strong>更多</strong>&nbsp;,那么上一个遍历的整数为&nbsp;<code>-1</code>&nbsp;。</li>
16+
</ul>
17+
18+
<p>请你返回一个整数数组,包含所有上一个遍历的整数。</p>
19+
20+
<p>&nbsp;</p>
21+
22+
<p><strong class="example">示例 1:</strong></p>
23+
24+
<pre>
25+
<b>输入:</b><code>words</code> = ["1","2","prev","prev","prev"]
26+
<b>输出:</b>[2,1,-1]
27+
<b>解释:</b>
28+
对于下标为 2 处的 "prev" ,上一个遍历的整数是 2 ,因为连续 "prev" 数目为 1 ,同时在数组 reverse_nums 中,第一个元素是 2 。
29+
对于下标为 3 处的 "prev" ,上一个遍历的整数是 1 ,因为连续 "prev" 数目为 2 ,同时在数组 reverse_nums 中,第二个元素是 1 。
30+
对于下标为 4 处的 "prev" ,上一个遍历的整数是 -1 ,因为连续 "prev" 数目为 3 ,但总共只遍历过 2 个整数。
31+
</pre>
32+
33+
<p><strong class="example">示例 2:</strong></p>
34+
35+
<pre>
36+
<b>输入:</b><code>words</code> = ["1","prev","2","prev","prev"]
37+
<b>输出:</b>[1,2,1]
38+
<strong>解释:</strong>
39+
对于下标为 1 处的 "prev" ,上一个遍历的整数是 1 。
40+
对于下标为 3 处的 "prev" ,上一个遍历的整数是 2 。
41+
对于下标为 4 处的 "prev" ,上一个遍历的整数是 1 ,因为连续 "prev"<strong>&nbsp;</strong>数目为 2 ,同时在数组 reverse_nums 中,第二个元素是 1 。
42+
</pre>
43+
44+
<p>&nbsp;</p>
45+
46+
<p><strong>提示:</strong></p>
47+
48+
<ul>
49+
<li><code>1 &lt;= words.length &lt;= 100</code></li>
50+
<li><code>words[i] == "prev"</code>&nbsp;或&nbsp;<code>1 &lt;= int(words[i]) &lt;= 100</code></li>
51+
</ul>
52+
53+
## 解法
54+
55+
<!-- 这里可写通用的实现逻辑 -->
56+
57+
<!-- tabs:start -->
58+
59+
### **Python3**
60+
61+
<!-- 这里可写当前语言的特殊实现逻辑 -->
62+
63+
```python
64+
65+
```
66+
67+
### **Java**
68+
69+
<!-- 这里可写当前语言的特殊实现逻辑 -->
70+
71+
```java
72+
73+
```
74+
75+
### **C++**
76+
77+
```cpp
78+
79+
```
80+
81+
### **Go**
82+
83+
```go
84+
85+
```
86+
87+
### **...**
88+
89+
```
90+
91+
```
92+
93+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# [2899. Last Visited Integers](https://leetcode.com/problems/last-visited-integers)
2+
3+
[中文文档](/solution/2800-2899/2899.Last%20Visited%20Integers/README.md)
4+
5+
## Description
6+
7+
<p>Given a <strong>0-indexed</strong> array of strings <code>words</code> where <code>words[i]</code> is either a positive integer represented as a string or the string <code>&quot;prev&quot;</code>.</p>
8+
9+
<p>Start iterating from the beginning of the array; for every <code>&quot;prev&quot;</code> string seen in <code>words</code>, find the <strong>last visited integer</strong> in <code>words</code> which is defined as follows:</p>
10+
11+
<ul>
12+
<li>Let <code>k</code> be the number of consecutive <code>&quot;prev&quot;</code> strings seen so far (containing the current string). Let <code>nums</code> be the <strong>0-indexed </strong>array of <strong>integers</strong> seen so far and <code>nums_reverse</code> be the reverse of <code>nums</code>, then the integer at <code>(k - 1)<sup>th</sup></code> index of <code>nums_reverse</code> will be the <strong>last visited integer</strong> for this <code>&quot;prev&quot;</code>.</li>
13+
<li>If <code>k</code> is <strong>greater</strong> than the total visited integers, then the last visited integer will be <code>-1</code>.</li>
14+
</ul>
15+
16+
<p>Return <em>an integer array containing the last visited integers.</em></p>
17+
18+
<p>&nbsp;</p>
19+
<p><strong class="example">Example 1:</strong></p>
20+
21+
<pre>
22+
<strong>Input:</strong> words = [&quot;1&quot;,&quot;2&quot;,&quot;prev&quot;,&quot;prev&quot;,&quot;prev&quot;]
23+
<strong>Output:</strong> [2,1,-1]
24+
<strong>Explanation:</strong>
25+
For &quot;prev&quot; at index = 2, last visited integer will be 2 as here the number of consecutive &quot;prev&quot; strings is 1, and in the array reverse_nums, 2 will be the first element.
26+
For &quot;prev&quot; at index = 3, last visited integer will be 1 as there are a total of two consecutive &quot;prev&quot; strings including this &quot;prev&quot; which are visited, and 1 is the second last visited integer.
27+
For &quot;prev&quot; at index = 4, last visited integer will be -1 as there are a total of three consecutive &quot;prev&quot; strings including this &quot;prev&quot; which are visited, but the total number of integers visited is two.
28+
</pre>
29+
30+
<p><strong class="example">Example 2:</strong></p>
31+
32+
<pre>
33+
<strong>Input:</strong> words = [&quot;1&quot;,&quot;prev&quot;,&quot;2&quot;,&quot;prev&quot;,&quot;prev&quot;]
34+
<strong>Output:</strong> [1,2,1]
35+
<strong>Explanation:</strong>
36+
For &quot;prev&quot; at index = 1, last visited integer will be 1.
37+
For &quot;prev&quot; at index = 3, last visited integer will be 2.
38+
For &quot;prev&quot; at index = 4, last visited integer will be 1 as there are a total of two consecutive &quot;prev&quot; strings including this &quot;prev&quot; which are visited, and 1 is the second last visited integer.
39+
</pre>
40+
41+
<p>&nbsp;</p>
42+
<p><strong>Constraints:</strong></p>
43+
44+
<ul>
45+
<li><code>1 &lt;= words.length &lt;= 100</code></li>
46+
<li><code>words[i] == &quot;prev&quot;</code> or <code>1 &lt;= int(words[i]) &lt;= 100</code></li>
47+
</ul>
48+
49+
## Solutions
50+
51+
<!-- tabs:start -->
52+
53+
### **Python3**
54+
55+
```python
56+
57+
```
58+
59+
### **Java**
60+
61+
```java
62+
63+
```
64+
65+
### **C++**
66+
67+
```cpp
68+
69+
```
70+
71+
### **Go**
72+
73+
```go
74+
75+
```
76+
77+
### **...**
78+
79+
```
80+
81+
```
82+
83+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# [2900. 最长相邻不相等子序列 I](https://leetcode.cn/problems/longest-unequal-adjacent-groups-subsequence-i)
2+
3+
[English Version](/solution/2900-2999/2900.Longest%20Unequal%20Adjacent%20Groups%20Subsequence%20I/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个整数&nbsp;<code>n</code>&nbsp;和一个下标从&nbsp;<strong>0</strong>&nbsp;开始的字符串数组&nbsp;<code>words</code>&nbsp;,和一个下标从 <strong>0</strong>&nbsp;开始的 <strong>二进制</strong>&nbsp;数组&nbsp;<code>groups</code>&nbsp;,两个数组长度都是&nbsp;<code>n</code>&nbsp;。</p>
10+
11+
<p>你需要从下标&nbsp;<code>[0, 1, ..., n - 1]</code>&nbsp;中选出一个&nbsp;<strong>最长子序列</strong>&nbsp;,将这个子序列记作长度为 <code>k</code> 的&nbsp;<code>[i<sub>0</sub>, i<sub>1</sub>, ..., i<sub>k - 1</sub>]</code>&nbsp;,对于所有满足&nbsp;<code>0 &lt; j + 1 &lt; k</code>&nbsp;&nbsp;<code>j</code>&nbsp;都有&nbsp;<code>groups[i<sub>j</sub>] != groups[i<sub>j + 1</sub>]</code>&nbsp;。</p>
12+
13+
<p>请你返回一个字符串数组,它是下标子序列&nbsp;<strong>依次</strong>&nbsp;对应&nbsp;<code>words</code>&nbsp;数组中的字符串连接形成的字符串数组。如果有多个答案,返回任意一个。</p>
14+
15+
<p><strong>子序列</strong>&nbsp;指的是从原数组中删掉一些(也可能一个也不删掉)元素,剩余元素不改变相对位置得到的新的数组。</p>
16+
17+
<p><b>注意:</b><code>words</code>&nbsp;中的字符串长度可能 <strong>不相等</strong>&nbsp;。</p>
18+
19+
<p>&nbsp;</p>
20+
21+
<p><strong class="example">示例 1:</strong></p>
22+
23+
<pre>
24+
<b>输入:</b>n = 3, words = ["e","a","b"], groups = [0,0,1]
25+
<b>输出:</b>["e","b"]
26+
<strong>解释:</strong>一个可行的子序列是 [0,2] ,因为 groups[0] != groups[2] 。
27+
所以一个可行的答案是 [words[0],words[2]] = ["e","b"] 。
28+
另一个可行的子序列是 [1,2] ,因为 groups[1] != groups[2] 。
29+
得到答案为 [words[1],words[2]] = ["a","b"] 。
30+
这也是一个可行的答案。
31+
符合题意的最长子序列的长度为 2 。</pre>
32+
33+
<p><strong class="example">示例 2:</strong></p>
34+
35+
<pre>
36+
<b>输入:</b>n = 4, words = ["a","b","c","d"], groups = [1,0,1,1]
37+
<b>输出:</b>["a","b","c"]
38+
<b>解释:</b>一个可行的子序列为 [0,1,2] 因为 groups[0] != groups[1] 且 groups[1] != groups[2] 。
39+
所以一个可行的答案是 [words[0],words[1],words[2]] = ["a","b","c"] 。
40+
另一个可行的子序列为 [0,1,3] 因为 groups[0] != groups[1] 且 groups[1] != groups[3] 。
41+
得到答案为 [words[0],words[1],words[3]] = ["a","b","d"] 。
42+
这也是一个可行的答案。
43+
符合题意的最长子序列的长度为 3 。</pre>
44+
45+
<p>&nbsp;</p>
46+
47+
<p><strong>提示:</strong></p>
48+
49+
<ul>
50+
<li><code>1 &lt;= n == words.length == groups.length &lt;= 100</code></li>
51+
<li><code>1 &lt;= words[i].length &lt;= 10</code></li>
52+
<li><code>0 &lt;= groups[i] &lt; 2</code></li>
53+
<li><code>words</code>&nbsp;中的字符串 <strong>互不相同</strong>&nbsp;。</li>
54+
<li><code>words[i]</code>&nbsp;只包含小写英文字母。</li>
55+
</ul>
56+
57+
## 解法
58+
59+
<!-- 这里可写通用的实现逻辑 -->
60+
61+
<!-- tabs:start -->
62+
63+
### **Python3**
64+
65+
<!-- 这里可写当前语言的特殊实现逻辑 -->
66+
67+
```python
68+
69+
```
70+
71+
### **Java**
72+
73+
<!-- 这里可写当前语言的特殊实现逻辑 -->
74+
75+
```java
76+
77+
```
78+
79+
### **C++**
80+
81+
```cpp
82+
83+
```
84+
85+
### **Go**
86+
87+
```go
88+
89+
```
90+
91+
### **...**
92+
93+
```
94+
95+
```
96+
97+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# [2900. Longest Unequal Adjacent Groups Subsequence I](https://leetcode.com/problems/longest-unequal-adjacent-groups-subsequence-i)
2+
3+
[中文文档](/solution/2900-2999/2900.Longest%20Unequal%20Adjacent%20Groups%20Subsequence%20I/README.md)
4+
5+
## Description
6+
7+
<p>You are given an integer <code>n</code>, a <strong>0-indexed</strong> string array <code>words</code>, and a <strong>0-indexed</strong> <strong>binary</strong> array <code>groups</code>, both arrays having length <code>n</code>.</p>
8+
9+
<p>You need to select the <strong>longest</strong> <strong>subsequence</strong> from an array of indices <code>[0, 1, ..., n - 1]</code>, such that for the subsequence denoted as <code>[i<sub>0</sub>, i<sub>1</sub>, ..., i<sub>k - 1</sub>]</code> having length <code>k</code>, <code>groups[i<sub>j</sub>] != groups[i<sub>j + 1</sub>]</code>, for each <code>j</code> where <code>0 &lt; j + 1 &lt; k</code>.</p>
10+
11+
<p>Return <em>a string array containing the words corresponding to the indices <strong>(in order)</strong> in the selected subsequence</em>. If there are multiple answers, return<em> any of them</em>.</p>
12+
13+
<p>A <strong>subsequence</strong> of an array is a new array that is formed from the original array by deleting some (possibly none) of the elements without disturbing the relative positions of the remaining elements.</p>
14+
15+
<p><strong>Note:</strong> strings in <code>words</code> may be <strong>unequal</strong> in length.</p>
16+
17+
<p>&nbsp;</p>
18+
<p><strong class="example">Example 1:</strong></p>
19+
20+
<pre>
21+
<strong>Input:</strong> n = 3, words = [&quot;e&quot;,&quot;a&quot;,&quot;b&quot;], groups = [0,0,1]
22+
<strong>Output:</strong> [&quot;e&quot;,&quot;b&quot;]
23+
<strong>Explanation: </strong>A subsequence that can be selected is [0,2] because groups[0] != groups[2].
24+
So, a valid answer is [words[0],words[2]] = [&quot;e&quot;,&quot;b&quot;].
25+
Another subsequence that can be selected is [1,2] because groups[1] != groups[2].
26+
This results in [words[1],words[2]] = [&quot;a&quot;,&quot;b&quot;].
27+
It is also a valid answer.
28+
It can be shown that the length of the longest subsequence of indices that satisfies the condition is 2.</pre>
29+
30+
<p><strong class="example">Example 2:</strong></p>
31+
32+
<pre>
33+
<strong>Input:</strong> n = 4, words = [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;,&quot;d&quot;], groups = [1,0,1,1]
34+
<strong>Output:</strong> [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]
35+
<strong>Explanation:</strong> A subsequence that can be selected is [0,1,2] because groups[0] != groups[1] and groups[1] != groups[2].
36+
So, a valid answer is [words[0],words[1],words[2]] = [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;].
37+
Another subsequence that can be selected is [0,1,3] because groups[0] != groups[1] and groups[1] != groups[3].
38+
This results in [words[0],words[1],words[3]] = [&quot;a&quot;,&quot;b&quot;,&quot;d&quot;].
39+
It is also a valid answer.
40+
It can be shown that the length of the longest subsequence of indices that satisfies the condition is 3.
41+
</pre>
42+
43+
<p>&nbsp;</p>
44+
<p><strong>Constraints:</strong></p>
45+
46+
<ul>
47+
<li><code>1 &lt;= n == words.length == groups.length &lt;= 100</code></li>
48+
<li><code>1 &lt;= words[i].length &lt;= 10</code></li>
49+
<li><code>0 &lt;= groups[i] &lt; 2</code></li>
50+
<li><code>words</code> consists of <strong>distinct</strong> strings.</li>
51+
<li><code>words[i]</code> consists of lowercase English letters.</li>
52+
</ul>
53+
54+
## Solutions
55+
56+
<!-- tabs:start -->
57+
58+
### **Python3**
59+
60+
```python
61+
62+
```
63+
64+
### **Java**
65+
66+
```java
67+
68+
```
69+
70+
### **C++**
71+
72+
```cpp
73+
74+
```
75+
76+
### **Go**
77+
78+
```go
79+
80+
```
81+
82+
### **...**
83+
84+
```
85+
86+
```
87+
88+
<!-- tabs:end -->

0 commit comments

Comments
 (0)