diff --git a/solution/0000-0099/0020.Valid Parentheses/README_EN.md b/solution/0000-0099/0020.Valid Parentheses/README_EN.md index 427aaa6553630..cf0fc8a16deba 100644 --- a/solution/0000-0099/0020.Valid Parentheses/README_EN.md +++ b/solution/0000-0099/0020.Valid Parentheses/README_EN.md @@ -30,24 +30,35 @@ tags:
Example 1:
--Input: s = "()" -Output: true -+
Input: s = "()"
+ +Output: true
+Example 2:
--Input: s = "()[]{}" -Output: true -+
Input: s = "()[]{}"
+ +Output: true
+Example 3:
--Input: s = "(]" -Output: false -+
Input: s = "(]"
+ +Output: false
+Example 4:
+ +Input: s = "([])"
+ +Output: true
+
Constraints:
diff --git a/solution/0100-0199/0134.Gas Station/README_EN.md b/solution/0100-0199/0134.Gas Station/README_EN.md index 33105cc79f9c8..da715b425416d 100644 --- a/solution/0100-0199/0134.Gas Station/README_EN.md +++ b/solution/0100-0199/0134.Gas Station/README_EN.md @@ -21,7 +21,7 @@ tags:You have a car with an unlimited gas tank and it costs cost[i]
of gas to travel from the ith
station to its next (i + 1)th
station. You begin the journey with an empty tank at one of the gas stations.
Given two integer arrays gas
and cost
, return the starting gas station's index if you can travel around the circuit once in the clockwise direction, otherwise return -1
. If there exists a solution, it is guaranteed to be unique
Given two integer arrays gas
and cost
, return the starting gas station's index if you can travel around the circuit once in the clockwise direction, otherwise return -1
. If there exists a solution, it is guaranteed to be unique.
Example 1:
diff --git a/solution/0100-0199/0152.Maximum Product Subarray/README.md b/solution/0100-0199/0152.Maximum Product Subarray/README.md index 73a092c061375..8749d1f0a4a19 100644 --- a/solution/0100-0199/0152.Maximum Product Subarray/README.md +++ b/solution/0100-0199/0152.Maximum Product Subarray/README.md @@ -17,7 +17,7 @@ tags: -给你一个整数数组 nums
,请你找出数组中乘积最大的非空连续子数组(该子数组中至少包含一个数字),并返回该子数组所对应的乘积。
给你一个整数数组 nums
,请你找出数组中乘积最大的非空连续 子数组(该子数组中至少包含一个数字),并返回该子数组所对应的乘积。
测试用例的答案是一个 32-位 整数。
@@ -45,7 +45,7 @@ tags:1 <= nums.length <= 2 * 104
-10 <= nums[i] <= 10
nums
的任何前缀或后缀的乘积都 保证 是一个 32-位 整数nums
的任何子数组的乘积都 保证 是一个 32-位 整数1 <= nums.length <= 2 * 104
-10 <= nums[i] <= 10
nums
is guaranteed to fit in a 32-bit integer.nums
is guaranteed to fit in a 32-bit integer.给定一个整数,编写一个算法将这个数转换为十六进制数。对于负整数,我们通常使用 补码运算 方法。
-注意:
+答案字符串中的所有字母都应该是小写字符,并且除了 0 本身之外,答案中不应该有任何前置零。
-a-f
)都必须是小写。'0'
来表示;对于其他情况,十六进制字符串中的第一个字符将不会是0字符。 注意: 不允许使用任何由库提供的将数字直接转换或格式化为十六进制的方法来解决这个问题。
-示例 1:
+-
-输入: -26 +示例 1:
-输出: -"1a" ++输入:num = 26 +输出:"1a"-示例 2:
+示例 2:
-输入: --1 - -输出: -"ffffffff" +输入:num = -1 +输出:"ffffffff"++ +
提示:
+ +
-231 <= num <= 231 - 1
Given an integer num
, return a string representing its hexadecimal representation. For negative integers, two’s complement method is used.
Given a 32-bit integer num
, return a string representing its hexadecimal representation. For negative integers, two’s complement method is used.
All the letters in the answer string should be lowercase characters, and there should not be any leading zeros in the answer except for the zero itself.
diff --git a/solution/0400-0499/0442.Find All Duplicates in an Array/README.md b/solution/0400-0499/0442.Find All Duplicates in an Array/README.md index 1486960699bcd..e84c30318cc2c 100644 --- a/solution/0400-0499/0442.Find All Duplicates in an Array/README.md +++ b/solution/0400-0499/0442.Find All Duplicates in an Array/README.md @@ -19,7 +19,7 @@ tags:给你一个长度为 n
的整数数组 nums
,其中 nums
的所有整数都在范围 [1, n]
内,且每个整数出现 一次 或 两次 。请你找出所有出现 两次 的整数,并以数组形式返回。
你必须设计并实现一个时间复杂度为 O(n)
且仅使用常量额外空间的算法解决此问题。
你必须设计并实现一个时间复杂度为 O(n)
且仅使用常量额外空间(不包括存储输出所需的空间)的算法解决此问题。
diff --git a/solution/0400-0499/0442.Find All Duplicates in an Array/README_EN.md b/solution/0400-0499/0442.Find All Duplicates in an Array/README_EN.md index 361061fbb722e..3ece71c0a4ce0 100644 --- a/solution/0400-0499/0442.Find All Duplicates in an Array/README_EN.md +++ b/solution/0400-0499/0442.Find All Duplicates in an Array/README_EN.md @@ -19,7 +19,7 @@ tags:
Given an integer array nums
of length n
where all the integers of nums
are in the range [1, n]
and each integer appears once or twice, return an array of all the integers that appears twice.
You must write an algorithm that runs in O(n)
time and uses only constant extra space.
You must write an algorithm that runs in O(n)
time and uses only constant auxiliary space, excluding the space needed to store the output
Example 1:
diff --git a/solution/0400-0499/0451.Sort Characters By Frequency/README.md b/solution/0400-0499/0451.Sort Characters By Frequency/README.md index aafb28f4cfe41..777e1e8ee609a 100644 --- a/solution/0400-0499/0451.Sort Characters By Frequency/README.md +++ b/solution/0400-0499/0451.Sort Characters By Frequency/README.md @@ -27,7 +27,7 @@ tags:-
示例 1:
+示例 1:
输入: s = "tree" @@ -36,7 +36,7 @@ tags: 因此'e'必须出现在'r'和't'之前。此外,"eetr"也是一个有效的答案。-
示例 2:
+示例 2:
输入: s = "cccaaa" @@ -45,7 +45,7 @@ tags: 注意"cacaca"是不正确的,因为相同的字母必须放在一起。-
示例 3:
+示例 3:
输入: s = "Aabb" diff --git a/solution/0400-0499/0455.Assign Cookies/README.md b/solution/0400-0499/0455.Assign Cookies/README.md index 94e99259c1f2b..b5b132e42c223 100644 --- a/solution/0400-0499/0455.Assign Cookies/README.md +++ b/solution/0400-0499/0455.Assign Cookies/README.md @@ -21,41 +21,45 @@ tags:假设你是一位很棒的家长,想要给你的孩子们一些小饼干。但是,每个孩子最多只能给一块饼干。
-对每个孩子
- +i
,都有一个胃口值g[i]
,这是能让孩子们满足胃口的饼干的最小尺寸;并且每块饼干j
,都有一个尺寸s[j]
。如果s[j] >= g[i]
,我们可以将这个饼干j
分配给孩子i
,这个孩子会得到满足。你的目标是尽可能满足越多数量的孩子,并输出这个最大数值。对每个孩子
+ -i
,都有一个胃口值g[i]
,这是能让孩子们满足胃口的饼干的最小尺寸;并且每块饼干j
,都有一个尺寸s[j]
。如果s[j] >= g[i]
,我们可以将这个饼干j
分配给孩子i
,这个孩子会得到满足。你的目标是尽可能满足越多数量的孩子,并输出这个最大数值。示例 1:
+示例 1:
输入: g = [1,2,3], s = [1,1] 输出: 1 解释: -你有三个孩子和两块小饼干,3个孩子的胃口值分别是:1,2,3。 -虽然你有两块小饼干,由于他们的尺寸都是1,你只能让胃口值是1的孩子满足。 -所以你应该输出1。 +你有三个孩子和两块小饼干,3 个孩子的胃口值分别是:1,2,3。 +虽然你有两块小饼干,由于他们的尺寸都是 1,你只能让胃口值是 1 的孩子满足。 +所以你应该输出 1。-示例 2:
+示例 2:
输入: g = [1,2], s = [1,2,3] 输出: 2 解释: -你有两个孩子和三块小饼干,2个孩子的胃口值分别是1,2。 +你有两个孩子和三块小饼干,2 个孩子的胃口值分别是 1,2。 你拥有的饼干数量和尺寸都足以让所有孩子满足。 -所以你应该输出2. +所以你应该输出 2。-+
提示:
1 <= g.length <= 3 * 104
0 <= s.length <= 3 * 104
1 <= g[i], s[j] <= 231 - 1
1 <= g.length <= 3 * 104
0 <= s.length <= 3 * 104
1 <= g[i], s[j] <= 231 - 1
+ +
注意:本题与 2410. 运动员和训练师的最大匹配数 题相同。
+ ## 解法 diff --git a/solution/0400-0499/0455.Assign Cookies/README_EN.md b/solution/0400-0499/0455.Assign Cookies/README_EN.md index c67de754267d9..80aa932c46378 100644 --- a/solution/0400-0499/0455.Assign Cookies/README_EN.md +++ b/solution/0400-0499/0455.Assign Cookies/README_EN.md @@ -53,6 +53,9 @@ You need to output 2.1 <= g[i], s[j] <= 231 - 1
+
Note: This question is the same as 2410: Maximum Matching of Players With Trainers.
+ ## Solutions 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 e09529c3d876d..2f0696381ea24 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 @@ -8,7 +8,7 @@ tags: -# [596. 超过5名学生的课](https://leetcode.cn/problems/classes-more-than-5-students) +# [596. 超过 5 名学生的课](https://leetcode.cn/problems/classes-more-than-5-students) [English Version](/solution/0500-0599/0596.Classes%20More%20Than%205%20Students/README_EN.md) @@ -25,21 +25,21 @@ tags: | student | varchar | | class | varchar | +-------------+---------+ -在 SQL 中,(student, class)是该表的主键列。 +(student, class)是该表的主键(不同值的列的组合)。 该表的每一行表示学生的名字和他们注册的班级。-
查询 至少有5个学生 的所有班级。
+查询 至少有 5 个学生 的所有班级。
以 任意顺序 返回结果表。
-查询结果格式如下所示。
+结果格式如下所示。
-
示例 1:
+示例 1:
输入: @@ -64,10 +64,10 @@ Courses table: | Math | +---------+ 解释: --数学课有6个学生,所以我们包括它。 --英语课有1名学生,所以我们不包括它。 --生物课有1名学生,所以我们不包括它。 --计算机课有1个学生,所以我们不包括它。+-数学课有 6 个学生,所以我们包括它。 +-英语课有 1 名学生,所以我们不包括它。 +-生物课有 1 名学生,所以我们不包括它。 +-计算机课有 1 个学生,所以我们不包括它。 diff --git a/solution/0600-0699/0608.Tree Node/README.md b/solution/0600-0699/0608.Tree Node/README.md index be3cea39ad54f..536d9fee784ba 100644 --- a/solution/0600-0699/0608.Tree Node/README.md +++ b/solution/0600-0699/0608.Tree Node/README.md @@ -97,6 +97,10 @@ Tree table: 解释:如果树中只有一个节点,则只需要输出其根属性。 +
+ +
注意:本题与 3054. 二叉树节点 一致。
+ ## 解法 diff --git a/solution/0600-0699/0608.Tree Node/README_EN.md b/solution/0600-0699/0608.Tree Node/README_EN.md index e297ff967f37e..7844b95107b07 100644 --- a/solution/0600-0699/0608.Tree Node/README_EN.md +++ b/solution/0600-0699/0608.Tree Node/README_EN.md @@ -96,6 +96,9 @@ Tree table: Explanation: If there is only one node on the tree, you only need to output its root attributes. ++
Note: This question is the same as 3054: Binary Tree Nodes.
+ ## Solutions diff --git a/solution/0700-0799/0703.Kth Largest Element in a Stream/README.md b/solution/0700-0799/0703.Kth Largest Element in a Stream/README.md index 689934a897b33..4d45554a49337 100644 --- a/solution/0700-0799/0703.Kth Largest Element in a Stream/README.md +++ b/solution/0700-0799/0703.Kth Largest Element in a Stream/README.md @@ -23,41 +23,60 @@ tags:设计一个找到数据流中第 k
大元素的类(class)。注意是排序后的第 k
大元素,不是第 k
个不同的元素。
请实现 KthLargest
类:
请实现 KthLargest
类:
KthLargest(int k, int[] nums)
使用整数 k
和整数流 nums
初始化对象。int add(int val)
将 val
插入数据流 nums
后,返回当前数据流中第 k
大的元素。+
-
示例:
+示例 1:
--输入: -["KthLargest", "add", "add", "add", "add", "add"] -[[3, [4, 5, 8, 2]], [3], [5], [10], [9], [4]] -输出: -[null, 4, 5, 5, 8, 8] +++ +输入:
-解释: -KthLargest kthLargest = new KthLargest(3, [4, 5, 8, 2]); -kthLargest.add(3); // return 4 -kthLargest.add(5); // return 5 -kthLargest.add(10); // return 5 -kthLargest.add(9); // return 8 -kthLargest.add(4); // return 8 - +
+["KthLargest", "add", "add", "add", "add", "add"]
+[[3, [4, 5, 8, 2]], [3], [5], [10], [9], [4]]输出:[null, 4, 5, 5, 8, 8]
-+
解释:
+ +KthLargest kthLargest = new KthLargest(3, [4, 5, 8, 2]);
+ +
+kthLargest.add(3); // 返回 4
+kthLargest.add(5); // 返回 5
+kthLargest.add(10); // 返回 5
+kthLargest.add(9); // 返回 8
+kthLargest.add(4); // 返回 8+
示例 2:
+ +++ +输入:
+ +
+["KthLargest", "add", "add", "add", "add"]
+[[4, [7, 7, 7, 7, 8, 3]], [2], [10], [9], [9]]输出:[null, 7, 7, 7, 8]
+ +解释:
+KthLargest kthLargest = new KthLargest(4, [7, 7, 7, 7, 8, 3]);
+kthLargest.add(2); // 返回 7
+kthLargest.add(10); // 返回 7
+kthLargest.add(9); // 返回 7
+kthLargest.add(9); // 返回 8提示:
1 <= k <= 104
0 <= nums.length <= 104
-104 <= nums[i] <= 104
-104 <= val <= 104
1 <= k <= 104
0 <= nums.length <= 104
-104 <= nums[i] <= 104
-104 <= val <= 104
add
方法 104
次k
大元素时,数组中至少有 k
个元素Design a class to find the kth
largest element in a stream. Note that it is the kth
largest element in the sorted order, not the kth
distinct element.
You are part of a university admissions office and need to keep track of the kth
highest test score from applicants in real-time. This helps to determine cut-off marks for interviews and admissions dynamically as new applicants submit their scores.
Implement KthLargest
class:
You are tasked to implement a class which, for a given integer k
, maintains a stream of test scores and continuously returns the k
th highest test score after a new score has been submitted. More specifically, we are looking for the k
th highest score in the sorted list of all scores.
Implement the KthLargest
class:
KthLargest(int k, int[] nums)
Initializes the object with the integer k
and the stream of integers nums
.int add(int val)
Appends the integer val
to the stream and returns the element representing the kth
largest element in the stream.KthLargest(int k, int[] nums)
Initializes the object with the integer k
and the stream of test scores nums
.int add(int val)
Adds a new test score val
to the stream and returns the element representing the kth
largest element in the pool of test scores so far.
Example 1:
--Input -["KthLargest", "add", "add", "add", "add", "add"] -[[3, [4, 5, 8, 2]], [3], [5], [10], [9], [4]] -Output -[null, 4, 5, 5, 8, 8] - -Explanation -KthLargest kthLargest = new KthLargest(3, [4, 5, 8, 2]); -kthLargest.add(3); // return 4 -kthLargest.add(5); // return 5 -kthLargest.add(10); // return 5 -kthLargest.add(9); // return 8 -kthLargest.add(4); // return 8 -+
Input:
+["KthLargest", "add", "add", "add", "add", "add"]
+[[3, [4, 5, 8, 2]], [3], [5], [10], [9], [4]]
Output: [null, 4, 5, 5, 8, 8]
+ +Explanation:
+ +KthLargest kthLargest = new KthLargest(3, [4, 5, 8, 2]);
+kthLargest.add(3); // return 4
+kthLargest.add(5); // return 5
+kthLargest.add(10); // return 5
+kthLargest.add(9); // return 8
+kthLargest.add(4); // return 8
Example 2:
+ +Input:
+["KthLargest", "add", "add", "add", "add"]
+[[4, [7, 7, 7, 7, 8, 3]], [2], [10], [9], [9]]
Output: [null, 7, 7, 7, 8]
+ +Explanation:
+KthLargest kthLargest = new KthLargest(4, [7, 7, 7, 7, 8, 3]);
Constraints:
1 <= k <= 104
0 <= nums.length <= 104
1 <= nums.length <= 104
1 <= k <= nums.length + 1
-104 <= nums[i] <= 104
-104 <= val <= 104
104
calls will be made to add
.k
elements in the array when you search for the kth
element.diff --git a/solution/0700-0799/0791.Custom Sort String/README_EN.md b/solution/0700-0799/0791.Custom Sort String/README_EN.md index ddc2e942c3849..dd8b76f10b181 100644 --- a/solution/0700-0799/0791.Custom Sort String/README_EN.md +++ b/solution/0700-0799/0791.Custom Sort String/README_EN.md @@ -46,7 +46,7 @@ tags:
Explanation: The characters "b"
, "c"
, and "a"
from order
dictate the order for the characters in s
. The character "d"
in s
does not appear in order
, so its position is flexible.
Following the order of appearance in order
, "b"
, "c"
, and "a"
from s
should be arranged as "b"
, "c"
, "a"
. "d"
can be placed at any position since it's not in order. The output "bcad"
correctly follows this rule. Other arrangements like "bacd"
or "bcda"
would also be valid, as long as "b"
, "c"
, "a"
maintain their order.
Following the order of appearance in order
, "b"
, "c"
, and "a"
from s
should be arranged as "b"
, "c"
, "a"
. "d"
can be placed at any position since it's not in order. The output "bcad"
correctly follows this rule. Other arrangements like "dbca"
or "bcda"
would also be valid, as long as "b"
, "c"
, "a"
maintain their order.
diff --git a/solution/0800-0899/0840.Magic Squares In Grid/README_EN.md b/solution/0800-0899/0840.Magic Squares In Grid/README_EN.md index 70ee41f412e26..b71101923e713 100644 --- a/solution/0800-0899/0840.Magic Squares In Grid/README_EN.md +++ b/solution/0800-0899/0840.Magic Squares In Grid/README_EN.md @@ -21,7 +21,7 @@ tags:
A 3 x 3
magic square is a 3 x 3
grid filled with distinct numbers from 1 to 9 such that each row, column, and both diagonals all have the same sum.
Given a row x col
grid
of integers, how many 3 x 3
contiguous magic square subgrids are there?
Given a row x col
grid
of integers, how many 3 x 3
magic square subgrids are there?
Note: while a magic square can only contain numbers from 1 to 9, grid
may contain numbers up to 15.
r
行 c
列的棋盘,按前述方法编号,棋盘格中可能存在 “蛇” 或 “梯子”;如果 board[r][c] != -1
,那个蛇或梯子的目的地将会是 board[r][c]
。编号为 1
和 n2
的方格上没有蛇或梯子。
r
行 c
列的棋盘,按前述方法编号,棋盘格中可能存在 “蛇” 或 “梯子”;如果 board[r][c] != -1
,那个蛇或梯子的目的地将会是 board[r][c]
。编号为 1
和 n2
的方格不是任何蛇或梯子的起点。
注意,玩家在每回合的前进过程中最多只能爬过蛇或梯子一次:就算目的地是另一条蛇或梯子的起点,玩家也 不能 继续移动。
@@ -48,7 +48,7 @@ tags:-
示例 1:
+示例 1:
输入:board = [[-1,-1,-1,-1,-1,-1],[-1,-1,-1,-1,-1,-1],[-1,-1,-1,-1,-1,-1],[-1,35,-1,-1,13,-1],[-1,-1,-1,-1,-1,-1],[-1,15,-1,-1,-1,-1]] @@ -62,7 +62,7 @@ tags: 可以证明需要至少 4 次移动才能到达最后一个方格,所以答案是 4 。-
示例 2:
+示例 2:
输入:board = [[-1,-1],[-1,3]] @@ -76,7 +76,7 @@ tags:
n == board.length == board[i].length
2 <= n <= 20
grid[i][j]
的值是 -1
或在范围 [1, n2]
内board[i][j]
的值是 -1
或在范围 [1, n2]
内1
和 n2
的方格上没有蛇或梯子A board square on row r
and column c
has a snake or ladder if board[r][c] != -1
. The destination of that snake or ladder is board[r][c]
. Squares 1
and n2
do not have a snake or ladder.
A board square on row r
and column c
has a snake or ladder if board[r][c] != -1
. The destination of that snake or ladder is board[r][c]
. Squares 1
and n2
are not the starting points of any snake or ladder.
Note that you only take a snake or ladder at most once per move. If the destination to a snake or ladder is the start of another snake or ladder, you do not follow the subsequent snake or ladder.
@@ -73,7 +73,7 @@ This is the lowest possible number of moves to reach the last square, so returnn == board.length == board[i].length
2 <= n <= 20
board[i][j]
is either -1
or in the range [1, n2]
.1
and n2
do not have any ladders or snakes.1
and n2
are not the starting points of any snake or ladder.返回使 nums
中的每个值都变成唯一的所需要的最少操作次数。
生成的测试用例保证答案在 32 位整数范围内。
+-
示例 1:
+示例 1:
输入:nums = [1,2,2] @@ -35,7 +37,7 @@ tags: 解释:经过一次 move 操作,数组将变为 [1, 2, 3]。-
示例 2:
+示例 2:
输入:nums = [3,2,1,2,1,7] diff --git a/solution/0900-0999/0945.Minimum Increment to Make Array Unique/README_EN.md b/solution/0900-0999/0945.Minimum Increment to Make Array Unique/README_EN.md index 54526c5199143..5bb44f951e300 100644 --- a/solution/0900-0999/0945.Minimum Increment to Make Array Unique/README_EN.md +++ b/solution/0900-0999/0945.Minimum Increment to Make Array Unique/README_EN.md @@ -40,7 +40,7 @@ tags: Input: nums = [3,2,1,2,1,7] Output: 6 Explanation: After 6 moves, the array could be [3, 4, 1, 2, 5, 7]. -It can be shown with 5 or less moves that it is impossible for the array to have all unique values. +It can be shown that it is impossible for the array to have all unique values with 5 or less moves.
diff --git a/solution/1000-1099/1039.Minimum Score Triangulation of Polygon/README_EN.md b/solution/1000-1099/1039.Minimum Score Triangulation of Polygon/README_EN.md index 34e695ae0722b..dc5f2cece7f3d 100644 --- a/solution/1000-1099/1039.Minimum Score Triangulation of Polygon/README_EN.md +++ b/solution/1000-1099/1039.Minimum Score Triangulation of Polygon/README_EN.md @@ -19,37 +19,51 @@ tags: -
You have a convex n
-sided polygon where each vertex has an integer value. You are given an integer array values
where values[i]
is the value of the ith
vertex (i.e., clockwise order).
You have a convex n
-sided polygon where each vertex has an integer value. You are given an integer array values
where values[i]
is the value of the ith
vertex in clockwise order.
You will triangulate the polygon into n - 2
triangles. For each triangle, the value of that triangle is the product of the values of its vertices, and the total score of the triangulation is the sum of these values over all n - 2
triangles in the triangulation.
Polygon triangulation is a process where you divide a polygon into a set of triangles and the vertices of each triangle must also be vertices of the original polygon. Note that no other shapes other than triangles are allowed in the division. This process will result in n - 2
triangles.
Return the smallest possible total score that you can achieve with some triangulation of the polygon.
+You will triangulate the polygon. For each triangle, the weight of that triangle is the product of the values at its vertices. The total score of the triangulation is the sum of these weights over all n - 2
triangles.
Return the minimum possible score that you can achieve with some triangulation of the polygon.
Example 1:
--Input: values = [1,2,3] -Output: 6 -Explanation: The polygon is already triangulated, and the score of the only triangle is 6. -+ +
Input: values = [1,2,3]
+ +Output: 6
+ +Explanation: The polygon is already triangulated, and the score of the only triangle is 6.
+Example 2:
--Input: values = [3,7,4,5] -Output: 144 -Explanation: There are two triangulations, with possible scores: 3*7*5 + 4*5*7 = 245, or 3*4*5 + 3*4*7 = 144. -The minimum score is 144. -+ +
Input: values = [3,7,4,5]
+ +Output: 144
+ +Explanation: There are two triangulations, with possible scores: 3*7*5 + 4*5*7 = 245, or 3*4*5 + 3*4*7 = 144.
+The minimum score is 144.
Example 3:
--Input: values = [1,3,1,4,1,5] -Output: 13 -Explanation: The minimum score triangulation has score 1*1*3 + 1*1*4 + 1*1*5 + 1*1*1 = 13. -+ +
Input: values = [1,3,1,4,1,5]
+ +Output: 13
+ +Explanation: The minimum score triangulation is 1*1*3 + 1*1*4 + 1*1*5 + 1*1*1 = 13.
+
Constraints:
diff --git a/solution/1300-1399/1330.Reverse Subarray To Maximize Array Value/README_EN.md b/solution/1300-1399/1330.Reverse Subarray To Maximize Array Value/README_EN.md index d392ca046cf87..b6c4724709a74 100644 --- a/solution/1300-1399/1330.Reverse Subarray To Maximize Array Value/README_EN.md +++ b/solution/1300-1399/1330.Reverse Subarray To Maximize Array Value/README_EN.md @@ -46,8 +46,9 @@ tags:Constraints:
1 <= nums.length <= 3 * 104
2 <= nums.length <= 3 * 104
-105 <= nums[i] <= 105
给你一个由若干 0
和 1
组成的数组 nums
以及整数 k
。如果所有 1
都至少相隔 k
个元素,则返回 True
;否则,返回 False
。
给你一个由若干 0
和 1
组成的数组 nums
以及整数 k
。如果所有 1
都至少相隔 k
个元素,则返回 true ;否则,返回 false
。
示例 1:
-输入:nums = [1,0,0,0,1,0,0,1], k = 2 ++输入:nums = [1,0,0,0,1,0,0,1], k = 2 输出:true 解释:每个 1 都至少相隔 2 个元素。示例 2:
-+
-
输入:nums = [1,0,0,1,0,1], k = 2 ++输入:nums = [1,0,0,1,0,1], k = 2 输出:false 解释:第二个 1 和第三个 1 之间只隔了 1 个元素。-示例 3:
- -输入:nums = [1,1,1,1,1], k = 0 -输出:true -- -示例 4:
- -输入:nums = [0,1,0,1], k = 1 -输出:true --
提示:
1 <= nums.length <= 10^5
1 <= nums.length <= 105
0 <= k <= nums.length
nums[i]
的值为 0
或 1
1 <= s.length <= 500
s
consists of only lowercase English letters and/or digits.s
consists of only lowercase English letters and digits.1 <= tasks[i] <= 109
+
Note: This question is the same as 2870: Minimum Number of Operations to Make Array Empty.
+ ## Solutions diff --git a/solution/2300-2399/2324.Product Sales Analysis IV/README.md b/solution/2300-2399/2324.Product Sales Analysis IV/README.md index e0ce6c0c0ccc7..b87a1e626e225 100644 --- a/solution/2300-2399/2324.Product Sales Analysis IV/README.md +++ b/solution/2300-2399/2324.Product Sales Analysis IV/README.md @@ -95,7 +95,7 @@ Product 表: - 在产品 3 上花费 7 * 15 = 105。 用户101在产品3上花的钱最多。 用户 102: - - 在产品 1 上花费 (9 + 7)* 10 = 150 + - 在产品 1 上花费 (9 + 6)* 10 = 150 - 在产品 2 上花费 6 * 25 = 150 - 在产品 3 上花费 10 * 15 = 150。 用户 102 在产品 1、2、3 上花的钱最多。 diff --git a/solution/2300-2399/2397.Maximum Rows Covered by Columns/README_EN.md b/solution/2300-2399/2397.Maximum Rows Covered by Columns/README_EN.md index 082f5951418e2..e8104a8827abe 100644 --- a/solution/2300-2399/2397.Maximum Rows Covered by Columns/README_EN.md +++ b/solution/2300-2399/2397.Maximum Rows Covered by Columns/README_EN.md @@ -22,43 +22,56 @@ tags: -You are given a 0-indexed m x n
binary matrix matrix
and an integer numSelect
, which denotes the number of distinct columns you must select from matrix
.
You are given an m x n
binary matrix matrix
and an integer numSelect
.
Let us consider s = {c1, c2, ...., cnumSelect}
as the set of columns selected by you. A row row
is covered by s
if:
Your goal is to select exactly numSelect
distinct columns from matrix
such that you cover as many rows as possible.
A row is considered covered if all the 1
's in that row are also part of a column that you have selected. If a row does not have any 1
s, it is also considered covered.
More formally, let us consider selected = {c1, c2, ...., cnumSelect}
as the set of columns selected by you. A row i
is covered by selected
if:
matrix[row][col]
(0 <= col <= n - 1
) where matrix[row][col] == 1
, col
is present in s
or,row
has a value of 1
.matrix[i][j] == 1
, the column j
is in selected
.i
has a value of 1
.You need to choose numSelect
columns such that the number of rows that are covered is maximized.
Return the maximum number of rows that can be covered by a set of numSelect
columns.
Return the maximum number of rows that can be covered by a set of numSelect
columns.
Example 1:
--Input: matrix = [[0,0,0],[1,0,1],[0,1,1],[0,0,1]], numSelect = 2 -Output: 3 -Explanation: One possible way to cover 3 rows is shown in the diagram above. -We choose s = {0, 2}. -- Row 0 is covered because it has no occurrences of 1. -- Row 1 is covered because the columns with value 1, i.e. 0 and 2 are present in s. -- Row 2 is not covered because matrix[2][1] == 1 but 1 is not present in s. -- Row 3 is covered because matrix[2][2] == 1 and 2 is present in s. -Thus, we can cover three rows. -Note that s = {1, 2} will also cover 3 rows, but it can be shown that no more than three rows can be covered. -+ +
Input: matrix = [[0,0,0],[1,0,1],[0,1,1],[0,0,1]], numSelect = 2
+ +Output: 3
+ +Explanation:
+ +One possible way to cover 3 rows is shown in the diagram above.
+We choose s = {0, 2}.
+- Row 0 is covered because it has no occurrences of 1.
+- Row 1 is covered because the columns with value 1, i.e. 0 and 2 are present in s.
+- Row 2 is not covered because matrix[2][1] == 1 but 1 is not present in s.
+- Row 3 is covered because matrix[2][2] == 1 and 2 is present in s.
+Thus, we can cover three rows.
+Note that s = {1, 2} will also cover 3 rows, but it can be shown that no more than three rows can be covered.
Example 2:
--Input: matrix = [[1],[0]], numSelect = 1 -Output: 2 -Explanation: Selecting the only column will result in both rows being covered since the entire matrix is selected. -Therefore, we return 2. -+ +
Input: matrix = [[1],[0]], numSelect = 1
+ +Output: 2
+ +Explanation:
+ +Selecting the only column will result in both rows being covered since the entire matrix is selected.
+
Constraints:
diff --git a/solution/2400-2499/2410.Maximum Matching of Players With Trainers/README_EN.md b/solution/2400-2499/2410.Maximum Matching of Players With Trainers/README_EN.md index 30b237f79e30f..eee7b8f4ad56b 100644 --- a/solution/2400-2499/2410.Maximum Matching of Players With Trainers/README_EN.md +++ b/solution/2400-2499/2410.Maximum Matching of Players With Trainers/README_EN.md @@ -58,6 +58,9 @@ Each player can only be matched with one trainer, so the maximum answer is 1.1 <= players[i], trainers[j] <= 109
+
Note: This question is the same as 445: Assign Cookies.
+ ## Solutions diff --git a/solution/2500-2599/2582.Pass the Pillow/README.md b/solution/2500-2599/2582.Pass the Pillow/README.md index 7995ffa41a767..9fc8015c56440 100644 --- a/solution/2500-2599/2582.Pass the Pillow/README.md +++ b/solution/2500-2599/2582.Pass the Pillow/README.md @@ -56,6 +56,10 @@ tags:1 <= time <= 1000
+ +
注意:本题与 3178.找出 K 秒后拿着球的孩子 一致。
+ ## 解法 diff --git a/solution/2500-2599/2582.Pass the Pillow/README_EN.md b/solution/2500-2599/2582.Pass the Pillow/README_EN.md index e8c4d48d2a36b..52b220b3b8715 100644 --- a/solution/2500-2599/2582.Pass the Pillow/README_EN.md +++ b/solution/2500-2599/2582.Pass the Pillow/README_EN.md @@ -53,6 +53,9 @@ After two seconds, the 3rd person is holding the pillow.1 <= time <= 1000
+
Note: This question is the same as 3178: Find the Child Who Has the Ball After K Seconds.
+ ## Solutions diff --git a/solution/2800-2899/2863.Maximum Length of Semi-Decreasing Subarrays/README.md b/solution/2800-2899/2863.Maximum Length of Semi-Decreasing Subarrays/README.md index c12c29a77c75f..95efc548c5ea2 100644 --- a/solution/2800-2899/2863.Maximum Length of Semi-Decreasing Subarrays/README.md +++ b/solution/2800-2899/2863.Maximum Length of Semi-Decreasing Subarrays/README.md @@ -11,7 +11,7 @@ tags: -# [2863. 最长半递减数组 🔒](https://leetcode.cn/problems/maximum-length-of-semi-decreasing-subarrays) +# [2863. 最长半递减子数组的长度 🔒](https://leetcode.cn/problems/maximum-length-of-semi-decreasing-subarrays) [English Version](/solution/2800-2899/2863.Maximum%20Length%20of%20Semi-Decreasing%20Subarrays/README_EN.md) diff --git a/solution/2800-2899/2865.Beautiful Towers I/README.md b/solution/2800-2899/2865.Beautiful Towers I/README.md index 485a5227c4a58..936540edc060a 100644 --- a/solution/2800-2899/2865.Beautiful Towers I/README.md +++ b/solution/2800-2899/2865.Beautiful Towers I/README.md @@ -20,25 +20,9 @@ tags: -给你一个长度为 n
下标从 0 开始的整数数组 maxHeights
。
给定一个包含 n
个整数的数组 heights
表示 n
座连续的塔中砖块的数量。你的任务是移除一些砖块来形成一个 山脉状 的塔排列。在这种布置中,塔高度先是非递减,有一个或多个连续塔达到最大峰值,然后非递增排列。
你的任务是在坐标轴上建 n
座塔。第 i
座塔的下标为 i
,高度为 heights[i]
。
如果以下条件满足,我们称这些塔是 美丽 的:
- -1 <= heights[i] <= maxHeights[i]
heights
是一个 山脉 数组。如果存在下标 i
满足以下条件,那么我们称数组 heights
是一个 山脉 数组:
0 < j <= i
,都有 heights[j - 1] <= heights[j]
i <= k < n - 1
,都有 heights[k + 1] <= heights[k]
请你返回满足 美丽塔 要求的方案中,高度和的最大值 。
+返回满足山脉状塔排列的方案中,高度和的最大值 。
@@ -47,31 +31,22 @@ tags:
输入:maxHeights = [5,3,4,1,1] 输出:13 -解释:和最大的美丽塔方案为 heights = [5,3,3,1,1] ,这是一个美丽塔方案,因为: -- 1 <= heights[i] <= maxHeights[i] -- heights 是个山脉数组,峰值在 i = 0 处。 -13 是所有美丽塔方案中的最大高度和。+解释:我们移除一些砖块来形成 heights = [5,3,3,1,1],峰值位于下标 0。 +
示例 2:
输入:maxHeights = [6,5,3,9,2,7] 输出:22 -解释: 和最大的美丽塔方案为 heights = [3,3,3,9,2,2] ,这是一个美丽塔方案,因为: -- 1 <= heights[i] <= maxHeights[i] -- heights 是个山脉数组,峰值在 i = 3 处。 -22 是所有美丽塔方案中的最大高度和。+解释:我们移除一些砖块来形成 heights = [3,3,3,9,2,2],峰值位于下标 3。
示例 3:
输入:maxHeights = [3,2,5,5,2,3] 输出:18 -解释:和最大的美丽塔方案为 heights = [2,2,5,5,2,2] ,这是一个美丽塔方案,因为: -- 1 <= heights[i] <= maxHeights[i] -- heights 是个山脉数组,最大值在 i = 2 处。 -注意,在这个方案中,i = 3 也是一个峰值。 -18 是所有美丽塔方案中的最大高度和。 +解释:我们移除一些砖块来形成 heights = [2,2,5,5,2,2],峰值位于下标 2 或 3。
@@ -79,8 +54,8 @@ tags:
提示:
1 <= n == maxHeights <= 103
1 <= maxHeights[i] <= 109
1 <= n == heights.length <= 103
1 <= heights[i] <= 109
Constraints:
1 <= n == heights <= 103
1 <= n == heights.length <= 103
1 <= heights[i] <= 109
Constraints:
1 <= n == maxHeights <= 105
1 <= n == maxHeights.length <= 105
1 <= maxHeights[i] <= 109
1 <= nums[i] <= 106
+
Note: This question is the same as 2244: Minimum Rounds to Complete All Tasks.
+ ## Solutions diff --git a/solution/3000-3099/3054.Binary Tree Nodes/README_EN.md b/solution/3000-3099/3054.Binary Tree Nodes/README_EN.md index 9493460c1ea71..8e378f287d40e 100644 --- a/solution/3000-3099/3054.Binary Tree Nodes/README_EN.md +++ b/solution/3000-3099/3054.Binary Tree Nodes/README_EN.md @@ -72,10 +72,13 @@ Tree table: +---+-------+ Explanation: - Node 5 is the root node since it has no parent node. -- Nodes 1, 3, 6, and 8 are leaf nodes because they don't have any child nodes. -- Nodes 2, 4, and 7 are inner nodes as they serve as parents to some of the nodes in the structure. +- Nodes 1, 3, 6, and 9 are leaf nodes because they don't have any child nodes. +- Nodes 2, and 8 are inner nodes as they serve as parents to some of the nodes in the structure. ++
Note: This question is the same as 608: Tree Node.
+ ## Solutions diff --git a/solution/3100-3199/3134.Find the Median of the Uniqueness Array/README.md b/solution/3100-3199/3134.Find the Median of the Uniqueness Array/README.md index adfa421aafb35..7970ae1d3e7ef 100644 --- a/solution/3100-3199/3134.Find the Median of the Uniqueness Array/README.md +++ b/solution/3100-3199/3134.Find the Median of the Uniqueness Array/README.md @@ -84,32 +84,203 @@ tags: -### 方法一 +### 方法一:二分查找 + 双指针 + +我们记数组 $\textit{nums}$ 的长度为 $n$,那么唯一性数组的长度为 $m = \frac{(1 + n) \times n}{2}$,而唯一性数组的中位数就是这 $m$ 个数中的第 $\frac{m + 1}{2}$ 小的数字。 + +考虑唯一性数组中,有多少个数小于等于 $x$。随着 $x$ 的增大,只会有越来越多的数小于等于 $x$。这存在着单调性,因此,我们可以二分枚举 $x$,找到第一个 $x$,满足唯一性数组中小于等于 $x$ 的数的个数大于等于 $\frac{m + 1}{2}$,这个 $x$ 就是唯一性数组的中位数。 + +我们定义二分查找的左边界 $l = 0$,右边界 $r = n$,然后进行二分查找,对于每个 $\textit{mid}$,我们检查唯一性数组中小于等于 $\textit{mid}$ 的数的个数是否大于等于 $\frac{m + 1}{2}$。我们通过函数 $\text{check}(mx)$ 来实现这一点。 + +函数 $\text{check}(mx)$ 的实现思路如下: + +由于子数组越长,不同元素的个数越多,因此,我们可以利用双指针维护一个滑动窗口,使得窗口中的子数组的不同元素的个数不超过 $mx$。具体地,我们维护一个哈希表 $\textit{cnt}$,$\textit{cnt}[x]$ 表示窗口中元素 $x$ 的个数。我们使用两个指针 $l$ 和 $r$,其中 $l$ 表示窗口的左边界,而 $r$ 表示窗口的右边界。初始时 $l = r = 0$。 + +我们枚举 $r$,对于每个 $r$,我们将 $\textit{nums}[r]$ 加入窗口中,并更新 $\textit{cnt}[\textit{nums}[r]]$。如果窗口中的不同元素的个数超过了 $mx$,我们需要将 $l$ 右移,直到窗口中的不同元素的个数不超过 $mx$。此时,右端点为 $r$,而左端点为 $[l,..r]$ 的子数组都是满足条件的,一共有 $r - l + 1$ 个子数组。我们将这个数量累加到 $k$ 中,如果 $k$ 大于等于 $\frac{m + 1}{2}$,那么说明唯一性数组中小于等于 $\textit{mid}$ 的数的个数大于等于 $\frac{m + 1}{2}$,我们返回 $\text{true}$,否则返回 $\text{false}$。 + +时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{nums}$ 的长度。 #### Python3 ```python - +class Solution: + def medianOfUniquenessArray(self, nums: List[int]) -> int: + def check(mx: int) -> bool: + cnt = defaultdict(int) + k = l = 0 + for r, x in enumerate(nums): + cnt[x] += 1 + while len(cnt) > mx: + y = nums[l] + cnt[y] -= 1 + if cnt[y] == 0: + cnt.pop(y) + l += 1 + k += r - l + 1 + if k >= (m + 1) // 2: + return True + return False + + n = len(nums) + m = (1 + n) * n // 2 + return bisect_left(range(n), True, key=check) ``` #### Java ```java - +class Solution { + private long m; + private int[] nums; + + public int medianOfUniquenessArray(int[] nums) { + int n = nums.length; + this.nums = nums; + m = (1L + n) * n / 2; + int l = 0, r = n; + while (l < r) { + int mid = (l + r) >> 1; + if (check(mid)) { + r = mid; + } else { + l = mid + 1; + } + } + return l; + } + + private boolean check(int mx) { + Map1 <= k <= 50
+
Note: This question is the same as 2582: Pass the Pillow.
+ ## Solutions diff --git a/solution/3200-3299/3213.Construct String with Minimum Cost/README.md b/solution/3200-3299/3213.Construct String with Minimum Cost/README.md index c32e43fe2fe81..131e340f019a6 100644 --- a/solution/3200-3299/3213.Construct String with Minimum Cost/README.md +++ b/solution/3200-3299/3213.Construct String with Minimum Cost/README.md @@ -25,7 +25,7 @@ tags:设想一个空字符串 s
。
你可以执行以下操作任意次数(包括零次):
+你可以执行以下操作任意次数(包括 零 次):
[0, words.length - 1]
的索引 i
。给定字符串 target
,一个字符串数组 words
以及一个整数数组 costs
,两个数组长度相同。
给你一个字符串 target
、一个字符串数组 words
以及一个整数数组 costs
,这两个数组长度相同。
想象一个空字符串 s
。
设想一个空字符串 s
。
您可以执行以下操作任意次数(包括 零):
+你可以执行以下操作任意次数(包括 零 次):
[0, words.length - 1]
中选择一个下标 i
。words[i]
添加到 s
。costs[i]
。[0, words.length - 1]
的索引 i
。words[i]
追加到 s
。costs[i]
。返回使 s
与 target
相等的 最小 开销。如果不可能做到,返回 -1。
返回使 s
等于 target
的 最小 成本。如果不可能,返回 -1
。
-
示例 1:
+示例 1:
-输入:target = "abcdef", words = ["abdef","abc","d","def","ef"], costs = [100,1,1,10,5]
+输入: target = "abcdef", words = ["abdef","abc","d","def","ef"], costs = [100,1,1,10,5]
-输出:7
+输出: 7
解释:
-通过执行以下操作可以实现最低开销:
-"abc"
添加到 s
,得到 s = "abc"
。"d"
添加到 s
,得到 s = "abcd"
。"ef"
添加到 s
,得到 s = "abcdef"
。"abc"
追加到 s
,得到 s = "abc"
。"d"
追加到 s
,得到 s = "abcd"
。"ef"
追加到 s
,得到 s = "abcdef"
。示例 2:
+示例 2:
-输入:target = "aaaa", words = ["z","zz","zzz"], costs = [1,10,100]
+输入: target = "aaaa", words = ["z","zz","zzz"], costs = [1,10,100]
-输出:-1
+输出: -1
解释:
-不可能使 s
与 target
相等,所以我们返回 -1。
无法使 s
等于 target
,因此返回 -1。
@@ -68,7 +62,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3253.Co
1 <= target.length <= 2000
1 <= words.length == costs.length <= 50
1 <= words[i].length <= target.length
target
和 words[i]
只包含小写英语字母。target
和 words[i]
仅由小写英文字母组成。1 <= costs[i] <= 105
You are given the head
of a doubly linked list, which contains nodes that have a next pointer and a previous pointer.
给定一个 双链表 的 head
节点,其中的节点具有指向下一个节点的指针和上一个节点的指针。
Return an integer array which contains the elements of the linked list in order.
+返回一个 按顺序 包含链表中元素的整数数组。
-
Example 1:
+ +示例 1:
Input: head = [1,2,3,4,3,2,1]
+输入:head = [1,2,3,4,3,2,1]
-Output: [1,2,3,4,3,2,1]
+输出:[1,2,3,4,3,2,1]
Example 2:
+示例 2:
Input: head = [2,2,2,2,2]
+输入:head = [2,2,2,2,2]
-Output: [2,2,2,2,2]
+输出:[2,2,2,2,2]
Example 3:
+示例 3:
Input: head = [3,2,3,2,3,2]
+输入:head = [3,2,3,2,3,2]
-Output: [3,2,3,2,3,2]
+输出:[3,2,3,2,3,2]
-
Constraints:
+ +提示:
[1, 50]
.[1, 50]
范围。1 <= Node.val <= 50