diff --git a/solution/0100-0199/0176.Second Highest Salary/README.md b/solution/0100-0199/0176.Second Highest Salary/README.md index 24f765ff93e15..6bfed0d0b9fbd 100644 --- a/solution/0100-0199/0176.Second Highest Salary/README.md +++ b/solution/0100-0199/0176.Second Highest Salary/README.md @@ -17,13 +17,13 @@ | id | int | | salary | int | +-------------+------+ -id 是这个表的主键。 +在 SQL 中,id 是这个表的主键。 表的每一行包含员工的工资信息。

 

-

编写一个 SQL 查询,获取并返回 Employee 表中第二高的薪水 。如果不存在第二高的薪水,查询应该返回 null

+

查询并返回 Employee 表中第二高的薪水 。如果不存在第二高的薪水,查询应该返回 null(Pandas 则返回 None)

查询结果如下例所示。

diff --git a/solution/0100-0199/0176.Second Highest Salary/README_EN.md b/solution/0100-0199/0176.Second Highest Salary/README_EN.md index 176810b1ca9ee..8c557729a6187 100644 --- a/solution/0100-0199/0176.Second Highest Salary/README_EN.md +++ b/solution/0100-0199/0176.Second Highest Salary/README_EN.md @@ -13,13 +13,13 @@ | id | int | | salary | int | +-------------+------+ -id is the primary key column for this table. +In SQL, id is the primary key column for this table. Each row of this table contains information about the salary of an employee.

 

-

Find the second highest salary from the Employee table. If there is no second highest salary, return null.

+

Find the second highest salary from the Employee table. If there is no second highest salary, return null (return None in Pandas).

The result format is in the following example.

diff --git a/solution/0100-0199/0177.Nth Highest Salary/README.md b/solution/0100-0199/0177.Nth Highest Salary/README.md index ed36f801dfad0..23bb08a27befc 100644 --- a/solution/0100-0199/0177.Nth Highest Salary/README.md +++ b/solution/0100-0199/0177.Nth Highest Salary/README.md @@ -15,13 +15,13 @@ | id | int | | salary | int | +-------------+------+ -Id是该表的主键列。 +在 SQL 中,id 是该表的主键。 该表的每一行都包含有关员工工资的信息。

 

-

编写一个SQL查询来报告 Employee 表中第 n 高的工资。如果没有第 n 个最高工资,查询应该报告为 null

+

查询 Employee 表中第 n 高的工资。如果没有第 n 个最高工资,查询结果应该为 null

查询结果格式如下所示。

diff --git a/solution/0100-0199/0177.Nth Highest Salary/README_EN.md b/solution/0100-0199/0177.Nth Highest Salary/README_EN.md index 3067ebc64bb0a..4cf8ed6f95af3 100644 --- a/solution/0100-0199/0177.Nth Highest Salary/README_EN.md +++ b/solution/0100-0199/0177.Nth Highest Salary/README_EN.md @@ -13,7 +13,7 @@ | id | int | | salary | int | +-------------+------+ -id is the primary key column for this table. +In SQL, id is the primary key column for this table. Each row of this table contains information about the salary of an employee. diff --git a/solution/0100-0199/0178.Rank Scores/README.md b/solution/0100-0199/0178.Rank Scores/README.md index 2d094ad07cb9c..e2aa7e6de3501 100644 --- a/solution/0100-0199/0178.Rank Scores/README.md +++ b/solution/0100-0199/0178.Rank Scores/README.md @@ -15,13 +15,13 @@ | id | int | | score | decimal | +-------------+---------+ -Id是该表的主键。 -该表的每一行都包含了一场比赛的分数。Score是一个有两位小数点的浮点值。 +在 SQL 中,id 是该表的主键。 +该表的每一行都包含了一场比赛的分数。Score 是一个有两位小数点的浮点值。

 

-

编写 SQL 查询对分数进行排序。排名按以下规则计算:

+

查询并对分数进行排序。排名按以下规则计算:

-

编写一个 SQL 查询以报告 大国 的国家名称、人口和面积。

+

查询并报告 大国 的国家名称、人口和面积。

任意顺序 返回结果表。

diff --git a/solution/0500-0599/0595.Big Countries/README_EN.md b/solution/0500-0599/0595.Big Countries/README_EN.md index d152e032c268a..fe6b0a6c40596 100644 --- a/solution/0500-0599/0595.Big Countries/README_EN.md +++ b/solution/0500-0599/0595.Big Countries/README_EN.md @@ -16,7 +16,7 @@ | population | int | | gdp | bigint | +-------------+---------+ -name is the primary key column for this table. +In SQL, name is the primary key column for this table. Each row of this table gives information about the name of a country, the continent to which it belongs, its area, the population, and its GDP value. diff --git a/solution/0500-0599/0596.Classes More Than 5 Students/README.md b/solution/0500-0599/0596.Classes More Than 5 Students/README.md index 21bfa79ea2a47..da7216c9bd81c 100644 --- a/solution/0500-0599/0596.Classes More Than 5 Students/README.md +++ b/solution/0500-0599/0596.Classes More Than 5 Students/README.md @@ -15,13 +15,13 @@ | student | varchar | | class | varchar | +-------------+---------+ -(student, class)是该表的主键列。 +在 SQL 中,(student, class)是该表的主键列。 该表的每一行表示学生的名字和他们注册的班级。

 

-

编写一个SQL查询来报告 至少有5个学生 的所有班级。

+

查询 至少有5个学生 的所有班级。

任意顺序 返回结果表。

diff --git a/solution/0500-0599/0596.Classes More Than 5 Students/README_EN.md b/solution/0500-0599/0596.Classes More Than 5 Students/README_EN.md index 4a1c305857fe3..c110e3d4eb610 100644 --- a/solution/0500-0599/0596.Classes More Than 5 Students/README_EN.md +++ b/solution/0500-0599/0596.Classes More Than 5 Students/README_EN.md @@ -13,7 +13,7 @@ | student | varchar | | class | varchar | +-------------+---------+ -(student, class) is the primary key column for this table. +In SQL, (student, class) is the primary key column for this table. Each row of this table indicates the name of a student and the class in which they are enrolled. diff --git a/solution/0600-0699/0607.Sales Person/README.md b/solution/0600-0699/0607.Sales Person/README.md index 0f19638099ddc..b9a11a9405950 100644 --- a/solution/0600-0699/0607.Sales Person/README.md +++ b/solution/0600-0699/0607.Sales Person/README.md @@ -18,7 +18,7 @@ | commission_rate | int | | hire_date | date | +-----------------+---------+ -sales_id 是该表的主键列。 +在 SQL 中,sales_id 是该表的主键列。 该表的每一行都显示了销售人员的姓名和 ID ,以及他们的工资、佣金率和雇佣日期。 @@ -34,7 +34,7 @@ sales_id 是该表的主键列。 | name | varchar | | city | varchar | +-------------+---------+ -com_id 是该表的主键列。 +在 SQL 中,com_id 是该表的主键列。 该表的每一行都表示公司的名称和 ID ,以及公司所在的城市。 @@ -52,7 +52,7 @@ com_id 是该表的主键列。 | sales_id | int | | amount | int | +-------------+------+ -order_id 是该表的主键列。 +在 SQL 中,order_id 是该表的主键列。 com_id 是 Company 表中 com_id 的外键。 sales_id 是来自销售员表 sales_id 的外键。 该表的每一行包含一个订单的信息。这包括公司的 ID 、销售人员的 ID 、订单日期和支付的金额。 @@ -60,7 +60,7 @@ sales_id 是来自销售员表 sales_id 的外键。

 

-

编写一个SQL查询,报告没有任何与名为 “RED” 的公司相关的订单的所有销售人员的姓名。

+

查询没有任何与名为 “RED” 的公司相关的订单的所有销售人员的姓名。

任意顺序 返回结果表。

diff --git a/solution/0600-0699/0607.Sales Person/README_EN.md b/solution/0600-0699/0607.Sales Person/README_EN.md index 20de0a1a0f74e..f41739a04ca2e 100644 --- a/solution/0600-0699/0607.Sales Person/README_EN.md +++ b/solution/0600-0699/0607.Sales Person/README_EN.md @@ -16,7 +16,7 @@ | commission_rate | int | | hire_date | date | +-----------------+---------+ -sales_id is the primary key column for this table. +In SQL, sales_id is the primary key column for this table. Each row of this table indicates the name and the ID of a salesperson alongside their salary, commission rate, and hire date. @@ -32,7 +32,7 @@ Each row of this table indicates the name and the ID of a salesperson alongside | name | varchar | | city | varchar | +-------------+---------+ -com_id is the primary key column for this table. +In SQL, com_id is the primary key column for this table. Each row of this table indicates the name and the ID of a company and the city in which the company is located. @@ -50,9 +50,9 @@ Each row of this table indicates the name and the ID of a company and the city i | sales_id | int | | amount | int | +-------------+------+ -order_id is the primary key column for this table. -com_id is a foreign key to com_id from the Company table. -sales_id is a foreign key to sales_id from the SalesPerson table. +In SQL, order_id is the primary key column for this table. +com_id is a foreign key (join key in Pandas) to com_id from the Company table. +sales_id is a foreign key (join key in Pandas) to sales_id from the SalesPerson table. Each row of this table contains information about one order. This includes the ID of the company, the ID of the salesperson, the date of the order, and the amount paid. diff --git a/solution/0800-0899/0874.Walking Robot Simulation/README.md b/solution/0800-0899/0874.Walking Robot Simulation/README.md index 7e4790b5b6c7e..f35c9232a2ce3 100644 --- a/solution/0800-0899/0874.Walking Robot Simulation/README.md +++ b/solution/0800-0899/0874.Walking Robot Simulation/README.md @@ -6,15 +6,15 @@ -

机器人在一个无限大小的 XY 网格平面上行走,从点 (0, 0) 处开始出发,面向北方。该机器人可以接收以下三种类型的命令 commands

+

机器人在一个无限大小的 XY 网格平面上行走,从点 (0, 0) 处开始出发,面向北方。该机器人可以接收以下三种类型的命令 commands

-

在网格上有一些格子被视为障碍物 obstacles 。第 i 个障碍物位于网格点  obstacles[i] = (xi, yi)

+

在网格上有一些格子被视为障碍物 obstacles 。第 i 个障碍物位于网格点  obstacles[i] = (xi, yi)

机器人无法走到障碍物上,它将会停留在障碍物的前一个网格方块上,但仍然可以继续尝试进行该路线的其余部分。

@@ -24,7 +24,7 @@
-
 
+
 
@@ -41,7 +41,7 @@
-

 

+

 

示例 1:

@@ -55,7 +55,7 @@ 3. 向东移动 3 个单位,到达 (3, 4) 距离原点最远的是 (3, 4) ,距离为 32 + 42 = 25 -

示例 2:

+

示例 2:

 输入:commands = [4,-1,4,-2,4], obstacles = [[2,4]]
@@ -68,15 +68,15 @@
 5. 向北走 4 个单位,到达 (1, 8)
 距离原点最远的是 (1, 8) ,距离为 12 + 82 = 65
-

 

+

 

提示:

diff --git a/solution/0900-0999/0960.Delete Columns to Make Sorted III/README.md b/solution/0900-0999/0960.Delete Columns to Make Sorted III/README.md index 9c6d852b65764..9d18587401a0d 100644 --- a/solution/0900-0999/0960.Delete Columns to Make Sorted III/README.md +++ b/solution/0900-0999/0960.Delete Columns to Make Sorted III/README.md @@ -24,9 +24,9 @@ 输入:strs = ["babca","bbazb"] 输出:3 解释: -删除 0、1 和 4 这三列后,最终得到的数组是 A = ["bc", "az"]。 -这两行是分别按字典序排列的(即,A[0][0] <= A[0][1] 且 A[1][0] <= A[1][1])。 -注意,A[0] > A[1] —— 数组 A 不一定是按字典序排列的。 +删除 0、1 和 4 这三列后,最终得到的数组是 strs = ["bc", "az"]。 +这两行是分别按字典序排列的(即,strs[0][0] <= strs[0][1] 且 strs[1][0] <= strs[1][1])。 +注意,strs[0] > strs[1] —— 数组 strs 不一定是按字典序排列的。

示例 2:

diff --git a/solution/0900-0999/0979.Distribute Coins in Binary Tree/README.md b/solution/0900-0999/0979.Distribute Coins in Binary Tree/README.md index e9b452818ca7f..28e0d7af02787 100644 --- a/solution/0900-0999/0979.Distribute Coins in Binary Tree/README.md +++ b/solution/0900-0999/0979.Distribute Coins in Binary Tree/README.md @@ -6,56 +6,40 @@ -

给定一个有 N 个结点的二叉树的根结点 root,树中的每个结点上都对应有 node.val 枚硬币,并且总共有 N 枚硬币。

+

给你一个有 n 个结点的二叉树的根结点 root ,其中树中每个结点 node 都对应有 node.val 枚硬币。整棵树上一共有 n 枚硬币。

-

在一次移动中,我们可以选择两个相邻的结点,然后将一枚硬币从其中一个结点移动到另一个结点。(移动可以是从父结点到子结点,或者从子结点移动到父结点。)。

+

在一次移动中,我们可以选择两个相邻的结点,然后将一枚硬币从其中一个结点移动到另一个结点。移动可以是从父结点到子结点,或者从子结点移动到父结点。

-

返回使每个结点上只有一枚硬币所需的移动次数。

+

返回使每个结点上 只有 一枚硬币所需的 最少 移动次数。

 

-

示例 1:

- -

- -
输入:[3,0,0]
+

示例 1:

+ +
+输入:root = [3,0,0]
 输出:2
-解释:从树的根结点开始,我们将一枚硬币移到它的左子结点上,一枚硬币移到它的右子结点上。
+解释:一枚硬币从根结点移动到左子结点,一枚硬币从根结点移动到右子结点。
 
-

示例 2:

- -

- -
输入:[0,3,0]
+

示例 2:

+ +
+输入:root = [0,3,0]
 输出:3
-解释:从根结点的左子结点开始,我们将两枚硬币移到根结点上 [移动两次]。然后,我们把一枚硬币从根结点移到右子结点上。
-
- -

示例 3:

- -

- -
输入:[1,0,2]
-输出:2
-
- -

示例 4:

- -

- -
输入:[1,0,0,null,3]
-输出:4
+解释:将两枚硬币从根结点的左子结点移动到根结点(两次移动)。然后,将一枚硬币从根结点移动到右子结点。
 

 

提示:

-
    -
  1. 1<= N <= 100
  2. -
  3. 0 <= node.val <= N
  4. -
+
    +
  • 树中节点的数目为 n
  • +
  • 1 <= n <= 100
  • +
  • 0 <= Node.val <= n
  • +
  • 所有 Node.val 的值之和是 n
  • +
## 解法 diff --git a/solution/0900-0999/0992.Subarrays with K Different Integers/README.md b/solution/0900-0999/0992.Subarrays with K Different Integers/README.md index 832c480e04482..c710af79f8bbb 100644 --- a/solution/0900-0999/0992.Subarrays with K Different Integers/README.md +++ b/solution/0900-0999/0992.Subarrays with K Different Integers/README.md @@ -6,7 +6,7 @@ -

给定一个正整数数组 nums和一个整数 k ,返回 num 中 「好子数组」 的数目。

+

给定一个正整数数组 nums和一个整数 k,返回 nums 中 「好子数组」 的数目。

如果 nums 的某个子数组中不同整数的个数恰好为 k,则称 nums 的这个连续、不一定不同的子数组为 好子数组 」

diff --git a/solution/1000-1099/1050.Actors and Directors Who Cooperated At Least Three Times/README.md b/solution/1000-1099/1050.Actors and Directors Who Cooperated At Least Three Times/README.md index 13fe6f6c300fe..cfbddd1260b59 100644 --- a/solution/1000-1099/1050.Actors and Directors Who Cooperated At Least Three Times/README.md +++ b/solution/1000-1099/1050.Actors and Directors Who Cooperated At Least Three Times/README.md @@ -16,16 +16,19 @@ | director_id | int | | timestamp | int | +-------------+---------+ -timestamp 是这张表的主键. +在 SQL 中,timestamp 是这张表的主键.

 

-

写一条SQL查询语句获取合作过至少三次的演员和导演的 id 对 (actor_id, director_id)

+

查询合作过至少三次的演员和导演的 id 对 (actor_id, director_id)

-

示例:

+

 

+ +

示例 1:

+输入:
 ActorDirector 表:
 +-------------+-------------+-------------+
 | actor_id    | director_id | timestamp   |
@@ -38,13 +41,13 @@ ActorDirector 表:
 | 2           | 1           | 5           |
 | 2           | 1           | 6           |
 +-------------+-------------+-------------+
-
-Result 表:
+输出:
 +-------------+-------------+
 | actor_id    | director_id |
 +-------------+-------------+
 | 1           | 1           |
 +-------------+-------------+
+解释:
 唯一的 id 对是 (1, 1),他们恰好合作了 3 次。
## 解法 diff --git a/solution/1000-1099/1050.Actors and Directors Who Cooperated At Least Three Times/README_EN.md b/solution/1000-1099/1050.Actors and Directors Who Cooperated At Least Three Times/README_EN.md index f720c682c3a8f..dc66265e6d820 100644 --- a/solution/1000-1099/1050.Actors and Directors Who Cooperated At Least Three Times/README_EN.md +++ b/solution/1000-1099/1050.Actors and Directors Who Cooperated At Least Three Times/README_EN.md @@ -14,7 +14,7 @@ | director_id | int | | timestamp | int | +-------------+---------+ -timestamp is the primary key column for this table. +In SQL, timestamp is the primary key column for this table.

 

diff --git a/solution/1100-1199/1148.Article Views I/README.md b/solution/1100-1199/1148.Article Views I/README.md index d8db86a2076af..61ac964a58201 100644 --- a/solution/1100-1199/1148.Article Views I/README.md +++ b/solution/1100-1199/1148.Article Views I/README.md @@ -17,18 +17,25 @@ | viewer_id | int | | view_date | date | +---------------+---------+ -此表无主键,因此可能会存在重复行。 +此表可能会存在重复行。(换句话说,在 SQL 中这个表没有主键) 此表的每一行都表示某人在某天浏览了某位作者的某篇文章。 请注意,同一人的 author_id 和 viewer_id 是相同的。

 

-

请编写一条 SQL 查询以找出所有浏览过自己文章的作者,结果按照 id 升序排列。

+

请查询出所有浏览过自己文章的作者

+ +

结果按照 id 升序排列。

查询结果的格式如下所示:

+

 

+ +

示例 1:

+
+输入:
 Views 表:
 +------------+-----------+-----------+------------+
 | article_id | author_id | viewer_id | view_date  |
@@ -42,7 +49,7 @@ Views 表:
 | 3          | 4         | 4         | 2019-07-21 |
 +------------+-----------+-----------+------------+
 
-结果表:
+输出:
 +------+
 | id   |
 +------+
diff --git a/solution/1100-1199/1148.Article Views I/README_EN.md b/solution/1100-1199/1148.Article Views I/README_EN.md
index 147033c783f5e..7336bb5803094 100644
--- a/solution/1100-1199/1148.Article Views I/README_EN.md	
+++ b/solution/1100-1199/1148.Article Views I/README_EN.md	
@@ -15,7 +15,7 @@
 | viewer_id     | int     |
 | view_date     | date    |
 +---------------+---------+
-There is no primary key for this table, it may have duplicate rows.
+The table may have duplicate rows (In other words, there is no primary key for this table in SQL).
 Each row of this table indicates that some viewer viewed an article (written by some author) on some date. 
 Note that equal author_id and viewer_id indicate the same person.
 
diff --git a/solution/1100-1199/1173.Immediate Food Delivery I/README.md b/solution/1100-1199/1173.Immediate Food Delivery I/README.md index 4e5e685d356b3..619d9cdf60902 100644 --- a/solution/1100-1199/1173.Immediate Food Delivery I/README.md +++ b/solution/1100-1199/1173.Immediate Food Delivery I/README.md @@ -17,7 +17,7 @@ | order_date | date | | customer_pref_delivery_date | date | +-----------------------------+---------+ -delivery_id 是表的主键。 +在 SQL 中,delivery_id 是表的主键。 该表保存着顾客的食物配送信息,顾客在某个日期下了订单,并指定了一个期望的配送日期(和下单日期相同或者在那之后)。 @@ -25,7 +25,7 @@ delivery_id 是表的主键。

如果顾客期望的配送日期和下单日期相同,则该订单称为 「即时订单」,否则称为「计划订单」。

-

写一条 SQL 查询语句获取即时订单所占的百分比, 保留两位小数。

+

查询即时订单所占的百分比, 保留两位小数。

查询结果如下所示。

diff --git a/solution/1100-1199/1173.Immediate Food Delivery I/README_EN.md b/solution/1100-1199/1173.Immediate Food Delivery I/README_EN.md index 8b69d6c3204b8..136ce5c195ed7 100644 --- a/solution/1100-1199/1173.Immediate Food Delivery I/README_EN.md +++ b/solution/1100-1199/1173.Immediate Food Delivery I/README_EN.md @@ -15,7 +15,7 @@ | order_date | date | | customer_pref_delivery_date | date | +-----------------------------+---------+ -delivery_id is the primary key of this table. +In SQL, delivery_id is the primary key of this table. The table holds information about food delivery to customers that make orders at some date and specify a preferred delivery date (on the same order date or after it). diff --git a/solution/1200-1299/1280.Students and Examinations/README.md b/solution/1200-1299/1280.Students and Examinations/README.md index b4dee4430b30d..d881547cb6c39 100644 --- a/solution/1200-1299/1280.Students and Examinations/README.md +++ b/solution/1200-1299/1280.Students and Examinations/README.md @@ -8,48 +8,60 @@

学生表: Students

-
+---------------+---------+
+
++---------------+---------+
 | Column Name   | Type    |
 +---------------+---------+
 | student_id    | int     |
 | student_name  | varchar |
 +---------------+---------+
-主键为 student_id(学生ID),该表内的每一行都记录有学校一名学生的信息。
+在 SQL 中,主键为 student_id(学生ID)。
+该表内的每一行都记录有学校一名学生的信息。
 

 

科目表: Subjects

-
+--------------+---------+
+
++--------------+---------+
 | Column Name  | Type    |
 +--------------+---------+
 | subject_name | varchar |
 +--------------+---------+
-主键为 subject_name(科目名称),每一行记录学校的一门科目名称。
+在 SQL 中,主键为 subject_name(科目名称)。
+每一行记录学校的一门科目名称。
 

 

考试表: Examinations

-
+--------------+---------+
+
++--------------+---------+
 | Column Name  | Type    |
 +--------------+---------+
 | student_id   | int     |
 | subject_name | varchar |
 +--------------+---------+
-这张表压根没有主键,可能会有重复行。
-学生表里的一个学生修读科目表里的每一门科目,而这张考试表的每一行记录就表示学生表里的某个学生参加了一次科目表里某门科目的测试。
+这个表可能包含重复数据(换句话说,在 SQL 中,这个表没有主键)。
+学生表里的一个学生修读科目表里的每一门科目。
+这张考试表的每一行记录就表示学生表里的某个学生参加了一次科目表里某门科目的测试。
 

 

-

要求写一段 SQL 语句,查询出每个学生参加每一门科目测试的次数,结果按 student_idsubject_name 排序。

+

查询出每个学生参加每一门科目测试的次数,结果按 student_idsubject_name 排序。

+ +

查询结构格式如下所示。

+ +

 

-

查询结构格式如下所示:

+

示例 1:

-
Students table:
+
+输入:
+Students table:
 +------------+--------------+
 | student_id | student_name |
 +------------+--------------+
@@ -82,7 +94,7 @@ Examinations table:
 | 2          | Math         |
 | 1          | Math         |
 +------------+--------------+
-Result table:
+输出:
 +------------+--------------+--------------+----------------+
 | student_id | student_name | subject_name | attended_exams |
 +------------+--------------+--------------+----------------+
@@ -99,6 +111,7 @@ Result table:
 | 13         | John         | Physics      | 1              |
 | 13         | John         | Programming  | 1              |
 +------------+--------------+--------------+----------------+
+解释:
 结果表需包含所有学生和所有科目(即便测试次数为0):
 Alice 参加了 3 次数学测试, 2 次物理测试,以及 1 次编程测试;
 Bob 参加了 1 次数学测试, 1 次编程测试,没有参加物理测试;
diff --git a/solution/1200-1299/1280.Students and Examinations/README_EN.md b/solution/1200-1299/1280.Students and Examinations/README_EN.md
index 86b64ff780bbb..a161dc650d88c 100644
--- a/solution/1200-1299/1280.Students and Examinations/README_EN.md	
+++ b/solution/1200-1299/1280.Students and Examinations/README_EN.md	
@@ -13,7 +13,7 @@
 | student_id    | int     |
 | student_name  | varchar |
 +---------------+---------+
-student_id is the primary key for this table.
+In SQL, student_id is the primary key for this table.
 Each row of this table contains the ID and the name of one student in the school.
 
@@ -27,7 +27,7 @@ Each row of this table contains the ID and the name of one student in the school +--------------+---------+ | subject_name | varchar | +--------------+---------+ -subject_name is the primary key for this table. +In SQL, subject_name is the primary key for this table. Each row of this table contains the name of one subject in the school.
@@ -42,7 +42,7 @@ Each row of this table contains the name of one subject in the school. | student_id | int | | subject_name | varchar | +--------------+---------+ -There is no primary key for this table. It may contain duplicates. +This table may contain duplicates (In other words, there is no primary key for this table in SQL). Each student from the Students table takes every course from the Subjects table. Each row of this table indicates that a student with ID student_id attended the exam of subject_name.
diff --git a/solution/1300-1399/1378.Replace Employee ID With The Unique Identifier/README.md b/solution/1300-1399/1378.Replace Employee ID With The Unique Identifier/README.md index dbf2d3c0967f1..8742605b743d4 100644 --- a/solution/1300-1399/1378.Replace Employee ID With The Unique Identifier/README.md +++ b/solution/1300-1399/1378.Replace Employee ID With The Unique Identifier/README.md @@ -15,7 +15,7 @@ | id | int | | name | varchar | +---------------+---------+ -id 是这张表的主键。 +在 SQL 中,id 是这张表的主键。 这张表的每一行分别代表了某公司其中一位员工的名字和 ID 。
@@ -30,7 +30,7 @@ id 是这张表的主键。 | id | int | | unique_id | int | +---------------+---------+ -(id, unique_id) 是这张表的主键。 +在 SQL 中,(id, unique_id) 是这张表的主键。 这张表的每一行包含了该公司某位员工的 ID 和他的唯一标识码(unique ID)。
diff --git a/solution/1300-1399/1378.Replace Employee ID With The Unique Identifier/README_EN.md b/solution/1300-1399/1378.Replace Employee ID With The Unique Identifier/README_EN.md index 72325e548f748..d0f3040e0d86e 100644 --- a/solution/1300-1399/1378.Replace Employee ID With The Unique Identifier/README_EN.md +++ b/solution/1300-1399/1378.Replace Employee ID With The Unique Identifier/README_EN.md @@ -13,7 +13,7 @@ | id | int | | name | varchar | +---------------+---------+ -id is the primary key for this table. +In SQL, id is the primary key for this table. Each row of this table contains the id and the name of an employee in a company. @@ -28,7 +28,7 @@ Each row of this table contains the id and the name of an employee in a company. | id | int | | unique_id | int | +---------------+---------+ -(id, unique_id) is the primary key for this table. +In SQL, (id, unique_id) is the primary key for this table. Each row of this table contains the id and the corresponding unique id of an employee in the company. diff --git a/solution/1400-1499/1462.Course Schedule IV/README.md b/solution/1400-1499/1462.Course Schedule IV/README.md index 5eb312a8af399..edb69f982bd3e 100644 --- a/solution/1400-1499/1462.Course Schedule IV/README.md +++ b/solution/1400-1499/1462.Course Schedule IV/README.md @@ -61,6 +61,7 @@
  • ai != bi
  • 每一对 [ai, bi] 都 不同
  • 先修课程图中没有环。
  • +
  • 1 <= queries.length <= 104
  • 0 <= ui, vi <= n - 1
  • ui != vi
  • diff --git a/solution/1400-1499/1484.Group Sold Products By The Date/README.md b/solution/1400-1499/1484.Group Sold Products By The Date/README.md index 2ba4ee3e8039e..900785de2ff33 100644 --- a/solution/1400-1499/1484.Group Sold Products By The Date/README.md +++ b/solution/1400-1499/1484.Group Sold Products By The Date/README.md @@ -15,13 +15,13 @@ | sell_date | date | | product | varchar | +-------------+---------+ -此表没有主键,它可能包含重复项。 +这个表可能包含重复数据(换句话说,在 SQL 中,这个表没有主键)。 此表的每一行都包含产品名称和在市场上销售的日期。

     

    -

    编写一个 SQL 查询来查找每个日期、销售的不同产品的数量及其名称。
    +

    查询每个日期、销售的不同产品的数量及其名称。
    每个日期的销售产品名称应按词典序排列。
    返回按 sell_date 排序的结果表。
    查询结果格式如下例所示。

    @@ -55,7 +55,8 @@ Activities 表: 解释: 对于2020-05-30,出售的物品是 (Headphone, Basketball, T-shirt),按词典序排列,并用逗号 ',' 分隔。 对于2020-06-01,出售的物品是 (Pencil, Bible),按词典序排列,并用逗号分隔。 -对于2020-06-02,出售的物品是 (Mask),只需返回该物品名。 +对于2020-06-02,出售的物品是 (Mask),只需返回该物品名。 + ## 解法 diff --git a/solution/1400-1499/1484.Group Sold Products By The Date/README_EN.md b/solution/1400-1499/1484.Group Sold Products By The Date/README_EN.md index 276aa42a1822f..9838bd63d5bf0 100644 --- a/solution/1400-1499/1484.Group Sold Products By The Date/README_EN.md +++ b/solution/1400-1499/1484.Group Sold Products By The Date/README_EN.md @@ -13,7 +13,7 @@ | sell_date | date | | product | varchar | +-------------+---------+ -There is no primary key for this table, it may contain duplicates. +This table may contain duplicates (In other words, there is no primary key for this table in SQL). Each row of this table contains the product name and the date it was sold in a market. diff --git a/solution/1500-1599/1517.Find Users With Valid E-Mails/README.md b/solution/1500-1599/1517.Find Users With Valid E-Mails/README.md index adeea3defaa52..88049e6a7b740 100644 --- a/solution/1500-1599/1517.Find Users With Valid E-Mails/README.md +++ b/solution/1500-1599/1517.Find Users With Valid E-Mails/README.md @@ -6,7 +6,7 @@ -

    用户表: Users

    +

    用户表: Users

     +---------------+---------+
    @@ -16,28 +16,31 @@
     | name          | varchar |
     | mail          | varchar | 
     +---------------+---------+
    -user_id (用户 ID)是该表的主键。
    +在 SQL 中,user_id (用户 ID)是该表的主键。
     这个表包含用户在某网站上注册的信息。有些邮箱是无效的。

     

    -

    写一条 SQL 语句,查询拥有有效邮箱的用户。

    +

    查询拥有有效邮箱的用户。

    有效的邮箱包含符合下列条件的前缀名和域名:

    按任意顺序返回结果表。

    +

    查询格式如下所示。

    +

     

    -

    查询格式如下所示:

    +

    示例 1:

    -Users
    +输入:
    +Users:
     +---------+-----------+-------------------------+
     | user_id | name      | mail                    |
     +---------+-----------+-------------------------+
    @@ -50,7 +53,7 @@ user_id (用户 ID)是该表的主键。
     | 7       | Shapiro   | .shapo@leetcode.com     |
     +---------+-----------+-------------------------+
     
    -结果表:
    +输出:
     +---------+-----------+-------------------------+
     | user_id | name      | mail                    |
     +---------+-----------+-------------------------+
    @@ -58,6 +61,7 @@ user_id (用户 ID)是该表的主键。
     | 3       | Annabelle | bella-@leetcode.com     |
     | 4       | Sally     | sally.come@leetcode.com |
     +---------+-----------+-------------------------+
    +解释:
     2 号用户的邮箱没有域名。
     5 号用户的邮箱包含非法字符 #。
     6 号用户的邮箱的域名不是 leetcode。
    diff --git a/solution/1500-1599/1517.Find Users With Valid E-Mails/README_EN.md b/solution/1500-1599/1517.Find Users With Valid E-Mails/README_EN.md
    index 7034a6016b290..24dcb5606b71d 100644
    --- a/solution/1500-1599/1517.Find Users With Valid E-Mails/README_EN.md	
    +++ b/solution/1500-1599/1517.Find Users With Valid E-Mails/README_EN.md	
    @@ -14,7 +14,7 @@
     | name          | varchar |
     | mail          | varchar |
     +---------------+---------+
    -user_id is the primary key for this table.
    +In SQL, user_id is the primary key for this table.
     This table contains information of the users signed up in a website. Some e-mails are invalid.
     
    diff --git a/solution/1500-1599/1527.Patients With a Condition/README.md b/solution/1500-1599/1527.Patients With a Condition/README.md index 8944c4ac667c1..0047f46eb6c83 100644 --- a/solution/1500-1599/1527.Patients With a Condition/README.md +++ b/solution/1500-1599/1527.Patients With a Condition/README.md @@ -16,13 +16,13 @@ | patient_name | varchar | | conditions | varchar | +--------------+---------+ -patient_id (患者 ID)是该表的主键。 +在 SQL 中,patient_id (患者 ID)是该表的主键。 'conditions' (疾病)包含 0 个或以上的疾病代码,以空格分隔。 这个表包含医院中患者的信息。

     

    -

    写一条 SQL 语句,查询患有 I 类糖尿病的患者 ID (patient_id)、患者姓名(patient_name)以及其患有的所有疾病代码(conditions)。I 类糖尿病的代码总是包含前缀 DIAB1 。

    +

    查询患有 I 类糖尿病的患者 ID (patient_id)、患者姓名(patient_name)以及其患有的所有疾病代码(conditions)。I 类糖尿病的代码总是包含前缀 DIAB1 。

    任意顺序 返回结果表。

    diff --git a/solution/1500-1599/1527.Patients With a Condition/README_EN.md b/solution/1500-1599/1527.Patients With a Condition/README_EN.md index 587fb76fd8c5a..475d1cd89ad02 100644 --- a/solution/1500-1599/1527.Patients With a Condition/README_EN.md +++ b/solution/1500-1599/1527.Patients With a Condition/README_EN.md @@ -14,7 +14,7 @@ | patient_name | varchar | | conditions | varchar | +--------------+---------+ -patient_id is the primary key for this table. +In SQL, patient_id is the primary key for this table. 'conditions' contains 0 or more code separated by spaces. This table contains information of the patients in the hospital. diff --git a/solution/1600-1699/1667.Fix Names in a Table/README.md b/solution/1600-1699/1667.Fix Names in a Table/README.md index ecbf04e98623d..df39a8378fd5e 100644 --- a/solution/1600-1699/1667.Fix Names in a Table/README.md +++ b/solution/1600-1699/1667.Fix Names in a Table/README.md @@ -15,7 +15,7 @@ | user_id | int | | name | varchar | +----------------+---------+ -user_id 是该表的主键。 +在 SQL 中,user_id 是该表的主键。 该表包含用户的 ID 和名字。名字仅由小写和大写字符组成。 diff --git a/solution/1600-1699/1667.Fix Names in a Table/README_EN.md b/solution/1600-1699/1667.Fix Names in a Table/README_EN.md index 68042f5d72f56..3d598167c0473 100644 --- a/solution/1600-1699/1667.Fix Names in a Table/README_EN.md +++ b/solution/1600-1699/1667.Fix Names in a Table/README_EN.md @@ -13,7 +13,7 @@ | user_id | int | | name | varchar | +----------------+---------+ -user_id is the primary key for this table. +In SQL, user_id is the primary key for this table. This table contains the ID and the name of the user. The name consists of only lowercase and uppercase characters. diff --git a/solution/1600-1699/1683.Invalid Tweets/README.md b/solution/1600-1699/1683.Invalid Tweets/README.md index 2d8158dd8259b..5660252b8bf64 100644 --- a/solution/1600-1699/1683.Invalid Tweets/README.md +++ b/solution/1600-1699/1683.Invalid Tweets/README.md @@ -8,26 +8,31 @@

    表:Tweets

    -
    +----------------+---------+
    +
    ++----------------+---------+
     | Column Name    | Type    |
     +----------------+---------+
     | tweet_id       | int     |
     | content        | varchar |
     +----------------+---------+
    -tweet_id 是这个表的主键。
    +在 SQL 中,tweet_id 是这个表的主键。
     这个表包含某社交媒体 App 中所有的推文。
    -

     

    +

     

    -

    写一条 SQL 语句,查询所有无效推文的编号(ID)。当推文内容中的字符数严格大于 15 时,该推文是无效的。

    +

    查询所有无效推文的编号(ID)。当推文内容中的字符数严格大于 15 时,该推文是无效的。

    任意顺序返回结果表。

    -

    查询结果格式如下示例所示:

    +

    查询结果格式如下所示:

    + +

     

    -

     

    +

    示例 1:

    -
    Tweets 表:
    +
    +输入:
    +Tweets 表:
     +----------+----------------------------------+
     | tweet_id | content                          |
     +----------+----------------------------------+
    @@ -35,12 +40,13 @@ tweet_id 是这个表的主键。
     | 2        | Let us make America great again! |
     +----------+----------------------------------+
     
    -结果表:
    +输出:
     +----------+
     | tweet_id |
     +----------+
     | 2        |
     +----------+
    +解释:
     推文 1 的长度 length = 14。该推文是有效的。
     推文 2 的长度 length = 32。该推文是无效的。
     
    diff --git a/solution/1600-1699/1683.Invalid Tweets/README_EN.md b/solution/1600-1699/1683.Invalid Tweets/README_EN.md index 5d25f03312339..20bbb6f216652 100644 --- a/solution/1600-1699/1683.Invalid Tweets/README_EN.md +++ b/solution/1600-1699/1683.Invalid Tweets/README_EN.md @@ -13,7 +13,7 @@ | tweet_id | int | | content | varchar | +----------------+---------+ -tweet_id is the primary key for this table. +In SQL, tweet_id is the primary key for this table. This table contains all the tweets in a social media app.
    diff --git a/solution/1600-1699/1693.Daily Leads and Partners/README.md b/solution/1600-1699/1693.Daily Leads and Partners/README.md index a3e1169b583f9..5c4edf464e478 100644 --- a/solution/1600-1699/1693.Daily Leads and Partners/README.md +++ b/solution/1600-1699/1693.Daily Leads and Partners/README.md @@ -17,13 +17,13 @@ | lead_id | int | | partner_id | int | +-------------+---------+ -该表没有主键。 +这个表可能包含重复数据(换句话说,在 SQL 中,这个表没有主键)。 该表包含日期、产品的名称,以及售给的领导和合伙人的编号。 名称只包含小写英文字母。

     

    -

    写一条 SQL 语句,使得对于每一个 date_id 和 make_name,返回不同的 lead_id 以及不同的 partner_id 的数量。

    +

    对于每一个 date_id 和 make_name,返回不同的 lead_id 以及不同的 partner_id 的数量。

    任意顺序 返回结果表。

    diff --git a/solution/1600-1699/1693.Daily Leads and Partners/README_EN.md b/solution/1600-1699/1693.Daily Leads and Partners/README_EN.md index 49f4653f54a07..97536babbbf93 100644 --- a/solution/1600-1699/1693.Daily Leads and Partners/README_EN.md +++ b/solution/1600-1699/1693.Daily Leads and Partners/README_EN.md @@ -15,7 +15,7 @@ | lead_id | int | | partner_id | int | +-------------+---------+ -This table does not have a primary key. +This table may contain duplicates (In other words, there is no primary key for this table in SQL). This table contains the date and the name of the product sold and the IDs of the lead and partner it was sold to. The name consists of only lowercase English letters. diff --git a/solution/1700-1799/1741.Find Total Time Spent by Each Employee/README.md b/solution/1700-1799/1741.Find Total Time Spent by Each Employee/README.md index c8c8f95afd5b8..18cbf121c32e6 100644 --- a/solution/1700-1799/1741.Find Total Time Spent by Each Employee/README.md +++ b/solution/1700-1799/1741.Find Total Time Spent by Each Employee/README.md @@ -17,7 +17,7 @@ | in_time | int | | out_time | int | +-------------+------+ -(emp_id, event_day, in_time) 是这个表的主键。 +在 SQL 中,(emp_id, event_day, in_time) 是这个表的主键。 该表显示了员工在办公室的出入情况。 event_day 是此事件发生的日期,in_time 是员工进入办公室的时间,而 out_time 是他们离开办公室的时间。 in_time 和 out_time 的取值在1到1440之间。 diff --git a/solution/1700-1799/1741.Find Total Time Spent by Each Employee/README_EN.md b/solution/1700-1799/1741.Find Total Time Spent by Each Employee/README_EN.md index 4a9aa94f0032e..f5ea5886c8004 100644 --- a/solution/1700-1799/1741.Find Total Time Spent by Each Employee/README_EN.md +++ b/solution/1700-1799/1741.Find Total Time Spent by Each Employee/README_EN.md @@ -15,7 +15,7 @@ | in_time | int | | out_time | int | +-------------+------+ -(emp_id, event_day, in_time) is the primary key of this table. +In SQL, (emp_id, event_day, in_time) is the primary key of this table. The table shows the employees' entries and exits in an office. event_day is the day at which this event happened, in_time is the minute at which the employee entered the office, and out_time is the minute at which they left the office. in_time and out_time are between 1 and 1440. diff --git a/solution/1700-1799/1745.Palindrome Partitioning IV/README.md b/solution/1700-1799/1745.Palindrome Partitioning IV/README.md index fdc2cadeaa245..36d8aaf665349 100644 --- a/solution/1700-1799/1745.Palindrome Partitioning IV/README.md +++ b/solution/1700-1799/1745.Palindrome Partitioning IV/README.md @@ -1,4 +1,4 @@ -# [1745. 回文串分割 IV](https://leetcode.cn/problems/palindrome-partitioning-iv) +# [1745. 分割回文串 IV](https://leetcode.cn/problems/palindrome-partitioning-iv) [English Version](/solution/1700-1799/1745.Palindrome%20Partitioning%20IV/README_EN.md) diff --git a/solution/1700-1799/1757.Recyclable and Low Fat Products/README.md b/solution/1700-1799/1757.Recyclable and Low Fat Products/README.md index f26afaa90adca..43b40fa508b54 100644 --- a/solution/1700-1799/1757.Recyclable and Low Fat Products/README.md +++ b/solution/1700-1799/1757.Recyclable and Low Fat Products/README.md @@ -16,19 +16,24 @@ | low_fats | enum | | recyclable | enum | +-------------+---------+ -product_id 是这个表的主键。 +在 SQL 中,product_id 是这个表的主键。 low_fats 是枚举类型,取值为以下两种 ('Y', 'N'),其中 'Y' 表示该产品是低脂产品,'N' 表示不是低脂产品。 recyclable 是枚举类型,取值为以下两种 ('Y', 'N'),其中 'Y' 表示该产品可回收,而 'N' 表示不可回收。 -

     

    +

     

    -

    写出 SQL 语句,查找既是低脂又是可回收的产品编号。

    +

    查找既是低脂又是可回收的产品编号。

    返回结果 无顺序要求

    查询结果格式如下例所示:

    +

     

    + +

    示例 1:

    +
    +输入:
     Products 表:
     +-------------+----------+------------+
     | product_id  | low_fats | recyclable |
    @@ -39,13 +44,14 @@ Products 表:
     | 3           | Y        | Y          |
     | 4           | N        | N          |
     +-------------+----------+------------+
    -Result 表:
    +输出:
     +-------------+
     | product_id  |
     +-------------+
     | 1           |
     | 3           |
     +-------------+
    +解释:
     只有产品 id 为 1 和 3 的产品,既是低脂又是可回收的产品。
     
    diff --git a/solution/1700-1799/1757.Recyclable and Low Fat Products/README_EN.md b/solution/1700-1799/1757.Recyclable and Low Fat Products/README_EN.md index 3818e22c28354..2ad2c785bf9dd 100644 --- a/solution/1700-1799/1757.Recyclable and Low Fat Products/README_EN.md +++ b/solution/1700-1799/1757.Recyclable and Low Fat Products/README_EN.md @@ -14,7 +14,7 @@ | low_fats | enum | | recyclable | enum | +-------------+---------+ -product_id is the primary key for this table. +In SQL, product_id is the primary key for this table. low_fats is an ENUM of type ('Y', 'N') where 'Y' means this product is low fat and 'N' means it is not. recyclable is an ENUM of types ('Y', 'N') where 'Y' means this product is recyclable and 'N' means it is not. diff --git a/solution/1700-1799/1795.Rearrange Products Table/README.md b/solution/1700-1799/1795.Rearrange Products Table/README.md index adc6eabcc7f61..a44a6d34e3b56 100644 --- a/solution/1700-1799/1795.Rearrange Products Table/README.md +++ b/solution/1700-1799/1795.Rearrange Products Table/README.md @@ -17,9 +17,9 @@ | store2 | int | | store3 | int | +-------------+---------+ -这张表的主键是product_id(产品Id)。 -每行存储了这一产品在不同商店store1, store2, store3的价格。 -如果这一产品在商店里没有出售,则值将为null。 +在 SQL 中,这张表的主键是 product_id(产品Id)。 +每行存储了这一产品在不同商店 store1, store2, store3 的价格。 +如果这一产品在商店里没有出售,则值将为 null。

     

    @@ -54,8 +54,8 @@ Products table: | 1 | store3 | 80 | +------------+--------+-------+ 解释: -产品0在store1,store2,store3的价格分别为95,100,105。 -产品1在store1,store3的价格分别为70,80。在store2无法买到。 +产品 0 在 store1、store2、store3 的价格分别为 95、100、105。 +产品 1 在 store1、store3 的价格分别为 70、80。在 store2 无法买到。 ## 解法 diff --git a/solution/1800-1899/1851.Minimum Interval to Include Each Query/README.md b/solution/1800-1899/1851.Minimum Interval to Include Each Query/README.md index cfa80f36ba856..997027f30c5cf 100644 --- a/solution/1800-1899/1851.Minimum Interval to Include Each Query/README.md +++ b/solution/1800-1899/1851.Minimum Interval to Include Each Query/README.md @@ -8,11 +8,11 @@

    给你一个二维整数数组 intervals ,其中 intervals[i] = [lefti, righti] 表示第 i 个区间开始于 lefti 、结束于 righti(包含两侧取值,闭区间)。区间的 长度 定义为区间中包含的整数数目,更正式地表达是 righti - lefti + 1

    -

    再给你一个整数数组 queries 。第 j 个查询的答案是满足 lefti <= queries[j] <= righti长度最小区间 i 的长度 。如果不存在这样的区间,那么答案是 -1

    +

    再给你一个整数数组 queries 。第 j 个查询的答案是满足 lefti <= queries[j] <= righti长度最小区间 i 的长度 。如果不存在这样的区间,那么答案是 -1

    以数组形式返回对应查询的所有答案。

    -

     

    +

     

    示例 1:

    @@ -38,16 +38,16 @@ - Query = 22:区间 [20,25] 是包含 22 的最小区间,答案为 25 - 20 + 1 = 6 。 -

     

    +

     

    提示:

    ## 解法 diff --git a/solution/1800-1899/1873.Calculate Special Bonus/README.md b/solution/1800-1899/1873.Calculate Special Bonus/README.md index 7ca11491928c1..7a0f8bc25a31b 100644 --- a/solution/1800-1899/1873.Calculate Special Bonus/README.md +++ b/solution/1800-1899/1873.Calculate Special Bonus/README.md @@ -16,17 +16,15 @@ | name | varchar | | salary | int | +-------------+---------+ -employee_id 是这个表的主键。 +在 SQL 中,employee_id 是这个表的主键。 此表的每一行给出了雇员id ,名字和薪水。

     

    -

    计算每个雇员的奖金。如果一个雇员的id是奇数并且他的名字不是以'M'开头,那么他的奖金是他工资的100%,否则奖金为0。

    +

    计算每个雇员的奖金。如果一个雇员的 id 是 奇数 并且他的名字不是以 'M' 开头,那么他的奖金是他工资的 100% ,否则奖金为 0

    -

     

    - -

    返回的结果集请按照employee_id排序。

    +

    返回的结果按照 employee_id 排序。

    返回结果格式如下面的例子所示。

    diff --git a/solution/1800-1899/1873.Calculate Special Bonus/README_EN.md b/solution/1800-1899/1873.Calculate Special Bonus/README_EN.md index 40db86084bc37..82c29ac2c574c 100644 --- a/solution/1800-1899/1873.Calculate Special Bonus/README_EN.md +++ b/solution/1800-1899/1873.Calculate Special Bonus/README_EN.md @@ -14,7 +14,7 @@ | name | varchar | | salary | int | +-------------+---------+ -employee_id is the primary key for this table. +In SQL, employee_id is the primary key for this table. Each row of this table indicates the employee ID, employee name, and salary. diff --git a/solution/1900-1999/1907.Count Salary Categories/README.md b/solution/1900-1999/1907.Count Salary Categories/README.md index 148ca8edec1ef..67c99fe88887f 100644 --- a/solution/1900-1999/1907.Count Salary Categories/README.md +++ b/solution/1900-1999/1907.Count Salary Categories/README.md @@ -15,13 +15,13 @@ | account_id | int | | income | int | +-------------+------+ -account_id 是这个表的主键。 +在 SQL 中,account_id 是这个表的主键。 每一行都包含一个银行帐户的月收入的信息。

     

    -

    写出一个 SQL 查询,来报告每个工资类别的银行账户数量。 工资类别如下:

    +

    查询每个工资类别的银行账户数量。 工资类别如下:

    -

    The result table must contain all three categories. If there are no accounts in a category, then report 0.

    +

    The result table must contain all three categories. If there are no accounts in a category, return 0.

    Return the result table in any order.

    diff --git a/solution/2000-2099/2082.The Number of Rich Customers/README.md b/solution/2000-2099/2082.The Number of Rich Customers/README.md index 877dddc01cb1f..af9a973bc7d55 100644 --- a/solution/2000-2099/2082.The Number of Rich Customers/README.md +++ b/solution/2000-2099/2082.The Number of Rich Customers/README.md @@ -16,15 +16,15 @@ | customer_id | int | | amount | int | +-------------+------+ -bill_id 是这个表的主键。 +在 SQL 中,bill_id 是这个表的主键。 每一行包含一个订单的金额及相关客户的信息。

     

    -

    写一条 SQL 语句,查询至少有一个订单的金额严格大于 500 的客户的数量。

    +

    查询至少有一个订单的金额严格大于 500 的客户的数量。

    -

    查询结果格式如下示例所示:

    +

    返回结果格式如下示例所示:

     

    diff --git a/solution/2000-2099/2082.The Number of Rich Customers/README_EN.md b/solution/2000-2099/2082.The Number of Rich Customers/README_EN.md index afc6638ba5d0c..2490194fa1788 100644 --- a/solution/2000-2099/2082.The Number of Rich Customers/README_EN.md +++ b/solution/2000-2099/2082.The Number of Rich Customers/README_EN.md @@ -14,7 +14,7 @@ | customer_id | int | | amount | int | +-------------+------+ -bill_id is the primary key for this table. +In SQL, bill_id is the primary key for this table. Each row contains information about the amount of one bill and the customer associated with it. diff --git a/solution/2300-2399/2356.Number of Unique Subjects Taught by Each Teacher/README.md b/solution/2300-2399/2356.Number of Unique Subjects Taught by Each Teacher/README.md index b3c48a80d8c05..9f373ce7d3ef3 100644 --- a/solution/2300-2399/2356.Number of Unique Subjects Taught by Each Teacher/README.md +++ b/solution/2300-2399/2356.Number of Unique Subjects Taught by Each Teacher/README.md @@ -16,13 +16,13 @@ | subject_id | int | | dept_id | int | +-------------+------+ -(subject_id, dept_id) 是该表的主键。 +在 SQL 中,(subject_id, dept_id) 是该表的主键。 该表中的每一行都表示带有 teacher_id 的教师在系 dept_id 中教授科目 subject_id。

     

    -

    写一个 SQL 来查询每位老师在大学里教授的科目种类的数量。

    +

    查询每位老师在大学里教授的科目种类的数量。

    任意顺序 返回结果表。

    diff --git a/solution/2300-2399/2356.Number of Unique Subjects Taught by Each Teacher/README_EN.md b/solution/2300-2399/2356.Number of Unique Subjects Taught by Each Teacher/README_EN.md index 49e22b95f4fb3..1d93f1f220ed7 100644 --- a/solution/2300-2399/2356.Number of Unique Subjects Taught by Each Teacher/README_EN.md +++ b/solution/2300-2399/2356.Number of Unique Subjects Taught by Each Teacher/README_EN.md @@ -14,7 +14,7 @@ | subject_id | int | | dept_id | int | +-------------+------+ -(subject_id, dept_id) is the primary key for this table. +In SQL, (subject_id, dept_id) is the primary key for this table. Each row in this table indicates that the teacher with teacher_id teaches the subject subject_id in the department dept_id. diff --git a/solution/2500-2599/2520.Count the Digits That Divide a Number/README.md b/solution/2500-2599/2520.Count the Digits That Divide a Number/README.md index 58f8f6dd6b79f..1e49f9659a57b 100644 --- a/solution/2500-2599/2520.Count the Digits That Divide a Number/README.md +++ b/solution/2500-2599/2520.Count the Digits That Divide a Number/README.md @@ -158,7 +158,7 @@ impl Solution { .filter(|&c| c != '0') .filter(|&c| num % c.to_digit(10).unwrap() as i32 == 0) .count() as i32 - } + } } ``` diff --git a/solution/2500-2599/2520.Count the Digits That Divide a Number/README_EN.md b/solution/2500-2599/2520.Count the Digits That Divide a Number/README_EN.md index f2f56b65adb9e..8608fb4cb3fd2 100644 --- a/solution/2500-2599/2520.Count the Digits That Divide a Number/README_EN.md +++ b/solution/2500-2599/2520.Count the Digits That Divide a Number/README_EN.md @@ -145,7 +145,7 @@ impl Solution { .filter(|&c| c != '0') .filter(|&c| num % c.to_digit(10).unwrap() as i32 == 0) .count() as i32 - } + } } ``` diff --git a/solution/2700-2799/2784.Check if Array is Good/README.md b/solution/2700-2799/2784.Check if Array is Good/README.md new file mode 100644 index 0000000000000..ede09e725920f --- /dev/null +++ b/solution/2700-2799/2784.Check if Array is Good/README.md @@ -0,0 +1,171 @@ +# [2784. 检查数组是否是好的](https://leetcode.cn/problems/check-if-array-is-good) + +[English Version](/solution/2700-2799/2784.Check%20if%20Array%20is%20Good/README_EN.md) + +## 题目描述 + + + +

    给你一个整数数组 nums ,如果它是数组 base[n] 的一个排列,我们称它是个  数组。

    + +

    base[n] = [1, 2, ..., n - 1, n, n] (换句话说,它是一个长度为 n + 1 且包含 1 到 n - 1 恰好各一次,包含 n  两次的一个数组)。比方说,base[1] = [1, 1] ,base[3] = [1, 2, 3, 3] 。

    + +

    如果数组是一个好数组,请你返回 true ,否则返回 false 。

    + +

    注意:数组的排列是这些数字按任意顺序排布后重新得到的数组。

    + +

     

    + +

    示例 1:

    + +
    输入:nums = [2, 1, 3]
    +输出:false
    +解释:因为数组的最大元素是 3 ,唯一可以构成这个数组的 base[n] 对应的 n = 3 。但是 base[3] 有 4 个元素,但数组 nums 只有 3 个元素,所以无法得到 base[3] = [1, 2, 3, 3] 的排列,所以答案为 false 。
    +
    + +

    示例 2:

    + +
    输入:nums = [1, 3, 3, 2]
    +输出:true
    +解释:因为数组的最大元素是 3 ,唯一可以构成这个数组的 base[n] 对应的 n = 3 ,可以看出数组是 base[3] = [1, 2, 3, 3] 的一个排列(交换 nums 中第二个和第四个元素)。所以答案为 true 。
    + +

    示例 3:

    + +
    输入:nums = [1, 1]
    +输出:true
    +解释:因为数组的最大元素是 1 ,唯一可以构成这个数组的 base[n] 对应的 n = 1,可以看出数组是 base[1] = [1, 1] 的一个排列。所以答案为 true 。
    + +

    示例 4:

    + +
    输入:nums = [3, 4, 4, 1, 2, 1]
    +输出:false
    +解释:因为数组的最大元素是 4 ,唯一可以构成这个数组的 base[n] 对应的 n = 4 。但是 base[n] 有 5 个元素而 nums 有 6 个元素。所以答案为 false 。
    +
    + +

     

    + +

    提示:

    + + + +## 解法 + + + + + +### **Python3** + + + +```python +class Solution: + def isGood(self, nums: List[int]) -> bool: + n = len(nums) - 1 + cnt = Counter(nums) + cnt[n] -= 2 + for i in range(1, n): + cnt[i] -= 1 + return all(v == 0 for v in cnt.values()) +``` + +### **Java** + + + +```java +class Solution { + public boolean isGood(int[] nums) { + int n = nums.length - 1; + int[] cnt = new int[201]; + for (int x : nums) { + ++cnt[x]; + } + cnt[n] -= 2; + for (int i = 1; i < n; ++i) { + cnt[i] -= 1; + } + for (int x : cnt) { + if (x != 0) { + return false; + } + } + return true; + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + bool isGood(vector& nums) { + int n = nums.size() - 1; + vector cnt(201); + for (int x : nums) { + ++cnt[x]; + } + cnt[n] -= 2; + for (int i = 1; i < n; ++i) { + --cnt[i]; + } + for (int x : cnt) { + if (x) { + return false; + } + } + return true; + } +}; +``` + +### **Go** + +```go +func isGood(nums []int) bool { + n := len(nums) - 1 + cnt := [201]int{} + for _, x := range nums { + cnt[x]++ + } + cnt[n] -= 2 + for i := 1; i < n; i++ { + cnt[i]-- + } + for _, x := range cnt { + if x != 0 { + return false + } + } + return true +} +``` + +### **TypeScript** + +```ts +function isGood(nums: number[]): boolean { + const n = nums.length - 1; + const cnt: number[] = new Array(201).fill(0); + for (const x of nums) { + ++cnt[x]; + } + cnt[n] -= 2; + for (let i = 1; i < n; ++i) { + cnt[i]--; + } + return cnt.every(x => x >= 0); +} +``` + +### **...** + +``` + +``` + + diff --git a/solution/2700-2799/2784.Check if Array is Good/README_EN.md b/solution/2700-2799/2784.Check if Array is Good/README_EN.md new file mode 100644 index 0000000000000..3aebc55b70478 --- /dev/null +++ b/solution/2700-2799/2784.Check if Array is Good/README_EN.md @@ -0,0 +1,165 @@ +# [2784. Check if Array is Good](https://leetcode.com/problems/check-if-array-is-good) + +[中文文档](/solution/2700-2799/2784.Check%20if%20Array%20is%20Good/README.md) + +## Description + +

    You are given an integer array nums. We consider an array good if it is a permutation of an array base[n].

    + +

    base[n] = [1, 2, ..., n - 1, n, n] (in other words, it is an array of length n + 1 which contains 1 to n - 1 exactly once, plus two occurrences of n). For example, base[1] = [1, 1] and base[3] = [1, 2, 3, 3].

    + +

    Return true if the given array is good, otherwise return false.

    + +

    Note: A permutation of integers represents an arrangement of these numbers.

    + +

     

    +

    Example 1:

    + +
    +Input: nums = [2, 1, 3]
    +Output: false
    +Explanation: Since the maximum element of the array is 3, the only candidate n for which this array could be a permutation of base[n], is n = 3. However, base[3] has four elements but array nums has three. Therefore, it can not be a permutation of base[3] = [1, 2, 3, 3]. So the answer is false.
    +
    + +

    Example 2:

    + +
    +Input: nums = [1, 3, 3, 2]
    +Output: true
    +Explanation: Since the maximum element of the array is 3, the only candidate n for which this array could be a permutation of base[n], is n = 3. It can be seen that nums is a permutation of base[3] = [1, 2, 3, 3] (by swapping the second and fourth elements in nums, we reach base[3]). Therefore, the answer is true.
    + +

    Example 3:

    + +
    +Input: nums = [1, 1]
    +Output: true
    +Explanation: Since the maximum element of the array is 1, the only candidate n for which this array could be a permutation of base[n], is n = 1. It can be seen that nums is a permutation of base[1] = [1, 1]. Therefore, the answer is true.
    + +

    Example 4:

    + +
    +Input: nums = [3, 4, 4, 1, 2, 1]
    +Output: false
    +Explanation: Since the maximum element of the array is 4, the only candidate n for which this array could be a permutation of base[n], is n = 4. However, base[4] has five elements but array nums has six. Therefore, it can not be a permutation of base[4] = [1, 2, 3, 4, 4]. So the answer is false.
    +
    + +

     

    +

    Constraints:

    + +
      +
    • 1 <= nums.length <= 100
    • +
    • 1 <= num[i] <= 200
    • +
    + +## Solutions + + + +### **Python3** + +```python +class Solution: + def isGood(self, nums: List[int]) -> bool: + n = len(nums) - 1 + cnt = Counter(nums) + cnt[n] -= 2 + for i in range(1, n): + cnt[i] -= 1 + return all(v == 0 for v in cnt.values()) +``` + +### **Java** + +```java +class Solution { + public boolean isGood(int[] nums) { + int n = nums.length - 1; + int[] cnt = new int[201]; + for (int x : nums) { + ++cnt[x]; + } + cnt[n] -= 2; + for (int i = 1; i < n; ++i) { + cnt[i] -= 1; + } + for (int x : cnt) { + if (x != 0) { + return false; + } + } + return true; + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + bool isGood(vector& nums) { + int n = nums.size() - 1; + vector cnt(201); + for (int x : nums) { + ++cnt[x]; + } + cnt[n] -= 2; + for (int i = 1; i < n; ++i) { + --cnt[i]; + } + for (int x : cnt) { + if (x) { + return false; + } + } + return true; + } +}; +``` + +### **Go** + +```go +func isGood(nums []int) bool { + n := len(nums) - 1 + cnt := [201]int{} + for _, x := range nums { + cnt[x]++ + } + cnt[n] -= 2 + for i := 1; i < n; i++ { + cnt[i]-- + } + for _, x := range cnt { + if x != 0 { + return false + } + } + return true +} +``` + +### **TypeScript** + +```ts +function isGood(nums: number[]): boolean { + const n = nums.length - 1; + const cnt: number[] = new Array(201).fill(0); + for (const x of nums) { + ++cnt[x]; + } + cnt[n] -= 2; + for (let i = 1; i < n; ++i) { + cnt[i]--; + } + return cnt.every(x => x >= 0); +} +``` + +### **...** + +``` + +``` + + diff --git a/solution/2700-2799/2784.Check if Array is Good/Solution.cpp b/solution/2700-2799/2784.Check if Array is Good/Solution.cpp new file mode 100644 index 0000000000000..1a57cf4f2d8af --- /dev/null +++ b/solution/2700-2799/2784.Check if Array is Good/Solution.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + bool isGood(vector& nums) { + int n = nums.size() - 1; + vector cnt(201); + for (int x : nums) { + ++cnt[x]; + } + cnt[n] -= 2; + for (int i = 1; i < n; ++i) { + --cnt[i]; + } + for (int x : cnt) { + if (x) { + return false; + } + } + return true; + } +}; \ No newline at end of file diff --git a/solution/2700-2799/2784.Check if Array is Good/Solution.go b/solution/2700-2799/2784.Check if Array is Good/Solution.go new file mode 100644 index 0000000000000..dfbe96f7a16b1 --- /dev/null +++ b/solution/2700-2799/2784.Check if Array is Good/Solution.go @@ -0,0 +1,17 @@ +func isGood(nums []int) bool { + n := len(nums) - 1 + cnt := [201]int{} + for _, x := range nums { + cnt[x]++ + } + cnt[n] -= 2 + for i := 1; i < n; i++ { + cnt[i]-- + } + for _, x := range cnt { + if x != 0 { + return false + } + } + return true +} \ No newline at end of file diff --git a/solution/2700-2799/2784.Check if Array is Good/Solution.java b/solution/2700-2799/2784.Check if Array is Good/Solution.java new file mode 100644 index 0000000000000..d75b4487f7407 --- /dev/null +++ b/solution/2700-2799/2784.Check if Array is Good/Solution.java @@ -0,0 +1,19 @@ +class Solution { + public boolean isGood(int[] nums) { + int n = nums.length - 1; + int[] cnt = new int[201]; + for (int x : nums) { + ++cnt[x]; + } + cnt[n] -= 2; + for (int i = 1; i < n; ++i) { + cnt[i] -= 1; + } + for (int x : cnt) { + if (x != 0) { + return false; + } + } + return true; + } +} \ No newline at end of file diff --git a/solution/2700-2799/2784.Check if Array is Good/Solution.py b/solution/2700-2799/2784.Check if Array is Good/Solution.py new file mode 100644 index 0000000000000..d7244504b3eb7 --- /dev/null +++ b/solution/2700-2799/2784.Check if Array is Good/Solution.py @@ -0,0 +1,8 @@ +class Solution: + def isGood(self, nums: List[int]) -> bool: + n = len(nums) - 1 + cnt = Counter(nums) + cnt[n] -= 2 + for i in range(1, n): + cnt[i] -= 1 + return all(v == 0 for v in cnt.values()) diff --git a/solution/2700-2799/2784.Check if Array is Good/Solution.ts b/solution/2700-2799/2784.Check if Array is Good/Solution.ts new file mode 100644 index 0000000000000..c954ffbdf362b --- /dev/null +++ b/solution/2700-2799/2784.Check if Array is Good/Solution.ts @@ -0,0 +1,12 @@ +function isGood(nums: number[]): boolean { + const n = nums.length - 1; + const cnt: number[] = new Array(201).fill(0); + for (const x of nums) { + ++cnt[x]; + } + cnt[n] -= 2; + for (let i = 1; i < n; ++i) { + cnt[i]--; + } + return cnt.every(x => x >= 0); +} diff --git a/solution/2700-2799/2785.Sort Vowels in a String/README.md b/solution/2700-2799/2785.Sort Vowels in a String/README.md new file mode 100644 index 0000000000000..edb9599ed0f8b --- /dev/null +++ b/solution/2700-2799/2785.Sort Vowels in a String/README.md @@ -0,0 +1,172 @@ +# [2785. 将字符串中的元音字母排序](https://leetcode.cn/problems/sort-vowels-in-a-string) + +[English Version](/solution/2700-2799/2785.Sort%20Vowels%20in%20a%20String/README_EN.md) + +## 题目描述 + + + +

    给你一个下标从 0 开始的字符串 s ,将 s 中的元素重新 排列 得到新的字符串 t ,它满足:

    + +
      +
    • 所有辅音字母都在原来的位置上。更正式的,如果满足 0 <= i < s.length 的下标 i 处的 s[i] 是个辅音字母,那么 t[i] = s[i] 。
    • +
    • 元音字母都必须以他们的 ASCII 值按 非递减 顺序排列。更正式的,对于满足 0 <= i < j < s.length 的下标 i 和 j  ,如果 s[i] 和 s[j] 都是元音字母,那么 t[i] 的 ASCII 值不能大于 t[j] 的 ASCII 值。
    • +
    + +

    请你返回结果字母串。

    + +

    元音字母为 'a' ,'e' ,'i' ,'o' 和 'u' ,它们可能是小写字母也可能是大写字母,辅音字母是除了这 5 个字母以外的所有字母。

    + +

     

    + +

    示例 1:

    + +
    +输入:s = "lEetcOde"
    +输出:"lEOtcede"
    +解释:'E' ,'O' 和 'e' 是 s 中的元音字母,'l' ,'t' ,'c' 和 'd' 是所有的辅音。将元音字母按照 ASCII 值排序,辅音字母留在原地。
    +
    + +

    示例 2:

    + +
    +输入:s = "lYmpH"
    +输出:"lYmpH"
    +解释:s 中没有元音字母(s 中都为辅音字母),所以我们返回 "lYmpH" 。
    +
    + +

     

    + +

    提示:

    + +
      +
    • 1 <= s.length <= 105
    • +
    • s 只包含英语字母表中的 大写 小写 字母。
    • +
    + +## 解法 + + + + + +### **Python3** + + + +```python +class Solution: + def sortVowels(self, s: str) -> str: + vs = [c for c in s if c.lower() in "aeiou"] + vs.sort() + cs = list(s) + j = 0 + for i, c in enumerate(cs): + if c.lower() in "aeiou": + cs[i] = vs[j] + j += 1 + return "".join(cs) +``` + +### **Java** + + + +```java +class Solution { + public String sortVowels(String s) { + List vs = new ArrayList<>(); + char[] cs = s.toCharArray(); + for (char c : cs) { + char d = Character.toLowerCase(c); + if (d == 'a' || d == 'e' || d == 'i' || d == 'o' || d == 'u') { + vs.add(c); + } + } + Collections.sort(vs); + for (int i = 0, j = 0; i < cs.length; ++i) { + char d = Character.toLowerCase(cs[i]); + if (d == 'a' || d == 'e' || d == 'i' || d == 'o' || d == 'u') { + cs[i] = vs.get(j++); + } + } + return String.valueOf(cs); + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + string sortVowels(string s) { + string vs; + for (auto c : s) { + char d = tolower(c); + if (d == 'a' || d == 'e' || d == 'i' || d == 'o' || d == 'u') { + vs.push_back(c); + } + } + sort(vs.begin(), vs.end()); + for (int i = 0, j = 0; i < s.size(); ++i) { + char d = tolower(s[i]); + if (d == 'a' || d == 'e' || d == 'i' || d == 'o' || d == 'u') { + s[i] = vs[j++]; + } + } + return s; + } +}; +``` + +### **Go** + +```go +func sortVowels(s string) string { + cs := []byte(s) + vs := []byte{} + for _, c := range cs { + d := c | 32 + if d == 'a' || d == 'e' || d == 'i' || d == 'o' || d == 'u' { + vs = append(vs, c) + } + } + sort.Slice(vs, func(i, j int) bool { return vs[i] < vs[j] }) + j := 0 + for i, c := range cs { + d := c | 32 + if d == 'a' || d == 'e' || d == 'i' || d == 'o' || d == 'u' { + cs[i] = vs[j] + j++ + } + } + return string(cs) +} +``` + +### **TypeScript** + +```ts +function sortVowels(s: string): string { + const vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']; + const vs = s + .split('') + .filter(c => vowels.includes(c)) + .sort(); + const ans: string[] = []; + let j = 0; + for (const c of s) { + ans.push(vowels.includes(c) ? vs[j++] : c); + } + return ans.join(''); +} +``` + +### **...** + +``` + +``` + + diff --git a/solution/2700-2799/2785.Sort Vowels in a String/README_EN.md b/solution/2700-2799/2785.Sort Vowels in a String/README_EN.md new file mode 100644 index 0000000000000..f049e61593892 --- /dev/null +++ b/solution/2700-2799/2785.Sort Vowels in a String/README_EN.md @@ -0,0 +1,162 @@ +# [2785. Sort Vowels in a String](https://leetcode.com/problems/sort-vowels-in-a-string) + +[中文文档](/solution/2700-2799/2785.Sort%20Vowels%20in%20a%20String/README.md) + +## Description + +

    Given a 0-indexed string s, permute s to get a new string t such that:

    + +
      +
    • All consonants remain in their original places. More formally, if there is an index i with 0 <= i < s.length such that s[i] is a consonant, then t[i] = s[i].
    • +
    • The vowels must be sorted in the nondecreasing order of their ASCII values. More formally, for pairs of indices i, j with 0 <= i < j < s.length such that s[i] and s[j] are vowels, then t[i] must not have a higher ASCII value than t[j].
    • +
    + +

    Return the resulting string.

    + +

    The vowels are 'a', 'e', 'i', 'o', and 'u', and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.

    + +

     

    +

    Example 1:

    + +
    +Input: s = "lEetcOde"
    +Output: "lEOtcede"
    +Explanation: 'E', 'O', and 'e' are the vowels in s; 'l', 't', 'c', and 'd' are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places.
    +
    + +

    Example 2:

    + +
    +Input: s = "lYmpH"
    +Output: "lYmpH"
    +Explanation: There are no vowels in s (all characters in s are consonants), so we return "lYmpH".
    +
    + +

     

    +

    Constraints:

    + +
      +
    • 1 <= s.length <= 105
    • +
    • s consists only of letters of the English alphabet in uppercase and lowercase.
    • +
    + +## Solutions + + + +### **Python3** + +```python +class Solution: + def sortVowels(self, s: str) -> str: + vs = [c for c in s if c.lower() in "aeiou"] + vs.sort() + cs = list(s) + j = 0 + for i, c in enumerate(cs): + if c.lower() in "aeiou": + cs[i] = vs[j] + j += 1 + return "".join(cs) +``` + +### **Java** + +```java +class Solution { + public String sortVowels(String s) { + List vs = new ArrayList<>(); + char[] cs = s.toCharArray(); + for (char c : cs) { + char d = Character.toLowerCase(c); + if (d == 'a' || d == 'e' || d == 'i' || d == 'o' || d == 'u') { + vs.add(c); + } + } + Collections.sort(vs); + for (int i = 0, j = 0; i < cs.length; ++i) { + char d = Character.toLowerCase(cs[i]); + if (d == 'a' || d == 'e' || d == 'i' || d == 'o' || d == 'u') { + cs[i] = vs.get(j++); + } + } + return String.valueOf(cs); + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + string sortVowels(string s) { + string vs; + for (auto c : s) { + char d = tolower(c); + if (d == 'a' || d == 'e' || d == 'i' || d == 'o' || d == 'u') { + vs.push_back(c); + } + } + sort(vs.begin(), vs.end()); + for (int i = 0, j = 0; i < s.size(); ++i) { + char d = tolower(s[i]); + if (d == 'a' || d == 'e' || d == 'i' || d == 'o' || d == 'u') { + s[i] = vs[j++]; + } + } + return s; + } +}; +``` + +### **Go** + +```go +func sortVowels(s string) string { + cs := []byte(s) + vs := []byte{} + for _, c := range cs { + d := c | 32 + if d == 'a' || d == 'e' || d == 'i' || d == 'o' || d == 'u' { + vs = append(vs, c) + } + } + sort.Slice(vs, func(i, j int) bool { return vs[i] < vs[j] }) + j := 0 + for i, c := range cs { + d := c | 32 + if d == 'a' || d == 'e' || d == 'i' || d == 'o' || d == 'u' { + cs[i] = vs[j] + j++ + } + } + return string(cs) +} +``` + +### **TypeScript** + +```ts +function sortVowels(s: string): string { + const vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']; + const vs = s + .split('') + .filter(c => vowels.includes(c)) + .sort(); + const ans: string[] = []; + let j = 0; + for (const c of s) { + ans.push(vowels.includes(c) ? vs[j++] : c); + } + return ans.join(''); +} +``` + +### **...** + +``` + +``` + + diff --git a/solution/2700-2799/2785.Sort Vowels in a String/Solution.cpp b/solution/2700-2799/2785.Sort Vowels in a String/Solution.cpp new file mode 100644 index 0000000000000..0c95df92496ec --- /dev/null +++ b/solution/2700-2799/2785.Sort Vowels in a String/Solution.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + string sortVowels(string s) { + string vs; + for (auto c : s) { + char d = tolower(c); + if (d == 'a' || d == 'e' || d == 'i' || d == 'o' || d == 'u') { + vs.push_back(c); + } + } + sort(vs.begin(), vs.end()); + for (int i = 0, j = 0; i < s.size(); ++i) { + char d = tolower(s[i]); + if (d == 'a' || d == 'e' || d == 'i' || d == 'o' || d == 'u') { + s[i] = vs[j++]; + } + } + return s; + } +}; \ No newline at end of file diff --git a/solution/2700-2799/2785.Sort Vowels in a String/Solution.go b/solution/2700-2799/2785.Sort Vowels in a String/Solution.go new file mode 100644 index 0000000000000..12122cff094a9 --- /dev/null +++ b/solution/2700-2799/2785.Sort Vowels in a String/Solution.go @@ -0,0 +1,20 @@ +func sortVowels(s string) string { + cs := []byte(s) + vs := []byte{} + for _, c := range cs { + d := c | 32 + if d == 'a' || d == 'e' || d == 'i' || d == 'o' || d == 'u' { + vs = append(vs, c) + } + } + sort.Slice(vs, func(i, j int) bool { return vs[i] < vs[j] }) + j := 0 + for i, c := range cs { + d := c | 32 + if d == 'a' || d == 'e' || d == 'i' || d == 'o' || d == 'u' { + cs[i] = vs[j] + j++ + } + } + return string(cs) +} \ No newline at end of file diff --git a/solution/2700-2799/2785.Sort Vowels in a String/Solution.java b/solution/2700-2799/2785.Sort Vowels in a String/Solution.java new file mode 100644 index 0000000000000..3a2e65271dc53 --- /dev/null +++ b/solution/2700-2799/2785.Sort Vowels in a String/Solution.java @@ -0,0 +1,20 @@ +class Solution { + public String sortVowels(String s) { + List vs = new ArrayList<>(); + char[] cs = s.toCharArray(); + for (char c : cs) { + char d = Character.toLowerCase(c); + if (d == 'a' || d == 'e' || d == 'i' || d == 'o' || d == 'u') { + vs.add(c); + } + } + Collections.sort(vs); + for (int i = 0, j = 0; i < cs.length; ++i) { + char d = Character.toLowerCase(cs[i]); + if (d == 'a' || d == 'e' || d == 'i' || d == 'o' || d == 'u') { + cs[i] = vs.get(j++); + } + } + return String.valueOf(cs); + } +} \ No newline at end of file diff --git a/solution/2700-2799/2785.Sort Vowels in a String/Solution.py b/solution/2700-2799/2785.Sort Vowels in a String/Solution.py new file mode 100644 index 0000000000000..d61e830ed3036 --- /dev/null +++ b/solution/2700-2799/2785.Sort Vowels in a String/Solution.py @@ -0,0 +1,11 @@ +class Solution: + def sortVowels(self, s: str) -> str: + vs = [c for c in s if c.lower() in "aeiou"] + vs.sort() + cs = list(s) + j = 0 + for i, c in enumerate(cs): + if c.lower() in "aeiou": + cs[i] = vs[j] + j += 1 + return "".join(cs) diff --git a/solution/2700-2799/2785.Sort Vowels in a String/Solution.ts b/solution/2700-2799/2785.Sort Vowels in a String/Solution.ts new file mode 100644 index 0000000000000..ca2beb5def2fa --- /dev/null +++ b/solution/2700-2799/2785.Sort Vowels in a String/Solution.ts @@ -0,0 +1,13 @@ +function sortVowels(s: string): string { + const vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']; + const vs = s + .split('') + .filter(c => vowels.includes(c)) + .sort(); + const ans: string[] = []; + let j = 0; + for (const c of s) { + ans.push(vowels.includes(c) ? vs[j++] : c); + } + return ans.join(''); +} diff --git a/solution/2700-2799/2786.Visit Array Positions to Maximize Score/README.md b/solution/2700-2799/2786.Visit Array Positions to Maximize Score/README.md new file mode 100644 index 0000000000000..a6e6395bd8273 --- /dev/null +++ b/solution/2700-2799/2786.Visit Array Positions to Maximize Score/README.md @@ -0,0 +1,151 @@ +# [2786. 访问数组中的位置使分数最大](https://leetcode.cn/problems/visit-array-positions-to-maximize-score) + +[English Version](/solution/2700-2799/2786.Visit%20Array%20Positions%20to%20Maximize%20Score/README_EN.md) + +## 题目描述 + + + +

    给你一个下标从 0 开始的整数数组 nums 和一个正整数 x 。

    + +

    一开始 在数组的位置 0 处,你可以按照下述规则访问数组中的其他位置:

    + +
      +
    • 如果你当前在位置 i ,那么你可以移动到满足 i < j 的 任意 位置 j 。
    • +
    • 对于你访问的位置 i ,你可以获得分数 nums[i] 。
    • +
    • 如果你从位置 i 移动到位置 j 且 nums[i] 和 nums[j] 的 奇偶性 不同,那么你将失去分数 x 。
    • +
    + +

    请你返回你能得到的 最大 得分之和。

    + +

    注意 ,你一开始的分数为 nums[0] 。

    + +

     

    + +

    示例 1:

    + +
    输入:nums = [2,3,6,1,9,2], x = 5
    +输出:13
    +解释:我们可以按顺序访问数组中的位置:0 -> 2 -> 3 -> 4 。
    +对应位置的值为 2 ,6 ,1 和 9 。因为 6 和 1 的奇偶性不同,所以下标从 2 -> 3 让你失去 x = 5 分。
    +总得分为:2 + 6 + 1 + 9 - 5 = 13 。
    +
    + +

    示例 2:

    + +
    输入:nums = [2,4,6,8], x = 3
    +输出:20
    +解释:数组中的所有元素奇偶性都一样,所以我们可以将每个元素都访问一次,而且不会失去任何分数。
    +总得分为:2 + 4 + 6 + 8 = 20 。
    +
    + +

     

    + +

    提示:

    + +
      +
    • 2 <= nums.length <= 105
    • +
    • 1 <= nums[i], x <= 106
    • +
    + +## 解法 + + + + + +### **Python3** + + + +```python +class Solution: + def maxScore(self, nums: List[int], x: int) -> int: + f = [-inf] * 2 + f[nums[0] & 1] = nums[0] + for v in nums[1:]: + f[v & 1] = max(f[v & 1] + v, f[v & 1 ^ 1] + v - x) + return max(f) +``` + +### **Java** + + + +```java +class Solution { + public long maxScore(int[] nums, int x) { + long[] f = new long[2]; + Arrays.fill(f, -(1L << 60)); + f[nums[0] & 1] = nums[0]; + for (int i = 1; i < nums.length; ++i) { + f[nums[i] & 1] = Math.max(f[nums[i] & 1] + nums[i], f[nums[i] & 1 ^ 1] + nums[i] - x); + } + return Math.max(f[0], f[1]); + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + long long maxScore(vector& nums, int x) { + const long long inf = 1LL << 60; + vector f(2, -inf); + f[nums[0] & 1] = nums[0]; + int n = nums.size(); + for (int i = 1; i < n; ++i) { + f[nums[i] & 1] = max(f[nums[i] & 1] + nums[i], f[nums[i] & 1 ^ 1] + nums[i] - x); + } + return max(f[0], f[1]); + } +}; +``` + +### **Go** + +```go +func maxScore(nums []int, x int) int64 { + const inf int = 1 << 40 + f := [2]int{-inf, -inf} + f[nums[0]&1] = nums[0] + for _, v := range nums[1:] { + f[v&1] = max(f[v&1]+v, f[v&1^1]+v-x) + } + return int64(max(f[0], f[1])) +} + +func max(a, b int) int { + if a > b { + return a + } + return b +} +``` + +### **TypeScript** + +```ts +function maxScore(nums: number[], x: number): number { + const inf = 1 << 30; + const f: number[] = Array(2).fill(-inf); + f[nums[0] & 1] = nums[0]; + for (let i = 1; i < nums.length; ++i) { + f[nums[i] & 1] = Math.max( + f[nums[i] & 1] + nums[i], + f[(nums[i] & 1) ^ 1] + nums[i] - x, + ); + } + return Math.max(f[0], f[1]); +} +``` + +### **...** + +``` + +``` + + diff --git a/solution/2700-2799/2786.Visit Array Positions to Maximize Score/README_EN.md b/solution/2700-2799/2786.Visit Array Positions to Maximize Score/README_EN.md new file mode 100644 index 0000000000000..dd44665d3a91e --- /dev/null +++ b/solution/2700-2799/2786.Visit Array Positions to Maximize Score/README_EN.md @@ -0,0 +1,143 @@ +# [2786. Visit Array Positions to Maximize Score](https://leetcode.com/problems/visit-array-positions-to-maximize-score) + +[中文文档](/solution/2700-2799/2786.Visit%20Array%20Positions%20to%20Maximize%20Score/README.md) + +## Description + +

    You are given a 0-indexed integer array nums and a positive integer x.

    + +

    You are initially at position 0 in the array and you can visit other positions according to the following rules:

    + +
      +
    • If you are currently in position i, then you can move to any position j such that i < j.
    • +
    • For each position i that you visit, you get a score of nums[i].
    • +
    • If you move from a position i to a position j and the parities of nums[i] and nums[j] differ, then you lose a score of x.
    • +
    + +

    Return the maximum total score you can get.

    + +

    Note that initially you have nums[0] points.

    + +

     

    +

    Example 1:

    + +
    +Input: nums = [2,3,6,1,9,2], x = 5
    +Output: 13
    +Explanation: We can visit the following positions in the array: 0 -> 2 -> 3 -> 4.
    +The corresponding values are 2, 6, 1 and 9. Since the integers 6 and 1 have different parities, the move 2 -> 3 will make you lose a score of x = 5.
    +The total score will be: 2 + 6 + 1 + 9 - 5 = 13.
    +
    + +

    Example 2:

    + +
    +Input: nums = [2,4,6,8], x = 3
    +Output: 20
    +Explanation: All the integers in the array have the same parities, so we can visit all of them without losing any score.
    +The total score is: 2 + 4 + 6 + 8 = 20.
    +
    + +

     

    +

    Constraints:

    + +
      +
    • 2 <= nums.length <= 105
    • +
    • 1 <= nums[i], x <= 106
    • +
    + +## Solutions + + + +### **Python3** + +```python +class Solution: + def maxScore(self, nums: List[int], x: int) -> int: + f = [-inf] * 2 + f[nums[0] & 1] = nums[0] + for v in nums[1:]: + f[v & 1] = max(f[v & 1] + v, f[v & 1 ^ 1] + v - x) + return max(f) +``` + +### **Java** + +```java +class Solution { + public long maxScore(int[] nums, int x) { + long[] f = new long[2]; + Arrays.fill(f, -(1L << 60)); + f[nums[0] & 1] = nums[0]; + for (int i = 1; i < nums.length; ++i) { + f[nums[i] & 1] = Math.max(f[nums[i] & 1] + nums[i], f[nums[i] & 1 ^ 1] + nums[i] - x); + } + return Math.max(f[0], f[1]); + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + long long maxScore(vector& nums, int x) { + const long long inf = 1LL << 60; + vector f(2, -inf); + f[nums[0] & 1] = nums[0]; + int n = nums.size(); + for (int i = 1; i < n; ++i) { + f[nums[i] & 1] = max(f[nums[i] & 1] + nums[i], f[nums[i] & 1 ^ 1] + nums[i] - x); + } + return max(f[0], f[1]); + } +}; +``` + +### **Go** + +```go +func maxScore(nums []int, x int) int64 { + const inf int = 1 << 40 + f := [2]int{-inf, -inf} + f[nums[0]&1] = nums[0] + for _, v := range nums[1:] { + f[v&1] = max(f[v&1]+v, f[v&1^1]+v-x) + } + return int64(max(f[0], f[1])) +} + +func max(a, b int) int { + if a > b { + return a + } + return b +} +``` + +### **TypeScript** + +```ts +function maxScore(nums: number[], x: number): number { + const inf = 1 << 30; + const f: number[] = Array(2).fill(-inf); + f[nums[0] & 1] = nums[0]; + for (let i = 1; i < nums.length; ++i) { + f[nums[i] & 1] = Math.max( + f[nums[i] & 1] + nums[i], + f[(nums[i] & 1) ^ 1] + nums[i] - x, + ); + } + return Math.max(f[0], f[1]); +} +``` + +### **...** + +``` + +``` + + diff --git a/solution/2700-2799/2786.Visit Array Positions to Maximize Score/Solution.cpp b/solution/2700-2799/2786.Visit Array Positions to Maximize Score/Solution.cpp new file mode 100644 index 0000000000000..f46acc1a46c0e --- /dev/null +++ b/solution/2700-2799/2786.Visit Array Positions to Maximize Score/Solution.cpp @@ -0,0 +1,13 @@ +class Solution { +public: + long long maxScore(vector& nums, int x) { + const long long inf = 1LL << 60; + vector f(2, -inf); + f[nums[0] & 1] = nums[0]; + int n = nums.size(); + for (int i = 1; i < n; ++i) { + f[nums[i] & 1] = max(f[nums[i] & 1] + nums[i], f[nums[i] & 1 ^ 1] + nums[i] - x); + } + return max(f[0], f[1]); + } +}; \ No newline at end of file diff --git a/solution/2700-2799/2786.Visit Array Positions to Maximize Score/Solution.go b/solution/2700-2799/2786.Visit Array Positions to Maximize Score/Solution.go new file mode 100644 index 0000000000000..d901cf3a038f5 --- /dev/null +++ b/solution/2700-2799/2786.Visit Array Positions to Maximize Score/Solution.go @@ -0,0 +1,16 @@ +func maxScore(nums []int, x int) int64 { + const inf int = 1 << 40 + f := [2]int{-inf, -inf} + f[nums[0]&1] = nums[0] + for _, v := range nums[1:] { + f[v&1] = max(f[v&1]+v, f[v&1^1]+v-x) + } + return int64(max(f[0], f[1])) +} + +func max(a, b int) int { + if a > b { + return a + } + return b +} \ No newline at end of file diff --git a/solution/2700-2799/2786.Visit Array Positions to Maximize Score/Solution.java b/solution/2700-2799/2786.Visit Array Positions to Maximize Score/Solution.java new file mode 100644 index 0000000000000..8cc079c29be30 --- /dev/null +++ b/solution/2700-2799/2786.Visit Array Positions to Maximize Score/Solution.java @@ -0,0 +1,11 @@ +class Solution { + public long maxScore(int[] nums, int x) { + long[] f = new long[2]; + Arrays.fill(f, -(1L << 60)); + f[nums[0] & 1] = nums[0]; + for (int i = 1; i < nums.length; ++i) { + f[nums[i] & 1] = Math.max(f[nums[i] & 1] + nums[i], f[nums[i] & 1 ^ 1] + nums[i] - x); + } + return Math.max(f[0], f[1]); + } +} \ No newline at end of file diff --git a/solution/2700-2799/2786.Visit Array Positions to Maximize Score/Solution.py b/solution/2700-2799/2786.Visit Array Positions to Maximize Score/Solution.py new file mode 100644 index 0000000000000..71aff1444cc22 --- /dev/null +++ b/solution/2700-2799/2786.Visit Array Positions to Maximize Score/Solution.py @@ -0,0 +1,7 @@ +class Solution: + def maxScore(self, nums: List[int], x: int) -> int: + f = [-inf] * 2 + f[nums[0] & 1] = nums[0] + for v in nums[1:]: + f[v & 1] = max(f[v & 1] + v, f[v & 1 ^ 1] + v - x) + return max(f) diff --git a/solution/2700-2799/2786.Visit Array Positions to Maximize Score/Solution.ts b/solution/2700-2799/2786.Visit Array Positions to Maximize Score/Solution.ts new file mode 100644 index 0000000000000..442b93615e52d --- /dev/null +++ b/solution/2700-2799/2786.Visit Array Positions to Maximize Score/Solution.ts @@ -0,0 +1,12 @@ +function maxScore(nums: number[], x: number): number { + const inf = 1 << 30; + const f: number[] = Array(2).fill(-inf); + f[nums[0] & 1] = nums[0]; + for (let i = 1; i < nums.length; ++i) { + f[nums[i] & 1] = Math.max( + f[nums[i] & 1] + nums[i], + f[(nums[i] & 1) ^ 1] + nums[i] - x, + ); + } + return Math.max(f[0], f[1]); +} diff --git a/solution/2700-2799/2787.Ways to Express an Integer as Sum of Powers/README.md b/solution/2700-2799/2787.Ways to Express an Integer as Sum of Powers/README.md new file mode 100644 index 0000000000000..715e4873e087a --- /dev/null +++ b/solution/2700-2799/2787.Ways to Express an Integer as Sum of Powers/README.md @@ -0,0 +1,169 @@ +# [2787. 将一个数字表示成幂的和的方案数](https://leetcode.cn/problems/ways-to-express-an-integer-as-sum-of-powers) + +[English Version](/solution/2700-2799/2787.Ways%20to%20Express%20an%20Integer%20as%20Sum%20of%20Powers/README_EN.md) + +## 题目描述 + + + +

    给你两个  整数 n 和 x 。

    + +

    请你返回将 n 表示成一些 互不相同 正整数的 x 次幂之和的方案数。换句话说,你需要返回互不相同整数 [n1, n2, ..., nk] 的集合数目,满足 n = n1x + n2x + ... + nkx 。

    + +

    由于答案可能非常大,请你将它对 109 + 7 取余后返回。

    + +

    比方说,n = 160 且 x = 3 ,一个表示 n 的方法是 n = 23 + 33 + 53 

    + +

     

    + +

    示例 1:

    + +
    输入:n = 10, x = 2
    +输出:1
    +解释:我们可以将 n 表示为:n = 32 + 12 = 10 。
    +这是唯一将 10 表达成不同整数 2 次方之和的方案。
    +
    + +

    示例 2:

    + +
    输入:n = 4, x = 1
    +输出:2
    +解释:我们可以将 n 按以下方案表示:
    +- n = 41 = 4 。
    +- n = 31 + 11 = 4 。
    +
    + +

     

    + +

    提示:

    + +
      +
    • 1 <= n <= 300
    • +
    • 1 <= x <= 5
    • +
    + +## 解法 + + + + + +### **Python3** + + + +```python +class Solution: + def numberOfWays(self, n: int, x: int) -> int: + mod = 10**9 + 7 + f = [[0] * (n + 1) for _ in range(n + 1)] + f[0][0] = 1 + for i in range(1, n + 1): + k = pow(i, x) + for j in range(n + 1): + f[i][j] = f[i - 1][j] + if k <= j: + f[i][j] = (f[i][j] + f[i - 1][j - k]) % mod + return f[n][n] +``` + +### **Java** + + + +```java +class Solution { + public int numberOfWays(int n, int x) { + final int mod = (int) 1e9 + 7; + int[][] f = new int[n + 1][n + 1]; + f[0][0] = 1; + for (int i = 1; i <= n; ++i) { + long k = (long) Math.pow(i, x); + for (int j = 0; j <= n; ++j) { + f[i][j] = f[i - 1][j]; + if (k <= j) { + f[i][j] = (f[i][j] + f[i - 1][j - (int) k]) % mod; + } + } + } + return f[n][n]; + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + int numberOfWays(int n, int x) { + const int mod = 1e9 + 7; + int f[n + 1][n + 1]; + memset(f, 0, sizeof(f)); + f[0][0] = 1; + for (int i = 1; i <= n; ++i) { + long long k = (long long) pow(i, x); + for (int j = 0; j <= n; ++j) { + f[i][j] = f[i - 1][j]; + if (k <= j) { + f[i][j] = (f[i][j] + f[i - 1][j - k]) % mod; + } + } + } + return f[n][n]; + } +}; +``` + +### **Go** + +```go +func numberOfWays(n int, x int) int { + const mod int = 1e9 + 7 + f := make([][]int, n+1) + for i := range f { + f[i] = make([]int, n+1) + } + f[0][0] = 1 + for i := 1; i <= n; i++ { + k := int(math.Pow(float64(i), float64(x))) + for j := 0; j <= n; j++ { + f[i][j] = f[i-1][j] + if k <= j { + f[i][j] = (f[i][j] + f[i-1][j-k]) % mod + } + } + } + return f[n][n] +} +``` + +### **TypeScript** + +```ts +function numberOfWays(n: number, x: number): number { + const mod = 10 ** 9 + 7; + const f: number[][] = Array(n + 1) + .fill(0) + .map(() => Array(n + 1).fill(0)); + f[0][0] = 1; + for (let i = 1; i <= n; ++i) { + const k = Math.pow(i, x); + for (let j = 0; j <= n; ++j) { + f[i][j] = f[i - 1][j]; + if (k <= j) { + f[i][j] = (f[i][j] + f[i - 1][j - k]) % mod; + } + } + } + return f[n][n]; +} +``` + +### **...** + +``` + +``` + + diff --git a/solution/2700-2799/2787.Ways to Express an Integer as Sum of Powers/README_EN.md b/solution/2700-2799/2787.Ways to Express an Integer as Sum of Powers/README_EN.md new file mode 100644 index 0000000000000..b301e42aaaf34 --- /dev/null +++ b/solution/2700-2799/2787.Ways to Express an Integer as Sum of Powers/README_EN.md @@ -0,0 +1,161 @@ +# [2787. Ways to Express an Integer as Sum of Powers](https://leetcode.com/problems/ways-to-express-an-integer-as-sum-of-powers) + +[中文文档](/solution/2700-2799/2787.Ways%20to%20Express%20an%20Integer%20as%20Sum%20of%20Powers/README.md) + +## Description + +

    Given two positive integers n and x.

    + +

    Return the number of ways n can be expressed as the sum of the xth power of unique positive integers, in other words, the number of sets of unique integers [n1, n2, ..., nk] where n = n1x + n2x + ... + nkx.

    + +

    Since the result can be very large, return it modulo 109 + 7.

    + +

    For example, if n = 160 and x = 3, one way to express n is n = 23 + 33 + 53.

    + +

     

    +

    Example 1:

    + +
    +Input: n = 10, x = 2
    +Output: 1
    +Explanation: We can express n as the following: n = 32 + 12 = 10.
    +It can be shown that it is the only way to express 10 as the sum of the 2nd power of unique integers.
    +
    + +

    Example 2:

    + +
    +Input: n = 4, x = 1
    +Output: 2
    +Explanation: We can express n in the following ways:
    +- n = 41 = 4.
    +- n = 31 + 11 = 4.
    +
    + +

     

    +

    Constraints:

    + +
      +
    • 1 <= n <= 300
    • +
    • 1 <= x <= 5
    • +
    + +## Solutions + + + +### **Python3** + +```python +class Solution: + def numberOfWays(self, n: int, x: int) -> int: + mod = 10**9 + 7 + f = [[0] * (n + 1) for _ in range(n + 1)] + f[0][0] = 1 + for i in range(1, n + 1): + k = pow(i, x) + for j in range(n + 1): + f[i][j] = f[i - 1][j] + if k <= j: + f[i][j] = (f[i][j] + f[i - 1][j - k]) % mod + return f[n][n] +``` + +### **Java** + +```java +class Solution { + public int numberOfWays(int n, int x) { + final int mod = (int) 1e9 + 7; + int[][] f = new int[n + 1][n + 1]; + f[0][0] = 1; + for (int i = 1; i <= n; ++i) { + long k = (long) Math.pow(i, x); + for (int j = 0; j <= n; ++j) { + f[i][j] = f[i - 1][j]; + if (k <= j) { + f[i][j] = (f[i][j] + f[i - 1][j - (int) k]) % mod; + } + } + } + return f[n][n]; + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + int numberOfWays(int n, int x) { + const int mod = 1e9 + 7; + int f[n + 1][n + 1]; + memset(f, 0, sizeof(f)); + f[0][0] = 1; + for (int i = 1; i <= n; ++i) { + long long k = (long long) pow(i, x); + for (int j = 0; j <= n; ++j) { + f[i][j] = f[i - 1][j]; + if (k <= j) { + f[i][j] = (f[i][j] + f[i - 1][j - k]) % mod; + } + } + } + return f[n][n]; + } +}; +``` + +### **Go** + +```go +func numberOfWays(n int, x int) int { + const mod int = 1e9 + 7 + f := make([][]int, n+1) + for i := range f { + f[i] = make([]int, n+1) + } + f[0][0] = 1 + for i := 1; i <= n; i++ { + k := int(math.Pow(float64(i), float64(x))) + for j := 0; j <= n; j++ { + f[i][j] = f[i-1][j] + if k <= j { + f[i][j] = (f[i][j] + f[i-1][j-k]) % mod + } + } + } + return f[n][n] +} +``` + +### **TypeScript** + +```ts +function numberOfWays(n: number, x: number): number { + const mod = 10 ** 9 + 7; + const f: number[][] = Array(n + 1) + .fill(0) + .map(() => Array(n + 1).fill(0)); + f[0][0] = 1; + for (let i = 1; i <= n; ++i) { + const k = Math.pow(i, x); + for (let j = 0; j <= n; ++j) { + f[i][j] = f[i - 1][j]; + if (k <= j) { + f[i][j] = (f[i][j] + f[i - 1][j - k]) % mod; + } + } + } + return f[n][n]; +} +``` + +### **...** + +``` + +``` + + diff --git a/solution/2700-2799/2787.Ways to Express an Integer as Sum of Powers/Solution.cpp b/solution/2700-2799/2787.Ways to Express an Integer as Sum of Powers/Solution.cpp new file mode 100644 index 0000000000000..3c8513068aea9 --- /dev/null +++ b/solution/2700-2799/2787.Ways to Express an Integer as Sum of Powers/Solution.cpp @@ -0,0 +1,19 @@ +class Solution { +public: + int numberOfWays(int n, int x) { + const int mod = 1e9 + 7; + int f[n + 1][n + 1]; + memset(f, 0, sizeof(f)); + f[0][0] = 1; + for (int i = 1; i <= n; ++i) { + long long k = (long long) pow(i, x); + for (int j = 0; j <= n; ++j) { + f[i][j] = f[i - 1][j]; + if (k <= j) { + f[i][j] = (f[i][j] + f[i - 1][j - k]) % mod; + } + } + } + return f[n][n]; + } +}; \ No newline at end of file diff --git a/solution/2700-2799/2787.Ways to Express an Integer as Sum of Powers/Solution.go b/solution/2700-2799/2787.Ways to Express an Integer as Sum of Powers/Solution.go new file mode 100644 index 0000000000000..1942e32df63ab --- /dev/null +++ b/solution/2700-2799/2787.Ways to Express an Integer as Sum of Powers/Solution.go @@ -0,0 +1,18 @@ +func numberOfWays(n int, x int) int { + const mod int = 1e9 + 7 + f := make([][]int, n+1) + for i := range f { + f[i] = make([]int, n+1) + } + f[0][0] = 1 + for i := 1; i <= n; i++ { + k := int(math.Pow(float64(i), float64(x))) + for j := 0; j <= n; j++ { + f[i][j] = f[i-1][j] + if k <= j { + f[i][j] = (f[i][j] + f[i-1][j-k]) % mod + } + } + } + return f[n][n] +} \ No newline at end of file diff --git a/solution/2700-2799/2787.Ways to Express an Integer as Sum of Powers/Solution.java b/solution/2700-2799/2787.Ways to Express an Integer as Sum of Powers/Solution.java new file mode 100644 index 0000000000000..9849c4c98145e --- /dev/null +++ b/solution/2700-2799/2787.Ways to Express an Integer as Sum of Powers/Solution.java @@ -0,0 +1,17 @@ +class Solution { + public int numberOfWays(int n, int x) { + final int mod = (int) 1e9 + 7; + int[][] f = new int[n + 1][n + 1]; + f[0][0] = 1; + for (int i = 1; i <= n; ++i) { + long k = (long) Math.pow(i, x); + for (int j = 0; j <= n; ++j) { + f[i][j] = f[i - 1][j]; + if (k <= j) { + f[i][j] = (f[i][j] + f[i - 1][j - (int) k]) % mod; + } + } + } + return f[n][n]; + } +} \ No newline at end of file diff --git a/solution/2700-2799/2787.Ways to Express an Integer as Sum of Powers/Solution.py b/solution/2700-2799/2787.Ways to Express an Integer as Sum of Powers/Solution.py new file mode 100644 index 0000000000000..11962c877430d --- /dev/null +++ b/solution/2700-2799/2787.Ways to Express an Integer as Sum of Powers/Solution.py @@ -0,0 +1,12 @@ +class Solution: + def numberOfWays(self, n: int, x: int) -> int: + mod = 10**9 + 7 + f = [[0] * (n + 1) for _ in range(n + 1)] + f[0][0] = 1 + for i in range(1, n + 1): + k = pow(i, x) + for j in range(n + 1): + f[i][j] = f[i - 1][j] + if k <= j: + f[i][j] = (f[i][j] + f[i - 1][j - k]) % mod + return f[n][n] diff --git a/solution/2700-2799/2787.Ways to Express an Integer as Sum of Powers/Solution.ts b/solution/2700-2799/2787.Ways to Express an Integer as Sum of Powers/Solution.ts new file mode 100644 index 0000000000000..9c7c4bc3290a5 --- /dev/null +++ b/solution/2700-2799/2787.Ways to Express an Integer as Sum of Powers/Solution.ts @@ -0,0 +1,17 @@ +function numberOfWays(n: number, x: number): number { + const mod = 10 ** 9 + 7; + const f: number[][] = Array(n + 1) + .fill(0) + .map(() => Array(n + 1).fill(0)); + f[0][0] = 1; + for (let i = 1; i <= n; ++i) { + const k = Math.pow(i, x); + for (let j = 0; j <= n; ++j) { + f[i][j] = f[i - 1][j]; + if (k <= j) { + f[i][j] = (f[i][j] + f[i - 1][j - k]) % mod; + } + } + } + return f[n][n]; +} diff --git a/solution/2700-2799/2788.Split Strings by Separator/README.md b/solution/2700-2799/2788.Split Strings by Separator/README.md new file mode 100644 index 0000000000000..7ed09457bad0e --- /dev/null +++ b/solution/2700-2799/2788.Split Strings by Separator/README.md @@ -0,0 +1,171 @@ +# [2788. 按分隔符拆分字符串](https://leetcode.cn/problems/split-strings-by-separator) + +[English Version](/solution/2700-2799/2788.Split%20Strings%20by%20Separator/README_EN.md) + +## 题目描述 + + + +

    给你一个字符串数组 words 和一个字符 separator ,请你按 separator 拆分 words 中的每个字符串。

    + +

    返回一个由拆分后的新字符串组成的字符串数组,不包括空字符串

    + +

    注意

    + +
      +
    • separator 用于决定拆分发生的位置,但它不包含在结果字符串中。
    • +
    • 拆分可能形成两个以上的字符串。
    • +
    • 结果字符串必须保持初始相同的先后顺序。
    • +
    + +

     

    + +

    示例 1:

    + +
    +输入:words = ["one.two.three","four.five","six"], separator = "."
    +输出:["one","two","three","four","five","six"]
    +解释:在本示例中,我们进行下述拆分:
    +
    +"one.two.three" 拆分为 "one", "two", "three"
    +"four.five" 拆分为 "four", "five"
    +"six" 拆分为 "six" 
    +
    +因此,结果数组为 ["one","two","three","four","five","six"] 。
    + +

    示例 2:

    + +
    +输入:words = ["$easy$","$problem$"], separator = "$"
    +输出:["easy","problem"]
    +解释:在本示例中,我们进行下述拆分:
    +
    +"$easy$" 拆分为 "easy"(不包括空字符串)
    +"$problem$" 拆分为 "problem"(不包括空字符串)
    +
    +因此,结果数组为 ["easy","problem"] 。
    +
    + +

    示例 3:

    + +
    +输入:words = ["|||"], separator = "|"
    +输出:[]
    +解释:在本示例中,"|||" 的拆分结果将只包含一些空字符串,所以我们返回一个空数组 [] 。 
    + +

     

    + +

    提示:

    + +
      +
    • 1 <= words.length <= 100
    • +
    • 1 <= words[i].length <= 20
    • +
    • words[i] 中的字符要么是小写英文字母,要么就是字符串 ".,|$#@" 中的字符(不包括引号)
    • +
    • separator 是字符串 ".,|$#@" 中的某个字符(不包括引号)
    • +
    + +## 解法 + + + + + +### **Python3** + + + +```python +class Solution: + def splitWordsBySeparator(self, words: List[str], separator: str) -> List[str]: + return [s for w in words for s in w.split(separator) if s] +``` + +### **Java** + + + +```java +import java.util.regex.Pattern; + +class Solution { + public List splitWordsBySeparator(List words, char separator) { + List ans = new ArrayList<>(); + for (var w : words) { + for (var s : w.split(Pattern.quote(String.valueOf(separator)))) { + if (s.length() > 0) { + ans.add(s); + } + } + } + return ans; + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + vector splitWordsBySeparator(vector& words, char separator) { + vector ans; + for (auto& w : words) { + for (auto& s : split(w, separator)) { + if (!s.empty()) { + ans.emplace_back(s); + } + } + } + return ans; + } + + vector split(string& s, char c) { + vector res; + stringstream ss(s); + string t; + while (getline(ss, t, c)) { + res.push_back(t); + } + return res; + } +}; +``` + +### **Go** + +```go +func splitWordsBySeparator(words []string, separator byte) (ans []string) { + for _, w := range words { + for _, s := range strings.Split(w, string(separator)) { + if s != "" { + ans = append(ans, s) + } + } + } + return +} +``` + +### **TypeScript** + +```ts +function splitWordsBySeparator(words: string[], separator: string): string[] { + const ans: string[] = []; + for (const w of words) { + for (const s of w.split(separator)) { + if (s.length > 0) { + ans.push(s); + } + } + } + return ans; +} +``` + +### **...** + +``` + +``` + + diff --git a/solution/2700-2799/2788.Split Strings by Separator/README_EN.md b/solution/2700-2799/2788.Split Strings by Separator/README_EN.md new file mode 100644 index 0000000000000..e639d84506ac1 --- /dev/null +++ b/solution/2700-2799/2788.Split Strings by Separator/README_EN.md @@ -0,0 +1,161 @@ +# [2788. Split Strings by Separator](https://leetcode.com/problems/split-strings-by-separator) + +[中文文档](/solution/2700-2799/2788.Split%20Strings%20by%20Separator/README.md) + +## Description + +

    Given an array of strings words and a character separator, split each string in words by separator.

    + +

    Return an array of strings containing the new strings formed after the splits, excluding empty strings.

    + +

    Notes

    + +
      +
    • separator is used to determine where the split should occur, but it is not included as part of the resulting strings.
    • +
    • A split may result in more than two strings.
    • +
    • The resulting strings must maintain the same order as they were initially given.
    • +
    + +

     

    +

    Example 1:

    + +
    +Input: words = ["one.two.three","four.five","six"], separator = "."
    +Output: ["one","two","three","four","five","six"]
    +Explanation: In this example we split as follows:
    +
    +"one.two.three" splits into "one", "two", "three"
    +"four.five" splits into "four", "five"
    +"six" splits into "six" 
    +
    +Hence, the resulting array is ["one","two","three","four","five","six"].
    + +

    Example 2:

    + +
    +Input: words = ["$easy$","$problem$"], separator = "$"
    +Output: ["easy","problem"]
    +Explanation: In this example we split as follows: 
    +
    +"$easy$" splits into "easy" (excluding empty strings)
    +"$problem$" splits into "problem" (excluding empty strings)
    +
    +Hence, the resulting array is ["easy","problem"].
    +
    + +

    Example 3:

    + +
    +Input: words = ["|||"], separator = "|"
    +Output: []
    +Explanation: In this example the resulting split of "|||" will contain only empty strings, so we return an empty array []. 
    + +

     

    +

    Constraints:

    + +
      +
    • 1 <= words.length <= 100
    • +
    • 1 <= words[i].length <= 20
    • +
    • characters in words[i] are either lowercase English letters or characters from the string ".,|$#@" (excluding the quotes)
    • +
    • separator is a character from the string ".,|$#@" (excluding the quotes)
    • +
    + +## Solutions + + + +### **Python3** + +```python +class Solution: + def splitWordsBySeparator(self, words: List[str], separator: str) -> List[str]: + return [s for w in words for s in w.split(separator) if s] +``` + +### **Java** + +```java +import java.util.regex.Pattern; + +class Solution { + public List splitWordsBySeparator(List words, char separator) { + List ans = new ArrayList<>(); + for (var w : words) { + for (var s : w.split(Pattern.quote(String.valueOf(separator)))) { + if (s.length() > 0) { + ans.add(s); + } + } + } + return ans; + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + vector splitWordsBySeparator(vector& words, char separator) { + vector ans; + for (auto& w : words) { + for (auto& s : split(w, separator)) { + if (!s.empty()) { + ans.emplace_back(s); + } + } + } + return ans; + } + + vector split(string& s, char c) { + vector res; + stringstream ss(s); + string t; + while (getline(ss, t, c)) { + res.push_back(t); + } + return res; + } +}; +``` + +### **Go** + +```go +func splitWordsBySeparator(words []string, separator byte) (ans []string) { + for _, w := range words { + for _, s := range strings.Split(w, string(separator)) { + if s != "" { + ans = append(ans, s) + } + } + } + return +} +``` + +### **TypeScript** + +```ts +function splitWordsBySeparator(words: string[], separator: string): string[] { + const ans: string[] = []; + for (const w of words) { + for (const s of w.split(separator)) { + if (s.length > 0) { + ans.push(s); + } + } + } + return ans; +} +``` + +### **...** + +``` + +``` + + diff --git a/solution/2700-2799/2788.Split Strings by Separator/Solution.cpp b/solution/2700-2799/2788.Split Strings by Separator/Solution.cpp new file mode 100644 index 0000000000000..d27decd5b345d --- /dev/null +++ b/solution/2700-2799/2788.Split Strings by Separator/Solution.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + vector splitWordsBySeparator(vector& words, char separator) { + vector ans; + for (auto& w : words) { + for (auto& s : split(w, separator)) { + if (!s.empty()) { + ans.emplace_back(s); + } + } + } + return ans; + } + + vector split(string& s, char c) { + vector res; + stringstream ss(s); + string t; + while (getline(ss, t, c)) { + res.push_back(t); + } + return res; + } +}; \ No newline at end of file diff --git a/solution/2700-2799/2788.Split Strings by Separator/Solution.go b/solution/2700-2799/2788.Split Strings by Separator/Solution.go new file mode 100644 index 0000000000000..bda097b7c0097 --- /dev/null +++ b/solution/2700-2799/2788.Split Strings by Separator/Solution.go @@ -0,0 +1,10 @@ +func splitWordsBySeparator(words []string, separator byte) (ans []string) { + for _, w := range words { + for _, s := range strings.Split(w, string(separator)) { + if s != "" { + ans = append(ans, s) + } + } + } + return +} \ No newline at end of file diff --git a/solution/2700-2799/2788.Split Strings by Separator/Solution.java b/solution/2700-2799/2788.Split Strings by Separator/Solution.java new file mode 100644 index 0000000000000..4c0be69b6ead6 --- /dev/null +++ b/solution/2700-2799/2788.Split Strings by Separator/Solution.java @@ -0,0 +1,15 @@ +import java.util.regex.Pattern; + +class Solution { + public List splitWordsBySeparator(List words, char separator) { + List ans = new ArrayList<>(); + for (var w : words) { + for (var s : w.split(Pattern.quote(String.valueOf(separator)))) { + if (s.length() > 0) { + ans.add(s); + } + } + } + return ans; + } +} \ No newline at end of file diff --git a/solution/2700-2799/2788.Split Strings by Separator/Solution.py b/solution/2700-2799/2788.Split Strings by Separator/Solution.py new file mode 100644 index 0000000000000..e494687df0a02 --- /dev/null +++ b/solution/2700-2799/2788.Split Strings by Separator/Solution.py @@ -0,0 +1,3 @@ +class Solution: + def splitWordsBySeparator(self, words: List[str], separator: str) -> List[str]: + return [s for w in words for s in w.split(separator) if s] diff --git a/solution/2700-2799/2788.Split Strings by Separator/Solution.ts b/solution/2700-2799/2788.Split Strings by Separator/Solution.ts new file mode 100644 index 0000000000000..076f8be7e9bb1 --- /dev/null +++ b/solution/2700-2799/2788.Split Strings by Separator/Solution.ts @@ -0,0 +1,11 @@ +function splitWordsBySeparator(words: string[], separator: string): string[] { + const ans: string[] = []; + for (const w of words) { + for (const s of w.split(separator)) { + if (s.length > 0) { + ans.push(s); + } + } + } + return ans; +} diff --git a/solution/2700-2799/2789.Largest Element in an Array after Merge Operations/README.md b/solution/2700-2799/2789.Largest Element in an Array after Merge Operations/README.md new file mode 100644 index 0000000000000..b039cf7abc3dd --- /dev/null +++ b/solution/2700-2799/2789.Largest Element in an Array after Merge Operations/README.md @@ -0,0 +1,157 @@ +# [2789. 合并后数组中的最大元素](https://leetcode.cn/problems/largest-element-in-an-array-after-merge-operations) + +[English Version](/solution/2700-2799/2789.Largest%20Element%20in%20an%20Array%20after%20Merge%20Operations/README_EN.md) + +## 题目描述 + + + +

    给你一个下标从 0 开始、由正整数组成的数组 nums

    + +

    你可以在数组上执行下述操作 任意 次:

    + +
      +
    • 选中一个同时满足 0 <= i < nums.length - 1nums[i] <= nums[i + 1] 的整数 i 。将元素 nums[i + 1] 替换为 nums[i] + nums[i + 1] ,并从数组中删除元素 nums[i]
    • +
    + +

    返回你可以从最终数组中获得的 最大 元素的值。

    + +

     

    + +

    示例 1:

    + +
    输入:nums = [2,3,7,9,3]
    +输出:21
    +解释:我们可以在数组上执行下述操作:
    +- 选中 i = 0 ,得到数组 nums = [5,7,9,3] 。
    +- 选中 i = 1 ,得到数组 nums = [5,16,3] 。
    +- 选中 i = 0 ,得到数组 nums = [21,3] 。
    +最终数组中的最大元素是 21 。可以证明我们无法获得更大的元素。
    +
    + +

    示例 2:

    + +
    输入:nums = [5,3,3]
    +输出:11
    +解释:我们可以在数组上执行下述操作:
    +- 选中 i = 1 ,得到数组 nums = [5,6] 。
    +- 选中 i = 0 ,得到数组 nums = [11] 。
    +最终数组中只有一个元素,即 11 。
    +
    + +

     

    + +

    提示:

    + +
      +
    • 1 <= nums.length <= 105
    • +
    • 1 <= nums[i] <= 106
    • +
    + +## 解法 + + + + + +### **Python3** + + + +```python +class Solution: + def maxArrayValue(self, nums: List[int]) -> int: + for i in range(len(nums) - 2, -1, -1): + if nums[i] <= nums[i + 1]: + nums[i] += nums[i + 1] + return max(nums) +``` + +### **Java** + + + +```java +class Solution { + public long maxArrayValue(int[] nums) { + int n = nums.length; + long ans = nums[n - 1], t = nums[n - 1]; + for (int i = n - 2; i >= 0; --i) { + if (nums[i] <= t) { + t += nums[i]; + } else { + t = nums[i]; + } + ans = Math.max(ans, t); + } + return ans; + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + long long maxArrayValue(vector& nums) { + int n = nums.size(); + long long ans = nums[n - 1], t = nums[n - 1]; + for (int i = n - 2; ~i; --i) { + if (nums[i] <= t) { + t += nums[i]; + } else { + t = nums[i]; + } + ans = max(ans, t); + } + return ans; + } +}; +``` + +### **Go** + +```go +func maxArrayValue(nums []int) int64 { + n := len(nums) + ans, t := nums[n-1], nums[n-1] + for i := n - 2; i >= 0; i-- { + if nums[i] <= t { + t += nums[i] + } else { + t = nums[i] + } + ans = max(ans, t) + } + return int64(ans) +} + +func max(a, b int) int { + if a > b { + return a + } + return b +} +``` + +### **TypeScript** + +```ts +function maxArrayValue(nums: number[]): number { + for (let i = nums.length - 2; i >= 0; --i) { + if (nums[i] <= nums[i + 1]) { + nums[i] += nums[i + 1]; + } + } + return Math.max(...nums); +} +``` + +### **...** + +``` + +``` + + diff --git a/solution/2700-2799/2789.Largest Element in an Array after Merge Operations/README_EN.md b/solution/2700-2799/2789.Largest Element in an Array after Merge Operations/README_EN.md new file mode 100644 index 0000000000000..f6ba901940213 --- /dev/null +++ b/solution/2700-2799/2789.Largest Element in an Array after Merge Operations/README_EN.md @@ -0,0 +1,149 @@ +# [2789. Largest Element in an Array after Merge Operations](https://leetcode.com/problems/largest-element-in-an-array-after-merge-operations) + +[中文文档](/solution/2700-2799/2789.Largest%20Element%20in%20an%20Array%20after%20Merge%20Operations/README.md) + +## Description + +

    You are given a 0-indexed array nums consisting of positive integers.

    + +

    You can do the following operation on the array any number of times:

    + +
      +
    • Choose an integer i such that 0 <= i < nums.length - 1 and nums[i] <= nums[i + 1]. Replace the element nums[i + 1] with nums[i] + nums[i + 1] and delete the element nums[i] from the array.
    • +
    + +

    Return the value of the largest element that you can possibly obtain in the final array.

    + +

     

    +

    Example 1:

    + +
    +Input: nums = [2,3,7,9,3]
    +Output: 21
    +Explanation: We can apply the following operations on the array:
    +- Choose i = 0. The resulting array will be nums = [5,7,9,3].
    +- Choose i = 1. The resulting array will be nums = [5,16,3].
    +- Choose i = 0. The resulting array will be nums = [21,3].
    +The largest element in the final array is 21. It can be shown that we cannot obtain a larger element.
    +
    + +

    Example 2:

    + +
    +Input: nums = [5,3,3]
    +Output: 11
    +Explanation: We can do the following operations on the array:
    +- Choose i = 1. The resulting array will be nums = [5,6].
    +- Choose i = 0. The resulting array will be nums = [11].
    +There is only one element in the final array, which is 11.
    +
    + +

     

    +

    Constraints:

    + +
      +
    • 1 <= nums.length <= 105
    • +
    • 1 <= nums[i] <= 106
    • +
    + +## Solutions + + + +### **Python3** + +```python +class Solution: + def maxArrayValue(self, nums: List[int]) -> int: + for i in range(len(nums) - 2, -1, -1): + if nums[i] <= nums[i + 1]: + nums[i] += nums[i + 1] + return max(nums) +``` + +### **Java** + +```java +class Solution { + public long maxArrayValue(int[] nums) { + int n = nums.length; + long ans = nums[n - 1], t = nums[n - 1]; + for (int i = n - 2; i >= 0; --i) { + if (nums[i] <= t) { + t += nums[i]; + } else { + t = nums[i]; + } + ans = Math.max(ans, t); + } + return ans; + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + long long maxArrayValue(vector& nums) { + int n = nums.size(); + long long ans = nums[n - 1], t = nums[n - 1]; + for (int i = n - 2; ~i; --i) { + if (nums[i] <= t) { + t += nums[i]; + } else { + t = nums[i]; + } + ans = max(ans, t); + } + return ans; + } +}; +``` + +### **Go** + +```go +func maxArrayValue(nums []int) int64 { + n := len(nums) + ans, t := nums[n-1], nums[n-1] + for i := n - 2; i >= 0; i-- { + if nums[i] <= t { + t += nums[i] + } else { + t = nums[i] + } + ans = max(ans, t) + } + return int64(ans) +} + +func max(a, b int) int { + if a > b { + return a + } + return b +} +``` + +### **TypeScript** + +```ts +function maxArrayValue(nums: number[]): number { + for (let i = nums.length - 2; i >= 0; --i) { + if (nums[i] <= nums[i + 1]) { + nums[i] += nums[i + 1]; + } + } + return Math.max(...nums); +} +``` + +### **...** + +``` + +``` + + diff --git a/solution/2700-2799/2789.Largest Element in an Array after Merge Operations/Solution.cpp b/solution/2700-2799/2789.Largest Element in an Array after Merge Operations/Solution.cpp new file mode 100644 index 0000000000000..216cf02ff1e46 --- /dev/null +++ b/solution/2700-2799/2789.Largest Element in an Array after Merge Operations/Solution.cpp @@ -0,0 +1,16 @@ +class Solution { +public: + long long maxArrayValue(vector& nums) { + int n = nums.size(); + long long ans = nums[n - 1], t = nums[n - 1]; + for (int i = n - 2; ~i; --i) { + if (nums[i] <= t) { + t += nums[i]; + } else { + t = nums[i]; + } + ans = max(ans, t); + } + return ans; + } +}; \ No newline at end of file diff --git a/solution/2700-2799/2789.Largest Element in an Array after Merge Operations/Solution.go b/solution/2700-2799/2789.Largest Element in an Array after Merge Operations/Solution.go new file mode 100644 index 0000000000000..922e74fe196fa --- /dev/null +++ b/solution/2700-2799/2789.Largest Element in an Array after Merge Operations/Solution.go @@ -0,0 +1,20 @@ +func maxArrayValue(nums []int) int64 { + n := len(nums) + ans, t := nums[n-1], nums[n-1] + for i := n - 2; i >= 0; i-- { + if nums[i] <= t { + t += nums[i] + } else { + t = nums[i] + } + ans = max(ans, t) + } + return int64(ans) +} + +func max(a, b int) int { + if a > b { + return a + } + return b +} \ No newline at end of file diff --git a/solution/2700-2799/2789.Largest Element in an Array after Merge Operations/Solution.java b/solution/2700-2799/2789.Largest Element in an Array after Merge Operations/Solution.java new file mode 100644 index 0000000000000..ec5915fae4bab --- /dev/null +++ b/solution/2700-2799/2789.Largest Element in an Array after Merge Operations/Solution.java @@ -0,0 +1,15 @@ +class Solution { + public long maxArrayValue(int[] nums) { + int n = nums.length; + long ans = nums[n - 1], t = nums[n - 1]; + for (int i = n - 2; i >= 0; --i) { + if (nums[i] <= t) { + t += nums[i]; + } else { + t = nums[i]; + } + ans = Math.max(ans, t); + } + return ans; + } +} \ No newline at end of file diff --git a/solution/2700-2799/2789.Largest Element in an Array after Merge Operations/Solution.py b/solution/2700-2799/2789.Largest Element in an Array after Merge Operations/Solution.py new file mode 100644 index 0000000000000..b53bbaa2f802a --- /dev/null +++ b/solution/2700-2799/2789.Largest Element in an Array after Merge Operations/Solution.py @@ -0,0 +1,6 @@ +class Solution: + def maxArrayValue(self, nums: List[int]) -> int: + for i in range(len(nums) - 2, -1, -1): + if nums[i] <= nums[i + 1]: + nums[i] += nums[i + 1] + return max(nums) diff --git a/solution/2700-2799/2789.Largest Element in an Array after Merge Operations/Solution.ts b/solution/2700-2799/2789.Largest Element in an Array after Merge Operations/Solution.ts new file mode 100644 index 0000000000000..a6eb3872e2a03 --- /dev/null +++ b/solution/2700-2799/2789.Largest Element in an Array after Merge Operations/Solution.ts @@ -0,0 +1,8 @@ +function maxArrayValue(nums: number[]): number { + for (let i = nums.length - 2; i >= 0; --i) { + if (nums[i] <= nums[i + 1]) { + nums[i] += nums[i + 1]; + } + } + return Math.max(...nums); +} diff --git a/solution/2700-2799/2790.Maximum Number of Groups With Increasing Length/README.md b/solution/2700-2799/2790.Maximum Number of Groups With Increasing Length/README.md new file mode 100644 index 0000000000000..8d662b4e67b40 --- /dev/null +++ b/solution/2700-2799/2790.Maximum Number of Groups With Increasing Length/README.md @@ -0,0 +1,190 @@ +# [2790. 长度递增组的最大数目](https://leetcode.cn/problems/maximum-number-of-groups-with-increasing-length) + +[English Version](/solution/2700-2799/2790.Maximum%20Number%20of%20Groups%20With%20Increasing%20Length/README_EN.md) + +## 题目描述 + + + +

    给你一个下标从 0 开始、长度为 n 的数组 usageLimits

    + +

    你的任务是使用从 0n - 1 的数字创建若干组,并确保每个数字 i所有组 中使用的次数总共不超过 usageLimits[i] 次。此外,还必须满足以下条件:

    + +
      +
    • 每个组必须由 不同 的数字组成,也就是说,单个组内不能存在重复的数字。
    • +
    • 每个组(除了第一个)的长度必须 严格大于 前一个组。
    • +
    + +

    在满足所有条件的情况下,以整数形式返回可以创建的最大组数。

    + +

     

    + +

    示例 1:

    + +
    +输入:usageLimits = [1,2,5]
    +输出:3
    +解释:在这个示例中,我们可以使用 0 至多一次,使用 1 至多 2 次,使用 2 至多 5 次。
    +一种既能满足所有条件,又能创建最多组的方式是: 
    +组 1 包含数字 [2] 。
    +组 2 包含数字 [1,2] 。
    +组 3 包含数字 [0,1,2] 。 
    +可以证明能够创建的最大组数是 3 。 
    +所以,输出是 3 。 
    + +

    示例 2:

    + +
    +输入:usageLimits = [2,1,2]
    +输出:2
    +解释:在这个示例中,我们可以使用 0 至多 2 次,使用 1 至多 1 次,使用 2 至多 2 次。
    +一种既能满足所有条件,又能创建最多组的方式是: 
    +组 1 包含数字 [0] 。 
    +组 2 包含数字 [1,2] 。
    +可以证明能够创建的最大组数是 2 。 
    +所以,输出是 2 。 
    +
    + +

    示例 3:

    + +
    +输入:usageLimits = [1,1]
    +输出:1
    +解释:在这个示例中,我们可以使用 0 和 1 至多 1 次。 
    +一种既能满足所有条件,又能创建最多组的方式是:
    +组 1 包含数字 [0] 。
    +可以证明能够创建的最大组数是 1 。 
    +所以,输出是 1 。 
    +
    + +

     

    + +

    提示:

    + +
      +
    • 1 <= usageLimits.length <= 105
    • +
    • 1 <= usageLimits[i] <= 109
    • +
    + +## 解法 + + + + + +### **Python3** + + + +```python +class Solution: + def maxIncreasingGroups(self, usageLimits: List[int]) -> int: + usageLimits.sort() + k, n = 0, len(usageLimits) + for i in range(n): + if usageLimits[i] > k: + k += 1 + usageLimits[i] -= k + if i + 1 < n: + usageLimits[i + 1] += usageLimits[i] + return k +``` + +```python +class Solution: + def maxIncreasingGroups(self, usageLimits: List[int]) -> int: + usageLimits.sort() + k = s = 0 + for x in usageLimits: + s += x + if s > k: + k += 1 + s -= k + return k +``` + +### **Java** + + + +```java +class Solution { + public int maxIncreasingGroups(List usageLimits) { + Collections.sort(usageLimits); + int k = 0; + long s = 0; + for (int x : usageLimits) { + s += x; + if (s > k) { + ++k; + s -= k; + } + } + return k; + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + int maxIncreasingGroups(vector& usageLimits) { + sort(usageLimits.begin(), usageLimits.end()); + int k = 0; + long long s = 0; + for (int x : usageLimits) { + s += x; + if (s > k) { + ++k; + s -= k; + } + } + return k; + } +}; +``` + +### **Go** + +```go +func maxIncreasingGroups(usageLimits []int) int { + sort.Ints(usageLimits) + s, k := 0, 0 + for _, x := range usageLimits { + s += x + if s > k { + k++ + s -= k + } + } + return k +} +``` + +### **TypeScript** + +```ts +function maxIncreasingGroups(usageLimits: number[]): number { + usageLimits.sort((a, b) => a - b); + let k = 0; + let s = 0; + for (const x of usageLimits) { + s += x; + if (s > k) { + ++k; + s -= k; + } + } + return k; +} +``` + +### **...** + +``` + +``` + + diff --git a/solution/2700-2799/2790.Maximum Number of Groups With Increasing Length/README_EN.md b/solution/2700-2799/2790.Maximum Number of Groups With Increasing Length/README_EN.md new file mode 100644 index 0000000000000..c1f0cd451a915 --- /dev/null +++ b/solution/2700-2799/2790.Maximum Number of Groups With Increasing Length/README_EN.md @@ -0,0 +1,180 @@ +# [2790. Maximum Number of Groups With Increasing Length](https://leetcode.com/problems/maximum-number-of-groups-with-increasing-length) + +[中文文档](/solution/2700-2799/2790.Maximum%20Number%20of%20Groups%20With%20Increasing%20Length/README.md) + +## Description + +

    You are given a 0-indexed array usageLimits of length n.

    + +

    Your task is to create groups using numbers from 0 to n - 1, ensuring that each number, i, is used no more than usageLimits[i] times in total across all groups. You must also satisfy the following conditions:

    + +
      +
    • Each group must consist of distinct numbers, meaning that no duplicate numbers are allowed within a single group.
    • +
    • Each group (except the first one) must have a length strictly greater than the previous group.
    • +
    + +

    Return an integer denoting the maximum number of groups you can create while satisfying these conditions.

    + +

     

    +

    Example 1:

    + +
    +Input: usageLimits = [1,2,5]
    +Output: 3
    +Explanation: In this example, we can use 0 at most once, 1 at most twice, and 2 at most five times.
    +One way of creating the maximum number of groups while satisfying the conditions is: 
    +Group 1 contains the number [2].
    +Group 2 contains the numbers [1,2].
    +Group 3 contains the numbers [0,1,2]. 
    +It can be shown that the maximum number of groups is 3. 
    +So, the output is 3. 
    + +

    Example 2:

    + +
    +Input: usageLimits = [2,1,2]
    +Output: 2
    +Explanation: In this example, we can use 0 at most twice, 1 at most once, and 2 at most twice.
    +One way of creating the maximum number of groups while satisfying the conditions is:
    +Group 1 contains the number [0].
    +Group 2 contains the numbers [1,2].
    +It can be shown that the maximum number of groups is 2.
    +So, the output is 2. 
    +
    + +

    Example 3:

    + +
    +Input: usageLimits = [1,1]
    +Output: 1
    +Explanation: In this example, we can use both 0 and 1 at most once.
    +One way of creating the maximum number of groups while satisfying the conditions is:
    +Group 1 contains the number [0].
    +It can be shown that the maximum number of groups is 1.
    +So, the output is 1. 
    +
    + +

     

    +

    Constraints:

    + +
      +
    • 1 <= usageLimits.length <= 105
    • +
    • 1 <= usageLimits[i] <= 109
    • +
    + +## Solutions + + + +### **Python3** + +```python +class Solution: + def maxIncreasingGroups(self, usageLimits: List[int]) -> int: + usageLimits.sort() + k, n = 0, len(usageLimits) + for i in range(n): + if usageLimits[i] > k: + k += 1 + usageLimits[i] -= k + if i + 1 < n: + usageLimits[i + 1] += usageLimits[i] + return k +``` + +```python +class Solution: + def maxIncreasingGroups(self, usageLimits: List[int]) -> int: + usageLimits.sort() + k = s = 0 + for x in usageLimits: + s += x + if s > k: + k += 1 + s -= k + return k +``` + +### **Java** + +```java +class Solution { + public int maxIncreasingGroups(List usageLimits) { + Collections.sort(usageLimits); + int k = 0; + long s = 0; + for (int x : usageLimits) { + s += x; + if (s > k) { + ++k; + s -= k; + } + } + return k; + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + int maxIncreasingGroups(vector& usageLimits) { + sort(usageLimits.begin(), usageLimits.end()); + int k = 0; + long long s = 0; + for (int x : usageLimits) { + s += x; + if (s > k) { + ++k; + s -= k; + } + } + return k; + } +}; +``` + +### **Go** + +```go +func maxIncreasingGroups(usageLimits []int) int { + sort.Ints(usageLimits) + s, k := 0, 0 + for _, x := range usageLimits { + s += x + if s > k { + k++ + s -= k + } + } + return k +} +``` + +### **TypeScript** + +```ts +function maxIncreasingGroups(usageLimits: number[]): number { + usageLimits.sort((a, b) => a - b); + let k = 0; + let s = 0; + for (const x of usageLimits) { + s += x; + if (s > k) { + ++k; + s -= k; + } + } + return k; +} +``` + +### **...** + +``` + +``` + + diff --git a/solution/2700-2799/2790.Maximum Number of Groups With Increasing Length/Solution.cpp b/solution/2700-2799/2790.Maximum Number of Groups With Increasing Length/Solution.cpp new file mode 100644 index 0000000000000..a927b610fb97c --- /dev/null +++ b/solution/2700-2799/2790.Maximum Number of Groups With Increasing Length/Solution.cpp @@ -0,0 +1,16 @@ +class Solution { +public: + int maxIncreasingGroups(vector& usageLimits) { + sort(usageLimits.begin(), usageLimits.end()); + int k = 0; + long long s = 0; + for (int x : usageLimits) { + s += x; + if (s > k) { + ++k; + s -= k; + } + } + return k; + } +}; \ No newline at end of file diff --git a/solution/2700-2799/2790.Maximum Number of Groups With Increasing Length/Solution.go b/solution/2700-2799/2790.Maximum Number of Groups With Increasing Length/Solution.go new file mode 100644 index 0000000000000..4ecc3fff8fa58 --- /dev/null +++ b/solution/2700-2799/2790.Maximum Number of Groups With Increasing Length/Solution.go @@ -0,0 +1,12 @@ +func maxIncreasingGroups(usageLimits []int) int { + sort.Ints(usageLimits) + s, k := 0, 0 + for _, x := range usageLimits { + s += x + if s > k { + k++ + s -= k + } + } + return k +} \ No newline at end of file diff --git a/solution/2700-2799/2790.Maximum Number of Groups With Increasing Length/Solution.java b/solution/2700-2799/2790.Maximum Number of Groups With Increasing Length/Solution.java new file mode 100644 index 0000000000000..46d72595a8a7d --- /dev/null +++ b/solution/2700-2799/2790.Maximum Number of Groups With Increasing Length/Solution.java @@ -0,0 +1,15 @@ +class Solution { + public int maxIncreasingGroups(List usageLimits) { + Collections.sort(usageLimits); + int k = 0; + long s = 0; + for (int x : usageLimits) { + s += x; + if (s > k) { + ++k; + s -= k; + } + } + return k; + } +} \ No newline at end of file diff --git a/solution/2700-2799/2790.Maximum Number of Groups With Increasing Length/Solution.py b/solution/2700-2799/2790.Maximum Number of Groups With Increasing Length/Solution.py new file mode 100644 index 0000000000000..165a9b7d1fdda --- /dev/null +++ b/solution/2700-2799/2790.Maximum Number of Groups With Increasing Length/Solution.py @@ -0,0 +1,10 @@ +class Solution: + def maxIncreasingGroups(self, usageLimits: List[int]) -> int: + usageLimits.sort() + k = s = 0 + for x in usageLimits: + s += x + if s > k: + k += 1 + s -= k + return k diff --git a/solution/2700-2799/2790.Maximum Number of Groups With Increasing Length/Solution.ts b/solution/2700-2799/2790.Maximum Number of Groups With Increasing Length/Solution.ts new file mode 100644 index 0000000000000..20bac1bc13a0f --- /dev/null +++ b/solution/2700-2799/2790.Maximum Number of Groups With Increasing Length/Solution.ts @@ -0,0 +1,13 @@ +function maxIncreasingGroups(usageLimits: number[]): number { + usageLimits.sort((a, b) => a - b); + let k = 0; + let s = 0; + for (const x of usageLimits) { + s += x; + if (s > k) { + ++k; + s -= k; + } + } + return k; +} diff --git a/solution/2700-2799/2791.Count Paths That Can Form a Palindrome in a Tree/README.md b/solution/2700-2799/2791.Count Paths That Can Form a Palindrome in a Tree/README.md new file mode 100644 index 0000000000000..adbecd76174e4 --- /dev/null +++ b/solution/2700-2799/2791.Count Paths That Can Form a Palindrome in a Tree/README.md @@ -0,0 +1,219 @@ +# [2791. 树中可以形成回文的路径数](https://leetcode.cn/problems/count-paths-that-can-form-a-palindrome-in-a-tree) + +[English Version](/solution/2700-2799/2791.Count%20Paths%20That%20Can%20Form%20a%20Palindrome%20in%20a%20Tree/README_EN.md) + +## 题目描述 + + + +

    给你一棵 (即,一个连通、无向且无环的图), 节点为 0 ,由编号从 0n - 1n 个节点组成。这棵树用一个长度为 n 、下标从 0 开始的数组 parent 表示,其中 parent[i] 为节点 i 的父节点,由于节点 0 为根节点,所以 parent[0] == -1

    + +

    另给你一个长度为 n 的字符串 s ,其中 s[i] 是分配给 iparent[i] 之间的边的字符。s[0] 可以忽略。

    + +

    找出满足 u < v ,且从 uv 的路径上分配的字符可以 重新排列 形成 回文 的所有节点对 (u, v) ,并返回节点对的数目。

    + +

    如果一个字符串正着读和反着读都相同,那么这个字符串就是一个 回文

    + +

     

    + +

    示例 1:

    + +

    + +
    输入:parent = [-1,0,0,1,1,2], s = "acaabc"
    +输出:8
    +解释:符合题目要求的节点对分别是:
    +- (0,1)、(0,2)、(1,3)、(1,4) 和 (2,5) ,路径上只有一个字符,满足回文定义。
    +- (2,3),路径上字符形成的字符串是 "aca" ,满足回文定义。
    +- (1,5),路径上字符形成的字符串是 "cac" ,满足回文定义。
    +- (3,5),路径上字符形成的字符串是 "acac" ,可以重排形成回文 "acca" 。
    +
    + +

    示例 2:

    + +
    输入:parent = [-1,0,0,0,0], s = "aaaaa"
    +输出:10
    +解释:任何满足 u < v 的节点对 (u,v) 都符合题目要求。
    +
    + +

     

    + +

    提示:

    + +
      +
    • n == parent.length == s.length
    • +
    • 1 <= n <= 105
    • +
    • 对于所有 i >= 10 <= parent[i] <= n - 1 均成立
    • +
    • parent[0] == -1
    • +
    • parent 表示一棵有效的树
    • +
    • s 仅由小写英文数字组成
    • +
    + +## 解法 + + + + + +### **Python3** + + + +```python +class Solution: + def countPalindromePaths(self, parent: List[int], s: str) -> int: + def dfs(i: int, xor: int): + nonlocal ans + for j, v in g[i]: + x = xor ^ v + ans += cnt[x] + for k in range(26): + ans += cnt[x ^ (1 << k)] + cnt[x] += 1 + dfs(j, x) + + n = len(parent) + g = defaultdict(list) + for i in range(1, n): + p = parent[i] + g[p].append((i, 1 << (ord(s[i]) - ord('a')))) + ans = 0 + cnt = Counter({0: 1}) + dfs(0, 0) + return ans +``` + +### **Java** + + + +```java +class Solution { + private List[] g; + private Map cnt = new HashMap<>(); + private long ans; + + public long countPalindromePaths(List parent, String s) { + int n = parent.size(); + g = new List[n]; + cnt.put(0, 1); + Arrays.setAll(g, k -> new ArrayList<>()); + for (int i = 1; i < n; ++i) { + int p = parent.get(i); + g[p].add(new int[] {i, 1 << (s.charAt(i) - 'a')}); + } + dfs(0, 0); + return ans; + } + + private void dfs(int i, int xor) { + for (int[] e : g[i]) { + int j = e[0], v = e[1]; + int x = xor ^ v; + ans += cnt.getOrDefault(x, 0); + for (int k = 0; k < 26; ++k) { + ans += cnt.getOrDefault(x ^ (1 << k), 0); + } + cnt.merge(x, 1, Integer::sum); + dfs(j, x); + } + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + long long countPalindromePaths(vector& parent, string s) { + int n = parent.size(); + vector>> g(n); + unordered_map cnt; + cnt[0] = 1; + for (int i = 1; i < n; ++i) { + int p = parent[i]; + g[p].emplace_back(i, 1 << (s[i] - 'a')); + } + long long ans = 0; + function dfs = [&](int i, int xo) { + for (auto [j, v] : g[i]) { + int x = xo ^ v; + ans += cnt[x]; + for (int k = 0; k < 26; ++k) { + ans += cnt[x ^ (1 << k)]; + } + ++cnt[x]; + dfs(j, x); + } + }; + dfs(0, 0); + return ans; + } +}; +``` + +### **Go** + +```go +func countPalindromePaths(parent []int, s string) (ans int64) { + type pair struct{ i, v int } + n := len(parent) + g := make([][]pair, n) + for i := 1; i < n; i++ { + p := parent[i] + g[p] = append(g[p], pair{i, 1 << (s[i] - 'a')}) + } + cnt := map[int]int{0: 1} + var dfs func(i, xor int) + dfs = func(i, xor int) { + for _, e := range g[i] { + x := xor ^ e.v + ans += int64(cnt[x]) + for k := 0; k < 26; k++ { + ans += int64(cnt[x^(1< []); + for (let i = 1; i < n; ++i) { + g[parent[i]].push([i, 1 << (s.charCodeAt(i) - 97)]); + } + const cnt: Map = new Map(); + cnt.set(0, 1); + let ans = 0; + const dfs = (i: number, xor: number): void => { + for (const [j, v] of g[i]) { + const x = xor ^ v; + ans += cnt.get(x) || 0; + for (let k = 0; k < 26; ++k) { + ans += cnt.get(x ^ (1 << k)) || 0; + } + cnt.set(x, (cnt.get(x) || 0) + 1); + dfs(j, x); + } + }; + dfs(0, 0); + return ans; +} +``` + +### **...** + +``` + +``` + + diff --git a/solution/2700-2799/2791.Count Paths That Can Form a Palindrome in a Tree/README_EN.md b/solution/2700-2799/2791.Count Paths That Can Form a Palindrome in a Tree/README_EN.md new file mode 100644 index 0000000000000..6d97a486e0dcf --- /dev/null +++ b/solution/2700-2799/2791.Count Paths That Can Form a Palindrome in a Tree/README_EN.md @@ -0,0 +1,211 @@ +# [2791. Count Paths That Can Form a Palindrome in a Tree](https://leetcode.com/problems/count-paths-that-can-form-a-palindrome-in-a-tree) + +[中文文档](/solution/2700-2799/2791.Count%20Paths%20That%20Can%20Form%20a%20Palindrome%20in%20a%20Tree/README.md) + +## Description + +

    You are given a tree (i.e. a connected, undirected graph that has no cycles) rooted at node 0 consisting of n nodes numbered from 0 to n - 1. The tree is represented by a 0-indexed array parent of size n, where parent[i] is the parent of node i. Since node 0 is the root, parent[0] == -1.

    + +

    You are also given a string s of length n, where s[i] is the character assigned to the edge between i and parent[i]. s[0] can be ignored.

    + +

    Return the number of pairs of nodes (u, v) such that u < v and the characters assigned to edges on the path from u to v can be rearranged to form a palindrome.

    + +

    A string is a palindrome when it reads the same backwards as forwards.

    + +

     

    +

    Example 1:

    + +

    + +
    +Input: parent = [-1,0,0,1,1,2], s = "acaabc"
    +Output: 8
    +Explanation: The valid pairs are:
    +- All the pairs (0,1), (0,2), (1,3), (1,4) and (2,5) result in one character which is always a palindrome.
    +- The pair (2,3) result in the string "aca" which is a palindrome.
    +- The pair (1,5) result in the string "cac" which is a palindrome.
    +- The pair (3,5) result in the string "acac" which can be rearranged into the palindrome "acca".
    +
    + +

    Example 2:

    + +
    +Input: parent = [-1,0,0,0,0], s = "aaaaa"
    +Output: 10
    +Explanation: Any pair of nodes (u,v) where u < v is valid.
    +
    + +

     

    +

    Constraints:

    + +
      +
    • n == parent.length == s.length
    • +
    • 1 <= n <= 105
    • +
    • 0 <= parent[i] <= n - 1 for all i >= 1
    • +
    • parent[0] == -1
    • +
    • parent represents a valid tree.
    • +
    • s consists of only lowercase English letters.
    • +
    + +## Solutions + + + +### **Python3** + +```python +class Solution: + def countPalindromePaths(self, parent: List[int], s: str) -> int: + def dfs(i: int, xor: int): + nonlocal ans + for j, v in g[i]: + x = xor ^ v + ans += cnt[x] + for k in range(26): + ans += cnt[x ^ (1 << k)] + cnt[x] += 1 + dfs(j, x) + + n = len(parent) + g = defaultdict(list) + for i in range(1, n): + p = parent[i] + g[p].append((i, 1 << (ord(s[i]) - ord('a')))) + ans = 0 + cnt = Counter({0: 1}) + dfs(0, 0) + return ans +``` + +### **Java** + +```java +class Solution { + private List[] g; + private Map cnt = new HashMap<>(); + private long ans; + + public long countPalindromePaths(List parent, String s) { + int n = parent.size(); + g = new List[n]; + cnt.put(0, 1); + Arrays.setAll(g, k -> new ArrayList<>()); + for (int i = 1; i < n; ++i) { + int p = parent.get(i); + g[p].add(new int[] {i, 1 << (s.charAt(i) - 'a')}); + } + dfs(0, 0); + return ans; + } + + private void dfs(int i, int xor) { + for (int[] e : g[i]) { + int j = e[0], v = e[1]; + int x = xor ^ v; + ans += cnt.getOrDefault(x, 0); + for (int k = 0; k < 26; ++k) { + ans += cnt.getOrDefault(x ^ (1 << k), 0); + } + cnt.merge(x, 1, Integer::sum); + dfs(j, x); + } + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + long long countPalindromePaths(vector& parent, string s) { + int n = parent.size(); + vector>> g(n); + unordered_map cnt; + cnt[0] = 1; + for (int i = 1; i < n; ++i) { + int p = parent[i]; + g[p].emplace_back(i, 1 << (s[i] - 'a')); + } + long long ans = 0; + function dfs = [&](int i, int xo) { + for (auto [j, v] : g[i]) { + int x = xo ^ v; + ans += cnt[x]; + for (int k = 0; k < 26; ++k) { + ans += cnt[x ^ (1 << k)]; + } + ++cnt[x]; + dfs(j, x); + } + }; + dfs(0, 0); + return ans; + } +}; +``` + +### **Go** + +```go +func countPalindromePaths(parent []int, s string) (ans int64) { + type pair struct{ i, v int } + n := len(parent) + g := make([][]pair, n) + for i := 1; i < n; i++ { + p := parent[i] + g[p] = append(g[p], pair{i, 1 << (s[i] - 'a')}) + } + cnt := map[int]int{0: 1} + var dfs func(i, xor int) + dfs = func(i, xor int) { + for _, e := range g[i] { + x := xor ^ e.v + ans += int64(cnt[x]) + for k := 0; k < 26; k++ { + ans += int64(cnt[x^(1< []); + for (let i = 1; i < n; ++i) { + g[parent[i]].push([i, 1 << (s.charCodeAt(i) - 97)]); + } + const cnt: Map = new Map(); + cnt.set(0, 1); + let ans = 0; + const dfs = (i: number, xor: number): void => { + for (const [j, v] of g[i]) { + const x = xor ^ v; + ans += cnt.get(x) || 0; + for (let k = 0; k < 26; ++k) { + ans += cnt.get(x ^ (1 << k)) || 0; + } + cnt.set(x, (cnt.get(x) || 0) + 1); + dfs(j, x); + } + }; + dfs(0, 0); + return ans; +} +``` + +### **...** + +``` + +``` + + diff --git a/solution/2700-2799/2791.Count Paths That Can Form a Palindrome in a Tree/Solution.cpp b/solution/2700-2799/2791.Count Paths That Can Form a Palindrome in a Tree/Solution.cpp new file mode 100644 index 0000000000000..f6c50f2e62ca6 --- /dev/null +++ b/solution/2700-2799/2791.Count Paths That Can Form a Palindrome in a Tree/Solution.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + long long countPalindromePaths(vector& parent, string s) { + int n = parent.size(); + vector>> g(n); + unordered_map cnt; + cnt[0] = 1; + for (int i = 1; i < n; ++i) { + int p = parent[i]; + g[p].emplace_back(i, 1 << (s[i] - 'a')); + } + long long ans = 0; + function dfs = [&](int i, int xo) { + for (auto [j, v] : g[i]) { + int x = xo ^ v; + ans += cnt[x]; + for (int k = 0; k < 26; ++k) { + ans += cnt[x ^ (1 << k)]; + } + ++cnt[x]; + dfs(j, x); + } + }; + dfs(0, 0); + return ans; + } +}; \ No newline at end of file diff --git a/solution/2700-2799/2791.Count Paths That Can Form a Palindrome in a Tree/Solution.go b/solution/2700-2799/2791.Count Paths That Can Form a Palindrome in a Tree/Solution.go new file mode 100644 index 0000000000000..3fc3347bb86ab --- /dev/null +++ b/solution/2700-2799/2791.Count Paths That Can Form a Palindrome in a Tree/Solution.go @@ -0,0 +1,24 @@ +func countPalindromePaths(parent []int, s string) (ans int64) { + type pair struct{ i, v int } + n := len(parent) + g := make([][]pair, n) + for i := 1; i < n; i++ { + p := parent[i] + g[p] = append(g[p], pair{i, 1 << (s[i] - 'a')}) + } + cnt := map[int]int{0: 1} + var dfs func(i, xor int) + dfs = func(i, xor int) { + for _, e := range g[i] { + x := xor ^ e.v + ans += int64(cnt[x]) + for k := 0; k < 26; k++ { + ans += int64(cnt[x^(1<[] g; + private Map cnt = new HashMap<>(); + private long ans; + + public long countPalindromePaths(List parent, String s) { + int n = parent.size(); + g = new List[n]; + cnt.put(0, 1); + Arrays.setAll(g, k -> new ArrayList<>()); + for (int i = 1; i < n; ++i) { + int p = parent.get(i); + g[p].add(new int[] {i, 1 << (s.charAt(i) - 'a')}); + } + dfs(0, 0); + return ans; + } + + private void dfs(int i, int xor) { + for (int[] e : g[i]) { + int j = e[0], v = e[1]; + int x = xor ^ v; + ans += cnt.getOrDefault(x, 0); + for (int k = 0; k < 26; ++k) { + ans += cnt.getOrDefault(x ^ (1 << k), 0); + } + cnt.merge(x, 1, Integer::sum); + dfs(j, x); + } + } +} \ No newline at end of file diff --git a/solution/2700-2799/2791.Count Paths That Can Form a Palindrome in a Tree/Solution.py b/solution/2700-2799/2791.Count Paths That Can Form a Palindrome in a Tree/Solution.py new file mode 100644 index 0000000000000..956f3c153c17b --- /dev/null +++ b/solution/2700-2799/2791.Count Paths That Can Form a Palindrome in a Tree/Solution.py @@ -0,0 +1,21 @@ +class Solution: + def countPalindromePaths(self, parent: List[int], s: str) -> int: + def dfs(i: int, xor: int): + nonlocal ans + for j, v in g[i]: + x = xor ^ v + ans += cnt[x] + for k in range(26): + ans += cnt[x ^ (1 << k)] + cnt[x] += 1 + dfs(j, x) + + n = len(parent) + g = defaultdict(list) + for i in range(1, n): + p = parent[i] + g[p].append((i, 1 << (ord(s[i]) - ord('a')))) + ans = 0 + cnt = Counter({0: 1}) + dfs(0, 0) + return ans diff --git a/solution/2700-2799/2791.Count Paths That Can Form a Palindrome in a Tree/Solution.ts b/solution/2700-2799/2791.Count Paths That Can Form a Palindrome in a Tree/Solution.ts new file mode 100644 index 0000000000000..748de190ffca4 --- /dev/null +++ b/solution/2700-2799/2791.Count Paths That Can Form a Palindrome in a Tree/Solution.ts @@ -0,0 +1,23 @@ +function countPalindromePaths(parent: number[], s: string): number { + const n = parent.length; + const g: [number, number][][] = Array.from({ length: n }, () => []); + for (let i = 1; i < n; ++i) { + g[parent[i]].push([i, 1 << (s.charCodeAt(i) - 97)]); + } + const cnt: Map = new Map(); + cnt.set(0, 1); + let ans = 0; + const dfs = (i: number, xor: number): void => { + for (const [j, v] of g[i]) { + const x = xor ^ v; + ans += cnt.get(x) || 0; + for (let k = 0; k < 26; ++k) { + ans += cnt.get(x ^ (1 << k)) || 0; + } + cnt.set(x, (cnt.get(x) || 0) + 1); + dfs(j, x); + } + }; + dfs(0, 0); + return ans; +} diff --git a/solution/2700-2799/2791.Count Paths That Can Form a Palindrome in a Tree/images/treedrawio-8drawio.png b/solution/2700-2799/2791.Count Paths That Can Form a Palindrome in a Tree/images/treedrawio-8drawio.png new file mode 100644 index 0000000000000..e4cfe3832fd00 Binary files /dev/null and b/solution/2700-2799/2791.Count Paths That Can Form a Palindrome in a Tree/images/treedrawio-8drawio.png differ diff --git a/solution/CONTEST_README.md b/solution/CONTEST_README.md index 0ee556159ae6f..8a44287a1ef51 100644 --- a/solution/CONTEST_README.md +++ b/solution/CONTEST_README.md @@ -22,6 +22,20 @@ ## 往期竞赛 +#### 第 355 场周赛(2023-07-23 10:30, 90 分钟) 参赛人数 4112 + +- [2788. 按分隔符拆分字符串](/solution/2700-2799/2788.Split%20Strings%20by%20Separator/README.md) +- [2789. 合并后数组中的最大元素](/solution/2700-2799/2789.Largest%20Element%20in%20an%20Array%20after%20Merge%20Operations/README.md) +- [2790. 长度递增组的最大数目](/solution/2700-2799/2790.Maximum%20Number%20of%20Groups%20With%20Increasing%20Length/README.md) +- [2791. 树中可以形成回文的路径数](/solution/2700-2799/2791.Count%20Paths%20That%20Can%20Form%20a%20Palindrome%20in%20a%20Tree/README.md) + +#### 第 109 场双周赛(2023-07-22 22:30, 90 分钟) 参赛人数 2461 + +- [2784. 检查数组是否是好的](/solution/2700-2799/2784.Check%20if%20Array%20is%20Good/README.md) +- [2785. 将字符串中的元音字母排序](/solution/2700-2799/2785.Sort%20Vowels%20in%20a%20String/README.md) +- [2786. 访问数组中的位置使分数最大](/solution/2700-2799/2786.Visit%20Array%20Positions%20to%20Maximize%20Score/README.md) +- [2787. 将一个数字表示成幂的和的方案数](/solution/2700-2799/2787.Ways%20to%20Express%20an%20Integer%20as%20Sum%20of%20Powers/README.md) + #### 第 354 场周赛(2023-07-16 10:30, 90 分钟) 参赛人数 3957 - [2778. 特殊元素平方和](/solution/2700-2799/2778.Sum%20of%20Squares%20of%20Special%20Elements/README.md) @@ -1371,7 +1385,7 @@ - [1742. 盒子中小球的最大数量](/solution/1700-1799/1742.Maximum%20Number%20of%20Balls%20in%20a%20Box/README.md) - [1743. 从相邻元素对还原数组](/solution/1700-1799/1743.Restore%20the%20Array%20From%20Adjacent%20Pairs/README.md) - [1744. 你能在你最喜欢的那天吃到你最喜欢的糖果吗?](/solution/1700-1799/1744.Can%20You%20Eat%20Your%20Favorite%20Candy%20on%20Your%20Favorite%20Day/README.md) -- [1745. 回文串分割 IV](/solution/1700-1799/1745.Palindrome%20Partitioning%20IV/README.md) +- [1745. 分割回文串 IV](/solution/1700-1799/1745.Palindrome%20Partitioning%20IV/README.md) #### 第 225 场周赛(2021-01-24 10:30, 90 分钟) 参赛人数 3853 diff --git a/solution/CONTEST_README_EN.md b/solution/CONTEST_README_EN.md index 96e1c17fb89b2..72928b4aaab75 100644 --- a/solution/CONTEST_README_EN.md +++ b/solution/CONTEST_README_EN.md @@ -25,6 +25,20 @@ Get your rating changes right after the completion of LeetCode contests, https:/ ## Past Contests +#### Weekly Contest 355 + +- [2788. Split Strings by Separator](/solution/2700-2799/2788.Split%20Strings%20by%20Separator/README_EN.md) +- [2789. Largest Element in an Array after Merge Operations](/solution/2700-2799/2789.Largest%20Element%20in%20an%20Array%20after%20Merge%20Operations/README_EN.md) +- [2790. Maximum Number of Groups With Increasing Length](/solution/2700-2799/2790.Maximum%20Number%20of%20Groups%20With%20Increasing%20Length/README_EN.md) +- [2791. Count Paths That Can Form a Palindrome in a Tree](/solution/2700-2799/2791.Count%20Paths%20That%20Can%20Form%20a%20Palindrome%20in%20a%20Tree/README_EN.md) + +#### Biweekly Contest 109 + +- [2784. Check if Array is Good](/solution/2700-2799/2784.Check%20if%20Array%20is%20Good/README_EN.md) +- [2785. Sort Vowels in a String](/solution/2700-2799/2785.Sort%20Vowels%20in%20a%20String/README_EN.md) +- [2786. Visit Array Positions to Maximize Score](/solution/2700-2799/2786.Visit%20Array%20Positions%20to%20Maximize%20Score/README_EN.md) +- [2787. Ways to Express an Integer as Sum of Powers](/solution/2700-2799/2787.Ways%20to%20Express%20an%20Integer%20as%20Sum%20of%20Powers/README_EN.md) + #### Weekly Contest 354 - [2778. Sum of Squares of Special Elements](/solution/2700-2799/2778.Sum%20of%20Squares%20of%20Special%20Elements/README_EN.md) diff --git a/solution/README.md b/solution/README.md index 097de7ec9c106..12e6526f07824 100644 --- a/solution/README.md +++ b/solution/README.md @@ -38,7 +38,7 @@ | 0025 | [K 个一组翻转链表](/solution/0000-0099/0025.Reverse%20Nodes%20in%20k-Group/README.md) | `递归`,`链表` | 困难 | | | 0026 | [删除有序数组中的重复项](/solution/0000-0099/0026.Remove%20Duplicates%20from%20Sorted%20Array/README.md) | `数组`,`双指针` | 简单 | | | 0027 | [移除元素](/solution/0000-0099/0027.Remove%20Element/README.md) | `数组`,`双指针` | 简单 | | -| 0028 | [找出字符串中第一个匹配项的下标](/solution/0000-0099/0028.Find%20the%20Index%20of%20the%20First%20Occurrence%20in%20a%20String/README.md) | `双指针`,`字符串`,`字符串匹配` | 中等 | | +| 0028 | [找出字符串中第一个匹配项的下标](/solution/0000-0099/0028.Find%20the%20Index%20of%20the%20First%20Occurrence%20in%20a%20String/README.md) | `双指针`,`字符串`,`字符串匹配` | 简单 | | | 0029 | [两数相除](/solution/0000-0099/0029.Divide%20Two%20Integers/README.md) | `位运算`,`数学` | 中等 | | | 0030 | [串联所有单词的子串](/solution/0000-0099/0030.Substring%20with%20Concatenation%20of%20All%20Words/README.md) | `哈希表`,`字符串`,`滑动窗口` | 困难 | | | 0031 | [下一个排列](/solution/0000-0099/0031.Next%20Permutation/README.md) | `数组`,`双指针` | 中等 | | @@ -640,7 +640,7 @@ | 0627 | [变更性别](/solution/0600-0699/0627.Swap%20Salary/README.md) | `数据库` | 简单 | | | 0628 | [三个数的最大乘积](/solution/0600-0699/0628.Maximum%20Product%20of%20Three%20Numbers/README.md) | `数组`,`数学`,`排序` | 简单 | | | 0629 | [K 个逆序对数组](/solution/0600-0699/0629.K%20Inverse%20Pairs%20Array/README.md) | `动态规划` | 困难 | | -| 0630 | [课程表 III](/solution/0600-0699/0630.Course%20Schedule%20III/README.md) | `贪心`,`数组`,`堆(优先队列)` | 困难 | | +| 0630 | [课程表 III](/solution/0600-0699/0630.Course%20Schedule%20III/README.md) | `贪心`,`数组`,`排序`,`堆(优先队列)` | 困难 | | | 0631 | [设计 Excel 求和公式](/solution/0600-0699/0631.Design%20Excel%20Sum%20Formula/README.md) | `图`,`设计`,`拓扑排序` | 困难 | 🔒 | | 0632 | [最小区间](/solution/0600-0699/0632.Smallest%20Range%20Covering%20Elements%20from%20K%20Lists/README.md) | `贪心`,`数组`,`哈希表`,`排序`,`滑动窗口`,`堆(优先队列)` | 困难 | | | 0633 | [平方数之和](/solution/0600-0699/0633.Sum%20of%20Square%20Numbers/README.md) | `数学`,`双指针`,`二分查找` | 中等 | | @@ -1703,7 +1703,7 @@ | 1690 | [石子游戏 VII](/solution/1600-1699/1690.Stone%20Game%20VII/README.md) | `数组`,`数学`,`动态规划`,`博弈` | 中等 | 第 219 场周赛 | | 1691 | [堆叠长方体的最大高度](/solution/1600-1699/1691.Maximum%20Height%20by%20Stacking%20Cuboids/README.md) | `数组`,`动态规划`,`排序` | 困难 | 第 219 场周赛 | | 1692 | [计算分配糖果的不同方式](/solution/1600-1699/1692.Count%20Ways%20to%20Distribute%20Candies/README.md) | `动态规划` | 困难 | 🔒 | -| 1693 | [每天的领导和合伙人](/solution/1600-1699/1693.Daily%20Leads%20and%20Partners/README.md) | | 简单 | | +| 1693 | [每天的领导和合伙人](/solution/1600-1699/1693.Daily%20Leads%20and%20Partners/README.md) | `数据库` | 简单 | | | 1694 | [重新格式化电话号码](/solution/1600-1699/1694.Reformat%20Phone%20Number/README.md) | `字符串` | 简单 | 第 220 场周赛 | | 1695 | [删除子数组的最大得分](/solution/1600-1699/1695.Maximum%20Erasure%20Value/README.md) | `数组`,`哈希表`,`滑动窗口` | 中等 | 第 220 场周赛 | | 1696 | [跳跃游戏 VI](/solution/1600-1699/1696.Jump%20Game%20VI/README.md) | `队列`,`数组`,`动态规划`,`滑动窗口`,`单调队列`,`堆(优先队列)` | 中等 | 第 220 场周赛 | @@ -1755,7 +1755,7 @@ | 1742 | [盒子中小球的最大数量](/solution/1700-1799/1742.Maximum%20Number%20of%20Balls%20in%20a%20Box/README.md) | `哈希表`,`数学`,`计数` | 简单 | 第 226 场周赛 | | 1743 | [从相邻元素对还原数组](/solution/1700-1799/1743.Restore%20the%20Array%20From%20Adjacent%20Pairs/README.md) | `数组`,`哈希表` | 中等 | 第 226 场周赛 | | 1744 | [你能在你最喜欢的那天吃到你最喜欢的糖果吗?](/solution/1700-1799/1744.Can%20You%20Eat%20Your%20Favorite%20Candy%20on%20Your%20Favorite%20Day/README.md) | `数组`,`前缀和` | 中等 | 第 226 场周赛 | -| 1745 | [回文串分割 IV](/solution/1700-1799/1745.Palindrome%20Partitioning%20IV/README.md) | `字符串`,`动态规划` | 困难 | 第 226 场周赛 | +| 1745 | [分割回文串 IV](/solution/1700-1799/1745.Palindrome%20Partitioning%20IV/README.md) | `字符串`,`动态规划` | 困难 | 第 226 场周赛 | | 1746 | [经过一次操作后的最大子数组和](/solution/1700-1799/1746.Maximum%20Subarray%20Sum%20After%20One%20Operation/README.md) | `数组`,`动态规划` | 中等 | 🔒 | | 1747 | [应该被禁止的 Leetflex 账户](/solution/1700-1799/1747.Leetflex%20Banned%20Accounts/README.md) | `数据库` | 中等 | 🔒 | | 1748 | [唯一元素的和](/solution/1700-1799/1748.Sum%20of%20Unique%20Elements/README.md) | `数组`,`哈希表`,`计数` | 简单 | 第 45 场双周赛 | @@ -2222,7 +2222,7 @@ | 2209 | [用地毯覆盖后的最少白色砖块](/solution/2200-2299/2209.Minimum%20White%20Tiles%20After%20Covering%20With%20Carpets/README.md) | `字符串`,`动态规划`,`前缀和` | 困难 | 第 74 场双周赛 | | 2210 | [统计数组中峰和谷的数量](/solution/2200-2299/2210.Count%20Hills%20and%20Valleys%20in%20an%20Array/README.md) | `数组` | 简单 | 第 285 场周赛 | | 2211 | [统计道路上的碰撞次数](/solution/2200-2299/2211.Count%20Collisions%20on%20a%20Road/README.md) | `栈`,`字符串` | 中等 | 第 285 场周赛 | -| 2212 | [射箭比赛中的最大得分](/solution/2200-2299/2212.Maximum%20Points%20in%20an%20Archery%20Competition/README.md) | `位运算`,`递归`,`数组`,`枚举` | 中等 | 第 285 场周赛 | +| 2212 | [射箭比赛中的最大得分](/solution/2200-2299/2212.Maximum%20Points%20in%20an%20Archery%20Competition/README.md) | `位运算`,`数组`,`回溯`,`枚举` | 中等 | 第 285 场周赛 | | 2213 | [由单个字符重复的最长子字符串](/solution/2200-2299/2213.Longest%20Substring%20of%20One%20Repeating%20Character/README.md) | `线段树`,`数组`,`字符串`,`有序集合` | 困难 | 第 285 场周赛 | | 2214 | [通关游戏所需的最低生命值](/solution/2200-2299/2214.Minimum%20Health%20to%20Beat%20Game/README.md) | `贪心`,`数组`,`前缀和` | 中等 | 🔒 | | 2215 | [找出两数组的不同](/solution/2200-2299/2215.Find%20the%20Difference%20of%20Two%20Arrays/README.md) | `数组`,`哈希表` | 简单 | 第 286 场周赛 | @@ -2794,6 +2794,14 @@ | 2781 | [最长合法子字符串的长度](/solution/2700-2799/2781.Length%20of%20the%20Longest%20Valid%20Substring/README.md) | `数组`,`哈希表`,`字符串`,`滑动窗口` | 困难 | 第 354 场周赛 | | 2782 | [唯一类别的数量](/solution/2700-2799/2782.Number%20of%20Unique%20Categories/README.md) | | 中等 | 🔒 | | 2783 | [航班入座率和等待名单分析](/solution/2700-2799/2783.Flight%20Occupancy%20and%20Waitlist%20Analysis/README.md) | | 中等 | 🔒 | +| 2784 | [检查数组是否是好的](/solution/2700-2799/2784.Check%20if%20Array%20is%20Good/README.md) | | 简单 | 第 109 场双周赛 | +| 2785 | [将字符串中的元音字母排序](/solution/2700-2799/2785.Sort%20Vowels%20in%20a%20String/README.md) | | 中等 | 第 109 场双周赛 | +| 2786 | [访问数组中的位置使分数最大](/solution/2700-2799/2786.Visit%20Array%20Positions%20to%20Maximize%20Score/README.md) | | 中等 | 第 109 场双周赛 | +| 2787 | [将一个数字表示成幂的和的方案数](/solution/2700-2799/2787.Ways%20to%20Express%20an%20Integer%20as%20Sum%20of%20Powers/README.md) | | 中等 | 第 109 场双周赛 | +| 2788 | [按分隔符拆分字符串](/solution/2700-2799/2788.Split%20Strings%20by%20Separator/README.md) | | 简单 | 第 355 场周赛 | +| 2789 | [合并后数组中的最大元素](/solution/2700-2799/2789.Largest%20Element%20in%20an%20Array%20after%20Merge%20Operations/README.md) | | 中等 | 第 355 场周赛 | +| 2790 | [长度递增组的最大数目](/solution/2700-2799/2790.Maximum%20Number%20of%20Groups%20With%20Increasing%20Length/README.md) | | 困难 | 第 355 场周赛 | +| 2791 | [树中可以形成回文的路径数](/solution/2700-2799/2791.Count%20Paths%20That%20Can%20Form%20a%20Palindrome%20in%20a%20Tree/README.md) | | 困难 | 第 355 场周赛 | ## 版权 diff --git a/solution/README_EN.md b/solution/README_EN.md index a73ec1bd2b45a..b9b623819698c 100644 --- a/solution/README_EN.md +++ b/solution/README_EN.md @@ -36,7 +36,7 @@ Press Control+F(or Command+F on the | 0025 | [Reverse Nodes in k-Group](/solution/0000-0099/0025.Reverse%20Nodes%20in%20k-Group/README_EN.md) | `Recursion`,`Linked List` | Hard | | | 0026 | [Remove Duplicates from Sorted Array](/solution/0000-0099/0026.Remove%20Duplicates%20from%20Sorted%20Array/README_EN.md) | `Array`,`Two Pointers` | Easy | | | 0027 | [Remove Element](/solution/0000-0099/0027.Remove%20Element/README_EN.md) | `Array`,`Two Pointers` | Easy | | -| 0028 | [Find the Index of the First Occurrence in a String](/solution/0000-0099/0028.Find%20the%20Index%20of%20the%20First%20Occurrence%20in%20a%20String/README_EN.md) | `Two Pointers`,`String`,`String Matching` | Medium | | +| 0028 | [Find the Index of the First Occurrence in a String](/solution/0000-0099/0028.Find%20the%20Index%20of%20the%20First%20Occurrence%20in%20a%20String/README_EN.md) | `Two Pointers`,`String`,`String Matching` | Easy | | | 0029 | [Divide Two Integers](/solution/0000-0099/0029.Divide%20Two%20Integers/README_EN.md) | `Bit Manipulation`,`Math` | Medium | | | 0030 | [Substring with Concatenation of All Words](/solution/0000-0099/0030.Substring%20with%20Concatenation%20of%20All%20Words/README_EN.md) | `Hash Table`,`String`,`Sliding Window` | Hard | | | 0031 | [Next Permutation](/solution/0000-0099/0031.Next%20Permutation/README_EN.md) | `Array`,`Two Pointers` | Medium | | @@ -638,7 +638,7 @@ Press Control+F(or Command+F on the | 0627 | [Swap Salary](/solution/0600-0699/0627.Swap%20Salary/README_EN.md) | `Database` | Easy | | | 0628 | [Maximum Product of Three Numbers](/solution/0600-0699/0628.Maximum%20Product%20of%20Three%20Numbers/README_EN.md) | `Array`,`Math`,`Sorting` | Easy | | | 0629 | [K Inverse Pairs Array](/solution/0600-0699/0629.K%20Inverse%20Pairs%20Array/README_EN.md) | `Dynamic Programming` | Hard | | -| 0630 | [Course Schedule III](/solution/0600-0699/0630.Course%20Schedule%20III/README_EN.md) | `Greedy`,`Array`,`Heap (Priority Queue)` | Hard | | +| 0630 | [Course Schedule III](/solution/0600-0699/0630.Course%20Schedule%20III/README_EN.md) | `Greedy`,`Array`,`Sorting`,`Heap (Priority Queue)` | Hard | | | 0631 | [Design Excel Sum Formula](/solution/0600-0699/0631.Design%20Excel%20Sum%20Formula/README_EN.md) | `Graph`,`Design`,`Topological Sort` | Hard | 🔒 | | 0632 | [Smallest Range Covering Elements from K Lists](/solution/0600-0699/0632.Smallest%20Range%20Covering%20Elements%20from%20K%20Lists/README_EN.md) | `Greedy`,`Array`,`Hash Table`,`Sorting`,`Sliding Window`,`Heap (Priority Queue)` | Hard | | | 0633 | [Sum of Square Numbers](/solution/0600-0699/0633.Sum%20of%20Square%20Numbers/README_EN.md) | `Math`,`Two Pointers`,`Binary Search` | Medium | | @@ -1701,7 +1701,7 @@ Press Control+F(or Command+F on the | 1690 | [Stone Game VII](/solution/1600-1699/1690.Stone%20Game%20VII/README_EN.md) | `Array`,`Math`,`Dynamic Programming`,`Game Theory` | Medium | Weekly Contest 219 | | 1691 | [Maximum Height by Stacking Cuboids](/solution/1600-1699/1691.Maximum%20Height%20by%20Stacking%20Cuboids/README_EN.md) | `Array`,`Dynamic Programming`,`Sorting` | Hard | Weekly Contest 219 | | 1692 | [Count Ways to Distribute Candies](/solution/1600-1699/1692.Count%20Ways%20to%20Distribute%20Candies/README_EN.md) | `Dynamic Programming` | Hard | 🔒 | -| 1693 | [Daily Leads and Partners](/solution/1600-1699/1693.Daily%20Leads%20and%20Partners/README_EN.md) | | Easy | | +| 1693 | [Daily Leads and Partners](/solution/1600-1699/1693.Daily%20Leads%20and%20Partners/README_EN.md) | `Database` | Easy | | | 1694 | [Reformat Phone Number](/solution/1600-1699/1694.Reformat%20Phone%20Number/README_EN.md) | `String` | Easy | Weekly Contest 220 | | 1695 | [Maximum Erasure Value](/solution/1600-1699/1695.Maximum%20Erasure%20Value/README_EN.md) | `Array`,`Hash Table`,`Sliding Window` | Medium | Weekly Contest 220 | | 1696 | [Jump Game VI](/solution/1600-1699/1696.Jump%20Game%20VI/README_EN.md) | `Queue`,`Array`,`Dynamic Programming`,`Sliding Window`,`Monotonic Queue`,`Heap (Priority Queue)` | Medium | Weekly Contest 220 | @@ -2220,7 +2220,7 @@ Press Control+F(or Command+F on the | 2209 | [Minimum White Tiles After Covering With Carpets](/solution/2200-2299/2209.Minimum%20White%20Tiles%20After%20Covering%20With%20Carpets/README_EN.md) | `String`,`Dynamic Programming`,`Prefix Sum` | Hard | Biweekly Contest 74 | | 2210 | [Count Hills and Valleys in an Array](/solution/2200-2299/2210.Count%20Hills%20and%20Valleys%20in%20an%20Array/README_EN.md) | `Array` | Easy | Weekly Contest 285 | | 2211 | [Count Collisions on a Road](/solution/2200-2299/2211.Count%20Collisions%20on%20a%20Road/README_EN.md) | `Stack`,`String` | Medium | Weekly Contest 285 | -| 2212 | [Maximum Points in an Archery Competition](/solution/2200-2299/2212.Maximum%20Points%20in%20an%20Archery%20Competition/README_EN.md) | `Bit Manipulation`,`Recursion`,`Array`,`Enumeration` | Medium | Weekly Contest 285 | +| 2212 | [Maximum Points in an Archery Competition](/solution/2200-2299/2212.Maximum%20Points%20in%20an%20Archery%20Competition/README_EN.md) | `Bit Manipulation`,`Array`,`Backtracking`,`Enumeration` | Medium | Weekly Contest 285 | | 2213 | [Longest Substring of One Repeating Character](/solution/2200-2299/2213.Longest%20Substring%20of%20One%20Repeating%20Character/README_EN.md) | `Segment Tree`,`Array`,`String`,`Ordered Set` | Hard | Weekly Contest 285 | | 2214 | [Minimum Health to Beat Game](/solution/2200-2299/2214.Minimum%20Health%20to%20Beat%20Game/README_EN.md) | `Greedy`,`Array`,`Prefix Sum` | Medium | 🔒 | | 2215 | [Find the Difference of Two Arrays](/solution/2200-2299/2215.Find%20the%20Difference%20of%20Two%20Arrays/README_EN.md) | `Array`,`Hash Table` | Easy | Weekly Contest 286 | @@ -2792,6 +2792,14 @@ Press Control+F(or Command+F on the | 2781 | [Length of the Longest Valid Substring](/solution/2700-2799/2781.Length%20of%20the%20Longest%20Valid%20Substring/README_EN.md) | `Array`,`Hash Table`,`String`,`Sliding Window` | Hard | Weekly Contest 354 | | 2782 | [Number of Unique Categories](/solution/2700-2799/2782.Number%20of%20Unique%20Categories/README_EN.md) | | Medium | 🔒 | | 2783 | [Flight Occupancy and Waitlist Analysis](/solution/2700-2799/2783.Flight%20Occupancy%20and%20Waitlist%20Analysis/README_EN.md) | | Medium | 🔒 | +| 2784 | [Check if Array is Good](/solution/2700-2799/2784.Check%20if%20Array%20is%20Good/README_EN.md) | | Easy | Biweekly Contest 109 | +| 2785 | [Sort Vowels in a String](/solution/2700-2799/2785.Sort%20Vowels%20in%20a%20String/README_EN.md) | | Medium | Biweekly Contest 109 | +| 2786 | [Visit Array Positions to Maximize Score](/solution/2700-2799/2786.Visit%20Array%20Positions%20to%20Maximize%20Score/README_EN.md) | | Medium | Biweekly Contest 109 | +| 2787 | [Ways to Express an Integer as Sum of Powers](/solution/2700-2799/2787.Ways%20to%20Express%20an%20Integer%20as%20Sum%20of%20Powers/README_EN.md) | | Medium | Biweekly Contest 109 | +| 2788 | [Split Strings by Separator](/solution/2700-2799/2788.Split%20Strings%20by%20Separator/README_EN.md) | | Easy | Weekly Contest 355 | +| 2789 | [Largest Element in an Array after Merge Operations](/solution/2700-2799/2789.Largest%20Element%20in%20an%20Array%20after%20Merge%20Operations/README_EN.md) | | Medium | Weekly Contest 355 | +| 2790 | [Maximum Number of Groups With Increasing Length](/solution/2700-2799/2790.Maximum%20Number%20of%20Groups%20With%20Increasing%20Length/README_EN.md) | | Hard | Weekly Contest 355 | +| 2791 | [Count Paths That Can Form a Palindrome in a Tree](/solution/2700-2799/2791.Count%20Paths%20That%20Can%20Form%20a%20Palindrome%20in%20a%20Tree/README_EN.md) | | Hard | Weekly Contest 355 | ## Copyright diff --git a/solution/summary.md b/solution/summary.md index 8eb9cc8da24c0..96b1bcf684bc4 100644 --- a/solution/summary.md +++ b/solution/summary.md @@ -1778,7 +1778,7 @@ - [1742.盒子中小球的最大数量](/solution/1700-1799/1742.Maximum%20Number%20of%20Balls%20in%20a%20Box/README.md) - [1743.从相邻元素对还原数组](/solution/1700-1799/1743.Restore%20the%20Array%20From%20Adjacent%20Pairs/README.md) - [1744.你能在你最喜欢的那天吃到你最喜欢的糖果吗?](/solution/1700-1799/1744.Can%20You%20Eat%20Your%20Favorite%20Candy%20on%20Your%20Favorite%20Day/README.md) - - [1745.回文串分割 IV](/solution/1700-1799/1745.Palindrome%20Partitioning%20IV/README.md) + - [1745.分割回文串 IV](/solution/1700-1799/1745.Palindrome%20Partitioning%20IV/README.md) - [1746.经过一次操作后的最大子数组和](/solution/1700-1799/1746.Maximum%20Subarray%20Sum%20After%20One%20Operation/README.md) - [1747.应该被禁止的 Leetflex 账户](/solution/1700-1799/1747.Leetflex%20Banned%20Accounts/README.md) - [1748.唯一元素的和](/solution/1700-1799/1748.Sum%20of%20Unique%20Elements/README.md) @@ -2837,3 +2837,11 @@ - [2781.最长合法子字符串的长度](/solution/2700-2799/2781.Length%20of%20the%20Longest%20Valid%20Substring/README.md) - [2782.唯一类别的数量](/solution/2700-2799/2782.Number%20of%20Unique%20Categories/README.md) - [2783.航班入座率和等待名单分析](/solution/2700-2799/2783.Flight%20Occupancy%20and%20Waitlist%20Analysis/README.md) + - [2784.检查数组是否是好的](/solution/2700-2799/2784.Check%20if%20Array%20is%20Good/README.md) + - [2785.将字符串中的元音字母排序](/solution/2700-2799/2785.Sort%20Vowels%20in%20a%20String/README.md) + - [2786.访问数组中的位置使分数最大](/solution/2700-2799/2786.Visit%20Array%20Positions%20to%20Maximize%20Score/README.md) + - [2787.将一个数字表示成幂的和的方案数](/solution/2700-2799/2787.Ways%20to%20Express%20an%20Integer%20as%20Sum%20of%20Powers/README.md) + - [2788.按分隔符拆分字符串](/solution/2700-2799/2788.Split%20Strings%20by%20Separator/README.md) + - [2789.合并后数组中的最大元素](/solution/2700-2799/2789.Largest%20Element%20in%20an%20Array%20after%20Merge%20Operations/README.md) + - [2790.长度递增组的最大数目](/solution/2700-2799/2790.Maximum%20Number%20of%20Groups%20With%20Increasing%20Length/README.md) + - [2791.树中可以形成回文的路径数](/solution/2700-2799/2791.Count%20Paths%20That%20Can%20Form%20a%20Palindrome%20in%20a%20Tree/README.md) diff --git a/solution/summary_en.md b/solution/summary_en.md index 67fef65afc1e5..46a88e58ad64d 100644 --- a/solution/summary_en.md +++ b/solution/summary_en.md @@ -2837,3 +2837,11 @@ - [2781.Length of the Longest Valid Substring](/solution/2700-2799/2781.Length%20of%20the%20Longest%20Valid%20Substring/README_EN.md) - [2782.Number of Unique Categories](/solution/2700-2799/2782.Number%20of%20Unique%20Categories/README_EN.md) - [2783.Flight Occupancy and Waitlist Analysis](/solution/2700-2799/2783.Flight%20Occupancy%20and%20Waitlist%20Analysis/README_EN.md) + - [2784.Check if Array is Good](/solution/2700-2799/2784.Check%20if%20Array%20is%20Good/README_EN.md) + - [2785.Sort Vowels in a String](/solution/2700-2799/2785.Sort%20Vowels%20in%20a%20String/README_EN.md) + - [2786.Visit Array Positions to Maximize Score](/solution/2700-2799/2786.Visit%20Array%20Positions%20to%20Maximize%20Score/README_EN.md) + - [2787.Ways to Express an Integer as Sum of Powers](/solution/2700-2799/2787.Ways%20to%20Express%20an%20Integer%20as%20Sum%20of%20Powers/README_EN.md) + - [2788.Split Strings by Separator](/solution/2700-2799/2788.Split%20Strings%20by%20Separator/README_EN.md) + - [2789.Largest Element in an Array after Merge Operations](/solution/2700-2799/2789.Largest%20Element%20in%20an%20Array%20after%20Merge%20Operations/README_EN.md) + - [2790.Maximum Number of Groups With Increasing Length](/solution/2700-2799/2790.Maximum%20Number%20of%20Groups%20With%20Increasing%20Length/README_EN.md) + - [2791.Count Paths That Can Form a Palindrome in a Tree](/solution/2700-2799/2791.Count%20Paths%20That%20Can%20Form%20a%20Palindrome%20in%20a%20Tree/README_EN.md)