Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update lc problems #1806

Merged
merged 1 commit into from
Oct 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions solution/2800-2899/2899.Last Visited Integers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# [2899. 上一个遍历的整数](https://leetcode.cn/problems/last-visited-integers)

[English Version](/solution/2800-2899/2899.Last%20Visited%20Integers/README_EN.md)

## 题目描述

<!-- 这里写题目描述 -->

<p>给你一个下标从 <strong>0</strong>&nbsp;开始的字符串数组&nbsp;<code>words</code>&nbsp;,其中&nbsp;<code>words[i]</code>&nbsp;要么是一个字符串形式的正整数,要么是字符串&nbsp;<code>"prev"</code>&nbsp;。</p>

<p>我们从数组的开头开始遍历,对于 <code>words</code>&nbsp;中的每个&nbsp;<code>"prev"</code>&nbsp;字符串,找到 <code>words</code>&nbsp;中的 <strong>上一个遍历的整数</strong>&nbsp;,定义如下:</p>

<ul>
<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>
<li>如果&nbsp;<code>k</code>&nbsp;比目前为止遍历过的整数数目 <strong>更多</strong>&nbsp;,那么上一个遍历的整数为&nbsp;<code>-1</code>&nbsp;。</li>
</ul>

<p>请你返回一个整数数组,包含所有上一个遍历的整数。</p>

<p>&nbsp;</p>

<p><strong class="example">示例 1:</strong></p>

<pre>
<b>输入:</b><code>words</code> = ["1","2","prev","prev","prev"]
<b>输出:</b>[2,1,-1]
<b>解释:</b>
对于下标为 2 处的 "prev" ,上一个遍历的整数是 2 ,因为连续 "prev" 数目为 1 ,同时在数组 reverse_nums 中,第一个元素是 2 。
对于下标为 3 处的 "prev" ,上一个遍历的整数是 1 ,因为连续 "prev" 数目为 2 ,同时在数组 reverse_nums 中,第二个元素是 1 。
对于下标为 4 处的 "prev" ,上一个遍历的整数是 -1 ,因为连续 "prev" 数目为 3 ,但总共只遍历过 2 个整数。
</pre>

<p><strong class="example">示例 2:</strong></p>

<pre>
<b>输入:</b><code>words</code> = ["1","prev","2","prev","prev"]
<b>输出:</b>[1,2,1]
<strong>解释:</strong>
对于下标为 1 处的 "prev" ,上一个遍历的整数是 1 。
对于下标为 3 处的 "prev" ,上一个遍历的整数是 2 。
对于下标为 4 处的 "prev" ,上一个遍历的整数是 1 ,因为连续 "prev"<strong>&nbsp;</strong>数目为 2 ,同时在数组 reverse_nums 中,第二个元素是 1 。
</pre>

<p>&nbsp;</p>

<p><strong>提示:</strong></p>

<ul>
<li><code>1 &lt;= words.length &lt;= 100</code></li>
<li><code>words[i] == "prev"</code>&nbsp;或&nbsp;<code>1 &lt;= int(words[i]) &lt;= 100</code></li>
</ul>

## 解法

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

<!-- tabs:start -->

### **Python3**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```python

```

### **Java**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```java

```

### **C++**

```cpp

```

### **Go**

```go

```

### **...**

```

```

<!-- tabs:end -->
83 changes: 83 additions & 0 deletions solution/2800-2899/2899.Last Visited Integers/README_EN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# [2899. Last Visited Integers](https://leetcode.com/problems/last-visited-integers)

[中文文档](/solution/2800-2899/2899.Last%20Visited%20Integers/README.md)

## Description

<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>

<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>

<ul>
<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>
<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>
</ul>

<p>Return <em>an integer array containing the last visited integers.</em></p>

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

<pre>
<strong>Input:</strong> words = [&quot;1&quot;,&quot;2&quot;,&quot;prev&quot;,&quot;prev&quot;,&quot;prev&quot;]
<strong>Output:</strong> [2,1,-1]
<strong>Explanation:</strong>
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.
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.
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.
</pre>

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

<pre>
<strong>Input:</strong> words = [&quot;1&quot;,&quot;prev&quot;,&quot;2&quot;,&quot;prev&quot;,&quot;prev&quot;]
<strong>Output:</strong> [1,2,1]
<strong>Explanation:</strong>
For &quot;prev&quot; at index = 1, last visited integer will be 1.
For &quot;prev&quot; at index = 3, last visited integer will be 2.
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.
</pre>

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

<ul>
<li><code>1 &lt;= words.length &lt;= 100</code></li>
<li><code>words[i] == &quot;prev&quot;</code> or <code>1 &lt;= int(words[i]) &lt;= 100</code></li>
</ul>

## Solutions

<!-- tabs:start -->

### **Python3**

```python

```

### **Java**

```java

```

### **C++**

```cpp

```

### **Go**

```go

```

### **...**

```

```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# [2900. 最长相邻不相等子序列 I](https://leetcode.cn/problems/longest-unequal-adjacent-groups-subsequence-i)

[English Version](/solution/2900-2999/2900.Longest%20Unequal%20Adjacent%20Groups%20Subsequence%20I/README_EN.md)

## 题目描述

<!-- 这里写题目描述 -->

<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>

<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>

<p>请你返回一个字符串数组,它是下标子序列&nbsp;<strong>依次</strong>&nbsp;对应&nbsp;<code>words</code>&nbsp;数组中的字符串连接形成的字符串数组。如果有多个答案,返回任意一个。</p>

<p><strong>子序列</strong>&nbsp;指的是从原数组中删掉一些(也可能一个也不删掉)元素,剩余元素不改变相对位置得到的新的数组。</p>

<p><b>注意:</b><code>words</code>&nbsp;中的字符串长度可能 <strong>不相等</strong>&nbsp;。</p>

<p>&nbsp;</p>

<p><strong class="example">示例 1:</strong></p>

<pre>
<b>输入:</b>n = 3, words = ["e","a","b"], groups = [0,0,1]
<b>输出:</b>["e","b"]
<strong>解释:</strong>一个可行的子序列是 [0,2] ,因为 groups[0] != groups[2] 。
所以一个可行的答案是 [words[0],words[2]] = ["e","b"] 。
另一个可行的子序列是 [1,2] ,因为 groups[1] != groups[2] 。
得到答案为 [words[1],words[2]] = ["a","b"] 。
这也是一个可行的答案。
符合题意的最长子序列的长度为 2 。</pre>

<p><strong class="example">示例 2:</strong></p>

<pre>
<b>输入:</b>n = 4, words = ["a","b","c","d"], groups = [1,0,1,1]
<b>输出:</b>["a","b","c"]
<b>解释:</b>一个可行的子序列为 [0,1,2] 因为 groups[0] != groups[1] 且 groups[1] != groups[2] 。
所以一个可行的答案是 [words[0],words[1],words[2]] = ["a","b","c"] 。
另一个可行的子序列为 [0,1,3] 因为 groups[0] != groups[1] 且 groups[1] != groups[3] 。
得到答案为 [words[0],words[1],words[3]] = ["a","b","d"] 。
这也是一个可行的答案。
符合题意的最长子序列的长度为 3 。</pre>

<p>&nbsp;</p>

<p><strong>提示:</strong></p>

<ul>
<li><code>1 &lt;= n == words.length == groups.length &lt;= 100</code></li>
<li><code>1 &lt;= words[i].length &lt;= 10</code></li>
<li><code>0 &lt;= groups[i] &lt; 2</code></li>
<li><code>words</code>&nbsp;中的字符串 <strong>互不相同</strong>&nbsp;。</li>
<li><code>words[i]</code>&nbsp;只包含小写英文字母。</li>
</ul>

## 解法

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

<!-- tabs:start -->

### **Python3**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```python

```

### **Java**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```java

```

### **C++**

```cpp

```

### **Go**

```go

```

### **...**

```

```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# [2900. Longest Unequal Adjacent Groups Subsequence I](https://leetcode.com/problems/longest-unequal-adjacent-groups-subsequence-i)

[中文文档](/solution/2900-2999/2900.Longest%20Unequal%20Adjacent%20Groups%20Subsequence%20I/README.md)

## Description

<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>

<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>

<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>

<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>

<p><strong>Note:</strong> strings in <code>words</code> may be <strong>unequal</strong> in length.</p>

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

<pre>
<strong>Input:</strong> n = 3, words = [&quot;e&quot;,&quot;a&quot;,&quot;b&quot;], groups = [0,0,1]
<strong>Output:</strong> [&quot;e&quot;,&quot;b&quot;]
<strong>Explanation: </strong>A subsequence that can be selected is [0,2] because groups[0] != groups[2].
So, a valid answer is [words[0],words[2]] = [&quot;e&quot;,&quot;b&quot;].
Another subsequence that can be selected is [1,2] because groups[1] != groups[2].
This results in [words[1],words[2]] = [&quot;a&quot;,&quot;b&quot;].
It is also a valid answer.
It can be shown that the length of the longest subsequence of indices that satisfies the condition is 2.</pre>

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

<pre>
<strong>Input:</strong> n = 4, words = [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;,&quot;d&quot;], groups = [1,0,1,1]
<strong>Output:</strong> [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]
<strong>Explanation:</strong> A subsequence that can be selected is [0,1,2] because groups[0] != groups[1] and groups[1] != groups[2].
So, a valid answer is [words[0],words[1],words[2]] = [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;].
Another subsequence that can be selected is [0,1,3] because groups[0] != groups[1] and groups[1] != groups[3].
This results in [words[0],words[1],words[3]] = [&quot;a&quot;,&quot;b&quot;,&quot;d&quot;].
It is also a valid answer.
It can be shown that the length of the longest subsequence of indices that satisfies the condition is 3.
</pre>

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

<ul>
<li><code>1 &lt;= n == words.length == groups.length &lt;= 100</code></li>
<li><code>1 &lt;= words[i].length &lt;= 10</code></li>
<li><code>0 &lt;= groups[i] &lt; 2</code></li>
<li><code>words</code> consists of <strong>distinct</strong> strings.</li>
<li><code>words[i]</code> consists of lowercase English letters.</li>
</ul>

## Solutions

<!-- tabs:start -->

### **Python3**

```python

```

### **Java**

```java

```

### **C++**

```cpp

```

### **Go**

```go

```

### **...**

```

```

<!-- tabs:end -->
Loading