feat: add new lc problems

This commit is contained in:
yanglbme 2022-09-25 19:00:12 +08:00
parent cc645a0188
commit fac4075560
29 changed files with 772 additions and 36 deletions

View File

@ -4,18 +4,28 @@
## Description
<p>Given a list of <b>unique</b> words, return all the pairs of the&nbsp;<b><i>distinct</i></b> indices <code>(i, j)</code> in the given list, so that the concatenation of the two words&nbsp;<code>words[i] + words[j]</code> is a palindrome.</p>
<p>You are given a <strong>0-indexed</strong> array of <strong>unique</strong> strings <code>words</code>.</p>
<p>A <strong>palindrome pair</strong> is a pair of integers <code>(i, j)</code> such that:</p>
<ul>
<li><code>0 &lt;= i, j &lt; word.length</code>,</li>
<li><code>i != j</code>, and</li>
<li><code>words[i] + words[j]</code> (the concatenation of the two strings) is a palindrome string.</li>
</ul>
<p>Return <em>an array of all the <strong>palindrome pairs</strong> of </em><code>words</code>.</p>
<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = [&quot;abcd&quot;,&quot;dcba&quot;,&quot;lls&quot;,&quot;s&quot;,&quot;sssll&quot;]
<strong>Output:</strong> [[0,1],[1,0],[3,2],[2,4]]
<strong>Explanation:</strong> The palindromes are [&quot;dcbaabcd&quot;,&quot;abcddcba&quot;,&quot;slls&quot;,&quot;llssssll&quot;]
<strong>Explanation:</strong> The palindromes are [&quot;abcddcba&quot;,&quot;dcbaabcd&quot;,&quot;slls&quot;,&quot;llssssll&quot;]
</pre>
<p><strong>Example 2:</strong></p>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = [&quot;bat&quot;,&quot;tab&quot;,&quot;cat&quot;]
@ -23,11 +33,12 @@
<strong>Explanation:</strong> The palindromes are [&quot;battab&quot;,&quot;tabbat&quot;]
</pre>
<p><strong>Example 3:</strong></p>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> words = [&quot;a&quot;,&quot;&quot;]
<strong>Output:</strong> [[0,1],[1,0]]
<strong>Explanation:</strong> The palindromes are [&quot;a&quot;,&quot;a&quot;]
</pre>
<p>&nbsp;</p>
@ -36,7 +47,7 @@
<ul>
<li><code>1 &lt;= words.length &lt;= 5000</code></li>
<li><code>0 &lt;= words[i].length &lt;= 300</code></li>
<li><code>words[i]</code> consists of lower-case English letters.</li>
<li><code>words[i]</code> consists of lowercase English letters.</li>
</ul>
## Solutions

View File

@ -12,11 +12,17 @@
<li>A player who succeeds in placing <code>n</code> of their marks in a horizontal, vertical, or diagonal row wins the game.</li>
</ol>
<p>Implement the&nbsp;<code>TicTacToe</code> class:</p>
<p>Implement the <code>TicTacToe</code> class:</p>
<ul>
<li><code>TicTacToe(int n)</code> Initializes the object the size of the board <code>n</code>.</li>
<li><code>int move(int row, int col, int player)</code> Indicates that the player with id <code>player</code> plays at the cell <code>(row, col)</code> of the board. The move is guaranteed to be a valid move.</li>
<li><code>int move(int row, int col, int player)</code> Indicates that the player with id <code>player</code> plays at the cell <code>(row, col)</code> of the board. The move is guaranteed to be a valid move, and the two players alternate in making moves. Return
<ul>
<li><code>0</code> if there is <strong>no winner</strong> after the move,</li>
<li><code>1</code> if <strong>player 1</strong> is the winner after the move, or</li>
<li><code>2</code> if <strong>player 2</strong> is the winner after the move.</li>
</ul>
</li>
</ul>
<p>&nbsp;</p>

View File

@ -6,7 +6,12 @@
<p>Given an integer array <code>nums</code>, return <em>the number of <strong>reverse pairs</strong> in the array</em>.</p>
<p>A reverse pair is a pair <code>(i, j)</code> where <code>0 &lt;= i &lt; j &lt; nums.length</code> and <code>nums[i] &gt; 2 * nums[j]</code>.</p>
<p>A <strong>reverse pair</strong> is a pair <code>(i, j)</code> where:</p>
<ul>
<li><code>0 &lt;= i &lt; j &lt; nums.length</code> and</li>
<li><code>nums[i] &gt; 2 * nums[j]</code>.</li>
</ul>
<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
@ -27,7 +32,7 @@
<strong>Explanation:</strong> The reverse pairs are:
(1, 4) --&gt; nums[1] = 4, nums[4] = 1, 4 &gt; 2 * 1
(2, 4) --&gt; nums[2] = 3, nums[4] = 1, 3 &gt; 2 * 1
(3, 4) --&gt; nums[3] = 3, nums[4] = 1, 5 &gt; 2 * 1
(3, 4) --&gt; nums[3] = 5, nums[4] = 1, 5 &gt; 2 * 1
</pre>
<p>&nbsp;</p>

View File

@ -7,7 +7,7 @@
<p>Given two integer arrays <code>nums1</code> and <code>nums2</code>, return <em>the maximum length of a subarray that appears in <strong>both</strong> arrays</em>.</p>
<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [1,2,3,2,1], nums2 = [3,2,1,4,7]
@ -15,11 +15,12 @@
<strong>Explanation:</strong> The repeated subarray with maximum length is [3,2,1].
</pre>
<p><strong>Example 2:</strong></p>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [0,0,0,0,0], nums2 = [0,0,0,0,0]
<strong>Output:</strong> 5
<strong>Explanation:</strong> The repeated subarray with maximum length is [0,0,0,0,0].
</pre>
<p>&nbsp;</p>

View File

@ -42,6 +42,8 @@
<strong>解释:</strong>有 2 个全局倒置,和 1 个局部倒置。
</pre>
<p><strong>提示:</strong></p>
<ul>

View File

@ -9,18 +9,20 @@
<p>Given two anagrams <code>s1</code> and <code>s2</code>, return the smallest <code>k</code> for which <code>s1</code> and <code>s2</code> are <code>k</code><strong>-similar</strong>.</p>
<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s1 = &quot;ab&quot;, s2 = &quot;ba&quot;
<strong>Output:</strong> 1
<strong>Explanation:</strong> The two string are 1-similar because we can use one swap to change s1 to s2: &quot;ab&quot; --&gt; &quot;ba&quot;.
</pre>
<p><strong>Example 2:</strong></p>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s1 = &quot;abc&quot;, s2 = &quot;bca&quot;
<strong>Output:</strong> 2
<strong>Explanation:</strong> The two strings are 2-similar because we can use two swaps to change s1 to s2: &quot;abc&quot; --&gt; &quot;bac&quot; --&gt; &quot;bca&quot;.
</pre>
<p>&nbsp;</p>

View File

@ -109,7 +109,7 @@ class Solution:
class Solution:
def numDupDigitsAtMostN(self, n: int) -> int:
return n - self.f(n)
def f(self, n):
@cache
def dfs(pos, mask, lead, limit):

View File

@ -77,7 +77,7 @@ class Solution:
class Solution:
def numDupDigitsAtMostN(self, n: int) -> int:
return n - self.f(n)
def f(self, n):
@cache
def dfs(pos, mask, lead, limit):

View File

@ -4,14 +4,18 @@
## Description
<p>You are given two integer arrays <code>nums</code> and <code>multipliers</code><strong> </strong>of size <code>n</code> and <code>m</code> respectively, where <code>n &gt;= m</code>. The arrays are <strong>1-indexed</strong>.</p>
<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums</code> and <code>multipliers</code><strong> </strong>of size <code>n</code> and <code>m</code> respectively, where <code>n &gt;= m</code>.</p>
<p>You begin with a score of <code>0</code>. You want to perform <strong>exactly</strong> <code>m</code> operations. On the <code>i<sup>th</sup></code> operation <strong>(1-indexed)</strong>, you will:</p>
<p>You begin with a score of <code>0</code>. You want to perform <strong>exactly</strong> <code>m</code> operations. On the <code>i<sup>th</sup></code> operation (<strong>0-indexed</strong>) you will:</p>
<ul>
<li>Choose one integer <code>x</code> from <strong>either the start or the end </strong>of the array <code>nums</code>.</li>
<li>Add <code>multipliers[i] * x</code> to your score.</li>
<li>Remove <code>x</code> from the array <code>nums</code>.</li>
<li>Choose one integer <code>x</code> from <strong>either the start or the end </strong>of the array <code>nums</code>.</li>
<li>Add <code>multipliers[i] * x</code> to your score.
<ul>
<li>Note that <code>multipliers[0]</code> corresponds to the first operation, <code>multipliers[1]</code> to the second operation, and so on.</li>
</ul>
</li>
<li>Remove <code>x</code> from <code>nums</code>.</li>
</ul>
<p>Return <em>the <strong>maximum</strong> score after performing </em><code>m</code> <em>operations.</em></p>
@ -48,7 +52,7 @@ The total score is 50 + 15 - 9 + 4 + 42 = 102.
<ul>
<li><code>n == nums.length</code></li>
<li><code>m == multipliers.length</code></li>
<li><code>1 &lt;= m &lt;= 10<sup>3</sup></code></li>
<li><code>1 &lt;= m &lt;= 300</code></li>
<li><code>m &lt;= n &lt;= 10<sup>5</sup></code><code> </code></li>
<li><code>-1000 &lt;= nums[i], multipliers[i] &lt;= 1000</code></li>
</ul>

View File

@ -178,7 +178,7 @@ class Solution {
class Solution {
private int[] a = new int[11];
private int[][] dp = new int[11][1 << 11];
public int countSpecialNumbers(int n) {
return f(n);
}

View File

@ -148,7 +148,7 @@ class Solution {
class Solution {
private int[] a = new int[11];
private int[][] dp = new int[11][1 << 11];
public int countSpecialNumbers(int n) {
return f(n);
}

View File

@ -0,0 +1,78 @@
# [2418. 按身高排序](https://leetcode.cn/problems/sort-the-people)
[English Version](/solution/2400-2499/2418.Sort%20the%20People/README_EN.md)
## 题目描述
<!-- 这里写题目描述 -->
<p>给你一个字符串数组 <code>names</code> ,和一个由 <strong>互不相同</strong> 的正整数组成的数组 <code>heights</code> 。两个数组的长度均为 <code>n</code></p>
<p>对于每个下标 <code>i</code><code>names[i]</code><code>heights[i]</code> 表示第 <code>i</code> 个人的名字和身高。</p>
<p>请按身高 <strong>降序</strong> 顺序返回对应的名字数组 <code>names</code></p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre><strong>输入:</strong>names = ["Mary","John","Emma"], heights = [180,165,170]
<strong>输出:</strong>["Mary","Emma","John"]
<strong>解释:</strong>Mary 最高,接着是 Emma 和 John 。
</pre>
<p><strong>示例 2</strong></p>
<pre><strong>输入:</strong>names = ["Alice","Bob","Bob"], heights = [155,185,150]
<strong>输出:</strong>["Bob","Alice","Bob"]
<strong>解释:</strong>第一个 Bob 最高,然后是 Alice 和第二个 Bob 。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>n == names.length == heights.length</code></li>
<li><code>1 &lt;= n &lt;= 10<sup>3</sup></code></li>
<li><code>1 &lt;= names[i].length &lt;= 20</code></li>
<li><code>1 &lt;= heights[i] &lt;= 10<sup>5</sup></code></li>
<li><code>names[i]</code> 由大小写英文字母组成</li>
<li><code>heights</code> 中的所有值互不相同</li>
</ul>
## 解法
<!-- 这里可写通用的实现逻辑 -->
<!-- tabs:start -->
### **Python3**
<!-- 这里可写当前语言的特殊实现逻辑 -->
```python
```
### **Java**
<!-- 这里可写当前语言的特殊实现逻辑 -->
```java
```
### **TypeScript**
```ts
```
### **...**
```
```
<!-- tabs:end -->

View File

@ -0,0 +1,70 @@
# [2418. Sort the People](https://leetcode.com/problems/sort-the-people)
[中文文档](/solution/2400-2499/2418.Sort%20the%20People/README.md)
## Description
<p>You are given an array of strings <code>names</code>, and an array <code>heights</code> that consists of <strong>distinct</strong> positive integers. Both arrays are of length <code>n</code>.</p>
<p>For each index <code>i</code>, <code>names[i]</code> and <code>heights[i]</code> denote the name and height of the <code>i<sup>th</sup></code> person.</p>
<p>Return <code>names</code><em> sorted in <strong>descending</strong> order by the people&#39;s heights</em>.</p>
<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input:</strong> names = [&quot;Mary&quot;,&quot;John&quot;,&quot;Emma&quot;], heights = [180,165,170]
<strong>Output:</strong> [&quot;Mary&quot;,&quot;Emma&quot;,&quot;John&quot;]
<strong>Explanation:</strong> Mary is the tallest, followed by Emma and John.
</pre>
<p><strong>Example 2:</strong></p>
<pre>
<strong>Input:</strong> names = [&quot;Alice&quot;,&quot;Bob&quot;,&quot;Bob&quot;], heights = [155,185,150]
<strong>Output:</strong> [&quot;Bob&quot;,&quot;Alice&quot;,&quot;Bob&quot;]
<strong>Explanation:</strong> The first Bob is the tallest, followed by Alice and the second Bob.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == names.length == heights.length</code></li>
<li><code>1 &lt;= n &lt;= 10<sup>3</sup></code></li>
<li><code>1 &lt;= names[i].length &lt;= 20</code></li>
<li><code>1 &lt;= heights[i] &lt;= 10<sup>5</sup></code></li>
<li><code>names[i]</code> consists of lower and upper case English letters.</li>
<li>All the values of <code>heights</code> are distinct.</li>
</ul>
## Solutions
<!-- tabs:start -->
### **Python3**
```python
```
### **Java**
```java
```
### **TypeScript**
```ts
```
### **...**
```
```
<!-- tabs:end -->

View File

@ -0,0 +1,88 @@
# [2419. 按位与最大的最长子数组](https://leetcode.cn/problems/longest-subarray-with-maximum-bitwise-and)
[English Version](/solution/2400-2499/2419.Longest%20Subarray%20With%20Maximum%20Bitwise%20AND/README_EN.md)
## 题目描述
<!-- 这里写题目描述 -->
<p>给你一个长度为 <code>n</code> 的整数数组 <code>nums</code></p>
<p>考虑 <code>nums</code> 中进行 <strong>按位与bitwise AND</strong>运算得到的值 <strong>最大</strong><strong>非空</strong> 子数组。</p>
<ul>
<li>换句话说,令 <code>k</code><code>nums</code> <strong>任意</strong> 子数组执行按位与运算所能得到的最大值。那么,只需要考虑那些执行一次按位与运算后等于 <code>k</code> 的子数组。</li>
</ul>
<p>返回满足要求的 <strong>最长</strong> 子数组的长度。</p>
<p>数组的按位与就是对数组中的所有数字进行按位与运算。</p>
<p><strong>子数组</strong> 是数组中的一个连续元素序列。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>nums = [1,2,3,3,2,2]
<strong>输出:</strong>2
<strong>解释:</strong>
子数组按位与运算的最大值是 3 。
能得到此结果的最长子数组是 [3,3],所以返回 2 。
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>nums = [1,2,3,4]
<strong>输出:</strong>1
<strong>解释:</strong>
子数组按位与运算的最大值是 4 。
能得到此结果的最长子数组是 [4],所以返回 1 。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li>
</ul>
## 解法
<!-- 这里可写通用的实现逻辑 -->
<!-- tabs:start -->
### **Python3**
<!-- 这里可写当前语言的特殊实现逻辑 -->
```python
```
### **Java**
<!-- 这里可写当前语言的特殊实现逻辑 -->
```java
```
### **TypeScript**
```ts
```
### **...**
```
```
<!-- tabs:end -->

View File

@ -0,0 +1,78 @@
# [2419. Longest Subarray With Maximum Bitwise AND](https://leetcode.com/problems/longest-subarray-with-maximum-bitwise-and)
[中文文档](/solution/2400-2499/2419.Longest%20Subarray%20With%20Maximum%20Bitwise%20AND/README.md)
## Description
<p>You are given an integer array <code>nums</code> of size <code>n</code>.</p>
<p>Consider a <strong>non-empty</strong> subarray from <code>nums</code> that has the <strong>maximum</strong> possible <strong>bitwise AND</strong>.</p>
<ul>
<li>In other words, let <code>k</code> be the maximum value of the bitwise AND of <strong>any</strong> subarray of <code>nums</code>. Then, only subarrays with a bitwise AND equal to <code>k</code> should be considered.</li>
</ul>
<p>Return <em>the length of the <strong>longest</strong> such subarray</em>.</p>
<p>The bitwise AND of an array is the bitwise AND of all the numbers in it.</p>
<p>A <strong>subarray</strong> is a contiguous sequence of elements within an array.</p>
<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,3,2,2]
<strong>Output:</strong> 2
<strong>Explanation:</strong>
The maximum possible bitwise AND of a subarray is 3.
The longest subarray with that value is [3,3], so we return 2.
</pre>
<p><strong>Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
The maximum possible bitwise AND of a subarray is 4.
The longest subarray with that value is [4], so we return 1.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li>
</ul>
## Solutions
<!-- tabs:start -->
### **Python3**
```python
```
### **Java**
```java
```
### **TypeScript**
```ts
```
### **...**
```
```
<!-- tabs:end -->

View File

@ -0,0 +1,85 @@
# [2420. 找到所有好下标](https://leetcode.cn/problems/find-all-good-indices)
[English Version](/solution/2400-2499/2420.Find%20All%20Good%20Indices/README_EN.md)
## 题目描述
<!-- 这里写题目描述 -->
<p>给你一个大小为 <code>n</code>&nbsp;下标从 <strong>0</strong>&nbsp;开始的整数数组&nbsp;<code>nums</code>&nbsp;和一个正整数&nbsp;<code>k</code>&nbsp;</p>
<p>对于&nbsp;<code>k &lt;= i &lt; n - k</code>&nbsp;之间的一个下标&nbsp;<code>i</code>&nbsp;,如果它满足以下条件,我们就称它为一个&nbsp;<strong></strong>&nbsp;下标:</p>
<ul>
<li>下标 <code>i</code> <strong>之前</strong><code>k</code>&nbsp;个元素是 <strong>非递增的</strong>&nbsp;</li>
<li>下标 <code>i</code> <strong>之后</strong>&nbsp;<code>k</code>&nbsp;个元素是 <strong>非递减的</strong>&nbsp;</li>
</ul>
<p><strong>升序</strong>&nbsp;返回所有好下标。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre>
<b>输入:</b>nums = [2,1,1,1,3,4,1], k = 2
<b>输出:</b>[2,3]
<b>解释:</b>数组中有两个好下标:
- 下标 2 。子数组 [2,1] 是非递增的,子数组 [1,3] 是非递减的。
- 下标 3 。子数组 [1,1] 是非递增的,子数组 [3,4] 是非递减的。
注意,下标 4 不是好下标,因为 [4,1] 不是非递减的。</pre>
<p><strong>示例 2</strong></p>
<pre>
<b>输入:</b>nums = [2,1,1,2], k = 2
<b>输出:</b>[]
<b>解释:</b>数组中没有好下标。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>3 &lt;= n &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li>
<li><code>1 &lt;= k &lt;= n / 2</code></li>
</ul>
## 解法
<!-- 这里可写通用的实现逻辑 -->
<!-- tabs:start -->
### **Python3**
<!-- 这里可写当前语言的特殊实现逻辑 -->
```python
```
### **Java**
<!-- 这里可写当前语言的特殊实现逻辑 -->
```java
```
### **TypeScript**
```ts
```
### **...**
```
```
<!-- tabs:end -->

View File

@ -0,0 +1,75 @@
# [2420. Find All Good Indices](https://leetcode.com/problems/find-all-good-indices)
[中文文档](/solution/2400-2499/2420.Find%20All%20Good%20Indices/README.md)
## Description
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of size <code>n</code> and a positive integer <code>k</code>.</p>
<p>We call an index <code>i</code> in the range <code>k &lt;= i &lt; n - k</code> <strong>good</strong> if the following conditions are satisfied:</p>
<ul>
<li>The <code>k</code> elements that are just <strong>before</strong> the index <code>i</code> are in <strong>non-increasing</strong> order.</li>
<li>The <code>k</code> elements that are just <strong>after</strong> the index <code>i</code> are in <strong>non-decreasing</strong> order.</li>
</ul>
<p>Return <em>an array of all good indices sorted in <strong>increasing</strong> order</em>.</p>
<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,1,1,1,3,4,1], k = 2
<strong>Output:</strong> [2,3]
<strong>Explanation:</strong> There are two good indices in the array:
- Index 2. The subarray [2,1] is in non-increasing order, and the subarray [1,3] is in non-decreasing order.
- Index 3. The subarray [1,1] is in non-increasing order, and the subarray [3,4] is in non-decreasing order.
Note that the index 4 is not good because [4,1] is not non-decreasing.</pre>
<p><strong>Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,1,1,2], k = 2
<strong>Output:</strong> []
<strong>Explanation:</strong> There are no good indices in this array.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>3 &lt;= n &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li>
<li><code>1 &lt;= k &lt;= n / 2</code></li>
</ul>
## Solutions
<!-- tabs:start -->
### **Python3**
```python
```
### **Java**
```java
```
### **TypeScript**
```ts
```
### **...**
```
```
<!-- tabs:end -->

View File

@ -0,0 +1,106 @@
# [2421. 好路径的数目](https://leetcode.cn/problems/number-of-good-paths)
[English Version](/solution/2400-2499/2421.Number%20of%20Good%20Paths/README_EN.md)
## 题目描述
<!-- 这里写题目描述 -->
<p>给你一棵 <code>n</code>&nbsp;个节点的树(连通无向无环的图),节点编号从&nbsp;<code>0</code>&nbsp;&nbsp;<code>n - 1</code>&nbsp;且恰好有&nbsp;<code>n - 1</code>&nbsp;条边。</p>
<p>给你一个长度为 <code>n</code>&nbsp;下标从 <strong>0</strong>&nbsp;开始的整数数组&nbsp;<code>vals</code>&nbsp;,分别表示每个节点的值。同时给你一个二维整数数组&nbsp;<code>edges</code>&nbsp;,其中&nbsp;<code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code>&nbsp;表示节点&nbsp;<code>a<sub>i</sub></code>&nbsp;<code>b<sub>i</sub></code><sub>&nbsp;</sub>之间有一条&nbsp;<strong>无向</strong>&nbsp;边。</p>
<p>一条 <strong>好路径</strong>&nbsp;需要满足以下条件:</p>
<ol>
<li>开始节点和结束节点的值 <strong>相同</strong>&nbsp;</li>
<li>开始节点和结束节点中间的所有节点值都 <strong>小于等于</strong>&nbsp;开始节点的值(也就是说开始节点的值应该是路径上所有节点的最大值)。</li>
</ol>
<p>请你返回不同好路径的数目。</p>
<p>注意,一条路径和它反向的路径算作 <strong>同一</strong>&nbsp;路径。比方说,&nbsp;<code>0 -&gt; 1</code>&nbsp;&nbsp;<code>1 -&gt; 0</code>&nbsp;视为同一条路径。单个节点也视为一条合法路径。</p>
<p>&nbsp;</p>
<p><strong>示例&nbsp;1</strong></p>
<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2400-2499/2421.Number%20of%20Good%20Paths/images/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;"></p>
<pre><b>输入:</b>vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]]
<b>输出:</b>6
<b>解释:</b>总共有 5 条单个节点的好路径。
还有 1 条好路径1 -&gt; 0 -&gt; 2 -&gt; 4 。
(反方向的路径 4 -&gt; 2 -&gt; 0 -&gt; 1 视为跟 1 -&gt; 0 -&gt; 2 -&gt; 4 一样的路径)
注意 0 -&gt; 2 -&gt; 3 不是一条好路径,因为 vals[2] &gt; vals[0] 。
</pre>
<p><strong>示例 2</strong></p>
<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2400-2499/2421.Number%20of%20Good%20Paths/images/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;"></p>
<pre><b>输入:</b>vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]]
<b>输出:</b>7
<strong>解释:</strong>总共有 5 条单个节点的好路径。
还有 2 条好路径0 -&gt; 1 和 2 -&gt; 3 。
</pre>
<p><strong>示例 3</strong></p>
<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2400-2499/2421.Number%20of%20Good%20Paths/images/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;"></p>
<pre><b>输入:</b>vals = [1], edges = []
<b>输出:</b>1
<b>解释:</b>这棵树只有一个节点,所以只有一条好路径。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>n == vals.length</code></li>
<li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li>
<li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
<li><code>edges</code>&nbsp;表示一棵合法的树。</li>
</ul>
## 解法
<!-- 这里可写通用的实现逻辑 -->
<!-- tabs:start -->
### **Python3**
<!-- 这里可写当前语言的特殊实现逻辑 -->
```python
```
### **Java**
<!-- 这里可写当前语言的特殊实现逻辑 -->
```java
```
### **TypeScript**
```ts
```
### **...**
```
```
<!-- tabs:end -->

View File

@ -0,0 +1,93 @@
# [2421. Number of Good Paths](https://leetcode.com/problems/number-of-good-paths)
[中文文档](/solution/2400-2499/2421.Number%20of%20Good%20Paths/README.md)
## Description
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p>
<p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p>
<p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p>
<ol>
<li>The starting node and the ending node have the <strong>same</strong> value.</li>
<li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li>
</ol>
<p>Return <em>the number of distinct good paths</em>.</p>
<p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p>
<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2400-2499/2421.Number%20of%20Good%20Paths/images/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" />
<pre>
<strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]]
<strong>Output:</strong> 6
<strong>Explanation:</strong> There are 5 good paths consisting of a single node.
There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4.
(The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.)
Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0].
</pre>
<p><strong>Example 2:</strong></p>
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2400-2499/2421.Number%20of%20Good%20Paths/images/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" />
<pre>
<strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]]
<strong>Output:</strong> 7
<strong>Explanation:</strong> There are 5 good paths consisting of a single node.
There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3.
</pre>
<p><strong>Example 3:</strong></p>
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2400-2499/2421.Number%20of%20Good%20Paths/images/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" />
<pre>
<strong>Input:</strong> vals = [1], edges = []
<strong>Output:</strong> 1
<strong>Explanation:</strong> The tree consists of only one node, so there is one good path.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == vals.length</code></li>
<li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li>
<li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
<li><code>edges</code> represents a valid tree.</li>
</ul>
## Solutions
<!-- tabs:start -->
### **Python3**
```python
```
### **Java**
```java
```
### **TypeScript**
```ts
```
### **...**
```
```
<!-- tabs:end -->

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

View File

@ -23,6 +23,14 @@ https://lcpredictor.herokuapp.com
## 往期竞赛
#### 第 312 场周赛(2022-09-25 10:30, 90 分钟) 参赛人数 6637
- [2418. 按身高排序](/solution/2400-2499/2418.Sort%20the%20People/README.md)
- [2419. 按位与最大的最长子数组](/solution/2400-2499/2419.Longest%20Subarray%20With%20Maximum%20Bitwise%20AND/README.md)
- [2420. 找到所有好下标](/solution/2400-2499/2420.Find%20All%20Good%20Indices/README.md)
- [2421. 好路径的数目](/solution/2400-2499/2421.Number%20of%20Good%20Paths/README.md)
#### 第 311 场周赛(2022-09-18 10:30, 90 分钟) 参赛人数 6710
- [2413. 最小偶倍数](/solution/2400-2499/2413.Smallest%20Even%20Multiple/README.md)
@ -1385,7 +1393,7 @@ https://lcpredictor.herokuapp.com
#### 第 198 场周赛(2020-07-19 10:30, 90 分钟) 参赛人数 5780
- [1518. 换问题](/solution/1500-1599/1518.Water%20Bottles/README.md)
- [1518. 换问题](/solution/1500-1599/1518.Water%20Bottles/README.md)
- [1519. 子树中标签相同的节点数](/solution/1500-1599/1519.Number%20of%20Nodes%20in%20the%20Sub-Tree%20With%20the%20Same%20Label/README.md)
- [1520. 最多的不重叠子字符串](/solution/1500-1599/1520.Maximum%20Number%20of%20Non-Overlapping%20Substrings/README.md)
- [1521. 找到最接近目标值的函数值](/solution/1500-1599/1521.Find%20a%20Value%20of%20a%20Mysterious%20Function%20Closest%20to%20Target/README.md)

View File

@ -25,6 +25,14 @@ Get your rating changes right after the completion of LeetCode contests, https:/
## Past Contests
#### Weekly Contest 312
- [2418. Sort the People](/solution/2400-2499/2418.Sort%20the%20People/README_EN.md)
- [2419. Longest Subarray With Maximum Bitwise AND](/solution/2400-2499/2419.Longest%20Subarray%20With%20Maximum%20Bitwise%20AND/README_EN.md)
- [2420. Find All Good Indices](/solution/2400-2499/2420.Find%20All%20Good%20Indices/README_EN.md)
- [2421. Number of Good Paths](/solution/2400-2499/2421.Number%20of%20Good%20Paths/README_EN.md)
#### Weekly Contest 311
- [2413. Smallest Even Multiple](/solution/2400-2499/2413.Smallest%20Even%20Multiple/README_EN.md)

View File

@ -1779,7 +1779,7 @@
| 1767 | [寻找没有被执行的任务对](/solution/1700-1799/1767.Find%20the%20Subtasks%20That%20Did%20Not%20Execute/README.md) | `数据库` | 困难 | 🔒 |
| 1768 | [交替合并字符串](/solution/1700-1799/1768.Merge%20Strings%20Alternately/README.md) | `双指针`,`字符串` | 简单 | 第 229 场周赛 |
| 1769 | [移动所有球到每个盒子所需的最小操作数](/solution/1700-1799/1769.Minimum%20Number%20of%20Operations%20to%20Move%20All%20Balls%20to%20Each%20Box/README.md) | `数组`,`字符串` | 中等 | 第 229 场周赛 |
| 1770 | [执行乘法运算的最大分数](/solution/1700-1799/1770.Maximum%20Score%20from%20Performing%20Multiplication%20Operations/README.md) | `数组`,`动态规划` | 中等 | 第 229 场周赛 |
| 1770 | [执行乘法运算的最大分数](/solution/1700-1799/1770.Maximum%20Score%20from%20Performing%20Multiplication%20Operations/README.md) | `数组`,`动态规划` | 困难 | 第 229 场周赛 |
| 1771 | [由子序列构造的最长回文串的长度](/solution/1700-1799/1771.Maximize%20Palindrome%20Length%20From%20Subsequences/README.md) | `字符串`,`动态规划` | 困难 | 第 229 场周赛 |
| 1772 | [按受欢迎程度排列功能](/solution/1700-1799/1772.Sort%20Features%20by%20Popularity/README.md) | `数组`,`哈希表`,`字符串`,`排序` | 中等 | 🔒 |
| 1773 | [统计匹配检索规则的物品数量](/solution/1700-1799/1773.Count%20Items%20Matching%20a%20Rule/README.md) | `数组`,`字符串` | 简单 | 第 230 场周赛 |
@ -2427,6 +2427,10 @@
| 2415 | [反转二叉树的奇数层](/solution/2400-2499/2415.Reverse%20Odd%20Levels%20of%20Binary%20Tree/README.md) | `树`,`深度优先搜索`,`广度优先搜索`,`二叉树` | 中等 | 第 311 场周赛 |
| 2416 | [字符串的前缀分数和](/solution/2400-2499/2416.Sum%20of%20Prefix%20Scores%20of%20Strings/README.md) | `字典树`,`数组`,`字符串`,`计数` | 困难 | 第 311 场周赛 |
| 2417 | [Closest Fair Integer](/solution/2400-2499/2417.Closest%20Fair%20Integer/README.md) | | 中等 | 🔒 |
| 2418 | [按身高排序](/solution/2400-2499/2418.Sort%20the%20People/README.md) | | 简单 | 第 312 场周赛 |
| 2419 | [按位与最大的最长子数组](/solution/2400-2499/2419.Longest%20Subarray%20With%20Maximum%20Bitwise%20AND/README.md) | | 中等 | 第 312 场周赛 |
| 2420 | [找到所有好下标](/solution/2400-2499/2420.Find%20All%20Good%20Indices/README.md) | | 中等 | 第 312 场周赛 |
| 2421 | [好路径的数目](/solution/2400-2499/2421.Number%20of%20Good%20Paths/README.md) | | 困难 | 第 312 场周赛 |
## 版权

View File

@ -1777,7 +1777,7 @@ Press <kbd>Control</kbd>+<kbd>F</kbd>(or <kbd>Command</kbd>+<kbd>F</kbd> on the
| 1767 | [Find the Subtasks That Did Not Execute](/solution/1700-1799/1767.Find%20the%20Subtasks%20That%20Did%20Not%20Execute/README_EN.md) | `Database` | Hard | 🔒 |
| 1768 | [Merge Strings Alternately](/solution/1700-1799/1768.Merge%20Strings%20Alternately/README_EN.md) | `Two Pointers`,`String` | Easy | Weekly Contest 229 |
| 1769 | [Minimum Number of Operations to Move All Balls to Each Box](/solution/1700-1799/1769.Minimum%20Number%20of%20Operations%20to%20Move%20All%20Balls%20to%20Each%20Box/README_EN.md) | `Array`,`String` | Medium | Weekly Contest 229 |
| 1770 | [Maximum Score from Performing Multiplication Operations](/solution/1700-1799/1770.Maximum%20Score%20from%20Performing%20Multiplication%20Operations/README_EN.md) | `Array`,`Dynamic Programming` | Medium | Weekly Contest 229 |
| 1770 | [Maximum Score from Performing Multiplication Operations](/solution/1700-1799/1770.Maximum%20Score%20from%20Performing%20Multiplication%20Operations/README_EN.md) | `Array`,`Dynamic Programming` | Hard | Weekly Contest 229 |
| 1771 | [Maximize Palindrome Length From Subsequences](/solution/1700-1799/1771.Maximize%20Palindrome%20Length%20From%20Subsequences/README_EN.md) | `String`,`Dynamic Programming` | Hard | Weekly Contest 229 |
| 1772 | [Sort Features by Popularity](/solution/1700-1799/1772.Sort%20Features%20by%20Popularity/README_EN.md) | `Array`,`Hash Table`,`String`,`Sorting` | Medium | 🔒 |
| 1773 | [Count Items Matching a Rule](/solution/1700-1799/1773.Count%20Items%20Matching%20a%20Rule/README_EN.md) | `Array`,`String` | Easy | Weekly Contest 230 |
@ -2425,6 +2425,10 @@ Press <kbd>Control</kbd>+<kbd>F</kbd>(or <kbd>Command</kbd>+<kbd>F</kbd> on the
| 2415 | [Reverse Odd Levels of Binary Tree](/solution/2400-2499/2415.Reverse%20Odd%20Levels%20of%20Binary%20Tree/README_EN.md) | `Tree`,`Depth-First Search`,`Breadth-First Search`,`Binary Tree` | Medium | Weekly Contest 311 |
| 2416 | [Sum of Prefix Scores of Strings](/solution/2400-2499/2416.Sum%20of%20Prefix%20Scores%20of%20Strings/README_EN.md) | `Trie`,`Array`,`String`,`Counting` | Hard | Weekly Contest 311 |
| 2417 | [Closest Fair Integer](/solution/2400-2499/2417.Closest%20Fair%20Integer/README_EN.md) | | Medium | 🔒 |
| 2418 | [Sort the People](/solution/2400-2499/2418.Sort%20the%20People/README_EN.md) | | Easy | Weekly Contest 312 |
| 2419 | [Longest Subarray With Maximum Bitwise AND](/solution/2400-2499/2419.Longest%20Subarray%20With%20Maximum%20Bitwise%20AND/README_EN.md) | | Medium | Weekly Contest 312 |
| 2420 | [Find All Good Indices](/solution/2400-2499/2420.Find%20All%20Good%20Indices/README_EN.md) | | Medium | Weekly Contest 312 |
| 2421 | [Number of Good Paths](/solution/2400-2499/2421.Number%20of%20Good%20Paths/README_EN.md) | | Hard | Weekly Contest 312 |
## Copyright

View File

@ -206,17 +206,17 @@ def save(result):
if __name__ == '__main__':
cookie_cn = ''
cookie_en = ''
spider = Spider(cookie_cn, cookie_en)
res = spider.run()
save(res)
# cookie_cn = ''
# cookie_en = ''
# spider = Spider(cookie_cn, cookie_en)
# res = spider.run()
# save(res)
with open('./result.json', 'r', encoding='utf-8') as f:
res = f.read()
res = json.loads(res)
generate_readme(res)
generate_question_readme(res)
generate_summary(res)
# refresh(res)
# generate_readme(res)
# generate_question_readme(res)
# generate_summary(res)
refresh(res)

View File

@ -2465,3 +2465,7 @@
- [2415.反转二叉树的奇数层](/solution/2400-2499/2415.Reverse%20Odd%20Levels%20of%20Binary%20Tree/README.md)
- [2416.字符串的前缀分数和](/solution/2400-2499/2416.Sum%20of%20Prefix%20Scores%20of%20Strings/README.md)
- [2417.Closest Fair Integer](/solution/2400-2499/2417.Closest%20Fair%20Integer/README.md)
- [2418.按身高排序](/solution/2400-2499/2418.Sort%20the%20People/README.md)
- [2419.按位与最大的最长子数组](/solution/2400-2499/2419.Longest%20Subarray%20With%20Maximum%20Bitwise%20AND/README.md)
- [2420.找到所有好下标](/solution/2400-2499/2420.Find%20All%20Good%20Indices/README.md)
- [2421.好路径的数目](/solution/2400-2499/2421.Number%20of%20Good%20Paths/README.md)

View File

@ -2465,3 +2465,7 @@
- [2415.Reverse Odd Levels of Binary Tree](/solution/2400-2499/2415.Reverse%20Odd%20Levels%20of%20Binary%20Tree/README_EN.md)
- [2416.Sum of Prefix Scores of Strings](/solution/2400-2499/2416.Sum%20of%20Prefix%20Scores%20of%20Strings/README_EN.md)
- [2417.Closest Fair Integer](/solution/2400-2499/2417.Closest%20Fair%20Integer/README_EN.md)
- [2418.Sort the People](/solution/2400-2499/2418.Sort%20the%20People/README_EN.md)
- [2419.Longest Subarray With Maximum Bitwise AND](/solution/2400-2499/2419.Longest%20Subarray%20With%20Maximum%20Bitwise%20AND/README_EN.md)
- [2420.Find All Good Indices](/solution/2400-2499/2420.Find%20All%20Good%20Indices/README_EN.md)
- [2421.Number of Good Paths](/solution/2400-2499/2421.Number%20of%20Good%20Paths/README_EN.md)