diff --git a/solution/0200-0299/0237.Delete Node in a Linked List/README.md b/solution/0200-0299/0237.Delete Node in a Linked List/README.md index d6779914fcde8..49aeda52a5de0 100644 --- a/solution/0200-0299/0237.Delete Node in a Linked List/README.md +++ b/solution/0200-0299/0237.Delete Node in a Linked List/README.md @@ -128,4 +128,24 @@ func deleteNode(node *ListNode) { } ``` +### **C++** + +```cpp +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode(int x) : val(x), next(NULL) {} + * }; + */ +class Solution { +public: + void deleteNode(ListNode* node) { + node->val = node->next->val; + node->next = node->next->next; + } +}; +``` + diff --git a/solution/0200-0299/0237.Delete Node in a Linked List/README_EN.md b/solution/0200-0299/0237.Delete Node in a Linked List/README_EN.md index 3226d4d1bed31..165906c7e1d6d 100644 --- a/solution/0200-0299/0237.Delete Node in a Linked List/README_EN.md +++ b/solution/0200-0299/0237.Delete Node in a Linked List/README_EN.md @@ -135,4 +135,24 @@ func deleteNode(node *ListNode) { } ``` +### **C++** + +```cpp +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode(int x) : val(x), next(NULL) {} + * }; + */ +class Solution { +public: + void deleteNode(ListNode* node) { + node->val = node->next->val; + node->next = node->next->next; + } +}; +``` + diff --git a/solution/0200-0299/0237.Delete Node in a Linked List/Solution.c b/solution/0200-0299/0237.Delete Node in a Linked List/Solution.c new file mode 100644 index 0000000000000..365959e73a289 --- /dev/null +++ b/solution/0200-0299/0237.Delete Node in a Linked List/Solution.c @@ -0,0 +1,11 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * struct ListNode *next; + * }; + */ +void deleteNode(struct ListNode* node) { + node->val = node->next->val; + node->next = node->next->next; +} \ No newline at end of file diff --git a/solution/0200-0299/0237.Delete Node in a Linked List/Solution.cpp b/solution/0200-0299/0237.Delete Node in a Linked List/Solution.cpp new file mode 100644 index 0000000000000..cfc84af6b77ac --- /dev/null +++ b/solution/0200-0299/0237.Delete Node in a Linked List/Solution.cpp @@ -0,0 +1,15 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode(int x) : val(x), next(NULL) {} + * }; + */ +class Solution { +public: + void deleteNode(ListNode* node) { + node->val = node->next->val; + node->next = node->next->next; + } +}; \ No newline at end of file diff --git a/solution/1800-1899/1884.Egg Drop With 2 Eggs and N Floors/README.md b/solution/1800-1899/1884.Egg Drop With 2 Eggs and N Floors/README.md index bd433b66c88a8..472bdd5c73829 100644 --- a/solution/1800-1899/1884.Egg Drop With 2 Eggs and N Floors/README.md +++ b/solution/1800-1899/1884.Egg Drop With 2 Eggs and N Floors/README.md @@ -1,4 +1,4 @@ -# [1884. ](https://leetcode-cn.com/problems/egg-drop-with-2-eggs-and-n-floors) +# [1884. 鸡蛋掉落-两枚鸡蛋](https://leetcode-cn.com/problems/egg-drop-with-2-eggs-and-n-floors) [English Version](/solution/1800-1899/1884.Egg%20Drop%20With%202%20Eggs%20and%20N%20Floors/README_EN.md) @@ -6,7 +6,42 @@ -None +
给你 2 枚相同 的鸡蛋,和一栋从第 1
层到第 n
层共有 n
层楼的建筑。
已知存在楼层 f
,满足 0 <= f <= n
,任何从 高于 f
的楼层落下的鸡蛋都 会碎 ,从 f
楼层或比它低 的楼层落下的鸡蛋都 不会碎 。
每次操作,你可以取一枚 没有碎 的鸡蛋并把它从任一楼层 x
扔下(满足 1 <= x <= n
)。如果鸡蛋碎了,你就不能再次使用它。如果某枚鸡蛋扔下后没有摔碎,则可以在之后的操作中 重复使用 这枚鸡蛋。
请你计算并返回要确定 f
确切的值 的 最小操作次数 是多少?
+ +
示例 1:
+ ++输入:n = 2 +输出:2 +解释:我们可以将第一枚鸡蛋从 1 楼扔下,然后将第二枚从 2 楼扔下。 +如果第一枚鸡蛋碎了,可知 f = 0; +如果第二没鸡蛋碎了,但第一枚没碎,可知 f = 1; +否则,当两个鸡蛋都没碎时,可知 f = 2。 ++ +
示例 2:
+ ++输入:n = 100 +输出:14 ++ +
+ +
提示:
+ +1 <= n <= 1000
给你两个大小为 n x n
的二进制矩阵 mat
和 target
。现 以 90 度顺时针轮转 矩阵 mat
中的元素 若干次 ,如果能够使 mat
与 target
一致,返回 true
;否则,返回 false
。
+ +
示例 1:
++输入:mat = [[0,1],[1,0]], target = [[1,0],[0,1]] +输出:true +解释:顺时针轮转 90 度一次可以使 mat 和 target 一致。 ++ +
示例 2:
++输入:mat = [[0,1],[1,1]], target = [[1,0],[0,1]] +输出:false +解释:无法通过轮转矩阵中的元素使 equal 与 target 一致。 ++ +
示例 3:
++输入:mat = [[0,0,0],[0,1,0],[1,1,1]], target = [[1,1,1],[0,1,0],[0,0,0]] +输出:true +解释:顺时针轮转 90 度两次可以使 mat 和 target 一致。 ++ +
+ +
提示:
+ +n == mat.length == target.length
n == mat[i].length == target[i].length
1 <= n <= 10
mat[i][j]
和 target[i][j]
不是 0
就是 1
Given two n x n
binary matrices mat
and target
, return true
if it is possible to make mat
equal to target
by rotating mat
in 90-degree increments, or false
otherwise.
+
Example 1:
++Input: mat = [[0,1],[1,0]], target = [[1,0],[0,1]] +Output: true +Explanation: We can rotate mat 90 degrees clockwise to make mat equal target. ++ +
Example 2:
++Input: mat = [[0,1],[1,1]], target = [[1,0],[0,1]] +Output: false +Explanation: It is impossible to make mat equal to target by rotating mat. ++ +
Example 3:
++Input: mat = [[0,0,0],[0,1,0],[1,1,1]], target = [[1,1,1],[0,1,0],[0,0,0]] +Output: true +Explanation: We can rotate mat 90 degrees clockwise two times to make mat equal target. ++ +
+
Constraints:
+ +n == mat.length == target.length
n == mat[i].length == target[i].length
1 <= n <= 10
mat[i][j]
and target[i][j]
are either 0
or 1
.给你一个整数数组 nums
,你的目标是令 nums
中的所有元素相等。完成一次减少操作需要遵照下面的几个步骤:
nums
中的 最大 值。记这个值为 largest
并取其下标 i
(下标从 0 开始计数)。如果有多个元素都是最大值,则取最小的 i
。nums
中的 下一个最大 值,这个值 严格小于 largest
,记为 nextLargest
。nums[i]
减少到 nextLargest
。返回使 nums
中的所有元素相等的操作次数。
+ +
示例 1:
+ ++输入:nums = [5,1,3] +输出:3 +解释:需要 3 次操作使 nums 中的所有元素相等: +1. largest = 5 下标为 0 。nextLargest = 3 。将 nums[0] 减少到 3 。nums = [3,1,3] 。 +2. largest = 3 下标为 0 。nextLargest = 1 。将 nums[0] 减少到 1 。nums = [1,1,3] 。 +3. largest = 3 下标为 2 。nextLargest = 1 。将 nums[2] 减少到 1 。nums = [1,1,1] 。 ++ +
示例 2:
+ ++输入:nums = [1,1,1] +输出:0 +解释:nums 中的所有元素已经是相等的。 ++ +
示例 3:
+ ++输入:nums = [1,1,2,2,3] +输出:4 +解释:需要 4 次操作使 nums 中的所有元素相等: +1. largest = 3 下标为 4 。nextLargest = 2 。将 nums[4] 减少到 2 。nums = [1,1,2,2,2] 。 +2. largest = 2 下标为 2 。nextLargest = 1 。将 nums[2] 减少到 1 。nums = [1,1,1,2,2] 。 +3. largest = 2 下标为 3 。nextLargest = 1 。将 nums[3] 减少到 1 。nums = [1,1,1,1,2] 。 +4. largest = 2 下标为 4 。nextLargest = 1 。将 nums[4] 减少到 1 。nums = [1,1,1,1,1] 。 ++ +
+ +
提示:
+ +1 <= nums.length <= 5 * 104
1 <= nums[i] <= 5 * 104
Given an integer array nums
, your goal is to make all elements in nums
equal. To complete one operation, follow these steps:
nums
. Let its index be i
(0-indexed) and its value be largest
. If there are multiple elements with the largest value, pick the smallest i
.nums
strictly smaller than largest
. Let its value be nextLargest
.nums[i]
to nextLargest
.Return the number of operations to make all elements in nums
equal.
+
Example 1:
+ ++Input: nums = [5,1,3] +Output: 3 +Explanation: It takes 3 operations to make all elements in nums equal: +1. largest = 5 at index 0. nextLargest = 3. Reduce nums[0] to 3. nums = [3,1,3]. +2. largest = 3 at index 0. nextLargest = 1. Reduce nums[0] to 1. nums = [1,1,3]. +3. largest = 3 at index 2. nextLargest = 1. Reduce nums[2] to 1. nums = [1,1,1]. ++ +
Example 2:
+ ++Input: nums = [1,1,1] +Output: 0 +Explanation: All elements in nums are already equal. ++ +
Example 3:
+ ++Input: nums = [1,1,2,2,3] +Output: 4 +Explanation: It takes 4 operations to make all elements in nums equal: +1. largest = 3 at index 4. nextLargest = 2. Reduce nums[4] to 2. nums = [1,1,2,2,2]. +2. largest = 2 at index 2. nextLargest = 1. Reduce nums[2] to 1. nums = [1,1,1,2,2]. +3. largest = 2 at index 3. nextLargest = 1. Reduce nums[3] to 1. nums = [1,1,1,1,2]. +4. largest = 2 at index 4. nextLargest = 1. Reduce nums[4] to 1. nums = [1,1,1,1,1]. ++ +
+
Constraints:
+ +1 <= nums.length <= 5 * 104
1 <= nums[i] <= 5 * 104
给你一个二进制字符串 s
。你可以按任意顺序执行以下两种操作任意次:
s
的第一个字符并将它 添加 到字符串结尾。s
中任意一个字符并将该字符 反转 ,也就是如果值为 '0'
,则反转得到 '1'
,反之亦然。请你返回使 s
变成 交替 字符串的前提下, 类型 2 的 最少 操作次数 。
我们称一个字符串是 交替 的,需要满足任意相邻字符都不同。
+ +"010"
和 "1010"
都是交替的,但是字符串 "0100"
不是。+ +
示例 1:
+ +输入:s = "111000" +输出:2 +解释:执行第一种操作两次,得到 s = "100011" 。 +然后对第三个和第六个字符执行第二种操作,得到 s = "101010" 。 ++ +
示例 2:
+ +输入:s = "010" +输出:0 +解释:字符串已经是交替的。 ++ +
示例 3:
+ +输入:s = "1110" +输出:1 +解释:对第二个字符执行第二种操作,得到 s = "1010" 。 ++ +
+ +
提示:
+ +1 <= s.length <= 105
s[i]
要么是 '0'
,要么是 '1'
。You are given a binary string s
. You are allowed to perform two types of operations on the string in any sequence:
s
and append it to the end of the string.s
and flip its value, i.e., if its value is '0'
it becomes '1'
and vice-versa.Return the minimum number of type-2 operations you need to perform such that s
becomes alternating.
The string is called alternating if no two adjacent characters are equal.
+ +"010"
and "1010"
are alternating, while the string "0100"
is not.+
Example 1:
+ ++Input: s = "111000" +Output: 2 +Explanation: Use the first operation two times to make s = "100011". +Then, use the second operation on the third and sixth elements to make s = "101010". ++ +
Example 2:
+ ++Input: s = "010" +Output: 0 +Explanation: The string is already alternating. ++ +
Example 3:
+ ++Input: s = "1110" +Output: 1 +Explanation: Use the second operation on the second element to make s = "1010". ++ +
+
Constraints:
+ +1 <= s.length <= 105
s[i]
is either '0'
or '1'
.给你 n
个包裹,你需要把它们装在箱子里,每个箱子装一个包裹。总共有 m
个供应商提供 不同尺寸 的箱子(每个规格都有无数个箱子)。如果一个包裹的尺寸 小于等于 一个箱子的尺寸,那么这个包裹就可以放入这个箱子之中。
包裹的尺寸用一个整数数组 packages
表示,其中 packages[i]
是第 i
个包裹的尺寸。供应商用二维数组 boxes
表示,其中 boxes[j]
是第 j
个供应商提供的所有箱子尺寸的数组。
你想要选择 一个供应商 并只使用该供应商提供的箱子,使得 总浪费空间最小 。对于每个装了包裹的箱子,我们定义 浪费的 空间等于 箱子的尺寸 - 包裹的尺寸
。总浪费空间 为 所有 箱子中浪费空间的总和。
[4,8]
的箱子装下尺寸为 [2,3,5]
的包裹,你可以将尺寸为 2
和 3
的两个包裹装入两个尺寸为 4
的箱子中,同时把尺寸为 5
的包裹装入尺寸为 8
的箱子中。总浪费空间为 (4-2) + (4-3) + (8-5) = 6
。请你选择 最优 箱子供应商,使得 总浪费空间最小 。如果 无法 将所有包裹放入箱子中,请你返回 -1
。由于答案可能会 很大 ,请返回它对 109 + 7
取余 的结果。
+ +
示例 1:
+ ++输入:packages = [2,3,5], boxes = [[4,8],[2,8]] +输出:6 +解释:选择第一个供应商最优,用两个尺寸为 4 的箱子和一个尺寸为 8 的箱子。 +总浪费空间为 (4-2) + (4-3) + (8-5) = 6 。 ++ +
示例 2:
+ ++输入:packages = [2,3,5], boxes = [[1,4],[2,3],[3,4]] +输出:-1 +解释:没有箱子能装下尺寸为 5 的包裹。 ++ +
示例 3:
+ ++输入:packages = [3,5,8,10,11,12], boxes = [[12],[11,9],[10,5,14]] +输出:9 +解释:选择第三个供应商最优,用两个尺寸为 5 的箱子,两个尺寸为 10 的箱子和两个尺寸为 14 的箱子。 +总浪费空间为 (5-3) + (5-5) + (10-8) + (10-10) + (14-11) + (14-12) = 9 。 ++ +
+ +
提示:
+ +n == packages.length
m == boxes.length
1 <= n <= 105
1 <= m <= 105
1 <= packages[i] <= 105
1 <= boxes[j].length <= 105
1 <= boxes[j][k] <= 105
sum(boxes[j].length) <= 105
boxes[j]
中的元素 互不相同 。You have n
packages that you are trying to place in boxes, one package in each box. There are m
suppliers that each produce boxes of different sizes (with infinite supply). A package can be placed in a box if the size of the package is less than or equal to the size of the box.
The package sizes are given as an integer array packages
, where packages[i]
is the size of the ith
package. The suppliers are given as a 2D integer array boxes
, where boxes[j]
is an array of box sizes that the jth
supplier produces.
You want to choose a single supplier and use boxes from them such that the total wasted space is minimized. For each package in a box, we define the space wasted to be size of the box - size of the package
. The total wasted space is the sum of the space wasted in all the boxes.
[2,3,5]
and the supplier offers boxes of sizes [4,8]
, you can fit the packages of size-2
and size-3
into two boxes of size-4
and the package with size-5
into a box of size-8
. This would result in a waste of (4-2) + (4-3) + (8-5) = 6
.Return the minimum total wasted space by choosing the box supplier optimally, or -1
if it is impossible to fit all the packages inside boxes. Since the answer may be large, return it modulo 109 + 7
.
+
Example 1:
+ ++Input: packages = [2,3,5], boxes = [[4,8],[2,8]] +Output: 6 +Explanation: It is optimal to choose the first supplier, using two size-4 boxes and one size-8 box. +The total waste is (4-2) + (4-3) + (8-5) = 6. ++ +
Example 2:
+ ++Input: packages = [2,3,5], boxes = [[1,4],[2,3],[3,4]] +Output: -1 +Explanation: There is no box that the package of size 5 can fit in. ++ +
Example 3:
+ ++Input: packages = [3,5,8,10,11,12], boxes = [[12],[11,9],[10,5,14]] +Output: 9 +Explanation: It is optimal to choose the third supplier, using two size-5 boxes, two size-10 boxes, and two size-14 boxes. +The total waste is (5-3) + (5-5) + (10-8) + (10-10) + (14-11) + (14-12) = 9. ++ +
+
Constraints:
+ +n == packages.length
m == boxes.length
1 <= n <= 105
1 <= m <= 105
1 <= packages[i] <= 105
1 <= boxes[j].length <= 105
1 <= boxes[j][k] <= 105
sum(boxes[j].length) <= 105
boxes[j]
are distinct.You are given two identical eggs and you have access to a building with n
floors labeled from 1
to n
.
You know that there exists a floor f
where 0 <= f <= n
such that any egg dropped at a floor higher than f
will break, and any egg dropped at or below floor f
will not break.
In each move, you may take an unbroken egg and drop it from any floor x
(where 1 <= x <= n
). If the egg breaks, you can no longer use it. However, if the egg does not break, you may reuse it in future moves.
Return the minimum number of moves that you need to determine with certainty what the value of f
is.
\n
Example 1:
\n\n\nInput: n = 2\nOutput: 2\nExplanation: We can drop the first egg from floor 1 and the second egg from floor 2.\nIf the first egg breaks, we know that f = 0.\nIf the second egg breaks but the first egg didn't, we know that f = 1.\nOtherwise, if both eggs survive, we know that f = 2.\n\n\n
Example 2:
\n\n\nInput: n = 100\nOutput: 14\nExplanation: One optimal strategy is:\n- Drop the 1st egg at floor 9. If it breaks, we know f is between 0 and 8. Drop the 2nd egg starting\n from floor 1 and going up one at a time to find f within 7 more drops. Total drops is 1 + 7 = 8.\n- If the 1st egg does not break, drop the 1st egg again at floor 22. If it breaks, we know f is between 9\n and 21. Drop the 2nd egg starting from floor 10 and going up one at a time to find f within 12 more\n drops. Total drops is 2 + 12 = 14.\n- If the 1st egg does not break again, follow a similar process dropping the 1st egg from floors 34, 45,\n 55, 64, 72, 79, 85, 90, 94, 97, 99, and 100.\nRegardless of the outcome, it takes at most 14 drops to determine f.\n\n\n
\n
Constraints:
\n\n1 <= n <= 1000
Table: Employees
\r\n+-------------+---------+\r\n| Column Name | Type |\r\n+-------------+---------+\r\n| employee_id | int |\r\n| name | varchar |\r\n| salary | int |\r\n+-------------+---------+\r\nemployee_id is the primary key for this table.\r\nEach row of this table indicates the employee ID, employee name, and salary.\r\n\r\n\r\n
\r\n\r\n
A company wants to divide the employees into teams such that all the members on each team have the same salary. The teams should follow these criteria:
\r\n\r\nteam_id = 1
. Note that the salaries for employees not on a team are not included in this ranking.Write an SQL query to get the team_id
of each employee that is in a team.
Return the result table ordered by team_id
in ascending order. In case of a tie, order it by employee_id
in ascending order.
The query result format is in the following example:
\r\n\r\n\r\n\r\n
\r\nEmployees table:\r\n+-------------+---------+--------+\r\n| employee_id | name | salary |\r\n+-------------+---------+--------+\r\n| 2 | Meir | 3000 |\r\n| 3 | Michael | 3000 |\r\n| 7 | Addilyn | 7400 |\r\n| 8 | Juan | 6100 |\r\n| 9 | Kannon | 7400 |\r\n+-------------+---------+--------+\r\n\r\nResult table:\r\n+-------------+---------+--------+---------+\r\n| employee_id | name | salary | team_id |\r\n+-------------+---------+--------+---------+\r\n| 2 | Meir | 3000 | 1 |\r\n| 3 | Michael | 3000 | 1 |\r\n| 7 | Addilyn | 7400 | 2 |\r\n| 9 | Kannon | 7400 | 2 |\r\n+-------------+---------+--------+---------+\r\n\r\nMeir (employee_id=2) and Michael (employee_id=3) are in the same team because they have the same salary of 3000.\r\nAddilyn (employee_id=7) and Kannon (employee_id=9) are in the same team because they have the same salary of 7400.\r\nJuan (employee_id=8) is not included in any team because their salary of 6100 is unique (i.e. no other employee has the same salary).\r\nThe team IDs are assigned as follows (based on salary ranking, lowest first):\r\n- team_id=1: Meir and Michael, salary of 3000\r\n- team_id=2: Addilyn and Kannon, salary of 7400\r\nJuan's salary of 6100 is not included in the ranking because they are not on a team.", "content_cn": null, "tags_en": [], "tags_cn": [], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "MySQL", "langSlug": "mysql", "code": "# Write your MySQL query statement below", "__typename": "CodeSnippetNode"}, {"lang": "MS SQL Server", "langSlug": "mssql", "code": "/* Write your T-SQL query statement below */", "__typename": "CodeSnippetNode"}, {"lang": "Oracle", "langSlug": "oraclesql", "code": "/* Write your PL/SQL query statement below */", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1875](https://leetcode-cn.com/problems/group-employees-of-the-same-salary)", "[Group Employees of the Same Salary](/solution/1800-1899/1875.Group%20Employees%20of%20the%20Same%20Salary/README_EN.md)", "", "\u4e2d\u7b49", ""], "md_table_row_en": ["[1875](https://leetcode.com/problems/group-employees-of-the-same-salary)", "[Group Employees of the Same Salary](/solution/1800-1899/1875.Group%20Employees%20of%20the%20Same%20Salary/README_EN.md)", "", "Medium", "\ud83d\udd12"]}, {"question_id": "2029", "frontend_question_id": "1874", "paid_only": true, "paid_only_cn": true, "url_cn": "https://leetcode-cn.com/problems/minimize-product-sum-of-two-arrays", "url_en": "https://leetcode.com/problems/minimize-product-sum-of-two-arrays", "relative_path_cn": "/solution/1800-1899/1874.Minimize%20Product%20Sum%20of%20Two%20Arrays/README.md", "relative_path_en": "/solution/1800-1899/1874.Minimize%20Product%20Sum%20of%20Two%20Arrays/README_EN.md", "title_cn": "", "title_en": "Minimize Product Sum of Two Arrays", "question_title_slug": "minimize-product-sum-of-two-arrays", "content_en": "
The product sum of two equal-length arrays a
and b
is equal to the sum of a[i] * b[i]
for all 0 <= i < a.length
(0-indexed).
a = [1,2,3,4]
and b = [5,2,3,1]
, the product sum would be 1*5 + 2*2 + 3*3 + 4*1 = 22
.Given two arrays nums1
and nums2
of length n
, return the minimum product sum if you are allowed to rearrange the order of the elements in nums1
.
\r\n
Example 1:
\r\n\r\n\r\nInput: nums1 = [5,3,4,2], nums2 = [4,2,2,5]\r\nOutput: 40\r\nExplanation: We can rearrange nums1 to become [3,5,4,2]. The product sum of [3,5,4,2] and [4,2,2,5] is 3*4 + 5*2 + 4*2 + 2*5 = 40.\r\n\r\n\r\n
Example 2:
\r\n\r\n\r\nInput: nums1 = [2,1,4,5,7], nums2 = [3,2,4,8,6]\r\nOutput: 65\r\nExplanation: We can rearrange nums1 to become [5,7,4,1,2]. The product sum of [5,7,4,1,2] and [3,2,4,8,6] is 5*3 + 7*2 + 4*4 + 1*8 + 2*6 = 65.\r\n\r\n\r\n
\r\n
Constraints:
\r\n\r\nn == nums1.length == nums2.length
1 <= n <= 105
1 <= nums1[i], nums2[i] <= 100
Table: Employees
\r\n+-------------+---------+\r\n| Column Name | Type |\r\n+-------------+---------+\r\n| employee_id | int |\r\n| name | varchar |\r\n| salary | int |\r\n+-------------+---------+\r\nemployee_id is the primary key for this table.\r\nEach row of this table indicates the employee ID, employee name, and salary.\r\n\r\n\r\n
\r\n\r\n
Write an SQL query to calculate the bonus of each employee. The bonus of an employee is 100%
of their salary if the ID of the employee is an odd number and the employee name does not start with the character 'M'
. The bonus of an employee is 0
otherwise.
Return the result table ordered by employee_id
.
The query result format is in the following example:
\r\n\r\n\r\n\r\n
\r\nEmployees table:\r\n+-------------+---------+--------+\r\n| employee_id | name | salary |\r\n+-------------+---------+--------+\r\n| 2 | Meir | 3000 |\r\n| 3 | Michael | 3800 |\r\n| 7 | Addilyn | 7400 |\r\n| 8 | Juan | 6100 |\r\n| 9 | Kannon | 7700 |\r\n+-------------+---------+--------+\r\n\r\nResult table:\r\n+-------------+-------+\r\n| employee_id | bonus |\r\n+-------------+-------+\r\n| 2 | 0 |\r\n| 3 | 0 |\r\n| 7 | 7400 |\r\n| 8 | 0 |\r\n| 9 | 7700 |\r\n+-------------+-------+\r\n\r\nThe employees with IDs 2 and 8 get 0 bonus because they have an even employee_id.\r\nThe employee with ID 3 gets 0 bonus because their name starts with 'M'.\r\nThe rest of the employees get a 100% bonus.\r\n", "content_cn": null, "tags_en": [], "tags_cn": [], "difficulty_en": "Easy", "difficulty_cn": "\u7b80\u5355", "code_snippets": [{"lang": "MySQL", "langSlug": "mysql", "code": "# Write your MySQL query statement below", "__typename": "CodeSnippetNode"}, {"lang": "MS SQL Server", "langSlug": "mssql", "code": "/* Write your T-SQL query statement below */", "__typename": "CodeSnippetNode"}, {"lang": "Oracle", "langSlug": "oraclesql", "code": "/* Write your PL/SQL query statement below */", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1873](https://leetcode-cn.com/problems/calculate-special-bonus)", "[Calculate Special Bonus](/solution/1800-1899/1873.Calculate%20Special%20Bonus/README_EN.md)", "", "\u7b80\u5355", ""], "md_table_row_en": ["[1873](https://leetcode.com/problems/calculate-special-bonus)", "[Calculate Special Bonus](/solution/1800-1899/1873.Calculate%20Special%20Bonus/README_EN.md)", "", "Easy", "\ud83d\udd12"]}, {"question_id": "2019", "frontend_question_id": "1868", "paid_only": true, "paid_only_cn": true, "url_cn": "https://leetcode-cn.com/problems/product-of-two-run-length-encoded-arrays", "url_en": "https://leetcode.com/problems/product-of-two-run-length-encoded-arrays", "relative_path_cn": "/solution/1800-1899/1868.Product%20of%20Two%20Run-Length%20Encoded%20Arrays/README.md", "relative_path_en": "/solution/1800-1899/1868.Product%20of%20Two%20Run-Length%20Encoded%20Arrays/README_EN.md", "title_cn": "", "title_en": "Product of Two Run-Length Encoded Arrays", "question_title_slug": "product-of-two-run-length-encoded-arrays", "content_en": "
Run-length encoding is a compression algorithm that allows for an integer array nums
with many segments of consecutive repeated numbers to be represented by a (generally smaller) 2D array encoded
. Each encoded[i] = [vali, freqi]
describes the ith
segment of repeated numbers in nums
where vali
is the value that is repeated freqi
times.
nums = [1,1,1,2,2,2,2,2]
is represented by the run-length encoded array encoded = [[1,3],[2,5]]
. Another way to read this is "three 1
's followed by five 2
's".The product of two run-length encoded arrays encoded1
and encoded2
can be calculated using the following steps:
encoded1
and encoded2
into the full arrays nums1
and nums2
respectively.prodNums
of length nums1.length
and set prodNums[i] = nums1[i] * nums2[i]
.prodNums
into a run-length encoded array and return it.You are given two run-length encoded arrays encoded1
and encoded2
representing full arrays nums1
and nums2
respectively. Both nums1
and nums2
have the same length. Each encoded1[i] = [vali, freqi]
describes the ith
segment of nums1
, and each encoded2[j] = [valj, freqj]
describes the jth
segment of nums2
.
Return the product of encoded1
and encoded2
.
Note: Compression should be done such that the run-length encoded array has the minimum possible length.
\n\n\n
Example 1:
\n\n\nInput: encoded1 = [[1,3],[2,3]], encoded2 = [[6,3],[3,3]]\nOutput: [[6,6]]\nExplanation: encoded1 expands to [1,1,1,2,2,2] and encoded2 expands to [6,6,6,3,3,3].\nprodNums = [6,6,6,6,6,6], which is compressed into the run-length encoded array [[6,6]].\n\n\n
Example 2:
\n\n\nInput: encoded1 = [[1,3],[2,1],[3,2]], encoded2 = [[2,3],[3,3]]\nOutput: [[2,3],[6,1],[9,2]]\nExplanation: encoded1 expands to [1,1,1,2,3,3] and encoded2 expands to [2,2,2,3,3,3].\nprodNums = [2,2,2,6,9,9], which is compressed into the run-length encoded array [[2,3],[6,1],[9,2]].\n\n\n
\n
Constraints:
\n\n1 <= encoded1.length, encoded2.length <= 105
encoded1[i].length == 2
encoded2[j].length == 2
1 <= vali, freqi <= 104
for each encoded1[i]
.1 <= valj, freqj <= 104
for each encoded2[j]
.encoded1
and encoded2
represent are the same length.Table: OrdersDetails
\n+-------------+------+\n| Column Name | Type |\n+-------------+------+\n| order_id | int |\n| product_id | int |\n| quantity | int |\n+-------------+------+\n(order_id, product_id) is the primary key for this table.\nA single order is represented as multiple rows, one row for each product in the order.\nEach row of this table contains the quantity ordered of the product product_id in the order order_id.\n\n\n
\n\n
You are running an ecommerce site that is looking for imbalanced orders. An imbalanced order is one whose maximum quantity is strictly greater than the average quantity of every order (including itself).
\n\nThe average quantity of an order is calculated as (total quantity of all products in the order) / (number of different products in the order)
. The maximum quantity of an order is the highest quantity
of any single product in the order.
Write an SQL query to find the order_id
of all imbalanced orders.
Return the result table in any order.
\n\nThe query result format is in the following example:
\n\n\n\n
\nOrdersDetails table:\n+----------+------------+----------+\n| order_id | product_id | quantity |\n+----------+------------+----------+\n| 1 | 1 | 12 |\n| 1 | 2 | 10 |\n| 1 | 3 | 15 |\n| 2 | 1 | 8 |\n| 2 | 4 | 4 |\n| 2 | 5 | 6 |\n| 3 | 3 | 5 |\n| 3 | 4 | 18 |\n| 4 | 5 | 2 |\n| 4 | 6 | 8 |\n| 5 | 7 | 9 |\n| 5 | 8 | 9 |\n| 3 | 9 | 20 |\n| 2 | 9 | 4 |\n+----------+------------+----------+\n\nResult table:\n+----------+\n| order_id |\n+----------+\n| 1 |\n| 3 |\n+----------+\n\nThe average quantity of each order is:\n- order_id=1: (12+10+15)/3 = 12.3333333\n- order_id=2: (8+4+6+4)/4 = 5.5\n- order_id=3: (5+18+20)/3 = 14.333333\n- order_id=4: (2+8)/2 = 5\n- order_id=5: (9+9)/2 = 9\n\nThe maximum quantity of each order is:\n- order_id=1: max(12, 10, 15) = 15\n- order_id=2: max(8, 4, 6, 4) = 8\n- order_id=3: max(5, 18, 20) = 20\n- order_id=4: max(2, 8) = 8\n- order_id=5: max(9, 9) = 9\n\nOrders 1 and 3 are imbalanced because they have a maximum quantity that exceeds the average quantity of every order.\n\n", "content_cn": null, "tags_en": [], "tags_cn": [], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "MySQL", "langSlug": "mysql", "code": "# Write your MySQL query statement below", "__typename": "CodeSnippetNode"}, {"lang": "MS SQL Server", "langSlug": "mssql", "code": "/* Write your T-SQL query statement below */", "__typename": "CodeSnippetNode"}, {"lang": "Oracle", "langSlug": "oraclesql", "code": "/* Write your PL/SQL query statement below */", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1867](https://leetcode-cn.com/problems/orders-with-maximum-quantity-above-average)", "[Orders With Maximum Quantity Above Average](/solution/1800-1899/1867.Orders%20With%20Maximum%20Quantity%20Above%20Average/README_EN.md)", "", "\u4e2d\u7b49", "\ud83d\udd12"], "md_table_row_en": ["[1867](https://leetcode.com/problems/orders-with-maximum-quantity-above-average)", "[Orders With Maximum Quantity Above Average](/solution/1800-1899/1867.Orders%20With%20Maximum%20Quantity%20Above%20Average/README_EN.md)", "", "Medium", "\ud83d\udd12"]}, {"question_id": "2013", "frontend_question_id": "1883", "paid_only": false, "paid_only_cn": false, "url_cn": "https://leetcode-cn.com/problems/minimum-skips-to-arrive-at-meeting-on-time", "url_en": "https://leetcode.com/problems/minimum-skips-to-arrive-at-meeting-on-time", "relative_path_cn": "/solution/1800-1899/1883.Minimum%20Skips%20to%20Arrive%20at%20Meeting%20On%20Time/README.md", "relative_path_en": "/solution/1800-1899/1883.Minimum%20Skips%20to%20Arrive%20at%20Meeting%20On%20Time/README_EN.md", "title_cn": "\u51c6\u65f6\u62b5\u8fbe\u4f1a\u8bae\u73b0\u573a\u7684\u6700\u5c0f\u8df3\u8fc7\u4f11\u606f\u6b21\u6570", "title_en": "Minimum Skips to Arrive at Meeting On Time", "question_title_slug": "minimum-skips-to-arrive-at-meeting-on-time", "content_en": "
You are given an integer hoursBefore
, the number of hours you have to travel to your meeting. To arrive at your meeting, you have to travel through n
roads. The road lengths are given as an integer array dist
of length n
, where dist[i]
describes the length of the ith
road in kilometers. In addition, you are given an integer speed
, which is the speed (in km/h) you will travel at.
After you travel road i
, you must rest and wait for the next integer hour before you can begin traveling on the next road. Note that you do not have to rest after traveling the last road because you are already at the meeting.
1.4
hours, you must wait until the 2
hour mark before traveling the next road. If traveling a road takes exactly 2
hours, you do not need to wait.However, you are allowed to skip some rests to be able to arrive on time, meaning you do not need to wait for the next integer hour. Note that this means you may finish traveling future roads at different hour marks.
\n\n1.4
hours and traveling the second road takes 0.6
hours. Skipping the rest after the first road will mean you finish traveling the second road right at the 2
hour mark, letting you start traveling the third road immediately.Return the minimum number of skips required to arrive at the meeting on time, or -1
if it is impossible.
\n
Example 1:
\n\n\nInput: dist = [1,3,2], speed = 4, hoursBefore = 2\nOutput: 1\nExplanation:\nWithout skipping any rests, you will arrive in (1/4 + 3/4) + (3/4 + 1/4) + (2/4) = 2.5 hours.\nYou can skip the first rest to arrive in ((1/4 + 0) + (3/4 + 0)) + (2/4) = 1.5 hours.\nNote that the second rest is shortened because you finish traveling the second road at an integer hour due to skipping the first rest.\n\n\n
Example 2:
\n\n\nInput: dist = [7,3,5,5], speed = 2, hoursBefore = 10\nOutput: 2\nExplanation:\nWithout skipping any rests, you will arrive in (7/2 + 1/2) + (3/2 + 1/2) + (5/2 + 1/2) + (5/2) = 11.5 hours.\nYou can skip the first and third rest to arrive in ((7/2 + 0) + (3/2 + 0)) + ((5/2 + 0) + (5/2)) = 10 hours.\n\n\n
Example 3:
\n\n\nInput: dist = [7,3,5,5], speed = 1, hoursBefore = 10\nOutput: -1\nExplanation: It is impossible to arrive at the meeting on time even if you skip all the rests.\n\n\n
\n
Constraints:
\n\nn == dist.length
1 <= n <= 1000
1 <= dist[i] <= 105
1 <= speed <= 106
1 <= hoursBefore <= 107
\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570 hoursBefore
\uff0c\u8868\u793a\u4f60\u8981\u524d\u5f80\u4f1a\u8bae\u6240\u5269\u4e0b\u7684\u53ef\u7528\u5c0f\u65f6\u6570\u3002\u8981\u60f3\u6210\u529f\u62b5\u8fbe\u4f1a\u8bae\u73b0\u573a\uff0c\u4f60\u5fc5\u987b\u9014\u7ecf n
\u6761\u9053\u8def\u3002\u9053\u8def\u7684\u957f\u5ea6\u7528\u4e00\u4e2a\u957f\u5ea6\u4e3a n
\u7684\u6574\u6570\u6570\u7ec4 dist
\u8868\u793a\uff0c\u5176\u4e2d dist[i]
\u8868\u793a\u7b2c i
\u6761\u9053\u8def\u7684\u957f\u5ea6\uff08\u5355\u4f4d\uff1a\u5343\u7c73\uff09\u3002\u53e6\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570 speed
\uff0c\u8868\u793a\u4f60\u5728\u9053\u8def\u4e0a\u524d\u8fdb\u7684\u901f\u5ea6\uff08\u5355\u4f4d\uff1a\u5343\u7c73\u6bcf\u5c0f\u65f6\uff09\u3002
\u5f53\u4f60\u901a\u8fc7\u7b2c i
\u6761\u8def\u4e4b\u540e\uff0c\u5c31\u5fc5\u987b\u4f11\u606f\u5e76\u7b49\u5f85\uff0c\u76f4\u5230 \u4e0b\u4e00\u4e2a\u6574\u6570\u5c0f\u65f6 \u624d\u80fd\u5f00\u59cb\u7ee7\u7eed\u901a\u8fc7\u4e0b\u4e00\u6761\u9053\u8def\u3002\u6ce8\u610f\uff1a\u4f60\u4e0d\u9700\u8981\u5728\u901a\u8fc7\u6700\u540e\u4e00\u6761\u9053\u8def\u540e\u4f11\u606f\uff0c\u56e0\u4e3a\u90a3\u65f6\u4f60\u5df2\u7ecf\u62b5\u8fbe\u4f1a\u8bae\u73b0\u573a\u3002
1.4
\u5c0f\u65f6\uff0c\u90a3\u4f60\u5fc5\u987b\u505c\u4e0b\u6765\u7b49\u5f85\uff0c\u5230\u00a02
\u5c0f\u65f6\u624d\u53ef\u4ee5\u7ee7\u7eed\u901a\u8fc7\u4e0b\u4e00\u6761\u9053\u8def\u3002\u5982\u679c\u901a\u8fc7\u4e00\u6761\u9053\u8def\u6070\u597d\u7528\u53bb 2
\u5c0f\u65f6\uff0c\u5c31\u65e0\u9700\u7b49\u5f85\uff0c\u53ef\u4ee5\u76f4\u63a5\u7ee7\u7eed\u3002\u7136\u800c\uff0c\u4e3a\u4e86\u80fd\u51c6\u65f6\u5230\u8fbe\uff0c\u4f60\u53ef\u4ee5\u9009\u62e9 \u8df3\u8fc7 \u4e00\u4e9b\u8def\u7684\u4f11\u606f\u65f6\u95f4\uff0c\u8fd9\u610f\u5473\u7740\u4f60\u4e0d\u5fc5\u7b49\u5f85\u4e0b\u4e00\u4e2a\u6574\u6570\u5c0f\u65f6\u3002\u6ce8\u610f\uff0c\u8fd9\u610f\u5473\u7740\u4e0e\u4e0d\u8df3\u8fc7\u4efb\u4f55\u4f11\u606f\u65f6\u95f4\u76f8\u6bd4\uff0c\u4f60\u53ef\u80fd\u5728\u4e0d\u540c\u65f6\u523b\u5230\u8fbe\u63a5\u4e0b\u6765\u7684\u9053\u8def\u3002
\n\n1
\u6761\u9053\u8def\u7528\u53bb 1.4
\u5c0f\u65f6\uff0c\u4e14\u901a\u8fc7\u7b2c 2
\u6761\u9053\u8def\u7528\u53bb 0.6
\u5c0f\u65f6\u3002\u8df3\u8fc7\u7b2c 1
\u6761\u9053\u8def\u7684\u4f11\u606f\u65f6\u95f4\u610f\u5473\u7740\u4f60\u5c06\u4f1a\u5728\u6070\u597d\u00a02
\u5c0f\u65f6\u5b8c\u6210\u901a\u8fc7\u7b2c 2
\u6761\u9053\u8def\uff0c\u4e14\u4f60\u80fd\u591f\u7acb\u5373\u5f00\u59cb\u901a\u8fc7\u7b2c 3
\u6761\u9053\u8def\u3002\u8fd4\u56de\u51c6\u65f6\u62b5\u8fbe\u4f1a\u8bae\u73b0\u573a\u6240\u9700\u8981\u7684 \u6700\u5c0f\u8df3\u8fc7\u6b21\u6570 \uff0c\u5982\u679c \u65e0\u6cd5\u51c6\u65f6\u53c2\u4f1a \uff0c\u8fd4\u56de -1
\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1adist = [1,3,2], speed = 4, hoursBefore = 2\n\u8f93\u51fa\uff1a1\n\u89e3\u91ca\uff1a\n\u4e0d\u8df3\u8fc7\u4efb\u4f55\u4f11\u606f\u65f6\u95f4\uff0c\u4f60\u5c06\u7528 (1/4 + 3/4) + (3/4 + 1/4) + (2/4) = 2.5 \u5c0f\u65f6\u624d\u80fd\u62b5\u8fbe\u4f1a\u8bae\u73b0\u573a\u3002\n\u53ef\u4ee5\u8df3\u8fc7\u7b2c 1 \u6b21\u4f11\u606f\u65f6\u95f4\uff0c\u5171\u7528 ((1/4 + 0) + (3/4 + 0)) + (2/4) = 1.5 \u5c0f\u65f6\u62b5\u8fbe\u4f1a\u8bae\u73b0\u573a\u3002\n\u6ce8\u610f\uff0c\u7b2c 2 \u6b21\u4f11\u606f\u65f6\u95f4\u7f29\u77ed\u4e3a 0 \uff0c\u7531\u4e8e\u8df3\u8fc7\u7b2c 1 \u6b21\u4f11\u606f\u65f6\u95f4\uff0c\u4f60\u662f\u5728\u6574\u6570\u5c0f\u65f6\u5904\u5b8c\u6210\u901a\u8fc7\u7b2c 2 \u6761\u9053\u8def\u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1adist = [7,3,5,5], speed = 2, hoursBefore = 10\n\u8f93\u51fa\uff1a2\n\u89e3\u91ca\uff1a\n\u4e0d\u8df3\u8fc7\u4efb\u4f55\u4f11\u606f\u65f6\u95f4\uff0c\u4f60\u5c06\u7528 (7/2 + 1/2) + (3/2 + 1/2) + (5/2 + 1/2) + (5/2) = 11.5 \u5c0f\u65f6\u624d\u80fd\u62b5\u8fbe\u4f1a\u8bae\u73b0\u573a\u3002\n\u53ef\u4ee5\u8df3\u8fc7\u7b2c 1 \u6b21\u548c\u7b2c 3 \u6b21\u4f11\u606f\u65f6\u95f4\uff0c\u5171\u7528 ((7/2 + 0) + (3/2 + 0)) + ((5/2 + 0) + (5/2)) = 10 \u5c0f\u65f6\u62b5\u8fbe\u4f1a\u8bae\u73b0\u573a\u3002\n\n\n
\u793a\u4f8b 3\uff1a
\n\n\n\u8f93\u5165\uff1adist = [7,3,5,5], speed = 1, hoursBefore = 10\n\u8f93\u51fa\uff1a-1\n\u89e3\u91ca\uff1a\u5373\u4f7f\u8df3\u8fc7\u6240\u6709\u7684\u4f11\u606f\u65f6\u95f4\uff0c\u4e5f\u65e0\u6cd5\u51c6\u65f6\u53c2\u52a0\u4f1a\u8bae\u3002\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\nn == dist.length
1 <= n <= 1000
1 <= dist[i] <= 105
1 <= speed <= 106
1 <= hoursBefore <= 107
You are given two 0-indexed integer arrays servers
and tasks
of lengths n
\u200b\u200b\u200b\u200b\u200b\u200b and m
\u200b\u200b\u200b\u200b\u200b\u200b respectively. servers[i]
is the weight of the i\u200b\u200b\u200b\u200b\u200b\u200bth
\u200b\u200b\u200b\u200b server, and tasks[j]
is the time needed to process the j\u200b\u200b\u200b\u200b\u200b\u200bth
\u200b\u200b\u200b\u200b task in seconds.
Tasks are assigned to the servers using a task queue. Initially, all servers are free, and the queue is empty.
\n\nAt second j
, the jth
task is inserted into the queue (starting with the 0th
task being inserted at second 0
). As long as there are free servers and the queue is not empty, the task in the front of the queue will be assigned to a free server with the smallest weight, and in case of a tie, it is assigned to a free server with the smallest index.
If there are no free servers and the queue is not empty, we wait until a server becomes free and immediately assign the next task. If multiple servers become free at the same time, then multiple tasks from the queue will be assigned in order of insertion following the weight and index priorities above.
\n\nA server that is assigned task j
at second t
will be free again at second t + tasks[j]
.
Build an array ans
\u200b\u200b\u200b\u200b of length m
, where ans[j]
is the index of the server the j\u200b\u200b\u200b\u200b\u200b\u200bth
task will be assigned to.
Return the array ans
\u200b\u200b\u200b\u200b.
\n
Example 1:
\n\n\nInput: servers = [3,3,2], tasks = [1,2,3,2,1,2]\nOutput: [2,2,0,2,1,2]\nExplanation: Events in chronological order go as follows:\n- At second 0, task 0 is added and processed using server 2 until second 1.\n- At second 1, server 2 becomes free. Task 1 is added and processed using server 2 until second 3.\n- At second 2, task 2 is added and processed using server 0 until second 5.\n- At second 3, server 2 becomes free. Task 3 is added and processed using server 2 until second 5.\n- At second 4, task 4 is added and processed using server 1 until second 5.\n- At second 5, all servers become free. Task 5 is added and processed using server 2 until second 7.\n\n
Example 2:
\n\n\nInput: servers = [5,1,4,3,2], tasks = [2,1,2,4,5,2,1]\nOutput: [1,4,1,4,1,3,2]\nExplanation: Events in chronological order go as follows: \n- At second 0, task 0 is added and processed using server 1 until second 2.\n- At second 1, task 1 is added and processed using server 4 until second 2.\n- At second 2, servers 1 and 4 become free. Task 2 is added and processed using server 1 until second 4. \n- At second 3, task 3 is added and processed using server 4 until second 7.\n- At second 4, server 1 becomes free. Task 4 is added and processed using server 1 until second 9. \n- At second 5, task 5 is added and processed using server 3 until second 7.\n- At second 6, task 6 is added and processed using server 2 until second 7.\n\n\n
\n
Constraints:
\n\nservers.length == n
tasks.length == m
1 <= n, m <= 2 * 105
1 <= servers[i], tasks[j] <= 2 * 105
\u7ed9\u4f60\u4e24\u4e2a \u4e0b\u6807\u4ece 0 \u5f00\u59cb \u7684\u6574\u6570\u6570\u7ec4 servers
\u548c tasks
\uff0c\u957f\u5ea6\u5206\u522b\u4e3a n
\u200b\u200b\u200b\u200b\u200b\u200b \u548c m
\u200b\u200b\u200b\u200b\u200b\u200b \u3002servers[i]
\u662f\u7b2c i\u200b\u200b\u200b\u200b\u200b\u200b
\u200b\u200b\u200b\u200b \u53f0\u670d\u52a1\u5668\u7684 \u6743\u91cd \uff0c\u800c tasks[j]
\u662f\u5904\u7406\u7b2c j\u200b\u200b\u200b\u200b\u200b\u200b
\u9879\u4efb\u52a1 \u6240\u9700\u8981\u7684\u65f6\u95f4\uff08\u5355\u4f4d\uff1a\u79d2\uff09\u3002
\u4f60\u6b63\u5728\u8fd0\u884c\u4e00\u4e2a\u4eff\u771f\u7cfb\u7edf\uff0c\u5728\u5904\u7406\u5b8c\u6240\u6709\u4efb\u52a1\u540e\uff0c\u8be5\u7cfb\u7edf\u5c06\u4f1a\u5173\u95ed\u3002\u6bcf\u53f0\u670d\u52a1\u5668\u53ea\u80fd\u540c\u65f6\u5904\u7406\u4e00\u9879\u4efb\u52a1\u3002\u7b2c 0
\u9879\u4efb\u52a1\u5728\u7b2c 0
\u79d2\u53ef\u4ee5\u5f00\u59cb\u5904\u7406\uff0c\u76f8\u5e94\u5730\uff0c\u7b2c j
\u9879\u4efb\u52a1\u5728\u7b2c j
\u00a0\u79d2\u53ef\u4ee5\u5f00\u59cb\u5904\u7406\u3002\u5904\u7406\u7b2c j
\u9879\u4efb\u52a1\u65f6\uff0c\u4f60\u9700\u8981\u4e3a\u5b83\u5206\u914d\u4e00\u53f0 \u6743\u91cd\u6700\u5c0f \u7684\u7a7a\u95f2\u670d\u52a1\u5668\u3002\u5982\u679c\u5b58\u5728\u591a\u53f0\u76f8\u540c\u6743\u91cd\u7684\u7a7a\u95f2\u670d\u52a1\u5668\uff0c\u8bf7\u9009\u62e9 \u4e0b\u6807\u6700\u5c0f \u7684\u670d\u52a1\u5668\u3002\u5982\u679c\u4e00\u53f0\u7a7a\u95f2\u670d\u52a1\u5668\u5728\u7b2c t
\u79d2\u5206\u914d\u5230\u7b2c j
\u9879\u4efb\u52a1\uff0c\u90a3\u4e48\u5728 t + tasks[j]
\u65f6\u5b83\u5c06\u6062\u590d\u7a7a\u95f2\u72b6\u6001\u3002
\u5982\u679c\u6ca1\u6709\u7a7a\u95f2\u670d\u52a1\u5668\uff0c\u5219\u5fc5\u987b\u7b49\u5f85\uff0c\u76f4\u5230\u51fa\u73b0\u4e00\u53f0\u7a7a\u95f2\u670d\u52a1\u5668\uff0c\u5e76 \u5c3d\u53ef\u80fd\u65e9\u00a0\u5730\u5904\u7406\u5269\u4f59\u4efb\u52a1\u3002 \u5982\u679c\u6709\u591a\u9879\u4efb\u52a1\u7b49\u5f85\u5206\u914d\uff0c\u5219\u6309\u7167 \u4e0b\u6807\u9012\u589e \u7684\u987a\u5e8f\u5b8c\u6210\u5206\u914d\u3002
\n\n\u5982\u679c\u540c\u4e00\u65f6\u523b\u5b58\u5728\u591a\u53f0\u7a7a\u95f2\u670d\u52a1\u5668\uff0c\u53ef\u4ee5\u540c\u65f6\u5c06\u591a\u9879\u4efb\u52a1\u5206\u522b\u5206\u914d\u7ed9\u5b83\u4eec\u3002
\n\n\u6784\u5efa\u957f\u5ea6\u4e3a\u00a0m
\u7684\u7b54\u6848\u6570\u7ec4 ans
\uff0c\u5176\u4e2d ans[j]
\u662f\u7b2c j
\u9879\u4efb\u52a1\u5206\u914d\u7684\u670d\u52a1\u5668\u7684\u4e0b\u6807\u3002
\u8fd4\u56de\u7b54\u6848\u6570\u7ec4 ans
\u200b\u200b\u200b\u200b \u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1aservers = [3,3,2], tasks = [1,2,3,2,1,2]\n\u8f93\u51fa\uff1a[2,2,0,2,1,2]\n\u89e3\u91ca\uff1a\u4e8b\u4ef6\u6309\u65f6\u95f4\u987a\u5e8f\u5982\u4e0b\uff1a\n- 0 \u79d2\u65f6\uff0c\u7b2c 0 \u9879\u4efb\u52a1\u52a0\u5165\u5230\u4efb\u52a1\u961f\u5217\uff0c\u4f7f\u7528\u7b2c 2 \u53f0\u670d\u52a1\u5668\u5904\u7406\u5230 1 \u79d2\u3002\n- 1 \u79d2\u65f6\uff0c\u7b2c 2 \u53f0\u670d\u52a1\u5668\u7a7a\u95f2\uff0c\u7b2c 1 \u9879\u4efb\u52a1\u52a0\u5165\u5230\u4efb\u52a1\u961f\u5217\uff0c\u4f7f\u7528\u7b2c 2 \u53f0\u670d\u52a1\u5668\u5904\u7406\u5230 3 \u79d2\u3002\n- 2 \u79d2\u65f6\uff0c\u7b2c 2 \u9879\u4efb\u52a1\u52a0\u5165\u5230\u4efb\u52a1\u961f\u5217\uff0c\u4f7f\u7528\u7b2c 0 \u53f0\u670d\u52a1\u5668\u5904\u7406\u5230 5 \u79d2\u3002\n- 3 \u79d2\u65f6\uff0c\u7b2c 2 \u53f0\u670d\u52a1\u5668\u7a7a\u95f2\uff0c\u7b2c 3 \u9879\u4efb\u52a1\u52a0\u5165\u5230\u4efb\u52a1\u961f\u5217\uff0c\u4f7f\u7528\u7b2c 2 \u53f0\u670d\u52a1\u5668\u5904\u7406\u5230 5 \u79d2\u3002\n- 4 \u79d2\u65f6\uff0c\u7b2c 4 \u9879\u4efb\u52a1\u52a0\u5165\u5230\u4efb\u52a1\u961f\u5217\uff0c\u4f7f\u7528\u7b2c 1 \u53f0\u670d\u52a1\u5668\u5904\u7406\u5230 5 \u79d2\u3002\n- 5 \u79d2\u65f6\uff0c\u6240\u6709\u670d\u52a1\u5668\u90fd\u7a7a\u95f2\uff0c\u7b2c 5 \u9879\u4efb\u52a1\u52a0\u5165\u5230\u4efb\u52a1\u961f\u5217\uff0c\u4f7f\u7528\u7b2c 2 \u53f0\u670d\u52a1\u5668\u5904\u7406\u5230 7 \u79d2\u3002\n\n
\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1aservers = [5,1,4,3,2], tasks = [2,1,2,4,5,2,1]\n\u8f93\u51fa\uff1a[1,4,1,4,1,3,2]\n\u89e3\u91ca\uff1a\u4e8b\u4ef6\u6309\u65f6\u95f4\u987a\u5e8f\u5982\u4e0b\uff1a\n- 0 \u79d2\u65f6\uff0c\u7b2c 0 \u9879\u4efb\u52a1\u52a0\u5165\u5230\u4efb\u52a1\u961f\u5217\uff0c\u4f7f\u7528\u7b2c 1 \u53f0\u670d\u52a1\u5668\u5904\u7406\u5230 2 \u79d2\u3002\n- 1 \u79d2\u65f6\uff0c\u7b2c 1 \u9879\u4efb\u52a1\u52a0\u5165\u5230\u4efb\u52a1\u961f\u5217\uff0c\u4f7f\u7528\u7b2c 4 \u53f0\u670d\u52a1\u5668\u5904\u7406\u5230 2 \u79d2\u3002\n- 2 \u79d2\u65f6\uff0c\u7b2c 1 \u53f0\u548c\u7b2c 4 \u53f0\u670d\u52a1\u5668\u7a7a\u95f2\uff0c\u7b2c 2 \u9879\u4efb\u52a1\u52a0\u5165\u5230\u4efb\u52a1\u961f\u5217\uff0c\u4f7f\u7528\u7b2c 1 \u53f0\u670d\u52a1\u5668\u5904\u7406\u5230 4 \u79d2\u3002\n- 3 \u79d2\u65f6\uff0c\u7b2c 3 \u9879\u4efb\u52a1\u52a0\u5165\u5230\u4efb\u52a1\u961f\u5217\uff0c\u4f7f\u7528\u7b2c 4 \u53f0\u670d\u52a1\u5668\u5904\u7406\u5230 7 \u79d2\u3002\n- 4 \u79d2\u65f6\uff0c\u7b2c 1 \u53f0\u670d\u52a1\u5668\u7a7a\u95f2\uff0c\u7b2c 4 \u9879\u4efb\u52a1\u52a0\u5165\u5230\u4efb\u52a1\u961f\u5217\uff0c\u4f7f\u7528\u7b2c 1 \u53f0\u670d\u52a1\u5668\u5904\u7406\u5230 9 \u79d2\u3002\n- 5 \u79d2\u65f6\uff0c\u7b2c 5 \u9879\u4efb\u52a1\u52a0\u5165\u5230\u4efb\u52a1\u961f\u5217\uff0c\u4f7f\u7528\u7b2c 3 \u53f0\u670d\u52a1\u5668\u5904\u7406\u5230 7 \u79d2\u3002\n- 6 \u79d2\u65f6\uff0c\u7b2c 6 \u9879\u4efb\u52a1\u52a0\u5165\u5230\u4efb\u52a1\u961f\u5217\uff0c\u4f7f\u7528\u7b2c 2 \u53f0\u670d\u52a1\u5668\u5904\u7406\u5230 7 \u79d2\u3002\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\nservers.length == n
tasks.length == m
1 <= n, m <= 2 * 105
1 <= servers[i], tasks[j] <= 2 * 105
You are given a very large integer n
, represented as a string,\u200b\u200b\u200b\u200b\u200b\u200b and an integer digit x
. The digits in n
and the digit x
are in the inclusive range [1, 9]
, and n
may represent a negative number.
You want to maximize n
's numerical value by inserting x
anywhere in the decimal representation of n
\u200b\u200b\u200b\u200b\u200b\u200b. You cannot insert x
to the left of the negative sign.
n = 73
and x = 6
, it would be best to insert it between 7
and 3
, making n = 763
.n = -55
and x = 2
, it would be best to insert it before the first 5
, making n = -255
.Return a string representing the maximum value of n
\u200b\u200b\u200b\u200b\u200b\u200b after the insertion.
\n
Example 1:
\n\n\nInput: n = "99", x = 9\nOutput: "999"\nExplanation: The result is the same regardless of where you insert 9.\n\n\n
Example 2:
\n\n\nInput: n = "-13", x = 2\nOutput: "-123"\nExplanation: You can make n one of {-213, -123, -132}, and the largest of those three is -123.\n\n\n
\n
Constraints:
\n\n1 <= n.length <= 105
1 <= x <= 9
n
\u200b\u200b\u200b are in the range [1, 9]
.n
is a valid representation of an integer.n
,\u200b\u200b\u200b\u200b\u200b\u200b it will begin with '-'
.\u7ed9\u4f60\u4e00\u4e2a\u975e\u5e38\u5927\u7684\u6574\u6570 n
\u548c\u4e00\u4e2a\u6574\u6570\u6570\u5b57 x
\uff0c\u5927\u6574\u6570 n
\u00a0\u7528\u4e00\u4e2a\u5b57\u7b26\u4e32\u8868\u793a\u3002n
\u4e2d\u6bcf\u4e00\u4f4d\u6570\u5b57\u548c\u6570\u5b57 x
\u90fd\u5904\u4e8e\u95ed\u533a\u95f4 [1, 9]
\u4e2d\uff0c\u4e14 n
\u53ef\u80fd\u8868\u793a\u4e00\u4e2a \u8d1f\u6570 \u3002
\u4f60\u6253\u7b97\u901a\u8fc7\u5728 n
\u7684\u5341\u8fdb\u5236\u8868\u793a\u7684\u4efb\u610f\u4f4d\u7f6e\u63d2\u5165 x
\u6765 \u6700\u5927\u5316 n
\u7684 \u6570\u503c \u200b\u200b\u200b\u200b\u200b\u200b\u3002\u4f46 \u4e0d\u80fd \u5728\u8d1f\u53f7\u7684\u5de6\u8fb9\u63d2\u5165 x
\u3002
n = 73
\u4e14 x = 6
\uff0c\u90a3\u4e48\u6700\u4f73\u65b9\u6848\u662f\u5c06 6
\u63d2\u5165 7
\u548c 3
\u4e4b\u95f4\uff0c\u4f7f n = 763
\u3002n = -55
\u4e14 x = 2
\uff0c\u90a3\u4e48\u6700\u4f73\u65b9\u6848\u662f\u5c06 2
\u63d2\u5728\u7b2c\u4e00\u4e2a 5
\u4e4b\u524d\uff0c\u4f7f n = -255
\u3002\u8fd4\u56de\u63d2\u5165\u64cd\u4f5c\u540e\uff0c\u7528\u5b57\u7b26\u4e32\u8868\u793a\u7684\u00a0n
\u7684\u6700\u5927\u503c\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1an = \"99\", x = 9\n\u8f93\u51fa\uff1a\"999\"\n\u89e3\u91ca\uff1a\u4e0d\u7ba1\u5728\u54ea\u91cc\u63d2\u5165 9 \uff0c\u7ed3\u679c\u90fd\u662f\u76f8\u540c\u7684\u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1an = \"-13\", x = 2\n\u8f93\u51fa\uff1a\"-123\"\n\u89e3\u91ca\uff1a\u5411 n \u4e2d\u63d2\u5165 x \u53ef\u4ee5\u5f97\u5230 -213\u3001-123 \u6216\u8005 -132 \uff0c\u4e09\u8005\u4e2d\u6700\u5927\u7684\u662f -123 \u3002\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= n.length <= 105
1 <= x <= 9
n
\u200b\u200b\u200b \u4e2d\u6bcf\u4e00\u4f4d\u7684\u6570\u5b57\u90fd\u5728\u95ed\u533a\u95f4 [1, 9]
\u4e2d\u3002n
\u00a0\u4ee3\u8868\u4e00\u4e2a\u6709\u6548\u7684\u6574\u6570\u3002n
\u8868\u793a\u8d1f\u6570\u65f6\uff0c\u5c06\u4f1a\u4ee5\u5b57\u7b26 '-'
\u5f00\u59cb\u3002The letter value of a letter is its position in the alphabet starting from 0 (i.e. 'a' -> 0
, 'b' -> 1
, 'c' -> 2
, etc.).
The numerical value of some string of lowercase English letters s
is the concatenation of the letter values of each letter in s
, which is then converted into an integer.
s = "acb"
, we concatenate each letter's letter value, resulting in "021"
. After converting it, we get 21
.You are given three strings firstWord
, secondWord
, and targetWord
, each consisting of lowercase English letters 'a'
through 'j'
inclusive.
Return true
if the summation of the numerical values of firstWord
and secondWord
equals the numerical value of targetWord
, or false
otherwise.
\n
Example 1:
\n\n\nInput: firstWord = "acb", secondWord = "cba", targetWord = "cdb"\nOutput: true\nExplanation:\nThe numerical value of firstWord is "acb" -> "021" -> 21.\nThe numerical value of secondWord is "cba" -> "210" -> 210.\nThe numerical value of targetWord is "cdb" -> "231" -> 231.\nWe return true because 21 + 210 == 231.\n\n\n
Example 2:
\n\n\nInput: firstWord = "aaa", secondWord = "a", targetWord = "aab"\nOutput: false\nExplanation: \nThe numerical value of firstWord is "aaa" -> "000" -> 0.\nThe numerical value of secondWord is "a" -> "0" -> 0.\nThe numerical value of targetWord is "aab" -> "001" -> 1.\nWe return false because 0 + 0 != 1.\n\n\n
Example 3:
\n\n\nInput: firstWord = "aaa", secondWord = "a", targetWord = "aaaa"\nOutput: true\nExplanation: \nThe numerical value of firstWord is "aaa" -> "000" -> 0.\nThe numerical value of secondWord is "a" -> "0" -> 0.\nThe numerical value of targetWord is "aaaa" -> "0000" -> 0.\nWe return true because 0 + 0 == 0.\n\n\n
\n
Constraints:
\n\n1 <= firstWord.length,
secondWord.length,
targetWord.length <= 8
firstWord
, secondWord
, and targetWord
consist of lowercase English letters from 'a'
to 'j'
inclusive.\u5b57\u6bcd\u7684 \u5b57\u6bcd\u503c \u53d6\u51b3\u4e8e\u5b57\u6bcd\u5728\u5b57\u6bcd\u8868\u4e2d\u7684\u4f4d\u7f6e\uff0c\u4ece 0 \u5f00\u59cb \u8ba1\u6570\u3002\u5373\uff0c'a' -> 0
\u3001'b' -> 1
\u3001'c' -> 2
\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002
\u5bf9\u67d0\u4e2a\u7531\u5c0f\u5199\u5b57\u6bcd\u7ec4\u6210\u7684\u5b57\u7b26\u4e32\u00a0s
\u800c\u8a00\uff0c\u5176 \u6570\u503c \u5c31\u7b49\u4e8e\u5c06 s
\u4e2d\u6bcf\u4e2a\u5b57\u6bcd\u7684 \u5b57\u6bcd\u503c \u6309\u987a\u5e8f \u8fde\u63a5 \u5e76 \u8f6c\u6362 \u6210\u5bf9\u5e94\u6574\u6570\u3002
s = \"acb\"
\uff0c\u4f9d\u6b21\u8fde\u63a5\u6bcf\u4e2a\u5b57\u6bcd\u7684\u5b57\u6bcd\u503c\u53ef\u4ee5\u5f97\u5230 \"021\"
\uff0c\u8f6c\u6362\u4e3a\u6574\u6570\u5f97\u5230 21
\u3002\u7ed9\u4f60\u4e09\u4e2a\u5b57\u7b26\u4e32 firstWord
\u3001secondWord
\u548c targetWord
\uff0c\u6bcf\u4e2a\u5b57\u7b26\u4e32\u90fd\u7531\u4ece 'a'
\u5230 'j'
\uff08\u542b\u00a0'a'
\u548c 'j'
\uff09\u7684\u5c0f\u5199\u82f1\u6587\u5b57\u6bcd\u7ec4\u6210\u3002
\u5982\u679c\u00a0firstWord
\u548c secondWord
\u7684 \u6570\u503c\u4e4b\u548c \u7b49\u4e8e targetWord
\u7684\u6570\u503c\uff0c\u8fd4\u56de true
\uff1b\u5426\u5219\uff0c\u8fd4\u56de false
\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1afirstWord = \"acb\", secondWord = \"cba\", targetWord = \"cdb\"\n\u8f93\u51fa\uff1atrue\n\u89e3\u91ca\uff1a\nfirstWord \u7684\u6570\u503c\u4e3a \"acb\" -> \"021\" -> 21\nsecondWord \u7684\u6570\u503c\u4e3a \"cba\" -> \"210\" -> 210\ntargetWord \u7684\u6570\u503c\u4e3a \"cdb\" -> \"231\" -> 231\n\u7531\u4e8e 21 + 210 == 231 \uff0c\u8fd4\u56de true\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1afirstWord = \"aaa\", secondWord = \"a\", targetWord = \"aab\"\n\u8f93\u51fa\uff1afalse\n\u89e3\u91ca\uff1a\nfirstWord \u7684\u6570\u503c\u4e3a \"aaa\" -> \"000\" -> 0\nsecondWord \u7684\u6570\u503c\u4e3a \"a\" -> \"0\" -> 0\ntargetWord \u7684\u6570\u503c\u4e3a \"aab\" -> \"001\" -> 1\n\u7531\u4e8e 0 + 0 != 1 \uff0c\u8fd4\u56de false\n\n
\u793a\u4f8b 3\uff1a
\n\n\u8f93\u5165\uff1afirstWord = \"aaa\", secondWord = \"a\", targetWord = \"aaaa\"\n\u8f93\u51fa\uff1atrue\n\u89e3\u91ca\uff1a\nfirstWord \u7684\u6570\u503c\u4e3a \"aaa\" -> \"000\" -> 0\nsecondWord \u7684\u6570\u503c\u4e3a \"a\" -> \"0\" -> 0\ntargetWord \u7684\u6570\u503c\u4e3a \"aaaa\" -> \"0000\" -> 0\n\u7531\u4e8e 0 + 0 == 0 \uff0c\u8fd4\u56de true\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= firstWord.length,
secondWord.length,
targetWord.length <= 8
firstWord
\u3001secondWord
\u548c targetWord
\u4ec5\u7531\u4ece 'a'
\u5230 'j'
\uff08\u542b\u00a0'a'
\u548c 'j'
\uff09\u7684\u5c0f\u5199\u82f1\u6587\u5b57\u6bcd\u7ec4\u6210\u3002Given an array of strings words
, find the longest string in words
such that every prefix of it is also in words
.
words = ["a", "app", "ap"]
. The string "app"
has prefixes "ap"
and "a"
, all of which are in words
.Return the string described above. If there is more than one string with the same length, return the lexicographically smallest one, and if no string exists, return ""
.
\r\n
Example 1:
\r\n\r\n\r\nInput: words = ["k","ki","kir","kira", "kiran"]\r\nOutput: "kiran"\r\nExplanation: "kiran" has prefixes "kira", "kir", "ki", and "k", and all of them appear in words.\r\n\r\n\r\n
Example 2:
\r\n\r\n\r\nInput: words = ["a", "banana", "app", "appl", "ap", "apply", "apple"]\r\nOutput: "apple"\r\nExplanation: Both "apple" and "apply" have all their prefixes in words.\r\nHowever, "apple" is lexicographically smaller, so we return that.\r\n\r\n\r\n
Example 3:
\r\n\r\n\r\nInput: words = ["abc", "bc", "ab", "qwe"]\r\nOutput: ""\r\n\r\n\r\n
\r\n
Constraints:
\r\n\r\n1 <= words.length <= 105
1 <= words[i].length <= 105
1 <= sum(words[i].length) <= 105
Table: Days
\n+-------------+------+\n| Column Name | Type |\n+-------------+------+\n| day | date |\n+-------------+------+\nday is the primary key for this table.\n\n\n
\n\n
Write an SQL query to convert each date in Days
into a string formatted as "day_name, month_name day, year"
.
Return the result table in any order.
\n\nThe query result format is in the following example:
\n\n\n\n
\nDays table:\n+------------+\n| day |\n+------------+\n| 2022-04-12 |\n| 2021-08-09 |\n| 2020-06-26 |\n+------------+\n\nResult table:\n+-------------------------+\n| day |\n+-------------------------+\n| Tuesday, April 12, 2022 |\n| Monday, August 9, 2021 |\n| Friday, June 26, 2020 |\n+-------------------------+\nPlease note that the output is case-sensitive.\n\n", "content_cn": null, "tags_en": [], "tags_cn": [], "difficulty_en": "Easy", "difficulty_cn": "\u7b80\u5355", "code_snippets": [{"lang": "MySQL", "langSlug": "mysql", "code": "# Write your MySQL query statement below", "__typename": "CodeSnippetNode"}, {"lang": "MS SQL Server", "langSlug": "mssql", "code": "/* Write your T-SQL query statement below */", "__typename": "CodeSnippetNode"}, {"lang": "Oracle", "langSlug": "oraclesql", "code": "/* Write your PL/SQL query statement below */", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1853](https://leetcode-cn.com/problems/convert-date-format)", "[Convert Date Format](/solution/1800-1899/1853.Convert%20Date%20Format/README_EN.md)", "", "\u7b80\u5355", "\ud83d\udd12"], "md_table_row_en": ["[1853](https://leetcode.com/problems/convert-date-format)", "[Convert Date Format](/solution/1800-1899/1853.Convert%20Date%20Format/README_EN.md)", "", "Easy", "\ud83d\udd12"]}, {"question_id": "2003", "frontend_question_id": "1852", "paid_only": true, "paid_only_cn": true, "url_cn": "https://leetcode-cn.com/problems/distinct-numbers-in-each-subarray", "url_en": "https://leetcode.com/problems/distinct-numbers-in-each-subarray", "relative_path_cn": "/solution/1800-1899/1852.Distinct%20Numbers%20in%20Each%20Subarray/README.md", "relative_path_en": "/solution/1800-1899/1852.Distinct%20Numbers%20in%20Each%20Subarray/README_EN.md", "title_cn": "", "title_en": "Distinct Numbers in Each Subarray", "question_title_slug": "distinct-numbers-in-each-subarray", "content_en": "
Given an integer array nums
and an integer k
, you are asked to construct the array ans
of size n-k+1
where ans[i]
is the number of distinct numbers in the subarray nums[i:i+k-1] = [nums[i], nums[i+1], ..., nums[i+k-1]]
.
Return the array ans
.
\r\n
Example 1:
\r\n\r\n\r\nInput: nums = [1,2,3,2,2,1,3], k = 3\r\nOutput: [3,2,2,2,3]\r\nExplanation: The number of distinct elements in each subarray goes as follows:\r\n- nums[0:2] = [1,2,3] so ans[0] = 3\r\n- nums[1:3] = [2,3,2] so ans[1] = 2\r\n- nums[2:4] = [3,2,2] so ans[2] = 2\r\n- nums[3:5] = [2,2,1] so ans[3] = 2\r\n- nums[4:6] = [2,1,3] so ans[4] = 3\r\n\r\n\r\n
Example 2:
\r\n\r\n\r\nInput: nums = [1,1,1,1,2,3,4], k = 4\r\nOutput: [1,2,3,4]\r\nExplanation: The number of distinct elements in each subarray goes as follows:\r\n- nums[0:3] = [1,1,1,1] so ans[0] = 1\r\n- nums[1:4] = [1,1,1,2] so ans[1] = 2\r\n- nums[2:5] = [1,1,2,3] so ans[2] = 3\r\n- nums[3:6] = [1,2,3,4] so ans[3] = 4\r\n\r\n\r\n
\r\n
Constraints:
\r\n\r\n1 <= k <= nums.length <= 105
1 <= nums[i] <= 105
Alice and Bob take turns playing a game, with Alice starting first.
\r\n\r\nThere are n
stones arranged in a row. On each player's turn, while the number of stones is more than one, they will do the following:
x > 1
, and remove the leftmost x
stones from the row.The game stops when only one stone is left in the row.
\r\n\r\nThe score difference between Alice and Bob is (Alice's score - Bob's score)
. Alice's goal is to maximize the score difference, and Bob's goal is the minimize the score difference.
Given an integer array stones
of length n
where stones[i]
represents the value of the ith
stone from the left, return the score difference between Alice and Bob if they both play optimally.
\r\n
Example 1:
\r\n\r\n\r\nInput: stones = [-1,2,-3,4,-5]\r\nOutput: 5\r\nExplanation:\r\n- Alice removes the first 4 stones, adds (-1) + 2 + (-3) + 4 = 2 to her score, and places a stone of\r\n value 2 on the left. stones = [2,-5].\r\n- Bob removes the first 2 stones, adds 2 + (-5) = -3 to his score, and places a stone of value -3 on\r\n the left. stones = [-3].\r\nThe difference between their scores is 2 - (-3) = 5.\r\n\r\n\r\n
Example 2:
\r\n\r\n\r\nInput: stones = [7,-6,5,10,5,-2,-6]\r\nOutput: 13\r\nExplanation:\r\n- Alice removes all stones, adds 7 + (-6) + 5 + 10 + 5 + (-2) + (-6) = 13 to her score, and places a\r\n stone of value 13 on the left. stones = [13].\r\nThe difference between their scores is 13 - 0 = 13.\r\n\r\n\r\n
Example 3:
\r\n\r\n\r\nInput: stones = [-10,-12]\r\nOutput: -22\r\nExplanation:\r\n- Alice can only make one move, which is to remove both stones. She adds (-10) + (-12) = -22 to her\r\n score and places a stone of value -22 on the left. stones = [-22].\r\nThe difference between their scores is (-22) - 0 = -22.\r\n\r\n\r\n
\r\n
Constraints:
\r\n\r\nn == stones.length
2 <= n <= 105
-104 <= stones[i] <= 104
Alice \u548c Bob \u73a9\u4e00\u4e2a\u6e38\u620f\uff0c\u4e24\u4eba\u8f6e\u6d41\u64cd\u4f5c\uff0c Alice \u5148\u624b\u00a0\u3002
\n\n\u603b\u5171\u6709\u00a0n
\u00a0\u4e2a\u77f3\u5b50\u6392\u6210\u4e00\u884c\u3002\u8f6e\u5230\u67d0\u4e2a\u73a9\u5bb6\u7684\u56de\u5408\u65f6\uff0c\u5982\u679c\u77f3\u5b50\u7684\u6570\u76ee \u5927\u4e8e 1\u00a0\uff0c\u4ed6\u5c06\u6267\u884c\u4ee5\u4e0b\u64cd\u4f5c\uff1a
x > 1
\u00a0\uff0c\u5e76\u4e14 \u79fb\u9664\u00a0\u6700\u5de6\u8fb9\u7684\u00a0x
\u00a0\u4e2a\u77f3\u5b50\u3002\u5f53\u53ea\u5269\u4e0b \u4e00\u4e2a\u00a0\u77f3\u5b50\u65f6\uff0c\u6e38\u620f\u7ed3\u675f\u3002
\n\nAlice \u548c Bob \u7684 \u5206\u6570\u4e4b\u5dee\u00a0\u4e3a\u00a0(Alice \u7684\u5206\u6570\u00a0- Bob \u7684\u5206\u6570)
\u00a0\u3002\u00a0Alice \u7684\u76ee\u6807\u662f\u00a0\u6700\u5927\u5316\u00a0\u5206\u6570\u5dee\uff0cBob \u7684\u76ee\u6807\u662f \u6700\u5c0f\u5316\u00a0\u5206\u6570\u5dee\u3002
\u7ed9\u4f60\u4e00\u4e2a\u957f\u5ea6\u4e3a n
\u00a0\u7684\u6574\u6570\u6570\u7ec4\u00a0stones
\u00a0\uff0c\u5176\u4e2d\u00a0stones[i]
\u00a0\u662f \u4ece\u5de6\u8fb9\u8d77\u00a0\u7b2c\u00a0i
\u00a0\u4e2a\u77f3\u5b50\u7684\u4ef7\u503c\u3002\u8bf7\u4f60\u8fd4\u56de\u5728\u53cc\u65b9\u90fd\u91c7\u7528 \u6700\u4f18 \u7b56\u7565\u7684\u60c5\u51b5\u4e0b\uff0cAlice \u548c Bob \u7684 \u5206\u6570\u4e4b\u5dee \u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1astones = [-1,2,-3,4,-5]\n\u8f93\u51fa\uff1a5\n\u89e3\u91ca\uff1a\n- Alice \u79fb\u9664\u6700\u5de6\u8fb9\u7684 4 \u4e2a\u77f3\u5b50\uff0c\u5f97\u5206\u589e\u52a0 (-1) + 2 + (-3) + 4 = 2 \uff0c\u5e76\u4e14\u5c06\u4e00\u4e2a\u4ef7\u503c\u4e3a 2 \u7684\u77f3\u5b50\u653e\u5728\u6700\u5de6\u8fb9\u3002stones = [2,-5] \u3002\n- Bob \u79fb\u9664\u6700\u5de6\u8fb9\u7684 2 \u4e2a\u77f3\u5b50\uff0c\u5f97\u5206\u589e\u52a0 2 + (-5) = -3 \uff0c\u5e76\u4e14\u5c06\u4e00\u4e2a\u4ef7\u503c\u4e3a -3 \u7684\u77f3\u5b50\u653e\u5728\u6700\u5de6\u8fb9\u3002stones = [-3] \u3002\n\u4e24\u8005\u5206\u6570\u4e4b\u5dee\u4e3a 2 - (-3) = 5 \u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1astones = [7,-6,5,10,5,-2,-6]\n\u8f93\u51fa\uff1a13\n\u89e3\u91ca\uff1a\n- Alice \u79fb\u9664\u6240\u6709\u77f3\u5b50\uff0c\u5f97\u5206\u589e\u52a0 7 + (-6) + 5 + 10 + 5 + (-2) + (-6) = 13 \uff0c\u5e76\u4e14\u5c06\u4e00\u4e2a\u4ef7\u503c\u4e3a 13 \u7684\u77f3\u5b50\u653e\u5728\u6700\u5de6\u8fb9\u3002stones = [13] \u3002\n\u4e24\u8005\u5206\u6570\u4e4b\u5dee\u4e3a 13 - 0 = 13 \u3002\n\n\n
\u793a\u4f8b 3\uff1a
\n\n\u8f93\u5165\uff1astones = [-10,-12]\n\u8f93\u51fa\uff1a-22\n\u89e3\u91ca\uff1a\n- Alice \u53ea\u6709\u4e00\u79cd\u64cd\u4f5c\uff0c\u5c31\u662f\u79fb\u9664\u6240\u6709\u77f3\u5b50\u3002\u5f97\u5206\u589e\u52a0 (-10) + (-12) = -22 \uff0c\u5e76\u4e14\u5c06\u4e00\u4e2a\u4ef7\u503c\u4e3a -22 \u7684\u77f3\u5b50\u653e\u5728\u6700\u5de6\u8fb9\u3002stones = [-22] \u3002\n\u4e24\u8005\u5206\u6570\u4e4b\u5dee\u4e3a (-22) - 0 = -22 \u3002\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\nn == stones.length
2 <= n <= 105
-104 <= stones[i] <= 104
You are given a 0-indexed binary string s
and two integers minJump
and maxJump
. In the beginning, you are standing at index 0
, which is equal to '0'
. You can move from index i
to index j
if the following conditions are fulfilled:
i + minJump <= j <= min(i + maxJump, s.length - 1)
, ands[j] == '0'
.Return true
if you can reach index s.length - 1
in s
, or false
otherwise.
\n
Example 1:
\n\n\nInput: s = "011010", minJump = 2, maxJump = 3\nOutput: true\nExplanation:\nIn the first step, move from index 0 to index 3. \nIn the second step, move from index 3 to index 5.\n\n\n
Example 2:
\n\n\nInput: s = "01101110", minJump = 2, maxJump = 3\nOutput: false\n\n\n
\n
Constraints:
\n\n2 <= s.length <= 105
s[i]
is either '0'
or '1'
.s[0] == '0'
1 <= minJump <= maxJump < s.length
\u7ed9\u4f60\u4e00\u4e2a\u4e0b\u6807\u4ece 0 \u5f00\u59cb\u7684\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u00a0s
\u00a0\u548c\u4e24\u4e2a\u6574\u6570\u00a0minJump
\u548c\u00a0maxJump
\u00a0\u3002\u4e00\u5f00\u59cb\uff0c\u4f60\u5728\u4e0b\u6807\u00a00
\u00a0\u5904\uff0c\u4e14\u8be5\u4f4d\u7f6e\u7684\u503c\u4e00\u5b9a\u4e3a\u00a0'0'
\u00a0\u3002\u5f53\u540c\u65f6\u6ee1\u8db3\u5982\u4e0b\u6761\u4ef6\u65f6\uff0c\u4f60\u53ef\u4ee5\u4ece\u4e0b\u6807\u00a0i
\u00a0\u79fb\u52a8\u5230\u4e0b\u6807\u00a0j
\u00a0\u5904\uff1a
i + minJump <= j <= min(i + maxJump, s.length - 1)
\u00a0\u4e14s[j] == '0'
.\u5982\u679c\u4f60\u53ef\u4ee5\u5230\u8fbe s
\u00a0\u7684\u4e0b\u6807\u00a0s.length - 1
\u00a0\u5904\uff0c\u8bf7\u4f60\u8fd4\u56de\u00a0true
\u00a0\uff0c\u5426\u5219\u8fd4\u56de\u00a0false
\u00a0\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1as = \"011010\", minJump = 2, maxJump = 3\n\u8f93\u51fa\uff1atrue\n\u89e3\u91ca\uff1a\n\u7b2c\u4e00\u6b65\uff0c\u4ece\u4e0b\u6807 0 \u79fb\u52a8\u5230\u4e0b\u6807 3 \u3002\n\u7b2c\u4e8c\u6b65\uff0c\u4ece\u4e0b\u6807 3 \u79fb\u52a8\u5230\u4e0b\u6807 5 \u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1as = \"01101110\", minJump = 2, maxJump = 3\n\u8f93\u51fa\uff1afalse\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n2 <= s.length <= 105
s[i]
\u00a0\u8981\u4e48\u662f\u00a0'0'
\u00a0\uff0c\u8981\u4e48\u662f\u00a0'1'
s[0] == '0'
1 <= minJump <= maxJump < s.length
You are given a floating-point number hour
, representing the amount of time you have to reach the office. To commute to the office, you must take n
trains in sequential order. You are also given an integer array dist
of length n
, where dist[i]
describes the distance (in kilometers) of the ith
train ride.
Each train can only depart at an integer hour, so you may need to wait in between each train ride.
\n\n1st
train ride takes 1.5
hours, you must wait for an additional 0.5
hours before you can depart on the 2nd
train ride at the 2 hour mark.Return the minimum positive integer speed (in kilometers per hour) that all the trains must travel at for you to reach the office on time, or -1
if it is impossible to be on time.
Tests are generated such that the answer will not exceed 107
and hour
will have at most two digits after the decimal point.
\n
Example 1:
\n\n\nInput: dist = [1,3,2], hour = 6\nOutput: 1\nExplanation: At speed 1:\n- The first train ride takes 1/1 = 1 hour.\n- Since we are already at an integer hour, we depart immediately at the 1 hour mark. The second train takes 3/1 = 3 hours.\n- Since we are already at an integer hour, we depart immediately at the 4 hour mark. The third train takes 2/1 = 2 hours.\n- You will arrive at exactly the 6 hour mark.\n\n\n
Example 2:
\n\n\nInput: dist = [1,3,2], hour = 2.7\nOutput: 3\nExplanation: At speed 3:\n- The first train ride takes 1/3 = 0.33333 hours.\n- Since we are not at an integer hour, we wait until the 1 hour mark to depart. The second train ride takes 3/3 = 1 hour.\n- Since we are already at an integer hour, we depart immediately at the 2 hour mark. The third train takes 2/3 = 0.66667 hours.\n- You will arrive at the 2.66667 hour mark.\n\n\n
Example 3:
\n\n\nInput: dist = [1,3,2], hour = 1.9\nOutput: -1\nExplanation: It is impossible because the earliest the third train can depart is at the 2 hour mark.\n\n\n
\n
Constraints:
\n\nn == dist.length
1 <= n <= 105
1 <= dist[i] <= 105
1 <= hour <= 109
hour
.\u7ed9\u4f60\u4e00\u4e2a\u6d6e\u70b9\u6570 hour
\uff0c\u8868\u793a\u4f60\u5230\u8fbe\u529e\u516c\u5ba4\u53ef\u7528\u7684\u603b\u901a\u52e4\u65f6\u95f4\u3002\u8981\u5230\u8fbe\u529e\u516c\u5ba4\uff0c\u4f60\u5fc5\u987b\u6309\u7ed9\u5b9a\u6b21\u5e8f\u4e58\u5750 n
\u8d9f\u5217\u8f66\u3002\u53e6\u7ed9\u4f60\u4e00\u4e2a\u957f\u5ea6\u4e3a n
\u7684\u6574\u6570\u6570\u7ec4 dist
\uff0c\u5176\u4e2d dist[i]
\u8868\u793a\u7b2c i
\u8d9f\u5217\u8f66\u7684\u884c\u9a76\u8ddd\u79bb\uff08\u5355\u4f4d\u662f\u5343\u7c73\uff09\u3002
\u6bcf\u8d9f\u5217\u8f66\u5747\u53ea\u80fd\u5728\u6574\u70b9\u53d1\u8f66\uff0c\u6240\u4ee5\u4f60\u53ef\u80fd\u9700\u8981\u5728\u4e24\u8d9f\u5217\u8f66\u4e4b\u95f4\u7b49\u5f85\u4e00\u6bb5\u65f6\u95f4\u3002
\n\n1
\u8d9f\u5217\u8f66\u9700\u8981 1.5
\u5c0f\u65f6\uff0c\u90a3\u4f60\u5fc5\u987b\u518d\u7b49\u5f85 0.5
\u5c0f\u65f6\uff0c\u642d\u4e58\u5728\u7b2c 2 \u5c0f\u65f6\u53d1\u8f66\u7684\u7b2c 2
\u8d9f\u5217\u8f66\u3002\u8fd4\u56de\u80fd\u6ee1\u8db3\u4f60\u51c6\u65f6\u5230\u8fbe\u529e\u516c\u5ba4\u6240\u8981\u6c42\u5168\u90e8\u5217\u8f66\u7684 \u6700\u5c0f\u6b63\u6574\u6570 \u65f6\u901f\uff08\u5355\u4f4d\uff1a\u5343\u7c73\u6bcf\u5c0f\u65f6\uff09\uff0c\u5982\u679c\u65e0\u6cd5\u51c6\u65f6\u5230\u8fbe\uff0c\u5219\u8fd4\u56de -1
\u3002
\u751f\u6210\u7684\u6d4b\u8bd5\u7528\u4f8b\u4fdd\u8bc1\u7b54\u6848\u4e0d\u8d85\u8fc7 107
\uff0c\u4e14 hour
\u7684 \u5c0f\u6570\u70b9\u540e\u6700\u591a\u5b58\u5728\u4e24\u4f4d\u6570\u5b57 \u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1adist = [1,3,2], hour = 6\n\u8f93\u51fa\uff1a1\n\u89e3\u91ca\uff1a\u901f\u5ea6\u4e3a 1 \u65f6\uff1a\n- \u7b2c 1 \u8d9f\u5217\u8f66\u8fd0\u884c\u9700\u8981 1/1 = 1 \u5c0f\u65f6\u3002\n- \u7531\u4e8e\u662f\u5728\u6574\u6570\u65f6\u95f4\u5230\u8fbe\uff0c\u53ef\u4ee5\u7acb\u5373\u6362\u4e58\u5728\u7b2c 1 \u5c0f\u65f6\u53d1\u8f66\u7684\u5217\u8f66\u3002\u7b2c 2 \u8d9f\u5217\u8f66\u8fd0\u884c\u9700\u8981 3/1 = 3 \u5c0f\u65f6\u3002\n- \u7531\u4e8e\u662f\u5728\u6574\u6570\u65f6\u95f4\u5230\u8fbe\uff0c\u53ef\u4ee5\u7acb\u5373\u6362\u4e58\u5728\u7b2c 4 \u5c0f\u65f6\u53d1\u8f66\u7684\u5217\u8f66\u3002\u7b2c 3 \u8d9f\u5217\u8f66\u8fd0\u884c\u9700\u8981 2/1 = 2 \u5c0f\u65f6\u3002\n- \u4f60\u5c06\u4f1a\u6070\u597d\u5728\u7b2c 6 \u5c0f\u65f6\u5230\u8fbe\u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1adist = [1,3,2], hour = 2.7\n\u8f93\u51fa\uff1a3\n\u89e3\u91ca\uff1a\u901f\u5ea6\u4e3a 3 \u65f6\uff1a\n- \u7b2c 1 \u8d9f\u5217\u8f66\u8fd0\u884c\u9700\u8981 1/3 = 0.33333 \u5c0f\u65f6\u3002\n- \u7531\u4e8e\u4e0d\u662f\u5728\u6574\u6570\u65f6\u95f4\u5230\u8fbe\uff0c\u6545\u9700\u8981\u7b49\u5f85\u81f3\u7b2c 1 \u5c0f\u65f6\u624d\u80fd\u642d\u4e58\u5217\u8f66\u3002\u7b2c 2 \u8d9f\u5217\u8f66\u8fd0\u884c\u9700\u8981 3/3 = 1 \u5c0f\u65f6\u3002\n- \u7531\u4e8e\u662f\u5728\u6574\u6570\u65f6\u95f4\u5230\u8fbe\uff0c\u53ef\u4ee5\u7acb\u5373\u6362\u4e58\u5728\u7b2c 2 \u5c0f\u65f6\u53d1\u8f66\u7684\u5217\u8f66\u3002\u7b2c 3 \u8d9f\u5217\u8f66\u8fd0\u884c\u9700\u8981 2/3 = 0.66667 \u5c0f\u65f6\u3002\n- \u4f60\u5c06\u4f1a\u5728\u7b2c 2.66667 \u5c0f\u65f6\u5230\u8fbe\u3002\n\n
\u793a\u4f8b 3\uff1a
\n\n\n\u8f93\u5165\uff1adist = [1,3,2], hour = 1.9\n\u8f93\u51fa\uff1a-1\n\u89e3\u91ca\uff1a\u4e0d\u53ef\u80fd\u51c6\u65f6\u5230\u8fbe\uff0c\u56e0\u4e3a\u7b2c 3 \u8d9f\u5217\u8f66\u6700\u65e9\u662f\u5728\u7b2c 2 \u5c0f\u65f6\u53d1\u8f66\u3002\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\nn == dist.length
1 <= n <= 105
1 <= dist[i] <= 105
1 <= hour <= 109
hours
\u4e2d\uff0c\u5c0f\u6570\u70b9\u540e\u6700\u591a\u5b58\u5728\u4e24\u4f4d\u6570\u5b57Given a binary string s
, return true
if the longest contiguous segment of 1
s is strictly longer than the longest contiguous segment of 0
s in s
. Return false
otherwise.
s = "110100010"
the longest contiguous segment of 1
s has length 2
, and the longest contiguous segment of 0
s has length 3
.Note that if there are no 0
s, then the longest contiguous segment of 0
s is considered to have length 0
. The same applies if there are no 1
s.
\n
Example 1:
\n\n\nInput: s = "1101"\nOutput: true\nExplanation:\nThe longest contiguous segment of 1s has length 2: "1101"\nThe longest contiguous segment of 0s has length 1: "1101"\nThe segment of 1s is longer, so return true.\n\n\n
Example 2:
\n\n\nInput: s = "111000"\nOutput: false\nExplanation:\nThe longest contiguous segment of 1s has length 3: "111000"\nThe longest contiguous segment of 0s has length 3: "111000"\nThe segment of 1s is not longer, so return false.\n\n\n
Example 3:
\n\n\nInput: s = "110100010"\nOutput: false\nExplanation:\nThe longest contiguous segment of 1s has length 2: "110100010"\nThe longest contiguous segment of 0s has length 3: "110100010"\nThe segment of 1s is not longer, so return false.\n\n\n
\n
Constraints:
\n\n1 <= s.length <= 100
s[i]
is either '0'
or '1'
.\u7ed9\u4f60\u4e00\u4e2a\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32 s
\u3002\u5982\u679c\u5b57\u7b26\u4e32\u4e2d\u7531 1
\u7ec4\u6210\u7684 \u6700\u957f \u8fde\u7eed\u5b50\u5b57\u7b26\u4e32 \u4e25\u683c\u957f\u4e8e \u7531 0
\u7ec4\u6210\u7684 \u6700\u957f \u8fde\u7eed\u5b50\u5b57\u7b26\u4e32\uff0c\u8fd4\u56de true
\uff1b\u5426\u5219\uff0c\u8fd4\u56de false
\u3002
s = \"110100010\"
\u4e2d\uff0c\u7531 1
\u7ec4\u6210\u7684\u6700\u957f\u8fde\u7eed\u5b50\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u662f 2
\uff0c\u7531 0
\u7ec4\u6210\u7684\u6700\u957f\u8fde\u7eed\u5b50\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u662f 3
\u3002\u6ce8\u610f\uff0c\u5982\u679c\u5b57\u7b26\u4e32\u4e2d\u4e0d\u5b58\u5728 0
\uff0c\u6b64\u65f6\u8ba4\u4e3a\u7531 0
\u7ec4\u6210\u7684\u6700\u957f\u8fde\u7eed\u5b50\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u662f 0
\u3002\u5b57\u7b26\u4e32\u4e2d\u4e0d\u5b58\u5728 1
\u7684\u60c5\u51b5\u4e5f\u9002\u7528\u6b64\u89c4\u5219\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1as = \"1101\"\n\u8f93\u51fa\uff1atrue\n\u89e3\u91ca\uff1a\n\u7531\n\n1
\u7ec4\u6210\u7684\u6700\u957f\u8fde\u7eed\u5b50\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u662f 2\uff1a\"1101\"\n\u75310
\u7ec4\u6210\u7684\u6700\u957f\u8fde\u7eed\u5b50\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u662f 1\uff1a\"1101\"\n\u7531 1 \u7ec4\u6210\u7684\u5b50\u5b57\u7b26\u4e32\u66f4\u957f\uff0c\u6545\u8fd4\u56de true \u3002\n
\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1as = \"111000\"\n\u8f93\u51fa\uff1afalse\n\u89e3\u91ca\uff1a\n\u7531\n\n1
\u7ec4\u6210\u7684\u6700\u957f\u8fde\u7eed\u5b50\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u662f 3\uff1a\"111000\"\n\u75310
\u7ec4\u6210\u7684\u6700\u957f\u8fde\u7eed\u5b50\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u662f 3\uff1a\"111000\"\n\u7531 1 \u7ec4\u6210\u7684\u5b50\u5b57\u7b26\u4e32\u4e0d\u6bd4\u7531 0 \u7ec4\u6210\u7684\u5b50\u5b57\u7b26\u4e32\u957f\uff0c\u6545\u8fd4\u56de false \u3002\n
\u793a\u4f8b 3\uff1a
\n\n\n\u8f93\u5165\uff1as = \"110100010\"\n\u8f93\u51fa\uff1afalse\n\u89e3\u91ca\uff1a\n\u7531\n\n1
\u7ec4\u6210\u7684\u6700\u957f\u8fde\u7eed\u5b50\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u662f 2\uff1a\"110100010\"\n\u75310
\u7ec4\u6210\u7684\u6700\u957f\u8fde\u7eed\u5b50\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u662f 3\uff1a\"110100010\"\n\u7531 1 \u7ec4\u6210\u7684\u5b50\u5b57\u7b26\u4e32\u4e0d\u6bd4\u7531 0 \u7ec4\u6210\u7684\u5b50\u5b57\u7b26\u4e32\u957f\uff0c\u6545\u8fd4\u56de false \u3002\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= s.length <= 100
s[i]
\u4e0d\u662f '0'
\u5c31\u662f '1'
Table: Accounts
\r\n+----------------+------+\r\n| Column Name | Type |\r\n+----------------+------+\r\n| account_id | int |\r\n| max_income | int |\r\n+----------------+------+\r\naccount_id is the primary key for this table.\r\nEach row contains information about the maximum monthly income for one bank account.\r\n\r\n\r\n
\r\n\r\n
Table: Transactions
\r\n+----------------+----------+\r\n| Column Name | Type |\r\n+----------------+----------+\r\n| transaction_id | int |\r\n| account_id | int |\r\n| type | ENUM |\r\n| amount | int |\r\n| day | datetime |\r\n+----------------+----------+\r\ntransaction_id is the primary key for this table.\r\nEach row contains information about one transaction.\r\ntype is ENUM ('Creditor','Debtor') where 'Creditor' means the user deposited money into their account and 'Debtor' means the user withdrew money from their account.\r\namount is the amount of money depositied/withdrawn during the transaction.\r\n\r\n\r\n
\r\n\r\n
Write an SQL query to report the IDs of all suspicious bank accounts.
\r\n\r\nA bank account is suspicious if the total income exceeds the max_income
for this account for two or more consecutive months. The total income of an account in some month is the sum of all its deposits in that month (i.e., transactions of the type 'Creditor'
).
Return the result table in ascending order by transaction_id
.
The query result format is in the following example:
\r\n\r\n\r\n\r\n
\r\nAccounts table:\r\n+------------+------------+\r\n| account_id | max_income |\r\n+------------+------------+\r\n| 3 | 21000 |\r\n| 4 | 10400 |\r\n+------------+------------+\r\n\r\nTransactions table:\r\n+----------------+------------+----------+--------+---------------------+\r\n| transaction_id | account_id | type | amount | day |\r\n+----------------+------------+----------+--------+---------------------+\r\n| 2 | 3 | Creditor | 107100 | 2021-06-02 11:38:14 |\r\n| 4 | 4 | Creditor | 10400 | 2021-06-20 12:39:18 |\r\n| 11 | 4 | Debtor | 58800 | 2021-07-23 12:41:55 |\r\n| 1 | 4 | Creditor | 49300 | 2021-05-03 16:11:04 |\r\n| 15 | 3 | Debtor | 75500 | 2021-05-23 14:40:20 |\r\n| 10 | 3 | Creditor | 102100 | 2021-06-15 10:37:16 |\r\n| 14 | 4 | Creditor | 56300 | 2021-07-21 12:12:25 |\r\n| 19 | 4 | Debtor | 101100 | 2021-05-09 15:21:49 |\r\n| 8 | 3 | Creditor | 64900 | 2021-07-26 15:09:56 |\r\n| 7 | 3 | Creditor | 90900 | 2021-06-14 11:23:07 |\r\n+----------------+------------+----------+--------+---------------------+\r\n\r\nResult table:\r\n+------------+\r\n| account_id |\r\n+------------+\r\n| 3 |\r\n+------------+\r\n\r\nFor account 3:\r\n- In 6-2021, the user had an income of 107100 + 102100 + 90900 = 300100.\r\n- In 7-2021, the user had an income of 64900.\r\nWe can see that the income exceeded the max income of 21000 for two consecutive months, so we include 3 in the result table.\r\n\r\nFor account 4:\r\n- In 5-2021, the user had an income of 49300.\r\n- In 6-2021, the user had an income of 10400.\r\n- In 7-2021, the user had an income of 56300.\r\nWe can see that the income exceeded the max income in May and July, but not in June. Since the account did not exceed the max income for two consecutive months, we do not include it in the result table.\r\n", "content_cn": null, "tags_en": [], "tags_cn": [], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "MySQL", "langSlug": "mysql", "code": "# Write your MySQL query statement below", "__typename": "CodeSnippetNode"}, {"lang": "MS SQL Server", "langSlug": "mssql", "code": "/* Write your T-SQL query statement below */", "__typename": "CodeSnippetNode"}, {"lang": "Oracle", "langSlug": "oraclesql", "code": "/* Write your PL/SQL query statement below */", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1843](https://leetcode-cn.com/problems/suspicious-bank-accounts)", "[Suspicious Bank Accounts](/solution/1800-1899/1843.Suspicious%20Bank%20Accounts/README_EN.md)", "", "\u4e2d\u7b49", "\ud83d\udd12"], "md_table_row_en": ["[1843](https://leetcode.com/problems/suspicious-bank-accounts)", "[Suspicious Bank Accounts](/solution/1800-1899/1843.Suspicious%20Bank%20Accounts/README_EN.md)", "", "Medium", "\ud83d\udd12"]}, {"question_id": "1997", "frontend_question_id": "1842", "paid_only": true, "paid_only_cn": true, "url_cn": "https://leetcode-cn.com/problems/next-palindrome-using-same-digits", "url_en": "https://leetcode.com/problems/next-palindrome-using-same-digits", "relative_path_cn": "/solution/1800-1899/1842.Next%20Palindrome%20Using%20Same%20Digits/README.md", "relative_path_en": "/solution/1800-1899/1842.Next%20Palindrome%20Using%20Same%20Digits/README_EN.md", "title_cn": "", "title_en": "Next Palindrome Using Same Digits", "question_title_slug": "next-palindrome-using-same-digits", "content_en": "
You are given a numeric string num
, representing a very large palindrome.
Return the smallest palindrome larger than num
that can be created by rearranging its digits. If no such palindrome exists, return an empty string ""
.
A palindrome is a number that reads the same backward as forward.
\r\n\r\n\r\n
Example 1:
\r\n\r\n\r\nInput: num = "1221"\r\nOutput: "2112"\r\nExplanation: The next palindrome larger than "1221" is "2112".\r\n\r\n\r\n
Example 2:
\r\n\r\n\r\nInput: num = "32123"\r\nOutput: ""\r\nExplanation: No palindromes larger than "32123" can be made by rearranging the digits.\r\n\r\n\r\n
Example 3:
\r\n\r\n\r\nInput: num = "45544554"\r\nOutput: "54455445"\r\nExplanation: The next palindrome larger than "45544554" is "54455445".\r\n\r\n\r\n
\r\n
Constraints:
\r\n\r\n1 <= num.length <= 105
num
is a palindrome.There are n
uniquely-sized sticks whose lengths are integers from 1
to n
. You want to arrange the sticks such that exactly k
sticks are visible from the left. A stick is visible from the left if there are no longer sticks to the left of it.
[1,3,2,5,4]
, then the sticks with lengths 1
, 3
, and 5
are visible from the left.Given n
and k
, return the number of such arrangements. Since the answer may be large, return it modulo 109 + 7
.
\n
Example 1:
\n\n\nInput: n = 3, k = 2\nOutput: 3\nExplanation: [1,3,2], [2,3,1], and [2,1,3] are the only arrangements such that exactly 2 sticks are visible.\nThe visible sticks are underlined.\n\n\n
Example 2:
\n\n\nInput: n = 5, k = 5\nOutput: 1\nExplanation: [1,2,3,4,5] is the only arrangement such that all 5 sticks are visible.\nThe visible sticks are underlined.\n\n\n
Example 3:
\n\n\nInput: n = 20, k = 11\nOutput: 647427950\nExplanation: There are 647427950 (mod 109 + 7) ways to rearrange the sticks such that exactly 11 sticks are visible.\n\n\n
\n
Constraints:
\n\n1 <= n <= 1000
1 <= k <= n
\u6709 n
\u6839\u957f\u5ea6\u4e92\u4e0d\u76f8\u540c\u7684\u6728\u68cd\uff0c\u957f\u5ea6\u4e3a\u4ece 1
\u5230 n
\u7684\u6574\u6570\u3002\u8bf7\u4f60\u5c06\u8fd9\u4e9b\u6728\u68cd\u6392\u6210\u4e00\u6392\uff0c\u5e76\u6ee1\u8db3\u4ece\u5de6\u4fa7 \u53ef\u4ee5\u770b\u5230\u00a0\u6070\u597d k
\u6839\u6728\u68cd\u3002\u4ece\u5de6\u4fa7 \u53ef\u4ee5\u770b\u5230 \u6728\u68cd\u7684\u524d\u63d0\u662f\u8fd9\u4e2a\u6728\u68cd\u7684 \u5de6\u4fa7 \u4e0d\u5b58\u5728\u6bd4\u5b83 \u66f4\u957f\u7684 \u6728\u68cd\u3002
[1,3,2,5,4]
\uff0c\u90a3\u4e48\u4ece\u5de6\u4fa7\u53ef\u4ee5\u770b\u5230\u7684\u5c31\u662f\u957f\u5ea6\u5206\u522b\u4e3a 1
\u30013
\u30015
\u7684\u6728\u68cd\u3002\u7ed9\u4f60 n
\u548c k
\uff0c\u8fd4\u56de\u7b26\u5408\u9898\u76ee\u8981\u6c42\u7684\u6392\u5217 \u6570\u76ee \u3002\u7531\u4e8e\u7b54\u6848\u53ef\u80fd\u5f88\u5927\uff0c\u8bf7\u8fd4\u56de\u5bf9 109 + 7
\u53d6\u4f59 \u7684\u7ed3\u679c\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1an = 3, k = 2\n\u8f93\u51fa\uff1a3\n\u89e3\u91ca\uff1a[1,3,2], [2,3,1] \u548c [2,1,3] \u662f\u4ec5\u6709\u7684\u80fd\u6ee1\u8db3\u6070\u597d 2 \u6839\u6728\u68cd\u53ef\u4ee5\u770b\u5230\u7684\u6392\u5217\u3002\n\u53ef\u4ee5\u770b\u5230\u7684\u6728\u68cd\u5df2\u7ecf\u7528\u7c97\u4f53+\u659c\u4f53\u6807\u8bc6\u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1an = 5, k = 5\n\u8f93\u51fa\uff1a1\n\u89e3\u91ca\uff1a[1,2,3,4,5] \u662f\u552f\u4e00\u4e00\u79cd\u80fd\u6ee1\u8db3\u5168\u90e8 5 \u6839\u6728\u68cd\u53ef\u4ee5\u770b\u5230\u7684\u6392\u5217\u3002\n\u53ef\u4ee5\u770b\u5230\u7684\u6728\u68cd\u5df2\u7ecf\u7528\u7c97\u4f53+\u659c\u4f53\u6807\u8bc6\u3002\n\n\n
\u793a\u4f8b 3\uff1a
\n\n\u8f93\u5165\uff1an = 20, k = 11\n\u8f93\u51fa\uff1a647427950\n\u89e3\u91ca\uff1a\u603b\u5171\u6709 647427950 (mod 109 + 7) \u79cd\u80fd\u6ee1\u8db3\u6070\u597d\u6709 11 \u6839\u6728\u68cd\u53ef\u4ee5\u770b\u5230\u7684\u6392\u5217\u3002\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= n <= 1000
1 <= k <= n
You are given two integer arrays nums1
and nums2
. You are tasked to implement a data structure that supports queries of two types:
nums2
.(i, j)
such that nums1[i] + nums2[j]
equals a given value (0 <= i < nums1.length
and 0 <= j < nums2.length
).Implement the FindSumPairs
class:
FindSumPairs(int[] nums1, int[] nums2)
Initializes the FindSumPairs
object with two integer arrays nums1
and nums2
.void add(int index, int val)
Adds val
to nums2[index]
, i.e., apply nums2[index] += val
.int count(int tot)
Returns the number of pairs (i, j)
such that nums1[i] + nums2[j] == tot
.\n
Example 1:
\n\n\nInput\n["FindSumPairs", "count", "add", "count", "count", "add", "add", "count"]\n[[[1, 1, 2, 2, 2, 3], [1, 4, 5, 2, 5, 4]], [7], [3, 2], [8], [4], [0, 1], [1, 1], [7]]\nOutput\n[null, 8, null, 2, 1, null, null, 11]\n\nExplanation\nFindSumPairs findSumPairs = new FindSumPairs([1, 1, 2, 2, 2, 3], [1, 4, 5, 2, 5, 4]);\nfindSumPairs.count(7); // return 8; pairs (2,2), (3,2), (4,2), (2,4), (3,4), (4,4) make 2 + 5 and pairs (5,1), (5,5) make 3 + 4\nfindSumPairs.add(3, 2); // now nums2 = [1,4,5,4\n\n,5,4
]\nfindSumPairs.count(8); // return 2; pairs (5,2), (5,4) make 3 + 5\nfindSumPairs.count(4); // return 1; pair (5,0) makes 3 + 1\nfindSumPairs.add(0, 1); // now nums2 = [2
,4,5,4,5,4
]\nfindSumPairs.add(1, 1); // now nums2 = [2
,5,5,4,5,4
]\nfindSumPairs.count(7); // return 11; pairs (2,1), (2,2), (2,4), (3,1), (3,2), (3,4), (4,1), (4,2), (4,4) make 2 + 5 and pairs (5,3), (5,5) make 3 + 4\n
\n
Constraints:
\n\n1 <= nums1.length <= 1000
1 <= nums2.length <= 105
1 <= nums1[i] <= 109
1 <= nums2[i] <= 105
0 <= index < nums2.length
1 <= val <= 105
1 <= tot <= 109
1000
calls are made to add
and count
each.\u7ed9\u4f60\u4e24\u4e2a\u6574\u6570\u6570\u7ec4 nums1
\u548c nums2
\uff0c\u8bf7\u4f60\u5b9e\u73b0\u4e00\u4e2a\u652f\u6301\u4e0b\u8ff0\u4e24\u7c7b\u67e5\u8be2\u7684\u6570\u636e\u7ed3\u6784\uff1a
nums2
\u4e2d\u6307\u5b9a\u4e0b\u6807\u5bf9\u5e94\u5143\u7d20\u4e0a\u3002nums1[i] + nums2[j]
\u7b49\u4e8e\u6307\u5b9a\u503c\u7684\u4e0b\u6807\u5bf9 (i, j)
\u6570\u76ee\uff080 <= i < nums1.length
\u4e14 0 <= j < nums2.length
\uff09\u3002\u5b9e\u73b0 FindSumPairs
\u7c7b\uff1a
FindSumPairs(int[] nums1, int[] nums2)
\u4f7f\u7528\u6574\u6570\u6570\u7ec4\u00a0nums1
\u548c nums2
\u521d\u59cb\u5316 FindSumPairs
\u5bf9\u8c61\u3002void add(int index, int val)
\u5c06 val
\u52a0\u5230 nums2[index]
\u4e0a\uff0c\u5373\uff0c\u6267\u884c nums2[index] += val
\u3002int count(int tot)
\u8fd4\u56de\u6ee1\u8db3\u00a0nums1[i] + nums2[j] == tot
\u7684\u4e0b\u6807\u5bf9 (i, j)
\u6570\u76ee\u3002\u00a0
\n\n\u793a\u4f8b\uff1a
\n\n\n\u8f93\u5165\uff1a\n[\"FindSumPairs\", \"count\", \"add\", \"count\", \"count\", \"add\", \"add\", \"count\"]\n[[[1, 1, 2, 2, 2, 3], [1, 4, 5, 2, 5, 4]], [7], [3, 2], [8], [4], [0, 1], [1, 1], [7]]\n\u8f93\u51fa\uff1a\n[null, 8, null, 2, 1, null, null, 11]\n\n\u89e3\u91ca\uff1a\nFindSumPairs findSumPairs = new FindSumPairs([1, 1, 2, 2, 2, 3], [1, 4, 5, 2, 5, 4]);\nfindSumPairs.count(7); // \u8fd4\u56de 8 ; \u4e0b\u6807\u5bf9 (2,2), (3,2), (4,2), (2,4), (3,4), (4,4) \u6ee1\u8db3 2 + 5 = 7 \uff0c\u4e0b\u6807\u5bf9 (5,1), (5,5) \u6ee1\u8db3 3 + 4 = 7\nfindSumPairs.add(3, 2); // \u6b64\u65f6 nums2 = [1,4,5,4\n\n,5,4
]\nfindSumPairs.count(8); // \u8fd4\u56de 2 \uff1b\u4e0b\u6807\u5bf9 (5,2), (5,4) \u6ee1\u8db3 3 + 5 = 8\nfindSumPairs.count(4); // \u8fd4\u56de 1 \uff1b\u4e0b\u6807\u5bf9 (5,0) \u6ee1\u8db3 3 + 1 = 4\nfindSumPairs.add(0, 1); // \u6b64\u65f6 nums2 = [2
,4,5,4,5,4
]\nfindSumPairs.add(1, 1); // \u6b64\u65f6 nums2 = [2
,5,5,4,5,4
]\nfindSumPairs.count(7); // \u8fd4\u56de 11 \uff1b\u4e0b\u6807\u5bf9 (2,1), (2,2), (2,4), (3,1), (3,2), (3,4), (4,1), (4,2), (4,4) \u6ee1\u8db3 2 + 5 = 7 \uff0c\u4e0b\u6807\u5bf9 (5,3), (5,5) \u6ee1\u8db3 3 + 4 = 7\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= nums1.length <= 1000
1 <= nums2.length <= 105
1 <= nums1[i] <= 109
1 <= nums2[i] <= 105
0 <= index < nums2.length
1 <= val <= 105
1 <= tot <= 109
add
\u548c count
\u51fd\u6570\u5404 1000
\u6b21Given a binary string s
, return the minimum number of character swaps to make it alternating, or -1
if it is impossible.
The string is called alternating if no two adjacent characters are equal. For example, the strings "010"
and "1010"
are alternating, while the string "0100"
is not.
Any two characters may be swapped, even if they are not adjacent.
\n\n\n
Example 1:
\n\n\nInput: s = "111000"\nOutput: 1\nExplanation: Swap positions 1 and 4: "111000" -> "101010"\nThe string is now alternating.\n\n\n
Example 2:
\n\n\nInput: s = "010"\nOutput: 0\nExplanation: The string is already alternating, no swaps are needed.\n\n\n
Example 3:
\n\n\nInput: s = "1110"\nOutput: -1\n\n\n
\n
Constraints:
\n\n1 <= s.length <= 1000
s[i]
is either '0'
or '1'
.\u7ed9\u4f60\u4e00\u4e2a\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32 s
\uff0c\u73b0\u9700\u8981\u5c06\u5176\u8f6c\u5316\u4e3a\u4e00\u4e2a \u4ea4\u66ff\u5b57\u7b26\u4e32 \u3002\u8bf7\u4f60\u8ba1\u7b97\u5e76\u8fd4\u56de\u8f6c\u5316\u6240\u9700\u7684 \u6700\u5c0f \u5b57\u7b26\u4ea4\u6362\u6b21\u6570\uff0c\u5982\u679c\u65e0\u6cd5\u5b8c\u6210\u8f6c\u5316\uff0c\u8fd4\u56de -1
\u3002
\u4ea4\u66ff\u5b57\u7b26\u4e32 \u662f\u6307\uff1a\u76f8\u90bb\u5b57\u7b26\u4e4b\u95f4\u4e0d\u5b58\u5728\u76f8\u7b49\u60c5\u51b5\u7684\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff0c\u5b57\u7b26\u4e32 \"010\"
\u548c \"1010\"
\u5c5e\u4e8e\u4ea4\u66ff\u5b57\u7b26\u4e32\uff0c\u4f46 \"0100\"
\u4e0d\u662f\u3002
\u4efb\u610f\u4e24\u4e2a\u5b57\u7b26\u90fd\u53ef\u4ee5\u8fdb\u884c\u4ea4\u6362\uff0c\u4e0d\u5fc5\u76f8\u90bb \u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1as = \"111000\"\n\u8f93\u51fa\uff1a1\n\u89e3\u91ca\uff1a\u4ea4\u6362\u4f4d\u7f6e 1 \u548c 4\uff1a\"111000\" -> \"101010\" \uff0c\u5b57\u7b26\u4e32\u53d8\u4e3a\u4ea4\u66ff\u5b57\u7b26\u4e32\u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1as = \"010\"\n\u8f93\u51fa\uff1a0\n\u89e3\u91ca\uff1a\u5b57\u7b26\u4e32\u5df2\u7ecf\u662f\u4ea4\u66ff\u5b57\u7b26\u4e32\u4e86\uff0c\u4e0d\u9700\u8981\u4ea4\u6362\u3002\n\n\n
\u793a\u4f8b 3\uff1a
\n\n\n\u8f93\u5165\uff1as = \"1110\"\n\u8f93\u51fa\uff1a-1\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= s.length <= 1000
s[i]
\u7684\u503c\u4e3a '0'
\u6216 '1'
The XOR total of an array is defined as the bitwise XOR
of all its elements, or 0
if the array is empty.
[2,5,6]
is 2 XOR 5 XOR 6 = 1
.Given an array nums
, return the sum of all XOR totals for every subset of nums
.
Note: Subsets with the same elements should be counted multiple times.
\n\nAn array a
is a subset of an array b
if a
can be obtained from b
by deleting some (possibly zero) elements of b
.
\n
Example 1:
\n\n\nInput: nums = [1,3]\nOutput: 6\nExplanation: The 4 subsets of [1,3] are:\n- The empty subset has an XOR total of 0.\n- [1] has an XOR total of 1.\n- [3] has an XOR total of 3.\n- [1,3] has an XOR total of 1 XOR 3 = 2.\n0 + 1 + 3 + 2 = 6\n\n\n
Example 2:
\n\n\nInput: nums = [5,1,6]\nOutput: 28\nExplanation: The 8 subsets of [5,1,6] are:\n- The empty subset has an XOR total of 0.\n- [5] has an XOR total of 5.\n- [1] has an XOR total of 1.\n- [6] has an XOR total of 6.\n- [5,1] has an XOR total of 5 XOR 1 = 4.\n- [5,6] has an XOR total of 5 XOR 6 = 3.\n- [1,6] has an XOR total of 1 XOR 6 = 7.\n- [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2.\n0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28\n\n\n
Example 3:
\n\n\nInput: nums = [3,4,5,6,7,8]\nOutput: 480\nExplanation: The sum of all XOR totals for every subset is 480.\n\n\n
\n
Constraints:
\n\n1 <= nums.length <= 12
1 <= nums[i] <= 20
\u4e00\u4e2a\u6570\u7ec4\u7684 \u5f02\u6216\u603b\u548c \u5b9a\u4e49\u4e3a\u6570\u7ec4\u4e2d\u6240\u6709\u5143\u7d20\u6309\u4f4d XOR
\u7684\u7ed3\u679c\uff1b\u5982\u679c\u6570\u7ec4\u4e3a \u7a7a \uff0c\u5219\u5f02\u6216\u603b\u548c\u4e3a 0
\u3002
[2,5,6]
\u7684 \u5f02\u6216\u603b\u548c \u4e3a 2 XOR 5 XOR 6 = 1
\u3002\u7ed9\u4f60\u4e00\u4e2a\u6570\u7ec4 nums
\uff0c\u8bf7\u4f60\u6c42\u51fa nums
\u4e2d\u6bcf\u4e2a \u5b50\u96c6 \u7684 \u5f02\u6216\u603b\u548c \uff0c\u8ba1\u7b97\u5e76\u8fd4\u56de\u8fd9\u4e9b\u503c\u76f8\u52a0\u4e4b \u548c \u3002
\u6ce8\u610f\uff1a\u5728\u672c\u9898\u4e2d\uff0c\u5143\u7d20 \u76f8\u540c \u7684\u4e0d\u540c\u5b50\u96c6\u5e94 \u591a\u6b21 \u8ba1\u6570\u3002
\n\n\u6570\u7ec4 a
\u662f\u6570\u7ec4 b
\u7684\u4e00\u4e2a \u5b50\u96c6 \u7684\u524d\u63d0\u6761\u4ef6\u662f\uff1a\u4ece b
\u5220\u9664\u51e0\u4e2a\uff08\u4e5f\u53ef\u80fd\u4e0d\u5220\u9664\uff09\u5143\u7d20\u80fd\u591f\u5f97\u5230 a
\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1anums = [1,3]\n\u8f93\u51fa\uff1a6\n\u89e3\u91ca\uff1a[1,3] \u5171\u6709 4 \u4e2a\u5b50\u96c6\uff1a\n- \u7a7a\u5b50\u96c6\u7684\u5f02\u6216\u603b\u548c\u662f 0 \u3002\n- [1] \u7684\u5f02\u6216\u603b\u548c\u4e3a 1 \u3002\n- [3] \u7684\u5f02\u6216\u603b\u548c\u4e3a 3 \u3002\n- [1,3] \u7684\u5f02\u6216\u603b\u548c\u4e3a 1 XOR 3 = 2 \u3002\n0 + 1 + 3 + 2 = 6\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1anums = [5,1,6]\n\u8f93\u51fa\uff1a28\n\u89e3\u91ca\uff1a[5,1,6] \u5171\u6709 8 \u4e2a\u5b50\u96c6\uff1a\n- \u7a7a\u5b50\u96c6\u7684\u5f02\u6216\u603b\u548c\u662f 0 \u3002\n- [5] \u7684\u5f02\u6216\u603b\u548c\u4e3a 5 \u3002\n- [1] \u7684\u5f02\u6216\u603b\u548c\u4e3a 1 \u3002\n- [6] \u7684\u5f02\u6216\u603b\u548c\u4e3a 6 \u3002\n- [5,1] \u7684\u5f02\u6216\u603b\u548c\u4e3a 5 XOR 1 = 4 \u3002\n- [5,6] \u7684\u5f02\u6216\u603b\u548c\u4e3a 5 XOR 6 = 3 \u3002\n- [1,6] \u7684\u5f02\u6216\u603b\u548c\u4e3a 1 XOR 6 = 7 \u3002\n- [5,1,6] \u7684\u5f02\u6216\u603b\u548c\u4e3a 5 XOR 1 XOR 6 = 2 \u3002\n0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28\n\n\n
\u793a\u4f8b 3\uff1a
\n\n\u8f93\u5165\uff1anums = [3,4,5,6,7,8]\n\u8f93\u51fa\uff1a480\n\u89e3\u91ca\uff1a\u6bcf\u4e2a\u5b50\u96c6\u7684\u5168\u90e8\u5f02\u6216\u603b\u548c\u503c\u4e4b\u548c\u4e3a 480 \u3002\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= nums.length <= 12
1 <= nums[i] <= 20
Table: Teams
\r\n+----------------+---------+\r\n| Column Name | Type |\r\n+----------------+---------+\r\n| team_id | int |\r\n| team_name | varchar |\r\n+----------------+---------+\r\nteam_id is the primary key for this table.\r\nEach row contains information about one team in the league.\r\n\r\n\r\n
\r\n\r\n
Table: Matches
\r\n+-----------------+---------+\r\n| Column Name | Type |\r\n+-----------------+---------+\r\n| home_team_id | int |\r\n| away_team_id | int |\r\n| home_team_goals | int |\r\n| away_team_goals | int |\r\n+-----------------+---------+\r\n(home_team_id, away_team_id) is the primary key for this table.\r\nEach row contains information about one match.\r\nhome_team_goals is the number of goals scored by the home team.\r\naway_team_goals is the number of goals scored by the away team.\r\nThe winner of the match is the team with the higher number of goals.\r\n\r\n\r\n
\r\n\r\n
Write an SQL query to report the statistics of the league. The statistics should be built using the played matches where the winning team gets three points and the losing team gets no points. If a match ends with a draw, both teams get one point.
\r\n\r\nEach row of the result table should contain:
\r\n\r\nteam_name
- The name of the team in the Teams
table.matches_played
- The number of matches played as either a home or away team.points
- The total points the team has so far.goal_for
- The total number of goals scored by the team across all matches.goal_against
- The total number of goals scored by opponent teams against this team across all matches.goal_diff
- The result of goal_for - goal_against
.Return the result table in descending order by points
. If two or more teams have the same points
, order them in descending order by goal_diff
. If there is still a tie, order them by team_name
in lexicographical order.
The query result format is in the following example:
\r\n\r\n\r\n\r\n
\r\nTeams table:\r\n+---------+-----------+\r\n| team_id | team_name |\r\n+---------+-----------+\r\n| 1 | Ajax |\r\n| 4 | Dortmund |\r\n| 6 | Arsenal |\r\n+---------+-----------+\r\n\r\nMatches table:\r\n+--------------+--------------+-----------------+-----------------+\r\n| home_team_id | away_team_id | home_team_goals | away_team_goals |\r\n+--------------+--------------+-----------------+-----------------+\r\n| 1 | 4 | 0 | 1 |\r\n| 1 | 6 | 3 | 3 |\r\n| 4 | 1 | 5 | 2 |\r\n| 6 | 1 | 0 | 0 |\r\n+--------------+--------------+-----------------+-----------------+\r\n\r\n\r\nResult table:\r\n+-----------+----------------+--------+----------+--------------+-----------+\r\n| team_name | matches_played | points | goal_for | goal_against | goal_diff |\r\n+-----------+----------------+--------+----------+--------------+-----------+\r\n| Dortmund | 2 | 6 | 6 | 2 | 4 |\r\n| Arsenal | 2 | 2 | 3 | 3 | 0 |\r\n| Ajax | 4 | 2 | 5 | 9 | -4 |\r\n+-----------+----------------+--------+----------+--------------+-----------+\r\n\r\nAjax (team_id=1) played 4 matches: 2 losses and 2 draws. Total points = 0 + 0 + 1 + 1 = 2.\r\nDortmund (team_id=4) played 2 matches: 2 wins. Total points = 3 + 3 = 6.\r\nArsenal (team_id=6) played 2 matches: 2 draws. Total points = 1 + 1 = 2.\r\nDortmund is the first team in the table. Ajax and Arsenal have the same points, but since Arsenal has a higher goal_diff than Ajax, Arsenal comes before Ajax in the table.\r\n", "content_cn": null, "tags_en": [], "tags_cn": [], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "MySQL", "langSlug": "mysql", "code": "# Write your MySQL query statement below", "__typename": "CodeSnippetNode"}, {"lang": "MS SQL Server", "langSlug": "mssql", "code": "/* Write your T-SQL query statement below */", "__typename": "CodeSnippetNode"}, {"lang": "Oracle", "langSlug": "oraclesql", "code": "/* Write your PL/SQL query statement below */", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1841](https://leetcode-cn.com/problems/league-statistics)", "[League Statistics](/solution/1800-1899/1841.League%20Statistics/README_EN.md)", "", "\u4e2d\u7b49", "\ud83d\udd12"], "md_table_row_en": ["[1841](https://leetcode.com/problems/league-statistics)", "[League Statistics](/solution/1800-1899/1841.League%20Statistics/README_EN.md)", "", "Medium", "\ud83d\udd12"]}, {"question_id": "1990", "frontend_question_id": "1878", "paid_only": false, "paid_only_cn": false, "url_cn": "https://leetcode-cn.com/problems/get-biggest-three-rhombus-sums-in-a-grid", "url_en": "https://leetcode.com/problems/get-biggest-three-rhombus-sums-in-a-grid", "relative_path_cn": "/solution/1800-1899/1878.Get%20Biggest%20Three%20Rhombus%20Sums%20in%20a%20Grid/README.md", "relative_path_en": "/solution/1800-1899/1878.Get%20Biggest%20Three%20Rhombus%20Sums%20in%20a%20Grid/README_EN.md", "title_cn": "\u77e9\u9635\u4e2d\u6700\u5927\u7684\u4e09\u4e2a\u83f1\u5f62\u548c", "title_en": "Get Biggest Three Rhombus Sums in a Grid", "question_title_slug": "get-biggest-three-rhombus-sums-in-a-grid", "content_en": "
You are given an m x n
integer matrix grid
\u200b\u200b\u200b.
A rhombus sum is the sum of the elements that form the border of a regular rhombus shape in grid
\u200b\u200b\u200b. The rhombus must have the shape of a square rotated 45 degrees with each of the corners centered in a grid cell. Below is an image of four valid rhombus shapes with the corresponding colored cells that should be included in each rhombus sum:
Note that the rhombus can have an area of 0, which is depicted by the purple rhombus in the bottom right corner.
\n\nReturn the biggest three distinct rhombus sums in the grid
in descending order. If there are less than three distinct values, return all of them.
\n
Example 1:
\n\nInput: grid = [[3,4,5,1,3],[3,3,4,2,3],[20,30,200,40,10],[1,5,5,4,1],[4,3,2,2,5]]\nOutput: [228,216,211]\nExplanation: The rhombus shapes for the three biggest distinct rhombus sums are depicted above.\n- Blue: 20 + 3 + 200 + 5 = 228\n- Red: 200 + 2 + 10 + 4 = 216\n- Green: 5 + 200 + 4 + 2 = 211\n\n\n
Example 2:
\n\nInput: grid = [[1,2,3],[4,5,6],[7,8,9]]\nOutput: [20,9,8]\nExplanation: The rhombus shapes for the three biggest distinct rhombus sums are depicted above.\n- Blue: 4 + 2 + 6 + 8 = 20\n- Red: 9 (area 0 rhombus in the bottom right corner)\n- Green: 8 (area 0 rhombus in the bottom middle)\n\n\n
Example 3:
\n\n\nInput: grid = [[7,7,7]]\nOutput: [7]\nExplanation: All three possible rhombus sums are the same, so return [7].\n\n\n
\n
Constraints:
\n\nm == grid.length
n == grid[i].length
1 <= m, n <= 50
1 <= grid[i][j] <= 105
\u7ed9\u4f60\u4e00\u4e2a\u00a0m x n
\u00a0\u7684\u6574\u6570\u77e9\u9635\u00a0grid
\u00a0\u3002
\u83f1\u5f62\u548c \u6307\u7684\u662f grid
\u00a0\u4e2d\u4e00\u4e2a\u6b63\u83f1\u5f62 \u8fb9\u754c\u00a0\u4e0a\u7684\u5143\u7d20\u4e4b\u548c\u3002\u672c\u9898\u4e2d\u7684\u83f1\u5f62\u5fc5\u987b\u4e3a\u6b63\u65b9\u5f62\u65cb\u8f6c45\u5ea6\uff0c\u4e14\u56db\u4e2a\u89d2\u90fd\u5728\u4e00\u4e2a\u683c\u5b50\u5f53\u4e2d\u3002\u4e0b\u56fe\u662f\u56db\u4e2a\u53ef\u884c\u7684\u83f1\u5f62\uff0c\u6bcf\u4e2a\u83f1\u5f62\u548c\u5e94\u8be5\u5305\u542b\u7684\u683c\u5b50\u90fd\u7528\u4e86\u76f8\u5e94\u989c\u8272\u6807\u6ce8\u5728\u56fe\u4e2d\u3002
\u00a0
\n\n\u6ce8\u610f\uff0c\u83f1\u5f62\u53ef\u4ee5\u662f\u4e00\u4e2a\u9762\u79ef\u4e3a 0 \u7684\u533a\u57df\uff0c\u5982\u4e0a\u56fe\u4e2d\u53f3\u4e0b\u89d2\u7684\u7d2b\u8272\u83f1\u5f62\u6240\u793a\u3002
\n\n\u8bf7\u4f60\u6309\u7167 \u964d\u5e8f\u00a0\u8fd4\u56de grid
\u00a0\u4e2d\u4e09\u4e2a\u6700\u5927\u7684\u00a0\u4e92\u4e0d\u76f8\u540c\u7684\u83f1\u5f62\u548c\u00a0\u3002\u5982\u679c\u4e0d\u540c\u7684\u548c\u5c11\u4e8e\u4e09\u4e2a\uff0c\u5219\u5c06\u5b83\u4eec\u5168\u90e8\u8fd4\u56de\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1agrid = [[3,4,5,1,3],[3,3,4,2,3],[20,30,200,40,10],[1,5,5,4,1],[4,3,2,2,5]]\n\u8f93\u51fa\uff1a[228,216,211]\n\u89e3\u91ca\uff1a\u6700\u5927\u7684\u4e09\u4e2a\u83f1\u5f62\u548c\u5982\u4e0a\u56fe\u6240\u793a\u3002\n- \u84dd\u8272\uff1a20 + 3 + 200 + 5 = 228\n- \u7ea2\u8272\uff1a200 + 2 + 10 + 4 = 216\n- \u7eff\u8272\uff1a5 + 200 + 4 + 2 = 211\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1agrid = [[1,2,3],[4,5,6],[7,8,9]]\n\u8f93\u51fa\uff1a[20,9,8]\n\u89e3\u91ca\uff1a\u6700\u5927\u7684\u4e09\u4e2a\u83f1\u5f62\u548c\u5982\u4e0a\u56fe\u6240\u793a\u3002\n- \u84dd\u8272\uff1a4 + 2 + 6 + 8 = 20\n- \u7ea2\u8272\uff1a9 \uff08\u53f3\u4e0b\u89d2\u7ea2\u8272\u7684\u9762\u79ef\u4e3a 0 \u7684\u83f1\u5f62\uff09\n- \u7eff\u8272\uff1a8 \uff08\u4e0b\u65b9\u4e2d\u592e\u9762\u79ef\u4e3a 0 \u7684\u83f1\u5f62\uff09\n\n\n
\u793a\u4f8b 3\uff1a
\n\n\n\u8f93\u5165\uff1agrid = [[7,7,7]]\n\u8f93\u51fa\uff1a[7]\n\u89e3\u91ca\uff1a\u6240\u6709\u4e09\u4e2a\u53ef\u80fd\u7684\u83f1\u5f62\u548c\u90fd\u76f8\u540c\uff0c\u6240\u4ee5\u8fd4\u56de [7] \u3002\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\nm == grid.length
n == grid[i].length
1 <= m, n <= 100
1 <= grid[i][j] <= 105
You are given two integer arrays nums1
and nums2
of length n
.
The XOR sum of the two integer arrays is (nums1[0] XOR nums2[0]) + (nums1[1] XOR nums2[1]) + ... + (nums1[n - 1] XOR nums2[n - 1])
(0-indexed).
[1,2,3]
and [3,2,1]
is equal to (1 XOR 3) + (2 XOR 2) + (3 XOR 1) = 2 + 0 + 2 = 4
.Rearrange the elements of nums2
such that the resulting XOR sum is minimized.
Return the XOR sum after the rearrangement.
\n\n\n
Example 1:
\n\n\nInput: nums1 = [1,2], nums2 = [2,3]\nOutput: 2\nExplanation: Rearrange\n\nnums2
so that it becomes[3,2]
.\nThe XOR sum is (1 XOR 3) + (2 XOR 2) = 2 + 0 = 2.
Example 2:
\n\n\nInput: nums1 = [1,0,3], nums2 = [5,3,4]\nOutput: 8\nExplanation: Rearrange\n\nnums2
so that it becomes[5,4,3]
. \nThe XOR sum is (1 XOR 5) + (0 XOR 4) + (3 XOR 3) = 4 + 4 + 0 = 8.\n
\n
Constraints:
\n\nn == nums1.length
n == nums2.length
1 <= n <= 14
0 <= nums1[i], nums2[i] <= 107
\u7ed9\u4f60\u4e24\u4e2a\u6574\u6570\u6570\u7ec4\u00a0nums1
\u548c\u00a0nums2
\u00a0\uff0c\u5b83\u4eec\u957f\u5ea6\u90fd\u4e3a\u00a0n
\u00a0\u3002
\u4e24\u4e2a\u6570\u7ec4\u7684 \u5f02\u6216\u503c\u4e4b\u548c\u00a0\u4e3a\u00a0(nums1[0] XOR nums2[0]) + (nums1[1] XOR nums2[1]) + ... + (nums1[n - 1] XOR nums2[n - 1])
\u00a0\uff08\u4e0b\u6807\u4ece 0 \u5f00\u59cb\uff09\u3002
[1,2,3]
\u548c\u00a0[3,2,1]
\u00a0\u7684 \u5f02\u6216\u503c\u4e4b\u548c\u00a0\u7b49\u4e8e\u00a0(1 XOR 3) + (2 XOR 2) + (3 XOR 1) = 2 + 0 + 2 = 4
\u00a0\u3002\u8bf7\u4f60\u5c06\u00a0nums2
\u00a0\u4e2d\u7684\u5143\u7d20\u91cd\u65b0\u6392\u5217\uff0c\u4f7f\u5f97 \u5f02\u6216\u503c\u4e4b\u548c\u00a0\u6700\u5c0f\u00a0\u3002
\u8bf7\u4f60\u8fd4\u56de\u91cd\u65b0\u6392\u5217\u4e4b\u540e\u7684 \u5f02\u6216\u503c\u4e4b\u548c\u00a0\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1anums1 = [1,2], nums2 = [2,3]\n\u8f93\u51fa\uff1a2\n\u89e3\u91ca\uff1a\u5c06\n\nnums2
\u91cd\u65b0\u6392\u5217\u5f97\u5230[3,2] \u3002
\n\u5f02\u6216\u503c\u4e4b\u548c\u4e3a (1 XOR 3) + (2 XOR 2) = 2 + 0 = 2 \u3002
\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1anums1 = [1,0,3], nums2 = [5,3,4]\n\u8f93\u51fa\uff1a8\n\u89e3\u91ca\uff1a\u5c06\n\nnums2 \u91cd\u65b0\u6392\u5217\u5f97\u5230
[5,4,3] \u3002
\n\u5f02\u6216\u503c\u4e4b\u548c\u4e3a (1 XOR 5) + (0 XOR 4) + (3 XOR 3) = 4 + 4 + 0 = 8 \u3002\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\nn == nums1.length
n == nums2.length
1 <= n <= 14
0 <= nums1[i], nums2[i] <= 107
The pair sum of a pair (a,b)
is equal to a + b
. The maximum pair sum is the largest pair sum in a list of pairs.
(1,5)
, (2,3)
, and (4,4)
, the maximum pair sum would be max(1+5, 2+3, 4+4) = max(6, 5, 8) = 8
.Given an array nums
of even length n
, pair up the elements of nums
into n / 2
pairs such that:
nums
is in exactly one pair, andReturn the minimized maximum pair sum after optimally pairing up the elements.
\r\n\r\n\r\n
Example 1:
\r\n\r\n\r\nInput: nums = [3,5,2,3]\r\nOutput: 7\r\nExplanation: The elements can be paired up into pairs (3,3) and (5,2).\r\nThe maximum pair sum is max(3+3, 5+2) = max(6, 7) = 7.\r\n\r\n\r\n
Example 2:
\r\n\r\n\r\nInput: nums = [3,5,4,2,4,6]\r\nOutput: 8\r\nExplanation: The elements can be paired up into pairs (3,5), (4,4), and (6,2).\r\nThe maximum pair sum is max(3+5, 4+4, 6+2) = max(8, 8, 8) = 8.\r\n\r\n\r\n
\r\n
Constraints:
\r\n\r\nn == nums.length
2 <= n <= 105
n
is even.1 <= nums[i] <= 105
\u4e00\u4e2a\u6570\u5bf9\u00a0(a,b)
\u00a0\u7684 \u6570\u5bf9\u548c\u00a0\u7b49\u4e8e\u00a0a + b
\u00a0\u3002\u6700\u5927\u6570\u5bf9\u548c\u00a0\u662f\u4e00\u4e2a\u6570\u5bf9\u6570\u7ec4\u4e2d\u6700\u5927\u7684\u00a0\u6570\u5bf9\u548c\u00a0\u3002
(1,5)
\u00a0\uff0c(2,3)
\u00a0\u548c\u00a0(4,4)
\uff0c\u6700\u5927\u6570\u5bf9\u548c\u00a0\u4e3a\u00a0max(1+5, 2+3, 4+4) = max(6, 5, 8) = 8
\u00a0\u3002\u7ed9\u4f60\u4e00\u4e2a\u957f\u5ea6\u4e3a \u5076\u6570\u00a0n
\u00a0\u7684\u6570\u7ec4\u00a0nums
\u00a0\uff0c\u8bf7\u4f60\u5c06 nums
\u00a0\u4e2d\u7684\u5143\u7d20\u5206\u6210 n / 2
\u00a0\u4e2a\u6570\u5bf9\uff0c\u4f7f\u5f97\uff1a
nums
\u00a0\u4e2d\u6bcf\u4e2a\u5143\u7d20\u00a0\u6070\u597d\u00a0\u5728 \u4e00\u4e2a\u00a0\u6570\u5bf9\u4e2d\uff0c\u4e14\u8bf7\u4f60\u5728\u6700\u4f18\u6570\u5bf9\u5212\u5206\u7684\u65b9\u6848\u4e0b\uff0c\u8fd4\u56de\u6700\u5c0f\u7684 \u6700\u5927\u6570\u5bf9\u548c\u00a0\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1anums = [3,5,2,3]\n\u8f93\u51fa\uff1a7\n\u89e3\u91ca\uff1a\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u53ef\u4ee5\u5206\u4e3a\u6570\u5bf9 (3,3) \u548c (5,2) \u3002\n\u6700\u5927\u6570\u5bf9\u548c\u4e3a max(3+3, 5+2) = max(6, 7) = 7 \u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1anums = [3,5,4,2,4,6]\n\u8f93\u51fa\uff1a8\n\u89e3\u91ca\uff1a\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u53ef\u4ee5\u5206\u4e3a\u6570\u5bf9 (3,5)\uff0c(4,4) \u548c (6,2) \u3002\n\u6700\u5927\u6570\u5bf9\u548c\u4e3a max(3+5, 4+4, 6+2) = max(8, 8, 8) = 8 \u3002\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\nn == nums.length
2 <= n <= 105
n
\u00a0\u662f \u5076\u6570\u00a0\u30021 <= nums[i] <= 105
A string is good if there are no repeated characters.
\n\nGiven a string s
\u200b\u200b\u200b\u200b\u200b, return the number of good substrings of length three in s
\u200b\u200b\u200b\u200b\u200b\u200b.
Note that if there are multiple occurrences of the same substring, every occurrence should be counted.
\n\nA substring is a contiguous sequence of characters in a string.
\n\n\n
Example 1:
\n\n\nInput: s = "xyzzaz"\nOutput: 1\nExplanation: There are 4 substrings of size 3: "xyz", "yzz", "zza", and "zaz". \nThe only good substring of length 3 is "xyz".\n\n\n
Example 2:
\n\n\nInput: s = "aababcabc"\nOutput: 4\nExplanation: There are 7 substrings of size 3: "aab", "aba", "bab", "abc", "bca", "cab", and "abc".\nThe good substrings are "abc", "bca", "cab", and "abc".\n\n\n
\n
Constraints:
\n\n1 <= s.length <= 100
s
\u200b\u200b\u200b\u200b\u200b\u200b consists of lowercase English letters.\u5982\u679c\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e0d\u542b\u6709\u4efb\u4f55\u91cd\u590d\u5b57\u7b26\uff0c\u6211\u4eec\u79f0\u8fd9\u4e2a\u5b57\u7b26\u4e32\u4e3a \u597d\u00a0\u5b57\u7b26\u4e32\u3002
\n\n\u7ed9\u4f60\u4e00\u4e2a\u5b57\u7b26\u4e32 s
\u00a0\uff0c\u8bf7\u4f60\u8fd4\u56de s
\u00a0\u4e2d\u957f\u5ea6\u4e3a 3\u00a0\u7684 \u597d\u5b50\u5b57\u7b26\u4e32 \u7684\u6570\u91cf\u3002
\u6ce8\u610f\uff0c\u5982\u679c\u76f8\u540c\u7684\u597d\u5b50\u5b57\u7b26\u4e32\u51fa\u73b0\u591a\u6b21\uff0c\u6bcf\u4e00\u6b21\u90fd\u5e94\u8be5\u88ab\u8bb0\u5165\u7b54\u6848\u4e4b\u4e2d\u3002
\n\n\u5b50\u5b57\u7b26\u4e32 \u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u8fde\u7eed\u7684\u5b57\u7b26\u5e8f\u5217\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1as = \"xyzzaz\"\n\u8f93\u51fa\uff1a1\n\u89e3\u91ca\uff1a\u603b\u5171\u6709 4 \u4e2a\u957f\u5ea6\u4e3a 3 \u7684\u5b50\u5b57\u7b26\u4e32\uff1a\"xyz\"\uff0c\"yzz\"\uff0c\"zza\" \u548c \"zaz\" \u3002\n\u552f\u4e00\u7684\u957f\u5ea6\u4e3a 3 \u7684\u597d\u5b50\u5b57\u7b26\u4e32\u662f \"xyz\" \u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1as = \"aababcabc\"\n\u8f93\u51fa\uff1a4\n\u89e3\u91ca\uff1a\u603b\u5171\u6709 7 \u4e2a\u957f\u5ea6\u4e3a 3 \u7684\u5b50\u5b57\u7b26\u4e32\uff1a\"aab\"\uff0c\"aba\"\uff0c\"bab\"\uff0c\"abc\"\uff0c\"bca\"\uff0c\"cab\" \u548c \"abc\" \u3002\n\u597d\u5b50\u5b57\u7b26\u4e32\u5305\u62ec \"abc\"\uff0c\"bca\"\uff0c\"cab\" \u548c \"abc\" \u3002\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= s.length <= 100
s
\u200b\u200b\u200b\u200b\u200b\u200b \u53ea\u5305\u542b\u5c0f\u5199\u82f1\u6587\u5b57\u6bcd\u3002There is a directed graph of n
colored nodes and m
edges. The nodes are numbered from 0
to n - 1
.
You are given a string colors
where colors[i]
is a lowercase English letter representing the color of the ith
node in this graph (0-indexed). You are also given a 2D array edges
where edges[j] = [aj, bj]
indicates that there is a directed edge from node aj
to node bj
.
A valid path in the graph is a sequence of nodes x1 -> x2 -> x3 -> ... -> xk
such that there is a directed edge from xi
to xi+1
for every 1 <= i < k
. The color value of the path is the number of nodes that are colored the most frequently occurring color along that path.
Return the largest color value of any valid path in the given graph, or -1
if the graph contains a cycle.
\n
Example 1:
\n\n\nInput: colors = "abaca", edges = [[0,1],[0,2],[2,3],[3,4]]\nOutput: 3\nExplanation: The path 0 -> 2 -> 3 -> 4 contains 3 nodes that are colored "a" (red in the above image)
.\n
\n\nExample 2:
\n\n\nInput: colors = "a", edges = [[0,0]]\nOutput: -1\nExplanation: There is a cycle from 0 to 0.\n\n\n
\n
Constraints:
\n\nn == colors.length
m == edges.length
1 <= n <= 105
0 <= m <= 105
colors
consists of lowercase English letters.0 <= aj, bj < n
\u7ed9\u4f60\u4e00\u4e2a\u00a0\u6709\u5411\u56fe\u00a0\uff0c\u5b83\u542b\u6709\u00a0n
\u00a0\u4e2a\u8282\u70b9\u548c m
\u00a0\u6761\u8fb9\u3002\u8282\u70b9\u7f16\u53f7\u4ece\u00a00
\u5230\u00a0n - 1
\u00a0\u3002
\u7ed9\u4f60\u4e00\u4e2a\u5b57\u7b26\u4e32\u00a0colors
\uff0c\u5176\u4e2d\u00a0colors[i]
\u00a0\u662f\u5c0f\u5199\u82f1\u6587\u5b57\u6bcd\uff0c\u8868\u793a\u56fe\u4e2d\u7b2c i
\u00a0\u4e2a\u8282\u70b9\u7684 \u989c\u8272\u00a0\uff08\u4e0b\u6807\u4ece 0\u00a0\u5f00\u59cb\uff09\u3002\u540c\u65f6\u7ed9\u4f60\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\u00a0edges
\u00a0\uff0c\u5176\u4e2d\u00a0edges[j] = [aj, bj]
\u00a0\u8868\u793a\u4ece\u8282\u70b9\u00a0aj
\u00a0\u5230\u8282\u70b9\u00a0bj
\u00a0\u6709\u4e00\u6761\u00a0\u6709\u5411\u8fb9\u00a0\u3002
\u56fe\u4e2d\u4e00\u6761\u6709\u6548 \u8def\u5f84\u00a0\u662f\u4e00\u4e2a\u70b9\u5e8f\u5217\u00a0x1 -> x2 -> x3 -> ... -> xk
\u00a0\uff0c\u5bf9\u4e8e\u6240\u6709\u00a01 <= i < k
\u00a0\uff0c\u4ece\u00a0xi
\u5230\u00a0xi+1
\u00a0\u5728\u56fe\u4e2d\u6709\u4e00\u6761\u6709\u5411\u8fb9\u3002\u8def\u5f84\u7684 \u989c\u8272\u503c\u00a0\u662f\u8def\u5f84\u4e2d \u51fa\u73b0\u6b21\u6570\u6700\u591a \u989c\u8272\u7684\u8282\u70b9\u6570\u76ee\u3002
\u8bf7\u4f60\u8fd4\u56de\u7ed9\u5b9a\u56fe\u4e2d\u6709\u6548\u8def\u5f84\u91cc\u9762\u7684\u00a0\u6700\u5927\u989c\u8272\u503c\u00a0\u3002\u5982\u679c\u56fe\u4e2d\u542b\u6709\u73af\uff0c\u8bf7\u8fd4\u56de -1
\u00a0\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1acolors = \"abaca\", edges = [[0,1],[0,2],[2,3],[3,4]]\n\u8f93\u51fa\uff1a3\n\u89e3\u91ca\uff1a\u8def\u5f84 0 -> 2 -> 3 -> 4 \u542b\u6709 3 \u4e2a\u989c\u8272\u4e3a \"a\" \u7684\u8282\u70b9\uff08\u4e0a\u56fe\u4e2d\u7684\u7ea2\u8272\u8282\u70b9\uff09\u3002
\n
\n\n\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1acolors = \"a\", edges = [[0,0]]\n\u8f93\u51fa\uff1a-1\n\u89e3\u91ca\uff1a\u4ece 0 \u5230 0 \u6709\u4e00\u4e2a\u73af\u3002\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\nn == colors.length
m == edges.length
1 <= n <= 105
0 <= m <= 105
colors
\u00a0\u53ea\u542b\u6709\u5c0f\u5199\u82f1\u6587\u5b57\u6bcd\u30020 <= aj, bj\u00a0< n
The min-product of an array is equal to the minimum value in the array multiplied by the array's sum.
\n\n[3,2,5]
(minimum value is 2
) has a min-product of 2 * (3+2+5) = 2 * 10 = 20
.Given an array of integers nums
, return the maximum min-product of any non-empty subarray of nums
. Since the answer may be large, return it modulo 109 + 7
.
Note that the min-product should be maximized before performing the modulo operation. Testcases are generated such that the maximum min-product without modulo will fit in a 64-bit signed integer.
\n\nA subarray is a contiguous part of an array.
\n\n\n
Example 1:
\n\n\nInput: nums = [1,2,3,2]\nOutput: 14\nExplanation: The maximum min-product is achieved with the subarray [2,3,2] (minimum value is 2).\n2 * (2+3+2) = 2 * 7 = 14.\n\n\n
Example 2:
\n\n\nInput: nums = [2,3,3,1,2]\nOutput: 18\nExplanation: The maximum min-product is achieved with the subarray [3,3] (minimum value is 3).\n3 * (3+3) = 3 * 6 = 18.\n\n\n
Example 3:
\n\n\nInput: nums = [3,1,5,6,4,2]\nOutput: 60\nExplanation: The maximum min-product is achieved with the subarray [5,6,4] (minimum value is 4).\n4 * (5+6+4) = 4 * 15 = 60.\n\n\n
\n
Constraints:
\n\n1 <= nums.length <= 105
1 <= nums[i] <= 107
\u4e00\u4e2a\u6570\u7ec4\u7684 \u6700\u5c0f\u4e58\u79ef\u00a0\u5b9a\u4e49\u4e3a\u8fd9\u4e2a\u6570\u7ec4\u4e2d \u6700\u5c0f\u503c\u00a0\u4e58\u4ee5\u00a0\u6570\u7ec4\u7684 \u548c\u00a0\u3002
\n\n[3,2,5]
\u00a0\uff08\u6700\u5c0f\u503c\u662f\u00a02
\uff09\u7684\u6700\u5c0f\u4e58\u79ef\u4e3a\u00a02 * (3+2+5) = 2 * 10 = 20
\u00a0\u3002\u7ed9\u4f60\u4e00\u4e2a\u6b63\u6574\u6570\u6570\u7ec4\u00a0nums
\u00a0\uff0c\u8bf7\u4f60\u8fd4\u56de\u00a0nums
\u00a0\u4efb\u610f\u00a0\u975e\u7a7a\u5b50\u6570\u7ec4\u00a0\u7684\u6700\u5c0f\u4e58\u79ef\u00a0\u7684\u00a0\u6700\u5927\u503c\u00a0\u3002\u7531\u4e8e\u7b54\u6848\u53ef\u80fd\u5f88\u5927\uff0c\u8bf7\u4f60\u8fd4\u56de\u7b54\u6848\u5bf9\u00a0\u00a0109 + 7
\u00a0\u53d6\u4f59\u00a0\u7684\u7ed3\u679c\u3002
\u8bf7\u6ce8\u610f\uff0c\u6700\u5c0f\u4e58\u79ef\u7684\u6700\u5927\u503c\u8003\u8651\u7684\u662f\u53d6\u4f59\u64cd\u4f5c \u4e4b\u524d\u00a0\u7684\u7ed3\u679c\u3002\u9898\u76ee\u4fdd\u8bc1\u6700\u5c0f\u4e58\u79ef\u7684\u6700\u5927\u503c\u5728 \u4e0d\u53d6\u4f59 \u7684\u60c5\u51b5\u4e0b\u53ef\u4ee5\u7528 64 \u4f4d\u6709\u7b26\u53f7\u6574\u6570\u00a0\u4fdd\u5b58\u3002
\n\n\u5b50\u6570\u7ec4\u00a0\u5b9a\u4e49\u4e3a\u4e00\u4e2a\u6570\u7ec4\u7684 \u8fde\u7eed\u00a0\u90e8\u5206\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1anums = [1,2,3,2]\n\u8f93\u51fa\uff1a14\n\u89e3\u91ca\uff1a\u6700\u5c0f\u4e58\u79ef\u7684\u6700\u5927\u503c\u7531\u5b50\u6570\u7ec4 [2,3,2] \uff08\u6700\u5c0f\u503c\u662f 2\uff09\u5f97\u5230\u3002\n2 * (2+3+2) = 2 * 7 = 14 \u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1anums = [2,3,3,1,2]\n\u8f93\u51fa\uff1a18\n\u89e3\u91ca\uff1a\u6700\u5c0f\u4e58\u79ef\u7684\u6700\u5927\u503c\u7531\u5b50\u6570\u7ec4 [3,3] \uff08\u6700\u5c0f\u503c\u662f 3\uff09\u5f97\u5230\u3002\n3 * (3+3) = 3 * 6 = 18 \u3002\n\n\n
\u793a\u4f8b 3\uff1a
\n\n\n\u8f93\u5165\uff1anums = [3,1,5,6,4,2]\n\u8f93\u51fa\uff1a60\n\u89e3\u91ca\uff1a\u6700\u5c0f\u4e58\u79ef\u7684\u6700\u5927\u503c\u7531\u5b50\u6570\u7ec4 [5,6,4] \uff08\u6700\u5c0f\u503c\u662f 4\uff09\u5f97\u5230\u3002\n4 * (5+6+4) = 4 * 15 = 60 \u3002\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= nums.length <= 105
1 <= nums[i] <= 107
You are given two non-increasing 0-indexed integer arrays nums1
\u200b\u200b\u200b\u200b\u200b\u200b and nums2
\u200b\u200b\u200b\u200b\u200b\u200b.
A pair of indices (i, j)
, where 0 <= i < nums1.length
and 0 <= j < nums2.length
, is valid if both i <= j
and nums1[i] <= nums2[j]
. The distance of the pair is j - i
\u200b\u200b\u200b\u200b.
Return the maximum distance of any valid pair (i, j)
. If there are no valid pairs, return 0
.
An array arr
is non-increasing if arr[i-1] >= arr[i]
for every 1 <= i < arr.length
.
\n
Example 1:
\n\n\nInput: nums1 = [55,30,5,4,2], nums2 = [100,20,10,10,5]\nOutput: 2\nExplanation: The valid pairs are (0,0), (2,2), (2,3), (2,4), (3,3), (3,4), and (4,4).\nThe maximum distance is 2 with pair (2,4).\n\n\n
Example 2:
\n\n\nInput: nums1 = [2,2,2], nums2 = [10,10,1]\nOutput: 1\nExplanation: The valid pairs are (0,0), (0,1), and (1,1).\nThe maximum distance is 1 with pair (0,1).\n\n\n
Example 3:
\n\n\nInput: nums1 = [30,29,19,5], nums2 = [25,25,25,25,25]\nOutput: 2\nExplanation: The valid pairs are (2,2), (2,3), (2,4), (3,3), and (3,4).\nThe maximum distance is 2 with pair (2,4).\n\n\n
Example 4:
\n\n\nInput: nums1 = [5,4], nums2 = [3,2]\nOutput: 0\nExplanation: There are no valid pairs, so return 0.\n\n\n
\n
Constraints:
\n\n1 <= nums1.length <= 105
1 <= nums2.length <= 105
1 <= nums1[i], nums2[j] <= 105
nums1
and nums2
are non-increasing.\u7ed9\u4f60\u4e24\u4e2a \u975e\u9012\u589e \u7684\u6574\u6570\u6570\u7ec4 nums1
\u200b\u200b\u200b\u200b\u200b\u200b \u548c nums2
\u200b\u200b\u200b\u200b\u200b\u200b \uff0c\u6570\u7ec4\u4e0b\u6807\u5747 \u4ece 0 \u5f00\u59cb \u8ba1\u6570\u3002
\u4e0b\u6807\u5bf9 (i, j)
\u4e2d 0 <= i < nums1.length
\u4e14 0 <= j < nums2.length
\u3002\u5982\u679c\u8be5\u4e0b\u6807\u5bf9\u540c\u65f6\u6ee1\u8db3 i <= j
\u4e14 nums1[i] <= nums2[j]
\uff0c\u5219\u79f0\u4e4b\u4e3a \u6709\u6548 \u4e0b\u6807\u5bf9\uff0c\u8be5\u4e0b\u6807\u5bf9\u7684 \u8ddd\u79bb \u4e3a j - i
\u200b\u200b \u3002\u200b\u200b
\u8fd4\u56de\u6240\u6709 \u6709\u6548 \u4e0b\u6807\u5bf9 (i, j)
\u4e2d\u7684 \u6700\u5927\u8ddd\u79bb \u3002\u5982\u679c\u4e0d\u5b58\u5728\u6709\u6548\u4e0b\u6807\u5bf9\uff0c\u8fd4\u56de 0
\u3002
\u4e00\u4e2a\u6570\u7ec4 arr
\uff0c\u5982\u679c\u6bcf\u4e2a 1 <= i < arr.length
\u5747\u6709 arr[i-1] >= arr[i]
\u6210\u7acb\uff0c\u90a3\u4e48\u8be5\u6570\u7ec4\u662f\u4e00\u4e2a \u975e\u9012\u589e \u6570\u7ec4\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1anums1 = [55,30,5,4,2], nums2 = [100,20,10,10,5]\n\u8f93\u51fa\uff1a2\n\u89e3\u91ca\uff1a\u6709\u6548\u4e0b\u6807\u5bf9\u662f (0,0), (2,2), (2,3), (2,4), (3,3), (3,4) \u548c (4,4) \u3002\n\u6700\u5927\u8ddd\u79bb\u662f 2 \uff0c\u5bf9\u5e94\u4e0b\u6807\u5bf9 (2,4) \u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1anums1 = [2,2,2], nums2 = [10,10,1]\n\u8f93\u51fa\uff1a1\n\u89e3\u91ca\uff1a\u6709\u6548\u4e0b\u6807\u5bf9\u662f (0,0), (0,1) \u548c (1,1) \u3002\n\u6700\u5927\u8ddd\u79bb\u662f 1 \uff0c\u5bf9\u5e94\u4e0b\u6807\u5bf9 (0,1) \u3002\n\n
\u793a\u4f8b 3\uff1a
\n\n\n\u8f93\u5165\uff1anums1 = [30,29,19,5], nums2 = [25,25,25,25,25]\n\u8f93\u51fa\uff1a2\n\u89e3\u91ca\uff1a\u6709\u6548\u4e0b\u6807\u5bf9\u662f (2,2), (2,3), (2,4), (3,3) \u548c (3,4) \u3002\n\u6700\u5927\u8ddd\u79bb\u662f 2 \uff0c\u5bf9\u5e94\u4e0b\u6807\u5bf9 (2,4) \u3002\n\n\n
\u793a\u4f8b 4\uff1a
\n\n\n\u8f93\u5165\uff1anums1 = [5,4], nums2 = [3,2]\n\u8f93\u51fa\uff1a0\n\u89e3\u91ca\uff1a\u4e0d\u5b58\u5728\u6709\u6548\u4e0b\u6807\u5bf9\uff0c\u6240\u4ee5\u8fd4\u56de 0 \u3002\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= nums1.length <= 105
1 <= nums2.length <= 105
1 <= nums1[i], nums2[j] <= 105
nums1
\u548c nums2
\u90fd\u662f \u975e\u9012\u589e \u6570\u7ec4You are given a 2D integer array logs
where each logs[i] = [birthi, deathi]
indicates the birth and death years of the ith
person.
The population of some year x
is the number of people alive during that year. The ith
person is counted in year x
's population if x
is in the inclusive range [birthi, deathi - 1]
. Note that the person is not counted in the year that they die.
Return the earliest year with the maximum population.
\n\n\n
Example 1:
\n\n\nInput: logs = [[1993,1999],[2000,2010]]\nOutput: 1993\nExplanation: The maximum population is 1, and 1993 is the earliest year with this population.\n\n\n
Example 2:
\n\n\nInput: logs = [[1950,1961],[1960,1971],[1970,1981]]\nOutput: 1960\nExplanation: \nThe maximum population is 2, and it had happened in years 1960 and 1970.\nThe earlier year between them is 1960.\n\n
\n
Constraints:
\n\n1 <= logs.length <= 100
1950 <= birthi < deathi <= 2050
\u7ed9\u4f60\u4e00\u4e2a\u4e8c\u7ef4\u6574\u6570\u6570\u7ec4 logs
\uff0c\u5176\u4e2d\u6bcf\u4e2a logs[i] = [birthi, deathi]
\u8868\u793a\u7b2c i
\u4e2a\u4eba\u7684\u51fa\u751f\u548c\u6b7b\u4ea1\u5e74\u4efd\u3002
\u5e74\u4efd x
\u7684 \u4eba\u53e3 \u5b9a\u4e49\u4e3a\u8fd9\u4e00\u5e74\u671f\u95f4\u6d3b\u7740\u7684\u4eba\u7684\u6570\u76ee\u3002\u7b2c i
\u4e2a\u4eba\u88ab\u8ba1\u5165\u5e74\u4efd x
\u7684\u4eba\u53e3\u9700\u8981\u6ee1\u8db3\uff1ax
\u5728\u95ed\u533a\u95f4 [birthi, deathi - 1]
\u5185\u3002\u6ce8\u610f\uff0c\u4eba\u4e0d\u5e94\u5f53\u8ba1\u5165\u4ed6\u4eec\u6b7b\u4ea1\u5f53\u5e74\u7684\u4eba\u53e3\u4e2d\u3002
\u8fd4\u56de \u4eba\u53e3\u6700\u591a \u4e14 \u6700\u65e9 \u7684\u5e74\u4efd\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1alogs = [[1993,1999],[2000,2010]]\n\u8f93\u51fa\uff1a1993\n\u89e3\u91ca\uff1a\u4eba\u53e3\u6700\u591a\u4e3a 1 \uff0c\u800c 1993 \u662f\u4eba\u53e3\u4e3a 1 \u7684\u6700\u65e9\u5e74\u4efd\u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1alogs = [[1950,1961],[1960,1971],[1970,1981]]\n\u8f93\u51fa\uff1a1960\n\u89e3\u91ca\uff1a \n\u4eba\u53e3\u6700\u591a\u4e3a 2 \uff0c\u5206\u522b\u51fa\u73b0\u5728 1960 \u548c 1970 \u3002\n\u5176\u4e2d\u6700\u65e9\u5e74\u4efd\u662f 1960 \u3002\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= logs.length <= 100
1950 <= birthi < deathi <= 2050
Given the head
of a linked list, find all the values that appear more than once in the list and delete the nodes that have any of those values.
Return the linked list after the deletions.
\r\n\r\n\r\n
Example 1:
\r\n\r\nInput: head = [1,2,3,2]\r\nOutput: [1,3]\r\nExplanation: 2 appears twice in the linked list, so all 2's should be deleted. After deleting all 2's, we are left with [1,3].\r\n\r\n\r\n
Example 2:
\r\n\r\nInput: head = [2,1,1,2]\r\nOutput: []\r\nExplanation: 2 and 1 both appear twice. All the elements should be deleted.\r\n\r\n\r\n
Example 3:
\r\n\r\nInput: head = [3,2,2,1,3,2,4]\r\nOutput: [1,4]\r\nExplanation: 3 appears twice and 2 appears three times. After deleting all 3's and 2's, we are left with [1,4].\r\n\r\n\r\n
\r\n
Constraints:
\r\n\r\n[1, 105]
1 <= Node.val <= 105
Table: Transactions
\r\n+----------------+----------+\r\n| Column Name | Type |\r\n+----------------+----------+\r\n| transaction_id | int |\r\n| day | datetime |\r\n| amount | int |\r\n+----------------+----------+\r\ntransaction_id is the primary key for this table.\r\nEach row contains information about one transaction.\r\n\r\n\r\n
\r\n\r\n
Write an SQL query to report the IDs of the transactions with the maximum amount
on their respective day. If in one day there are multiple such transactions, return all of them.
Return the result table in ascending order by transaction_id
.
The query result format is in the following example:
\r\n\r\n\r\n\r\n
\r\nTransactions table:\r\n+----------------+--------------------+--------+\r\n| transaction_id | day | amount |\r\n+----------------+--------------------+--------+\r\n| 8 | 2021-4-3 15:57:28 | 57 |\r\n| 9 | 2021-4-28 08:47:25 | 21 |\r\n| 1 | 2021-4-29 13:28:30 | 58 |\r\n| 5 | 2021-4-28 16:39:59 | 40 |\r\n| 6 | 2021-4-29 23:39:28 | 58 |\r\n+----------------+--------------------+--------+\r\n\r\nResult table:\r\n+----------------+\r\n| transaction_id |\r\n+----------------+\r\n| 1 |\r\n| 5 |\r\n| 6 |\r\n| 8 |\r\n+----------------+\r\n"2021-4-3" --> We have one transaction with ID 8, so we add 8 to the result table.\r\n"2021-4-28" --> We have two transactions with IDs 5 and 9. The transaction with ID 5 has an amount of 40, while the transaction with ID 9 has an amount of 21. We only include the transaction with ID 5 as it has the maximum amount this day.\r\n"2021-4-29" --> We have two transactions with IDs 1 and 6. Both transactions have the same amount of 58, so we include both in the result table.\r\nWe order the result table by transaction_id after collecting these IDs.\r\n\r\n
\r\n
Follow up: Could you solve it without using the MAX()
function?
An experiment is being conducted in a lab. To ensure accuracy, there are two sensors collecting data simultaneously. You are given two arrays sensor1
and sensor2
, where sensor1[i]
and sensor2[i]
are the ith
data points collected by the two sensors.
However, this type of sensor has a chance of being defective, which causes exactly one data point to be dropped. After the data is dropped, all the data points to the right of the dropped data are shifted one place to the left, and the last data point is replaced with some random value. It is guaranteed that this random value will not be equal to the dropped value.
\n\n[1,2,3,4,5]
and 3
is dropped, the sensor could return [1,2,4,5,7]
(the last position can be any value, not just 7
).We know that there is a defect in at most one of the sensors. Return the sensor number (1
or 2
) with the defect. If there is no defect in either sensor or if it is impossible to determine the defective sensor, return -1
.
\n
Example 1:
\n\n\nInput: sensor1 = [2,3,4,5], sensor2 = [2,1,3,4]\nOutput: 1\nExplanation: Sensor 2 has the correct values.\nThe second data point from sensor 2 is dropped, and the last value of sensor 1 is replaced by a 5.\n\n\n
Example 2:
\n\n\nInput: sensor1 = [2,2,2,2,2], sensor2 = [2,2,2,2,5]\nOutput: -1\nExplanation: It is impossible to determine which sensor has a defect.\nDropping the last value for either sensor could produce the output for the other sensor.\n\n\n
Example 3:
\n\n\nInput: sensor1 = [2,3,2,2,3,2], sensor2 = [2,3,2,3,2,7]\nOutput: 2\nExplanation: Sensor 1 has the correct values.\nThe fourth data point from sensor 1 is dropped, and the last value of sensor 1 is replaced by a 7.\n\n\n
\n
Constraints:
\n\nsensor1.length == sensor2.length
1 <= sensor1.length <= 100
1 <= sensor1[i], sensor2[i] <= 100
\u5b9e\u9a8c\u5ba4\u91cc\u6b63\u5728\u8fdb\u884c\u4e00\u9879\u5b9e\u9a8c\u3002\u4e3a\u4e86\u786e\u4fdd\u6570\u636e\u7684\u51c6\u786e\u6027\uff0c\u540c\u65f6\u4f7f\u7528 \u4e24\u4e2a \u4f20\u611f\u5668\u6765\u91c7\u96c6\u6570\u636e\u3002\u60a8\u5c06\u83b7\u5f972\u4e2a\u6570\u7ec4 sensor1
and sensor2
\uff0c\u5176\u4e2d sensor1[i]
\u00a0\u548c\u00a0sensor2[i]
\u00a0\u5206\u522b\u662f\u4e24\u4e2a\u4f20\u611f\u5668\u5bf9\u7b2c i
\u4e2a\u6570\u636e\u70b9\u91c7\u96c6\u5230\u7684\u6570\u636e\u3002
\u4f46\u662f\uff0c\u8fd9\u79cd\u7c7b\u578b\u7684\u4f20\u611f\u5668\u6709\u53ef\u80fd\u5b58\u5728\u7f3a\u9677\uff0c\u5b83\u4f1a\u5bfc\u81f4 \u67d0\u4e00\u4e2a \u6570\u636e\u70b9\u91c7\u96c6\u7684\u6570\u636e\uff08\u6389\u843d\u503c\uff09\u88ab\u4e22\u5f03\u3002
\n\n\u6570\u636e\u88ab\u4e22\u5f03\u540e\uff0c\u6240\u6709\u5728\u5176\u53f3\u4fa7\u7684\u6570\u636e\u70b9\u91c7\u96c6\u7684\u6570\u636e\uff0c\u90fd\u4f1a\u88ab\u5411\u5de6\u79fb\u52a8\u4e00\u4e2a\u4f4d\u7f6e\uff0c\u6700\u540e\u4e00\u4e2a\u6570\u636e\u70b9\u91c7\u96c6\u7684\u6570\u636e\u4f1a\u88ab\u4e00\u4e9b\u968f\u673a\u503c\u66ff\u6362\u3002\u53ef\u4ee5\u4fdd\u8bc1\u6b64\u968f\u673a\u503c\u4e0d\u7b49\u4e8e\u6389\u843d\u503c\u3002
\n\n[1,2,3,4,5]
\u00a0\uff0c\u00a0\u6b64\u65f6 3
\u88ab\u4e22\u5f03\u4e86, \u4f20\u611f\u5668\u4f1a\u8fd4\u56de\u00a0[1,2,4,5,7]
(\u6700\u540e\u7684\u4f4d\u7f6e\u53ef\u4ee5\u662f\u4efb\u4f55\u503c, \u4e0d\u4ec5\u4ec5\u662f\u00a07
).\u53ef\u4ee5\u786e\u5b9a\u7684\u662f\uff0c\u6700\u591a\u6709\u4e00\u4e2a \u4f20\u611f\u5668\u6709\u7f3a\u9677\u3002\u8bf7\u8fd4\u56de\u8fd9\u4e2a\u6709\u7f3a\u9677\u7684\u4f20\u611f\u5668\u7684\u7f16\u53f7 \uff081
\u6216 2
\uff09\u3002\u5982\u679c\u4efb\u4e00\u4f20\u611f\u5668 \u6ca1\u6709\u7f3a\u9677 \uff0c\u6216\u8005 \u65e0\u6cd5 \u786e\u5b9a\u6709\u7f3a\u9677\u7684\u4f20\u611f\u5668\uff0c\u5219\u8fd4\u56de -1
\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1asensor1 = [2,3,4,5], sensor2 = [2,1,3,4]\n\u8f93\u51fa\uff1a1\n\u89e3\u91ca\uff1a\u4f20\u611f\u5668 2 \u8fd4\u56de\u4e86\u6240\u6709\u6b63\u786e\u7684\u6570\u636e.\n\u4f20\u611f\u56682\u5bf9\u7b2c\u4e8c\u4e2a\u6570\u636e\u70b9\u91c7\u96c6\u7684\u6570\u636e\uff0c\u88ab\u4f20\u611f\u56681\u4e22\u5f03\u4e86\uff0c\u4f20\u611f\u56681\u8fd4\u56de\u7684\u6700\u540e\u4e00\u4e2a\u6570\u636e\u88ab\u66ff\u6362\u4e3a 5 \u3002\n\n
\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1asensor1 = [2,2,2,2,2], sensor2 = [2,2,2,2,5]\n\u8f93\u51fa\uff1a-1\n\u89e3\u91ca\uff1a\u65e0\u6cd5\u5224\u5b9a\u62ff\u4e2a\u4f20\u611f\u5668\u662f\u6709\u7f3a\u9677\u7684\u3002\n\u5047\u8bbe\u4efb\u4e00\u4f20\u611f\u5668\u4e22\u5f03\u7684\u6570\u636e\u662f\u6700\u540e\u4e00\u4f4d\uff0c\u90a3\u4e48\uff0c\u53e6\u4e00\u4e2a\u4f20\u611f\u5668\u5c31\u80fd\u7ed9\u51fa\u4e0e\u4e4b\u5bf9\u5e94\u7684\u8f93\u51fa\u3002\n\n\n
\u793a\u4f8b 3\uff1a
\n\n\n\u8f93\u5165\uff1asensor1 = [2,3,2,2,3,2], sensor2 = [2,3,2,3,2,7]\n\u8f93\u51fa\uff1a2\n\u89e3\u91ca\uff1a\u4f20\u611f\u5668 1 \u8fd4\u56de\u4e86\u6240\u6709\u6b63\u786e\u7684\u6570\u636e.\n\u4f20\u611f\u5668 1 \u5bf9\u7b2c\u56db\u4e2a\u6570\u636e\u70b9\u7684\u91c7\u96c6\u6570\u636e\uff0c\u88ab\u4f20\u611f\u56682\u4e22\u5931\u4e86, \u4f20\u611f\u5668 2 \u8fd4\u56de\u7684\u6700\u540e\u4e00\u4e2a\u6570\u636e\u88ab\u66ff\u6362\u4e3a 7 \u3002\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\nsensor1.length == sensor2.length
1 <= sensor1.length <= 100
1 <= sensor1[i], sensor2[i] <= 100
You are given a string num
, representing a large integer, and an integer k
.
We call some integer wonderful if it is a permutation of the digits in num
and is greater in value than num
. There can be many wonderful integers. However, we only care about the smallest-valued ones.
num = "5489355142"
:\n\n\t"5489355214"
."5489355241"
."5489355412"
."5489355421"
.Return the minimum number of adjacent digit swaps that needs to be applied to num
to reach the kth
smallest wonderful integer.
The tests are generated in such a way that kth
smallest wonderful integer exists.
\n
Example 1:
\n\n\nInput: num = "5489355142", k = 4\nOutput: 2\nExplanation: The 4th smallest wonderful number is "5489355421". To get this number:\n- Swap index 7 with index 8: "5489355142" -> "5489355412"\n- Swap index 8 with index 9: "5489355412" -> "5489355421"\n\n\n
Example 2:
\n\n\nInput: num = "11112", k = 4\nOutput: 4\nExplanation: The 4th smallest wonderful number is "21111". To get this number:\n- Swap index 3 with index 4: "11112" -> "11121"\n- Swap index 2 with index 3: "11121" -> "11211"\n- Swap index 1 with index 2: "11211" -> "12111"\n- Swap index 0 with index 1: "12111" -> "21111"\n\n\n
Example 3:
\n\n\nInput: num = "00123", k = 1\nOutput: 1\nExplanation: The 1st smallest wonderful number is "00132". To get this number:\n- Swap index 3 with index 4: "00123" -> "00132"\n\n\n
\n
Constraints:
\n\n2 <= num.length <= 1000
1 <= k <= 1000
num
only consists of digits.\u7ed9\u4f60\u4e00\u4e2a\u8868\u793a\u5927\u6574\u6570\u7684\u5b57\u7b26\u4e32 num
\uff0c\u548c\u4e00\u4e2a\u6574\u6570 k
\u3002
\u5982\u679c\u67d0\u4e2a\u6574\u6570\u662f num
\u4e2d\u5404\u4f4d\u6570\u5b57\u7684\u4e00\u4e2a \u6392\u5217 \u4e14\u5b83\u7684 \u503c\u5927\u4e8e num
\uff0c\u5219\u79f0\u8fd9\u4e2a\u6574\u6570\u4e3a \u5999\u6570 \u3002\u53ef\u80fd\u5b58\u5728\u5f88\u591a\u5999\u6570\uff0c\u4f46\u662f\u53ea\u9700\u8981\u5173\u6ce8 \u503c\u6700\u5c0f \u7684\u90a3\u4e9b\u3002
num = \"5489355142\"
\uff1a\n\n\t\"5489355214\"
\"5489355241\"
\"5489355412\"
\"5489355421\"
\u8fd4\u56de\u8981\u5f97\u5230\u7b2c k
\u4e2a \u6700\u5c0f\u5999\u6570 \u9700\u8981\u5bf9 num
\u6267\u884c\u7684 \u76f8\u90bb\u4f4d\u6570\u5b57\u4ea4\u6362\u7684\u6700\u5c0f\u6b21\u6570 \u3002
\u6d4b\u8bd5\u7528\u4f8b\u662f\u6309\u5b58\u5728\u7b2c k
\u4e2a\u6700\u5c0f\u5999\u6570\u800c\u751f\u6210\u7684\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1anum = \"5489355142\", k = 4\n\u8f93\u51fa\uff1a2\n\u89e3\u91ca\uff1a\u7b2c 4 \u4e2a\u6700\u5c0f\u5999\u6570\u662f \"5489355421\" \uff0c\u8981\u60f3\u5f97\u5230\u8fd9\u4e2a\u6570\u5b57\uff1a\n- \u4ea4\u6362\u4e0b\u6807 7 \u548c\u4e0b\u6807 8 \u5bf9\u5e94\u7684\u4f4d\uff1a\"5489355142\" -> \"5489355412\"\n- \u4ea4\u6362\u4e0b\u6807 8 \u548c\u4e0b\u6807 9 \u5bf9\u5e94\u7684\u4f4d\uff1a\"5489355412\" -> \"5489355421\"\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1anum = \"11112\", k = 4\n\u8f93\u51fa\uff1a4\n\u89e3\u91ca\uff1a\u7b2c 4 \u4e2a\u6700\u5c0f\u5999\u6570\u662f \"21111\" \uff0c\u8981\u60f3\u5f97\u5230\u8fd9\u4e2a\u6570\u5b57\uff1a\n- \u4ea4\u6362\u4e0b\u6807 3 \u548c\u4e0b\u6807 4 \u5bf9\u5e94\u7684\u4f4d\uff1a\"11112\" -> \"11121\"\n- \u4ea4\u6362\u4e0b\u6807 2 \u548c\u4e0b\u6807 3 \u5bf9\u5e94\u7684\u4f4d\uff1a\"11121\" -> \"11211\"\n- \u4ea4\u6362\u4e0b\u6807 1 \u548c\u4e0b\u6807 2 \u5bf9\u5e94\u7684\u4f4d\uff1a\"11211\" -> \"12111\"\n- \u4ea4\u6362\u4e0b\u6807 0 \u548c\u4e0b\u6807 1 \u5bf9\u5e94\u7684\u4f4d\uff1a\"12111\" -> \"21111\"\n\n\n
\u793a\u4f8b 3\uff1a
\n\n\u8f93\u5165\uff1anum = \"00123\", k = 1\n\u8f93\u51fa\uff1a1\n\u89e3\u91ca\uff1a\u7b2c 1 \u4e2a\u6700\u5c0f\u5999\u6570\u662f \"00132\" \uff0c\u8981\u60f3\u5f97\u5230\u8fd9\u4e2a\u6570\u5b57\uff1a\n- \u4ea4\u6362\u4e0b\u6807 3 \u548c\u4e0b\u6807 4 \u5bf9\u5e94\u7684\u4f4d\uff1a\"00123\" -> \"00132\"\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n2 <= num.length <= 1000
1 <= k <= 1000
num
\u4ec5\u7531\u6570\u5b57\u7ec4\u6210You are given a 2D integer array intervals
, where intervals[i] = [lefti, righti]
describes the ith
interval starting at lefti
and ending at righti
(inclusive). The size of an interval is defined as the number of integers it contains, or more formally righti - lefti + 1
.
You are also given an integer array queries
. The answer to the jth
query is the size of the smallest interval i
such that lefti <= queries[j] <= righti
. If no such interval exists, the answer is -1
.
Return an array containing the answers to the queries.
\n\n\n
Example 1:
\n\n\nInput: intervals = [[1,4],[2,4],[3,6],[4,4]], queries = [2,3,4,5]\nOutput: [3,3,1,4]\nExplanation: The queries are processed as follows:\n- Query = 2: The interval [2,4] is the smallest interval containing 2. The answer is 4 - 2 + 1 = 3.\n- Query = 3: The interval [2,4] is the smallest interval containing 3. The answer is 4 - 2 + 1 = 3.\n- Query = 4: The interval [4,4] is the smallest interval containing 4. The answer is 4 - 4 + 1 = 1.\n- Query = 5: The interval [3,6] is the smallest interval containing 5. The answer is 6 - 3 + 1 = 4.\n\n\n
Example 2:
\n\n\nInput: intervals = [[2,3],[2,5],[1,8],[20,25]], queries = [2,19,5,22]\nOutput: [2,-1,4,6]\nExplanation: The queries are processed as follows:\n- Query = 2: The interval [2,3] is the smallest interval containing 2. The answer is 3 - 2 + 1 = 2.\n- Query = 19: None of the intervals contain 19. The answer is -1.\n- Query = 5: The interval [2,5] is the smallest interval containing 5. The answer is 5 - 2 + 1 = 4.\n- Query = 22: The interval [20,25] is the smallest interval containing 22. The answer is 25 - 20 + 1 = 6.\n\n\n
\n
Constraints:
\n\n1 <= intervals.length <= 105
1 <= queries.length <= 105
intervals[i].length == 2
1 <= lefti <= righti <= 107
1 <= queries[j] <= 107
\u7ed9\u4f60\u4e00\u4e2a\u4e8c\u7ef4\u6574\u6570\u6570\u7ec4 intervals
\uff0c\u5176\u4e2d intervals[i] = [lefti, righti]
\u8868\u793a\u7b2c i
\u4e2a\u533a\u95f4\u5f00\u59cb\u4e8e lefti
\u3001\u7ed3\u675f\u4e8e righti
\uff08\u5305\u542b\u4e24\u4fa7\u53d6\u503c\uff0c\u95ed\u533a\u95f4\uff09\u3002\u533a\u95f4\u7684 \u957f\u5ea6 \u5b9a\u4e49\u4e3a\u533a\u95f4\u4e2d\u5305\u542b\u7684\u6574\u6570\u6570\u76ee\uff0c\u66f4\u6b63\u5f0f\u5730\u8868\u8fbe\u662f righti - lefti + 1
\u3002
\u518d\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 queries
\u3002\u7b2c j
\u4e2a\u67e5\u8be2\u7684\u7b54\u6848\u662f\u6ee1\u8db3\u00a0lefti <= queries[j] <= righti
\u7684 \u957f\u5ea6\u6700\u5c0f\u533a\u95f4 i
\u7684\u957f\u5ea6 \u3002\u5982\u679c\u4e0d\u5b58\u5728\u8fd9\u6837\u7684\u533a\u95f4\uff0c\u90a3\u4e48\u7b54\u6848\u662f -1
\u3002
\u4ee5\u6570\u7ec4\u5f62\u5f0f\u8fd4\u56de\u5bf9\u5e94\u67e5\u8be2\u7684\u6240\u6709\u7b54\u6848\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1aintervals = [[1,4],[2,4],[3,6],[4,4]], queries = [2,3,4,5]\n\u8f93\u51fa\uff1a[3,3,1,4]\n\u89e3\u91ca\uff1a\u67e5\u8be2\u5904\u7406\u5982\u4e0b\uff1a\n- Query = 2 \uff1a\u533a\u95f4 [2,4] \u662f\u5305\u542b 2 \u7684\u6700\u5c0f\u533a\u95f4\uff0c\u7b54\u6848\u4e3a 4 - 2 + 1 = 3 \u3002\n- Query = 3 \uff1a\u533a\u95f4 [2,4] \u662f\u5305\u542b 3 \u7684\u6700\u5c0f\u533a\u95f4\uff0c\u7b54\u6848\u4e3a 4 - 2 + 1 = 3 \u3002\n- Query = 4 \uff1a\u533a\u95f4 [4,4] \u662f\u5305\u542b 4 \u7684\u6700\u5c0f\u533a\u95f4\uff0c\u7b54\u6848\u4e3a 4 - 4 + 1 = 1 \u3002\n- Query = 5 \uff1a\u533a\u95f4 [3,6] \u662f\u5305\u542b 5 \u7684\u6700\u5c0f\u533a\u95f4\uff0c\u7b54\u6848\u4e3a 6 - 3 + 1 = 4 \u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1aintervals = [[2,3],[2,5],[1,8],[20,25]], queries = [2,19,5,22]\n\u8f93\u51fa\uff1a[2,-1,4,6]\n\u89e3\u91ca\uff1a\u67e5\u8be2\u5904\u7406\u5982\u4e0b\uff1a\n- Query = 2 \uff1a\u533a\u95f4 [2,3] \u662f\u5305\u542b 2 \u7684\u6700\u5c0f\u533a\u95f4\uff0c\u7b54\u6848\u4e3a 3 - 2 + 1 = 2 \u3002\n- Query = 19\uff1a\u4e0d\u5b58\u5728\u5305\u542b 19 \u7684\u533a\u95f4\uff0c\u7b54\u6848\u4e3a -1 \u3002\n- Query = 5 \uff1a\u533a\u95f4 [2,5] \u662f\u5305\u542b 5 \u7684\u6700\u5c0f\u533a\u95f4\uff0c\u7b54\u6848\u4e3a 5 - 2 + 1 = 4 \u3002\n- Query = 22\uff1a\u533a\u95f4 [20,25] \u662f\u5305\u542b 22 \u7684\u6700\u5c0f\u533a\u95f4\uff0c\u7b54\u6848\u4e3a 25 - 20 + 1 = 6 \u3002\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= intervals.length <= 105
1 <= queries.length <= 105
queries[i].length == 2
1 <= lefti <= righti <= 107
1 <= queries[j] <= 107
You are given a string s
that consists of only digits.
Check if we can split s
into two or more non-empty substrings such that the numerical values of the substrings are in descending order and the difference between numerical values of every two adjacent substrings is equal to 1
.
s = "0090089"
can be split into ["0090", "089"]
with numerical values [90,89]
. The values are in descending order and adjacent values differ by 1
, so this way is valid.s = "001"
can be split into ["0", "01"]
, ["00", "1"]
, or ["0", "0", "1"]
. However all the ways are invalid because they have numerical values [0,1]
, [0,1]
, and [0,0,1]
respectively, all of which are not in descending order.Return true
if it is possible to split s
\u200b\u200b\u200b\u200b\u200b\u200b as described above, or false
otherwise.
A substring is a contiguous sequence of characters in a string.
\n\n\n
Example 1:
\n\n\nInput: s = "1234"\nOutput: false\nExplanation: There is no valid way to split s.\n\n\n
Example 2:
\n\n\nInput: s = "050043"\nOutput: true\nExplanation: s can be split into ["05", "004", "3"] with numerical values [5,4,3].\nThe values are in descending order with adjacent values differing by 1.\n\n\n
Example 3:
\n\n\nInput: s = "9080701"\nOutput: false\nExplanation: There is no valid way to split s.\n\n\n
Example 4:
\n\n\nInput: s = "10009998"\nOutput: true\nExplanation: s can be split into ["100", "099", "98"] with numerical values [100,99,98].\nThe values are in descending order with adjacent values differing by 1.\n\n\n
\n
Constraints:
\n\n1 <= s.length <= 20
s
only consists of digits.\u7ed9\u4f60\u4e00\u4e2a\u4ec5\u7531\u6570\u5b57\u7ec4\u6210\u7684\u5b57\u7b26\u4e32 s
\u3002
\u8bf7\u4f60\u5224\u65ad\u80fd\u5426\u5c06 s
\u62c6\u5206\u6210\u4e24\u4e2a\u6216\u8005\u591a\u4e2a \u975e\u7a7a\u5b50\u5b57\u7b26\u4e32 \uff0c\u4f7f\u5b50\u5b57\u7b26\u4e32\u7684 \u6570\u503c \u6309 \u964d\u5e8f \u6392\u5217\uff0c\u4e14\u6bcf\u4e24\u4e2a \u76f8\u90bb\u5b50\u5b57\u7b26\u4e32 \u7684\u6570\u503c\u4e4b \u5dee \u7b49\u4e8e 1
\u3002
s = \"0090089\"
\u53ef\u4ee5\u62c6\u5206\u6210 [\"0090\", \"089\"]
\uff0c\u6570\u503c\u4e3a [90,89]
\u3002\u8fd9\u4e9b\u6570\u503c\u6ee1\u8db3\u6309\u964d\u5e8f\u6392\u5217\uff0c\u4e14\u76f8\u90bb\u503c\u76f8\u5dee 1
\uff0c\u8fd9\u79cd\u62c6\u5206\u65b9\u6cd5\u53ef\u884c\u3002s = \"001\"
\u53ef\u4ee5\u62c6\u5206\u6210 [\"0\", \"01\"]
\u3001[\"00\", \"1\"]
\u6216 [\"0\", \"0\", \"1\"]
\u3002\u7136\u800c\uff0c\u6240\u6709\u8fd9\u4e9b\u62c6\u5206\u65b9\u6cd5\u90fd\u4e0d\u53ef\u884c\uff0c\u56e0\u4e3a\u5bf9\u5e94\u6570\u503c\u5206\u522b\u662f [0,1]
\u3001[0,1]
\u548c [0,0,1]
\uff0c\u90fd\u4e0d\u6ee1\u8db3\u6309\u964d\u5e8f\u6392\u5217\u7684\u8981\u6c42\u3002\u5982\u679c\u53ef\u4ee5\u6309\u8981\u6c42\u62c6\u5206 s
\uff0c\u8fd4\u56de true
\uff1b\u5426\u5219\uff0c\u8fd4\u56de false
\u3002
\u5b50\u5b57\u7b26\u4e32 \u662f\u5b57\u7b26\u4e32\u4e2d\u7684\u4e00\u4e2a\u8fde\u7eed\u5b57\u7b26\u5e8f\u5217\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1as = \"1234\"\n\u8f93\u51fa\uff1afalse\n\u89e3\u91ca\uff1a\u4e0d\u5b58\u5728\u62c6\u5206 s \u7684\u53ef\u884c\u65b9\u6cd5\u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1as = \"050043\"\n\u8f93\u51fa\uff1atrue\n\u89e3\u91ca\uff1as \u53ef\u4ee5\u62c6\u5206\u4e3a [\"05\", \"004\", \"3\"] \uff0c\u5bf9\u5e94\u6570\u503c\u4e3a [5,4,3] \u3002\n\u6ee1\u8db3\u6309\u964d\u5e8f\u6392\u5217\uff0c\u4e14\u76f8\u90bb\u503c\u76f8\u5dee 1
\u3002\n
\n\n\u793a\u4f8b 3\uff1a
\n\n\n\u8f93\u5165\uff1as = \"9080701\"\n\u8f93\u51fa\uff1afalse\n\u89e3\u91ca\uff1a\u4e0d\u5b58\u5728\u62c6\u5206 s \u7684\u53ef\u884c\u65b9\u6cd5\u3002\n\n\n
\u793a\u4f8b 4\uff1a
\n\n\n\u8f93\u5165\uff1as = \"10009998\"\n\u8f93\u51fa\uff1atrue\n\u89e3\u91ca\uff1as \u53ef\u4ee5\u62c6\u5206\u4e3a [\"100\", \"099\", \"98\"] \uff0c\u5bf9\u5e94\u6570\u503c\u4e3a [100,99,98] \u3002\n\u6ee1\u8db3\u6309\u964d\u5e8f\u6392\u5217\uff0c\u4e14\u76f8\u90bb\u503c\u76f8\u5dee 1
\u3002
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= s.length <= 20
s
\u4ec5\u7531\u6570\u5b57\u7ec4\u6210Given an integer array nums
(0-indexed) and two integers target
and start
, find an index i
such that nums[i] == target
and abs(i - start)
is minimized. Note that abs(x)
is the absolute value of x
.
Return abs(i - start)
.
It is guaranteed that target
exists in nums
.
\n
Example 1:
\n\n\nInput: nums = [1,2,3,4,5], target = 5, start = 3\nOutput: 1\nExplanation: nums[4] = 5 is the only value equal to target, so the answer is abs(4 - 3) = 1.\n\n\n
Example 2:
\n\n\nInput: nums = [1], target = 1, start = 0\nOutput: 0\nExplanation: nums[0] = 1 is the only value equal to target, so the answer is abs(0 - 0) = 0.\n\n\n
Example 3:
\n\n\nInput: nums = [1,1,1,1,1,1,1,1,1,1], target = 1, start = 0\nOutput: 0\nExplanation: Every value of nums is 1, but nums[0] minimizes abs(i - start), which is abs(0 - 0) = 0.\n\n\n
\n
Constraints:
\n\n1 <= nums.length <= 1000
1 <= nums[i] <= 104
0 <= start < nums.length
target
is in nums
.\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 nums
\uff08\u4e0b\u6807 \u4ece 0 \u5f00\u59cb \u8ba1\u6570\uff09\u4ee5\u53ca\u4e24\u4e2a\u6574\u6570 target
\u548c start
\uff0c\u8bf7\u4f60\u627e\u51fa\u4e00\u4e2a\u4e0b\u6807 i
\uff0c\u6ee1\u8db3 nums[i] == target
\u4e14 abs(i - start)
\u6700\u5c0f\u5316 \u3002\u6ce8\u610f\uff1aabs(x)
\u8868\u793a x
\u7684\u7edd\u5bf9\u503c\u3002
\u8fd4\u56de abs(i - start)
\u3002
\u9898\u76ee\u6570\u636e\u4fdd\u8bc1 target
\u5b58\u5728\u4e8e nums
\u4e2d\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1anums = [1,2,3,4,5], target = 5, start = 3\n\u8f93\u51fa\uff1a1\n\u89e3\u91ca\uff1anums[4] = 5 \u662f\u552f\u4e00\u4e00\u4e2a\u7b49\u4e8e target \u7684\u503c\uff0c\u6240\u4ee5\u7b54\u6848\u662f abs(4 - 3) = 1 \u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1anums = [1], target = 1, start = 0\n\u8f93\u51fa\uff1a0\n\u89e3\u91ca\uff1anums[0] = 1 \u662f\u552f\u4e00\u4e00\u4e2a\u7b49\u4e8e target \u7684\u503c\uff0c\u6240\u4ee5\u7b54\u6848\u662f abs(0 - 0) = 0 \u3002\n\n\n
\u793a\u4f8b 3\uff1a
\n\n\n\u8f93\u5165\uff1anums = [1,1,1,1,1,1,1,1,1,1], target = 1, start = 0\n\u8f93\u51fa\uff1a0\n\u89e3\u91ca\uff1anums \u4e2d\u7684\u6bcf\u4e2a\u503c\u90fd\u662f 1 \uff0c\u4f46 nums[0] \u4f7f abs(i - start) \u7684\u7ed3\u679c\u5f97\u4ee5\u6700\u5c0f\u5316\uff0c\u6240\u4ee5\u7b54\u6848\u662f abs(0 - 0) = 0 \u3002\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= nums.length <= 1000
1 <= nums[i] <= 104
0 <= start < nums.length
target
\u5b58\u5728\u4e8e nums
\u4e2dTable: Customers
\n+--------------+------+\n| Column Name | Type |\n+--------------+------+\n| customer_id | int |\n| year | int |\n| revenue | int |\n+--------------+------+\n(customer_id, year) is the primary key for this table.\nThis table contains the customer ID and the revenue of customers in different years.\nNote that this revenue can be negative.\n\n\n
\n\n
Write an SQL query to report the customers with postive revenue in the year 2021.
\n\nReturn the result table in any order.
\n\nThe query result format is in the following example:
\n\n\n\n
\nCustomers\n+-------------+------+---------+\n| customer_id | year | revenue |\n+-------------+------+---------+\n| 1 | 2018 | 50 |\n| 1 | 2021 | 30 |\n| 1 | 2020 | 70 |\n| 2 | 2021 | -50 |\n| 3 | 2018 | 10 |\n| 3 | 2016 | 50 |\n| 4 | 2021 | 20 |\n+-------------+------+---------+\n\nResult table:\n+-------------+\n| customer_id |\n+-------------+\n| 1 |\n| 4 |\n+-------------+\n\nCustomer 1 has revenue equal to 30 in year 2021.\nCustomer 2 has revenue equal to -50 in year 2021.\nCustomer 3 has no revenue in year 2021.\nCustomer 4 has revenue equal to 20 in year 2021.\nThus only customers 1 and 4 have postive revenue in year 2021.\n", "content_cn": "
\u8868\uff1aCustomers
\n+--------------+------+\n| Column Name | Type |\n+--------------+------+\n| customer_id | int |\n| year | int |\n| revenue | int |\n+--------------+------+\n(customer_id, year) \u662f\u8fd9\u4e2a\u8868\u7684\u4e3b\u952e\u3002\n\u8fd9\u4e2a\u8868\u5305\u542b\u5ba2\u6237 ID \u548c\u4e0d\u540c\u5e74\u4efd\u7684\u5ba2\u6237\u6536\u5165\u3002\n\u6ce8\u610f\uff0c\u8fd9\u4e2a\u6536\u5165\u53ef\u80fd\u662f\u8d1f\u6570\u3002\n\n\n
\u00a0
\n\n\u5199\u4e00\u4e2a SQL \u67e5\u8be2\u6765\u67e5\u8be2 2021 \u5e74\u5177\u6709 \u6b63\u6536\u5165 \u7684\u5ba2\u6237\u3002
\n\n\u53ef\u4ee5\u6309 \u4efb\u610f\u987a\u5e8f \u8fd4\u56de\u7ed3\u679c\u8868\u3002
\n\n\u67e5\u8be2\u7ed3\u679c\u683c\u5f0f\u5982\u4e0b\u4f8b\u3002
\n\n\u00a0
\n\n\nCustomers\n+-------------+------+---------+\n| customer_id | year | revenue |\n+-------------+------+---------+\n| 1 | 2018 | 50 |\n| 1 | 2021 | 30 |\n| 1 | 2020 | 70 |\n| 2 | 2021 | -50 |\n| 3 | 2018 | 10 |\n| 3 | 2016 | 50 |\n| 4 | 2021 | 20 |\n+-------------+------+---------+\n\nResult table:\n+-------------+\n| customer_id |\n+-------------+\n| 1 |\n| 4 |\n+-------------+\n\u5ba2\u6237 1 \u5728 2021 \u5e74\u7684\u6536\u5165\u7b49\u4e8e 30 \u3002\n\u5ba2\u6237 2 \u5728 2021 \u5e74\u7684\u6536\u5165\u7b49\u4e8e -50 \u3002\n\u5ba2\u6237 3 \u5728 2021 \u5e74\u6ca1\u6709\u6536\u5165\u3002\n\u5ba2\u6237 4 \u5728 2021 \u5e74\u7684\u6536\u5165\u7b49\u4e8e 20 \u3002\n\u56e0\u6b64\uff0c\u53ea\u6709\u5ba2\u6237 1 \u548c 4 \u5728 2021 \u5e74\u6709\u6b63\u6536\u5165\u3002\n", "tags_en": [], "tags_cn": [], "difficulty_en": "Easy", "difficulty_cn": "\u7b80\u5355", "code_snippets": [{"lang": "MySQL", "langSlug": "mysql", "code": "# Write your MySQL query statement below", "__typename": "CodeSnippetNode"}, {"lang": "MS SQL Server", "langSlug": "mssql", "code": "/* Write your T-SQL query statement below */", "__typename": "CodeSnippetNode"}, {"lang": "Oracle", "langSlug": "oraclesql", "code": "/* Write your PL/SQL query statement below */", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1821](https://leetcode-cn.com/problems/find-customers-with-positive-revenue-this-year)", "[\u5bfb\u627e\u4eca\u5e74\u5177\u6709\u6b63\u6536\u5165\u7684\u5ba2\u6237](/solution/1800-1899/1821.Find%20Customers%20With%20Positive%20Revenue%20this%20Year/README.md)", "", "\u7b80\u5355", "\ud83d\udd12"], "md_table_row_en": ["[1821](https://leetcode.com/problems/find-customers-with-positive-revenue-this-year)", "[Find Customers With Positive Revenue this Year](/solution/1800-1899/1821.Find%20Customers%20With%20Positive%20Revenue%20this%20Year/README_EN.md)", "", "Easy", "\ud83d\udd12"]}, {"question_id": "1972", "frontend_question_id": "1861", "paid_only": false, "paid_only_cn": false, "url_cn": "https://leetcode-cn.com/problems/rotating-the-box", "url_en": "https://leetcode.com/problems/rotating-the-box", "relative_path_cn": "/solution/1800-1899/1861.Rotating%20the%20Box/README.md", "relative_path_en": "/solution/1800-1899/1861.Rotating%20the%20Box/README_EN.md", "title_cn": "\u65cb\u8f6c\u76d2\u5b50", "title_en": "Rotating the Box", "question_title_slug": "rotating-the-box", "content_en": "
You are given an m x n
matrix of characters box
representing a side-view of a box. Each cell of the box is one of the following:
'#'
'*'
'.'
The box is rotated 90 degrees clockwise, causing some of the stones to fall due to gravity. Each stone falls down until it lands on an obstacle, another stone, or the bottom of the box. Gravity does not affect the obstacles' positions, and the inertia from the box's rotation does not affect the stones' horizontal positions.
\r\n\r\nIt is guaranteed that each stone in box
rests on an obstacle, another stone, or the bottom of the box.
Return an n x m
matrix representing the box after the rotation described above.
\r\n
Example 1:
\r\n\r\n\r\nInput: box = [["#",".","#"]]\r\nOutput: [["."],\r\n ["#"],\r\n ["#"]]\r\n\r\n\r\n
Example 2:
\r\n\r\n\r\nInput: box = [["#",".","*","."],\r\n ["#","#","*","."]]\r\nOutput: [["#","."],\r\n ["#","#"],\r\n ["*","*"],\r\n [".","."]]\r\n\r\n\r\n
Example 3:
\r\n\r\n\r\nInput: box = [["#","#","*",".","*","."],\r\n ["#","#","#","*",".","."],\r\n ["#","#","#",".","#","."]]\r\nOutput: [[".","#","#"],\r\n [".","#","#"],\r\n ["#","#","*"],\r\n ["#","*","."],\r\n ["#",".","*"],\r\n ["#",".","."]]\r\n\r\n\r\n
\r\n
Constraints:
\r\n\r\nm == box.length
n == box[i].length
1 <= m, n <= 500
box[i][j]
is either '#'
, '*'
, or '.'
.\u7ed9\u4f60\u4e00\u4e2a\u00a0m x n
\u00a0\u7684\u5b57\u7b26\u77e9\u9635\u00a0box
\u00a0\uff0c\u5b83\u8868\u793a\u4e00\u4e2a\u7bb1\u5b50\u7684\u4fa7\u89c6\u56fe\u3002\u7bb1\u5b50\u7684\u6bcf\u4e00\u4e2a\u683c\u5b50\u53ef\u80fd\u4e3a\uff1a
'#'
\u00a0\u8868\u793a\u77f3\u5934'*'
\u00a0\u8868\u793a\u56fa\u5b9a\u7684\u969c\u788d\u7269'.'
\u00a0\u8868\u793a\u7a7a\u4f4d\u7f6e\u8fd9\u4e2a\u7bb1\u5b50\u88ab \u987a\u65f6\u9488\u65cb\u8f6c 90 \u5ea6\u00a0\uff0c\u7531\u4e8e\u91cd\u529b\u539f\u56e0\uff0c\u90e8\u5206\u77f3\u5934\u7684\u4f4d\u7f6e\u4f1a\u53d1\u751f\u6539\u53d8\u3002\u6bcf\u4e2a\u77f3\u5934\u4f1a\u5782\u76f4\u6389\u843d\uff0c\u76f4\u5230\u5b83\u9047\u5230\u969c\u788d\u7269\uff0c\u53e6\u4e00\u4e2a\u77f3\u5934\u6216\u8005\u7bb1\u5b50\u7684\u5e95\u90e8\u3002\u91cd\u529b \u4e0d\u4f1a\u00a0\u5f71\u54cd\u969c\u788d\u7269\u7684\u4f4d\u7f6e\uff0c\u540c\u65f6\u7bb1\u5b50\u65cb\u8f6c\u4e0d\u4f1a\u4ea7\u751f\u60ef\u6027\u00a0\uff0c\u4e5f\u5c31\u662f\u8bf4\u77f3\u5934\u7684\u6c34\u5e73\u4f4d\u7f6e\u4e0d\u4f1a\u53d1\u751f\u6539\u53d8\u3002
\n\n\u9898\u76ee\u4fdd\u8bc1\u521d\u59cb\u65f6\u00a0box
\u00a0\u4e2d\u7684\u77f3\u5934\u8981\u4e48\u5728\u4e00\u4e2a\u969c\u788d\u7269\u4e0a\uff0c\u8981\u4e48\u5728\u53e6\u4e00\u4e2a\u77f3\u5934\u4e0a\uff0c\u8981\u4e48\u5728\u7bb1\u5b50\u7684\u5e95\u90e8\u3002
\u8bf7\u4f60\u8fd4\u56de\u4e00\u4e2a\u00a0n x m
\u7684\u77e9\u9635\uff0c\u8868\u793a\u6309\u7167\u4e0a\u8ff0\u65cb\u8f6c\u540e\uff0c\u7bb1\u5b50\u5185\u7684\u7ed3\u679c\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1abox = [[\"#\",\".\",\"#\"]]\n\u8f93\u51fa\uff1a[[\".\"],\n\u00a0 [\"#\"],\n\u00a0 [\"#\"]]\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1abox = [[\"#\",\".\",\"*\",\".\"],\n\u00a0 [\"#\",\"#\",\"*\",\".\"]]\n\u8f93\u51fa\uff1a[[\"#\",\".\"],\n\u00a0 [\"#\",\"#\"],\n\u00a0 [\"*\",\"*\"],\n\u00a0 [\".\",\".\"]]\n\n\n
\u793a\u4f8b 3\uff1a
\n\n\u8f93\u5165\uff1abox = [[\"#\",\"#\",\"*\",\".\",\"*\",\".\"],\n\u00a0 [\"#\",\"#\",\"#\",\"*\",\".\",\".\"],\n\u00a0 [\"#\",\"#\",\"#\",\".\",\"#\",\".\"]]\n\u8f93\u51fa\uff1a[[\".\",\"#\",\"#\"],\n\u00a0 [\".\",\"#\",\"#\"],\n\u00a0 [\"#\",\"#\",\"*\"],\n\u00a0 [\"#\",\"*\",\".\"],\n\u00a0 [\"#\",\".\",\"*\"],\n\u00a0 [\"#\",\".\",\".\"]]\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\nm == box.length
n == box[i].length
1 <= m, n <= 500
box[i][j]
\u00a0\u53ea\u53ef\u80fd\u662f\u00a0'#'
\u00a0\uff0c'*'
\u00a0\u6216\u8005\u00a0'.'
\u00a0\u3002You are given two integers memory1
and memory2
representing the available memory in bits on two memory sticks. There is currently a faulty program running that consumes an increasing amount of memory every second.
At the ith
second (starting from 1), i
bits of memory are allocated to the stick with more available memory (or from the first memory stick if both have the same available memory). If neither stick has at least i
bits of available memory, the program crashes.
Return an array containing [crashTime, memory1crash, memory2crash]
, where crashTime
is the time (in seconds) when the program crashed and memory1crash
and memory2crash
are the available bits of memory in the first and second sticks respectively.
\n
Example 1:
\n\n\nInput: memory1 = 2, memory2 = 2\nOutput: [3,1,0]\nExplanation: The memory is allocated as follows:\n- At the 1st second, 1 bit of memory is allocated to stick 1. The first stick now has 1 bit of available memory.\n- At the 2nd second, 2 bits of memory are allocated to stick 2. The second stick now has 0 bits of available memory.\n- At the 3rd second, the program crashes. The sticks have 1 and 0 bits available respectively.\n\n\n
Example 2:
\n\n\nInput: memory1 = 8, memory2 = 11\nOutput: [6,0,4]\nExplanation: The memory is allocated as follows:\n- At the 1st second, 1 bit of memory is allocated to stick 2. The second stick now has 10 bit of available memory.\n- At the 2nd second, 2 bits of memory are allocated to stick 2. The second stick now has 8 bits of available memory.\n- At the 3rd second, 3 bits of memory are allocated to stick 1. The first stick now has 5 bits of available memory.\n- At the 4th second, 4 bits of memory are allocated to stick 2. The second stick now has 4 bits of available memory.\n- At the 5th second, 5 bits of memory are allocated to stick 1. The first stick now has 0 bits of available memory.\n- At the 6th second, the program crashes. The sticks have 0 and 4 bits available respectively.\n\n\n
\n
Constraints:
\n\n0 <= memory1, memory2 <= 231 - 1
\u7ed9\u4f60\u4e24\u4e2a\u6574\u6570\u00a0memory1
\u548c\u00a0memory2
\u00a0\u5206\u522b\u8868\u793a\u4e24\u4e2a\u5185\u5b58\u6761\u5269\u4f59\u53ef\u7528\u5185\u5b58\u7684\u4f4d\u6570\u3002\u73b0\u5728\u6709\u4e00\u4e2a\u7a0b\u5e8f\u6bcf\u79d2\u9012\u589e\u7684\u901f\u5ea6\u6d88\u8017\u7740\u5185\u5b58\u3002
\u5728\u7b2c\u00a0i
\u00a0\u79d2\uff08\u79d2\u6570\u4ece 1 \u5f00\u59cb\uff09\uff0c\u6709 i
\u00a0\u4f4d\u5185\u5b58\u88ab\u5206\u914d\u5230\u00a0\u5269\u4f59\u5185\u5b58\u8f83\u591a\u00a0\u7684\u5185\u5b58\u6761\uff08\u5982\u679c\u4e24\u8005\u4e00\u6837\u591a\uff0c\u5219\u5206\u914d\u5230\u7b2c\u4e00\u4e2a\u5185\u5b58\u6761\uff09\u3002\u5982\u679c\u4e24\u8005\u5269\u4f59\u5185\u5b58\u90fd\u4e0d\u8db3 i
\u00a0\u4f4d\uff0c\u90a3\u4e48\u7a0b\u5e8f\u5c06 \u610f\u5916\u9000\u51fa\u00a0\u3002
\u8bf7\u4f60\u8fd4\u56de\u4e00\u4e2a\u6570\u7ec4\uff0c\u5305\u542b [crashTime, memory1crash, memory2crash]
\u00a0\uff0c\u5176\u4e2d\u00a0crashTime
\u662f\u7a0b\u5e8f\u610f\u5916\u9000\u51fa\u7684\u65f6\u95f4\uff08\u5355\u4f4d\u4e3a\u79d2\uff09\uff0c\u00a0memory1crash
\u548c\u00a0memory2crash
\u00a0\u5206\u522b\u662f\u4e24\u4e2a\u5185\u5b58\u6761\u6700\u540e\u5269\u4f59\u5185\u5b58\u7684\u4f4d\u6570\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1amemory1 = 2, memory2 = 2\n\u8f93\u51fa\uff1a[3,1,0]\n\u89e3\u91ca\uff1a\u5185\u5b58\u5206\u914d\u5982\u4e0b\uff1a\n- \u7b2c 1 \u79d2\uff0c\u5185\u5b58\u6761 1 \u88ab\u5360\u7528 1 \u4f4d\u5185\u5b58\u3002\u5185\u5b58\u6761 1 \u73b0\u5728\u6709 1 \u4f4d\u5269\u4f59\u53ef\u7528\u5185\u5b58\u3002\n- \u7b2c 2 \u79d2\uff0c\u5185\u5b58\u6761 2 \u88ab\u5360\u7528 2 \u4f4d\u5185\u5b58\u3002\u5185\u5b58\u6761 2 \u73b0\u5728\u6709 0 \u4f4d\u5269\u4f59\u53ef\u7528\u5185\u5b58\u3002\n- \u7b2c 3 \u79d2\uff0c\u7a0b\u5e8f\u610f\u5916\u9000\u51fa\uff0c\u4e24\u4e2a\u5185\u5b58\u6761\u5206\u522b\u6709 1 \u4f4d\u548c 0 \u4f4d\u5269\u4f59\u53ef\u7528\u5185\u5b58\u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1amemory1 = 8, memory2 = 11\n\u8f93\u51fa\uff1a[6,0,4]\n\u89e3\u91ca\uff1a\u5185\u5b58\u5206\u914d\u5982\u4e0b\uff1a\n- \u7b2c 1 \u79d2\uff0c\u5185\u5b58\u6761 2 \u88ab\u5360\u7528 1 \u4f4d\u5185\u5b58\uff0c\u5185\u5b58\u6761 2 \u73b0\u5728\u6709 10 \u4f4d\u5269\u4f59\u53ef\u7528\u5185\u5b58\u3002\n- \u7b2c 2 \u79d2\uff0c\u5185\u5b58\u6761 2 \u88ab\u5360\u7528 2 \u4f4d\u5185\u5b58\uff0c\u5185\u5b58\u6761 2 \u73b0\u5728\u6709 8 \u4f4d\u5269\u4f59\u53ef\u7528\u5185\u5b58\u3002\n- \u7b2c 3 \u79d2\uff0c\u5185\u5b58\u6761 1 \u88ab\u5360\u7528 3 \u4f4d\u5185\u5b58\uff0c\u5185\u5b58\u6761 1 \u73b0\u5728\u6709 5 \u4f4d\u5269\u4f59\u53ef\u7528\u5185\u5b58\u3002\n- \u7b2c 4 \u79d2\uff0c\u5185\u5b58\u6761 2 \u88ab\u5360\u7528 4 \u4f4d\u5185\u5b58\uff0c\u5185\u5b58\u6761 2 \u73b0\u5728\u6709 4 \u4f4d\u5269\u4f59\u53ef\u7528\u5185\u5b58\u3002\n- \u7b2c 5 \u79d2\uff0c\u5185\u5b58\u6761 1 \u88ab\u5360\u7528 5 \u4f4d\u5185\u5b58\uff0c\u5185\u5b58\u6761 1 \u73b0\u5728\u6709 0 \u4f4d\u5269\u4f59\u53ef\u7528\u5185\u5b58\u3002\n- \u7b2c 6 \u79d2\uff0c\u7a0b\u5e8f\u610f\u5916\u9000\u51fa\uff0c\u4e24\u4e2a\u5185\u5b58\u6761\u5206\u522b\u6709 0 \u4f4d\u548c 4 \u4f4d\u5269\u4f59\u53ef\u7528\u5185\u5b58\u3002\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n0 <= memory1, memory2 <= 231 - 1
A sentence is a list of words that are separated by a single space with no leading or trailing spaces. Each word consists of lowercase and uppercase English letters.
\r\n\r\nA sentence can be shuffled by appending the 1-indexed word position to each word then rearranging the words in the sentence.
\r\n\r\n"This is a sentence"
can be shuffled as "sentence4 a3 is2 This1"
or "is2 sentence4 This1 a3"
.Given a shuffled sentence s
containing no more than 9
words, reconstruct and return the original sentence.
\r\n
Example 1:
\r\n\r\n\r\nInput: s = "is2 sentence4 This1 a3"\r\nOutput: "This is a sentence"\r\nExplanation: Sort the words in s to their original positions "This1 is2 a3 sentence4", then remove the numbers.\r\n\r\n\r\n
Example 2:
\r\n\r\n\r\nInput: s = "Myself2 Me1 I4 and3"\r\nOutput: "Me Myself and I"\r\nExplanation: Sort the words in s to their original positions "Me1 Myself2 and3 I4", then remove the numbers.\r\n\r\n\r\n
\r\n
Constraints:
\r\n\r\n2 <= s.length <= 200
s
consists of lowercase and uppercase English letters, spaces, and digits from 1
to 9
.s
is between 1
and 9
.s
are separated by a single space.s
contains no leading or trailing spaces.\u4e00\u4e2a \u53e5\u5b50\u00a0\u6307\u7684\u662f\u4e00\u4e2a\u5e8f\u5217\u7684\u5355\u8bcd\u7528\u5355\u4e2a\u7a7a\u683c\u8fde\u63a5\u8d77\u6765\uff0c\u4e14\u5f00\u5934\u548c\u7ed3\u5c3e\u6ca1\u6709\u4efb\u4f55\u7a7a\u683c\u3002\u6bcf\u4e2a\u5355\u8bcd\u90fd\u53ea\u5305\u542b\u5c0f\u5199\u6216\u5927\u5199\u82f1\u6587\u5b57\u6bcd\u3002
\n\n\u6211\u4eec\u53ef\u4ee5\u7ed9\u4e00\u4e2a\u53e5\u5b50\u6dfb\u52a0 \u4ece 1 \u5f00\u59cb\u7684\u5355\u8bcd\u4f4d\u7f6e\u7d22\u5f15 \uff0c\u5e76\u4e14\u5c06\u53e5\u5b50\u4e2d\u6240\u6709\u5355\u8bcd\u00a0\u6253\u4e71\u987a\u5e8f\u00a0\u3002
\n\n\"This is a sentence\"
\u00a0\u53ef\u4ee5\u88ab\u6253\u4e71\u987a\u5e8f\u5f97\u5230\u00a0\"sentence4 a3 is2 This1\"
\u00a0\u6216\u8005\u00a0\"is2 sentence4 This1 a3\"
\u00a0\u3002\u7ed9\u4f60\u4e00\u4e2a \u6253\u4e71\u987a\u5e8f\u00a0\u7684\u53e5\u5b50\u00a0s
\u00a0\uff0c\u5b83\u5305\u542b\u7684\u5355\u8bcd\u4e0d\u8d85\u8fc7\u00a09
\u00a0\u4e2a\uff0c\u8bf7\u4f60\u91cd\u65b0\u6784\u9020\u5e76\u5f97\u5230\u539f\u672c\u987a\u5e8f\u7684\u53e5\u5b50\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1as = \"is2 sentence4 This1 a3\"\n\u8f93\u51fa\uff1a\"This is a sentence\"\n\u89e3\u91ca\uff1a\u5c06 s \u4e2d\u7684\u5355\u8bcd\u6309\u7167\u521d\u59cb\u4f4d\u7f6e\u6392\u5e8f\uff0c\u5f97\u5230 \"This1 is2 a3 sentence4\" \uff0c\u7136\u540e\u5220\u9664\u6570\u5b57\u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1as = \"Myself2 Me1 I4 and3\"\n\u8f93\u51fa\uff1a\"Me Myself and I\"\n\u89e3\u91ca\uff1a\u5c06 s \u4e2d\u7684\u5355\u8bcd\u6309\u7167\u521d\u59cb\u4f4d\u7f6e\u6392\u5e8f\uff0c\u5f97\u5230 \"Me1 Myself2 and3 I4\" \uff0c\u7136\u540e\u5220\u9664\u6570\u5b57\u3002\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n2 <= s.length <= 200
s
\u00a0\u53ea\u5305\u542b\u5c0f\u5199\u548c\u5927\u5199\u82f1\u6587\u5b57\u6bcd\u3001\u7a7a\u683c\u4ee5\u53ca\u4ece\u00a01
\u00a0\u5230\u00a09
\u00a0\u7684\u6570\u5b57\u3002s
\u00a0\u4e2d\u5355\u8bcd\u6570\u76ee\u4e3a\u00a01
\u00a0\u5230\u00a09
\u00a0\u4e2a\u3002s
\u00a0\u4e2d\u7684\u5355\u8bcd\u7531\u5355\u4e2a\u7a7a\u683c\u5206\u9694\u3002s
\u00a0\u4e0d\u5305\u542b\u4efb\u4f55\u524d\u5bfc\u6216\u8005\u540e\u7f00\u7a7a\u683c\u3002There are m
boys and n
girls in a class attending an upcoming party.
You are given an m x n
integer matrix grid
, where grid[i][j]
equals 0
or 1
. If grid[i][j] == 1
, then that means the ith
boy can invite the jth
girl to the party. A boy can invite at most one girl, and a girl can accept at most one invitation from a boy.
Return the maximum possible number of accepted invitations.
\n\n\n
Example 1:
\n\n\nInput: grid = [[1,1,1],\n [1,0,1],\n [0,0,1]]\nOutput: 3\nExplanation: The invitations are sent as follows:\n- The 1st boy invites the 2nd girl.\n- The 2nd boy invites the 1st girl.\n- The 3rd boy invites the 3rd girl.\n\n
Example 2:
\n\n\nInput: grid = [[1,0,1,0],\n [1,0,0,0],\n [0,0,1,0],\n [1,1,1,0]]\nOutput: 3\nExplanation: The invitations are sent as follows:\n-The 1st boy invites the 3rd girl.\n-The 2nd boy invites the 1st girl.\n-The 3rd boy invites no one.\n-The 4th boy invites the 2nd girl.\n\n
\n
Constraints:
\n\ngrid.length == m
grid[i].length == n
1 <= m, n <= 200
grid[i][j]
is either 0
or 1
.You want to build n
new buildings in a city. The new buildings will be built in a line and are labeled from 1
to n
.
However, there are city restrictions on the heights of the new buildings:
\n\n0
.1
.Additionally, there are city restrictions on the maximum height of specific buildings. These restrictions are given as a 2D integer array restrictions
where restrictions[i] = [idi, maxHeighti]
indicates that building idi
must have a height less than or equal to maxHeighti
.
It is guaranteed that each building will appear at most once in restrictions
, and building 1
will not be in restrictions
.
Return the maximum possible height of the tallest building.
\n\n\n
Example 1:
\n\nInput: n = 5, restrictions = [[2,1],[4,1]]\nOutput: 2\nExplanation: The green area in the image indicates the maximum allowed height for each building.\nWe can build the buildings with heights [0,1,2,1,2], and the tallest building has a height of 2.\n\n
Example 2:
\n\nInput: n = 6, restrictions = []\nOutput: 5\nExplanation: The green area in the image indicates the maximum allowed height for each building.\nWe can build the buildings with heights [0,1,2,3,4,5], and the tallest building has a height of 5.\n\n\n
Example 3:
\n\nInput: n = 10, restrictions = [[5,3],[2,5],[7,4],[10,3]]\nOutput: 5\nExplanation: The green area in the image indicates the maximum allowed height for each building.\nWe can build the buildings with heights [0,1,2,3,3,4,4,5,4,3], and the tallest building has a height of 5.\n\n\n
\n
Constraints:
\n\n2 <= n <= 109
0 <= restrictions.length <= min(n - 1, 105)
2 <= idi <= n
idi
is unique.0 <= maxHeighti <= 109
\u5728\u4e00\u5ea7\u57ce\u5e02\u91cc\uff0c\u4f60\u9700\u8981\u5efa\u00a0n
\u00a0\u680b\u65b0\u7684\u5efa\u7b51\u3002\u8fd9\u4e9b\u65b0\u7684\u5efa\u7b51\u4f1a\u4ece 1
\u00a0\u5230 n
\u00a0\u7f16\u53f7\u6392\u6210\u4e00\u5217\u3002
\u8fd9\u5ea7\u57ce\u5e02\u5bf9\u8fd9\u4e9b\u65b0\u5efa\u7b51\u6709\u4e00\u4e9b\u89c4\u5b9a\uff1a
\n\n0
\u00a0\u30021
\u00a0\u3002\u9664\u6b64\u4ee5\u5916\uff0c\u67d0\u4e9b\u5efa\u7b51\u8fd8\u6709\u989d\u5916\u7684\u6700\u9ad8\u9ad8\u5ea6\u9650\u5236\u3002\u8fd9\u4e9b\u9650\u5236\u4f1a\u4ee5\u4e8c\u7ef4\u6574\u6570\u6570\u7ec4\u00a0restrictions
\u00a0\u7684\u5f62\u5f0f\u7ed9\u51fa\uff0c\u5176\u4e2d\u00a0restrictions[i] = [idi, maxHeighti]
\u00a0\uff0c\u8868\u793a\u5efa\u7b51\u00a0idi
\u00a0\u7684\u9ad8\u5ea6 \u4e0d\u80fd\u8d85\u8fc7\u00a0maxHeighti
\u00a0\u3002
\u9898\u76ee\u4fdd\u8bc1\u6bcf\u680b\u5efa\u7b51\u5728 restrictions
\u00a0\u4e2d\u00a0\u81f3\u591a\u51fa\u73b0\u4e00\u6b21\u00a0\uff0c\u540c\u65f6\u5efa\u7b51 1
\u00a0\u4e0d\u4f1a\u00a0\u51fa\u73b0\u5728\u00a0restrictions
\u00a0\u4e2d\u3002
\u8bf7\u4f60\u8fd4\u56de \u6700\u9ad8\u00a0\u5efa\u7b51\u80fd\u8fbe\u5230\u7684 \u6700\u9ad8\u9ad8\u5ea6\u00a0\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1an = 5, restrictions = [[2,1],[4,1]]\n\u8f93\u51fa\uff1a2\n\u89e3\u91ca\uff1a\u4e0a\u56fe\u4e2d\u7684\u7eff\u8272\u533a\u57df\u4e3a\u6bcf\u680b\u5efa\u7b51\u88ab\u5141\u8bb8\u7684\u6700\u9ad8\u9ad8\u5ea6\u3002\n\u6211\u4eec\u53ef\u4ee5\u4f7f\u5efa\u7b51\u9ad8\u5ea6\u5206\u522b\u4e3a [0,1,2,1,2] \uff0c\u6700\u9ad8\u5efa\u7b51\u7684\u9ad8\u5ea6\u4e3a 2 \u3002\n\n
\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1an = 6, restrictions = []\n\u8f93\u51fa\uff1a5\n\u89e3\u91ca\uff1a\u4e0a\u56fe\u4e2d\u7684\u7eff\u8272\u533a\u57df\u4e3a\u6bcf\u680b\u5efa\u7b51\u88ab\u5141\u8bb8\u7684\u6700\u9ad8\u9ad8\u5ea6\u3002\n\u6211\u4eec\u53ef\u4ee5\u4f7f\u5efa\u7b51\u9ad8\u5ea6\u5206\u522b\u4e3a [0,1,2,3,4,5] \uff0c\u6700\u9ad8\u5efa\u7b51\u7684\u9ad8\u5ea6\u4e3a 5 \u3002\n\n\n
\u793a\u4f8b 3\uff1a
\n\n\u8f93\u5165\uff1an = 10, restrictions = [[5,3],[2,5],[7,4],[10,3]]\n\u8f93\u51fa\uff1a5\n\u89e3\u91ca\uff1a\u4e0a\u56fe\u4e2d\u7684\u7eff\u8272\u533a\u57df\u4e3a\u6bcf\u680b\u5efa\u7b51\u88ab\u5141\u8bb8\u7684\u6700\u9ad8\u9ad8\u5ea6\u3002\n\u6211\u4eec\u53ef\u4ee5\u4f7f\u5efa\u7b51\u9ad8\u5ea6\u5206\u522b\u4e3a [0,1,2,3,3,4,4,5,4,3] \uff0c\u6700\u9ad8\u5efa\u7b51\u7684\u9ad8\u5ea6\u4e3a 5 \u3002\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n2 <= n <= 109
0 <= restrictions.length <= min(n - 1, 105)
2 <= idi <= n
idi
\u00a0\u662f \u552f\u4e00\u7684\u00a0\u30020 <= maxHeighti <= 109
A string is considered beautiful if it satisfies the following conditions:
\n\n'a'
, 'e'
, 'i'
, 'o'
, 'u'
) must appear at least once in it.'a'
s before 'e'
s, all 'e'
s before 'i'
s, etc.).For example, strings "aeiou"
and "aaaaaaeiiiioou"
are considered beautiful, but "uaeio"
, "aeoiu"
, and "aaaeeeooo"
are not beautiful.
Given a string word
consisting of English vowels, return the length of the longest beautiful substring of word
. If no such substring exists, return 0
.
A substring is a contiguous sequence of characters in a string.
\n\n\n
Example 1:
\n\n\nInput: word = "aeiaaioaaaaeiiiiouuuooaauuaeiu"\nOutput: 13\nExplanation: The longest beautiful substring in word is "aaaaeiiiiouuu" of length 13.\n\n
Example 2:
\n\n\nInput: word = "aeeeiiiioooauuuaeiou"\nOutput: 5\nExplanation: The longest beautiful substring in word is "aeiou" of length 5.\n\n\n
Example 3:
\n\n\nInput: word = "a"\nOutput: 0\nExplanation: There is no beautiful substring, so return 0.\n\n\n
\n
Constraints:
\n\n1 <= word.length <= 5 * 105
word
consists of characters 'a'
, 'e'
, 'i'
, 'o'
, and 'u'
.\u5f53\u4e00\u4e2a\u5b57\u7b26\u4e32\u6ee1\u8db3\u5982\u4e0b\u6761\u4ef6\u65f6\uff0c\u6211\u4eec\u79f0\u5b83\u662f \u7f8e\u4e3d\u7684\u00a0\uff1a
\n\n'a'
\u00a0\uff0c'e'
\u00a0\uff0c'i'
\u00a0\uff0c'o'
\u00a0\uff0c'u'
\uff09\u90fd\u5fc5\u987b\u00a0\u81f3\u5c11\u00a0\u51fa\u73b0\u4e00\u6b21\u3002'a'
\u00a0\u90fd\u5728 'e'
\u00a0\u524d\u9762\uff0c\u6240\u6709\u7684 'e'
\u00a0\u90fd\u5728 'i'
\u00a0\u524d\u9762\uff0c\u4ee5\u6b64\u7c7b\u63a8\uff09\u6bd4\u65b9\u8bf4\uff0c\u5b57\u7b26\u4e32\u00a0\"aeiou\"
\u548c\u00a0\"aaaaaaeiiiioou\"
\u00a0\u90fd\u662f \u7f8e\u4e3d\u7684\u00a0\uff0c\u4f46\u662f\u00a0\"uaeio\"
\u00a0\uff0c\"aeoiu\"
\u00a0\u548c\u00a0\"aaaeeeooo\"
\u00a0\u4e0d\u662f\u7f8e\u4e3d\u7684\u00a0\u3002
\u7ed9\u4f60\u4e00\u4e2a\u53ea\u5305\u542b\u82f1\u6587\u5143\u97f3\u5b57\u6bcd\u7684\u5b57\u7b26\u4e32\u00a0word
\u00a0\uff0c\u8bf7\u4f60\u8fd4\u56de\u00a0word
\u4e2d \u6700\u957f\u7f8e\u4e3d\u5b50\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u00a0\u3002\u5982\u679c\u4e0d\u5b58\u5728\u8fd9\u6837\u7684\u5b50\u5b57\u7b26\u4e32\uff0c\u8bf7\u8fd4\u56de 0
\u00a0\u3002
\u5b50\u5b57\u7b26\u4e32 \u662f\u5b57\u7b26\u4e32\u4e2d\u4e00\u4e2a\u8fde\u7eed\u7684\u5b57\u7b26\u5e8f\u5217\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1aword = \"aeiaaioaaaaeiiiiouuuooaauuaeiu\"\n\u8f93\u51fa\uff1a13\n\u89e3\u91ca\uff1a\u6700\u957f\u5b50\u5b57\u7b26\u4e32\u662f \"aaaaeiiiiouuu\" \uff0c\u957f\u5ea6\u4e3a 13 \u3002\n\n
\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1aword = \"aeeeiiiioooauuuaeiou\"\n\u8f93\u51fa\uff1a5\n\u89e3\u91ca\uff1a\u6700\u957f\u5b50\u5b57\u7b26\u4e32\u662f \"aeiou\" \uff0c\u957f\u5ea6\u4e3a 5 \u3002\n\n\n
\u793a\u4f8b 3\uff1a
\n\n\n\u8f93\u5165\uff1aword = \"a\"\n\u8f93\u51fa\uff1a0\n\u89e3\u91ca\uff1a\u6ca1\u6709\u7f8e\u4e3d\u5b50\u5b57\u7b26\u4e32\uff0c\u6240\u4ee5\u8fd4\u56de 0 \u3002\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= word.length <= 5 * 105
word
\u00a0\u53ea\u5305\u542b\u5b57\u7b26\u00a0'a'
\uff0c'e'
\uff0c'i'
\uff0c'o'
\u00a0\u548c\u00a0'u'
\u00a0\u3002The frequency of an element is the number of times it occurs in an array.
\n\nYou are given an integer array nums
and an integer k
. In one operation, you can choose an index of nums
and increment the element at that index by 1
.
Return the maximum possible frequency of an element after performing at most k
operations.
\n
Example 1:
\n\n\nInput: nums = [1,2,4], k = 5\nOutput: 3\nExplanation: Increment the first element three times and the second element two times to make nums = [4,4,4].\n4 has a frequency of 3.\n\n
Example 2:
\n\n\nInput: nums = [1,4,8,13], k = 5\nOutput: 2\nExplanation: There are multiple optimal solutions:\n- Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2.\n- Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2.\n- Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2.\n\n\n
Example 3:
\n\n\nInput: nums = [3,9,6], k = 2\nOutput: 1\n\n\n
\n
Constraints:
\n\n1 <= nums.length <= 105
1 <= nums[i] <= 105
1 <= k <= 105
\u5143\u7d20\u7684 \u9891\u6570 \u662f\u8be5\u5143\u7d20\u5728\u4e00\u4e2a\u6570\u7ec4\u4e2d\u51fa\u73b0\u7684\u6b21\u6570\u3002
\n\n\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 nums
\u548c\u4e00\u4e2a\u6574\u6570 k
\u3002\u5728\u4e00\u6b65\u64cd\u4f5c\u4e2d\uff0c\u4f60\u53ef\u4ee5\u9009\u62e9 nums
\u7684\u4e00\u4e2a\u4e0b\u6807\uff0c\u5e76\u5c06\u8be5\u4e0b\u6807\u5bf9\u5e94\u5143\u7d20\u7684\u503c\u589e\u52a0 1
\u3002
\u6267\u884c\u6700\u591a k
\u6b21\u64cd\u4f5c\u540e\uff0c\u8fd4\u56de\u6570\u7ec4\u4e2d\u6700\u9ad8\u9891\u5143\u7d20\u7684 \u6700\u5927\u53ef\u80fd\u9891\u6570 \u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1anums = [1,2,4], k = 5\n\u8f93\u51fa\uff1a3\n\u89e3\u91ca\uff1a\u5bf9\u7b2c\u4e00\u4e2a\u5143\u7d20\u6267\u884c 3 \u6b21\u9012\u589e\u64cd\u4f5c\uff0c\u5bf9\u7b2c\u4e8c\u4e2a\u5143\u7d20\u6267 2 \u6b21\u9012\u589e\u64cd\u4f5c\uff0c\u6b64\u65f6 nums = [4,4,4] \u3002\n4 \u662f\u6570\u7ec4\u4e2d\u6700\u9ad8\u9891\u5143\u7d20\uff0c\u9891\u6570\u662f 3 \u3002\n\n
\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1anums = [1,4,8,13], k = 5\n\u8f93\u51fa\uff1a2\n\u89e3\u91ca\uff1a\u5b58\u5728\u591a\u79cd\u6700\u4f18\u89e3\u51b3\u65b9\u6848\uff1a\n- \u5bf9\u7b2c\u4e00\u4e2a\u5143\u7d20\u6267\u884c 3 \u6b21\u9012\u589e\u64cd\u4f5c\uff0c\u6b64\u65f6 nums = [4,4,8,13] \u30024 \u662f\u6570\u7ec4\u4e2d\u6700\u9ad8\u9891\u5143\u7d20\uff0c\u9891\u6570\u662f 2 \u3002\n- \u5bf9\u7b2c\u4e8c\u4e2a\u5143\u7d20\u6267\u884c 4 \u6b21\u9012\u589e\u64cd\u4f5c\uff0c\u6b64\u65f6 nums = [1,8,8,13] \u30028 \u662f\u6570\u7ec4\u4e2d\u6700\u9ad8\u9891\u5143\u7d20\uff0c\u9891\u6570\u662f 2 \u3002\n- \u5bf9\u7b2c\u4e09\u4e2a\u5143\u7d20\u6267\u884c 5 \u6b21\u9012\u589e\u64cd\u4f5c\uff0c\u6b64\u65f6 nums = [1,4,13,13] \u300213 \u662f\u6570\u7ec4\u4e2d\u6700\u9ad8\u9891\u5143\u7d20\uff0c\u9891\u6570\u662f 2 \u3002\n\n\n
\u793a\u4f8b 3\uff1a
\n\n\n\u8f93\u5165\uff1anums = [3,9,6], k = 2\n\u8f93\u51fa\uff1a1\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= nums.length <= 105
1 <= nums[i] <= 105
1 <= k <= 105
Given an integer n
(in base 10
) and a base k
, return the sum of the digits of n
after converting n
from base 10
to base k
.
After converting, each digit should be interpreted as a base 10
number, and the sum should be returned in base 10
.
\n
Example 1:
\n\n\nInput: n = 34, k = 6\nOutput: 9\nExplanation: 34 (base 10) expressed in base 6 is 54. 5 + 4 = 9.\n\n\n
Example 2:
\n\n\nInput: n = 10, k = 10\nOutput: 1\nExplanation: n is already in base 10. 1 + 0 = 1.\n\n\n
\n
Constraints:
\n\n1 <= n <= 100
2 <= k <= 10
\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570 n
\uff0810
\u8fdb\u5236\uff09\u548c\u4e00\u4e2a\u57fa\u6570 k
\uff0c\u8bf7\u4f60\u5c06 n
\u4ece 10
\u8fdb\u5236\u8868\u793a\u8f6c\u6362\u4e3a k
\u8fdb\u5236\u8868\u793a\uff0c\u8ba1\u7b97\u5e76\u8fd4\u56de\u8f6c\u6362\u540e\u5404\u4f4d\u6570\u5b57\u7684 \u603b\u548c \u3002
\u8f6c\u6362\u540e\uff0c\u5404\u4f4d\u6570\u5b57\u5e94\u5f53\u89c6\u4f5c\u662f 10
\u8fdb\u5236\u6570\u5b57\uff0c\u4e14\u5b83\u4eec\u7684\u603b\u548c\u4e5f\u5e94\u5f53\u6309 10
\u8fdb\u5236\u8868\u793a\u8fd4\u56de\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1an = 34, k = 6\n\u8f93\u51fa\uff1a9\n\u89e3\u91ca\uff1a34 (10 \u8fdb\u5236) \u5728 6 \u8fdb\u5236\u4e0b\u8868\u793a\u4e3a 54 \u30025 + 4 = 9 \u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1an = 10, k = 10\n\u8f93\u51fa\uff1a1\n\u89e3\u91ca\uff1an \u672c\u8eab\u5c31\u662f 10 \u8fdb\u5236\u3002 1 + 0 = 1 \u3002\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= n <= 100
2 <= k <= 10
Table: Contests
\n+--------------+------+\n| Column Name | Type |\n+--------------+------+\n| contest_id | int |\n| gold_medal | int |\n| silver_medal | int |\n| bronze_medal | int |\n+--------------+------+\ncontest_id is the primary key for this table.\nThis table contains the LeetCode contest ID and the user IDs of the gold, silver, and bronze medalists.\nIt is guaranteed that any consecutive contests have consecutive IDs and that no ID is skipped.\n\n
\n\n
Table: Users
\n+-------------+---------+\n| Column Name | Type |\n+-------------+---------+\n| user_id | int |\n| mail | varchar |\n| name | varchar |\n+-------------+---------+\nuser_id is the primary key for this table.\nThis table contains information about the users.\n\n\n
\n\n
Write an SQL query to report the name
and the mail
of all interview candidates. A user is an interview candidate if at least one of these two conditions is true:
Return the result table in any order.
\n\nThe query result format is in the following example:
\n\n\n\n
\nContests table:\n+------------+------------+--------------+--------------+\n| contest_id | gold_medal | silver_medal | bronze_medal |\n+------------+------------+--------------+--------------+\n| 190 | 1 | 5 | 2 |\n| 191 | 2 | 3 | 5 |\n| 192 | 5 | 2 | 3 |\n| 193 | 1 | 3 | 5 |\n| 194 | 4 | 5 | 2 |\n| 195 | 4 | 2 | 1 |\n| 196 | 1 | 5 | 2 |\n+------------+------------+--------------+--------------+\n\nUsers table:\n+---------+--------------------+-------+\n| user_id | mail | name |\n+---------+--------------------+-------+\n| 1 | sarah@leetcode.com | Sarah |\n| 2 | bob@leetcode.com | Bob |\n| 3 | alice@leetcode.com | Alice |\n| 4 | hercy@leetcode.com | Hercy |\n| 5 | quarz@leetcode.com | Quarz |\n+---------+--------------------+-------+\n\nResult table:\n+-------+--------------------+\n| name | mail |\n+-------+--------------------+\n| Sarah | sarah@leetcode.com |\n| Bob | bob@leetcode.com |\n| Alice | alice@leetcode.com |\n| Quarz | quarz@leetcode.com |\n+-------+--------------------+\n\nSarah won 3 gold medals (190, 193, and 196), so we include her in the result table.\nBob won a medal in 3 consecutive contests (190, 191, and 192), so we include him in the result table.\n - Note that he also won a medal in 3 other consecutive contests (194, 195, and 196).\nAlice won a medal in 3 consecutive contests (191, 192, and 193), so we include her in the result table.\nQuarz won a medal in 5 consecutive contests (190, 191, 192, 193, and 194), so we include them in the result table.\n\n\n
\n
Follow up:
\n\nn
or more consecutive contests"? How would you change your solution to get the interview candidates? Imagine that n
is the parameter of a stored procedure.The XOR sum of a list is the bitwise XOR
of all its elements. If the list only contains one element, then its XOR sum will be equal to this element.
[1,2,3,4]
is equal to 1 XOR 2 XOR 3 XOR 4 = 4
, and the XOR sum of [3]
is equal to 3
.You are given two 0-indexed arrays arr1
and arr2
that consist only of non-negative integers.
Consider the list containing the result of arr1[i] AND arr2[j]
(bitwise AND
) for every (i, j)
pair where 0 <= i < arr1.length
and 0 <= j < arr2.length
.
Return the XOR sum of the aforementioned list.
\n\n\n
Example 1:
\n\n\nInput: arr1 = [1,2,3], arr2 = [6,5]\nOutput: 0\nExplanation: The list = [1 AND 6, 1 AND 5, 2 AND 6, 2 AND 5, 3 AND 6, 3 AND 5] = [0,1,2,0,2,1].\nThe XOR sum = 0 XOR 1 XOR 2 XOR 0 XOR 2 XOR 1 = 0.\n\n\n
Example 2:
\n\n\nInput: arr1 = [12], arr2 = [4]\nOutput: 4\nExplanation: The list = [12 AND 4] = [4]. The XOR sum = 4.\n\n\n
\n
Constraints:
\n\n1 <= arr1.length, arr2.length <= 105
0 <= arr1[i], arr2[j] <= 109
\u5217\u8868\u7684 \u5f02\u6216\u548c\uff08XOR sum\uff09\u6307\u5bf9\u6240\u6709\u5143\u7d20\u8fdb\u884c\u6309\u4f4d XOR
\u8fd0\u7b97\u7684\u7ed3\u679c\u3002\u5982\u679c\u5217\u8868\u4e2d\u4ec5\u6709\u4e00\u4e2a\u5143\u7d20\uff0c\u90a3\u4e48\u5176 \u5f02\u6216\u548c \u5c31\u7b49\u4e8e\u8be5\u5143\u7d20\u3002
[1,2,3,4]
\u7684 \u5f02\u6216\u548c \u7b49\u4e8e 1 XOR 2 XOR 3 XOR 4 = 4
\uff0c\u800c [3]
\u7684 \u5f02\u6216\u548c \u7b49\u4e8e 3
\u3002\u7ed9\u4f60\u4e24\u4e2a\u4e0b\u6807 \u4ece 0 \u5f00\u59cb \u8ba1\u6570\u7684\u6570\u7ec4 arr1
\u548c arr2
\uff0c\u4e24\u6570\u7ec4\u5747\u7531\u975e\u8d1f\u6574\u6570\u7ec4\u6210\u3002
\u6839\u636e\u6bcf\u4e2a\u00a0(i, j)
\u6570\u5bf9\uff0c\u6784\u9020\u4e00\u4e2a\u7531 arr1[i] AND arr2[j]
\uff08\u6309\u4f4d AND
\u8fd0\u7b97\uff09\u7ed3\u679c\u7ec4\u6210\u7684\u5217\u8868\u3002\u5176\u4e2d 0 <= i < arr1.length
\u4e14 0 <= j < arr2.length
\u3002
\u8fd4\u56de\u4e0a\u8ff0\u5217\u8868\u7684 \u5f02\u6216\u548c \u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1aarr1 = [1,2,3], arr2 = [6,5]\n\u8f93\u51fa\uff1a0\n\u89e3\u91ca\uff1a\u5217\u8868 = [1 AND 6, 1 AND 5, 2 AND 6, 2 AND 5, 3 AND 6, 3 AND 5] = [0,1,2,0,2,1] \uff0c\n\u5f02\u6216\u548c = 0 XOR 1 XOR 2 XOR 0 XOR 2 XOR 1 = 0 \u3002\n\n
\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1aarr1 = [12], arr2 = [4]\n\u8f93\u51fa\uff1a4\n\u89e3\u91ca\uff1a\u5217\u8868 = [12 AND 4] = [4] \uff0c\u5f02\u6216\u548c = 4 \u3002\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= arr1.length, arr2.length <= 105
0 <= arr1[i], arr2[j] <= 109
You are given n
\u200b\u200b\u200b\u200b\u200b\u200b tasks labeled from 0
to n - 1
represented by a 2D integer array tasks
, where tasks[i] = [enqueueTimei, processingTimei]
means that the i\u200b\u200b\u200b\u200b\u200b\u200bth
\u200b\u200b\u200b\u200b task will be available to process at enqueueTimei
and will take processingTimei
to finish processing.
You have a single-threaded CPU that can process at most one task at a time and will act in the following way:
\n\nReturn the order in which the CPU will process the tasks.
\n\n\n
Example 1:
\n\n\nInput: tasks = [[1,2],[2,4],[3,2],[4,1]]\nOutput: [0,2,3,1]\nExplanation: The events go as follows: \n- At time = 1, task 0 is available to process. Available tasks = {0}.\n- Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}.\n- At time = 2, task 1 is available to process. Available tasks = {1}.\n- At time = 3, task 2 is available to process. Available tasks = {1, 2}.\n- Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}.\n- At time = 4, task 3 is available to process. Available tasks = {1, 3}.\n- At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}.\n- At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}.\n- At time = 10, the CPU finishes task 1 and becomes idle.\n\n\n
Example 2:
\n\n\nInput: tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]]\nOutput: [4,3,2,0,1]\nExplanation: The events go as follows:\n- At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}.\n- Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}.\n- At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}.\n- At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}.\n- At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}.\n- At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}.\n- At time = 40, the CPU finishes task 1 and becomes idle.\n\n\n
\n
Constraints:
\n\ntasks.length == n
1 <= n <= 105
1 <= enqueueTimei, processingTimei <= 109
\u7ed9\u4f60\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4 tasks
\uff0c\u7528\u4e8e\u8868\u793a n
\u200b\u200b\u200b\u200b\u200b\u200b \u9879\u4ece 0
\u5230 n - 1
\u7f16\u53f7\u7684\u4efb\u52a1\u3002\u5176\u4e2d tasks[i] = [enqueueTimei, processingTimei]
\u610f\u5473\u7740\u7b2c i\u200b\u200b\u200b\u200b\u200b\u200b
\u200b\u200b\u200b\u200b \u9879\u4efb\u52a1\u5c06\u4f1a\u4e8e enqueueTimei
\u65f6\u8fdb\u5165\u4efb\u52a1\u961f\u5217\uff0c\u9700\u8981 processingTimei
\u7684\u65f6\u957f\u5b8c\u6210\u6267\u884c\u3002
\u73b0\u6709\u4e00\u4e2a\u5355\u7ebf\u7a0b CPU \uff0c\u540c\u4e00\u65f6\u95f4\u53ea\u80fd\u6267\u884c \u6700\u591a\u4e00\u9879 \u4efb\u52a1\uff0c\u8be5 CPU \u5c06\u4f1a\u6309\u7167\u4e0b\u8ff0\u65b9\u5f0f\u8fd0\u884c\uff1a
\n\n\u8fd4\u56de CPU \u5904\u7406\u4efb\u52a1\u7684\u987a\u5e8f\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1atasks = [[1,2],[2,4],[3,2],[4,1]]\n\u8f93\u51fa\uff1a[0,2,3,1]\n\u89e3\u91ca\uff1a\u4e8b\u4ef6\u6309\u4e0b\u8ff0\u6d41\u7a0b\u8fd0\u884c\uff1a \n- time = 1 \uff0c\u4efb\u52a1 0 \u8fdb\u5165\u4efb\u52a1\u961f\u5217\uff0c\u53ef\u6267\u884c\u4efb\u52a1\u9879 = {0}\n- \u540c\u6837\u5728 time = 1 \uff0c\u7a7a\u95f2\u72b6\u6001\u7684 CPU \u5f00\u59cb\u6267\u884c\u4efb\u52a1 0 \uff0c\u53ef\u6267\u884c\u4efb\u52a1\u9879 = {}\n- time = 2 \uff0c\u4efb\u52a1 1 \u8fdb\u5165\u4efb\u52a1\u961f\u5217\uff0c\u53ef\u6267\u884c\u4efb\u52a1\u9879 = {1}\n- time = 3 \uff0c\u4efb\u52a1 2 \u8fdb\u5165\u4efb\u52a1\u961f\u5217\uff0c\u53ef\u6267\u884c\u4efb\u52a1\u9879 = {1, 2}\n- \u540c\u6837\u5728 time = 3 \uff0cCPU \u5b8c\u6210\u4efb\u52a1 0 \u5e76\u5f00\u59cb\u6267\u884c\u961f\u5217\u4e2d\u7528\u65f6\u6700\u77ed\u7684\u4efb\u52a1 2 \uff0c\u53ef\u6267\u884c\u4efb\u52a1\u9879 = {1}\n- time = 4 \uff0c\u4efb\u52a1 3 \u8fdb\u5165\u4efb\u52a1\u961f\u5217\uff0c\u53ef\u6267\u884c\u4efb\u52a1\u9879 = {1, 3}\n- time = 5 \uff0cCPU \u5b8c\u6210\u4efb\u52a1 2 \u5e76\u5f00\u59cb\u6267\u884c\u961f\u5217\u4e2d\u7528\u65f6\u6700\u77ed\u7684\u4efb\u52a1 3 \uff0c\u53ef\u6267\u884c\u4efb\u52a1\u9879 = {1}\n- time = 6 \uff0cCPU \u5b8c\u6210\u4efb\u52a1 3 \u5e76\u5f00\u59cb\u6267\u884c\u4efb\u52a1 1 \uff0c\u53ef\u6267\u884c\u4efb\u52a1\u9879 = {}\n- time = 10 \uff0cCPU \u5b8c\u6210\u4efb\u52a1 1 \u5e76\u8fdb\u5165\u7a7a\u95f2\u72b6\u6001\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1atasks = [[7,10],[7,12],[7,5],[7,4],[7,2]]\n\u8f93\u51fa\uff1a[4,3,2,0,1]\n\u89e3\u91ca\uff1a\u4e8b\u4ef6\u6309\u4e0b\u8ff0\u6d41\u7a0b\u8fd0\u884c\uff1a \n- time = 7 \uff0c\u6240\u6709\u4efb\u52a1\u540c\u65f6\u8fdb\u5165\u4efb\u52a1\u961f\u5217\uff0c\u53ef\u6267\u884c\u4efb\u52a1\u9879 = {0,1,2,3,4}\n- \u540c\u6837\u5728 time = 7 \uff0c\u7a7a\u95f2\u72b6\u6001\u7684 CPU \u5f00\u59cb\u6267\u884c\u4efb\u52a1 4 \uff0c\u53ef\u6267\u884c\u4efb\u52a1\u9879 = {0,1,2,3}\n- time = 9 \uff0cCPU \u5b8c\u6210\u4efb\u52a1 4 \u5e76\u5f00\u59cb\u6267\u884c\u4efb\u52a1 3 \uff0c\u53ef\u6267\u884c\u4efb\u52a1\u9879 = {0,1,2}\n- time = 13 \uff0cCPU \u5b8c\u6210\u4efb\u52a1 3 \u5e76\u5f00\u59cb\u6267\u884c\u4efb\u52a1 2 \uff0c\u53ef\u6267\u884c\u4efb\u52a1\u9879 = {0,1}\n- time = 18 \uff0cCPU \u5b8c\u6210\u4efb\u52a1 2 \u5e76\u5f00\u59cb\u6267\u884c\u4efb\u52a1 0 \uff0c\u53ef\u6267\u884c\u4efb\u52a1\u9879 = {1}\n- time = 28 \uff0cCPU \u5b8c\u6210\u4efb\u52a1 0 \u5e76\u5f00\u59cb\u6267\u884c\u4efb\u52a1 1 \uff0c\u53ef\u6267\u884c\u4efb\u52a1\u9879 = {}\n- time = 40 \uff0cCPU \u5b8c\u6210\u4efb\u52a1 1 \u5e76\u8fdb\u5165\u7a7a\u95f2\u72b6\u6001\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\ntasks.length == n
1 <= n <= 105
1 <= enqueueTimei, processingTimei <= 109
It is a sweltering summer day, and a boy wants to buy some ice cream bars.
\r\n\r\nAt the store, there are n
ice cream bars. You are given an array costs
of length n
, where costs[i]
is the price of the ith
ice cream bar in coins. The boy initially has coins
coins to spend, and he wants to buy as many ice cream bars as possible.
Return the maximum number of ice cream bars the boy can buy with coins
coins.
Note: The boy can buy the ice cream bars in any order.
\r\n\r\n\r\n
Example 1:
\r\n\r\n\r\nInput: costs = [1,3,2,4,1], coins = 7\r\nOutput: 4\r\nExplanation: The boy can buy ice cream bars at indices 0,1,2,4 for a total price of 1 + 3 + 2 + 1 = 7.\r\n\r\n\r\n
Example 2:
\r\n\r\n\r\nInput: costs = [10,6,8,7,7,8], coins = 5\r\nOutput: 0\r\nExplanation: The boy cannot afford any of the ice cream bars.\r\n\r\n\r\n
Example 3:
\r\n\r\n\r\nInput: costs = [1,6,3,1,2,5], coins = 20\r\nOutput: 6\r\nExplanation: The boy can buy all the ice cream bars for a total price of 1 + 6 + 3 + 1 + 2 + 5 = 18.\r\n\r\n\r\n
\r\n
Constraints:
\r\n\r\ncosts.length == n
1 <= n <= 105
1 <= costs[i] <= 105
1 <= coins <= 108
\u590f\u65e5\u708e\u708e\uff0c\u5c0f\u7537\u5b69 Tony \u60f3\u4e70\u4e00\u4e9b\u96ea\u7cd5\u6d88\u6d88\u6691\u3002
\n\n\u5546\u5e97\u4e2d\u65b0\u5230 n
\u652f\u96ea\u7cd5\uff0c\u7528\u957f\u5ea6\u4e3a n
\u7684\u6570\u7ec4 costs
\u8868\u793a\u96ea\u7cd5\u7684\u5b9a\u4ef7\uff0c\u5176\u4e2d costs[i]
\u8868\u793a\u7b2c i
\u652f\u96ea\u7cd5\u7684\u73b0\u91d1\u4ef7\u683c\u3002Tony \u4e00\u5171\u6709 coins
\u73b0\u91d1\u53ef\u4ee5\u7528\u4e8e\u6d88\u8d39\uff0c\u4ed6\u60f3\u8981\u4e70\u5c3d\u53ef\u80fd\u591a\u7684\u96ea\u7cd5\u3002
\u7ed9\u4f60\u4ef7\u683c\u6570\u7ec4 costs
\u548c\u73b0\u91d1\u91cf coins
\uff0c\u8bf7\u4f60\u8ba1\u7b97\u5e76\u8fd4\u56de Tony \u7528 coins
\u73b0\u91d1\u80fd\u591f\u4e70\u5230\u7684\u96ea\u7cd5\u7684 \u6700\u5927\u6570\u91cf \u3002
\u6ce8\u610f\uff1aTony \u53ef\u4ee5\u6309\u4efb\u610f\u987a\u5e8f\u8d2d\u4e70\u96ea\u7cd5\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1acosts = [1,3,2,4,1], coins = 7\n\u8f93\u51fa\uff1a4\n\u89e3\u91ca\uff1aTony \u53ef\u4ee5\u4e70\u4e0b\u6807\u4e3a 0\u30011\u30012\u30014 \u7684\u96ea\u7cd5\uff0c\u603b\u4ef7\u4e3a 1 + 3 + 2 + 1 = 7\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1acosts = [10,6,8,7,7,8], coins = 5\n\u8f93\u51fa\uff1a0\n\u89e3\u91ca\uff1aTony \u6ca1\u6709\u8db3\u591f\u7684\u94b1\u4e70\u4efb\u4f55\u4e00\u652f\u96ea\u7cd5\u3002\n\n\n
\u793a\u4f8b 3\uff1a
\n\n\u8f93\u5165\uff1acosts = [1,6,3,1,2,5], coins = 20\n\u8f93\u51fa\uff1a6\n\u89e3\u91ca\uff1aTony \u53ef\u4ee5\u4e70\u4e0b\u6240\u6709\u7684\u96ea\u7cd5\uff0c\u603b\u4ef7\u4e3a 1 + 6 + 3 + 1 + 2 + 5 = 18 \u3002\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\ncosts.length == n
1 <= n <= 105
1 <= costs[i] <= 105
1 <= coins <= 108
A pangram is a sentence where every letter of the English alphabet appears at least once.
\n\nGiven a string sentence
containing only lowercase English letters, return true
if sentence
is a pangram, or false
otherwise.
\n
Example 1:
\n\n\nInput: sentence = "thequickbrownfoxjumpsoverthelazydog"\nOutput: true\nExplanation: sentence contains at least one of every letter of the English alphabet.\n\n\n
Example 2:
\n\n\nInput: sentence = "leetcode"\nOutput: false\n\n\n
\n
Constraints:
\n\n1 <= sentence.length <= 1000
sentence
consists of lowercase English letters.\u5168\u5b57\u6bcd\u53e5 \u6307\u5305\u542b\u82f1\u8bed\u5b57\u6bcd\u8868\u4e2d\u6bcf\u4e2a\u5b57\u6bcd\u81f3\u5c11\u4e00\u6b21\u7684\u53e5\u5b50\u3002
\n\n\u7ed9\u4f60\u4e00\u4e2a\u4ec5\u7531\u5c0f\u5199\u82f1\u6587\u5b57\u6bcd\u7ec4\u6210\u7684\u5b57\u7b26\u4e32 sentence
\uff0c\u8bf7\u4f60\u5224\u65ad\u00a0sentence
\u662f\u5426\u4e3a \u5168\u5b57\u6bcd\u53e5 \u3002
\u5982\u679c\u662f\uff0c\u8fd4\u56de true
\uff1b\u5426\u5219\uff0c\u8fd4\u56de false
\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1asentence = \"thequickbrownfoxjumpsoverthelazydog\"\n\u8f93\u51fa\uff1atrue\n\u89e3\u91ca\uff1asentence
\u5305\u542b\u82f1\u8bed\u5b57\u6bcd\u8868\u4e2d\u6bcf\u4e2a\u5b57\u6bcd\u81f3\u5c11\u4e00\u6b21\u3002\n
\n\n\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1asentence = \"leetcode\"\n\u8f93\u51fa\uff1afalse\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= sentence.length <= 1000
sentence
\u7531\u5c0f\u5199\u82f1\u8bed\u5b57\u6bcd\u7ec4\u6210This is an interactive problem.
\n\nThere is a robot in a hidden grid, and you are trying to get it from its starting cell to the target cell in this grid. The grid is of size m x n
, and each cell in the grid is either empty or blocked. It is guaranteed that the starting cell and the target cell are different, and neither of them is blocked.
Each cell has a cost that you need to pay each time you move to the cell. The starting cell's cost is not applied before the robot moves.
\n\nYou want to find the minimum total cost to move the robot to the target cell. However, you do not know the grid's dimensions, the starting cell, nor the target cell. You are only allowed to ask queries to the GridMaster
object.
The GridMaster
class has the following functions:
boolean canMove(char direction)
Returns true
if the robot can move in that direction. Otherwise, it returns false
.int move(char direction)
Moves the robot in that direction and returns the cost of moving to that cell. If this move would move the robot to a blocked cell or off the grid, the move will be ignored, the robot will remain in the same position, and the function will return -1
.boolean isTarget()
Returns true
if the robot is currently on the target cell. Otherwise, it returns false
.Note that direction
in the above functions should be a character from {'U','D','L','R'}
, representing the directions up, down, left, and right, respectively.
Return the minimum total cost to get the robot from its initial starting cell to the target cell. If there is no valid path between the cells, return -1
.
Custom testing:
\n\nThe test input is read as a 2D matrix grid
of size m x n
and four integers r1
, c1
, r2
, and c2
where:
grid[i][j] == 0
indicates that the cell (i, j)
is blocked.grid[i][j] >= 1
indicates that the cell (i, j)
is empty and grid[i][j]
is the cost to move to that cell.(r1, c1)
is the starting cell of the robot.(r2, c2)
is the target cell of the robot.Remember that you will not have this information in your code.
\n\n\n
Example 1:
\n\n\nInput: grid = [[2,3],[1,1]], r1 = 0, c1 = 1, r2 = 1, c2 = 0\nOutput: 2\nExplanation: One possible interaction is described below:\nThe robot is initially standing on cell (0, 1), denoted by the 3.\n- master.canMove('U') returns false.\n- master.canMove('D') returns true.\n- master.canMove('L') returns true.\n- master.canMove('R') returns false.\n- master.move('L') moves the robot to the cell (0, 0) and returns 2.\n- master.isTarget() returns false.\n- master.canMove('U') returns false.\n- master.canMove('D') returns true.\n- master.canMove('L') returns false.\n- master.canMove('R') returns true.\n- master.move('D') moves the robot to the cell (1, 0) and returns 1.\n- master.isTarget() returns true.\n- master.move('L') doesn't move the robot and returns -1.\n- master.move('R') moves the robot to the cell (1, 1) and returns 1.\nWe now know that the target is the cell (0, 1), and the minimum total cost to reach it is 2.\n\n
Example 2:
\n\n\nInput: grid = [[0,3,1],[3,4,2],[1,2,0]], r1 = 2, c1 = 0, r2 = 0, c2 = 2\nOutput: 9\nExplanation: The minimum cost path is (2,0) -> (2,1) -> (1,1) -> (1,2) -> (0,2).\n\n\n
Example 3:
\n\n\nInput: grid = [[1,0],[0,1]], r1 = 0, c1 = 0, r2 = 1, c2 = 1\nOutput: -1\nExplanation: There is no path from the robot to the target cell.\n\n\n
\n
Constraints:
\n\n1 <= n, m <= 100
m == grid.length
n == grid[i].length
0 <= grid[i][j] <= 100
Table: Playback
\n+-------------+------+\n| Column Name | Type |\n+-------------+------+\n| session_id | int |\n| customer_id | int |\n| start_time | int |\n| end_time | int |\n+-------------+------+\nsession_id is the primary key for this table.\ncustomer_id is the ID of the customer watching this session.\nThe session runs during the inclusive interval between start_time and end_time.\nIt is guaranteed that start_time <= end_time and that two sessions for the same customer do not intersect.\n\n
\n\n
Table: Ads
\n+-------------+------+\n| Column Name | Type |\n+-------------+------+\n| ad_id | int |\n| customer_id | int |\n| timestamp | int |\n+-------------+------+\nad_id is the primary key for this table.\ncustomer_id is the ID of the customer viewing this ad.\ntimestamp is the moment of time at which the ad was shown.\n\n\n
\n\n
Write an SQL query to report all the sessions that did not get shown any ads.
\n\nReturn the result table in any order.
\n\nThe query result format is in the following example:
\n\n\n\n
\nPlayback table:\n+------------+-------------+------------+----------+\n| session_id | customer_id | start_time | end_time |\n+------------+-------------+------------+----------+\n| 1 | 1 | 1 | 5 |\n| 2 | 1 | 15 | 23 |\n| 3 | 2 | 10 | 12 |\n| 4 | 2 | 17 | 28 |\n| 5 | 2 | 2 | 8 |\n+------------+-------------+------------+----------+\n\nAds table:\n+-------+-------------+-----------+\n| ad_id | customer_id | timestamp |\n+-------+-------------+-----------+\n| 1 | 1 | 5 |\n| 2 | 2 | 17 |\n| 3 | 2 | 20 |\n+-------+-------------+-----------+\n\nResult table:\n+------------+\n| session_id |\n+------------+\n| 2 |\n| 3 |\n| 5 |\n+------------+\nThe ad with ID 1 was shown to user 1 at time 5 while they were in session 1.\nThe ad with ID 2 was shown to user 2 at time 17 while they were in session 4.\nThe ad with ID 3 was shown to user 2 at time 20 while they were in session 4.\nWe can see that sessions 1 and 4 had at least one ad. Sessions 2, 3, and 5 did not have any ads, so we return them.\n", "content_cn": "
Table: Playback
+-------------+------+\n| Column Name | Type |\n+-------------+------+\n| session_id | int |\n| customer_id | int |\n| start_time | int |\n| end_time | int |\n+-------------+------+\n\u8be5\u8868\u4e3b\u952e\u4e3a\uff1asession_id \uff08\u5267\u96c6id\uff09\ncustomer_id \u662f\u89c2\u770b\u8be5\u5267\u96c6\u7684\u89c2\u4f17id\n\u5267\u96c6\u64ad\u653e\u65f6\u95f4\u5305\u542bstart_time\uff08\u5f00\u59cb\u65f6\u95f4\uff09 \u53ca end_time\uff08\u7ed3\u675f\u65f6\u95f4\uff09\n\u53ef\u4ee5\u4fdd\u8bc1\u7684\u662f\uff0cstart_time\uff08\u5f00\u59cb\u65f6\u95f4\uff09<= end_time\uff08\u7ed3\u675f\u65f6\u95f4\uff09\uff0c\u4e00\u4e2a\u89c2\u4f17\u89c2\u770b\u7684\u4e24\u4e2a\u5267\u96c6\u7684\u65f6\u95f4\u4e0d\u4f1a\u51fa\u73b0\u91cd\u53e0\u3002\n\n
\u00a0
\n\nTable: Ads
+-------------+------+\n| Column Name | Type |\n+-------------+------+\n| ad_id | int |\n| customer_id | int |\n| timestamp | int |\n+-------------+------+\n\u8be5\u8868\u7684\u4e3b\u952e\u4e3a\uff1aad_id\uff08\u5e7f\u544aid\uff09\ncustomer_id \u4e3a \u89c2\u770b\u5e7f\u544a\u7684\u7528\u6237id\ntimestamp \u8868\u793a\u5e7f\u544a\u51fa\u73b0\u7684\u65f6\u95f4\u70b9\n\n\n
\u00a0
\n\n\u8bf7\u67e5\u51fa\uff0c\u6240\u6709\u6ca1\u6709\u5e7f\u544a\u51fa\u73b0\u8fc7\u7684\u5267\u96c6\u3002
\n\n\u5982\u679c\u89c2\u4f17\u89c2\u770b\u4e86\u5267\u96c6\uff0c\u5e76\u4e14\u5267\u96c6\u91cc\u51fa\u73b0\u4e86\u5e7f\u544a\uff0c\u5c31\u4e00\u5b9a\u4f1a\u6709\u89c2\u4f17\u89c2\u770b\u5e7f\u544a\u7684\u8bb0\u5f55\u3002
\n\n\u8fd4\u56de\u7ed3\u679c\u6ca1\u6709\u987a\u5e8f\u8981\u6c42\u3002
\n\n\u00a0
\n\n\u793a\u4f8b\uff1a
\n\nPlayback table:\n+------------+-------------+------------+----------+\n| session_id | customer_id | start_time | end_time |\n+------------+-------------+------------+----------+\n| 1 | 1 | 1 | 5 |\n| 2 | 1 | 15 | 23 |\n| 3 | 2 | 10 | 12 |\n| 4 | 2 | 17 | 28 |\n| 5 | 2 | 2 | 8 |\n+------------+-------------+------------+----------+\n\nAds table:\n+-------+-------------+-----------+\n| ad_id | customer_id | timestamp |\n+-------+-------------+-----------+\n| 1 | 1 | 5 |\n| 2 | 2 | 17 |\n| 3 | 2 | 20 |\n+-------+-------------+-----------+\n\nResult table:\n+------------+\n| session_id |\n+------------+\n| 2 |\n| 3 |\n| 5 |\n+------------+\n\u5e7f\u544a1\u51fa\u73b0\u5728\u4e86\u5267\u96c61\u7684\u65f6\u95f4\u6bb5\uff0c\u88ab\u89c2\u4f171\u770b\u5230\u4e86\u3002\n\u5e7f\u544a2\u51fa\u73b0\u5728\u4e86\u5267\u96c64\u7684\u65f6\u95f4\u6bb5\uff0c\u88ab\u89c2\u4f172\u770b\u5230\u4e86\u3002\n\u5e7f\u544a3\u51fa\u73b0\u5728\u4e86\u5267\u96c64\u7684\u65f6\u95f4\u6bb5\uff0c\u88ab\u89c2\u4f172\u770b\u5230\u4e86\u3002\n\u6211\u4eec\u53ef\u4ee5\u5f97\u51fa\u7ed3\u8bba\uff0c\u5267\u96c61 \u30014 \u5185\uff0c\u8d77\u7801\u67091\u5904\u5e7f\u544a\u3002 \u5267\u96c62 \u30013 \u30015 \u6ca1\u6709\u5e7f\u544a\u3002\n", "tags_en": [], "tags_cn": [], "difficulty_en": "Easy", "difficulty_cn": "\u7b80\u5355", "code_snippets": [{"lang": "MySQL", "langSlug": "mysql", "code": "# Write your MySQL query statement below", "__typename": "CodeSnippetNode"}, {"lang": "MS SQL Server", "langSlug": "mssql", "code": "/* Write your T-SQL query statement below */", "__typename": "CodeSnippetNode"}, {"lang": "Oracle", "langSlug": "oraclesql", "code": "/* Write your PL/SQL query statement below */", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1809](https://leetcode-cn.com/problems/ad-free-sessions)", "[\u6ca1\u6709\u5e7f\u544a\u7684\u5267\u96c6](/solution/1800-1899/1809.Ad-Free%20Sessions/README.md)", "", "\u7b80\u5355", "\ud83d\udd12"], "md_table_row_en": ["[1809](https://leetcode.com/problems/ad-free-sessions)", "[Ad-Free Sessions](/solution/1800-1899/1809.Ad-Free%20Sessions/README_EN.md)", "", "Easy", "\ud83d\udd12"]}, {"question_id": "1957", "frontend_question_id": "1847", "paid_only": false, "paid_only_cn": false, "url_cn": "https://leetcode-cn.com/problems/closest-room", "url_en": "https://leetcode.com/problems/closest-room", "relative_path_cn": "/solution/1800-1899/1847.Closest%20Room/README.md", "relative_path_en": "/solution/1800-1899/1847.Closest%20Room/README_EN.md", "title_cn": "\u6700\u8fd1\u7684\u623f\u95f4", "title_en": "Closest Room", "question_title_slug": "closest-room", "content_en": "
There is a hotel with n
rooms. The rooms are represented by a 2D integer array rooms
where rooms[i] = [roomIdi, sizei]
denotes that there is a room with room number roomIdi
and size equal to sizei
. Each roomIdi
is guaranteed to be unique.
You are also given k
queries in a 2D array queries
where queries[j] = [preferredj, minSizej]
. The answer to the jth
query is the room number id
of a room such that:
minSizej
, andabs(id - preferredj)
is minimized, where abs(x)
is the absolute value of x
.If there is a tie in the absolute difference, then use the room with the smallest such id
. If there is no such room, the answer is -1
.
Return an array answer
of length k
where answer[j]
contains the answer to the jth
query.
\n
Example 1:
\n\n\nInput: rooms = [[2,2],[1,2],[3,2]], queries = [[3,1],[3,3],[5,2]]\nOutput: [3,-1,3]\nExplanation: The answers to the queries are as follows:\nQuery = [3,1]: Room number 3 is the closest as abs(3 - 3) = 0, and its size of 2 is at least 1. The answer is 3.\nQuery = [3,3]: There are no rooms with a size of at least 3, so the answer is -1.\nQuery = [5,2]: Room number 3 is the closest as abs(3 - 5) = 2, and its size of 2 is at least 2. The answer is 3.\n\n
Example 2:
\n\n\nInput: rooms = [[1,4],[2,3],[3,5],[4,1],[5,2]], queries = [[2,3],[2,4],[2,5]]\nOutput: [2,1,3]\nExplanation: The answers to the queries are as follows:\nQuery = [2,3]: Room number 2 is the closest as abs(2 - 2) = 0, and its size of 3 is at least 3. The answer is 2.\nQuery = [2,4]: Room numbers 1 and 3 both have sizes of at least 4. The answer is 1 since it is smaller.\nQuery = [2,5]: Room number 3 is the only room with a size of at least 5. The answer is 3.\n\n
\n
Constraints:
\n\nn == rooms.length
1 <= n <= 105
k == queries.length
1 <= k <= 104
1 <= roomIdi, preferredj <= 107
1 <= sizei, minSizej <= 107
\u4e00\u4e2a\u9152\u5e97\u91cc\u6709\u00a0n
\u00a0\u4e2a\u623f\u95f4\uff0c\u8fd9\u4e9b\u623f\u95f4\u7528\u4e8c\u7ef4\u6574\u6570\u6570\u7ec4\u00a0rooms
\u00a0\u8868\u793a\uff0c\u5176\u4e2d\u00a0rooms[i] = [roomIdi, sizei]
\u00a0\u8868\u793a\u6709\u4e00\u4e2a\u623f\u95f4\u53f7\u4e3a\u00a0roomIdi
\u00a0\u7684\u623f\u95f4\u4e14\u5b83\u7684\u9762\u79ef\u4e3a\u00a0sizei
\u00a0\u3002\u6bcf\u4e00\u4e2a\u623f\u95f4\u53f7\u00a0roomIdi
\u00a0\u4fdd\u8bc1\u662f \u72ec\u4e00\u65e0\u4e8c\u00a0\u7684\u3002
\u540c\u65f6\u7ed9\u4f60 k
\u00a0\u4e2a\u67e5\u8be2\uff0c\u7528\u4e8c\u7ef4\u6570\u7ec4\u00a0queries
\u00a0\u8868\u793a\uff0c\u5176\u4e2d\u00a0queries[j] = [preferredj, minSizej]
\u00a0\u3002\u7b2c\u00a0j
\u00a0\u4e2a\u67e5\u8be2\u7684\u7b54\u6848\u662f\u6ee1\u8db3\u5982\u4e0b\u6761\u4ef6\u7684\u623f\u95f4\u00a0id
\u00a0\uff1a
minSizej
\u00a0\uff0c\u4e14abs(id - preferredj)
\u00a0\u7684\u503c \u6700\u5c0f\u00a0\uff0c\u5176\u4e2d\u00a0abs(x)
\u00a0\u662f\u00a0x
\u00a0\u7684\u7edd\u5bf9\u503c\u3002\u5982\u679c\u5dee\u7684\u7edd\u5bf9\u503c\u6709 \u76f8\u7b49\u00a0\u7684\uff0c\u9009\u62e9 \u6700\u5c0f\u00a0\u7684\u00a0id
\u00a0\u3002\u5982\u679c \u6ca1\u6709\u6ee1\u8db3\u6761\u4ef6\u7684\u623f\u95f4\u00a0\uff0c\u7b54\u6848\u4e3a -1
\u00a0\u3002
\u8bf7\u4f60\u8fd4\u56de\u957f\u5ea6\u4e3a k
\u00a0\u7684\u6570\u7ec4\u00a0answer
\u00a0\uff0c\u5176\u4e2d\u00a0answer[j]
\u00a0\u4e3a\u7b2c j
\u00a0\u4e2a\u67e5\u8be2\u7684\u7ed3\u679c\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1arooms = [[2,2],[1,2],[3,2]], queries = [[3,1],[3,3],[5,2]]\n\u8f93\u51fa\uff1a[3,-1,3]\n\u89e3\u91ca\uff1a\u67e5\u8be2\u7684\u7b54\u6848\u5982\u4e0b\uff1a\n\u67e5\u8be2 [3,1] \uff1a\u623f\u95f4 3 \u7684\u9762\u79ef\u4e3a 2 \uff0c\u5927\u4e8e\u7b49\u4e8e 1 \uff0c\u4e14\u53f7\u7801\u662f\u6700\u63a5\u8fd1 3 \u7684\uff0c\u4e3a abs(3 - 3) = 0 \uff0c\u6240\u4ee5\u7b54\u6848\u4e3a 3 \u3002\n\u67e5\u8be2 [3,3] \uff1a\u6ca1\u6709\u623f\u95f4\u7684\u9762\u79ef\u81f3\u5c11\u4e3a 3 \uff0c\u6240\u4ee5\u7b54\u6848\u4e3a -1 \u3002\n\u67e5\u8be2 [5,2] \uff1a\u623f\u95f4 3 \u7684\u9762\u79ef\u4e3a 2 \uff0c\u5927\u4e8e\u7b49\u4e8e 2 \uff0c\u4e14\u53f7\u7801\u662f\u6700\u63a5\u8fd1 5 \u7684\uff0c\u4e3a abs(3 - 5) = 2 \uff0c\u6240\u4ee5\u7b54\u6848\u4e3a 3 \u3002\n\n
\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1arooms = [[1,4],[2,3],[3,5],[4,1],[5,2]], queries = [[2,3],[2,4],[2,5]]\n\u8f93\u51fa\uff1a[2,1,3]\n\u89e3\u91ca\uff1a\u67e5\u8be2\u7684\u7b54\u6848\u5982\u4e0b\uff1a\n\u67e5\u8be2 [2,3] \uff1a\u623f\u95f4 2 \u7684\u9762\u79ef\u4e3a 3 \uff0c\u5927\u4e8e\u7b49\u4e8e 3 \uff0c\u4e14\u53f7\u7801\u662f\u6700\u63a5\u8fd1\u7684\uff0c\u4e3a abs(2 - 2) = 0 \uff0c\u6240\u4ee5\u7b54\u6848\u4e3a 2 \u3002\n\u67e5\u8be2 [2,4] \uff1a\u623f\u95f4 1 \u548c 3 \u7684\u9762\u79ef\u90fd\u81f3\u5c11\u4e3a 4 \uff0c\u7b54\u6848\u4e3a 1 \u56e0\u4e3a\u5b83\u623f\u95f4\u7f16\u53f7\u66f4\u5c0f\u3002\n\u67e5\u8be2 [2,5] \uff1a\u623f\u95f4 3 \u662f\u552f\u4e00\u9762\u79ef\u5927\u4e8e\u7b49\u4e8e 5 \u7684\uff0c\u6240\u4ee5\u7b54\u6848\u4e3a 3 \u3002\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\nn == rooms.length
1 <= n <= 105
k == queries.length
1 <= k <= 104
1 <= roomIdi, preferredj <= 107
1 <= sizei, minSizej <= 107
You are given an array of positive integers arr
. Perform some operations (possibly none) on arr
so that it satisfies these conditions:
arr
must be 1
.1
. In other words, abs(arr[i] - arr[i - 1]) <= 1
for each i
where 1 <= i < arr.length
(0-indexed). abs(x)
is the absolute value of x
.There are 2 types of operations that you can perform any number of times:
\n\narr
to a smaller positive integer.arr
to be in any order.Return the maximum possible value of an element in arr
after performing the operations to satisfy the conditions.
\n
Example 1:
\n\n\nInput: arr = [2,2,1,2,1]\nOutput: 2\nExplanation: \nWe can satisfy the conditions by rearranging\n\narr
so it becomes[1,2,2,2,1]
.\nThe largest element inarr
is 2.\n
Example 2:
\n\n\nInput: arr = [100,1,1000]\nOutput: 3\nExplanation: \nOne possible way to satisfy the conditions is by doing the following:\n1. Rearrange\n\narr
so it becomes[1,100,1000]
.\n2. Decrease the value of the second element to 2.\n3. Decrease the value of the third element to 3.\nNowarr = [1,2,3], which
satisfies the conditions.\nThe largest element inarr is 3.
\n
Example 3:
\n\n\nInput: arr = [1,2,3,4,5]\nOutput: 5\nExplanation: The array already satisfies the conditions, and the largest element is 5.\n\n\n
\n
Constraints:
\n\n1 <= arr.length <= 105
1 <= arr[i] <= 109
\u7ed9\u4f60\u4e00\u4e2a\u6b63\u6574\u6570\u6570\u7ec4\u00a0arr
\u00a0\u3002\u8bf7\u4f60\u5bf9 arr
\u00a0\u6267\u884c\u4e00\u4e9b\u64cd\u4f5c\uff08\u4e5f\u53ef\u4ee5\u4e0d\u8fdb\u884c\u4efb\u4f55\u64cd\u4f5c\uff09\uff0c\u4f7f\u5f97\u6570\u7ec4\u6ee1\u8db3\u4ee5\u4e0b\u6761\u4ef6\uff1a
arr
\u00a0\u4e2d \u7b2c\u4e00\u4e2a\u00a0\u5143\u7d20\u5fc5\u987b\u4e3a\u00a01
\u00a0\u30021
\u00a0\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u5bf9\u4e8e\u4efb\u610f\u7684 1 <= i < arr.length
\u00a0\uff08\u6570\u7ec4\u4e0b\u6807\u4ece 0 \u5f00\u59cb\uff09\uff0c\u90fd\u6ee1\u8db3\u00a0abs(arr[i] - arr[i - 1]) <= 1
\u00a0\u3002abs(x)
\u00a0\u4e3a\u00a0x
\u00a0\u7684\u7edd\u5bf9\u503c\u3002\u4f60\u53ef\u4ee5\u6267\u884c\u4ee5\u4e0b 2 \u79cd\u64cd\u4f5c\u4efb\u610f\u6b21\uff1a
\n\narr
\u00a0\u4e2d\u4efb\u610f\u5143\u7d20\u7684\u503c\uff0c\u4f7f\u5176\u53d8\u4e3a\u4e00\u4e2a \u66f4\u5c0f\u7684\u6b63\u6574\u6570\u00a0\u3002arr
\u00a0\u4e2d\u7684\u5143\u7d20\uff0c\u4f60\u53ef\u4ee5\u4ee5\u4efb\u610f\u987a\u5e8f\u91cd\u65b0\u6392\u5217\u3002\u8bf7\u4f60\u8fd4\u56de\u6267\u884c\u4ee5\u4e0a\u64cd\u4f5c\u540e\uff0c\u5728\u6ee1\u8db3\u524d\u6587\u6240\u8ff0\u7684\u6761\u4ef6\u4e0b\uff0carr
\u00a0\u4e2d\u53ef\u80fd\u7684 \u6700\u5927\u503c\u00a0\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1aarr = [2,2,1,2,1]\n\u8f93\u51fa\uff1a2\n\u89e3\u91ca\uff1a\n\u6211\u4eec\u53ef\u4ee5\u91cd\u65b0\u6392\u5217 arr \u5f97\u5230 [1,2,2,2,1] \uff0c\u8be5\u6570\u7ec4\u6ee1\u8db3\u6240\u6709\u6761\u4ef6\u3002
\narr \u4e2d\u6700\u5927\u5143\u7d20\u4e3a 2 \u3002\n
\n\n\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1aarr = [100,1,1000]\n\u8f93\u51fa\uff1a3\n\u89e3\u91ca\uff1a\n\u4e00\u4e2a\u53ef\u884c\u7684\u65b9\u6848\u5982\u4e0b\uff1a\n1. \u91cd\u65b0\u6392\u5217\n\narr
\u5f97\u5230[1,100,1000] \u3002
\n2. \u5c06\u7b2c\u4e8c\u4e2a\u5143\u7d20\u51cf\u5c0f\u4e3a 2 \u3002\n3. \u5c06\u7b2c\u4e09\u4e2a\u5143\u7d20\u51cf\u5c0f\u4e3a 3 \u3002\n\u73b0\u5728arr = [1,2,3] \uff0c\u6ee1\u8db3\u6240\u6709\u6761\u4ef6\u3002
\narr \u4e2d\u6700\u5927\u5143\u7d20\u4e3a 3 \u3002\n
\u793a\u4f8b 3\uff1a
\n\n\n\u8f93\u5165\uff1aarr = [1,2,3,4,5]\n\u8f93\u51fa\uff1a5\n\u89e3\u91ca\uff1a\u6570\u7ec4\u5df2\u7ecf\u6ee1\u8db3\u6240\u6709\u6761\u4ef6\uff0c\u6700\u5927\u5143\u7d20\u4e3a 5 \u3002\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= arr.length <= 105
1 <= arr[i] <= 109
Design a system that manages the reservation state of n
seats that are numbered from 1
to n
.
Implement the SeatManager
class:
SeatManager(int n)
Initializes a SeatManager
object that will manage n
seats numbered from 1
to n
. All seats are initially available.int reserve()
Fetches the smallest-numbered unreserved seat, reserves it, and returns its number.void unreserve(int seatNumber)
Unreserves the seat with the given seatNumber
.\n
Example 1:
\n\n\nInput\n["SeatManager", "reserve", "reserve", "unreserve", "reserve", "reserve", "reserve", "reserve", "unreserve"]\n[[5], [], [], [2], [], [], [], [], [5]]\nOutput\n[null, 1, 2, null, 2, 3, 4, 5, null]\n\nExplanation\nSeatManager seatManager = new SeatManager(5); // Initializes a SeatManager with 5 seats.\nseatManager.reserve(); // All seats are available, so return the lowest numbered seat, which is 1.\nseatManager.reserve(); // The available seats are [2,3,4,5], so return the lowest of them, which is 2.\nseatManager.unreserve(2); // Unreserve seat 2, so now the available seats are [2,3,4,5].\nseatManager.reserve(); // The available seats are [2,3,4,5], so return the lowest of them, which is 2.\nseatManager.reserve(); // The available seats are [3,4,5], so return the lowest of them, which is 3.\nseatManager.reserve(); // The available seats are [4,5], so return the lowest of them, which is 4.\nseatManager.reserve(); // The only available seat is seat 5, so return 5.\nseatManager.unreserve(5); // Unreserve seat 5, so now the available seats are [5].\n\n\n
\n
Constraints:
\n\n1 <= n <= 105
1 <= seatNumber <= n
reserve
, it is guaranteed that there will be at least one unreserved seat.unreserve
, it is guaranteed that seatNumber
will be reserved.105
calls in total will be made to reserve
and unreserve
.\u8bf7\u4f60\u8bbe\u8ba1\u4e00\u4e2a\u7ba1\u7406 n
\u00a0\u4e2a\u5ea7\u4f4d\u9884\u7ea6\u7684\u7cfb\u7edf\uff0c\u5ea7\u4f4d\u7f16\u53f7\u4ece\u00a01
\u00a0\u5230\u00a0n
\u00a0\u3002
\u8bf7\u4f60\u5b9e\u73b0\u00a0SeatManager
\u00a0\u7c7b\uff1a
SeatManager(int n)
\u00a0\u521d\u59cb\u5316\u4e00\u4e2a\u00a0SeatManager
\u00a0\u5bf9\u8c61\uff0c\u5b83\u7ba1\u7406\u4ece 1
\u00a0\u5230 n
\u00a0\u7f16\u53f7\u7684\u00a0n
\u00a0\u4e2a\u5ea7\u4f4d\u3002\u6240\u6709\u5ea7\u4f4d\u521d\u59cb\u90fd\u662f\u53ef\u9884\u7ea6\u7684\u3002int reserve()
\u00a0\u8fd4\u56de\u53ef\u4ee5\u9884\u7ea6\u5ea7\u4f4d\u7684\u00a0\u6700\u5c0f\u7f16\u53f7\u00a0\uff0c\u6b64\u5ea7\u4f4d\u53d8\u4e3a\u4e0d\u53ef\u9884\u7ea6\u3002void unreserve(int seatNumber)
\u00a0\u5c06\u7ed9\u5b9a\u7f16\u53f7\u00a0seatNumber
\u00a0\u5bf9\u5e94\u7684\u5ea7\u4f4d\u53d8\u6210\u53ef\u4ee5\u9884\u7ea6\u3002\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1a\n[\"SeatManager\", \"reserve\", \"reserve\", \"unreserve\", \"reserve\", \"reserve\", \"reserve\", \"reserve\", \"unreserve\"]\n[[5], [], [], [2], [], [], [], [], [5]]\n\u8f93\u51fa\uff1a\n[null, 1, 2, null, 2, 3, 4, 5, null]\n\n\u89e3\u91ca\uff1a\nSeatManager seatManager = new SeatManager(5); // \u521d\u59cb\u5316 SeatManager \uff0c\u6709 5 \u4e2a\u5ea7\u4f4d\u3002\nseatManager.reserve(); // \u6240\u6709\u5ea7\u4f4d\u90fd\u53ef\u4ee5\u9884\u7ea6\uff0c\u6240\u4ee5\u8fd4\u56de\u6700\u5c0f\u7f16\u53f7\u7684\u5ea7\u4f4d\uff0c\u4e5f\u5c31\u662f 1 \u3002\nseatManager.reserve(); // \u53ef\u4ee5\u9884\u7ea6\u7684\u5ea7\u4f4d\u4e3a [2,3,4,5] \uff0c\u8fd4\u56de\u6700\u5c0f\u7f16\u53f7\u7684\u5ea7\u4f4d\uff0c\u4e5f\u5c31\u662f 2 \u3002\nseatManager.unreserve(2); // \u5c06\u5ea7\u4f4d 2 \u53d8\u4e3a\u53ef\u4ee5\u9884\u7ea6\uff0c\u73b0\u5728\u53ef\u9884\u7ea6\u7684\u5ea7\u4f4d\u4e3a [2,3,4,5] \u3002\nseatManager.reserve(); // \u53ef\u4ee5\u9884\u7ea6\u7684\u5ea7\u4f4d\u4e3a [2,3,4,5] \uff0c\u8fd4\u56de\u6700\u5c0f\u7f16\u53f7\u7684\u5ea7\u4f4d\uff0c\u4e5f\u5c31\u662f 2 \u3002\nseatManager.reserve(); // \u53ef\u4ee5\u9884\u7ea6\u7684\u5ea7\u4f4d\u4e3a [3,4,5] \uff0c\u8fd4\u56de\u6700\u5c0f\u7f16\u53f7\u7684\u5ea7\u4f4d\uff0c\u4e5f\u5c31\u662f 3 \u3002\nseatManager.reserve(); // \u53ef\u4ee5\u9884\u7ea6\u7684\u5ea7\u4f4d\u4e3a [4,5] \uff0c\u8fd4\u56de\u6700\u5c0f\u7f16\u53f7\u7684\u5ea7\u4f4d\uff0c\u4e5f\u5c31\u662f 4 \u3002\nseatManager.reserve(); // \u552f\u4e00\u53ef\u4ee5\u9884\u7ea6\u7684\u662f\u5ea7\u4f4d 5 \uff0c\u6240\u4ee5\u8fd4\u56de 5 \u3002\nseatManager.unreserve(5); // \u5c06\u5ea7\u4f4d 5 \u53d8\u4e3a\u53ef\u4ee5\u9884\u7ea6\uff0c\u73b0\u5728\u53ef\u9884\u7ea6\u7684\u5ea7\u4f4d\u4e3a [5] \u3002\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= n <= 105
1 <= seatNumber <= n
reserve
\u00a0\u7684\u8c03\u7528\uff0c\u9898\u76ee\u4fdd\u8bc1\u81f3\u5c11\u5b58\u5728\u4e00\u4e2a\u53ef\u4ee5\u9884\u7ea6\u7684\u5ea7\u4f4d\u3002unreserve
\u00a0\u7684\u8c03\u7528\uff0c\u9898\u76ee\u4fdd\u8bc1\u00a0seatNumber
\u00a0\u5728\u8c03\u7528\u51fd\u6570\u524d\u90fd\u662f\u88ab\u9884\u7ea6\u72b6\u6001\u3002reserve
\u548c\u00a0unreserve
\u00a0\u7684\u8c03\u7528\u00a0\u603b\u5171\u00a0\u4e0d\u8d85\u8fc7\u00a0105
\u00a0\u6b21\u3002You are given a 0-indexed string s
that has lowercase English letters in its even indices and digits in its odd indices.
There is a function shift(c, x)
, where c
is a character and x
is a digit, that returns the xth
character after c
.
shift('a', 5) = 'f'
and shift('x', 0) = 'x'
.For every odd index i
, you want to replace the digit s[i]
with shift(s[i-1], s[i])
.
Return s
after replacing all digits. It is guaranteed that shift(s[i-1], s[i])
will never exceed 'z'
.
\n
Example 1:
\n\n\nInput: s = "a1c1e1"\nOutput: "abcdef"\nExplanation: The digits are replaced as follows:\n- s[1] -> shift('a',1) = 'b'\n- s[3] -> shift('c',1) = 'd'\n- s[5] -> shift('e',1) = 'f'\n\n
Example 2:
\n\n\nInput: s = "a1b2c3d4e"\nOutput: "abbdcfdhe"\nExplanation: The digits are replaced as follows:\n- s[1] -> shift('a',1) = 'b'\n- s[3] -> shift('b',2) = 'd'\n- s[5] -> shift('c',3) = 'f'\n- s[7] -> shift('d',4) = 'h'\n\n
\n
Constraints:
\n\n1 <= s.length <= 100
s
consists only of lowercase English letters and digits.shift(s[i-1], s[i]) <= 'z'
for all odd indices i
.\u7ed9\u4f60\u4e00\u4e2a\u4e0b\u6807\u4ece 0\u00a0\u5f00\u59cb\u7684\u5b57\u7b26\u4e32 s
\u00a0\uff0c\u5b83\u7684 \u5076\u6570 \u4e0b\u6807\u5904\u4e3a\u5c0f\u5199\u82f1\u6587\u5b57\u6bcd\uff0c\u5947\u6570\u00a0\u4e0b\u6807\u5904\u4e3a\u6570\u5b57\u3002
\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u00a0shift(c, x)
\u00a0\uff0c\u5176\u4e2d\u00a0c
\u00a0\u662f\u4e00\u4e2a\u5b57\u7b26\u4e14\u00a0x
\u00a0\u662f\u4e00\u4e2a\u6570\u5b57\uff0c\u51fd\u6570\u8fd4\u56de\u5b57\u6bcd\u8868\u4e2d\u00a0c
\u00a0\u540e\u9762\u7b2c x
\u00a0\u4e2a\u5b57\u7b26\u3002
shift('a', 5) = 'f'
\u00a0\u548c\u00a0shift('x', 0) = 'x'
\u00a0\u3002\u5bf9\u4e8e\u6bcf\u4e2a \u5947\u6570\u00a0\u4e0b\u6807\u00a0i
\u00a0\uff0c\u4f60\u9700\u8981\u5c06\u6570\u5b57\u00a0s[i]
\u7528\u00a0shift(s[i-1], s[i])
\u00a0\u66ff\u6362\u3002
\u8bf7\u4f60\u66ff\u6362\u6240\u6709\u6570\u5b57\u4ee5\u540e\uff0c\u5c06\u5b57\u7b26\u4e32 s
\u00a0\u8fd4\u56de\u3002\u9898\u76ee \u4fdd\u8bc1\u00a0shift(s[i-1], s[i])
\u00a0\u4e0d\u4f1a\u8d85\u8fc7 'z'
\u00a0\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1as = \"a1c1e1\"\n\u8f93\u51fa\uff1a\"abcdef\"\n\u89e3\u91ca\uff1a\u6570\u5b57\u88ab\u66ff\u6362\u7ed3\u679c\u5982\u4e0b\uff1a\n- s[1] -> shift('a',1) = 'b'\n- s[3] -> shift('c',1) = 'd'\n- s[5] -> shift('e',1) = 'f'\n\n
\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1as = \"a1b2c3d4e\"\n\u8f93\u51fa\uff1a\"abbdcfdhe\"\n\u89e3\u91ca\uff1a\u6570\u5b57\u88ab\u66ff\u6362\u7ed3\u679c\u5982\u4e0b\uff1a\n- s[1] -> shift('a',1) = 'b'\n- s[3] -> shift('b',2) = 'd'\n- s[5] -> shift('c',3) = 'f'\n- s[7] -> shift('d',4) = 'h'\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= s.length <= 100
s
\u00a0\u53ea\u5305\u542b\u5c0f\u5199\u82f1\u6587\u5b57\u6bcd\u548c\u6570\u5b57\u3002i
\u00a0\uff0c\u6ee1\u8db3\u00a0shift(s[i-1], s[i]) <= 'z'
\u00a0\u3002You are given two integers, m
and k
, and a stream of integers. You are tasked to implement a data structure that calculates the MKAverage for the stream.
The MKAverage can be calculated using these steps:
\n\nm
you should consider the MKAverage to be -1
. Otherwise, copy the last m
elements of the stream to a separate container.k
elements and the largest k
elements from the container.Implement the MKAverage
class:
MKAverage(int m, int k)
Initializes the MKAverage object with an empty stream and the two integers m
and k
.void addElement(int num)
Inserts a new element num
into the stream.int calculateMKAverage()
Calculates and returns the MKAverage for the current stream rounded down to the nearest integer.\n
Example 1:
\n\n\nInput\n["MKAverage", "addElement", "addElement", "calculateMKAverage", "addElement", "calculateMKAverage", "addElement", "addElement", "addElement", "calculateMKAverage"]\n[[3, 1], [3], [1], [], [10], [], [5], [5], [5], []]\nOutput\n[null, null, null, -1, null, 3, null, null, null, 5]\n\nExplanation\nMKAverage obj = new MKAverage(3, 1); \nobj.addElement(3); // current elements are [3]\nobj.addElement(1); // current elements are [3,1]\nobj.calculateMKAverage(); // return -1, because m = 3 and only 2 elements exist.\nobj.addElement(10); // current elements are [3,1,10]\nobj.calculateMKAverage(); // The last 3 elements are [3,1,10].\n // After removing smallest and largest 1 element the container will be [3].\n // The average of [3] equals 3/1 = 3, return 3\nobj.addElement(5); // current elements are [3,1,10,5]\nobj.addElement(5); // current elements are [3,1,10,5,5]\nobj.addElement(5); // current elements are [3,1,10,5,5,5]\nobj.calculateMKAverage(); // The last 3 elements are [5,5,5].\n // After removing smallest and largest 1 element the container will be [5].\n // The average of [5] equals 5/1 = 5, return 5\n
\n\n\n
Constraints:
\n\n3 <= m <= 105
1 <= k*2 < m
1 <= num <= 105
105
calls will be made to addElement
and calculateMKAverage
.\u7ed9\u4f60\u4e24\u4e2a\u6574\u6570\u00a0m
\u00a0\u548c\u00a0k
\u00a0\uff0c\u4ee5\u53ca\u6570\u636e\u6d41\u5f62\u5f0f\u7684\u82e5\u5e72\u6574\u6570\u3002\u4f60\u9700\u8981\u5b9e\u73b0\u4e00\u4e2a\u6570\u636e\u7ed3\u6784\uff0c\u8ba1\u7b97\u8fd9\u4e2a\u6570\u636e\u6d41\u7684 MK \u5e73\u5747\u503c\u00a0\u3002
MK \u5e73\u5747\u503c\u00a0\u6309\u7167\u5982\u4e0b\u6b65\u9aa4\u8ba1\u7b97\uff1a
\n\nm
\u00a0\u4e2a\uff0cMK \u5e73\u5747\u503c\u00a0\u4e3a -1
\u00a0\uff0c\u5426\u5219\u5c06\u6570\u636e\u6d41\u4e2d\u6700\u540e m
\u00a0\u4e2a\u5143\u7d20\u62f7\u8d1d\u5230\u4e00\u4e2a\u72ec\u7acb\u7684\u5bb9\u5668\u4e2d\u3002k
\u00a0\u4e2a\u6570\u548c\u6700\u5927\u7684 k
\u00a0\u4e2a\u6570\u3002\u8bf7\u4f60\u5b9e\u73b0\u00a0MKAverage
\u00a0\u7c7b\uff1a
MKAverage(int m, int k)
\u00a0\u7528\u4e00\u4e2a\u7a7a\u7684\u6570\u636e\u6d41\u548c\u4e24\u4e2a\u6574\u6570 m
\u00a0\u548c k
\u00a0\u521d\u59cb\u5316\u00a0MKAverage\u00a0\u5bf9\u8c61\u3002void addElement(int num)
\u00a0\u5f80\u6570\u636e\u6d41\u4e2d\u63d2\u5165\u4e00\u4e2a\u65b0\u7684\u5143\u7d20\u00a0num
\u00a0\u3002int calculateMKAverage()
\u00a0\u5bf9\u5f53\u524d\u7684\u6570\u636e\u6d41\u8ba1\u7b97\u5e76\u8fd4\u56de MK \u5e73\u5747\u6570\u00a0\uff0c\u7ed3\u679c\u9700 \u5411\u4e0b\u53d6\u6574\u5230\u6700\u8fd1\u7684\u6574\u6570 \u3002\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1a\n[\"MKAverage\", \"addElement\", \"addElement\", \"calculateMKAverage\", \"addElement\", \"calculateMKAverage\", \"addElement\", \"addElement\", \"addElement\", \"calculateMKAverage\"]\n[[3, 1], [3], [1], [], [10], [], [5], [5], [5], []]\n\u8f93\u51fa\uff1a\n[null, null, null, -1, null, 3, null, null, null, 5]\n\n\u89e3\u91ca\uff1a\nMKAverage obj = new MKAverage(3, 1); \nobj.addElement(3); // \u5f53\u524d\u5143\u7d20\u4e3a [3]\nobj.addElement(1); // \u5f53\u524d\u5143\u7d20\u4e3a [3,1]\nobj.calculateMKAverage(); // \u8fd4\u56de -1 \uff0c\u56e0\u4e3a m = 3 \uff0c\u4f46\u6570\u636e\u6d41\u4e2d\u53ea\u6709 2 \u4e2a\u5143\u7d20\nobj.addElement(10); // \u5f53\u524d\u5143\u7d20\u4e3a [3,1,10]\nobj.calculateMKAverage(); // \u6700\u540e 3 \u4e2a\u5143\u7d20\u4e3a [3,1,10]\n // \u5220\u9664\u6700\u5c0f\u4ee5\u53ca\u6700\u5927\u7684 1 \u4e2a\u5143\u7d20\u540e\uff0c\u5bb9\u5668\u4e3a\n\n[3]\n // [3] \u7684\u5e73\u5747\u503c\u7b49\u4e8e 3/1 = 3 \uff0c\u6545\u8fd4\u56de 3\nobj.addElement(5); // \u5f53\u524d\u5143\u7d20\u4e3a [3,1,10,5]\nobj.addElement(5); // \u5f53\u524d\u5143\u7d20\u4e3a [3,1,10,5,5]\nobj.addElement(5); // \u5f53\u524d\u5143\u7d20\u4e3a [3,1,10,5,5,5]\nobj.calculateMKAverage(); // \u6700\u540e 3 \u4e2a\u5143\u7d20\u4e3a [5,5,5]\n //
\u5220\u9664\u6700\u5c0f\u4ee5\u53ca\u6700\u5927\u7684 1 \u4e2a\u5143\u7d20\u540e\uff0c\u5bb9\u5668\u4e3a[5]
\n //
[5] \u7684\u5e73\u5747\u503c\u7b49\u4e8e 5/1 = 5 \uff0c\u6545\u8fd4\u56de 5\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n3 <= m <= 105
1 <= k*2 < m
1 <= num <= 105
addElement
\u4e0e\u00a0calculateMKAverage
\u00a0\u603b\u64cd\u4f5c\u6b21\u6570\u4e0d\u8d85\u8fc7 105
\u6b21\u3002There is a 3 lane road of length n
that consists of n + 1
points labeled from 0
to n
. A frog starts at point 0
in the second lane and wants to jump to point n
. However, there could be obstacles along the way.
You are given an array obstacles
of length n + 1
where each obstacles[i]
(ranging from 0 to 3) describes an obstacle on the lane obstacles[i]
at point i
. If obstacles[i] == 0
, there are no obstacles at point i
. There will be at most one obstacle in the 3 lanes at each point.
obstacles[2] == 1
, then there is an obstacle on lane 1 at point 2.The frog can only travel from point i
to point i + 1
on the same lane if there is not an obstacle on the lane at point i + 1
. To avoid obstacles, the frog can also perform a side jump to jump to another lane (even if they are not adjacent) at the same point if there is no obstacle on the new lane.
Return the minimum number of side jumps the frog needs to reach any lane at point n starting from lane 2
at point 0.
Note: There will be no obstacles on points 0
and n
.
\n
Example 1:
\n\nInput: obstacles = [0,1,2,3,0]\nOutput: 2 \nExplanation: The optimal solution is shown by the arrows above. There are 2 side jumps (red arrows).\nNote that the frog can jump over obstacles only when making side jumps (as shown at point 2).\n\n\n
Example 2:
\n\nInput: obstacles = [0,1,1,3,3,0]\nOutput: 0\nExplanation: There are no obstacles on lane 2. No side jumps are required.\n\n\n
Example 3:
\n\nInput: obstacles = [0,2,1,0,3,0]\nOutput: 2\nExplanation: The optimal solution is shown by the arrows above. There are 2 side jumps.\n\n\n
\n
Constraints:
\n\nobstacles.length == n + 1
1 <= n <= 5 * 105
0 <= obstacles[i] <= 3
obstacles[0] == obstacles[n] == 0
\u7ed9\u4f60\u4e00\u4e2a\u957f\u5ea6\u4e3a\u00a0n
\u00a0\u7684\u00a03 \u8dd1\u9053\u9053\u8def\u00a0\uff0c\u5b83\u603b\u5171\u5305\u542b\u00a0n + 1
\u00a0\u4e2a\u00a0\u70b9\u00a0\uff0c\u7f16\u53f7\u4e3a\u00a00
\u00a0\u5230\u00a0n
\u00a0\u3002\u4e00\u53ea\u9752\u86d9\u4ece\u00a00
\u00a0\u53f7\u70b9\u7b2c\u4e8c\u6761\u8dd1\u9053\u00a0\u51fa\u53d1\u00a0\uff0c\u5b83\u60f3\u8981\u8df3\u5230\u70b9\u00a0n
\u00a0\u5904\u3002\u7136\u800c\u9053\u8def\u4e0a\u53ef\u80fd\u6709\u4e00\u4e9b\u969c\u788d\u3002
\u7ed9\u4f60\u4e00\u4e2a\u957f\u5ea6\u4e3a n + 1
\u00a0\u7684\u6570\u7ec4\u00a0obstacles
\u00a0\uff0c\u5176\u4e2d\u00a0obstacles[i]
\u00a0\uff08\u53d6\u503c\u8303\u56f4\u4ece 0 \u5230 3\uff09\u8868\u793a\u5728\u70b9 i
\u00a0\u5904\u7684\u00a0obstacles[i]
\u00a0\u8dd1\u9053\u4e0a\u6709\u4e00\u4e2a\u969c\u788d\u3002\u5982\u679c\u00a0obstacles[i] == 0
\u00a0\uff0c\u90a3\u4e48\u70b9\u00a0i
\u00a0\u5904\u6ca1\u6709\u969c\u788d\u3002\u4efb\u4f55\u4e00\u4e2a\u70b9\u7684\u4e09\u6761\u8dd1\u9053\u4e2d\u00a0\u6700\u591a\u6709\u4e00\u4e2a\u00a0\u969c\u788d\u3002
obstacles[2] == 1
\u00a0\uff0c\u90a3\u4e48\u8bf4\u660e\u5728\u70b9 2 \u5904\u8dd1\u9053 1 \u6709\u969c\u788d\u3002\u8fd9\u53ea\u9752\u86d9\u4ece\u70b9 i
\u00a0\u8df3\u5230\u70b9 i + 1
\u00a0\u4e14\u8dd1\u9053\u4e0d\u53d8\u7684\u524d\u63d0\u662f\u70b9 i + 1
\u00a0\u7684\u540c\u4e00\u8dd1\u9053\u4e0a\u6ca1\u6709\u969c\u788d\u3002\u4e3a\u4e86\u8eb2\u907f\u969c\u788d\uff0c\u8fd9\u53ea\u9752\u86d9\u4e5f\u53ef\u4ee5\u5728\u00a0\u540c\u4e00\u4e2a\u00a0\u70b9\u5904\u00a0\u4fa7\u8df3\u00a0\u5230 \u53e6\u5916\u4e00\u6761\u00a0\u8dd1\u9053\uff08\u8fd9\u4e24\u6761\u8dd1\u9053\u53ef\u4ee5\u4e0d\u76f8\u90bb\uff09\uff0c\u4f46\u524d\u63d0\u662f\u8df3\u8fc7\u53bb\u7684\u8dd1\u9053\u8be5\u70b9\u5904\u6ca1\u6709\u969c\u788d\u3002
\u8fd9\u53ea\u9752\u86d9\u4ece\u70b9 0 \u5904\u8dd1\u9053 2
\u00a0\u51fa\u53d1\uff0c\u5e76\u60f3\u5230\u8fbe\u70b9 n
\u00a0\u5904\u7684 \u4efb\u4e00\u8dd1\u9053 \uff0c\u8bf7\u4f60\u8fd4\u56de \u6700\u5c11\u4fa7\u8df3\u6b21\u6570\u00a0\u3002
\u6ce8\u610f\uff1a\u70b9 0
\u00a0\u5904\u548c\u70b9 n
\u00a0\u5904\u7684\u4efb\u4e00\u8dd1\u9053\u90fd\u4e0d\u4f1a\u6709\u969c\u788d\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1aobstacles = [0,1,2,3,0]\n\u8f93\u51fa\uff1a2 \n\u89e3\u91ca\uff1a\u6700\u4f18\u65b9\u6848\u5982\u4e0a\u56fe\u7bad\u5934\u6240\u793a\u3002\u603b\u5171\u6709 2 \u6b21\u4fa7\u8df3\uff08\u7ea2\u8272\u7bad\u5934\uff09\u3002\n\u6ce8\u610f\uff0c\u8fd9\u53ea\u9752\u86d9\u53ea\u6709\u5f53\u4fa7\u8df3\u65f6\u624d\u53ef\u4ee5\u8df3\u8fc7\u969c\u788d\uff08\u5982\u4e0a\u56fe\u70b9 2 \u5904\u6240\u793a\uff09\u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1aobstacles = [0,1,1,3,3,0]\n\u8f93\u51fa\uff1a0\n\u89e3\u91ca\uff1a\u8dd1\u9053 2 \u6ca1\u6709\u4efb\u4f55\u969c\u788d\uff0c\u6240\u4ee5\u4e0d\u9700\u8981\u4efb\u4f55\u4fa7\u8df3\u3002\n\n\n
\u793a\u4f8b 3\uff1a
\n\n\u8f93\u5165\uff1aobstacles = [0,2,1,0,3,0]\n\u8f93\u51fa\uff1a2\n\u89e3\u91ca\uff1a\u6700\u4f18\u65b9\u6848\u5982\u4e0a\u56fe\u6240\u793a\u3002\u603b\u5171\u6709 2 \u6b21\u4fa7\u8df3\u3002\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\nobstacles.length == n + 1
1 <= n <= 5 * 105
0 <= obstacles[i] <= 3
obstacles[0] == obstacles[n] == 0
There are n
friends that are playing a game. The friends are sitting in a circle and are numbered from 1
to n
in clockwise order. More formally, moving clockwise from the ith
friend brings you to the (i+1)th
friend for 1 <= i < n
, and moving clockwise from the nth
friend brings you to the 1st
friend.
The rules of the game are as follows:
\r\n\r\n1st
friend.k
friends in the clockwise direction including the friend you started at. The counting wraps around the circle and may count some friends more than once.2
starting from the friend immediately clockwise of the friend who just lost and repeat.Given the number of friends, n
, and an integer k
, return the winner of the game.
\r\n
Example 1:
\r\n\r\nInput: n = 5, k = 2\r\nOutput: 3\r\nExplanation: Here are the steps of the game:\r\n1) Start at friend 1.\r\n2) Count 2 friends clockwise, which are friends 1 and 2.\r\n3) Friend 2 leaves the circle. Next start is friend 3.\r\n4) Count 2 friends clockwise, which are friends 3 and 4.\r\n5) Friend 4 leaves the circle. Next start is friend 5.\r\n6) Count 2 friends clockwise, which are friends 5 and 1.\r\n7) Friend 1 leaves the circle. Next start is friend 3.\r\n8) Count 2 friends clockwise, which are friends 3 and 5.\r\n9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.\r\n\r\n
Example 2:
\r\n\r\n\r\nInput: n = 6, k = 5\r\nOutput: 1\r\nExplanation: The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.\r\n\r\n\r\n
\r\n
Constraints:
\r\n\r\n1 <= k <= n <= 500
\u5171\u6709 n
\u540d\u5c0f\u4f19\u4f34\u4e00\u8d77\u505a\u6e38\u620f\u3002\u5c0f\u4f19\u4f34\u4eec\u56f4\u6210\u4e00\u5708\uff0c\u6309 \u987a\u65f6\u9488\u987a\u5e8f \u4ece 1
\u5230 n
\u7f16\u53f7\u3002\u786e\u5207\u5730\u8bf4\uff0c\u4ece\u7b2c i
\u540d\u5c0f\u4f19\u4f34\u987a\u65f6\u9488\u79fb\u52a8\u4e00\u4f4d\u4f1a\u5230\u8fbe\u7b2c (i+1)
\u540d\u5c0f\u4f19\u4f34\u7684\u4f4d\u7f6e\uff0c\u5176\u4e2d 1 <= i < n
\uff0c\u4ece\u7b2c n
\u540d\u5c0f\u4f19\u4f34\u987a\u65f6\u9488\u79fb\u52a8\u4e00\u4f4d\u4f1a\u56de\u5230\u7b2c 1
\u540d\u5c0f\u4f19\u4f34\u7684\u4f4d\u7f6e\u3002
\u6e38\u620f\u9075\u5faa\u5982\u4e0b\u89c4\u5219\uff1a
\n\n1
\u540d\u5c0f\u4f19\u4f34\u6240\u5728\u4f4d\u7f6e \u5f00\u59cb \u3002k
\u540d\u5c0f\u4f19\u4f34\uff0c\u8ba1\u6570\u65f6\u9700\u8981 \u5305\u542b \u8d77\u59cb\u65f6\u7684\u90a3\u4f4d\u5c0f\u4f19\u4f34\u3002\u9010\u4e2a\u7ed5\u5708\u8fdb\u884c\u8ba1\u6570\uff0c\u4e00\u4e9b\u5c0f\u4f19\u4f34\u53ef\u80fd\u4f1a\u88ab\u6570\u8fc7\u4e0d\u6b62\u4e00\u6b21\u30022
\u7ee7\u7eed\u6267\u884c\u3002\u7ed9\u4f60\u53c2\u4e0e\u6e38\u620f\u7684\u5c0f\u4f19\u4f34\u603b\u6570 n
\uff0c\u548c\u4e00\u4e2a\u6574\u6570 k
\uff0c\u8fd4\u56de\u6e38\u620f\u7684\u83b7\u80dc\u8005\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1an = 5, k = 2\n\u8f93\u51fa\uff1a3\n\u89e3\u91ca\uff1a\u6e38\u620f\u8fd0\u884c\u6b65\u9aa4\u5982\u4e0b\uff1a\n1) \u4ece\u5c0f\u4f19\u4f34 1 \u5f00\u59cb\u3002\n2) \u987a\u65f6\u9488\u6570 2 \u540d\u5c0f\u4f19\u4f34\uff0c\u4e5f\u5c31\u662f\u5c0f\u4f19\u4f34 1 \u548c 2 \u3002\n3) \u5c0f\u4f19\u4f34 2 \u79bb\u5f00\u5708\u5b50\u3002\u4e0b\u4e00\u6b21\u4ece\u5c0f\u4f19\u4f34 3 \u5f00\u59cb\u3002\n4) \u987a\u65f6\u9488\u6570 2 \u540d\u5c0f\u4f19\u4f34\uff0c\u4e5f\u5c31\u662f\u5c0f\u4f19\u4f34 3 \u548c 4 \u3002\n5) \u5c0f\u4f19\u4f34 4 \u79bb\u5f00\u5708\u5b50\u3002\u4e0b\u4e00\u6b21\u4ece\u5c0f\u4f19\u4f34 5 \u5f00\u59cb\u3002\n6) \u987a\u65f6\u9488\u6570 2 \u540d\u5c0f\u4f19\u4f34\uff0c\u4e5f\u5c31\u662f\u5c0f\u4f19\u4f34 5 \u548c 1 \u3002\n7) \u5c0f\u4f19\u4f34 1 \u79bb\u5f00\u5708\u5b50\u3002\u4e0b\u4e00\u6b21\u4ece\u5c0f\u4f19\u4f34 3 \u5f00\u59cb\u3002\n8) \u987a\u65f6\u9488\u6570 2 \u540d\u5c0f\u4f19\u4f34\uff0c\u4e5f\u5c31\u662f\u5c0f\u4f19\u4f34 3 \u548c 5 \u3002\n9) \u5c0f\u4f19\u4f34 5 \u79bb\u5f00\u5708\u5b50\u3002\u53ea\u5269\u4e0b\u5c0f\u4f19\u4f34 3 \u3002\u6240\u4ee5\u5c0f\u4f19\u4f34 3 \u662f\u6e38\u620f\u7684\u83b7\u80dc\u8005\u3002\n\n
\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1an = 6, k = 5\n\u8f93\u51fa\uff1a1\n\u89e3\u91ca\uff1a\u5c0f\u4f19\u4f34\u79bb\u5f00\u5708\u5b50\u7684\u987a\u5e8f\uff1a5\u30014\u30016\u30012\u30013 \u3002\u5c0f\u4f19\u4f34 1 \u662f\u6e38\u620f\u7684\u83b7\u80dc\u8005\u3002\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= k <= n <= 500
There is a function signFunc(x)
that returns:
1
if x
is positive.-1
if x
is negative.0
if x
is equal to 0
.You are given an integer array nums
. Let product
be the product of all values in the array nums
.
Return signFunc(product)
.
\n
Example 1:
\n\n\nInput: nums = [-1,-2,-3,-4,3,2,1]\nOutput: 1\nExplanation: The product of all values in the array is 144, and signFunc(144) = 1\n\n\n
Example 2:
\n\n\nInput: nums = [1,5,0,2,-3]\nOutput: 0\nExplanation: The product of all values in the array is 0, and signFunc(0) = 0\n\n\n
Example 3:
\n\n\nInput: nums = [-1,1,-1,1,-1]\nOutput: -1\nExplanation: The product of all values in the array is -1, and signFunc(-1) = -1\n\n\n
\n
Constraints:
\n\n1 <= nums.length <= 1000
-100 <= nums[i] <= 100
\u5df2\u77e5\u51fd\u6570\u00a0signFunc(x)
\u5c06\u4f1a\u6839\u636e x
\u7684\u6b63\u8d1f\u8fd4\u56de\u7279\u5b9a\u503c\uff1a
x
\u662f\u6b63\u6570\uff0c\u8fd4\u56de 1
\u3002x
\u662f\u8d1f\u6570\uff0c\u8fd4\u56de -1
\u3002x
\u662f\u7b49\u4e8e 0
\uff0c\u8fd4\u56de 0
\u3002\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 nums
\u3002\u4ee4 product
\u4e3a\u6570\u7ec4 nums
\u4e2d\u6240\u6709\u5143\u7d20\u503c\u7684\u4e58\u79ef\u3002
\u8fd4\u56de signFunc(product)
\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1anums = [-1,-2,-3,-4,3,2,1]\n\u8f93\u51fa\uff1a1\n\u89e3\u91ca\uff1a\u6570\u7ec4\u4e2d\u6240\u6709\u503c\u7684\u4e58\u79ef\u662f 144 \uff0c\u4e14 signFunc(144) = 1\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1anums = [1,5,0,2,-3]\n\u8f93\u51fa\uff1a0\n\u89e3\u91ca\uff1a\u6570\u7ec4\u4e2d\u6240\u6709\u503c\u7684\u4e58\u79ef\u662f 0 \uff0c\u4e14 signFunc(0) = 0\n\n\n
\u793a\u4f8b 3\uff1a
\n\n\n\u8f93\u5165\uff1anums = [-1,1,-1,1,-1]\n\u8f93\u51fa\uff1a-1\n\u89e3\u91ca\uff1a\u6570\u7ec4\u4e2d\u6240\u6709\u503c\u7684\u4e58\u79ef\u662f -1 \uff0c\u4e14 signFunc(-1) = -1\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= nums.length <= 1000
-100 <= nums[i] <= 100
A trie (pronounced as "try") or prefix tree is a tree data structure used to efficiently store and retrieve keys in a dataset of strings. There are various applications of this data structure, such as autocomplete and spellchecker.
\n\nImplement the Trie class:
\n\nTrie()
Initializes the trie object.void insert(String word)
Inserts the string word
into the trie.int countWordsEqualTo(String word)
Returns the number of instances of the string word
in the trie.int countWordsStartingWith(String prefix)
Returns the number of strings in the trie that have the string prefix
as a prefix.void erase(String word)
Erases the string word
from the trie.\n
Example 1:
\n\n\nInput\n["Trie", "insert", "insert", "countWordsEqualTo", "countWordsStartingWith", "erase", "countWordsEqualTo", "countWordsStartingWith", "erase", "countWordsStartingWith"]\n[[], ["apple"], ["apple"], ["apple"], ["app"], ["apple"], ["apple"], ["app"], ["apple"], ["app"]]\nOutput\n[null, null, null, 2, 2, null, 1, 1, null, 0]\n\nExplanation\nTrie trie = new Trie();\ntrie.insert("apple"); // Inserts "apple".\ntrie.insert("apple"); // Inserts another "apple".\ntrie.countWordsEqualTo("apple"); // There are two instances of "apple" so return 2.\ntrie.countWordsStartingWith("app"); // "app" is a prefix of "apple" so return 2.\ntrie.erase("apple"); // Erases one "apple".\ntrie.countWordsEqualTo("apple"); // Now there is only one instance of "apple" so return 1.\ntrie.countWordsStartingWith("app"); // return 1\ntrie.erase("apple"); // Erases "apple". Now the trie is empty.\ntrie.countWordsStartingWith("app"); // return 0\n\n\n
\n
Constraints:
\n\n1 <= word.length, prefix.length <= 2000
word
and prefix
consist only of lowercase English letters.3 * 104
calls in total will be made to insert
, countWordsEqualTo
, countWordsStartingWith
, and erase
.erase
, the string word
will exist in the trie.\u524d\u7f00\u6811\uff08trie\u00a0\uff0c\u53d1\u97f3\u4e3a \"try\"\uff09\u662f\u4e00\u4e2a\u6811\u72b6\u7684\u6570\u636e\u7ed3\u6784\uff0c\u7528\u4e8e\u9ad8\u6548\u5730\u5b58\u50a8\u548c\u68c0\u7d22\u4e00\u7cfb\u5217\u5b57\u7b26\u4e32\u7684\u524d\u7f00\u3002\u524d\u7f00\u6811\u6709\u8bb8\u591a\u5e94\u7528\uff0c\u5982\u81ea\u52a8\u8865\u5168\u548c\u62fc\u5199\u68c0\u67e5\u3002
\n\n\u5b9e\u73b0\u524d\u7f00\u6811 Trie \u7c7b\uff1a
\n\nTrie()
\u00a0\u521d\u59cb\u5316\u524d\u7f00\u6811\u5bf9\u8c61\u3002void insert(String word)
\u00a0\u5c06\u5b57\u7b26\u4e32\u00a0word
\u00a0\u63d2\u5165\u524d\u7f00\u6811\u4e2d\u3002int countWordsEqualTo(String word)
\u00a0\u8fd4\u56de\u524d\u7f00\u6811\u4e2d\u5b57\u7b26\u4e32\u00a0word
\u00a0\u7684\u5b9e\u4f8b\u4e2a\u6570\u3002int countWordsStartingWith(String prefix)
\u00a0\u8fd4\u56de\u524d\u7f00\u6811\u4e2d\u4ee5\u00a0prefix
\u00a0\u4e3a\u524d\u7f00\u7684\u5b57\u7b26\u4e32\u4e2a\u6570\u3002void erase(String word)
\u00a0\u4ece\u524d\u7f00\u6811\u4e2d\u79fb\u9664\u5b57\u7b26\u4e32\u00a0word
\u3002\u00a0
\n\n\u793a\u4f8b 1:
\n\n\u8f93\u5165\n[\"Trie\", \"insert\", \"insert\", \"countWordsEqualTo\", \"countWordsStartingWith\", \"erase\", \"countWordsEqualTo\", \"countWordsStartingWith\", \"erase\", \"countWordsStartingWith\"]\n[[], [\"apple\"], [\"apple\"], [\"apple\"], [\"app\"], [\"apple\"], [\"apple\"], [\"app\"], [\"apple\"], [\"app\"]]\n\u8f93\u51fa\n[null, null, null, 2, 2, null, 1, 1, null, 0]\n\n\u89e3\u91ca\nTrie trie = new Trie();\ntrie.insert(\"apple\"); // \u63d2\u5165 \"apple\"\u3002\ntrie.insert(\"apple\"); // \u63d2\u5165\u53e6\u4e00\u4e2a \"apple\"\u3002\ntrie.countWordsEqualTo(\"apple\"); // \u6709\u4e24\u4e2a \"apple\" \u5b9e\u4f8b\uff0c\u6240\u4ee5\u8fd4\u56de 2\u3002\ntrie.countWordsStartingWith(\"app\"); // \"app\" \u662f \"apple\" \u7684\u524d\u7f00\uff0c\u6240\u4ee5\u8fd4\u56de 2\u3002\ntrie.erase(\"apple\"); // \u79fb\u9664\u4e00\u4e2a \"apple\"\u3002\ntrie.countWordsEqualTo(\"apple\"); // \u73b0\u5728\u53ea\u6709\u4e00\u4e2a \"apple\" \u5b9e\u4f8b\uff0c\u6240\u4ee5\u8fd4\u56de 1\u3002\ntrie.countWordsStartingWith(\"app\"); // \u8fd4\u56de 1\ntrie.erase(\"apple\"); // \u79fb\u9664 \"apple\"\u3002\u73b0\u5728\u524d\u7f00\u6811\u662f\u7a7a\u7684\u3002\ntrie.countWordsStartingWith(\"app\"); // \u8fd4\u56de 0\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= word.length, prefix.length <= 2000
word
\u00a0\u548c\u00a0prefix
\u00a0\u53ea\u5305\u542b\u5c0f\u5199\u82f1\u6587\u5b57\u6bcd\u3002insert
\u3001\u00a0countWordsEqualTo
\u3001\u00a0countWordsStartingWith
\u00a0\u548c\u00a0erase
\u00a0\u603b\u5171\u8c03\u7528\u6700\u591a\u00a03 * 104
\u00a0\u6b21\u3002erase
\u00a0\u65f6\uff0c\u5b57\u7b26\u4e32\u00a0word
\u00a0\u603b\u662f\u5b58\u5728\u4e8e\u524d\u7f00\u6811\u4e2d\u3002Table: Products
\r\n+-------------+---------+\r\n| Column Name | Type |\r\n+-------------+---------+\r\n| product_id | int |\r\n| store1 | int |\r\n| store2 | int |\r\n| store3 | int |\r\n+-------------+---------+\r\nproduct_id is the primary key for this table.\r\nEach row in this table indicates the product's price in 3 different stores: store1, store2, and store3.\r\nIf the product is not available in a store, the price will be null in that store's column.\r\n\r\n\r\n
\r\n\r\n
Write an SQL query to rearrange the Products
table so that each row has (product_id, store, price)
. If a product is not available in a store, do not include a row with that product_id
and store
combination in the result table.
Return the result table in any order.
\r\n\r\nThe query result format is in the following example:
\r\n\r\n\r\n\r\n
\r\nProducts table:\r\n+------------+--------+--------+--------+\r\n| product_id | store1 | store2 | store3 |\r\n+------------+--------+--------+--------+\r\n| 0 | 95 | 100 | 105 |\r\n| 1 | 70 | null | 80 |\r\n+------------+--------+--------+--------+\r\n\r\nResult table:\r\n+------------+--------+-------+\r\n| product_id | store | price |\r\n+------------+--------+-------+\r\n| 0 | store1 | 95 |\r\n| 0 | store2 | 100 |\r\n| 0 | store3 | 105 |\r\n| 1 | store1 | 70 |\r\n| 1 | store3 | 80 |\r\n+------------+--------+-------+\r\n\r\nProduct 0 is available in all three stores with prices 95, 100, and 105 respectively.\r\nProduct 1 is available in store1 with price 70 and store3 with price 80. The product is not available in store2.\r\n", "content_cn": "
\u8868\uff1aProducts
\n+-------------+---------+\n| Column Name | Type |\n+-------------+---------+\n| product_id | int |\n| store1 | int |\n| store2 | int |\n| store3 | int |\n+-------------+---------+\n\u8fd9\u5f20\u8868\u7684\u4e3b\u952e\u662fproduct_id\uff08\u4ea7\u54c1Id\uff09\u3002\n\u6bcf\u884c\u5b58\u50a8\u4e86\u8fd9\u4e00\u4ea7\u54c1\u5728\u4e0d\u540c\u5546\u5e97store1, store2, store3\u7684\u4ef7\u683c\u3002\n\u5982\u679c\u8fd9\u4e00\u4ea7\u54c1\u5728\u5546\u5e97\u91cc\u6ca1\u6709\u51fa\u552e\uff0c\u5219\u503c\u5c06\u4e3anull\u3002\n\n\n
\u00a0
\n\n\u8bf7\u4f60\u91cd\u6784 Products
\u8868\uff0c\u67e5\u8be2\u6bcf\u4e2a\u4ea7\u54c1\u5728\u4e0d\u540c\u5546\u5e97\u7684\u4ef7\u683c\uff0c\u4f7f\u5f97\u8f93\u51fa\u7684\u683c\u5f0f\u53d8\u4e3a(product_id, store, price)
\u3002\u5982\u679c\u8fd9\u4e00\u4ea7\u54c1\u5728\u5546\u5e97\u91cc\u6ca1\u6709\u51fa\u552e\uff0c\u5219\u4e0d\u8f93\u51fa\u8fd9\u4e00\u884c\u3002
\u8f93\u51fa\u7ed3\u679c\u8868\u4e2d\u7684\u987a\u5e8f\u4e0d\u4f5c\u8981\u6c42\u3002
\n\n\u67e5\u8be2\u8f93\u51fa\u683c\u5f0f\u8bf7\u53c2\u8003\u4e0b\u9762\u793a\u4f8b\u3002
\n\n\u00a0
\n\n\nProducts table:\n+------------+--------+--------+--------+\n| product_id | store1 | store2 | store3 |\n+------------+--------+--------+--------+\n| 0 | 95 | 100 | 105 |\n| 1 | 70 | null | 80 |\n+------------+--------+--------+--------+\n\nResult table:\n+------------+--------+-------+\n| product_id | store | price |\n+------------+--------+-------+\n| 0 | store1 | 95 |\n| 0 | store2 | 100 |\n| 0 | store3 | 105 |\n| 1 | store1 | 70 |\n| 1 | store3 | 80 |\n+------------+--------+-------+\n\n\u4ea7\u54c10\u5728store1\uff0cstore2,store3\u7684\u4ef7\u683c\u5206\u522b\u4e3a95,100,105\u3002\n\u4ea7\u54c11\u5728store1\uff0cstore3\u7684\u4ef7\u683c\u5206\u522b\u4e3a70,80\u3002\u5728store2\u65e0\u6cd5\u4e70\u5230\u3002\n\n", "tags_en": [], "tags_cn": [], "difficulty_en": "Easy", "difficulty_cn": "\u7b80\u5355", "code_snippets": [{"lang": "MySQL", "langSlug": "mysql", "code": "# Write your MySQL query statement below", "__typename": "CodeSnippetNode"}, {"lang": "MS SQL Server", "langSlug": "mssql", "code": "/* Write your T-SQL query statement below */", "__typename": "CodeSnippetNode"}, {"lang": "Oracle", "langSlug": "oraclesql", "code": "/* Write your PL/SQL query statement below */", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1795](https://leetcode-cn.com/problems/rearrange-products-table)", "[\u6bcf\u4e2a\u4ea7\u54c1\u5728\u4e0d\u540c\u5546\u5e97\u7684\u4ef7\u683c](/solution/1700-1799/1795.Rearrange%20Products%20Table/README.md)", "", "\u7b80\u5355", "\ud83d\udd12"], "md_table_row_en": ["[1795](https://leetcode.com/problems/rearrange-products-table)", "[Rearrange Products Table](/solution/1700-1799/1795.Rearrange%20Products%20Table/README_EN.md)", "", "Easy", "\ud83d\udd12"]}, {"question_id": "1947", "frontend_question_id": "1819", "paid_only": false, "paid_only_cn": false, "url_cn": "https://leetcode-cn.com/problems/number-of-different-subsequences-gcds", "url_en": "https://leetcode.com/problems/number-of-different-subsequences-gcds", "relative_path_cn": "/solution/1800-1899/1819.Number%20of%20Different%20Subsequences%20GCDs/README.md", "relative_path_en": "/solution/1800-1899/1819.Number%20of%20Different%20Subsequences%20GCDs/README_EN.md", "title_cn": "\u5e8f\u5217\u4e2d\u4e0d\u540c\u6700\u5927\u516c\u7ea6\u6570\u7684\u6570\u76ee", "title_en": "Number of Different Subsequences GCDs", "question_title_slug": "number-of-different-subsequences-gcds", "content_en": "
You are given an array nums
that consists of positive integers.
The GCD of a sequence of numbers is defined as the greatest integer that divides all the numbers in the sequence evenly.
\n\n[4,6,16]
is 2
.A subsequence of an array is a sequence that can be formed by removing some elements (possibly none) of the array.
\n\n[2,5,10]
is a subsequence of [1,2,1,2,4,1,5,10]
.Return the number of different GCDs among all non-empty subsequences of nums
.
\n
Example 1:
\n\nInput: nums = [6,10,3]\nOutput: 5\nExplanation: The figure shows all the non-empty subsequences and their GCDs.\nThe different GCDs are 6, 10, 3, 2, and 1.\n\n\n
Example 2:
\n\n\nInput: nums = [5,15,40,5,6]\nOutput: 7\n\n\n
\n
Constraints:
\n\n1 <= nums.length <= 105
1 <= nums[i] <= 2 * 105
\u7ed9\u4f60\u4e00\u4e2a\u7531\u6b63\u6574\u6570\u7ec4\u6210\u7684\u6570\u7ec4 nums
\u3002
\u6570\u5b57\u5e8f\u5217\u7684 \u6700\u5927\u516c\u7ea6\u6570 \u5b9a\u4e49\u4e3a\u5e8f\u5217\u4e2d\u6240\u6709\u6574\u6570\u7684\u5171\u6709\u7ea6\u6570\u4e2d\u7684\u6700\u5927\u6574\u6570\u3002
\n\n[4,6,16]
\u7684\u6700\u5927\u516c\u7ea6\u6570\u662f 2
\u3002\u6570\u7ec4\u7684\u4e00\u4e2a \u5b50\u5e8f\u5217 \u672c\u8d28\u662f\u4e00\u4e2a\u5e8f\u5217\uff0c\u53ef\u4ee5\u901a\u8fc7\u5220\u9664\u6570\u7ec4\u4e2d\u7684\u67d0\u4e9b\u5143\u7d20\uff08\u6216\u8005\u4e0d\u5220\u9664\uff09\u5f97\u5230\u3002
\n\n[2,5,10]
\u662f [1,2,1,2,4,1,5,10]
\u7684\u4e00\u4e2a\u5b50\u5e8f\u5217\u3002\u8ba1\u7b97\u5e76\u8fd4\u56de nums
\u7684\u6240\u6709 \u975e\u7a7a \u5b50\u5e8f\u5217\u4e2d \u4e0d\u540c \u6700\u5927\u516c\u7ea6\u6570\u7684 \u6570\u76ee \u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1anums = [6,10,3]\n\u8f93\u51fa\uff1a5\n\u89e3\u91ca\uff1a\u4e0a\u56fe\u663e\u793a\u4e86\u6240\u6709\u7684\u975e\u7a7a\u5b50\u5e8f\u5217\u4e0e\u5404\u81ea\u7684\u6700\u5927\u516c\u7ea6\u6570\u3002\n\u4e0d\u540c\u7684\u6700\u5927\u516c\u7ea6\u6570\u4e3a 6 \u300110 \u30013 \u30012 \u548c 1 \u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1anums = [5,15,40,5,6]\n\u8f93\u51fa\uff1a7\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= nums.length <= 105
1 <= nums[i] <= 2 * 105
You are given two positive integer arrays nums1
and nums2
, both of length n
.
The absolute sum difference of arrays nums1
and nums2
is defined as the sum of |nums1[i] - nums2[i]|
for each 0 <= i < n
(0-indexed).
You can replace at most one element of nums1
with any other element in nums1
to minimize the absolute sum difference.
Return the minimum absolute sum difference after replacing at most one element in the array nums1
. Since the answer may be large, return it modulo 109 + 7
.
|x|
is defined as:
x
if x >= 0
, or-x
if x < 0
.\n
Example 1:
\n\n\nInput: nums1 = [1,7,5], nums2 = [2,3,5]\nOutput: 3\nExplanation: There are two possible optimal solutions:\n- Replace the second element with the first: [1,7,5] => [1,1,5], or\n- Replace the second element with the third: [1,7,5] => [1,5,5].\nBoth will yield an absolute sum difference of |1-2| + (|1-3| or |5-3|) + |5-5| =
3.\n
\n\nExample 2:
\n\n\nInput: nums1 = [2,4,6,8,10], nums2 = [2,4,6,8,10]\nOutput: 0\nExplanation: nums1 is equal to nums2 so no replacement is needed. This will result in an \nabsolute sum difference of 0.\n\n\n
Example 3:
\n\n\nInput: nums1 = [1,10,4,4,2,7], nums2 = [9,3,5,1,7,4]\nOutput: 20\nExplanation: Replace the first element with the second: [1,10,4,4,2,7] => [10,10,4,4,2,7].\nThis yields an absolute sum difference of |10-9| + |10-3| + |4-5| + |4-1| + |2-7| + |7-4| = 20
\n
\n\n\n
Constraints:
\n\nn == nums1.length
n == nums2.length
1 <= n <= 105
1 <= nums1[i], nums2[i] <= 105
\u7ed9\u4f60\u4e24\u4e2a\u6b63\u6574\u6570\u6570\u7ec4 nums1
\u548c nums2
\uff0c\u6570\u7ec4\u7684\u957f\u5ea6\u90fd\u662f n
\u3002
\u6570\u7ec4 nums1
\u548c nums2
\u7684 \u7edd\u5bf9\u5dee\u503c\u548c \u5b9a\u4e49\u4e3a\u6240\u6709 |nums1[i] - nums2[i]|
\uff080 <= i < n
\uff09\u7684 \u603b\u548c\uff08\u4e0b\u6807\u4ece 0 \u5f00\u59cb\uff09\u3002
\u4f60\u53ef\u4ee5\u9009\u7528 nums1
\u4e2d\u7684 \u4efb\u610f\u4e00\u4e2a \u5143\u7d20\u6765\u66ff\u6362 nums1
\u4e2d\u7684 \u81f3\u591a \u4e00\u4e2a\u5143\u7d20\uff0c\u4ee5 \u6700\u5c0f\u5316 \u7edd\u5bf9\u5dee\u503c\u548c\u3002
\u5728\u66ff\u6362\u6570\u7ec4 nums1
\u4e2d\u6700\u591a\u4e00\u4e2a\u5143\u7d20 \u4e4b\u540e \uff0c\u8fd4\u56de\u6700\u5c0f\u7edd\u5bf9\u5dee\u503c\u548c\u3002\u56e0\u4e3a\u7b54\u6848\u53ef\u80fd\u5f88\u5927\uff0c\u6240\u4ee5\u9700\u8981\u5bf9 109 + 7
\u53d6\u4f59 \u540e\u8fd4\u56de\u3002
|x|
\u5b9a\u4e49\u4e3a\uff1a
x >= 0
\uff0c\u503c\u4e3a x
\uff0c\u6216\u8005x <= 0
\uff0c\u503c\u4e3a -x
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1anums1 = [1,7,5], nums2 = [2,3,5]\n\u8f93\u51fa\uff1a3\n\u89e3\u91ca\uff1a\u6709\u4e24\u79cd\u53ef\u80fd\u7684\u6700\u4f18\u65b9\u6848\uff1a\n- \u5c06\u7b2c\u4e8c\u4e2a\u5143\u7d20\u66ff\u6362\u4e3a\u7b2c\u4e00\u4e2a\u5143\u7d20\uff1a[1,7,5] => [1,1,5] \uff0c\u6216\u8005\n- \u5c06\u7b2c\u4e8c\u4e2a\u5143\u7d20\u66ff\u6362\u4e3a\u7b2c\u4e09\u4e2a\u5143\u7d20\uff1a[1,7,5] => [1,5,5]\n\u4e24\u79cd\u65b9\u6848\u7684\u7edd\u5bf9\u5dee\u503c\u548c\u90fd\u662f |1-2| + (|1-3| \u6216\u8005 |5-3|) + |5-5| =
3\n
\n\n\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1anums1 = [2,4,6,8,10], nums2 = [2,4,6,8,10]\n\u8f93\u51fa\uff1a0\n\u89e3\u91ca\uff1anums1 \u548c nums2 \u76f8\u7b49\uff0c\u6240\u4ee5\u4e0d\u7528\u66ff\u6362\u5143\u7d20\u3002\u7edd\u5bf9\u5dee\u503c\u548c\u4e3a 0\n\n\n
\u793a\u4f8b 3\uff1a
\n\n\n\u8f93\u5165\uff1anums1 = [1,10,4,4,2,7], nums2 = [9,3,5,1,7,4]\n\u8f93\u51fa\uff1a20\n\u89e3\u91ca\uff1a\u5c06\u7b2c\u4e00\u4e2a\u5143\u7d20\u66ff\u6362\u4e3a\u7b2c\u4e8c\u4e2a\u5143\u7d20\uff1a[1,10,4,4,2,7] => [10,10,4,4,2,7]\n\u7edd\u5bf9\u5dee\u503c\u548c\u4e3a |10-9| + |10-3| + |4-5| + |4-1| + |2-7| + |7-4| = 20
\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\nn == nums1.length
n == nums2.length
1 <= n <= 105
1 <= nums1[i], nums2[i] <= 105
You are given the logs for users' actions on LeetCode, and an integer k
. The logs are represented by a 2D integer array logs
where each logs[i] = [IDi, timei]
indicates that the user with IDi
performed an action at the minute timei
.
Multiple users can perform actions simultaneously, and a single user can perform multiple actions in the same minute.
\n\nThe user active minutes (UAM) for a given user is defined as the number of unique minutes in which the user performed an action on LeetCode. A minute can only be counted once, even if multiple actions occur during it.
\n\nYou are to calculate a 1-indexed array answer
of size k
such that, for each j
(1 <= j <= k
), answer[j]
is the number of users whose UAM equals j
.
Return the array answer
as described above.
\n
Example 1:
\n\n\nInput: logs = [[0,5],[1,2],[0,2],[0,5],[1,3]], k = 5\nOutput: [0,2,0,0,0]\nExplanation:\nThe user with ID=0 performed actions at minutes 5, 2, and 5 again. Hence, they have a UAM of 2 (minute 5 is only counted once).\nThe user with ID=1 performed actions at minutes 2 and 3. Hence, they have a UAM of 2.\nSince both users have a UAM of 2, answer[2] is 2, and the remaining answer[j] values are 0.\n\n\n
Example 2:
\n\n\nInput: logs = [[1,1],[2,2],[2,3]], k = 4\nOutput: [1,1,0,0]\nExplanation:\nThe user with ID=1 performed a single action at minute 1. Hence, they have a UAM of 1.\nThe user with ID=2 performed actions at minutes 2 and 3. Hence, they have a UAM of 2.\nThere is one user with a UAM of 1 and one with a UAM of 2.\nHence, answer[1] = 1, answer[2] = 1, and the remaining values are 0.\n\n\n
\n
Constraints:
\n\n1 <= logs.length <= 104
0 <= IDi <= 109
1 <= timei <= 105
k
is in the range [The maximum UAM for a user, 105]
.\u7ed9\u4f60\u7528\u6237\u5728 LeetCode \u7684\u64cd\u4f5c\u65e5\u5fd7\uff0c\u548c\u4e00\u4e2a\u6574\u6570 k
\u3002\u65e5\u5fd7\u7528\u4e00\u4e2a\u4e8c\u7ef4\u6574\u6570\u6570\u7ec4 logs
\u8868\u793a\uff0c\u5176\u4e2d\u6bcf\u4e2a logs[i] = [IDi, timei]
\u8868\u793a ID \u4e3a IDi
\u7684\u7528\u6237\u5728 timei
\u5206\u949f\u65f6\u6267\u884c\u4e86\u67d0\u4e2a\u64cd\u4f5c\u3002
\u591a\u4e2a\u7528\u6237 \u53ef\u4ee5\u540c\u65f6\u6267\u884c\u64cd\u4f5c\uff0c\u5355\u4e2a\u7528\u6237\u53ef\u4ee5\u5728\u540c\u4e00\u5206\u949f\u5185\u6267\u884c \u591a\u4e2a\u64cd\u4f5c \u3002
\n\n\u6307\u5b9a\u7528\u6237\u7684 \u7528\u6237\u6d3b\u8dc3\u5206\u949f\u6570\uff08user active minutes\uff0cUAM\uff09 \u5b9a\u4e49\u4e3a\u7528\u6237\u5bf9 LeetCode \u6267\u884c\u64cd\u4f5c\u7684 \u552f\u4e00\u5206\u949f\u6570 \u3002 \u5373\u4f7f\u4e00\u5206\u949f\u5185\u6267\u884c\u591a\u4e2a\u64cd\u4f5c\uff0c\u4e5f\u53ea\u80fd\u6309\u4e00\u5206\u949f\u8ba1\u6570\u3002
\n\n\u8bf7\u4f60\u7edf\u8ba1\u7528\u6237\u6d3b\u8dc3\u5206\u949f\u6570\u7684\u5206\u5e03\u60c5\u51b5\uff0c\u7edf\u8ba1\u7ed3\u679c\u662f\u4e00\u4e2a\u957f\u5ea6\u4e3a k
\u4e14 \u4e0b\u6807\u4ece 1 \u5f00\u59cb\u8ba1\u6570 \u7684\u6570\u7ec4 answer
\uff0c\u5bf9\u4e8e\u6bcf\u4e2a j
\uff081 <= j <= k
\uff09\uff0canswer[j]
\u8868\u793a \u7528\u6237\u6d3b\u8dc3\u5206\u949f\u6570 \u7b49\u4e8e j
\u7684\u7528\u6237\u6570\u3002
\u8fd4\u56de\u4e0a\u9762\u63cf\u8ff0\u7684\u7b54\u6848\u6570\u7ec4 answer
\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1alogs = [[0,5],[1,2],[0,2],[0,5],[1,3]], k = 5\n\u8f93\u51fa\uff1a[0,2,0,0,0]\n\u89e3\u91ca\uff1a\nID=0 \u7684\u7528\u6237\u6267\u884c\u64cd\u4f5c\u7684\u5206\u949f\u5206\u522b\u662f\uff1a5 \u30012 \u548c 5 \u3002\u56e0\u6b64\uff0c\u8be5\u7528\u6237\u7684\u7528\u6237\u6d3b\u8dc3\u5206\u949f\u6570\u4e3a 2\uff08\u5206\u949f 5 \u53ea\u8ba1\u6570\u4e00\u6b21\uff09\nID=1 \u7684\u7528\u6237\u6267\u884c\u64cd\u4f5c\u7684\u5206\u949f\u5206\u522b\u662f\uff1a2 \u548c 3 \u3002\u56e0\u6b64\uff0c\u8be5\u7528\u6237\u7684\u7528\u6237\u6d3b\u8dc3\u5206\u949f\u6570\u4e3a 2\n2 \u4e2a\u7528\u6237\u7684\u7528\u6237\u6d3b\u8dc3\u5206\u949f\u6570\u90fd\u662f 2 \uff0canswer[2] \u4e3a 2 \uff0c\u5176\u4f59 answer[j] \u7684\u503c\u90fd\u662f 0\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1alogs = [[1,1],[2,2],[2,3]], k = 4\n\u8f93\u51fa\uff1a[1,1,0,0]\n\u89e3\u91ca\uff1a\nID=1 \u7684\u7528\u6237\u4ec5\u5728\u5206\u949f 1 \u6267\u884c\u5355\u4e2a\u64cd\u4f5c\u3002\u56e0\u6b64\uff0c\u8be5\u7528\u6237\u7684\u7528\u6237\u6d3b\u8dc3\u5206\u949f\u6570\u4e3a 1\nID=2 \u7684\u7528\u6237\u6267\u884c\u64cd\u4f5c\u7684\u5206\u949f\u5206\u522b\u662f\uff1a2 \u548c 3 \u3002\u56e0\u6b64\uff0c\u8be5\u7528\u6237\u7684\u7528\u6237\u6d3b\u8dc3\u5206\u949f\u6570\u4e3a 2\n1 \u4e2a\u7528\u6237\u7684\u7528\u6237\u6d3b\u8dc3\u5206\u949f\u6570\u662f 1 \uff0c1 \u4e2a\u7528\u6237\u7684\u7528\u6237\u6d3b\u8dc3\u5206\u949f\u6570\u662f 2 \n\u56e0\u6b64\uff0canswer[1] = 1 \uff0canswer[2] = 1 \uff0c\u5176\u4f59\u7684\u503c\u90fd\u662f 0\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= logs.length <= 104
0 <= IDi <= 109
1 <= timei <= 105
k
\u7684\u53d6\u503c\u8303\u56f4\u662f [\u7528\u6237\u7684\u6700\u5927\u7528\u6237\u6d3b\u8dc3\u5206\u949f\u6570, 105]
A sentence is a list of words that are separated by a single space with no leading or trailing spaces. Each of the words consists of only uppercase and lowercase English letters (no punctuation).
\n\n"Hello World"
, "HELLO"
, and "hello world hello world"
are all sentences.You are given a sentence s
\u200b\u200b\u200b\u200b\u200b\u200b and an integer k
\u200b\u200b\u200b\u200b\u200b\u200b. You want to truncate s
\u200b\u200b\u200b\u200b\u200b\u200b such that it contains only the first k
\u200b\u200b\u200b\u200b\u200b\u200b words. Return s
\u200b\u200b\u200b\u200b\u200b\u200b after truncating it.
\n
Example 1:
\n\n\nInput: s = "Hello how are you Contestant", k = 4\nOutput: "Hello how are you"\nExplanation:\nThe words in s are ["Hello", "how" "are", "you", "Contestant"].\nThe first 4 words are ["Hello", "how", "are", "you"].\nHence, you should return "Hello how are you".\n\n\n
Example 2:
\n\n\nInput: s = "What is the solution to this problem", k = 4\nOutput: "What is the solution"\nExplanation:\nThe words in s are ["What", "is" "the", "solution", "to", "this", "problem"].\nThe first 4 words are ["What", "is", "the", "solution"].\nHence, you should return "What is the solution".\n\n
Example 3:
\n\n\nInput: s = "chopper is not a tanuki", k = 5\nOutput: "chopper is not a tanuki"\n\n\n
\n
Constraints:
\n\n1 <= s.length <= 500
k
is in the range [1, the number of words in s]
.s
consist of only lowercase and uppercase English letters and spaces.s
are separated by a single space.\u53e5\u5b50 \u662f\u4e00\u4e2a\u5355\u8bcd\u5217\u8868\uff0c\u5217\u8868\u4e2d\u7684\u5355\u8bcd\u4e4b\u95f4\u7528\u5355\u4e2a\u7a7a\u683c\u9694\u5f00\uff0c\u4e14\u4e0d\u5b58\u5728\u524d\u5bfc\u6216\u5c3e\u968f\u7a7a\u683c\u3002\u6bcf\u4e2a\u5355\u8bcd\u4ec5\u7531\u5927\u5c0f\u5199\u82f1\u6587\u5b57\u6bcd\u7ec4\u6210\uff08\u4e0d\u542b\u6807\u70b9\u7b26\u53f7\uff09\u3002
\n\n\"Hello World\"
\u3001\"HELLO\"
\u548c \"hello world hello world\"
\u90fd\u662f\u53e5\u5b50\u3002\u7ed9\u4f60\u4e00\u4e2a\u53e5\u5b50 s
\u200b\u200b\u200b\u200b\u200b\u200b \u548c\u4e00\u4e2a\u6574\u6570 k
\u200b\u200b\u200b\u200b\u200b\u200b \uff0c\u8bf7\u4f60\u5c06 s
\u200b\u200b \u622a\u65ad \u200b\uff0c\u200b\u200b\u200b\u4f7f\u622a\u65ad\u540e\u7684\u53e5\u5b50\u4ec5\u542b \u524d k
\u200b\u200b\u200b\u200b\u200b\u200b \u4e2a\u5355\u8bcd\u3002\u8fd4\u56de \u622a\u65ad s
\u200b\u200b\u200b\u200b\u200b\u200b \u540e\u5f97\u5230\u7684\u53e5\u5b50\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1as = \"Hello how are you Contestant\", k = 4\n\u8f93\u51fa\uff1a\"Hello how are you\"\n\u89e3\u91ca\uff1a\ns \u4e2d\u7684\u5355\u8bcd\u4e3a [\"Hello\", \"how\" \"are\", \"you\", \"Contestant\"]\n\u524d 4 \u4e2a\u5355\u8bcd\u4e3a [\"Hello\", \"how\", \"are\", \"you\"]\n\u56e0\u6b64\uff0c\u5e94\u5f53\u8fd4\u56de \"Hello how are you\"\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1as = \"What is the solution to this problem\", k = 4\n\u8f93\u51fa\uff1a\"What is the solution\"\n\u89e3\u91ca\uff1a\ns \u4e2d\u7684\u5355\u8bcd\u4e3a [\"What\", \"is\" \"the\", \"solution\", \"to\", \"this\", \"problem\"]\n\u524d 4 \u4e2a\u5355\u8bcd\u4e3a [\"What\", \"is\", \"the\", \"solution\"]\n\u56e0\u6b64\uff0c\u5e94\u5f53\u8fd4\u56de \"What is the solution\"\n\n
\u793a\u4f8b 3\uff1a
\n\n\u8f93\u5165\uff1as = \"chopper is not a tanuki\", k = 5\n\u8f93\u51fa\uff1a\"chopper is not a tanuki\"\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= s.length <= 500
k
\u7684\u53d6\u503c\u8303\u56f4\u662f [1,\u00a0 s \u4e2d\u5355\u8bcd\u7684\u6570\u76ee]
s
\u4ec5\u7531\u5927\u5c0f\u5199\u82f1\u6587\u5b57\u6bcd\u548c\u7a7a\u683c\u7ec4\u6210s
\u4e2d\u7684\u5355\u8bcd\u4e4b\u95f4\u7531\u5355\u4e2a\u7a7a\u683c\u9694\u5f00You are given two strings firstString
and secondString
that are 0-indexed and consist only of lowercase English letters. Count the number of index quadruples (i,j,a,b)
that satisfy the following conditions:
0 <= i <= j < firstString.length
0 <= a <= b < secondString.length
firstString
that starts at the ith
character and ends at the jth
character (inclusive) is equal to the substring of secondString
that starts at the ath
character and ends at the bth
character (inclusive).j - a
is the minimum possible value among all quadruples that satisfy the previous conditions.Return the number of such quadruples.
\r\n\r\n\r\n
Example 1:
\r\n\r\n\r\nInput: firstString = "abcd", secondString = "bccda"\r\nOutput: 1\r\nExplanation: The quadruple (0,0,4,4) is the only one that satisfies all the conditions and minimizes j - a.\r\n\r\n\r\n
Example 2:
\r\n\r\n\r\nInput: firstString = "ab", secondString = "cd"\r\nOutput: 0\r\nExplanation: There are no quadruples satisfying all the conditions.\r\n\r\n\r\n
\r\n
Constraints:
\r\n\r\n1 <= firstString.length, secondString.length <= 2 * 105
\u8f93\u5165\u6570\u636e\u4e3a\u4e24\u4e2a\u5b57\u7b26\u4e32firstString
\u548c secondString
\uff0c\u4e24\u4e2a\u5b57\u7b26\u4e32\u4e0b\u6807\u5747\u4ece0\u5f00\u59cb\uff0c\u4e14\u5747\u53ea\u5305\u542b\u5c0f\u5199\u7684\u82f1\u6587\u5b57\u7b26\uff0c\u8bf7\u8ba1\u7b97\u6ee1\u8db3\u4e0b\u5217\u8981\u6c42\u7684\u4e0b\u6807\u56db\u5143\u7ec4(i,j,a,b)
\u7684\u4e2a\u6570\uff1a
0 <= i <= j < firstString.length
0 <= a <= b < secondString.length
firstString
\u5b57\u7b26\u4e32\u4e2d\u4ecei
\u4f4d\u7f6e\u5230j
\u4f4d\u7f6e\u7684\u5b50\u4e32(\u5305\u62ecj
\u4f4d\u7f6e\u7684\u5b57\u7b26)\u548csecondString
\u5b57\u7b26\u4e32\u4ecea
\u4f4d\u7f6e\u5230b
\u4f4d\u7f6e\u7684\u5b50\u4e32(\u5305\u62ecb
\u4f4d\u7f6e\u5b57\u7b26)\u76f8\u7b49j-a
\u7684\u6570\u503c\u662f\u6240\u6709\u7b26\u5408\u524d\u9762\u4e09\u4e2a\u6761\u4ef6\u7684\u56db\u5143\u7ec4\u4e2d\u53ef\u80fd\u7684\u6700\u5c0f\u503c\u8fd4\u56de\u7b26\u5408\u4e0a\u8ff0 4 \u4e2a\u6761\u4ef6\u7684\u56db\u5143\u7ec4\u7684 \u4e2a\u6570 \u3002
\n\n\u00a0
\n\n\u793a\u4f8b1\uff1a
\n\n\n\u8f93\u5165\uff1afirstString = \"abcd\", secondString = \"bccda\"\n\u8f93\u51fa\uff1a1\n\u89e3\u91ca\uff1a(0,0,4,4)\u662f\u552f\u4e00\u7b26\u5408\u6761\u4ef6\u7684\u56db\u5143\u7ec4\u4e14\u5176j-a
\u7684\u6570\u503c\u662f\u6700\u5c0f\u7684.\n
\n\n\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1afirstString = \"ab\", secondString = \"cd\"\n\u8f93\u51fa\uff1a0\n\u89e3\u91ca\uff1a\u6ca1\u6709\u4efb\u4f55\u4e00\u4e2a\u56db\u5143\u7ec4\u80fd\u6ee1\u8db3\u4e0a\u8ff04\u4e2a\u8981\u6c42.\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= firstString.length, secondString.length <= 2 * 105
Table: Employee
\n+---------------+---------+\n| Column Name | Type |\n+---------------+---------+\n| employee_id | int |\n| department_id | int |\n| primary_flag | varchar |\n+---------------+---------+\n(employee_id, department_id) is the primary key for this table.\nemployee_id is the id of the employee.\ndepartment_id is the id of the department to which the employee belongs.\nprimary_flag is an ENUM of type ('Y', 'N'). If the flag is 'Y', the department is the primary department for the employee. If the flag is 'N', the department is not the primary.\n\n\n
\n\n
Employees can belong to multiple departments. When the employee joins other departments, they need to decide which department is their primary department. Note that when an employee belongs to only one department, their primary column is 'N'
.
Write an SQL query to report all the employees with their primary department. For employees who belong to one department, report their only department.
\n\nReturn the result table in any order.
\n\nThe query result format is in the following example.
\n\n\n\n
\nEmployee table:\n+-------------+---------------+--------------+\n| employee_id | department_id | primary_flag |\n+-------------+---------------+--------------+\n| 1 | 1 | N |\n| 2 | 1 | Y |\n| 2 | 2 | N |\n| 3 | 3 | N |\n| 4 | 2 | N |\n| 4 | 3 | Y |\n| 4 | 4 | N |\n+-------------+---------------+--------------+\n\nResult table:\n+-------------+---------------+\n| employee_id | department_id |\n+-------------+---------------+\n| 1 | 1 |\n| 2 | 1 |\n| 3 | 3 |\n| 4 | 3 |\n+-------------+---------------+\n- The Primary department for employee 1 is 1.\n- The Primary department for employee 2 is 1.\n- The Primary department for employee 3 is 3.\n- The Primary department for employee 4 is 3.\n\n
\n", "content_cn": "
Table: Employee
+---------------+---------+\n| Column Name | Type |\n+---------------+---------+\n| employee_id | int |\n| department_id | int |\n| primary_flag | varchar |\n+---------------+---------+\n\u8fd9\u5f20\u8868\u7684\u4e3b\u952e\u4e3a employee_id, department_id\nemployee_id \u662f\u5458\u5de5\u7684ID\ndepartment_id \u662f\u90e8\u95e8\u7684ID\uff0c\u8868\u793a\u5458\u5de5\u4e0e\u8be5\u90e8\u95e8\u6709\u5173\u7cfb\nprimary_flag \u662f\u4e00\u4e2a\u679a\u4e3e\u7c7b\u578b\uff0c\u503c\u5206\u522b\u4e3a('Y', 'N'). \u5982\u679c\u503c\u4e3a'Y',\u8868\u793a\u8be5\u90e8\u95e8\u662f\u5458\u5de5\u7684\u76f4\u5c5e\u90e8\u95e8\u3002 \u5982\u679c\u503c\u662f'N',\u5219\u5426\n\n\n
\u00a0
\n\n\u4e00\u4e2a\u5458\u5de5\u53ef\u4ee5\u5c5e\u4e8e\u591a\u4e2a\u90e8\u95e8\u3002
\n\n\u5f53\u4e00\u4e2a\u5458\u5de5\u52a0\u5165\u8d85\u8fc7\u4e00\u4e2a\u90e8\u95e8\u7684\u65f6\u5019\uff0c\u4ed6\u9700\u8981\u51b3\u5b9a\u54ea\u4e2a\u90e8\u95e8\u662f\u4ed6\u7684\u76f4\u5c5e\u90e8\u95e8\u3002
\n\n\u8bf7\u6ce8\u610f\uff0c\u5f53\u5458\u5de5\u53ea\u52a0\u5165\u4e00\u4e2a\u90e8\u95e8\u7684\u65f6\u5019\uff0c\u90a3\u8fd9\u4e2a\u90e8\u95e8\u5c06\u9ed8\u8ba4\u4e3a\u4ed6\u7684\u76f4\u5c5e\u90e8\u95e8\uff0c\u867d\u7136\u8868\u8bb0\u5f55\u7684\u503c\u4e3a'N'
.
\u8bf7\u7f16\u5199\u4e00\u6bb5SQL\uff0c\u67e5\u51fa\u5458\u5de5\u6240\u5c5e\u7684\u76f4\u5c5e\u90e8\u95e8\u3002
\n\n\u8fd4\u56de\u7ed3\u679c\u6ca1\u6709\u987a\u5e8f\u8981\u6c42\u3002
\n\n\u00a0
\n\n\u793a\u4f8b\uff1a
\n\nEmployee table:\n+-------------+---------------+--------------+\n| employee_id | department_id | primary_flag |\n+-------------+---------------+--------------+\n| 1 | 1 | N |\n| 2 | 1 | Y |\n| 2 | 2 | N |\n| 3 | 3 | N |\n| 4 | 2 | N |\n| 4 | 3 | Y |\n| 4 | 4 | N |\n+-------------+---------------+--------------+\n\nResult table:\n+-------------+---------------+\n| employee_id | department_id |\n+-------------+---------------+\n| 1 | 1 |\n| 2 | 1 |\n| 3 | 3 |\n| 4 | 3 |\n+-------------+---------------+\n- \u5458\u5de51\u7684\u76f4\u5c5e\u90e8\u95e8\u662f1\n- \u5458\u5de52\u7684\u76f4\u5c5e\u90e8\u95e8\u662f1\n- \u5458\u5de53\u7684\u76f4\u5c5e\u90e8\u95e8\u662f3\n- \u5458\u5de54\u7684\u76f4\u5c5e\u90e8\u95e8\u662f3\n\n
\u00a0
\n", "tags_en": [], "tags_cn": [], "difficulty_en": "Easy", "difficulty_cn": "\u7b80\u5355", "code_snippets": [{"lang": "MySQL", "langSlug": "mysql", "code": "# Write your MySQL query statement below", "__typename": "CodeSnippetNode"}, {"lang": "MS SQL Server", "langSlug": "mssql", "code": "/* Write your T-SQL query statement below */", "__typename": "CodeSnippetNode"}, {"lang": "Oracle", "langSlug": "oraclesql", "code": "/* Write your PL/SQL query statement below */", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1789](https://leetcode-cn.com/problems/primary-department-for-each-employee)", "[\u5458\u5de5\u7684\u76f4\u5c5e\u90e8\u95e8](/solution/1700-1799/1789.Primary%20Department%20for%20Each%20Employee/README.md)", "", "\u7b80\u5355", "\ud83d\udd12"], "md_table_row_en": ["[1789](https://leetcode.com/problems/primary-department-for-each-employee)", "[Primary Department for Each Employee](/solution/1700-1799/1789.Primary%20Department%20for%20Each%20Employee/README_EN.md)", "", "Easy", "\ud83d\udd12"]}, {"question_id": "1941", "frontend_question_id": "1830", "paid_only": false, "paid_only_cn": false, "url_cn": "https://leetcode-cn.com/problems/minimum-number-of-operations-to-make-string-sorted", "url_en": "https://leetcode.com/problems/minimum-number-of-operations-to-make-string-sorted", "relative_path_cn": "/solution/1800-1899/1830.Minimum%20Number%20of%20Operations%20to%20Make%20String%20Sorted/README.md", "relative_path_en": "/solution/1800-1899/1830.Minimum%20Number%20of%20Operations%20to%20Make%20String%20Sorted/README_EN.md", "title_cn": "\u4f7f\u5b57\u7b26\u4e32\u6709\u5e8f\u7684\u6700\u5c11\u64cd\u4f5c\u6b21\u6570", "title_en": "Minimum Number of Operations to Make String Sorted", "question_title_slug": "minimum-number-of-operations-to-make-string-sorted", "content_en": "You are given a string s
(0-indexed)\u200b\u200b\u200b\u200b\u200b\u200b. You are asked to perform the following operation on s
\u200b\u200b\u200b\u200b\u200b\u200b until you get a sorted string:
i
such that 1 <= i < s.length
and s[i] < s[i - 1]
.j
such that i <= j < s.length
and s[k] < s[i - 1]
for all the possible values of k
in the range [i, j]
inclusive.i - 1
\u200b\u200b\u200b\u200b and j
\u200b\u200b\u200b\u200b\u200b.i
\u200b\u200b\u200b\u200b\u200b\u200b.Return the number of operations needed to make the string sorted. Since the answer can be too large, return it modulo 109 + 7
.
\n
Example 1:
\n\n\nInput: s = "cba"\nOutput: 5\nExplanation: The simulation goes as follows:\nOperation 1: i=2, j=2. Swap s[1] and s[2] to get s="cab", then reverse the suffix starting at 2. Now, s="cab".\nOperation 2: i=1, j=2. Swap s[0] and s[2] to get s="bac", then reverse the suffix starting at 1. Now, s="bca".\nOperation 3: i=2, j=2. Swap s[1] and s[2] to get s="bac", then reverse the suffix starting at 2. Now, s="bac".\nOperation 4: i=1, j=1. Swap s[0] and s[1] to get s="abc", then reverse the suffix starting at 1. Now, s="acb".\nOperation 5: i=2, j=2. Swap s[1] and s[2] to get s="abc", then reverse the suffix starting at 2. Now, s="abc".\n\n\n
Example 2:
\n\n\nInput: s = "aabaa"\nOutput: 2\nExplanation: The simulation goes as follows:\nOperation 1: i=3, j=4. Swap s[2] and s[4] to get s="aaaab", then reverse the substring starting at 3. Now, s="aaaba".\nOperation 2: i=4, j=4. Swap s[3] and s[4] to get s="aaaab", then reverse the substring starting at 4. Now, s="aaaab".\n\n\n
Example 3:
\n\n\nInput: s = "cdbea"\nOutput: 63\n\n
Example 4:
\n\n\nInput: s = "leetcodeleetcodeleetcode"\nOutput: 982157772\n\n\n
\n
Constraints:
\n\n1 <= s.length <= 3000
s
\u200b\u200b\u200b\u200b\u200b\u200b consists only of lowercase English letters.\u7ed9\u4f60\u4e00\u4e2a\u5b57\u7b26\u4e32\u00a0s
\u00a0\uff08\u4e0b\u6807\u4ece 0 \u5f00\u59cb\uff09\u3002\u4f60\u9700\u8981\u5bf9 s
\u00a0\u6267\u884c\u4ee5\u4e0b\u64cd\u4f5c\u76f4\u5230\u5b83\u53d8\u4e3a\u4e00\u4e2a\u6709\u5e8f\u5b57\u7b26\u4e32\uff1a
i
\u00a0\uff0c\u4f7f\u5f97\u00a01 <= i < s.length
\u4e14\u00a0s[i] < s[i - 1]
\u00a0\u3002j
\u00a0\uff0c\u4f7f\u5f97\u00a0i <= j < s.length
\u4e14\u5bf9\u4e8e\u6240\u6709\u5728\u95ed\u533a\u95f4\u00a0[i, j]
\u00a0\u4e4b\u95f4\u7684\u00a0k
\u00a0\u90fd\u6709\u00a0s[k] < s[i - 1]
\u00a0\u3002i - 1
\u200b\u200b\u200b\u200b \u548c\u00a0j
\u200b\u200b\u200b\u200b \u5904\u7684\u4e24\u4e2a\u5b57\u7b26\u3002i
\u00a0\u5f00\u59cb\u7684\u5b57\u7b26\u4e32\u540e\u7f00\u53cd\u8f6c\u3002\u8bf7\u4f60\u8fd4\u56de\u5c06\u5b57\u7b26\u4e32\u53d8\u6210\u6709\u5e8f\u7684\u6700\u5c11\u64cd\u4f5c\u6b21\u6570\u3002\u7531\u4e8e\u7b54\u6848\u53ef\u80fd\u4f1a\u5f88\u5927\uff0c\u8bf7\u8fd4\u56de\u5b83\u5bf9\u00a0109 + 7
\u00a0\u53d6\u4f59\u00a0\u7684\u7ed3\u679c\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1as = \"cba\"\n\u8f93\u51fa\uff1a5\n\u89e3\u91ca\uff1a\u6a21\u62df\u8fc7\u7a0b\u5982\u4e0b\u6240\u793a\uff1a\n\u64cd\u4f5c 1\uff1ai=2\uff0cj=2\u3002\u4ea4\u6362 s[1] \u548c s[2] \u5f97\u5230 s=\"cab\" \uff0c\u7136\u540e\u53cd\u8f6c\u4e0b\u6807\u4ece 2 \u5f00\u59cb\u7684\u540e\u7f00\u5b57\u7b26\u4e32\uff0c\u5f97\u5230 s=\"cab\" \u3002\n\u64cd\u4f5c 2\uff1ai=1\uff0cj=2\u3002\u4ea4\u6362 s[0] \u548c s[2] \u5f97\u5230 s=\"bac\" \uff0c\u7136\u540e\u53cd\u8f6c\u4e0b\u6807\u4ece 1 \u5f00\u59cb\u7684\u540e\u7f00\u5b57\u7b26\u4e32\uff0c\u5f97\u5230 s=\"bca\" \u3002\n\u64cd\u4f5c 3\uff1ai=2\uff0cj=2\u3002\u4ea4\u6362 s[1] \u548c s[2] \u5f97\u5230 s=\"bac\" \uff0c\u7136\u540e\u53cd\u8f6c\u4e0b\u6807\u4ece 2 \u5f00\u59cb\u7684\u540e\u7f00\u5b57\u7b26\u4e32\uff0c\u5f97\u5230 s=\"bac\" \u3002\n\u64cd\u4f5c 4\uff1ai=1\uff0cj=1\u3002\u4ea4\u6362 s[0] \u548c s[1] \u5f97\u5230 s=\"abc\" \uff0c\u7136\u540e\u53cd\u8f6c\u4e0b\u6807\u4ece 1 \u5f00\u59cb\u7684\u540e\u7f00\u5b57\u7b26\u4e32\uff0c\u5f97\u5230 s=\"acb\" \u3002\n\u64cd\u4f5c 5\uff1ai=2\uff0cj=2\u3002\u4ea4\u6362 s[1] \u548c s[2] \u5f97\u5230 s=\"abc\" \uff0c\u7136\u540e\u53cd\u8f6c\u4e0b\u6807\u4ece 2 \u5f00\u59cb\u7684\u540e\u7f00\u5b57\u7b26\u4e32\uff0c\u5f97\u5230 s=\"abc\" \u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1as = \"aabaa\"\n\u8f93\u51fa\uff1a2\n\u89e3\u91ca\uff1a\u6a21\u62df\u8fc7\u7a0b\u5982\u4e0b\u6240\u793a\uff1a\n\u64cd\u4f5c 1\uff1ai=3\uff0cj=4\u3002\u4ea4\u6362 s[2] \u548c s[4] \u5f97\u5230 s=\"aaaab\" \uff0c\u7136\u540e\u53cd\u8f6c\u4e0b\u6807\u4ece 3 \u5f00\u59cb\u7684\u540e\u7f00\u5b57\u7b26\u4e32\uff0c\u5f97\u5230 s=\"aaaba\" \u3002\n\u64cd\u4f5c 2\uff1ai=4\uff0cj=4\u3002\u4ea4\u6362 s[3] \u548c s[4] \u5f97\u5230 s=\"aaaab\" \uff0c\u7136\u540e\u53cd\u8f6c\u4e0b\u6807\u4ece 4 \u5f00\u59cb\u7684\u540e\u7f00\u5b57\u7b26\u4e32\uff0c\u5f97\u5230 s=\"aaaab\" \u3002\n\n\n
\u793a\u4f8b 3\uff1a
\n\n\u8f93\u5165\uff1as = \"cdbea\"\n\u8f93\u51fa\uff1a63\n\n
\u793a\u4f8b 4\uff1a
\n\n\u8f93\u5165\uff1as = \"leetcodeleetcodeleetcode\"\n\u8f93\u51fa\uff1a982157772\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= s.length <= 3000
s
\u200b \u53ea\u5305\u542b\u5c0f\u5199\u82f1\u6587\u5b57\u6bcd\u3002You are given a sorted array nums
of n
non-negative integers and an integer maximumBit
. You want to perform the following query n
times:
k < 2maximumBit
such that nums[0] XOR nums[1] XOR ... XOR nums[nums.length-1] XOR k
is maximized. k
is the answer to the ith
query.nums
.Return an array answer
, where answer[i]
is the answer to the ith
query.
\n
Example 1:
\n\n\nInput: nums = [0,1,1,3], maximumBit = 2\nOutput: [0,3,2,3]\nExplanation: The queries are answered as follows:\n1st query: nums = [0,1,1,3], k = 0 since 0 XOR 1 XOR 1 XOR 3 XOR 0 = 3.\n2nd query: nums = [0,1,1], k = 3 since 0 XOR 1 XOR 1 XOR 3 = 3.\n3rd query: nums = [0,1], k = 2 since 0 XOR 1 XOR 2 = 3.\n4th query: nums = [0], k = 3 since 0 XOR 3 = 3.\n\n\n
Example 2:
\n\n\nInput: nums = [2,3,4,7], maximumBit = 3\nOutput: [5,2,6,5]\nExplanation: The queries are answered as follows:\n1st query: nums = [2,3,4,7], k = 5 since 2 XOR 3 XOR 4 XOR 7 XOR 5 = 7.\n2nd query: nums = [2,3,4], k = 2 since 2 XOR 3 XOR 4 XOR 2 = 7.\n3rd query: nums = [2,3], k = 6 since 2 XOR 3 XOR 6 = 7.\n4th query: nums = [2], k = 5 since 2 XOR 5 = 7.\n\n\n
Example 3:
\n\n\nInput: nums = [0,1,2,2,5,7], maximumBit = 3\nOutput: [4,3,6,4,6,7]\n\n\n
\n
Constraints:
\n\nnums.length == n
1 <= n <= 105
1 <= maximumBit <= 20
0 <= nums[i] < 2maximumBit
nums
\u200b\u200b\u200b is sorted in ascending order.\u7ed9\u4f60\u4e00\u4e2a \u6709\u5e8f\u00a0\u6570\u7ec4\u00a0nums
\u00a0\uff0c\u5b83\u7531\u00a0n
\u00a0\u4e2a\u975e\u8d1f\u6574\u6570\u7ec4\u6210\uff0c\u540c\u65f6\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u00a0maximumBit
\u00a0\u3002\u4f60\u9700\u8981\u6267\u884c\u4ee5\u4e0b\u67e5\u8be2 n
\u00a0\u6b21\uff1a
k < 2maximumBit
\u00a0\uff0c\u4f7f\u5f97\u00a0nums[0] XOR nums[1] XOR ... XOR nums[nums.length-1] XOR k
\u00a0\u7684\u7ed3\u679c \u6700\u5927\u5316\u00a0\u3002k
\u00a0\u662f\u7b2c i
\u00a0\u4e2a\u67e5\u8be2\u7684\u7b54\u6848\u3002nums
\u00a0\u5220\u9664\u00a0\u6700\u540e\u00a0\u4e00\u4e2a\u5143\u7d20\u3002\u8bf7\u4f60\u8fd4\u56de\u4e00\u4e2a\u6570\u7ec4\u00a0answer
\u00a0\uff0c\u5176\u4e2d\u00a0answer[i]
\u662f\u7b2c\u00a0i
\u00a0\u4e2a\u67e5\u8be2\u7684\u7ed3\u679c\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1anums = [0,1,1,3], maximumBit = 2\n\u8f93\u51fa\uff1a[0,3,2,3]\n\u89e3\u91ca\uff1a\u67e5\u8be2\u7684\u7b54\u6848\u5982\u4e0b\uff1a\n\u7b2c\u4e00\u4e2a\u67e5\u8be2\uff1anums = [0,1,1,3]\uff0ck = 0\uff0c\u56e0\u4e3a 0 XOR 1 XOR 1 XOR 3 XOR 0 = 3 \u3002\n\u7b2c\u4e8c\u4e2a\u67e5\u8be2\uff1anums = [0,1,1]\uff0ck = 3\uff0c\u56e0\u4e3a 0 XOR 1 XOR 1 XOR 3 = 3 \u3002\n\u7b2c\u4e09\u4e2a\u67e5\u8be2\uff1anums = [0,1]\uff0ck = 2\uff0c\u56e0\u4e3a 0 XOR 1 XOR 2 = 3 \u3002\n\u7b2c\u56db\u4e2a\u67e5\u8be2\uff1anums = [0]\uff0ck = 3\uff0c\u56e0\u4e3a 0 XOR 3 = 3 \u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1anums = [2,3,4,7], maximumBit = 3\n\u8f93\u51fa\uff1a[5,2,6,5]\n\u89e3\u91ca\uff1a\u67e5\u8be2\u7684\u7b54\u6848\u5982\u4e0b\uff1a\n\u7b2c\u4e00\u4e2a\u67e5\u8be2\uff1anums = [2,3,4,7]\uff0ck = 5\uff0c\u56e0\u4e3a 2 XOR 3 XOR 4 XOR 7 XOR 5 = 7\u3002\n\u7b2c\u4e8c\u4e2a\u67e5\u8be2\uff1anums = [2,3,4]\uff0ck = 2\uff0c\u56e0\u4e3a 2 XOR 3 XOR 4 XOR 2 = 7 \u3002\n\u7b2c\u4e09\u4e2a\u67e5\u8be2\uff1anums = [2,3]\uff0ck = 6\uff0c\u56e0\u4e3a 2 XOR 3 XOR 6 = 7 \u3002\n\u7b2c\u56db\u4e2a\u67e5\u8be2\uff1anums = [2]\uff0ck = 5\uff0c\u56e0\u4e3a 2 XOR 5 = 7 \u3002\n\n\n
\u793a\u4f8b 3\uff1a
\n\n\n\u8f93\u5165\uff1anums = [0,1,2,2,5,7], maximumBit = 3\n\u8f93\u51fa\uff1a[4,3,6,4,6,7]\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\nnums.length == n
1 <= n <= 105
1 <= maximumBit <= 20
0 <= nums[i] < 2maximumBit
nums
\u200b\u200b\u200b \u4e2d\u7684\u6570\u5b57\u5df2\u7ecf\u6309\u00a0\u5347\u5e8f\u00a0\u6392\u597d\u5e8f\u3002You are given an array points
where points[i] = [xi, yi]
is the coordinates of the ith
point on a 2D plane. Multiple points can have the same coordinates.
You are also given an array queries
where queries[j] = [xj, yj, rj]
describes a circle centered at (xj, yj)
with a radius of rj
.
For each query queries[j]
, compute the number of points inside the jth
circle. Points on the border of the circle are considered inside.
Return an array answer
, where answer[j]
is the answer to the jth
query.
\n
Example 1:
\n\nInput: points = [[1,3],[3,3],[5,3],[2,2]], queries = [[2,3,1],[4,3,1],[1,1,2]]\nOutput: [3,2,2]\nExplanation: The points and circles are shown above.\nqueries[0] is the green circle, queries[1] is the red circle, and queries[2] is the blue circle.\n\n\n
Example 2:
\n\nInput: points = [[1,1],[2,2],[3,3],[4,4],[5,5]], queries = [[1,2,2],[2,2,2],[4,3,2],[4,3,3]]\nOutput: [2,3,2,4]\nExplanation: The points and circles are shown above.\nqueries[0] is green, queries[1] is red, queries[2] is blue, and queries[3] is purple.\n\n\n
\n
Constraints:
\n\n1 <= points.length <= 500
points[i].length == 2
0 <= x\u200b\u200b\u200b\u200b\u200b\u200bi, y\u200b\u200b\u200b\u200b\u200b\u200bi <= 500
1 <= queries.length <= 500
queries[j].length == 3
0 <= xj, yj <= 500
1 <= rj <= 500
\n
Follow up: Could you find the answer for each query in better complexity than O(n)
?
\u7ed9\u4f60\u4e00\u4e2a\u6570\u7ec4\u00a0points
\u00a0\uff0c\u5176\u4e2d\u00a0points[i] = [xi, yi]
\u00a0\uff0c\u8868\u793a\u7b2c\u00a0i
\u00a0\u4e2a\u70b9\u5728\u4e8c\u7ef4\u5e73\u9762\u4e0a\u7684\u5750\u6807\u3002\u591a\u4e2a\u70b9\u53ef\u80fd\u4f1a\u6709 \u76f8\u540c\u00a0\u7684\u5750\u6807\u3002
\u540c\u65f6\u7ed9\u4f60\u4e00\u4e2a\u6570\u7ec4\u00a0queries
\u00a0\uff0c\u5176\u4e2d\u00a0queries[j] = [xj, yj, rj]
\u00a0\uff0c\u8868\u793a\u4e00\u4e2a\u5706\u5fc3\u5728\u00a0(xj, yj)
\u00a0\u4e14\u534a\u5f84\u4e3a\u00a0rj
\u00a0\u7684\u5706\u3002
\u5bf9\u4e8e\u6bcf\u4e00\u4e2a\u67e5\u8be2\u00a0queries[j]
\u00a0\uff0c\u8ba1\u7b97\u5728\u7b2c j
\u00a0\u4e2a\u5706 \u5185\u00a0\u70b9\u7684\u6570\u76ee\u3002\u5982\u679c\u4e00\u4e2a\u70b9\u5728\u5706\u7684 \u8fb9\u754c\u4e0a\u00a0\uff0c\u6211\u4eec\u540c\u6837\u8ba4\u4e3a\u5b83\u5728\u5706\u00a0\u5185\u00a0\u3002
\u8bf7\u4f60\u8fd4\u56de\u4e00\u4e2a\u6570\u7ec4\u00a0answer
\u00a0\uff0c\u5176\u4e2d\u00a0answer[j]
\u662f\u7b2c\u00a0j
\u00a0\u4e2a\u67e5\u8be2\u7684\u7b54\u6848\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\u8f93\u5165\uff1apoints = [[1,3],[3,3],[5,3],[2,2]], queries = [[2,3,1],[4,3,1],[1,1,2]]\n\u8f93\u51fa\uff1a[3,2,2]\n\u89e3\u91ca\uff1a\u6240\u6709\u7684\u70b9\u548c\u5706\u5982\u4e0a\u56fe\u6240\u793a\u3002\nqueries[0] \u662f\u7eff\u8272\u7684\u5706\uff0cqueries[1] \u662f\u7ea2\u8272\u7684\u5706\uff0cqueries[2] \u662f\u84dd\u8272\u7684\u5706\u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\u8f93\u5165\uff1apoints = [[1,1],[2,2],[3,3],[4,4],[5,5]], queries = [[1,2,2],[2,2,2],[4,3,2],[4,3,3]]\n\u8f93\u51fa\uff1a[2,3,2,4]\n\u89e3\u91ca\uff1a\u6240\u6709\u7684\u70b9\u548c\u5706\u5982\u4e0a\u56fe\u6240\u793a\u3002\nqueries[0] \u662f\u7eff\u8272\u7684\u5706\uff0cqueries[1] \u662f\u7ea2\u8272\u7684\u5706\uff0cqueries[2] \u662f\u84dd\u8272\u7684\u5706\uff0cqueries[3] \u662f\u7d2b\u8272\u7684\u5706\u3002\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= points.length <= 500
points[i].length == 2
0 <= x\u200b\u200b\u200b\u200b\u200b\u200bi, y\u200b\u200b\u200b\u200b\u200b\u200bi <= 500
1 <= queries.length <= 500
queries[j].length == 3
0 <= xj, yj <= 500
1 <= rj <= 500
You are given an integer array nums
(0-indexed). In one operation, you can choose an element of the array and increment it by 1
.
nums = [1,2,3]
, you can choose to increment nums[1]
to make nums = [1,3,3]
.Return the minimum number of operations needed to make nums
strictly increasing.
An array nums
is strictly increasing if nums[i] < nums[i+1]
for all 0 <= i < nums.length - 1
. An array of length 1
is trivially strictly increasing.
\r\n
Example 1:
\r\n\r\n\r\nInput: nums = [1,1,1]\r\nOutput: 3\r\nExplanation: You can do the following operations:\r\n1) Increment nums[2], so nums becomes [1,1,2].\r\n2) Increment nums[1], so nums becomes [1,2,2].\r\n3) Increment nums[2], so nums becomes [1,2,3].\r\n\r\n\r\n
Example 2:
\r\n\r\n\r\nInput: nums = [1,5,2,4,1]\r\nOutput: 14\r\n\r\n\r\n
Example 3:
\r\n\r\n\r\nInput: nums = [8]\r\nOutput: 0\r\n\r\n\r\n
\r\n
Constraints:
\r\n\r\n1 <= nums.length <= 5000
1 <= nums[i] <= 104
\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u6570\u7ec4\u00a0nums
\u00a0\uff08\u4e0b\u6807\u4ece 0 \u5f00\u59cb\uff09\u3002\u6bcf\u4e00\u6b21\u64cd\u4f5c\u4e2d\uff0c\u4f60\u53ef\u4ee5\u9009\u62e9\u6570\u7ec4\u4e2d\u4e00\u4e2a\u5143\u7d20\uff0c\u5e76\u5c06\u5b83\u589e\u52a0\u00a01
\u00a0\u3002
nums = [1,2,3]
\u00a0\uff0c\u4f60\u53ef\u4ee5\u9009\u62e9\u589e\u52a0\u00a0nums[1]
\u00a0\u5f97\u5230\u00a0nums = [1,3,3]
\u00a0\u3002\u8bf7\u4f60\u8fd4\u56de\u4f7f nums
\u00a0\u4e25\u683c\u9012\u589e\u00a0\u7684 \u6700\u5c11\u00a0\u64cd\u4f5c\u6b21\u6570\u3002
\u6211\u4eec\u79f0\u6570\u7ec4\u00a0nums
\u00a0\u662f \u4e25\u683c\u9012\u589e\u7684\u00a0\uff0c\u5f53\u5b83\u6ee1\u8db3\u5bf9\u4e8e\u6240\u6709\u7684\u00a00 <= i < nums.length - 1
\u00a0\u90fd\u6709\u00a0nums[i] < nums[i+1]
\u00a0\u3002\u4e00\u4e2a\u957f\u5ea6\u4e3a 1
\u00a0\u7684\u6570\u7ec4\u662f\u4e25\u683c\u9012\u589e\u7684\u4e00\u79cd\u7279\u6b8a\u60c5\u51b5\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1anums = [1,1,1]\n\u8f93\u51fa\uff1a3\n\u89e3\u91ca\uff1a\u4f60\u53ef\u4ee5\u8fdb\u884c\u5982\u4e0b\u64cd\u4f5c\uff1a\n1) \u589e\u52a0 nums[2] \uff0c\u6570\u7ec4\u53d8\u4e3a [1,1,2] \u3002\n2) \u589e\u52a0 nums[1] \uff0c\u6570\u7ec4\u53d8\u4e3a [1,2,2] \u3002\n3) \u589e\u52a0 nums[2] \uff0c\u6570\u7ec4\u53d8\u4e3a [1,2,3] \u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1anums = [1,5,2,4,1]\n\u8f93\u51fa\uff1a14\n\n\n
\u793a\u4f8b 3\uff1a
\n\n\u8f93\u5165\uff1anums = [8]\n\u8f93\u51fa\uff1a0\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= nums.length <= 5000
1 <= nums[i] <= 104
There is a garden of n
flowers, and each flower has an integer beauty value. The flowers are arranged in a line. You are given an integer array flowers
of size n
and each flowers[i]
represents the beauty of the ith
flower.
A garden is valid if it meets these conditions:
\r\n\r\nAs the appointed gardener, you have the ability to remove any (possibly none) flowers from the garden. You want to remove flowers in a way that makes the remaining garden valid. The beauty of the garden is the sum of the beauty of all the remaining flowers.
\r\n\r\nReturn the maximum possible beauty of some valid garden after you have removed any (possibly none) flowers.
\r\n\r\n\r\n
Example 1:
\r\n\r\n\r\nInput: flowers = [1,2,3,1,2]\r\nOutput: 8\r\nExplanation: You can produce the valid garden [2,3,1,2] to have a total beauty of 2 + 3 + 1 + 2 = 8.\r\n\r\n
Example 2:
\r\n\r\n\r\nInput: flowers = [100,1,1,-3,1]\r\nOutput: 3\r\nExplanation: You can produce the valid garden [1,1,1] to have a total beauty of 1 + 1 + 1 = 3.\r\n\r\n\r\n
Example 3:
\r\n\r\n\r\nInput: flowers = [-1,-2,0,-1]\r\nOutput: -2\r\nExplanation: You can produce the valid garden [-1,-1] to have a total beauty of -1 + -1 = -2.\r\n\r\n\r\n
\r\n
Constraints:
\r\n\r\n2 <= flowers.length <= 105
-104 <= flowers[i] <= 104
You are given a positive integer primeFactors
. You are asked to construct a positive integer n
that satisfies the following conditions:
n
(not necessarily distinct) is at most primeFactors
.n
is maximized. Note that a divisor of n
is nice if it is divisible by every prime factor of n
. For example, if n = 12
, then its prime factors are [2,2,3]
, then 6
and 12
are nice divisors, while 3
and 4
are not.Return the number of nice divisors of n
. Since that number can be too large, return it modulo 109 + 7
.
Note that a prime number is a natural number greater than 1
that is not a product of two smaller natural numbers. The prime factors of a number n
is a list of prime numbers such that their product equals n
.
\r\n
Example 1:
\r\n\r\n\r\nInput: primeFactors = 5\r\nOutput: 6\r\nExplanation: 200 is a valid value of n.\r\nIt has 5 prime factors: [2,2,2,5,5], and it has 6 nice divisors: [10,20,40,50,100,200].\r\nThere is not other value of n that has at most 5 prime factors and more nice divisors.\r\n\r\n\r\n
Example 2:
\r\n\r\n\r\nInput: primeFactors = 8\r\nOutput: 18\r\n\r\n\r\n
\r\n
Constraints:
\r\n\r\n1 <= primeFactors <= 109
\u7ed9\u4f60\u4e00\u4e2a\u6b63\u6574\u6570\u00a0primeFactors
\u00a0\u3002\u4f60\u9700\u8981\u6784\u9020\u4e00\u4e2a\u6b63\u6574\u6570\u00a0n
\u00a0\uff0c\u5b83\u6ee1\u8db3\u4ee5\u4e0b\u6761\u4ef6\uff1a
n
\u00a0\u8d28\u56e0\u6570\uff08\u8d28\u56e0\u6570\u9700\u8981\u8003\u8651\u91cd\u590d\u7684\u60c5\u51b5\uff09\u7684\u6570\u76ee \u4e0d\u8d85\u8fc7\u00a0primeFactors
\u00a0\u4e2a\u3002n
\u00a0\u597d\u56e0\u5b50\u7684\u6570\u76ee\u6700\u5927\u5316\u3002\u5982\u679c n
\u00a0\u7684\u4e00\u4e2a\u56e0\u5b50\u53ef\u4ee5\u88ab n
\u00a0\u7684\u6bcf\u4e00\u4e2a\u8d28\u56e0\u6570\u6574\u9664\uff0c\u6211\u4eec\u79f0\u8fd9\u4e2a\u56e0\u5b50\u662f \u597d\u56e0\u5b50 \u3002\u6bd4\u65b9\u8bf4\uff0c\u5982\u679c\u00a0n = 12
\u00a0\uff0c\u90a3\u4e48\u5b83\u7684\u8d28\u56e0\u6570\u4e3a\u00a0[2,2,3]
\u00a0\uff0c\u90a3\u4e48\u00a06
\u00a0\u548c\u00a012
\u00a0\u662f\u597d\u56e0\u5b50\uff0c\u4f46\u00a03
\u548c\u00a04
\u00a0\u4e0d\u662f\u3002\u8bf7\u4f60\u8fd4\u56de\u00a0n
\u00a0\u7684\u597d\u56e0\u5b50\u7684\u6570\u76ee\u3002\u7531\u4e8e\u7b54\u6848\u53ef\u80fd\u4f1a\u5f88\u5927\uff0c\u8bf7\u8fd4\u56de\u7b54\u6848\u5bf9\u00a0109 + 7
\u00a0\u53d6\u4f59\u00a0\u7684\u7ed3\u679c\u3002
\u8bf7\u6ce8\u610f\uff0c\u4e00\u4e2a\u8d28\u6570\u7684\u5b9a\u4e49\u662f\u5927\u4e8e 1
\u00a0\uff0c\u4e14\u4e0d\u80fd\u88ab\u5206\u89e3\u4e3a\u4e24\u4e2a\u5c0f\u4e8e\u8be5\u6570\u7684\u81ea\u7136\u6570\u76f8\u4e58\u3002\u4e00\u4e2a\u6570 n
\u00a0\u7684\u8d28\u56e0\u5b50\u662f\u5c06 n
\u00a0\u5206\u89e3\u4e3a\u82e5\u5e72\u4e2a\u8d28\u56e0\u5b50\uff0c\u4e14\u5b83\u4eec\u7684\u4e58\u79ef\u4e3a n
\u00a0\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1aprimeFactors = 5\n\u8f93\u51fa\uff1a6\n\u89e3\u91ca\uff1a200 \u662f\u4e00\u4e2a\u53ef\u884c\u7684 n \u3002\n\u5b83\u6709 5 \u4e2a\u8d28\u56e0\u5b50\uff1a[2,2,2,5,5] \uff0c\u4e14\u6709 6 \u4e2a\u597d\u56e0\u5b50\uff1a[10,20,40,50,100,200] \u3002\n\u4e0d\u5b58\u5728\u522b\u7684 n \u6709\u81f3\u591a 5 \u4e2a\u8d28\u56e0\u5b50\uff0c\u4e14\u540c\u65f6\u6709\u66f4\u591a\u7684\u597d\u56e0\u5b50\u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1aprimeFactors = 8\n\u8f93\u51fa\uff1a18\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= primeFactors <= 109
You are given an even integer n
\u200b\u200b\u200b\u200b\u200b\u200b. You initially have a permutation perm
of size n
\u200b\u200b where perm[i] == i
\u200b (0-indexed)\u200b\u200b\u200b\u200b.
In one operation, you will create a new array arr
, and for each i
:
i % 2 == 0
, then arr[i] = perm[i / 2]
.i % 2 == 1
, then arr[i] = perm[n / 2 + (i - 1) / 2]
.You will then assign arr
\u200b\u200b\u200b\u200b to perm
.
Return the minimum non-zero number of operations you need to perform on perm
to return the permutation to its initial value.
\n
Example 1:
\n\n\nInput: n = 2\nOutput: 1\nExplanation: perm = [0,1] initially.\nAfter the 1st operation, perm = [0,1]\nSo it takes only 1 operation.\n\n\n
Example 2:
\n\n\nInput: n = 4\nOutput: 2\nExplanation: perm = [0,1,2,3] initially.\nAfter the 1st operation, perm = [0,2,1,3]\nAfter the 2nd operation, perm = [0,1,2,3]\nSo it takes only 2 operations.\n\n\n
Example 3:
\n\n\nInput: n = 6\nOutput: 4\n\n\n
\n
Constraints:
\n\n2 <= n <= 1000
n
\u200b\u200b\u200b\u200b\u200b\u200b is even.\u7ed9\u4f60\u4e00\u4e2a\u5076\u6570 n
\u200b\u200b\u200b\u200b\u200b\u200b \uff0c\u5df2\u77e5\u5b58\u5728\u4e00\u4e2a\u957f\u5ea6\u4e3a n
\u7684\u6392\u5217 perm
\uff0c\u5176\u4e2d perm[i] == i
\u200b\uff08\u4e0b\u6807 \u4ece 0 \u5f00\u59cb \u8ba1\u6570\uff09\u3002
\u4e00\u6b65\u64cd\u4f5c\u4e2d\uff0c\u4f60\u5c06\u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4 arr
\uff0c\u5bf9\u4e8e\u6bcf\u4e2a i
\uff1a
i % 2 == 0
\uff0c\u90a3\u4e48 arr[i] = perm[i / 2]
i % 2 == 1
\uff0c\u90a3\u4e48 arr[i] = perm[n / 2 + (i - 1) / 2]
\u7136\u540e\u5c06 arr
\u200b\u200b \u8d4b\u503c\u200b\u200b\u7ed9 perm
\u3002
\u8981\u60f3\u4f7f perm
\u56de\u5230\u6392\u5217\u521d\u59cb\u503c\uff0c\u81f3\u5c11\u9700\u8981\u6267\u884c\u591a\u5c11\u6b65\u64cd\u4f5c\uff1f\u8fd4\u56de\u6700\u5c0f\u7684 \u975e\u96f6 \u64cd\u4f5c\u6b65\u6570\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1an = 2\n\u8f93\u51fa\uff1a1\n\u89e3\u91ca\uff1a\u6700\u521d\uff0cperm = [0,1]\n\u7b2c 1\u00a0\u6b65\u64cd\u4f5c\u540e\uff0cperm = [0,1]\n\u6240\u4ee5\uff0c\u4ec5\u9700\u6267\u884c 1 \u6b65\u64cd\u4f5c\n\n
\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1an = 4\n\u8f93\u51fa\uff1a2\n\u89e3\u91ca\uff1a\u6700\u521d\uff0cperm = [0,1,2,3]\n\u7b2c 1\u00a0\u6b65\u64cd\u4f5c\u540e\uff0cperm = [0,2,1,3]\n\u7b2c 2\u00a0\u6b65\u64cd\u4f5c\u540e\uff0cperm = [0,1,2,3]\n\u6240\u4ee5\uff0c\u4ec5\u9700\u6267\u884c 2 \u6b65\u64cd\u4f5c\n\n
\u793a\u4f8b 3\uff1a
\n\n\n\u8f93\u5165\uff1an = 6\n\u8f93\u51fa\uff1a4\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n2 <= n <= 1000
n
\u200b\u200b\u200b\u200b\u200b\u200b \u662f\u4e00\u4e2a\u5076\u6570You are given a string s
that contains some bracket pairs, with each pair containing a non-empty key.
"(name)is(age)yearsold"
, there are two bracket pairs that contain the keys "name"
and "age"
.You know the values of a wide range of keys. This is represented by a 2D string array knowledge
where each knowledge[i] = [keyi, valuei]
indicates that key keyi
has a value of valuei
.
You are tasked to evaluate all of the bracket pairs. When you evaluate a bracket pair that contains some key keyi
, you will:
keyi
and the bracket pair with the key's corresponding valuei
.keyi
and the bracket pair with a question mark "?"
(without the quotation marks).Each key will appear at most once in your knowledge
. There will not be any nested brackets in s
.
Return the resulting string after evaluating all of the bracket pairs.
\n\n\n
Example 1:
\n\n\nInput: s = "(name)is(age)yearsold", knowledge = [["name","bob"],["age","two"]]\nOutput: "bobistwoyearsold"\nExplanation:\nThe key "name" has a value of "bob", so replace "(name)" with "bob".\nThe key "age" has a value of "two", so replace "(age)" with "two".\n\n\n
Example 2:
\n\n\nInput: s = "hi(name)", knowledge = [["a","b"]]\nOutput: "hi?"\nExplanation: As you do not know the value of the key "name", replace "(name)" with "?".\n\n\n
Example 3:
\n\n\nInput: s = "(a)(a)(a)aaa", knowledge = [["a","yes"]]\nOutput: "yesyesyesaaa"\nExplanation: The same key can appear multiple times.\nThe key "a" has a value of "yes", so replace all occurrences of "(a)" with "yes".\nNotice that the "a"s not in a bracket pair are not evaluated.\n\n\n
Example 4:
\n\n\nInput: s = "(a)(b)", knowledge = [["a","b"],["b","a"]]\nOutput: "ba"\n\n
\n
Constraints:
\n\n1 <= s.length <= 105
0 <= knowledge.length <= 105
knowledge[i].length == 2
1 <= keyi.length, valuei.length <= 10
s
consists of lowercase English letters and round brackets '('
and ')'
.'('
in s
will have a corresponding close bracket ')'
.s
will be non-empty.s
.keyi
and valuei
consist of lowercase English letters.keyi
in knowledge
is unique.\u7ed9\u4f60\u4e00\u4e2a\u5b57\u7b26\u4e32\u00a0s
\u00a0\uff0c\u5b83\u5305\u542b\u4e00\u4e9b\u62ec\u53f7\u5bf9\uff0c\u6bcf\u4e2a\u62ec\u53f7\u4e2d\u5305\u542b\u4e00\u4e2a \u975e\u7a7a\u00a0\u7684\u952e\u3002
\"(name)is(age)yearsold\"
\u00a0\u4e2d\uff0c\u6709\u00a0\u4e24\u4e2a\u00a0\u62ec\u53f7\u5bf9\uff0c\u5206\u522b\u5305\u542b\u952e\u00a0\"name\"
\u548c\u00a0\"age\"
\u00a0\u3002\u4f60\u77e5\u9053\u8bb8\u591a\u952e\u5bf9\u5e94\u7684\u503c\uff0c\u8fd9\u4e9b\u5173\u7cfb\u7531\u4e8c\u7ef4\u5b57\u7b26\u4e32\u6570\u7ec4\u00a0knowledge
\u00a0\u8868\u793a\uff0c\u5176\u4e2d\u00a0knowledge[i] = [keyi, valuei]
\u00a0\uff0c\u8868\u793a\u952e\u00a0keyi
\u00a0\u5bf9\u5e94\u7684\u503c\u4e3a\u00a0valuei
\u00a0\u3002
\u4f60\u9700\u8981\u66ff\u6362 \u6240\u6709\u00a0\u7684\u62ec\u53f7\u5bf9\u3002\u5f53\u4f60\u66ff\u6362\u4e00\u4e2a\u62ec\u53f7\u5bf9\uff0c\u4e14\u5b83\u5305\u542b\u7684\u952e\u4e3a\u00a0keyi
\u00a0\u65f6\uff0c\u4f60\u9700\u8981\uff1a
keyi
\u00a0\u548c\u62ec\u53f7\u7528\u5bf9\u5e94\u7684\u503c\u00a0valuei
\u00a0\u66ff\u6362\u3002knowledge
\u00a0\u4e2d\u65e0\u6cd5\u5f97\u77e5\u67d0\u4e2a\u952e\u5bf9\u5e94\u7684\u503c\uff0c\u4f60\u9700\u8981\u5c06\u00a0keyi
\u00a0\u548c\u62ec\u53f7\u7528\u95ee\u53f7\u00a0\"?\"
\u00a0\u66ff\u6362\uff08\u4e0d\u9700\u8981\u5f15\u53f7\uff09\u3002knowledge
\u00a0\u4e2d\u6bcf\u4e2a\u952e\u6700\u591a\u53ea\u4f1a\u51fa\u73b0\u4e00\u6b21\u3002s
\u00a0\u4e2d\u4e0d\u4f1a\u6709\u5d4c\u5957\u7684\u62ec\u53f7\u3002
\u8bf7\u4f60\u8fd4\u56de\u66ff\u6362 \u6240\u6709\u00a0\u62ec\u53f7\u5bf9\u540e\u7684\u7ed3\u679c\u5b57\u7b26\u4e32\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1as = \"(name)is(age)yearsold\", knowledge = [[\"name\",\"bob\"],[\"age\",\"two\"]]\n\u8f93\u51fa\uff1a\"bobistwoyearsold\"\n\u89e3\u91ca\uff1a\n\u952e \"name\" \u5bf9\u5e94\u7684\u503c\u4e3a \"bob\" \uff0c\u6240\u4ee5\u5c06 \"(name)\" \u66ff\u6362\u4e3a \"bob\" \u3002\n\u952e \"age\" \u5bf9\u5e94\u7684\u503c\u4e3a \"two\" \uff0c\u6240\u4ee5\u5c06 \"(age)\" \u66ff\u6362\u4e3a \"two\" \u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1as = \"hi(name)\", knowledge = [[\"a\",\"b\"]]\n\u8f93\u51fa\uff1a\"hi?\"\n\u89e3\u91ca\uff1a\u7531\u4e8e\u4e0d\u77e5\u9053\u952e \"name\" \u5bf9\u5e94\u7684\u503c\uff0c\u6240\u4ee5\u7528 \"?\" \u66ff\u6362 \"(name)\" \u3002\n\n\n
\u793a\u4f8b 3\uff1a
\n\n\u8f93\u5165\uff1as = \"(a)(a)(a)aaa\", knowledge = [[\"a\",\"yes\"]]\n\u8f93\u51fa\uff1a\"yesyesyesaaa\"\n\u89e3\u91ca\uff1a\u76f8\u540c\u7684\u952e\u5728 s \u4e2d\u53ef\u80fd\u4f1a\u51fa\u73b0\u591a\u6b21\u3002\n\u952e \"a\" \u5bf9\u5e94\u7684\u503c\u4e3a \"yes\" \uff0c\u6240\u4ee5\u5c06\u6240\u6709\u7684 \"(a)\" \u66ff\u6362\u4e3a \"yes\" \u3002\n\u6ce8\u610f\uff0c\u4e0d\u5728\u62ec\u53f7\u91cc\u7684 \"a\" \u4e0d\u9700\u8981\u88ab\u66ff\u6362\u3002\n\n\n
\u793a\u4f8b 4\uff1a
\n\n\u8f93\u5165\uff1as = \"(a)(b)\", knowledge = [[\"a\",\"b\"],[\"b\",\"a\"]]\n\u8f93\u51fa\uff1a\"ba\"\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= s.length <= 105
0 <= knowledge.length <= 105
knowledge[i].length == 2
1 <= keyi.length, valuei.length <= 10
s
\u00a0\u53ea\u5305\u542b\u5c0f\u5199\u82f1\u6587\u5b57\u6bcd\u548c\u5706\u62ec\u53f7\u00a0'('
\u00a0\u548c\u00a0')'
\u00a0\u3002s
\u00a0\u4e2d\u6bcf\u4e00\u4e2a\u5de6\u5706\u62ec\u53f7\u00a0'('
\u00a0\u90fd\u6709\u5bf9\u5e94\u7684\u53f3\u5706\u62ec\u53f7\u00a0')'
\u00a0\u3002s
\u00a0\u4e2d\u6bcf\u5bf9\u62ec\u53f7\u5185\u7684\u952e\u90fd\u4e0d\u4f1a\u4e3a\u7a7a\u3002s
\u00a0\u4e2d\u4e0d\u4f1a\u6709\u5d4c\u5957\u62ec\u53f7\u5bf9\u3002keyi
\u00a0\u548c\u00a0valuei
\u00a0\u53ea\u5305\u542b\u5c0f\u5199\u82f1\u6587\u5b57\u6bcd\u3002knowledge
\u00a0\u4e2d\u7684\u00a0keyi
\u00a0\u4e0d\u4f1a\u91cd\u590d\u3002You are given a string word
that consists of digits and lowercase English letters.
You will replace every non-digit character with a space. For example, "a123bc34d8ef34"
will become " 123 34 8 34"
. Notice that you are left with some integers that are separated by at least one space: "123"
, "34"
, "8"
, and "34"
.
Return the number of different integers after performing the replacement operations on word
.
Two integers are considered different if their decimal representations without any leading zeros are different.
\n\n\n
Example 1:
\n\n\nInput: word = "a123bc34d8ef34"\nOutput: 3\nExplanation: The three different integers are "123", "34", and "8". Notice that "34" is only counted once.\n\n\n
Example 2:
\n\n\nInput: word = "leet1234code234"\nOutput: 2\n\n\n
Example 3:
\n\n\nInput: word = "a1b01c001"\nOutput: 1\nExplanation: The three integers "1", "01", and "001" all represent the same integer because\nthe leading zeros are ignored when comparing their decimal values.\n\n\n
\n
Constraints:
\n\n1 <= word.length <= 1000
word
consists of digits and lowercase English letters.\u7ed9\u4f60\u4e00\u4e2a\u5b57\u7b26\u4e32 word
\uff0c\u8be5\u5b57\u7b26\u4e32\u7531\u6570\u5b57\u548c\u5c0f\u5199\u82f1\u6587\u5b57\u6bcd\u7ec4\u6210\u3002
\u8bf7\u4f60\u7528\u7a7a\u683c\u66ff\u6362\u6bcf\u4e2a\u4e0d\u662f\u6570\u5b57\u7684\u5b57\u7b26\u3002\u4f8b\u5982\uff0c\"a123bc34d8ef34\"
\u5c06\u4f1a\u53d8\u6210 \" 123\u00a0 34 8\u00a0 34\"
\u3002\u6ce8\u610f\uff0c\u5269\u4e0b\u7684\u8fd9\u4e9b\u6574\u6570\u4e3a\uff08\u76f8\u90bb\u5f7c\u6b64\u81f3\u5c11\u6709\u4e00\u4e2a\u7a7a\u683c\u9694\u5f00\uff09\uff1a\"123\"
\u3001\"34\"
\u3001\"8\"
\u548c \"34\"
\u3002
\u8fd4\u56de\u5bf9 word
\u5b8c\u6210\u66ff\u6362\u540e\u5f62\u6210\u7684 \u4e0d\u540c \u6574\u6570\u7684\u6570\u76ee\u3002
\u53ea\u6709\u5f53\u4e24\u4e2a\u6574\u6570\u7684 \u4e0d\u542b\u524d\u5bfc\u96f6 \u7684\u5341\u8fdb\u5236\u8868\u793a\u4e0d\u540c\uff0c \u624d\u8ba4\u4e3a\u8fd9\u4e24\u4e2a\u6574\u6570\u4e5f\u4e0d\u540c\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1aword = \"a123bc34d8ef34\"\n\u8f93\u51fa\uff1a3\n\u89e3\u91ca\uff1a\u4e0d\u540c\u7684\u6574\u6570\u6709 \"123\"\u3001\"34\" \u548c \"8\" \u3002\u6ce8\u610f\uff0c\"34\" \u53ea\u8ba1\u6570\u4e00\u6b21\u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1aword = \"leet1234code234\"\n\u8f93\u51fa\uff1a2\n\n\n
\u793a\u4f8b 3\uff1a
\n\n\n\u8f93\u5165\uff1aword = \"a1b01c001\"\n\u8f93\u51fa\uff1a1\n\u89e3\u91ca\uff1a\"1\"\u3001\"01\" \u548c \"001\" \u89c6\u4e3a\u540c\u4e00\u4e2a\u6574\u6570\u7684\u5341\u8fdb\u5236\u8868\u793a\uff0c\u56e0\u4e3a\u5728\u6bd4\u8f83\u5341\u8fdb\u5236\u503c\u65f6\u4f1a\u5ffd\u7565\u524d\u5bfc\u96f6\u7684\u5b58\u5728\u3002\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= word.length <= 1000
word
\u7531\u6570\u5b57\u548c\u5c0f\u5199\u82f1\u6587\u5b57\u6bcd\u7ec4\u6210Table: Players
\r\n+----------------+---------+\r\n| Column Name | Type |\r\n+----------------+---------+\r\n| player_id | int |\r\n| player_name | varchar |\r\n+----------------+---------+\r\nplayer_id is the primary key for this table.\r\nEach row in this table contains the name and the ID of a tennis player.\r\n\r\n\r\n
\r\n\r\n
Table: Championships
\r\n+---------------+---------+\r\n| Column Name | Type |\r\n+---------------+---------+\r\n| year | int |\r\n| Wimbledon | int |\r\n| Fr_open | int |\r\n| US_open | int |\r\n| Au_open | int |\r\n+---------------+---------+\r\nyear is the primary key for this table.\r\nEach row of this table containts the IDs of the players who won one each tennis tournament of the grand slam.\r\n\r\n\r\n
\r\n\r\n
Write an SQL query to report the number of grand slam tournaments won by each player. Do not include the players who did not win any tournament.
\r\n\r\nReturn the result table in any order.
\r\n\r\nThe query result format is in the following example:
\r\n\r\n\r\n\r\n
\r\nPlayers table:\r\n+-----------+-------------+\r\n| player_id | player_name |\r\n+-----------+-------------+\r\n| 1 | Nadal |\r\n| 2 | Federer |\r\n| 3 | Novak |\r\n+-----------+-------------+\r\n\r\nChampionships table:\r\n+------+-----------+---------+---------+---------+\r\n| year | Wimbledon | Fr_open | US_open | Au_open |\r\n+------+-----------+---------+---------+---------+\r\n| 2018 | 1 | 1 | 1 | 1 |\r\n| 2019 | 1 | 1 | 2 | 2 |\r\n| 2020 | 2 | 1 | 2 | 2 |\r\n+------+-----------+---------+---------+---------+\r\n\r\nResult table:\r\n+-----------+-------------+-------------------+\r\n| player_id | player_name | grand_slams_count |\r\n+-----------+-------------+-------------------+\r\n| 2 | Federer | 5 |\r\n| 1 | Nadal | 7 |\r\n+-----------+-------------+-------------------+\r\n\r\nPlayer 1 (Nadal) won 7 titles: Wimbledon (2018, 2019), Fr_open (2018, 2019, 2020), US_open (2018), and Au_open (2018).\r\nPlayer 2 (Federer) won 5 titles: Wimbledon (2020), US_open (2019, 2020), and Au_open (2019, 2020).\r\nPlayer 3 (Novak) did not win anything, we did not include them in the result table.\r\n", "content_cn": "
\u8868\uff1aPlayers
\n+----------------+---------+\n| Column Name | Type |\n+----------------+---------+\n| player_id | int |\n| player_name | varchar |\n+----------------+---------+\nplayer_id \u662f\u8fd9\u4e2a\u8868\u7684\u4e3b\u952e\n\u8fd9\u4e2a\u8868\u7684\u6bcf\u4e00\u884c\u7ed9\u51fa\u4e00\u4e2a\u7f51\u7403\u8fd0\u52a8\u5458\u7684 ID \u548c \u59d3\u540d\n\n\n
\u8868\uff1aChampionships
\n+---------------+---------+\n| Column Name | Type |\n+---------------+---------+\n| year | int |\n| Wimbledon | int |\n| Fr_open | int |\n| US_open | int |\n| Au_open | int |\n+---------------+---------+\nyear \u662f\u8fd9\u4e2a\u8868\u7684\u4e3b\u952e\n\u8be5\u8868\u7684\u6bcf\u4e00\u884c\u90fd\u5305\u542b\u5728\u6bcf\u573a\u5927\u6ee1\u8d2f\u7f51\u7403\u6bd4\u8d5b\u4e2d\u8d62\u5f97\u6bd4\u8d5b\u7684\u7403\u5458\u7684 ID\n\n\n
\u00a0
\n\n\u8bf7\u5199\u51fa\u67e5\u8be2\u8bed\u53e5\uff0c\u67e5\u8be2\u51fa\u6bcf\u4e00\u4e2a\u7403\u5458\u8d62\u5f97\u5927\u6ee1\u8d2f\u6bd4\u8d5b\u7684\u6b21\u6570\u3002\u7ed3\u679c\u4e0d\u5305\u542b\u6ca1\u6709\u8d62\u5f97\u6bd4\u8d5b\u7684\u7403\u5458\u7684ID \u3002
\n\n\u7ed3\u679c\u96c6\u65e0\u987a\u5e8f\u8981\u6c42\u3002
\n\n\u67e5\u8be2\u7ed3\u679c\u7684\u683c\u5f0f\uff0c\u5982\u4e0b\u6240\u793a\uff1a
\n\n\u00a0
\n\n\nPlayers \u8868\uff1a\n+-----------+-------------+\n| player_id | player_name |\n+-----------+-------------+\n| 1 | Nadal |\n| 2 | Federer |\n| 3 | Novak |\n+-----------+-------------+\n\nChampionships \u8868\uff1a\n+------+-----------+---------+---------+---------+\n| year | Wimbledon | Fr_open | US_open | Au_open |\n+------+-----------+---------+---------+---------+\n| 2018 | 1 | 1 | 1 | 1 |\n| 2019 | 1 | 1 | 2 | 2 |\n| 2020 | 2 | 1 | 2 | 2 |\n+------+-----------+---------+---------+---------+\n\nResult \u8868\uff1a\n+-----------+-------------+-------------------+\n| player_id | player_name | grand_slams_count |\n+-----------+-------------+-------------------+\n| 2 | Federer | 5 |\n| 1 | Nadal | 7 |\n+-----------+-------------+-------------------+\n\nPlayer 1 (Nadal) \u83b7\u5f97\u4e86 7 \u6b21\u5927\u6ee1\u8d2f\uff1a\u5176\u4e2d\u6e29\u7f51 2 \u6b21(2018, 2019), \u6cd5\u56fd\u516c\u5f00\u8d5b 3 \u6b21 (2018, 2019, 2020), \u7f8e\u56fd\u516c\u5f00\u8d5b 1 \u6b21 (2018)\u4ee5\u53ca\u6fb3\u7f51\u516c\u5f00\u8d5b 1 \u6b21 (2018) \u3002\nPlayer 2 (Federer) \u83b7\u5f97\u4e86 5 \u6b21\u5927\u6ee1\u8d2f\uff1a\u5176\u4e2d\u6e29\u7f51 1 \u6b21 (2020), \u7f8e\u56fd\u516c\u5f00\u8d5b 2 \u6b21 (2019, 2020) \u4ee5\u53ca\u6fb3\u7f51\u516c\u5f00\u8d5b 2 \u6b21 (2019, 2020) \u3002\nPlayer 3 (Novak) \u6ca1\u6709\u8d62\u5f97\uff0c\u56e0\u6b64\u4e0d\u5305\u542b\u5728\u7ed3\u679c\u96c6\u4e2d\u3002\n\n", "tags_en": [], "tags_cn": [], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "MySQL", "langSlug": "mysql", "code": "# Write your MySQL query statement below", "__typename": "CodeSnippetNode"}, {"lang": "MS SQL Server", "langSlug": "mssql", "code": "/* Write your T-SQL query statement below */", "__typename": "CodeSnippetNode"}, {"lang": "Oracle", "langSlug": "oraclesql", "code": "/* Write your PL/SQL query statement below */", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1783](https://leetcode-cn.com/problems/grand-slam-titles)", "[\u5927\u6ee1\u8d2f\u6570\u91cf](/solution/1700-1799/1783.Grand%20Slam%20Titles/README.md)", "", "\u4e2d\u7b49", "\ud83d\udd12"], "md_table_row_en": ["[1783](https://leetcode.com/problems/grand-slam-titles)", "[Grand Slam Titles](/solution/1700-1799/1783.Grand%20Slam%20Titles/README_EN.md)", "", "Medium", "\ud83d\udd12"]}, {"question_id": "1931", "frontend_question_id": "1778", "paid_only": true, "paid_only_cn": true, "url_cn": "https://leetcode-cn.com/problems/shortest-path-in-a-hidden-grid", "url_en": "https://leetcode.com/problems/shortest-path-in-a-hidden-grid", "relative_path_cn": "/solution/1700-1799/1778.Shortest%20Path%20in%20a%20Hidden%20Grid/README.md", "relative_path_en": "/solution/1700-1799/1778.Shortest%20Path%20in%20a%20Hidden%20Grid/README_EN.md", "title_cn": "", "title_en": "Shortest Path in a Hidden Grid", "question_title_slug": "shortest-path-in-a-hidden-grid", "content_en": "
This is an interactive problem.
\n\nThere is a robot in a hidden grid, and you are trying to get it from its starting cell to the target cell in this grid. The grid is of size m x n
, and each cell in the grid is either empty or blocked. It is guaranteed that the starting cell and the target cell are different, and neither of them is blocked.
You want to find the minimum distance to the target cell. However, you do not know the grid's dimensions, the starting cell, nor the target cell. You are only allowed to ask queries to the GridMaster
object.
Thr GridMaster
class has the following functions:
boolean canMove(char direction)
Returns true
if the robot can move in that direction. Otherwise, it returns false
.void move(char direction)
Moves the robot in that direction. If this move would move the robot to a blocked cell or off the grid, the move will be ignored, and the robot will remain in the same position.boolean isTarget()
Returns true
if the robot is currently on the target cell. Otherwise, it returns false
.Note that direction
in the above functions should be a character from {'U','D','L','R'}
, representing the directions up, down, left, and right, respectively.
Return the minimum distance between the robot's initial starting cell and the target cell. If there is no valid path between the cells, return -1
.
Custom testing:
\n\nThe test input is read as a 2D matrix grid
of size m x n
where:
grid[i][j] == -1
indicates that the robot is in cell (i, j)
(the starting cell).grid[i][j] == 0
indicates that the cell (i, j)
is blocked.grid[i][j] == 1
indicates that the cell (i, j)
is empty.grid[i][j] == 2
indicates that the cell (i, j)
is the target cell.There is exactly one -1
and 2
in grid
. Remember that you will not have this information in your code.
\n
Example 1:
\n\n\nInput: grid = [[1,2],[-1,0]]\nOutput: 2\nExplanation: One possible interaction is described below:\nThe robot is initially standing on cell (1, 0), denoted by the -1.\n- master.canMove('U') returns true.\n- master.canMove('D') returns false.\n- master.canMove('L') returns false.\n- master.canMove('R') returns false.\n- master.move('U') moves the robot to the cell (0, 0).\n- master.isTarget() returns false.\n- master.canMove('U') returns false.\n- master.canMove('D') returns true.\n- master.canMove('L') returns false.\n- master.canMove('R') returns true.\n- master.move('R') moves the robot to the cell (0, 1).\n- master.isTarget() returns true. \nWe now know that the target is the cell (0, 1), and the shortest path to the target cell is 2.\n\n\n
Example 2:
\n\n\nInput: grid = [[0,0,-1],[1,1,1],[2,0,0]]\nOutput: 4\nExplanation: The minimum distance between the robot and the target cell is 4.\n\n
Example 3:
\n\n\nInput: grid = [[-1,0],[0,2]]\nOutput: -1\nExplanation: There is no path from the robot to the target cell.\n\n
\n
Constraints:
\n\n1 <= n, m <= 500
m == grid.length
n == grid[i].length
grid[i][j]
is either -1
, 0
, 1
, or 2
.-1
in grid
.2
in grid
.You are given an integer array coins
of length n
which represents the n
coins that you own. The value of the ith
coin is coins[i]
. You can make some value x
if you can choose some of your n
coins such that their values sum up to x
.
Return the maximum number of consecutive integer values that you can make with your coins starting from and including 0
.
Note that you may have multiple coins of the same value.
\n\n\n
Example 1:
\n\n\nInput: coins = [1,3]\nOutput: 2\nExplanation: You can make the following values:\n- 0: take []\n- 1: take [1]\nYou can make 2 consecutive integer values starting from 0.\n\n
Example 2:
\n\n\nInput: coins = [1,1,1,4]\nOutput: 8\nExplanation: You can make the following values:\n- 0: take []\n- 1: take [1]\n- 2: take [1,1]\n- 3: take [1,1,1]\n- 4: take [4]\n- 5: take [4,1]\n- 6: take [4,1,1]\n- 7: take [4,1,1,1]\nYou can make 8 consecutive integer values starting from 0.\n\n
Example 3:
\n\n\nInput: nums = [1,4,10,3,1]\nOutput: 20\n\n
\n
Constraints:
\n\ncoins.length == n
1 <= n <= 4 * 104
1 <= coins[i] <= 4 * 104
\u7ed9\u4f60\u4e00\u4e2a\u957f\u5ea6\u4e3a n
\u00a0\u7684\u6574\u6570\u6570\u7ec4\u00a0coins
\u00a0\uff0c\u5b83\u4ee3\u8868\u4f60\u62e5\u6709\u7684\u00a0n
\u00a0\u4e2a\u786c\u5e01\u3002\u7b2c\u00a0i
\u00a0\u4e2a\u786c\u5e01\u7684\u503c\u4e3a\u00a0coins[i]
\u00a0\u3002\u5982\u679c\u4f60\u4ece\u8fd9\u4e9b\u786c\u5e01\u4e2d\u9009\u51fa\u4e00\u90e8\u5206\u786c\u5e01\uff0c\u5b83\u4eec\u7684\u548c\u4e3a\u00a0x
\u00a0\uff0c\u90a3\u4e48\u79f0\uff0c\u4f60\u53ef\u4ee5\u00a0\u6784\u9020\u00a0\u51fa\u00a0x
\u00a0\u3002
\u8bf7\u8fd4\u56de\u4ece 0
\u00a0\u5f00\u59cb\uff08\u5305\u62ec\u00a00
\u00a0\uff09\uff0c\u4f60\u6700\u591a\u80fd\u00a0\u6784\u9020\u00a0\u51fa\u591a\u5c11\u4e2a\u8fde\u7eed\u6574\u6570\u3002
\u4f60\u53ef\u80fd\u6709\u591a\u4e2a\u76f8\u540c\u503c\u7684\u786c\u5e01\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1acoins = [1,3]\n\u8f93\u51fa\uff1a2\n\u89e3\u91ca\uff1a\u4f60\u53ef\u4ee5\u5f97\u5230\u4ee5\u4e0b\u8fd9\u4e9b\u503c\uff1a\n- 0\uff1a\u4ec0\u4e48\u90fd\u4e0d\u53d6 []\n- 1\uff1a\u53d6 [1]\n\u4ece 0 \u5f00\u59cb\uff0c\u4f60\u53ef\u4ee5\u6784\u9020\u51fa 2 \u4e2a\u8fde\u7eed\u6574\u6570\u3002\n\n
\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1acoins = [1,1,1,4]\n\u8f93\u51fa\uff1a8\n\u89e3\u91ca\uff1a\u4f60\u53ef\u4ee5\u5f97\u5230\u4ee5\u4e0b\u8fd9\u4e9b\u503c\uff1a\n- 0\uff1a\u4ec0\u4e48\u90fd\u4e0d\u53d6 []\n- 1\uff1a\u53d6 [1]\n- 2\uff1a\u53d6 [1,1]\n- 3\uff1a\u53d6 [1,1,1]\n- 4\uff1a\u53d6 [4]\n- 5\uff1a\u53d6 [4,1]\n- 6\uff1a\u53d6 [4,1,1]\n- 7\uff1a\u53d6 [4,1,1,1]\n\u4ece 0 \u5f00\u59cb\uff0c\u4f60\u53ef\u4ee5\u6784\u9020\u51fa 8 \u4e2a\u8fde\u7eed\u6574\u6570\u3002\n\n
\u793a\u4f8b 3\uff1a
\n\n\n\u8f93\u5165\uff1anums = [1,4,10,3,1]\n\u8f93\u51fa\uff1a20\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\ncoins.length == n
1 <= n <= 4 * 104
1 <= coins[i] <= 4 * 104
You are given three positive integers: n
, index
, and maxSum
. You want to construct an array nums
(0-indexed) that satisfies the following conditions:
nums.length == n
nums[i]
is a positive integer where 0 <= i < n
.abs(nums[i] - nums[i+1]) <= 1
where 0 <= i < n-1
.nums
does not exceed maxSum
.nums[index]
is maximized.Return nums[index]
of the constructed array.
Note that abs(x)
equals x
if x >= 0
, and -x
otherwise.
\n
Example 1:
\n\n\nInput: n = 4, index = 2, maxSum = 6\nOutput: 2\nExplanation: nums = [1,2,2,1] is one array that satisfies all the conditions.\nThere are no arrays that satisfy all the conditions and have nums[2] == 3, so 2 is the maximum nums[2].\n\n\n
Example 2:
\n\n\nInput: n = 6, index = 1, maxSum = 10\nOutput: 3\n\n\n
\n
Constraints:
\n\n1 <= n <= maxSum <= 109
0 <= index < n
\u7ed9\u4f60\u4e09\u4e2a\u6b63\u6574\u6570 n
\u3001index
\u548c maxSum
\u3002\u4f60\u9700\u8981\u6784\u9020\u4e00\u4e2a\u540c\u65f6\u6ee1\u8db3\u4e0b\u8ff0\u6240\u6709\u6761\u4ef6\u7684\u6570\u7ec4 nums
\uff08\u4e0b\u6807 \u4ece 0 \u5f00\u59cb \u8ba1\u6570\uff09\uff1a
nums.length == n
nums[i]
\u662f \u6b63\u6574\u6570 \uff0c\u5176\u4e2d 0 <= i < n
abs(nums[i] - nums[i+1]) <= 1
\uff0c\u5176\u4e2d 0 <= i < n-1
nums
\u4e2d\u6240\u6709\u5143\u7d20\u4e4b\u548c\u4e0d\u8d85\u8fc7 maxSum
nums[index]
\u7684\u503c\u88ab \u6700\u5927\u5316\u8fd4\u56de\u4f60\u6240\u6784\u9020\u7684\u6570\u7ec4\u4e2d\u7684 nums[index]
\u3002
\u6ce8\u610f\uff1aabs(x)
\u7b49\u4e8e x
\u7684\u524d\u63d0\u662f x >= 0
\uff1b\u5426\u5219\uff0cabs(x)
\u7b49\u4e8e -x
\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1an = 4, index = 2, maxSum = 6\n\u8f93\u51fa\uff1a2\n\u89e3\u91ca\uff1a\u6570\u7ec4 [1,1,2,1] \u548c [1,2,2,1] \u6ee1\u8db3\u6240\u6709\u6761\u4ef6\u3002\u4e0d\u5b58\u5728\u5176\u4ed6\u5728\u6307\u5b9a\u4e0b\u6807\u5904\u5177\u6709\u66f4\u5927\u503c\u7684\u6709\u6548\u6570\u7ec4\u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1an = 6, index = 1, maxSum = 10\n\u8f93\u51fa\uff1a3\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= n <= maxSum <= 109
0 <= index < n
You are given a 2D integer array orders
, where each orders[i] = [pricei, amounti, orderTypei]
denotes that amounti
orders have been placed of type orderTypei
at the price pricei
. The orderTypei
is:
0
if it is a batch of buy
orders, or1
if it is a batch of sell
orders.Note that orders[i]
represents a batch of amounti
independent orders with the same price and order type. All orders represented by orders[i]
will be placed before all orders represented by orders[i+1]
for all valid i
.
There is a backlog that consists of orders that have not been executed. The backlog is initially empty. When an order is placed, the following happens:
\r\n\r\nbuy
order, you look at the sell
order with the smallest price in the backlog. If that sell
order's price is smaller than or equal to the current buy
order's price, they will match and be executed, and that sell
order will be removed from the backlog. Else, the buy
order is added to the backlog.sell
order, you look at the buy
order with the largest price in the backlog. If that buy
order's price is larger than or equal to the current sell
order's price, they will match and be executed, and that buy
order will be removed from the backlog. Else, the sell
order is added to the backlog.Return the total amount of orders in the backlog after placing all the orders from the input. Since this number can be large, return it modulo 109 + 7
.
\r\n
Example 1:
\r\n\r\nInput: orders = [[10,5,0],[15,2,1],[25,1,1],[30,4,0]]\r\nOutput: 6\r\nExplanation: Here is what happens with the orders:\r\n- 5 orders of type buy with price 10 are placed. There are no sell orders, so the 5 orders are added to the backlog.\r\n- 2 orders of type sell with price 15 are placed. There are no buy orders with prices larger than or equal to 15, so the 2 orders are added to the backlog.\r\n- 1 order of type sell with price 25 is placed. There are no buy orders with prices larger than or equal to 25 in the backlog, so this order is added to the backlog.\r\n- 4 orders of type buy with price 30 are placed. The first 2 orders are matched with the 2 sell orders of the least price, which is 15 and these 2 sell orders are removed from the backlog. The 3rd order is matched with the sell order of the least price, which is 25 and this sell order is removed from the backlog. Then, there are no more sell orders in the backlog, so the 4th order is added to the backlog.\r\nFinally, the backlog has 5 buy orders with price 10, and 1 buy order with price 30. So the total number of orders in the backlog is 6.\r\n\r\n\r\n
Example 2:
\r\n\r\nInput: orders = [[7,1000000000,1],[15,3,0],[5,999999995,0],[5,1,1]]\r\nOutput: 999999984\r\nExplanation: Here is what happens with the orders:\r\n- 109 orders of type sell with price 7 are placed. There are no buy orders, so the 109 orders are added to the backlog.\r\n- 3 orders of type buy with price 15 are placed. They are matched with the 3 sell orders with the least price which is 7, and those 3 sell orders are removed from the backlog.\r\n- 999999995 orders of type buy with price 5 are placed. The least price of a sell order is 7, so the 999999995 orders are added to the backlog.\r\n- 1 order of type sell with price 5 is placed. It is matched with the buy order of the highest price, which is 5, and that buy order is removed from the backlog.\r\nFinally, the backlog has (1000000000-3) sell orders with price 7, and (999999995-1) buy orders with price 5. So the total number of orders = 1999999991, which is equal to 999999984 % (109 + 7).\r\n\r\n\r\n
\r\n
Constraints:
\r\n\r\n1 <= orders.length <= 105
orders[i].length == 3
1 <= pricei, amounti <= 109
orderTypei
is either 0
or 1
.\u7ed9\u4f60\u4e00\u4e2a\u4e8c\u7ef4\u6574\u6570\u6570\u7ec4 orders
\uff0c\u5176\u4e2d\u6bcf\u4e2a orders[i] = [pricei, amounti, orderTypei]
\u8868\u793a\u6709 amounti
\u7b14\u7c7b\u578b\u4e3a\u00a0orderTypei
\u3001\u4ef7\u683c\u4e3a\u00a0pricei
\u7684\u8ba2\u5355\u3002
\u8ba2\u5355\u7c7b\u578b orderTypei
\u53ef\u4ee5\u5206\u4e3a\u4e24\u79cd\uff1a
0
\u8868\u793a\u8fd9\u662f\u4e00\u6279\u91c7\u8d2d\u8ba2\u5355 buy
1
\u8868\u793a\u8fd9\u662f\u4e00\u6279\u9500\u552e\u8ba2\u5355 sell
\u6ce8\u610f\uff0corders[i]
\u8868\u793a\u4e00\u6279\u5171\u8ba1 amounti
\u7b14\u7684\u72ec\u7acb\u8ba2\u5355\uff0c\u8fd9\u4e9b\u8ba2\u5355\u7684\u4ef7\u683c\u548c\u7c7b\u578b\u76f8\u540c\u3002\u5bf9\u4e8e\u6240\u6709\u6709\u6548\u7684 i
\uff0c\u7531 orders[i]
\u8868\u793a\u7684\u6240\u6709\u8ba2\u5355\u63d0\u4ea4\u65f6\u95f4\u5747\u65e9\u4e8e orders[i+1]
\u8868\u793a\u7684\u6240\u6709\u8ba2\u5355\u3002
\u5b58\u5728\u7531\u672a\u6267\u884c\u8ba2\u5355\u7ec4\u6210\u7684 \u79ef\u538b\u8ba2\u5355 \u3002\u79ef\u538b\u8ba2\u5355\u6700\u521d\u662f\u7a7a\u7684\u3002\u63d0\u4ea4\u8ba2\u5355\u65f6\uff0c\u4f1a\u53d1\u751f\u4ee5\u4e0b\u60c5\u51b5\uff1a
\n\nbuy
\uff0c\u5219\u53ef\u4ee5\u67e5\u770b\u79ef\u538b\u8ba2\u5355\u4e2d\u4ef7\u683c \u6700\u4f4e \u7684\u9500\u552e\u8ba2\u5355 sell
\u3002\u5982\u679c\u8be5\u9500\u552e\u8ba2\u5355 sell
\u7684\u4ef7\u683c \u4f4e\u4e8e\u6216\u7b49\u4e8e \u5f53\u524d\u91c7\u8d2d\u8ba2\u5355 buy
\u7684\u4ef7\u683c\uff0c\u5219\u5339\u914d\u5e76\u6267\u884c\u8fd9\u4e24\u7b14\u8ba2\u5355\uff0c\u5e76\u5c06\u9500\u552e\u8ba2\u5355 sell
\u4ece\u79ef\u538b\u8ba2\u5355\u4e2d\u5220\u9664\u3002\u5426\u5219\uff0c\u91c7\u8d2d\u8ba2\u5355 buy
\u5c06\u4f1a\u6dfb\u52a0\u5230\u79ef\u538b\u8ba2\u5355\u4e2d\u3002sell
\uff0c\u5219\u53ef\u4ee5\u67e5\u770b\u79ef\u538b\u8ba2\u5355\u4e2d\u4ef7\u683c \u6700\u9ad8 \u7684\u91c7\u8d2d\u8ba2\u5355 buy
\u3002\u5982\u679c\u8be5\u91c7\u8d2d\u8ba2\u5355 buy
\u7684\u4ef7\u683c \u9ad8\u4e8e\u6216\u7b49\u4e8e \u5f53\u524d\u9500\u552e\u8ba2\u5355 sell
\u7684\u4ef7\u683c\uff0c\u5219\u5339\u914d\u5e76\u6267\u884c\u8fd9\u4e24\u7b14\u8ba2\u5355\uff0c\u5e76\u5c06\u91c7\u8d2d\u8ba2\u5355 buy
\u4ece\u79ef\u538b\u8ba2\u5355\u4e2d\u5220\u9664\u3002\u5426\u5219\uff0c\u9500\u552e\u8ba2\u5355 sell
\u5c06\u4f1a\u6dfb\u52a0\u5230\u79ef\u538b\u8ba2\u5355\u4e2d\u3002\u8f93\u5165\u6240\u6709\u8ba2\u5355\u540e\uff0c\u8fd4\u56de\u79ef\u538b\u8ba2\u5355\u4e2d\u7684 \u8ba2\u5355\u603b\u6570 \u3002\u7531\u4e8e\u6570\u5b57\u53ef\u80fd\u5f88\u5927\uff0c\u6240\u4ee5\u9700\u8981\u8fd4\u56de\u5bf9 109 + 7
\u53d6\u4f59\u7684\u7ed3\u679c\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1aorders = [[10,5,0],[15,2,1],[25,1,1],[30,4,0]]\n\u8f93\u51fa\uff1a6\n\u89e3\u91ca\uff1a\u8f93\u5165\u8ba2\u5355\u540e\u4f1a\u53d1\u751f\u4e0b\u8ff0\u60c5\u51b5\uff1a\n- \u63d0\u4ea4 5 \u7b14\u91c7\u8d2d\u8ba2\u5355\uff0c\u4ef7\u683c\u4e3a 10 \u3002\u6ca1\u6709\u9500\u552e\u8ba2\u5355\uff0c\u6240\u4ee5\u8fd9 5 \u7b14\u8ba2\u5355\u6dfb\u52a0\u5230\u79ef\u538b\u8ba2\u5355\u4e2d\u3002\n- \u63d0\u4ea4 2 \u7b14\u9500\u552e\u8ba2\u5355\uff0c\u4ef7\u683c\u4e3a 15 \u3002\u6ca1\u6709\u91c7\u8d2d\u8ba2\u5355\u7684\u4ef7\u683c\u5927\u4e8e\u6216\u7b49\u4e8e 15 \uff0c\u6240\u4ee5\u8fd9 2 \u7b14\u8ba2\u5355\u6dfb\u52a0\u5230\u79ef\u538b\u8ba2\u5355\u4e2d\u3002\n- \u63d0\u4ea4 1 \u7b14\u9500\u552e\u8ba2\u5355\uff0c\u4ef7\u683c\u4e3a 25 \u3002\u6ca1\u6709\u91c7\u8d2d\u8ba2\u5355\u7684\u4ef7\u683c\u5927\u4e8e\u6216\u7b49\u4e8e 25 \uff0c\u6240\u4ee5\u8fd9 1 \u7b14\u8ba2\u5355\u6dfb\u52a0\u5230\u79ef\u538b\u8ba2\u5355\u4e2d\u3002\n- \u63d0\u4ea4 4 \u7b14\u91c7\u8d2d\u8ba2\u5355\uff0c\u4ef7\u683c\u4e3a 30 \u3002\u524d 2 \u7b14\u91c7\u8d2d\u8ba2\u5355\u4e0e\u4ef7\u683c\u6700\u4f4e\uff08\u4ef7\u683c\u4e3a 15\uff09\u7684 2 \u7b14\u9500\u552e\u8ba2\u5355\u5339\u914d\uff0c\u4ece\u79ef\u538b\u8ba2\u5355\u4e2d\u5220\u9664\u8fd9 2 \u7b14\u9500\u552e\u8ba2\u5355\u3002\u7b2c 3 \u7b14\u91c7\u8d2d\u8ba2\u5355\u4e0e\u4ef7\u683c\u6700\u4f4e\u7684 1 \u7b14\u9500\u552e\u8ba2\u5355\u5339\u914d\uff0c\u9500\u552e\u8ba2\u5355\u4ef7\u683c\u4e3a 25 \uff0c\u4ece\u79ef\u538b\u8ba2\u5355\u4e2d\u5220\u9664\u8fd9 1 \u7b14\u9500\u552e\u8ba2\u5355\u3002\u79ef\u538b\u8ba2\u5355\u4e2d\u4e0d\u5b58\u5728\u66f4\u591a\u9500\u552e\u8ba2\u5355\uff0c\u6240\u4ee5\u7b2c 4 \u7b14\u91c7\u8d2d\u8ba2\u5355\u9700\u8981\u6dfb\u52a0\u5230\u79ef\u538b\u8ba2\u5355\u4e2d\u3002\n\u6700\u7ec8\uff0c\u79ef\u538b\u8ba2\u5355\u4e2d\u6709 5 \u7b14\u4ef7\u683c\u4e3a 10 \u7684\u91c7\u8d2d\u8ba2\u5355\uff0c\u548c 1 \u7b14\u4ef7\u683c\u4e3a 30 \u7684\u91c7\u8d2d\u8ba2\u5355\u3002\u6240\u4ee5\u79ef\u538b\u8ba2\u5355\u4e2d\u7684\u8ba2\u5355\u603b\u6570\u4e3a 6 \u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1aorders = [[7,1000000000,1],[15,3,0],[5,999999995,0],[5,1,1]]\n\u8f93\u51fa\uff1a999999984\n\u89e3\u91ca\uff1a\u8f93\u5165\u8ba2\u5355\u540e\u4f1a\u53d1\u751f\u4e0b\u8ff0\u60c5\u51b5\uff1a\n- \u63d0\u4ea4 109 \u7b14\u9500\u552e\u8ba2\u5355\uff0c\u4ef7\u683c\u4e3a 7 \u3002\u6ca1\u6709\u91c7\u8d2d\u8ba2\u5355\uff0c\u6240\u4ee5\u8fd9 109 \u7b14\u8ba2\u5355\u6dfb\u52a0\u5230\u79ef\u538b\u8ba2\u5355\u4e2d\u3002\n- \u63d0\u4ea4 3 \u7b14\u91c7\u8d2d\u8ba2\u5355\uff0c\u4ef7\u683c\u4e3a 15 \u3002\u8fd9\u4e9b\u91c7\u8d2d\u8ba2\u5355\u4e0e\u4ef7\u683c\u6700\u4f4e\uff08\u4ef7\u683c\u4e3a 7 \uff09\u7684 3 \u7b14\u9500\u552e\u8ba2\u5355\u5339\u914d\uff0c\u4ece\u79ef\u538b\u8ba2\u5355\u4e2d\u5220\u9664\u8fd9 3 \u7b14\u9500\u552e\u8ba2\u5355\u3002\n- \u63d0\u4ea4 999999995 \u7b14\u91c7\u8d2d\u8ba2\u5355\uff0c\u4ef7\u683c\u4e3a 5 \u3002\u9500\u552e\u8ba2\u5355\u7684\u6700\u4f4e\u4ef7\u4e3a 7 \uff0c\u6240\u4ee5\u8fd9 999999995 \u7b14\u8ba2\u5355\u6dfb\u52a0\u5230\u79ef\u538b\u8ba2\u5355\u4e2d\u3002\n- \u63d0\u4ea4 1 \u7b14\u9500\u552e\u8ba2\u5355\uff0c\u4ef7\u683c\u4e3a 5 \u3002\u8fd9\u7b14\u9500\u552e\u8ba2\u5355\u4e0e\u4ef7\u683c\u6700\u9ad8\uff08\u4ef7\u683c\u4e3a 5 \uff09\u7684 1 \u7b14\u91c7\u8d2d\u8ba2\u5355\u5339\u914d\uff0c\u4ece\u79ef\u538b\u8ba2\u5355\u4e2d\u5220\u9664\u8fd9 1 \u7b14\u91c7\u8d2d\u8ba2\u5355\u3002\n\u6700\u7ec8\uff0c\u79ef\u538b\u8ba2\u5355\u4e2d\u6709 (1000000000-3) \u7b14\u4ef7\u683c\u4e3a 7 \u7684\u9500\u552e\u8ba2\u5355\uff0c\u548c (999999995-1) \u7b14\u4ef7\u683c\u4e3a 5 \u7684\u91c7\u8d2d\u8ba2\u5355\u3002\u6240\u4ee5\u79ef\u538b\u8ba2\u5355\u4e2d\u7684\u8ba2\u5355\u603b\u6570\u4e3a 1999999991 \uff0c\u7b49\u4e8e 999999984 % (109 + 7) \u3002\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= orders.length <= 105
orders[i].length == 3
1 <= pricei, amounti <= 109
orderTypei
\u4e3a 0
\u6216 1
Given an array of positive integers nums
, return the maximum possible sum of an ascending subarray in nums
.
A subarray is defined as a contiguous sequence of numbers in an array.
\n\nA subarray [numsl, numsl+1, ..., numsr-1, numsr]
is ascending if for all i
where l <= i < r
, numsi < numsi+1
. Note that a subarray of size 1
is ascending.
\n
Example 1:
\n\n\nInput: nums = [10,20,30,5,10,50]\nOutput: 65\nExplanation: [5,10,50] is the ascending subarray with the maximum sum of 65.\n\n\n
Example 2:
\n\n\nInput: nums = [10,20,30,40,50]\nOutput: 150\nExplanation: [10,20,30,40,50] is the ascending subarray with the maximum sum of 150.\n\n\n
Example 3:
\n\n\nInput: nums = [12,17,15,13,10,11,12]\nOutput: 33\nExplanation: [10,11,12] is the ascending subarray with the maximum sum of 33.\n\n\n
Example 4:
\n\n\nInput: nums = [100,10,1]\nOutput: 100\n\n\n
\n
Constraints:
\n\n1 <= nums.length <= 100
1 <= nums[i] <= 100
\u7ed9\u4f60\u4e00\u4e2a\u6b63\u6574\u6570\u7ec4\u6210\u7684\u6570\u7ec4 nums
\uff0c\u8fd4\u56de nums
\u4e2d\u4e00\u4e2a \u5347\u5e8f \u5b50\u6570\u7ec4\u7684\u6700\u5927\u53ef\u80fd\u5143\u7d20\u548c\u3002
\u5b50\u6570\u7ec4\u662f\u6570\u7ec4\u4e2d\u7684\u4e00\u4e2a\u8fde\u7eed\u6570\u5b57\u5e8f\u5217\u3002
\n\n\u5df2\u77e5\u5b50\u6570\u7ec4 [numsl, numsl+1, ..., numsr-1, numsr]
\uff0c\u82e5\u5bf9\u6240\u6709 i
\uff08l <= i < r
\uff09\uff0cnumsi < numsi+1
\u90fd\u6210\u7acb\uff0c\u5219\u79f0\u8fd9\u4e00\u5b50\u6570\u7ec4\u4e3a \u5347\u5e8f \u5b50\u6570\u7ec4\u3002\u6ce8\u610f\uff0c\u5927\u5c0f\u4e3a 1
\u7684\u5b50\u6570\u7ec4\u4e5f\u89c6\u4f5c \u5347\u5e8f \u5b50\u6570\u7ec4\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1anums = [10,20,30,5,10,50]\n\u8f93\u51fa\uff1a65\n\u89e3\u91ca\uff1a[5,10,50] \u662f\u5143\u7d20\u548c\u6700\u5927\u7684\u5347\u5e8f\u5b50\u6570\u7ec4\uff0c\u6700\u5927\u5143\u7d20\u548c\u4e3a 65 \u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1anums = [10,20,30,40,50]\n\u8f93\u51fa\uff1a150\n\u89e3\u91ca\uff1a[10,20,30,40,50] \u662f\u5143\u7d20\u548c\u6700\u5927\u7684\u5347\u5e8f\u5b50\u6570\u7ec4\uff0c\u6700\u5927\u5143\u7d20\u548c\u4e3a 150 \u3002 \n\n\n
\u793a\u4f8b 3\uff1a
\n\n\n\u8f93\u5165\uff1anums = [12,17,15,13,10,11,12]\n\u8f93\u51fa\uff1a33\n\u89e3\u91ca\uff1a[10,11,12] \u662f\u5143\u7d20\u548c\u6700\u5927\u7684\u5347\u5e8f\u5b50\u6570\u7ec4\uff0c\u6700\u5927\u5143\u7d20\u548c\u4e3a 33 \u3002 \n\n\n
\u793a\u4f8b 4\uff1a
\n\n\n\u8f93\u5165\uff1anums = [100,10,1]\n\u8f93\u51fa\uff1a100\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= nums.length <= 100
1 <= nums[i] <= 100
Table: Products
\r\n+-------------+---------+\r\n| Column Name | Type |\r\n+-------------+---------+\r\n| product_id | int |\r\n| store | enum |\r\n| price | int |\r\n+-------------+---------+\r\n(product_id,store) is the primary key for this table.\r\nstore is an ENUM of type ('store1', 'store2', 'store3') where each represents the store this product is available at.\r\nprice is the price of the product at this store.\r\n\r\n
\r\n\r\n
Write an SQL query to find the price of each product in each store.
\r\n\r\nReturn the result table in any order.
\r\n\r\nThe query result format is in the following example:
\r\n\r\n\r\n\r\n
\r\nProducts table:\r\n+-------------+--------+-------+\r\n| product_id | store | price |\r\n+-------------+--------+-------+\r\n| 0 | store1 | 95 |\r\n| 0 | store3 | 105 |\r\n| 0 | store2 | 100 |\r\n| 1 | store1 | 70 |\r\n| 1 | store3 | 80 |\r\n+-------------+--------+-------+\r\nResult table:\r\n+-------------+--------+--------+--------+\r\n| product_id | store1 | store2 | store3 |\r\n+-------------+--------+--------+--------+\r\n| 0 | 95 | 100 | 105 |\r\n| 1 | 70 | null | 80 |\r\n+-------------+--------+--------+--------+\r\nProduct 0 price's are 95 for store1, 100 for store2 and, 105 for store3.\r\nProduct 1 price's are 70 for store1, 80 for store3 and, it's not sold in store2.\r\n", "content_cn": "
\u8868\uff1aProducts
\n+-------------+---------+\n| Column Name | Type |\n+-------------+---------+\n| product_id | int |\n| store | enum |\n| price | int |\n+-------------+---------+\n(product_id,store) \u662f\u8fd9\u4e2a\u8868\u7684\u4e3b\u952e\u3002\nstore \u5b57\u6bb5\u662f\u679a\u4e3e\u7c7b\u578b\uff0c\u5b83\u7684\u53d6\u503c\u4e3a\u4ee5\u4e0b\u4e09\u79cd ('store1', 'store2', 'store3') \u3002\nprice \u662f\u8be5\u5546\u54c1\u5728\u8fd9\u5bb6\u5546\u5e97\u4e2d\u7684\u4ef7\u683c\u3002\n\n
\u00a0
\n\n\u5199\u51fa\u4e00\u4e2a SQL \u67e5\u8be2\u8bed\u53e5\uff0c\u67e5\u627e\u6bcf\u79cd\u4ea7\u54c1\u5728\u5404\u4e2a\u5546\u5e97\u4e2d\u7684\u4ef7\u683c\u3002
\n\n\u53ef\u4ee5\u4ee5 \u4efb\u4f55\u987a\u5e8f \u8f93\u51fa\u7ed3\u679c\u3002
\n\n\u67e5\u8be2\u7ed3\u679c\u683c\u5f0f\u5982\u4e0b\u4f8b\u6240\u793a\uff1a
\n\n\nProducts \u8868\uff1a\n+-------------+--------+-------+\n| product_id | store | price |\n+-------------+--------+-------+\n| 0 | store1 | 95 |\n| 0 | store3 | 105 |\n| 0 | store2 | 100 |\n| 1 | store1 | 70 |\n| 1 | store3 | 80 |\n+-------------+--------+-------+\nResult \u8868\uff1a\n+-------------+--------+--------+--------+\n| product_id | store1 | store2 | store3 |\n+-------------+--------+--------+--------+\n| 0 | 95 | 100 | 105 |\n| 1 | 70 | null | 80 |\n+-------------+--------+--------+--------+\n\u4ea7\u54c1 0 \u7684\u4ef7\u683c\u5728\u5546\u5e97 1 \u4e3a 95 \uff0c\u5546\u5e97 2 \u4e3a 100 \uff0c\u5546\u5e97 3 \u4e3a 105 \u3002\n\u4ea7\u54c1 1 \u7684\u4ef7\u683c\u5728\u5546\u5e97 1 \u4e3a 70 \uff0c\u5546\u5e97 3 \u7684\u4ea7\u54c1 1 \u4ef7\u683c\u4e3a 80 \uff0c\u4f46\u5728\u5546\u5e97 2 \u4e2d\u6ca1\u6709\u9500\u552e\u3002\n\n", "tags_en": [], "tags_cn": [], "difficulty_en": "Easy", "difficulty_cn": "\u7b80\u5355", "code_snippets": [{"lang": "MySQL", "langSlug": "mysql", "code": "# Write your MySQL query statement below", "__typename": "CodeSnippetNode"}, {"lang": "MS SQL Server", "langSlug": "mssql", "code": "/* Write your T-SQL query statement below */", "__typename": "CodeSnippetNode"}, {"lang": "Oracle", "langSlug": "oraclesql", "code": "/* Write your PL/SQL query statement below */", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1777](https://leetcode-cn.com/problems/products-price-for-each-store)", "[\u6bcf\u5bb6\u5546\u5e97\u7684\u4ea7\u54c1\u4ef7\u683c](/solution/1700-1799/1777.Product%27s%20Price%20for%20Each%20Store/README.md)", "", "\u7b80\u5355", "\ud83d\udd12"], "md_table_row_en": ["[1777](https://leetcode.com/problems/products-price-for-each-store)", "[Product's Price for Each Store](/solution/1700-1799/1777.Product%27s%20Price%20for%20Each%20Store/README_EN.md)", "", "Easy", "\ud83d\udd12"]}, {"question_id": "1925", "frontend_question_id": "1814", "paid_only": false, "paid_only_cn": false, "url_cn": "https://leetcode-cn.com/problems/count-nice-pairs-in-an-array", "url_en": "https://leetcode.com/problems/count-nice-pairs-in-an-array", "relative_path_cn": "/solution/1800-1899/1814.Count%20Nice%20Pairs%20in%20an%20Array/README.md", "relative_path_en": "/solution/1800-1899/1814.Count%20Nice%20Pairs%20in%20an%20Array/README_EN.md", "title_cn": "\u7edf\u8ba1\u4e00\u4e2a\u6570\u7ec4\u4e2d\u597d\u5bf9\u5b50\u7684\u6570\u76ee", "title_en": "Count Nice Pairs in an Array", "question_title_slug": "count-nice-pairs-in-an-array", "content_en": "
You are given an array nums
that consists of non-negative integers. Let us define rev(x)
as the reverse of the non-negative integer x
. For example, rev(123) = 321
, and rev(120) = 21
. A pair of indices (i, j)
is nice if it satisfies all of the following conditions:
0 <= i < j < nums.length
nums[i] + rev(nums[j]) == nums[j] + rev(nums[i])
Return the number of nice pairs of indices. Since that number can be too large, return it modulo 109 + 7
.
\n
Example 1:
\n\n\nInput: nums = [42,11,1,97]\nOutput: 2\nExplanation: The two pairs are:\n - (0,3) : 42 + rev(97) = 42 + 79 = 121, 97 + rev(42) = 97 + 24 = 121.\n - (1,2) : 11 + rev(1) = 11 + 1 = 12, 1 + rev(11) = 1 + 11 = 12.\n\n\n
Example 2:
\n\n\nInput: nums = [13,10,35,24,76]\nOutput: 4\n\n\n
\n
Constraints:
\n\n1 <= nums.length <= 105
0 <= nums[i] <= 109
\u7ed9\u4f60\u4e00\u4e2a\u6570\u7ec4\u00a0nums
\u00a0\uff0c\u6570\u7ec4\u4e2d\u53ea\u5305\u542b\u975e\u8d1f\u6574\u6570\u3002\u5b9a\u4e49\u00a0rev(x)
\u00a0\u7684\u503c\u4e3a\u5c06\u6574\u6570\u00a0x
\u00a0\u5404\u4e2a\u6570\u5b57\u4f4d\u53cd\u8f6c\u5f97\u5230\u7684\u7ed3\u679c\u3002\u6bd4\u65b9\u8bf4\u00a0rev(123) = 321
\u00a0\uff0c\u00a0rev(120) = 21
\u00a0\u3002\u6211\u4eec\u79f0\u6ee1\u8db3\u4e0b\u9762\u6761\u4ef6\u7684\u4e0b\u6807\u5bf9\u00a0(i, j)
\u662f\u00a0\u597d\u7684\u00a0\uff1a
0 <= i < j < nums.length
nums[i] + rev(nums[j]) == nums[j] + rev(nums[i])
\u8bf7\u4f60\u8fd4\u56de\u597d\u4e0b\u6807\u5bf9\u7684\u6570\u76ee\u3002\u7531\u4e8e\u7ed3\u679c\u53ef\u80fd\u4f1a\u5f88\u5927\uff0c\u8bf7\u5c06\u7ed3\u679c\u5bf9\u00a0109 + 7
\u00a0\u53d6\u4f59\u00a0\u540e\u8fd4\u56de\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1anums = [42,11,1,97]\n\u8f93\u51fa\uff1a2\n\u89e3\u91ca\uff1a\u4e24\u4e2a\u5750\u6807\u5bf9\u4e3a\uff1a\n - (0,3)\uff1a42 + rev(97) = 42 + 79 = 121, 97 + rev(42) = 97 + 24 = 121 \u3002\n - (1,2)\uff1a11 + rev(1) = 11 + 1 = 12, 1 + rev(11) = 1 + 11 = 12 \u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1anums = [13,10,35,24,76]\n\u8f93\u51fa\uff1a4\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= nums.length <= 105
0 <= nums[i] <= 109
There is a donuts shop that bakes donuts in batches of batchSize
. They have a rule where they must serve all of the donuts of a batch before serving any donuts of the next batch. You are given an integer batchSize
and an integer array groups
, where groups[i]
denotes that there is a group of groups[i]
customers that will visit the shop. Each customer will get exactly one donut.
When a group visits the shop, all customers of the group must be served before serving any of the following groups. A group will be happy if they all get fresh donuts. That is, the first customer of the group does not receive a donut that was left over from the previous group.
\n\nYou can freely rearrange the ordering of the groups. Return the maximum possible number of happy groups after rearranging the groups.
\n\n\n
Example 1:
\n\n\nInput: batchSize = 3, groups = [1,2,3,4,5,6]\nOutput: 4\nExplanation: You can arrange the groups as [6,2,4,5,1,3]. Then the 1st, 2nd, 4th, and 6th groups will be happy.\n\n\n
Example 2:
\n\n\nInput: batchSize = 4, groups = [1,3,2,5,2,2,1,6]\nOutput: 4\n\n\n
\n
Constraints:
\n\n1 <= batchSize <= 9
1 <= groups.length <= 30
1 <= groups[i] <= 109
\u6709\u4e00\u4e2a\u751c\u751c\u5708\u5546\u5e97\uff0c\u6bcf\u6279\u6b21\u90fd\u70e4\u00a0batchSize
\u00a0\u4e2a\u751c\u751c\u5708\u3002\u8fd9\u4e2a\u5e97\u94fa\u6709\u4e2a\u89c4\u5219\uff0c\u5c31\u662f\u5728\u70e4\u4e00\u6279\u65b0\u7684\u751c\u751c\u5708\u65f6\uff0c\u4e4b\u524d \u6240\u6709\u00a0\u751c\u751c\u5708\u90fd\u5fc5\u987b\u5df2\u7ecf\u5168\u90e8\u9500\u552e\u5b8c\u6bd5\u3002\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570 batchSize
\u00a0\u548c\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 groups
\u00a0\uff0c\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u6574\u6570\u90fd\u4ee3\u8868\u4e00\u6279\u524d\u6765\u8d2d\u4e70\u751c\u751c\u5708\u7684\u987e\u5ba2\uff0c\u5176\u4e2d groups[i]
\u00a0\u8868\u793a\u8fd9\u4e00\u6279\u987e\u5ba2\u7684\u4eba\u6570\u3002\u6bcf\u4e00\u4f4d\u987e\u5ba2\u90fd\u6070\u597d\u53ea\u8981\u4e00\u4e2a\u751c\u751c\u5708\u3002
\u5f53\u6709\u4e00\u6279\u987e\u5ba2\u6765\u5230\u5546\u5e97\u65f6\uff0c\u4ed6\u4eec\u6240\u6709\u4eba\u90fd\u5fc5\u987b\u5728\u4e0b\u4e00\u6279\u987e\u5ba2\u6765\u4e4b\u524d\u8d2d\u4e70\u5b8c\u751c\u751c\u5708\u3002\u5982\u679c\u4e00\u6279\u987e\u5ba2\u4e2d\u7b2c\u4e00\u4f4d\u987e\u5ba2\u5f97\u5230\u7684\u751c\u751c\u5708\u4e0d\u662f\u4e0a\u4e00\u7ec4\u5269\u4e0b\u7684\uff0c\u90a3\u4e48\u8fd9\u4e00\u7ec4\u4eba\u90fd\u4f1a\u5f88\u5f00\u5fc3\u3002
\n\n\u4f60\u53ef\u4ee5\u968f\u610f\u5b89\u6392\u6bcf\u6279\u987e\u5ba2\u5230\u6765\u7684\u987a\u5e8f\u3002\u8bf7\u4f60\u8fd4\u56de\u5728\u6b64\u524d\u63d0\u4e0b\uff0c\u6700\u591a\u00a0\u6709\u591a\u5c11\u7ec4\u4eba\u4f1a\u611f\u5230\u5f00\u5fc3\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1abatchSize = 3, groups = [1,2,3,4,5,6]\n\u8f93\u51fa\uff1a4\n\u89e3\u91ca\uff1a\u4f60\u53ef\u4ee5\u5c06\u8fd9\u4e9b\u6279\u6b21\u7684\u987e\u5ba2\u987a\u5e8f\u5b89\u6392\u4e3a [6,2,4,5,1,3] \u3002\u90a3\u4e48\u7b2c 1\uff0c2\uff0c4\uff0c6 \u7ec4\u90fd\u4f1a\u611f\u5230\u5f00\u5fc3\u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1abatchSize = 4, groups = [1,3,2,5,2,2,1,6]\n\u8f93\u51fa\uff1a4\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= batchSize <= 9
1 <= groups.length <= 30
1 <= groups[i] <= 109
A sentence is a list of words that are separated by a single space with no leading or trailing spaces. For example, "Hello World"
, "HELLO"
, "hello world hello world"
are all sentences. Words consist of only uppercase and lowercase English letters.
Two sentences sentence1
and sentence2
are similar if it is possible to insert an arbitrary sentence (possibly empty) inside one of these sentences such that the two sentences become equal. For example, sentence1 = "Hello my name is Jane"
and sentence2 = "Hello Jane"
can be made equal by inserting "my name is"
between "Hello"
and "Jane"
in sentence2
.
Given two sentences sentence1
and sentence2
, return true
if sentence1
and sentence2
are similar. Otherwise, return false
.
\n
Example 1:
\n\n\nInput: sentence1 = "My name is Haley", sentence2 = "My Haley"\nOutput: true\nExplanation: sentence2 can be turned to sentence1 by inserting "name is" between "My" and "Haley".\n\n\n
Example 2:
\n\n\nInput: sentence1 = "of", sentence2 = "A lot of words"\nOutput: false\nExplanation: No single sentence can be inserted inside one of the sentences to make it equal to the other.\n\n\n
Example 3:
\n\n\nInput: sentence1 = "Eating right now", sentence2 = "Eating"\nOutput: true\nExplanation: sentence2 can be turned to sentence1 by inserting "right now" at the end of the sentence.\n\n\n
Example 4:
\n\n\nInput: sentence1 = "Luky", sentence2 = "Lucccky"\nOutput: false\n\n\n
\n
Constraints:
\n\n1 <= sentence1.length, sentence2.length <= 100
sentence1
and sentence2
consist of lowercase and uppercase English letters and spaces.sentence1
and sentence2
are separated by a single space.\u4e00\u4e2a\u53e5\u5b50\u662f\u7531\u4e00\u4e9b\u5355\u8bcd\u4e0e\u5b83\u4eec\u4e4b\u95f4\u7684\u5355\u4e2a\u7a7a\u683c\u7ec4\u6210\uff0c\u4e14\u53e5\u5b50\u7684\u5f00\u5934\u548c\u7ed3\u5c3e\u6ca1\u6709\u591a\u4f59\u7a7a\u683c\u3002\u6bd4\u65b9\u8bf4\uff0c\"Hello World\"
\u00a0\uff0c\"HELLO\"
\u00a0\uff0c\"hello world hello world\"
\u00a0\u90fd\u662f\u53e5\u5b50\u3002\u6bcf\u4e2a\u5355\u8bcd\u90fd \u53ea\u00a0\u5305\u542b\u5927\u5199\u548c\u5c0f\u5199\u82f1\u6587\u5b57\u6bcd\u3002
\u5982\u679c\u4e24\u4e2a\u53e5\u5b50\u00a0sentence1
\u548c\u00a0sentence2
\u00a0\uff0c\u53ef\u4ee5\u901a\u8fc7\u5f80\u5176\u4e2d\u4e00\u4e2a\u53e5\u5b50\u63d2\u5165\u4e00\u4e2a\u4efb\u610f\u7684\u53e5\u5b50\uff08\u53ef\u4ee5\u662f\u7a7a\u53e5\u5b50\uff09\u800c\u5f97\u5230\u53e6\u4e00\u4e2a\u53e5\u5b50\uff0c\u90a3\u4e48\u6211\u4eec\u79f0\u8fd9\u4e24\u4e2a\u53e5\u5b50\u662f \u76f8\u4f3c\u7684\u00a0\u3002\u6bd4\u65b9\u8bf4\uff0csentence1 = \"Hello my name is Jane\"
\u4e14\u00a0sentence2 = \"Hello Jane\"
\u00a0\uff0c\u6211\u4eec\u53ef\u4ee5\u5f80 sentence2
\u00a0\u4e2d\u00a0\"Hello\"
\u548c\u00a0\"Jane\"
\u00a0\u4e4b\u95f4\u63d2\u5165\u00a0\"my name is\"
\u00a0\u5f97\u5230 sentence1
\u00a0\u3002
\u7ed9\u4f60\u4e24\u4e2a\u53e5\u5b50\u00a0sentence1
\u548c\u00a0sentence2
\u00a0\uff0c\u5982\u679c\u00a0sentence1
\u548c\u00a0sentence2
\u662f\u76f8\u4f3c\u7684\uff0c\u8bf7\u4f60\u8fd4\u56de\u00a0true
\u00a0\uff0c\u5426\u5219\u8fd4\u56de\u00a0false
\u00a0\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1asentence1 = \"My name is Haley\", sentence2 = \"My Haley\"\n\u8f93\u51fa\uff1atrue\n\u89e3\u91ca\uff1a\u53ef\u4ee5\u5f80 sentence2 \u4e2d \"My\" \u548c \"Haley\" \u4e4b\u95f4\u63d2\u5165 \"name is\" \uff0c\u5f97\u5230 sentence1 \u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1asentence1 = \"of\", sentence2 = \"A lot of words\"\n\u8f93\u51fa\uff1afalse\n\u89e3\u91ca\uff1a\u6ca1\u6cd5\u5f80\u8fd9\u4e24\u4e2a\u53e5\u5b50\u4e2d\u7684\u4e00\u4e2a\u53e5\u5b50\u53ea\u63d2\u5165\u4e00\u4e2a\u53e5\u5b50\u5c31\u5f97\u5230\u53e6\u4e00\u4e2a\u53e5\u5b50\u3002\n\n\n
\u793a\u4f8b 3\uff1a
\n\n\u8f93\u5165\uff1asentence1 = \"Eating right now\", sentence2 = \"Eating\"\n\u8f93\u51fa\uff1atrue\n\u89e3\u91ca\uff1a\u53ef\u4ee5\u5f80 sentence2 \u7684\u7ed3\u5c3e\u63d2\u5165 \"right now\" \u5f97\u5230 sentence1 \u3002\n\n\n
\u793a\u4f8b 4\uff1a
\n\n\u8f93\u5165\uff1asentence1 = \"Luky\", sentence2 = \"Lucccky\"\n\u8f93\u51fa\uff1afalse\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= sentence1.length, sentence2.length <= 100
sentence1
\u00a0\u548c\u00a0sentence2
\u00a0\u90fd\u53ea\u5305\u542b\u5927\u5c0f\u5199\u82f1\u6587\u5b57\u6bcd\u548c\u7a7a\u683c\u3002sentence1
\u00a0\u548c\u00a0sentence2
\u00a0\u4e2d\u7684\u5355\u8bcd\u90fd\u53ea\u7531\u5355\u4e2a\u7a7a\u683c\u9694\u5f00\u3002You are given coordinates
, a string that represents the coordinates of a square of the chessboard. Below is a chessboard for your reference.
Return true
if the square is white, and false
if the square is black.
The coordinate will always represent a valid chessboard square. The coordinate will always have the letter first, and the number second.
\n\n\n
Example 1:
\n\n\nInput: coordinates = "a1"\nOutput: false\nExplanation: From the chessboard above, the square with coordinates "a1" is black, so return false.\n\n\n
Example 2:
\n\n\nInput: coordinates = "h3"\nOutput: true\nExplanation: From the chessboard above, the square with coordinates "h3" is white, so return true.\n\n\n
Example 3:
\n\n\nInput: coordinates = "c7"\nOutput: false\n\n\n
\n
Constraints:
\n\ncoordinates.length == 2
'a' <= coordinates[0] <= 'h'
'1' <= coordinates[1] <= '8'
\u7ed9\u4f60\u4e00\u4e2a\u5750\u6807\u00a0coordinates
\u00a0\uff0c\u5b83\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u8868\u793a\u56fd\u9645\u8c61\u68cb\u68cb\u76d8\u4e2d\u4e00\u4e2a\u683c\u5b50\u7684\u5750\u6807\u3002\u4e0b\u56fe\u662f\u56fd\u9645\u8c61\u68cb\u68cb\u76d8\u793a\u610f\u56fe\u3002
\u5982\u679c\u6240\u7ed9\u683c\u5b50\u7684\u989c\u8272\u662f\u767d\u8272\uff0c\u8bf7\u4f60\u8fd4\u56de\u00a0true
\uff0c\u5982\u679c\u662f\u9ed1\u8272\uff0c\u8bf7\u8fd4\u56de\u00a0false
\u00a0\u3002
\u7ed9\u5b9a\u5750\u6807\u4e00\u5b9a\u4ee3\u8868\u56fd\u9645\u8c61\u68cb\u68cb\u76d8\u4e0a\u4e00\u4e2a\u5b58\u5728\u7684\u683c\u5b50\u3002\u5750\u6807\u7b2c\u4e00\u4e2a\u5b57\u7b26\u662f\u5b57\u6bcd\uff0c\u7b2c\u4e8c\u4e2a\u5b57\u7b26\u662f\u6570\u5b57\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1acoordinates = \"a1\"\n\u8f93\u51fa\uff1afalse\n\u89e3\u91ca\uff1a\u5982\u4e0a\u56fe\u68cb\u76d8\u6240\u793a\uff0c\"a1\" \u5750\u6807\u7684\u683c\u5b50\u662f\u9ed1\u8272\u7684\uff0c\u6240\u4ee5\u8fd4\u56de false \u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1acoordinates = \"h3\"\n\u8f93\u51fa\uff1atrue\n\u89e3\u91ca\uff1a\u5982\u4e0a\u56fe\u68cb\u76d8\u6240\u793a\uff0c\"h3\" \u5750\u6807\u7684\u683c\u5b50\u662f\u767d\u8272\u7684\uff0c\u6240\u4ee5\u8fd4\u56de true \u3002\n\n\n
\u793a\u4f8b 3\uff1a
\n\n\n\u8f93\u5165\uff1acoordinates = \"c7\"\n\u8f93\u51fa\uff1afalse\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\ncoordinates.length == 2
'a' <= coordinates[0] <= 'h'
'1' <= coordinates[1] <= '8'
You are given a string array features
where features[i]
is a single word that represents the name of a feature of the latest product you are working on. You have made a survey where users have reported which features they like. You are given a string array responses
, where each responses[i]
is a string containing space-separated words.
The popularity of a feature is the number of responses[i]
that contain the feature. You want to sort the features in non-increasing order by their popularity. If two features have the same popularity, order them by their original index in features
. Notice that one response could contain the same feature multiple times; this feature is only counted once in its popularity.
Return the features in sorted order.
\n\n\n
Example 1:
\n\n\nInput: features = ["cooler","lock","touch"], responses = ["i like cooler cooler","lock touch cool","locker like touch"]\nOutput: ["touch","cooler","lock"]\nExplanation: appearances("cooler") = 1, appearances("lock") = 1, appearances("touch") = 2. Since "cooler" and "lock" both had 1 appearance, "cooler" comes first because "cooler" came first in the features array.\n\n\n
Example 2:
\n\n\nInput: features = ["a","aa","b","c"], responses = ["a","a aa","a a a a a","b a"]\nOutput: ["a","aa","b","c"]\n\n\n
\n
Constraints:
\n\n1 <= features.length <= 104
1 <= features[i].length <= 10
features
contains no duplicates.features[i]
consists of lowercase letters.1 <= responses.length <= 102
1 <= responses[i].length <= 103
responses[i]
consists of lowercase letters and spaces.responses[i]
contains no two consecutive spaces.responses[i]
has no leading or trailing spaces.\u7ed9\u5b9a\u4e00\u4e2a\u5b57\u7b26\u4e32\u6570\u7ec4\u00a0features
\u00a0\uff0c\u5176\u4e2d\u00a0features[i]
\u00a0\u662f\u4e00\u4e2a\u5355\u8bcd\uff0c\u63cf\u8ff0\u4f60\u6700\u8fd1\u53c2\u4e0e\u5f00\u53d1\u7684\u9879\u76ee\u4e2d\u4e00\u4e2a\u529f\u80fd\u7684\u540d\u79f0\u3002\u4f60\u8c03\u67e5\u4e86\u7528\u6237\u559c\u6b22\u54ea\u4e9b\u529f\u80fd\u3002\u53e6\u7ed9\u5b9a\u4e00\u4e2a\u5b57\u7b26\u4e32\u6570\u7ec4\u00a0responses
\uff0c\u5176\u4e2d\u00a0responses[i]
\u00a0\u662f\u4e00\u4e2a\u5305\u542b\u4ee5\u7a7a\u683c\u5206\u9694\u7684\u4e00\u7cfb\u5217\u5355\u8bcd\u7684\u5b57\u7b26\u4e32\u3002
\u4f60\u60f3\u8981\u6309\u7167\u53d7\u6b22\u8fce\u7a0b\u5ea6\u6392\u5217\u8fd9\u4e9b\u529f\u80fd\u3002\u00a0\u4e25\u683c\u5730\u8bf4\uff0c\u4ee4\u00a0appearances(word)
\u00a0\u662f\u6ee1\u8db3 responses[i]
\u00a0\u4e2d\u5305\u542b\u5355\u8bcd\u00a0word
\u00a0\u7684\u00a0i
\u00a0\u7684\u4e2a\u6570\uff0c\u5219\u5f53\u00a0appearances(features[x]) > appearances(features[y])
\u00a0\u65f6\uff0c\u7b2c\u00a0x
\u00a0\u4e2a\u529f\u80fd\u6bd4\u7b2c\u00a0y
\u00a0\u4e2a\u529f\u80fd\u66f4\u53d7\u6b22\u8fce\u3002
\u8fd4\u56de\u4e00\u4e2a\u6570\u7ec4\u00a0sortedFeatures
\u00a0\uff0c\u5305\u542b\u6309\u53d7\u6b22\u8fce\u7a0b\u5ea6\u6392\u5217\u7684\u529f\u80fd\u540d\u79f0\u3002\u5f53\u7b2c\u00a0x
\u00a0 \u4e2a\u529f\u80fd\u548c\u7b2c\u00a0y
\u00a0\u4e2a\u529f\u80fd\u7684\u53d7\u6b22\u8fce\u7a0b\u5ea6\u76f8\u540c\u4e14\u00a0x < y
\u00a0\u65f6\uff0c\u4f60\u5e94\u5f53\u5c06\u7b2c\u00a0x
\u00a0\u4e2a\u529f\u80fd\u653e\u5728\u7b2c\u00a0y
\u00a0\u4e2a\u529f\u80fd\u4e4b\u524d\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1afeatures = [\"cooler\",\"lock\",\"touch\"], responses = [\"i like cooler cooler\",\"lock touch cool\",\"locker like touch\"]\n\u8f93\u51fa\uff1a[\"touch\",\"cooler\",\"lock\"]\n\u89e3\u91ca\uff1aappearances(\"cooler\") = 1\uff0cappearances(\"lock\") = 1\uff0cappearances(\"touch\") = 2\u3002\u7531\u4e8e \"cooler\" \u548c \"lock\" \u90fd\u51fa\u73b0\u4e86 1 \u6b21\uff0c\u4e14 \"cooler\" \u5728\u539f\u6570\u7ec4\u7684\u524d\u9762\uff0c\u6240\u4ee5 \"cooler\" \u4e5f\u5e94\u8be5\u5728\u7ed3\u679c\u6570\u7ec4\u7684\u524d\u9762\u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1afeatures = [\"a\",\"aa\",\"b\",\"c\"], responses = [\"a\",\"a aa\",\"a a a a a\",\"b a\"]\n\u8f93\u51fa\uff1a[\"a\",\"aa\",\"b\",\"c\"]\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= features.length\u00a0<= 104
1 <= features[i].length <= 10
features
\u00a0\u4e0d\u5305\u542b\u91cd\u590d\u9879\u3002features[i]
\u00a0\u7531\u5c0f\u5199\u5b57\u6bcd\u6784\u6210\u30021 <= responses.length <= 102
1 <= responses[i].length <= 103
responses[i]
\u00a0\u7531\u5c0f\u5199\u5b57\u6bcd\u548c\u7a7a\u683c\u7ec4\u6210\u3002responses[i]
\u00a0\u4e0d\u5305\u542b\u4e24\u4e2a\u8fde\u7eed\u7684\u7a7a\u683c\u3002responses[i]
\u00a0\u6ca1\u6709\u524d\u7f6e\u6216\u540e\u7f6e\u7a7a\u683c\u3002You are given an array of integers nums
(0-indexed) and an integer k
.
The score of a subarray (i, j)
is defined as min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)
. A good subarray is a subarray where i <= k <= j
.
Return the maximum possible score of a good subarray.
\n\n\n
Example 1:
\n\n\nInput: nums = [1,4,3,7,4,5], k = 3\nOutput: 15\nExplanation: The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15. \n\n\n
Example 2:
\n\n\nInput: nums = [5,5,4,5,4,1,1,1], k = 0\nOutput: 20\nExplanation: The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.\n\n\n
\n
Constraints:
\n\n1 <= nums.length <= 105
1 <= nums[i] <= 2 * 104
0 <= k < nums.length
\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u6570\u7ec4\u00a0nums
\u00a0\uff08\u4e0b\u6807\u4ece 0 \u5f00\u59cb\uff09\u548c\u4e00\u4e2a\u6574\u6570\u00a0k
\u00a0\u3002
\u4e00\u4e2a\u5b50\u6570\u7ec4 (i, j)
\u00a0\u7684 \u5206\u6570\u00a0\u5b9a\u4e49\u4e3a\u00a0min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)
\u00a0\u3002\u4e00\u4e2a\u00a0\u597d\u00a0\u5b50\u6570\u7ec4\u7684\u4e24\u4e2a\u7aef\u70b9\u4e0b\u6807\u9700\u8981\u6ee1\u8db3\u00a0i <= k <= j
\u00a0\u3002
\u8bf7\u4f60\u8fd4\u56de \u597d\u00a0\u5b50\u6570\u7ec4\u7684\u6700\u5927\u53ef\u80fd \u5206\u6570\u00a0\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1anums = [1,4,3,7,4,5], k = 3\n\u8f93\u51fa\uff1a15\n\u89e3\u91ca\uff1a\u6700\u4f18\u5b50\u6570\u7ec4\u7684\u5de6\u53f3\u7aef\u70b9\u4e0b\u6807\u662f (1, 5) \uff0c\u5206\u6570\u4e3a min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15 \u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1anums = [5,5,4,5,4,1,1,1], k = 0\n\u8f93\u51fa\uff1a20\n\u89e3\u91ca\uff1a\u6700\u4f18\u5b50\u6570\u7ec4\u7684\u5de6\u53f3\u7aef\u70b9\u4e0b\u6807\u662f (0, 4) \uff0c\u5206\u6570\u4e3a min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20 \u3002\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= nums.length <= 105
1 <= nums[i] <= 2 * 104
0 <= k < nums.length
There is a school that has classes of students and each class will be having a final exam. You are given a 2D integer array classes
, where classes[i] = [passi, totali]
. You know beforehand that in the ith
class, there are totali
total students, but only passi
number of students will pass the exam.
You are also given an integer extraStudents
. There are another extraStudents
brilliant students that are guaranteed to pass the exam of any class they are assigned to. You want to assign each of the extraStudents
students to a class in a way that maximizes the average pass ratio across all the classes.
The pass ratio of a class is equal to the number of students of the class that will pass the exam divided by the total number of students of the class. The average pass ratio is the sum of pass ratios of all the classes divided by the number of the classes.
\n\nReturn the maximum possible average pass ratio after assigning the extraStudents
students. Answers within 10-5
of the actual answer will be accepted.
\n
Example 1:
\n\n\nInput: classes = [[1,2],[3,5],[2,2]], extraStudents
= 2\nOutput: 0.78333\nExplanation: You can assign the two extra students to the first class. The average pass ratio will be equal to (3/4 + 3/5 + 2/2) / 3 = 0.78333.\n
\n\nExample 2:
\n\n\nInput: classes = [[2,4],[3,9],[4,5],[2,10]], extraStudents
= 4\nOutput: 0.53485\n
\n\n\n
Constraints:
\n\n1 <= classes.length <= 105
classes[i].length == 2
1 <= passi <= totali <= 105
1 <= extraStudents <= 105
\u4e00\u6240\u5b66\u6821\u91cc\u6709\u4e00\u4e9b\u73ed\u7ea7\uff0c\u6bcf\u4e2a\u73ed\u7ea7\u91cc\u6709\u4e00\u4e9b\u5b66\u751f\uff0c\u73b0\u5728\u6bcf\u4e2a\u73ed\u90fd\u4f1a\u8fdb\u884c\u4e00\u573a\u671f\u672b\u8003\u8bd5\u3002\u7ed9\u4f60\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4 classes
\u00a0\uff0c\u5176\u4e2d\u00a0classes[i] = [passi, totali]
\u00a0\uff0c\u8868\u793a\u4f60\u63d0\u524d\u77e5\u9053\u4e86\u7b2c\u00a0i
\u00a0\u4e2a\u73ed\u7ea7\u603b\u5171\u6709\u00a0totali
\u00a0\u4e2a\u5b66\u751f\uff0c\u5176\u4e2d\u53ea\u6709\u00a0passi
\u00a0\u4e2a\u5b66\u751f\u53ef\u4ee5\u901a\u8fc7\u8003\u8bd5\u3002
\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u00a0extraStudents
\u00a0\uff0c\u8868\u793a\u989d\u5916\u6709\u00a0extraStudents
\u00a0\u4e2a\u806a\u660e\u7684\u5b66\u751f\uff0c\u4ed6\u4eec \u4e00\u5b9a\u00a0\u80fd\u901a\u8fc7\u4efb\u4f55\u73ed\u7ea7\u7684\u671f\u672b\u8003\u3002\u4f60\u9700\u8981\u7ed9\u8fd9\u00a0extraStudents
\u00a0\u4e2a\u5b66\u751f\u6bcf\u4eba\u90fd\u5b89\u6392\u4e00\u4e2a\u73ed\u7ea7\uff0c\u4f7f\u5f97 \u6240\u6709\u00a0\u73ed\u7ea7\u7684 \u5e73\u5747\u00a0\u901a\u8fc7\u7387 \u6700\u5927\u00a0\u3002
\u4e00\u4e2a\u73ed\u7ea7\u7684\u00a0\u901a\u8fc7\u7387\u00a0\u7b49\u4e8e\u8fd9\u4e2a\u73ed\u7ea7\u901a\u8fc7\u8003\u8bd5\u7684\u5b66\u751f\u4eba\u6570\u9664\u4ee5\u8fd9\u4e2a\u73ed\u7ea7\u7684\u603b\u4eba\u6570\u3002\u5e73\u5747\u901a\u8fc7\u7387\u00a0\u662f\u6240\u6709\u73ed\u7ea7\u7684\u901a\u8fc7\u7387\u4e4b\u548c\u9664\u4ee5\u73ed\u7ea7\u6570\u76ee\u3002
\n\n\u8bf7\u4f60\u8fd4\u56de\u5728\u5b89\u6392\u8fd9 extraStudents
\u4e2a\u5b66\u751f\u53bb\u5bf9\u5e94\u73ed\u7ea7\u540e\u7684 \u6700\u5927\u00a0\u5e73\u5747\u901a\u8fc7\u7387\u3002\u4e0e\u6807\u51c6\u7b54\u6848\u8bef\u5dee\u8303\u56f4\u5728\u00a010-5
\u00a0\u4ee5\u5185\u7684\u7ed3\u679c\u90fd\u4f1a\u89c6\u4e3a\u6b63\u786e\u7ed3\u679c\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1aclasses = [[1,2],[3,5],[2,2]], extraStudents
= 2\n\u8f93\u51fa\uff1a0.78333\n\u89e3\u91ca\uff1a\u4f60\u53ef\u4ee5\u5c06\u989d\u5916\u7684\u4e24\u4e2a\u5b66\u751f\u90fd\u5b89\u6392\u5230\u7b2c\u4e00\u4e2a\u73ed\u7ea7\uff0c\u5e73\u5747\u901a\u8fc7\u7387\u4e3a (3/4 + 3/5 + 2/2) / 3 = 0.78333 \u3002\n
\n\n\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1aclasses = [[2,4],[3,9],[4,5],[2,10]], extraStudents
= 4\n\u8f93\u51fa\uff1a0.53485\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= classes.length <= 105
classes[i].length == 2
1 <= passi <= totali <= 105
1 <= extraStudents <= 105
There is an undirected star graph consisting of n
nodes labeled from 1
to n
. A star graph is a graph where there is one center node and exactly n - 1
edges that connect the center node with every other node.
You are given a 2D integer array edges
where each edges[i] = [ui, vi]
indicates that there is an edge between the nodes ui
and vi
. Return the center of the given star graph.
\r\n
Example 1:
\r\n\r\nInput: edges = [[1,2],[2,3],[4,2]]\r\nOutput: 2\r\nExplanation: As shown in the figure above, node 2 is connected to every other node, so 2 is the center.\r\n\r\n\r\n
Example 2:
\r\n\r\n\r\nInput: edges = [[1,2],[5,1],[1,3],[1,4]]\r\nOutput: 1\r\n\r\n\r\n
\r\n
Constraints:
\r\n\r\n3 <= n <= 105
edges.length == n - 1
edges[i].length == 2
1 <= ui, vi <= n
ui != vi
edges
represent a valid star graph.\u6709\u4e00\u4e2a\u65e0\u5411\u7684 \u661f\u578b \u56fe\uff0c\u7531 n
\u4e2a\u7f16\u53f7\u4ece 1
\u5230 n
\u7684\u8282\u70b9\u7ec4\u6210\u3002\u661f\u578b\u56fe\u6709\u4e00\u4e2a \u4e2d\u5fc3 \u8282\u70b9\uff0c\u5e76\u4e14\u6070\u6709 n - 1
\u6761\u8fb9\u5c06\u4e2d\u5fc3\u8282\u70b9\u4e0e\u5176\u4ed6\u6bcf\u4e2a\u8282\u70b9\u8fde\u63a5\u8d77\u6765\u3002
\u7ed9\u4f60\u4e00\u4e2a\u4e8c\u7ef4\u6574\u6570\u6570\u7ec4 edges
\uff0c\u5176\u4e2d\u00a0edges[i] = [ui, vi]
\u8868\u793a\u5728\u8282\u70b9 ui
\u548c vi
\u4e4b\u95f4\u5b58\u5728\u4e00\u6761\u8fb9\u3002\u8bf7\u4f60\u627e\u51fa\u5e76\u8fd4\u56de\u00a0edges
\u6240\u8868\u793a\u661f\u578b\u56fe\u7684\u4e2d\u5fc3\u8282\u70b9\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1aedges = [[1,2],[2,3],[4,2]]\n\u8f93\u51fa\uff1a2\n\u89e3\u91ca\uff1a\u5982\u4e0a\u56fe\u6240\u793a\uff0c\u8282\u70b9 2 \u4e0e\u5176\u4ed6\u6bcf\u4e2a\u8282\u70b9\u90fd\u76f8\u8fde\uff0c\u6240\u4ee5\u8282\u70b9 2 \u662f\u4e2d\u5fc3\u8282\u70b9\u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1aedges = [[1,2],[5,1],[1,3],[1,4]]\n\u8f93\u51fa\uff1a1\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n3 <= n <= 105
edges.length == n - 1
edges[i].length == 2
1 <= ui, vi <= n
ui != vi
edges
\u8868\u793a\u4e00\u4e2a\u6709\u6548\u7684\u661f\u578b\u56feYou are given two strings s1
and s2
of equal length. A string swap is an operation where you choose two indices in a string (not necessarily different) and swap the characters at these indices.
Return true
if it is possible to make both strings equal by performing at most one string swap on exactly one of the strings. Otherwise, return false
.
\n
Example 1:
\n\n\nInput: s1 = "bank", s2 = "kanb"\nOutput: true\nExplanation: For example, swap the first character with the last character of s2 to make "bank".\n\n\n
Example 2:
\n\n\nInput: s1 = "attack", s2 = "defend"\nOutput: false\nExplanation: It is impossible to make them equal with one string swap.\n\n\n
Example 3:
\n\n\nInput: s1 = "kelb", s2 = "kelb"\nOutput: true\nExplanation: The two strings are already equal, so no string swap operation is required.\n\n\n
Example 4:
\n\n\nInput: s1 = "abcd", s2 = "dcba"\nOutput: false\n\n\n
\n
Constraints:
\n\n1 <= s1.length, s2.length <= 100
s1.length == s2.length
s1
and s2
consist of only lowercase English letters.\u7ed9\u4f60\u957f\u5ea6\u76f8\u7b49\u7684\u4e24\u4e2a\u5b57\u7b26\u4e32 s1
\u548c s2
\u3002\u4e00\u6b21 \u5b57\u7b26\u4e32\u4ea4\u6362 \u64cd\u4f5c\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a\u9009\u51fa\u67d0\u4e2a\u5b57\u7b26\u4e32\u4e2d\u7684\u4e24\u4e2a\u4e0b\u6807\uff08\u4e0d\u5fc5\u4e0d\u540c\uff09\uff0c\u5e76\u4ea4\u6362\u8fd9\u4e24\u4e2a\u4e0b\u6807\u6240\u5bf9\u5e94\u7684\u5b57\u7b26\u3002
\u5982\u679c\u5bf9 \u5176\u4e2d\u4e00\u4e2a\u5b57\u7b26\u4e32 \u6267\u884c \u6700\u591a\u4e00\u6b21\u5b57\u7b26\u4e32\u4ea4\u6362 \u5c31\u53ef\u4ee5\u4f7f\u4e24\u4e2a\u5b57\u7b26\u4e32\u76f8\u7b49\uff0c\u8fd4\u56de true
\uff1b\u5426\u5219\uff0c\u8fd4\u56de false
\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1as1 = \"bank\", s2 = \"kanb\"\n\u8f93\u51fa\uff1atrue\n\u89e3\u91ca\uff1a\u4f8b\u5982\uff0c\u4ea4\u6362 s2 \u4e2d\u7684\u7b2c\u4e00\u4e2a\u548c\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u53ef\u4ee5\u5f97\u5230 \"bank\"\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1as1 = \"attack\", s2 = \"defend\"\n\u8f93\u51fa\uff1afalse\n\u89e3\u91ca\uff1a\u4e00\u6b21\u5b57\u7b26\u4e32\u4ea4\u6362\u65e0\u6cd5\u4f7f\u4e24\u4e2a\u5b57\u7b26\u4e32\u76f8\u7b49\n\n\n
\u793a\u4f8b 3\uff1a
\n\n\u8f93\u5165\uff1as1 = \"kelb\", s2 = \"kelb\"\n\u8f93\u51fa\uff1atrue\n\u89e3\u91ca\uff1a\u4e24\u4e2a\u5b57\u7b26\u4e32\u5df2\u7ecf\u76f8\u7b49\uff0c\u6240\u4ee5\u4e0d\u9700\u8981\u8fdb\u884c\u5b57\u7b26\u4e32\u4ea4\u6362\n\n\n
\u793a\u4f8b 4\uff1a
\n\n\u8f93\u5165\uff1as1 = \"abcd\", s2 = \"dcba\"\n\u8f93\u51fa\uff1afalse\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= s1.length, s2.length <= 100
s1.length == s2.length
s1
\u548c s2
\u4ec5\u7531\u5c0f\u5199\u82f1\u6587\u5b57\u6bcd\u7ec4\u6210Table: Tasks
\n+----------------+---------+\n| Column Name | Type |\n+----------------+---------+\n| task_id | int |\n| subtasks_count | int |\n+----------------+---------+\ntask_id is the primary key for this table.\nEach row in this table indicates that task_id was divided into subtasks_count subtasks labelled from 1 to subtasks_count.\nIt is guaranteed that 2 <= subtasks_count <= 20.\n\n\n
\n\n
Table: Executed
\n+---------------+---------+\n| Column Name | Type |\n+---------------+---------+\n| task_id | int |\n| subtask_id | int |\n+---------------+---------+\n(task_id, subtask_id) is the primary key for this table.\nEach row in this table indicates that for the task task_id, the subtask with ID subtask_id was executed successfully.\nIt is guaranteed that subtask_id <= subtasks_count for each task_id.\n\n
\n\n
Write an SQL query to report the IDs of the missing subtasks for each task_id
.
Return the result table in any order.
\n\nThe query result format is in the following example:
\n\n\n\n
\nTasks table:\n+---------+----------------+\n| task_id | subtasks_count |\n+---------+----------------+\n| 1 | 3 |\n| 2 | 2 |\n| 3 | 4 |\n+---------+----------------+\n\nExecuted table:\n+---------+------------+\n| task_id | subtask_id |\n+---------+------------+\n| 1 | 2 |\n| 3 | 1 |\n| 3 | 2 |\n| 3 | 3 |\n| 3 | 4 |\n+---------+------------+\n\nResult table:\n+---------+------------+\n| task_id | subtask_id |\n+---------+------------+\n| 1 | 1 |\n| 1 | 3 |\n| 2 | 1 |\n| 2 | 2 |\n+---------+------------+\nTask 1 was divided into 3 subtasks (1, 2, 3). Only subtask 2 was executed successfully, so we include (1, 1) and (1, 3) in the answer.\nTask 2 was divided into 2 subtasks (1, 2). No subtask was executed successfully, so we include (2, 1) and (2, 2) in the answer.\nTask 3 was divided into 4 subtasks (1, 2, 3, 4). All of the subtasks were executed successfully.\n\n", "content_cn": "
\u8868\uff1aTasks
\n+----------------+---------+\n| Column Name | Type |\n+----------------+---------+\n| task_id | int |\n| subtasks_count | int |\n+----------------+---------+\ntask_id \u662f\u8fd9\u4e2a\u8868\u7684\u4e3b\u952e\u3002\ntask_id \u8868\u793a\u7684\u4e3a\u4e3b\u4efb\u52a1\u7684id,\u6bcf\u4e00\u4e2atask_id\u88ab\u5206\u4e3a\u4e86\u591a\u4e2a\u5b50\u4efb\u52a1(subtasks)\uff0csubtasks_count\u8868\u793a\u4e3a\u5b50\u4efb\u52a1\u7684\u4e2a\u6570\uff08n\uff09\uff0c\u5b83\u7684\u503c\u8868\u793a\u4e86\u5b50\u4efb\u52a1\u7684\u7d22\u5f15\u4ece1\u5230n\u3002\n\u672c\u8868\u4fdd\u8bc12 <=subtasks_count<= 20\u3002\n\n\n
\u8868\uff1a Executed
\n+---------------+---------+\n| Column Name | Type |\n+---------------+---------+\n| task_id | int |\n| subtask_id | int |\n+---------------+---------+\n(task_id, subtask_id) \u662f\u8fd9\u4e2a\u8868\u7684\u4e3b\u952e\u3002\n\u6bcf\u4e00\u884c\u8868\u793a\u6807\u8bb0\u4e3atask_id\u7684\u4e3b\u4efb\u52a1\u4e0e\u6807\u8bb0\u4e3asubtask_id\u7684\u5b50\u4efb\u52a1\u88ab\u6210\u529f\u6267\u884c\u3002\n\u672c\u8868\u4fdd\u8bc1\uff0c\u5bf9\u4e8e\u6bcf\u4e00\u4e2atask_id\uff0csubtask_id <= subtasks_count\u3002\n\n\n
\u00a0
\n\n\u8bf7\u8bd5\u5199\u4e00\u4e2aSQL\u67e5\u8be2\u8bed\u53e5\u62a5\u544a\u6ca1\u6709\u88ab\u6267\u884c\u7684\uff08\u4e3b\u4efb\u52a1\uff0c\u5b50\u4efb\u52a1\uff09\u5bf9\uff0c\u5373\u6ca1\u6709\u88ab\u6267\u884c\u7684\uff08task_id, subtask_id\uff09\u3002
\n\n\u4ee5 \u4efb\u4f55\u987a\u5e8f \u8fd4\u56de\u5373\u53ef\u3002
\n\n\u67e5\u8be2\u7ed3\u679c\u683c\u5f0f\u5982\u4e0b\uff1a
\n\n\u00a0
\n\n\nTasks table:\n+---------+----------------+\n| task_id | subtasks_count |\n+---------+----------------+\n| 1 | 3 |\n| 2 | 2 |\n| 3 | 4 |\n+---------+----------------+\n\nExecuted table:\n+---------+------------+\n| task_id | subtask_id |\n+---------+------------+\n| 1 | 2 |\n| 3 | 1 |\n| 3 | 2 |\n| 3 | 3 |\n| 3 | 4 |\n+---------+------------+\n\nResult table:\n+---------+------------+\n| task_id | subtask_id |\n+---------+------------+\n| 1 | 1 |\n| 1 | 3 |\n| 2 | 1 |\n| 2 | 2 |\n+---------+------------+\nTask 1 \u88ab\u5206\u6210\u4e86 3 subtasks (1, 2, 3)\u3002\u53ea\u6709 subtask 2 \u88ab\u6210\u529f\u6267\u884c, \u6240\u4ee5\u6211\u4eec\u8fd4\u56de (1, 1) \u548c (1, 3) \u8fd9\u4e24\u4e2a\u4e3b\u4efb\u52a1\u5b50\u4efb\u52a1\u5bf9\u3002\nTask 2 \u88ab\u5206\u6210\u4e86 2 subtasks (1, 2)\u3002\u6ca1\u6709\u4e00\u4e2asubtask\u88ab\u6210\u529f\u6267\u884c, \u56e0\u6b64\u6211\u4eec\u8fd4\u56de(2, 1)\u548c(2, 2)\u3002\nTask 3 \u88ab\u5206\u6210\u4e86 4 subtasks (1, 2, 3, 4)\u3002\u6240\u6709\u7684subtask\u90fd\u88ab\u6210\u529f\u6267\u884c\uff0c\u56e0\u6b64\u5bf9\u4e8eTask 3,\u6211\u4eec\u4e0d\u8fd4\u56de\u4efb\u4f55\u503c\u3002\n\n", "tags_en": [], "tags_cn": [], "difficulty_en": "Hard", "difficulty_cn": "\u56f0\u96be", "code_snippets": [{"lang": "MySQL", "langSlug": "mysql", "code": "# Write your MySQL query statement below", "__typename": "CodeSnippetNode"}, {"lang": "MS SQL Server", "langSlug": "mssql", "code": "/* Write your T-SQL query statement below */", "__typename": "CodeSnippetNode"}, {"lang": "Oracle", "langSlug": "oraclesql", "code": "/* Write your PL/SQL query statement below */", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1767](https://leetcode-cn.com/problems/find-the-subtasks-that-did-not-execute)", "[\u5bfb\u627e\u6ca1\u6709\u88ab\u6267\u884c\u7684\u4efb\u52a1\u5bf9](/solution/1700-1799/1767.Find%20the%20Subtasks%20That%20Did%20Not%20Execute/README.md)", "", "\u56f0\u96be", "\ud83d\udd12"], "md_table_row_en": ["[1767](https://leetcode.com/problems/find-the-subtasks-that-did-not-execute)", "[Find the Subtasks That Did Not Execute](/solution/1700-1799/1767.Find%20the%20Subtasks%20That%20Did%20Not%20Execute/README_EN.md)", "", "Hard", "\ud83d\udd12"]}, {"question_id": "1913", "frontend_question_id": "1787", "paid_only": false, "paid_only_cn": false, "url_cn": "https://leetcode-cn.com/problems/make-the-xor-of-all-segments-equal-to-zero", "url_en": "https://leetcode.com/problems/make-the-xor-of-all-segments-equal-to-zero", "relative_path_cn": "/solution/1700-1799/1787.Make%20the%20XOR%20of%20All%20Segments%20Equal%20to%20Zero/README.md", "relative_path_en": "/solution/1700-1799/1787.Make%20the%20XOR%20of%20All%20Segments%20Equal%20to%20Zero/README_EN.md", "title_cn": "\u4f7f\u6240\u6709\u533a\u95f4\u7684\u5f02\u6216\u7ed3\u679c\u4e3a\u96f6", "title_en": "Make the XOR of All Segments Equal to Zero", "question_title_slug": "make-the-xor-of-all-segments-equal-to-zero", "content_en": "
You are given an array nums
\u200b\u200b\u200b and an integer k
\u200b\u200b\u200b\u200b\u200b. The XOR of a segment [left, right]
where left <= right
is the XOR
of all the elements with indices between left
and right
, inclusive: nums[left] XOR nums[left+1] XOR ... XOR nums[right]
.
Return the minimum number of elements to change in the array such that the XOR
of all segments of size k
\u200b\u200b\u200b\u200b\u200b\u200b is equal to zero.
\n
Example 1:
\n\n\nInput: nums = [1,2,0,3,0], k = 1\nOutput: 3\nExplanation: Modify the array from [1,2,0,3,0] to from [0,0,0,0,0].\n\n\n
Example 2:
\n\n\nInput: nums = [3,4,5,2,1,7,3,4,7], k = 3\nOutput: 3\nExplanation: Modify the array from [3,4,5,2,1,7,3,4,7] to [3,4,7,3,4,7,3,4,7].\n\n\n
Example 3:
\n\n\nInput: nums = [1,2,4,1,2,5,1,2,6], k = 3\nOutput: 3\nExplanation: Modify the array from [1,2,4,1,2,5,1,2,6] to [1,2,3,1,2,3,1,2,3].\n\n
\n
Constraints:
\n\n1 <= k <= nums.length <= 2000
\u200b\u200b\u200b\u200b\u200b\u200b0 <= nums[i] < 210
\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 nums
\u200b\u200b\u200b \u548c\u4e00\u4e2a\u6574\u6570 k
\u200b\u200b\u200b\u200b\u200b \u3002\u533a\u95f4 [left, right]
\uff08left <= right
\uff09\u7684 \u5f02\u6216\u7ed3\u679c \u662f\u5bf9\u4e0b\u6807\u4f4d\u4e8e\u00a0left
\u548c right
\uff08\u5305\u62ec left
\u548c right
\uff09\u4e4b\u95f4\u6240\u6709\u5143\u7d20\u8fdb\u884c XOR
\u8fd0\u7b97\u7684\u7ed3\u679c\uff1anums[left] XOR nums[left+1] XOR ... XOR nums[right]
\u3002
\u8fd4\u56de\u6570\u7ec4\u4e2d \u8981\u66f4\u6539\u7684\u6700\u5c0f\u5143\u7d20\u6570 \uff0c\u4ee5\u4f7f\u6240\u6709\u957f\u5ea6\u4e3a k
\u7684\u533a\u95f4\u5f02\u6216\u7ed3\u679c\u7b49\u4e8e\u96f6\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1anums = [1,2,0,3,0], k = 1\n\u8f93\u51fa\uff1a3\n\u89e3\u91ca\uff1a\u5c06\u6570\u7ec4 [1,2,0,3,0] \u4fee\u6539\u4e3a [0,0,0,0,0]\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1anums = [3,4,5,2,1,7,3,4,7], k = 3\n\u8f93\u51fa\uff1a3\n\u89e3\u91ca\uff1a\u5c06\u6570\u7ec4 [3,4,5,2,1,7,3,4,7] \u4fee\u6539\u4e3a [3,4,7,3,4,7,3,4,7]\n\n\n
\u793a\u4f8b 3\uff1a
\n\n\n\u8f93\u5165\uff1anums = [1,2,4,1,2,5,1,2,6], k = 3\n\u8f93\u51fa\uff1a3\n\u89e3\u91ca\uff1a\u5c06\u6570\u7ec4[1,2,4,1,2,5,1,2,6] \u4fee\u6539\u4e3a [1,2,3,1,2,3,1,2,3]\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= k <= nums.length <= 2000
\u200b\u200b\u200b\u200b\u200b\u200b0 <= nums[i] < 210
There is an undirected weighted connected graph. You are given a positive integer n
which denotes that the graph has n
nodes labeled from 1
to n
, and an array edges
where each edges[i] = [ui, vi, weighti]
denotes that there is an edge between nodes ui
and vi
with weight equal to weighti
.
A path from node start
to node end
is a sequence of nodes [z0, z1, z2, ..., zk]
such that z0 = start
and zk = end
and there is an edge between zi
and zi+1
where 0 <= i <= k-1
.
The distance of a path is the sum of the weights on the edges of the path. Let distanceToLastNode(x)
denote the shortest distance of a path between node n
and node x
. A restricted path is a path that also satisfies that distanceToLastNode(zi) > distanceToLastNode(zi+1)
where 0 <= i <= k-1
.
Return the number of restricted paths from node 1
to node n
. Since that number may be too large, return it modulo 109 + 7
.
\n
Example 1:
\n\nInput: n = 5, edges = [[1,2,3],[1,3,3],[2,3,1],[1,4,2],[5,2,2],[3,5,1],[5,4,10]]\nOutput: 3\nExplanation: Each circle contains the node number in black and its distanceToLastNode value in blue.
The three restricted paths are:\n1) 1 --> 2 --> 5\n2) 1 --> 2 --> 3 --> 5\n3) 1 --> 3 --> 5\n
\n\nExample 2:
\n\nInput: n = 7, edges = [[1,3,1],[4,1,2],[7,3,4],[2,5,3],[5,6,1],[6,7,2],[7,5,3],[2,6,4]]\nOutput: 1\nExplanation: Each circle contains the node number in black and its distanceToLastNode value in blue.
The only restricted path is 1 --> 3 --> 7.\n
\n\n\n
Constraints:
\n\n1 <= n <= 2 * 104
n - 1 <= edges.length <= 4 * 104
edges[i].length == 3
1 <= ui, vi <= n
ui != vi
1 <= weighti <= 105
\u73b0\u6709\u4e00\u4e2a\u52a0\u6743\u65e0\u5411\u8fde\u901a\u56fe\u3002\u7ed9\u4f60\u4e00\u4e2a\u6b63\u6574\u6570 n
\uff0c\u8868\u793a\u56fe\u4e2d\u6709 n
\u4e2a\u8282\u70b9\uff0c\u5e76\u6309\u4ece 1
\u5230 n
\u7ed9\u8282\u70b9\u7f16\u53f7\uff1b\u53e6\u7ed9\u4f60\u4e00\u4e2a\u6570\u7ec4 edges
\uff0c\u5176\u4e2d\u6bcf\u4e2a edges[i] = [ui, vi, weighti]
\u8868\u793a\u5b58\u5728\u4e00\u6761\u4f4d\u4e8e\u8282\u70b9 ui
\u548c vi
\u4e4b\u95f4\u7684\u8fb9\uff0c\u8fd9\u6761\u8fb9\u7684\u6743\u91cd\u4e3a weighti
\u3002
\u4ece\u8282\u70b9 start
\u51fa\u53d1\u5230\u8282\u70b9 end
\u7684\u8def\u5f84\u662f\u4e00\u4e2a\u5f62\u5982 [z0, z1, z2, ..., zk]
\u7684\u8282\u70b9\u5e8f\u5217\uff0c\u6ee1\u8db3 z0 = start
\u3001zk = end
\u4e14\u5728\u6240\u6709\u7b26\u5408 0 <= i <= k-1
\u7684\u8282\u70b9 zi
\u548c zi+1
\u4e4b\u95f4\u5b58\u5728\u4e00\u6761\u8fb9\u3002
\u8def\u5f84\u7684\u8ddd\u79bb\u5b9a\u4e49\u4e3a\u8fd9\u6761\u8def\u5f84\u4e0a\u6240\u6709\u8fb9\u7684\u6743\u91cd\u603b\u548c\u3002\u7528 distanceToLastNode(x)
\u8868\u793a\u8282\u70b9 n
\u548c x
\u4e4b\u95f4\u8def\u5f84\u7684\u6700\u77ed\u8ddd\u79bb\u3002\u53d7\u9650\u8def\u5f84 \u4e3a\u6ee1\u8db3 distanceToLastNode(zi) > distanceToLastNode(zi+1)
\u7684\u4e00\u6761\u8def\u5f84\uff0c\u5176\u4e2d 0 <= i <= k-1
\u3002
\u8fd4\u56de\u4ece\u8282\u70b9 1
\u51fa\u53d1\u5230\u8282\u70b9 n
\u7684 \u53d7\u9650\u8def\u5f84\u6570 \u3002\u7531\u4e8e\u6570\u5b57\u53ef\u80fd\u5f88\u5927\uff0c\u8bf7\u8fd4\u56de\u5bf9 109 + 7
\u53d6\u4f59 \u7684\u7ed3\u679c\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1an = 5, edges = [[1,2,3],[1,3,3],[2,3,1],[1,4,2],[5,2,2],[3,5,1],[5,4,10]]\n\u8f93\u51fa\uff1a3\n\u89e3\u91ca\uff1a\u6bcf\u4e2a\u5706\u5305\u542b\u9ed1\u8272\u7684\u8282\u70b9\u7f16\u53f7\u548c\u84dd\u8272\u7684 distanceToLastNode \u503c\u3002\u4e09\u6761\u53d7\u9650\u8def\u5f84\u5206\u522b\u662f\uff1a\n1) 1 --> 2 --> 5\n2) 1 --> 2 --> 3 --> 5\n3) 1 --> 3 --> 5\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1an = 7, edges = [[1,3,1],[4,1,2],[7,3,4],[2,5,3],[5,6,1],[6,7,2],[7,5,3],[2,6,4]]\n\u8f93\u51fa\uff1a1\n\u89e3\u91ca\uff1a\u6bcf\u4e2a\u5706\u5305\u542b\u9ed1\u8272\u7684\u8282\u70b9\u7f16\u53f7\u548c\u84dd\u8272\u7684 distanceToLastNode \u503c\u3002\u552f\u4e00\u4e00\u6761\u53d7\u9650\u8def\u5f84\u662f\uff1a1 --> 3 --> 7 \u3002\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= n <= 2 * 104
n - 1 <= edges.length <= 4 * 104
edges[i].length == 3
1 <= ui, vi <= n
ui != vi
1 <= weighti <= 105
You are given an integer array nums
and two integers limit
and goal
. The array nums
has an interesting property that abs(nums[i]) <= limit
.
Return the minimum number of elements you need to add to make the sum of the array equal to goal
. The array must maintain its property that abs(nums[i]) <= limit
.
Note that abs(x)
equals x
if x >= 0
, and -x
otherwise.
\n
Example 1:
\n\n\nInput: nums = [1,-1,1], limit = 3, goal = -4\nOutput: 2\nExplanation: You can add -2 and -3, then the sum of the array will be 1 - 1 + 1 - 2 - 3 = -4.\n\n\n
Example 2:
\n\n\nInput: nums = [1,-10,9,1], limit = 100, goal = 0\nOutput: 1\n\n\n
\n
Constraints:
\n\n1 <= nums.length <= 105
1 <= limit <= 106
-limit <= nums[i] <= limit
-109 <= goal <= 109
\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 nums
\uff0c\u548c\u4e24\u4e2a\u6574\u6570 limit
\u4e0e goal
\u3002\u6570\u7ec4 nums
\u6709\u4e00\u6761\u91cd\u8981\u5c5e\u6027\uff1aabs(nums[i]) <= limit
\u3002
\u8fd4\u56de\u4f7f\u6570\u7ec4\u5143\u7d20\u603b\u548c\u7b49\u4e8e goal
\u6240\u9700\u8981\u5411\u6570\u7ec4\u4e2d\u6dfb\u52a0\u7684 \u6700\u5c11\u5143\u7d20\u6570\u91cf \uff0c\u6dfb\u52a0\u5143\u7d20 \u4e0d\u5e94\u6539\u53d8 \u6570\u7ec4\u4e2d abs(nums[i]) <= limit
\u8fd9\u4e00\u5c5e\u6027\u3002
\u6ce8\u610f\uff0c\u5982\u679c x >= 0
\uff0c\u90a3\u4e48 abs(x)
\u7b49\u4e8e x
\uff1b\u5426\u5219\uff0c\u7b49\u4e8e -x
\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1anums = [1,-1,1], limit = 3, goal = -4\n\u8f93\u51fa\uff1a2\n\u89e3\u91ca\uff1a\u53ef\u4ee5\u5c06 -2 \u548c -3 \u6dfb\u52a0\u5230\u6570\u7ec4\u4e2d\uff0c\u6570\u7ec4\u7684\u5143\u7d20\u603b\u548c\u53d8\u4e3a 1 - 1 + 1 - 2 - 3 = -4 \u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1anums = [1,-10,9,1], limit = 100, goal = 0\n\u8f93\u51fa\uff1a1\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= nums.length <= 105
1 <= limit <= 106
-limit <= nums[i] <= limit
-109 <= goal <= 109
Given a binary string s
\u200b\u200b\u200b\u200b\u200bwithout leading zeros, return true
\u200b\u200b\u200b if s
contains at most one contiguous segment of ones. Otherwise, return false
.
\n
Example 1:
\n\n\nInput: s = "1001"\nOutput: false\nExplanation: The ones do not form a contiguous segment.\n\n\n
Example 2:
\n\n\nInput: s = "110"\nOutput: true\n\n
\n
Constraints:
\n\n1 <= s.length <= 100
s[i]
\u200b\u200b\u200b\u200b is either '0'
or '1'
.s[0]
is '1'
.\u7ed9\u4f60\u4e00\u4e2a\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32 s
\uff0c\u8be5\u5b57\u7b26\u4e32 \u4e0d\u542b\u524d\u5bfc\u96f6 \u3002
\u5982\u679c s
\u6700\u591a\u5305\u542b \u4e00\u4e2a\u7531\u8fde\u7eed\u7684 '1'
\u7ec4\u6210\u7684\u5b57\u6bb5 \uff0c\u8fd4\u56de true
\u200b\u200b\u200b \u3002\u5426\u5219\uff0c\u8fd4\u56de false
\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1as = \"1001\"\n\u8f93\u51fa\uff1afalse\n\u89e3\u91ca\uff1a\u5b57\u7b26\u4e32\u4e2d\u7684 1 \u6ca1\u6709\u5f62\u6210\u4e00\u4e2a\u8fde\u7eed\u5b57\u6bb5\u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1as = \"110\"\n\u8f93\u51fa\uff1atrue\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= s.length <= 100
s[i]
\u200b\u200b\u200b\u200b \u4e3a '0'
\u6216 '1'
s[0]
\u4e3a '1'
There are n
buildings in a line. You are given an integer array heights
of size n
that represents the heights of the buildings in the line.
The ocean is to the right of the buildings. A building has an ocean view if the building can see the ocean without obstructions. Formally, a building has an ocean view if all the buildings to its right have a smaller height.
\r\n\r\nReturn a list of indices (0-indexed) of buildings that have an ocean view, sorted in increasing order.
\r\n\r\n\r\n
Example 1:
\r\n\r\n\r\nInput: heights = [4,2,3,1]\r\nOutput: [0,2,3]\r\nExplanation: Building 1 (0-indexed) does not have an ocean view because building 2 is taller.\r\n\r\n\r\n
Example 2:
\r\n\r\n\r\nInput: heights = [4,3,2,1]\r\nOutput: [0,1,2,3]\r\nExplanation: All the buildings have an ocean view.\r\n\r\n
Example 3:
\r\n\r\n\r\nInput: heights = [1,3,2,4]\r\nOutput: [3]\r\nExplanation: Only building 3 has an ocean view.\r\n\r\n
Example 4:
\r\n\r\n\r\nInput: heights = [2,2,2,2]\r\nOutput: [3]\r\nExplanation: Buildings cannot see the ocean if there are buildings of the same height to its right.\r\n\r\n
\r\n
Constraints:
\r\n\r\n1 <= heights.length <= 105
1 <= heights[i] <= 109
\u6709 n
\u5ea7\u5efa\u7b51\u7269\u3002\u7ed9\u4f60\u4e00\u4e2a\u5927\u5c0f\u4e3a n
\u7684\u6574\u6570\u6570\u7ec4 heights
\u8868\u793a\u6bcf\u4e00\u4e2a\u5efa\u7b51\u7269\u7684\u9ad8\u5ea6\u3002
\u5efa\u7b51\u7269\u7684\u53f3\u8fb9\u662f\u6d77\u6d0b\u3002\u5982\u679c\u5efa\u7b51\u7269\u53ef\u4ee5\u65e0\u969c\u788d\u5730\u770b\u5230\u6d77\u6d0b\uff0c\u5219\u5efa\u7b51\u7269\u80fd\u770b\u5230\u6d77\u666f\u3002\u786e\u5207\u5730\u8bf4\uff0c\u5982\u679c\u4e00\u5ea7\u5efa\u7b51\u7269\u53f3\u8fb9\u7684\u6240\u6709\u5efa\u7b51\u90fd\u6bd4\u5b83 \u77ee \u65f6\uff0c\u5c31\u8ba4\u4e3a\u5b83\u80fd\u770b\u5230\u6d77\u666f\u3002
\n\n\u8fd4\u56de\u80fd\u770b\u5230\u6d77\u666f\u5efa\u7b51\u7269\u7684\u4e0b\u6807\u5217\u8868\uff08\u4e0b\u6807 \u4ece 0
\u5f00\u59cb \uff09\uff0c\u5e76\u6309\u5347\u5e8f\u6392\u5217\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1aheights = [4,2,3,1]\n\u8f93\u51fa\uff1a[0,2,3]\n\u89e3\u91ca\uff1a1 \u53f7\u5efa\u7b51\u7269\u770b\u4e0d\u5230\u6d77\u666f\uff0c\u56e0\u4e3a 2 \u53f7\u5efa\u7b51\u7269\u6bd4\u5b83\u9ad8\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1aheights = [4,3,2,1]\n\u8f93\u51fa\uff1a[0,1,2,3]\n\u89e3\u91ca\uff1a\u6240\u6709\u7684\u5efa\u7b51\u7269\u90fd\u80fd\u770b\u5230\u6d77\u666f\u3002\n\n
\u793a\u4f8b 3\uff1a
\n\n\n\u8f93\u5165\uff1aheights = [1,3,2,4]\n\u8f93\u51fa\uff1a[3]\n\u89e3\u91ca\uff1a\u53ea\u6709 3 \u53f7\u5efa\u7b51\u7269\u80fd\u770b\u5230\u6d77\u666f\u3002\n\n
\u793a\u4f8b 4\uff1a
\n\n\n\u8f93\u5165\uff1aheights = [2,2,2,2]\n\u8f93\u51fa\uff1a[3]\n\u89e3\u91ca\uff1a\u5982\u679c\u5efa\u7b51\u7269\u53f3\u8fb9\u6709\u76f8\u540c\u9ad8\u5ea6\u7684\u5efa\u7b51\u7269\u5219\u65e0\u6cd5\u770b\u5230\u6d77\u666f\u3002\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= heights.length <= 105
1 <= heights[i] <= 109
Table: Products
\n+-------------+---------+\n| Column Name | Type |\n+-------------+---------+\n| product_id | int |\n| low_fats | enum |\n| recyclable | enum |\n+-------------+---------+\nproduct_id is the primary key for this table.\nlow_fats is an ENUM of type ('Y', 'N') where 'Y' means this product is low fat and 'N' means it is not.\nrecyclable is an ENUM of types ('Y', 'N') where 'Y' means this product is recyclable and 'N' means it is not.\n\n
\n\n
Write an SQL query to find the ids of products that are both low fat and recyclable.
\n\nReturn the result table in any order.
\n\nThe query result format is in the following example:
\n\n\n\n
\nProducts table:\n+-------------+----------+------------+\n| product_id | low_fats | recyclable |\n+-------------+----------+------------+\n| 0 | Y | N |\n| 1 | Y | Y |\n| 2 | N | Y |\n| 3 | Y | Y |\n| 4 | N | N |\n+-------------+----------+------------+\nResult table:\n+-------------+\n| product_id |\n+-------------+\n| 1 |\n| 3 |\n+-------------+\nOnly products 1 and 3 are both low fat and recyclable.\n\n", "content_cn": "
\u8868\uff1aProducts
\n+-------------+---------+\n| Column Name | Type |\n+-------------+---------+\n| product_id | int |\n| low_fats | enum |\n| recyclable | enum |\n+-------------+---------+\nproduct_id \u662f\u8fd9\u4e2a\u8868\u7684\u4e3b\u952e\u3002\nlow_fats \u662f\u679a\u4e3e\u7c7b\u578b\uff0c\u53d6\u503c\u4e3a\u4ee5\u4e0b\u4e24\u79cd ('Y', 'N')\uff0c\u5176\u4e2d 'Y' \u8868\u793a\u8be5\u4ea7\u54c1\u662f\u4f4e\u8102\u4ea7\u54c1\uff0c'N' \u8868\u793a\u4e0d\u662f\u4f4e\u8102\u4ea7\u54c1\u3002\nrecyclable \u662f\u679a\u4e3e\u7c7b\u578b\uff0c\u53d6\u503c\u4e3a\u4ee5\u4e0b\u4e24\u79cd ('Y', 'N')\uff0c\u5176\u4e2d 'Y' \u8868\u793a\u8be5\u4ea7\u54c1\u53ef\u56de\u6536\uff0c\u800c 'N' \u8868\u793a\u4e0d\u53ef\u56de\u6536\u3002\n\n
\u00a0
\n\n\u5199\u51fa SQL \u8bed\u53e5\uff0c\u67e5\u627e\u65e2\u662f\u4f4e\u8102\u53c8\u662f\u53ef\u56de\u6536\u7684\u4ea7\u54c1\u7f16\u53f7\u3002
\n\n\u8fd4\u56de\u7ed3\u679c \u65e0\u987a\u5e8f\u8981\u6c42 \u3002
\n\n\u67e5\u8be2\u7ed3\u679c\u683c\u5f0f\u5982\u4e0b\u4f8b\u6240\u793a\uff1a
\n\n\nProducts \u8868\uff1a\n+-------------+----------+------------+\n| product_id | low_fats | recyclable |\n+-------------+----------+------------+\n| 0 | Y | N |\n| 1 | Y | Y |\n| 2 | N | Y |\n| 3 | Y | Y |\n| 4 | N | N |\n+-------------+----------+------------+\nResult \u8868\uff1a\n+-------------+\n| product_id |\n+-------------+\n| 1 |\n| 3 |\n+-------------+\n\u53ea\u6709\u4ea7\u54c1 id \u4e3a 1 \u548c 3 \u7684\u4ea7\u54c1\uff0c\u65e2\u662f\u4f4e\u8102\u53c8\u662f\u53ef\u56de\u6536\u7684\u4ea7\u54c1\u3002\n\n", "tags_en": [], "tags_cn": [], "difficulty_en": "Easy", "difficulty_cn": "\u7b80\u5355", "code_snippets": [{"lang": "MySQL", "langSlug": "mysql", "code": "# Write your MySQL query statement below", "__typename": "CodeSnippetNode"}, {"lang": "MS SQL Server", "langSlug": "mssql", "code": "/* Write your T-SQL query statement below */", "__typename": "CodeSnippetNode"}, {"lang": "Oracle", "langSlug": "oraclesql", "code": "/* Write your PL/SQL query statement below */", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1757](https://leetcode-cn.com/problems/recyclable-and-low-fat-products)", "[\u53ef\u56de\u6536\u4e14\u4f4e\u8102\u7684\u4ea7\u54c1](/solution/1700-1799/1757.Recyclable%20and%20Low%20Fat%20Products/README.md)", "", "\u7b80\u5355", "\ud83d\udd12"], "md_table_row_en": ["[1757](https://leetcode.com/problems/recyclable-and-low-fat-products)", "[Recyclable and Low Fat Products](/solution/1700-1799/1757.Recyclable%20and%20Low%20Fat%20Products/README_EN.md)", "", "Easy", "\ud83d\udd12"]}, {"question_id": "1907", "frontend_question_id": "1803", "paid_only": false, "paid_only_cn": false, "url_cn": "https://leetcode-cn.com/problems/count-pairs-with-xor-in-a-range", "url_en": "https://leetcode.com/problems/count-pairs-with-xor-in-a-range", "relative_path_cn": "/solution/1800-1899/1803.Count%20Pairs%20With%20XOR%20in%20a%20Range/README.md", "relative_path_en": "/solution/1800-1899/1803.Count%20Pairs%20With%20XOR%20in%20a%20Range/README_EN.md", "title_cn": "\u7edf\u8ba1\u5f02\u6216\u503c\u5728\u8303\u56f4\u5185\u7684\u6570\u5bf9\u6709\u591a\u5c11", "title_en": "Count Pairs With XOR in a Range", "question_title_slug": "count-pairs-with-xor-in-a-range", "content_en": "
Given a (0-indexed) integer array nums
and two integers low
and high
, return the number of nice pairs.
A nice pair is a pair (i, j)
where 0 <= i < j < nums.length
and low <= (nums[i] XOR nums[j]) <= high
.
\r\n
Example 1:
\r\n\r\n\r\nInput: nums = [1,4,2,7], low = 2, high = 6\r\nOutput: 6\r\nExplanation: All nice pairs (i, j) are as follows:\r\n - (0, 1): nums[0] XOR nums[1] = 5 \r\n - (0, 2): nums[0] XOR nums[2] = 3\r\n - (0, 3): nums[0] XOR nums[3] = 6\r\n - (1, 2): nums[1] XOR nums[2] = 6\r\n - (1, 3): nums[1] XOR nums[3] = 3\r\n - (2, 3): nums[2] XOR nums[3] = 5\r\n\r\n\r\n
Example 2:
\r\n\r\n\r\nInput: nums = [9,8,4,2,1], low = 5, high = 14\r\nOutput: 8\r\nExplanation: All nice pairs (i, j) are as follows:\r\n\u200b\u200b\u200b\u200b\u200b - (0, 2): nums[0] XOR nums[2] = 13\r\n - (0, 3): nums[0] XOR nums[3] = 11\r\n - (0, 4): nums[0] XOR nums[4] = 8\r\n - (1, 2): nums[1] XOR nums[2] = 12\r\n - (1, 3): nums[1] XOR nums[3] = 10\r\n - (1, 4): nums[1] XOR nums[4] = 9\r\n - (2, 3): nums[2] XOR nums[3] = 6\r\n - (2, 4): nums[2] XOR nums[4] = 5\r\n\r\n
\r\n
Constraints:
\r\n\r\n1 <= nums.length <= 2 * 104
1 <= nums[i] <= 2 * 104
1 <= low <= high <= 2 * 104
\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 nums
\uff08\u4e0b\u6807 \u4ece 0 \u5f00\u59cb \u8ba1\u6570\uff09\u4ee5\u53ca\u4e24\u4e2a\u6574\u6570\uff1alow
\u548c high
\uff0c\u8bf7\u8fd4\u56de \u6f02\u4eae\u6570\u5bf9 \u7684\u6570\u76ee\u3002
\u6f02\u4eae\u6570\u5bf9 \u662f\u4e00\u4e2a\u5f62\u5982 (i, j)
\u7684\u6570\u5bf9\uff0c\u5176\u4e2d 0 <= i < j < nums.length
\u4e14 low <= (nums[i] XOR nums[j]) <= high
\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1anums = [1,4,2,7], low = 2, high = 6\n\u8f93\u51fa\uff1a6\n\u89e3\u91ca\uff1a\u6240\u6709\u6f02\u4eae\u6570\u5bf9 (i, j) \u5217\u51fa\u5982\u4e0b\uff1a\n - (0, 1): nums[0] XOR nums[1] = 5 \n - (0, 2): nums[0] XOR nums[2] = 3\n - (0, 3): nums[0] XOR nums[3] = 6\n - (1, 2): nums[1] XOR nums[2] = 6\n - (1, 3): nums[1] XOR nums[3] = 3\n - (2, 3): nums[2] XOR nums[3] = 5\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1anums = [9,8,4,2,1], low = 5, high = 14\n\u8f93\u51fa\uff1a8\n\u89e3\u91ca\uff1a\u6240\u6709\u6f02\u4eae\u6570\u5bf9 (i, j) \u5217\u51fa\u5982\u4e0b\uff1a\n\u200b\u200b\u200b\u200b\u200b - (0, 2): nums[0] XOR nums[2] = 13\n\u00a0 - (0, 3): nums[0] XOR nums[3] = 11\n\u00a0 - (0, 4): nums[0] XOR nums[4] = 8\n\u00a0 - (1, 2): nums[1] XOR nums[2] = 12\n\u00a0 - (1, 3): nums[1] XOR nums[3] = 10\n\u00a0 - (1, 4): nums[1] XOR nums[4] = 9\n\u00a0 - (2, 3): nums[2] XOR nums[3] = 6\n\u00a0 - (2, 4): nums[2] XOR nums[4] = 5\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= nums.length <= 2 * 104
1 <= nums[i] <= 2 * 104
1 <= low <= high <= 2 * 104
You are given nums
, an array of positive integers of size 2 * n
. You must perform n
operations on this array.
In the ith
operation (1-indexed), you will:
x
and y
.i * gcd(x, y)
.x
and y
from nums
.Return the maximum score you can receive after performing n
operations.
The function gcd(x, y)
is the greatest common divisor of x
and y
.
\n
Example 1:
\n\n\nInput: nums = [1,2]\nOutput: 1\nExplanation: The optimal choice of operations is:\n(1 * gcd(1, 2)) = 1\n\n\n
Example 2:
\n\n\nInput: nums = [3,4,6,8]\nOutput: 11\nExplanation: The optimal choice of operations is:\n(1 * gcd(3, 6)) + (2 * gcd(4, 8)) = 3 + 8 = 11\n\n\n
Example 3:
\n\n\nInput: nums = [1,2,3,4,5,6]\nOutput: 14\nExplanation: The optimal choice of operations is:\n(1 * gcd(1, 5)) + (2 * gcd(2, 4)) + (3 * gcd(3, 6)) = 1 + 4 + 9 = 14\n\n\n
\n
Constraints:
\n\n1 <= n <= 7
nums.length == 2 * n
1 <= nums[i] <= 106
\u7ed9\u4f60\u00a0nums
\u00a0\uff0c\u5b83\u662f\u4e00\u4e2a\u5927\u5c0f\u4e3a\u00a02 * n
\u00a0\u7684\u6b63\u6574\u6570\u6570\u7ec4\u3002\u4f60\u5fc5\u987b\u5bf9\u8fd9\u4e2a\u6570\u7ec4\u6267\u884c n
\u00a0\u6b21\u64cd\u4f5c\u3002
\u5728\u7b2c\u00a0i
\u00a0\u6b21\u64cd\u4f5c\u65f6\uff08\u64cd\u4f5c\u7f16\u53f7\u4ece 1\u00a0\u5f00\u59cb\uff09\uff0c\u4f60\u9700\u8981\uff1a
x
\u548c\u00a0y
\u00a0\u3002i * gcd(x, y)
\u00a0\u3002x
\u00a0\u548c\u00a0y
\u4ece\u00a0nums
\u00a0\u4e2d\u5220\u9664\u3002\u8bf7\u4f60\u8fd4\u56de n
\u00a0\u6b21\u64cd\u4f5c\u540e\u4f60\u80fd\u83b7\u5f97\u7684\u5206\u6570\u548c\u6700\u5927\u4e3a\u591a\u5c11\u3002
\u51fd\u6570\u00a0gcd(x, y)
\u00a0\u662f\u00a0x
\u548c\u00a0y
\u00a0\u7684\u6700\u5927\u516c\u7ea6\u6570\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1anums = [1,2]\n\u8f93\u51fa\uff1a1\n\u89e3\u91ca\uff1a\u6700\u4f18\u64cd\u4f5c\u662f\uff1a\n(1 * gcd(1, 2)) = 1\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1anums = [3,4,6,8]\n\u8f93\u51fa\uff1a11\n\u89e3\u91ca\uff1a\u6700\u4f18\u64cd\u4f5c\u662f\uff1a\n(1 * gcd(3, 6)) + (2 * gcd(4, 8)) = 3 + 8 = 11\n\n\n
\u793a\u4f8b 3\uff1a
\n\n\u8f93\u5165\uff1anums = [1,2,3,4,5,6]\n\u8f93\u51fa\uff1a14\n\u89e3\u91ca\uff1a\u6700\u4f18\u64cd\u4f5c\u662f\uff1a\n(1 * gcd(1, 5)) + (2 * gcd(2, 4)) + (3 * gcd(3, 6)) = 1 + 4 + 9 = 14\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= n <= 7
nums.length == 2 * n
1 <= nums[i] <= 106
There is an authentication system that works with authentication tokens. For each session, the user will receive a new authentication token that will expire timeToLive
seconds after the currentTime
. If the token is renewed, the expiry time will be extended to expire timeToLive
seconds after the (potentially different) currentTime
.
Implement the AuthenticationManager
class:
AuthenticationManager(int timeToLive)
constructs the AuthenticationManager
and sets the timeToLive
.generate(string tokenId, int currentTime)
generates a new token with the given tokenId
at the given currentTime
in seconds.renew(string tokenId, int currentTime)
renews the unexpired token with the given tokenId
at the given currentTime
in seconds. If there are no unexpired tokens with the given tokenId
, the request is ignored, and nothing happens.countUnexpiredTokens(int currentTime)
returns the number of unexpired tokens at the given currentTime.Note that if a token expires at time t
, and another action happens on time t
(renew
or countUnexpiredTokens
), the expiration takes place before the other actions.
\n
Example 1:
\n\nInput\n["AuthenticationManager", "\n\nrenew
", "generate", "countUnexpiredTokens
", "generate", "renew
", "renew
", "countUnexpiredTokens
"]\n[[5], ["aaa", 1], ["aaa", 2], [6], ["bbb", 7], ["aaa", 8], ["bbb", 10], [15]]\nOutput\n[null, null, null, 1, null, null, null, 0]\n\nExplanation\nAuthenticationManager authenticationManager = new AuthenticationManager(5); // Constructs the AuthenticationManager withtimeToLive
= 5 seconds.\nauthenticationManager.renew
("aaa", 1); // No token exists with tokenId "aaa" at time 1, so nothing happens.\nauthenticationManager.generate("aaa", 2); // Generates a new token with tokenId "aaa" at time 2.\nauthenticationManager.countUnexpiredTokens
(6); // The token with tokenId "aaa" is the only unexpired one at time 6, so return 1.\nauthenticationManager.generate("bbb", 7); // Generates a new token with tokenId "bbb" at time 7.\nauthenticationManager.renew
("aaa", 8); // The token with tokenId "aaa" expired at time 7, and 8 >= 7, so at time 8 therenew
request is ignored, and nothing happens.\nauthenticationManager.renew
("bbb", 10); // The token with tokenId "bbb" is unexpired at time 10, so therenew
request is fulfilled and now the token will expire at time 15.\nauthenticationManager.countUnexpiredTokens
(15); // The token with tokenId "bbb" expires at time 15, and the token with tokenId "aaa" expired at time 7, so currently no token is unexpired, so return 0.\n
\n
Constraints:
\n\n1 <= timeToLive <= 108
1 <= currentTime <= 108
1 <= tokenId.length <= 5
tokenId
consists only of lowercase letters.generate
will contain unique values of tokenId
.currentTime
across all the function calls will be strictly increasing.2000
calls will be made to all functions combined.\u4f60\u9700\u8981\u8bbe\u8ba1\u4e00\u4e2a\u5305\u542b\u9a8c\u8bc1\u7801\u7684\u9a8c\u8bc1\u7cfb\u7edf\u3002\u6bcf\u4e00\u6b21\u9a8c\u8bc1\u4e2d\uff0c\u7528\u6237\u4f1a\u6536\u5230\u4e00\u4e2a\u65b0\u7684\u9a8c\u8bc1\u7801\uff0c\u8fd9\u4e2a\u9a8c\u8bc1\u7801\u5728 currentTime
\u00a0\u65f6\u523b\u4e4b\u540e timeToLive
\u00a0\u79d2\u8fc7\u671f\u3002\u5982\u679c\u9a8c\u8bc1\u7801\u88ab\u66f4\u65b0\u4e86\uff0c\u90a3\u4e48\u5b83\u4f1a\u5728 currentTime
\u00a0\uff08\u53ef\u80fd\u4e0e\u4e4b\u524d\u7684 currentTime
\u00a0\u4e0d\u540c\uff09\u65f6\u523b\u5ef6\u957f\u00a0timeToLive
\u00a0\u79d2\u3002
\u8bf7\u4f60\u5b9e\u73b0\u00a0AuthenticationManager
\u00a0\u7c7b\uff1a
AuthenticationManager(int timeToLive)
\u00a0\u6784\u9020\u00a0AuthenticationManager
\u00a0\u5e76\u8bbe\u7f6e\u00a0timeToLive
\u00a0\u53c2\u6570\u3002generate(string tokenId, int currentTime)
\u00a0\u7ed9\u5b9a tokenId
\u00a0\uff0c\u5728\u5f53\u524d\u65f6\u95f4\u00a0currentTime
\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u9a8c\u8bc1\u7801\u3002renew(string tokenId, int currentTime)
\u00a0\u5c06\u7ed9\u5b9a tokenId
\u00a0\u4e14 \u672a\u8fc7\u671f\u00a0\u7684\u9a8c\u8bc1\u7801\u5728 currentTime
\u00a0\u65f6\u523b\u66f4\u65b0\u3002\u5982\u679c\u7ed9\u5b9a\u00a0tokenId
\u00a0\u5bf9\u5e94\u7684\u9a8c\u8bc1\u7801\u4e0d\u5b58\u5728\u6216\u5df2\u8fc7\u671f\uff0c\u8bf7\u4f60\u5ffd\u7565\u8be5\u64cd\u4f5c\uff0c\u4e0d\u4f1a\u6709\u4efb\u4f55\u66f4\u65b0\u64cd\u4f5c\u53d1\u751f\u3002countUnexpiredTokens(int currentTime)
\u00a0\u8bf7\u8fd4\u56de\u5728\u7ed9\u5b9a\u00a0currentTime
\u00a0\u65f6\u523b\uff0c\u672a\u8fc7\u671f\u00a0\u7684\u9a8c\u8bc1\u7801\u6570\u76ee\u3002\u5982\u679c\u4e00\u4e2a\u9a8c\u8bc1\u7801\u5728\u65f6\u523b\u00a0t
\u00a0\u8fc7\u671f\uff0c\u4e14\u53e6\u4e00\u4e2a\u64cd\u4f5c\u6070\u597d\u5728\u65f6\u523b\u00a0t
\u00a0\u53d1\u751f\uff08renew
\u00a0\u6216\u8005\u00a0countUnexpiredTokens
\u00a0\u64cd\u4f5c\uff09\uff0c\u8fc7\u671f\u4e8b\u4ef6\u00a0\u4f18\u5148\u4e8e\u00a0\u5176\u4ed6\u64cd\u4f5c\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1a\n[\"AuthenticationManager\", \"\n\nrenew
\", \"generate\", \"countUnexpiredTokens
\", \"generate\", \"renew
\", \"renew
\", \"countUnexpiredTokens
\"]\n[[5], [\"aaa\", 1], [\"aaa\", 2], [6], [\"bbb\", 7], [\"aaa\", 8], [\"bbb\", 10], [15]]\n\u8f93\u51fa\uff1a\n[null, null, null, 1, null, null, null, 0]\n\n\u89e3\u91ca\uff1a\nAuthenticationManager authenticationManager = new AuthenticationManager(5); // \u6784\u9020 AuthenticationManager \uff0c\u8bbe\u7f6etimeToLive
= 5 \u79d2\u3002\nauthenticationManager.renew
(\"aaa\", 1); // \u65f6\u523b 1 \u65f6\uff0c\u6ca1\u6709\u9a8c\u8bc1\u7801\u7684 tokenId \u4e3a \"aaa\" \uff0c\u6ca1\u6709\u9a8c\u8bc1\u7801\u88ab\u66f4\u65b0\u3002\nauthenticationManager.generate(\"aaa\", 2); // \u65f6\u523b 2 \u65f6\uff0c\u751f\u6210\u4e00\u4e2a tokenId \u4e3a \"aaa\" \u7684\u65b0\u9a8c\u8bc1\u7801\u3002\nauthenticationManager.countUnexpiredTokens
(6); // \u65f6\u523b 6 \u65f6\uff0c\u53ea\u6709 tokenId \u4e3a \"aaa\" \u7684\u9a8c\u8bc1\u7801\u672a\u8fc7\u671f\uff0c\u6240\u4ee5\u8fd4\u56de 1 \u3002\nauthenticationManager.generate(\"bbb\", 7); // \u65f6\u523b 7 \u65f6\uff0c\u751f\u6210\u4e00\u4e2a tokenId \u4e3a \"bbb\" \u7684\u65b0\u9a8c\u8bc1\u7801\u3002\nauthenticationManager.renew
(\"aaa\", 8); // tokenId \u4e3a \"aaa\" \u7684\u9a8c\u8bc1\u7801\u5728\u65f6\u523b 7 \u8fc7\u671f\uff0c\u4e14 8 >= 7 \uff0c\u6240\u4ee5\u65f6\u523b 8 \u7684renew \u64cd\u4f5c\u88ab\u5ffd\u7565\uff0c\u6ca1\u6709\u9a8c\u8bc1\u7801\u88ab\u66f4\u65b0\u3002\nauthenticationManager.renew
(\"bbb\", 10); // tokenId \u4e3a \"bbb\" \u7684\u9a8c\u8bc1\u7801\u5728\u65f6\u523b 10 \u6ca1\u6709\u8fc7\u671f\uff0c\u6240\u4ee5 renew \u64cd\u4f5c\u4f1a\u6267\u884c\uff0c\u8be5 token \u5c06\u5728\u65f6\u523b 15 \u8fc7\u671f\u3002\nauthenticationManager.countUnexpiredTokens
(15); // tokenId \u4e3a \"bbb\" \u7684\u9a8c\u8bc1\u7801\u5728\u65f6\u523b 15 \u8fc7\u671f\uff0ctokenId \u4e3a \"aaa\" \u7684\u9a8c\u8bc1\u7801\u5728\u65f6\u523b 7 \u8fc7\u671f\uff0c\u6240\u6709\u9a8c\u8bc1\u7801\u5747\u5df2\u8fc7\u671f\uff0c\u6240\u4ee5\u8fd4\u56de 0 \u3002\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= timeToLive <= 108
1 <= currentTime <= 108
1 <= tokenId.length <= 5
tokenId
\u00a0\u53ea\u5305\u542b\u5c0f\u5199\u82f1\u6587\u5b57\u6bcd\u3002generate
\u00a0\u51fd\u6570\u7684\u8c03\u7528\u90fd\u4f1a\u5305\u542b\u72ec\u4e00\u65e0\u4e8c\u7684\u00a0tokenId
\u00a0\u503c\u3002currentTime
\u00a0\u7684\u503c \u4e25\u683c\u9012\u589e\u00a0\u30022000
\u00a0\u6b21\u3002Given an alphanumeric string s
, return the second largest numerical digit that appears in s
, or -1
if it does not exist.
An alphanumeric string is a string consisting of lowercase English letters and digits.
\n\n\n
Example 1:
\n\n\nInput: s = "dfa12321afd"\nOutput: 2\nExplanation: The digits that appear in s are [1, 2, 3]. The second largest digit is 2.\n\n\n
Example 2:
\n\n\nInput: s = "abc1111"\nOutput: -1\nExplanation: The digits that appear in s are [1]. There is no second largest digit. \n\n\n
\n
Constraints:
\n\n1 <= s.length <= 500
s
consists of only lowercase English letters and/or digits.\u7ed9\u4f60\u4e00\u4e2a\u6df7\u5408\u5b57\u7b26\u4e32\u00a0s
\u00a0\uff0c\u8bf7\u4f60\u8fd4\u56de s
\u00a0\u4e2d \u7b2c\u4e8c\u5927 \u7684\u6570\u5b57\uff0c\u5982\u679c\u4e0d\u5b58\u5728\u7b2c\u4e8c\u5927\u7684\u6570\u5b57\uff0c\u8bf7\u4f60\u8fd4\u56de -1
\u00a0\u3002
\u6df7\u5408\u5b57\u7b26\u4e32 \u7531\u5c0f\u5199\u82f1\u6587\u5b57\u6bcd\u548c\u6570\u5b57\u7ec4\u6210\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1as = \"dfa12321afd\"\n\u8f93\u51fa\uff1a2\n\u89e3\u91ca\uff1a\u51fa\u73b0\u5728 s \u4e2d\u7684\u6570\u5b57\u5305\u62ec [1, 2, 3] \u3002\u7b2c\u4e8c\u5927\u7684\u6570\u5b57\u662f 2 \u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1as = \"abc1111\"\n\u8f93\u51fa\uff1a-1\n\u89e3\u91ca\uff1a\u51fa\u73b0\u5728 s \u4e2d\u7684\u6570\u5b57\u53ea\u5305\u542b [1] \u3002\u6ca1\u6709\u7b2c\u4e8c\u5927\u7684\u6570\u5b57\u3002\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= s.length <= 500
s
\u00a0\u53ea\u5305\u542b\u5c0f\u5199\u82f1\u6587\u5b57\u6bcd\u548c\uff08\u6216\uff09\u6570\u5b57\u3002Design a queue-like data structure that moves the most recently used element to the end of the queue.
\n\nImplement the MRUQueue
class:
MRUQueue(int n)
constructs the MRUQueue
with n
elements: [1,2,3,...,n]
.int fetch(int k)
moves the kth
element (1-indexed) to the end of the queue and returns it.\n
Example 1:
\n\n\nInput:\n["MRUQueue", "fetch", "fetch", "fetch", "fetch"]\n[[8], [3], [5], [2], [8]]\nOutput:\n[null, 3, 6, 2, 2]\n\nExplanation:\nMRUQueue mRUQueue = new MRUQueue(8); // Initializes the queue to [1,2,3,4,5,6,7,8].\nmRUQueue.fetch(3); // Moves the 3rd element (3) to the end of the queue to become [1,2,4,5,6,7,8,3] and returns it.\nmRUQueue.fetch(5); // Moves the 5th element (6) to the end of the queue to become [1,2,4,5,7,8,3,6] and returns it.\nmRUQueue.fetch(2); // Moves the 2nd element (2) to the end of the queue to become [1,4,5,7,8,3,6,2] and returns it.\nmRUQueue.fetch(8); // The 8th element (2) is already at the end of the queue so just return it.\n\n\n
\n
Constraints:
\n\n1 <= n <= 2000
1 <= k <= n
2000
calls will be made to fetch
.\nFollow up: Finding an
O(n)
algorithm per fetch
is a bit easy. Can you find an algorithm with a better complexity for each fetch
call?", "content_cn": "\u8bbe\u8ba1\u4e00\u79cd\u7c7b\u4f3c\u961f\u5217\u7684\u6570\u636e\u7ed3\u6784\uff0c\u8be5\u6570\u636e\u7ed3\u6784\u5c06\u6700\u8fd1\u4f7f\u7528\u7684\u5143\u7d20\u79fb\u5230\u961f\u5217\u5c3e\u90e8\u3002
\n\n\u5b9e\u73b0\u00a0MRUQueue
\u00a0\u7c7b\uff1a
MRUQueue(int n)
\u00a0\u00a0\u4f7f\u7528\u00a0n
\u00a0\u4e2a\u5143\u7d20\uff1a\u00a0[1,2,3,...,n]
\u00a0\u6784\u9020\u00a0MRUQueue
\u00a0\u3002fetch(int k)
\u00a0\u5c06\u7b2c\u00a0k
\u00a0\u4e2a\u5143\u7d20\uff08\u4ece 1 \u5f00\u59cb\u7d22\u5f15\uff09\u79fb\u5230\u961f\u5c3e\uff0c\u5e76\u8fd4\u56de\u8be5\u5143\u7d20\u3002\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1a\n[\"MRUQueue\", \"fetch\", \"fetch\", \"fetch\", \"fetch\"]\n[[8], [3], [5], [2], [8]]\n\u8f93\u51fa\uff1a\n[null, 3, 6, 2, 2]\n\n\u89e3\u91ca\uff1a\nMRUQueue mRUQueue = new MRUQueue(8); // \u521d\u59cb\u5316\u961f\u5217\u4e3a [1,2,3,4,5,6,7,8]\u3002\nmRUQueue.fetch(3); // \u5c06\u7b2c 3 \u4e2a\u5143\u7d20 (3) \u79fb\u5230\u961f\u5c3e\uff0c\u4f7f\u961f\u5217\u53d8\u4e3a [1,2,4,5,6,7,8,3] \u5e76\u8fd4\u56de\u8be5\u5143\u7d20\u3002\nmRUQueue.fetch(5); // \u5c06\u7b2c 5 \u4e2a\u5143\u7d20 (6) \u79fb\u5230\u961f\u5c3e\uff0c\u4f7f\u961f\u5217\u53d8\u4e3a [1,2,4,5,7,8,3,6] \u5e76\u8fd4\u56de\u8be5\u5143\u7d20\u3002\nmRUQueue.fetch(2); // \u5c06\u7b2c 2 \u4e2a\u5143\u7d20 (2) \u79fb\u5230\u961f\u5c3e\uff0c\u4f7f\u961f\u5217\u53d8\u4e3a [1,4,5,7,8,3,6,2] \u5e76\u8fd4\u56de\u8be5\u5143\u7d20\u3002\nmRUQueue.fetch(8); // \u7b2c 8 \u4e2a\u5143\u7d20 (2) \u5df2\u7ecf\u5728\u961f\u5217\u5c3e\u90e8\u4e86\uff0c\u6240\u4ee5\u76f4\u63a5\u8fd4\u56de\u8be5\u5143\u7d20\u5373\u53ef\u3002\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= n <= 2000
1 <= k <= n
2000
\u00a0\u6b21\u00a0fetch
\u00a0
\n\u8fdb\u9636\uff1a\u627e\u5230\u6bcf\u6b21\u00a0fetch
\u00a0\u7684\u590d\u6742\u5ea6\u4e3a\u00a0O(n)
\u00a0\u7684\u7b97\u6cd5\u6bd4\u8f83\u7b80\u5355\u3002\u4f60\u53ef\u4ee5\u627e\u5230\u6bcf\u6b21\u00a0fetch
\u00a0\u7684\u590d\u6742\u5ea6\u66f4\u4f73\u7684\u7b97\u6cd5\u5417\uff1f", "tags_en": ["Design", "Array"], "tags_cn": ["\u8bbe\u8ba1", "\u6570\u7ec4"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class MRUQueue {\npublic:\n MRUQueue(int n) {\n\n }\n \n int fetch(int k) {\n\n }\n};\n\n/**\n * Your MRUQueue object will be instantiated and called as such:\n * MRUQueue* obj = new MRUQueue(n);\n * int param_1 = obj->fetch(k);\n */", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class MRUQueue {\n\n public MRUQueue(int n) {\n\n }\n \n public int fetch(int k) {\n\n }\n}\n\n/**\n * Your MRUQueue object will be instantiated and called as such:\n * MRUQueue obj = new MRUQueue(n);\n * int param_1 = obj.fetch(k);\n */", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class MRUQueue(object):\n\n def __init__(self, n):\n \"\"\"\n :type n: int\n \"\"\"\n\n\n def fetch(self, k):\n \"\"\"\n :type k: int\n :rtype: int\n \"\"\"\n\n\n\n# Your MRUQueue object will be instantiated and called as such:\n# obj = MRUQueue(n)\n# param_1 = obj.fetch(k)", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class MRUQueue:\n\n def __init__(self, n: int):\n\n\n def fetch(self, k: int) -> int:\n\n\n\n# Your MRUQueue object will be instantiated and called as such:\n# obj = MRUQueue(n)\n# param_1 = obj.fetch(k)", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\n\ntypedef struct {\n\n} MRUQueue;\n\n\nMRUQueue* mRUQueueCreate(int n) {\n\n}\n\nint mRUQueueFetch(MRUQueue* obj, int k) {\n\n}\n\nvoid mRUQueueFree(MRUQueue* obj) {\n\n}\n\n/**\n * Your MRUQueue struct will be instantiated and called as such:\n * MRUQueue* obj = mRUQueueCreate(n);\n * int param_1 = mRUQueueFetch(obj, k);\n \n * mRUQueueFree(obj);\n*/", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class MRUQueue {\n\n public MRUQueue(int n) {\n\n }\n \n public int Fetch(int k) {\n\n }\n}\n\n/**\n * Your MRUQueue object will be instantiated and called as such:\n * MRUQueue obj = new MRUQueue(n);\n * int param_1 = obj.Fetch(k);\n */", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number} n\n */\nvar MRUQueue = function(n) {\n\n};\n\n/** \n * @param {number} k\n * @return {number}\n */\nMRUQueue.prototype.fetch = function(k) {\n\n};\n\n/**\n * Your MRUQueue object will be instantiated and called as such:\n * var obj = new MRUQueue(n)\n * var param_1 = obj.fetch(k)\n */", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "class MRUQueue\n\n=begin\n :type n: Integer\n=end\n def initialize(n)\n\n end\n\n\n=begin\n :type k: Integer\n :rtype: Integer\n=end\n def fetch(k)\n\n end\n\n\nend\n\n# Your MRUQueue object will be instantiated and called as such:\n# obj = MRUQueue.new(n)\n# param_1 = obj.fetch(k)", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "\nclass MRUQueue {\n\n init(_ n: Int) {\n\n }\n \n func fetch(_ k: Int) -> Int {\n\n }\n}\n\n/**\n * Your MRUQueue object will be instantiated and called as such:\n * let obj = MRUQueue(n)\n * let ret_1: Int = obj.fetch(k)\n */", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "type MRUQueue struct {\n\n}\n\n\nfunc Constructor(n int) MRUQueue {\n\n}\n\n\nfunc (this *MRUQueue) Fetch(k int) int {\n\n}\n\n\n/**\n * Your MRUQueue object will be instantiated and called as such:\n * obj := Constructor(n);\n * param_1 := obj.Fetch(k);\n */", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "class MRUQueue(_n: Int) {\n\n def fetch(k: Int): Int = {\n \n }\n\n}\n\n/**\n * Your MRUQueue object will be instantiated and called as such:\n * var obj = new MRUQueue(n)\n * var param_1 = obj.fetch(k)\n */", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class MRUQueue(n: Int) {\n\n fun fetch(k: Int): Int {\n\n }\n\n}\n\n/**\n * Your MRUQueue object will be instantiated and called as such:\n * var obj = MRUQueue(n)\n * var param_1 = obj.fetch(k)\n */", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "struct MRUQueue {\n\n}\n\n\n/**\n * `&self` means the method takes an immutable reference.\n * If you need a mutable reference, change it to `&mut self` instead.\n */\nimpl MRUQueue {\n\n fn new(n: i32) -> Self {\n\n }\n \n fn fetch(&self, k: i32) -> i32 {\n\n }\n}\n\n/**\n * Your MRUQueue object will be instantiated and called as such:\n * let obj = MRUQueue::new(n);\n * let ret_1: i32 = obj.fetch(k);\n */", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class MRUQueue {\n /**\n * @param Integer $n\n */\n function __construct($n) {\n\n }\n\n /**\n * @param Integer $k\n * @return Integer\n */\n function fetch($k) {\n\n }\n}\n\n/**\n * Your MRUQueue object will be instantiated and called as such:\n * $obj = MRUQueue($n);\n * $ret_1 = $obj->fetch($k);\n */", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "class MRUQueue {\n constructor(n: number) {\n\n }\n\n fetch(k: number): number {\n\n }\n}\n\n/**\n * Your MRUQueue object will be instantiated and called as such:\n * var obj = new MRUQueue(n)\n * var param_1 = obj.fetch(k)\n */", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define mru-queue%\n (class object%\n (super-new)\n\n ; n : exact-integer?\n (init-field\n n)\n \n ; fetch : exact-integer? -> exact-integer?\n (define/public (fetch k)\n\n )))\n\n;; Your mru-queue% object will be instantiated and called as such:\n;; (define obj (new mru-queue% [n n]))\n;; (define param_1 (send obj fetch k))", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1756](https://leetcode-cn.com/problems/design-most-recently-used-queue)", "[\u8bbe\u8ba1\u6700\u8fd1\u4f7f\u7528\uff08MRU\uff09\u961f\u5217](/solution/1700-1799/1756.Design%20Most%20Recently%20Used%20Queue/README.md)", "`\u8bbe\u8ba1`,`\u6570\u7ec4`", "\u4e2d\u7b49", "\ud83d\udd12"], "md_table_row_en": ["[1756](https://leetcode.com/problems/design-most-recently-used-queue)", "[Design Most Recently Used Queue](/solution/1700-1799/1756.Design%20Most%20Recently%20Used%20Queue/README_EN.md)", "`Design`,`Array`", "Medium", "\ud83d\udd12"]}, {"question_id": "1902", "frontend_question_id": "1776", "paid_only": false, "paid_only_cn": false, "url_cn": "https://leetcode-cn.com/problems/car-fleet-ii", "url_en": "https://leetcode.com/problems/car-fleet-ii", "relative_path_cn": "/solution/1700-1799/1776.Car%20Fleet%20II/README.md", "relative_path_en": "/solution/1700-1799/1776.Car%20Fleet%20II/README_EN.md", "title_cn": "\u8f66\u961f II", "title_en": "Car Fleet II", "question_title_slug": "car-fleet-ii", "content_en": "There are n
cars traveling at different speeds in the same direction along a one-lane road. You are given an array cars
of length n
, where cars[i] = [positioni, speedi]
represents:
positioni
is the distance between the ith
car and the beginning of the road in meters. It is guaranteed that positioni < positioni+1
.speedi
is the initial speed of the ith
car in meters per second.For simplicity, cars can be considered as points moving along the number line. Two cars collide when they occupy the same position. Once a car collides with another car, they unite and form a single car fleet. The cars in the formed fleet will have the same position and the same speed, which is the initial speed of the slowest car in the fleet.
\n\nReturn an array answer
, where answer[i]
is the time, in seconds, at which the ith
car collides with the next car, or -1
if the car does not collide with the next car. Answers within 10-5
of the actual answers are accepted.
\n
Example 1:
\n\n\nInput: cars = [[1,2],[2,1],[4,3],[7,2]]\nOutput: [1.00000,-1.00000,3.00000,-1.00000]\nExplanation: After exactly one second, the first car will collide with the second car, and form a car fleet with speed 1 m/s. After exactly 3 seconds, the third car will collide with the fourth car, and form a car fleet with speed 2 m/s.\n\n\n
Example 2:
\n\n\nInput: cars = [[3,4],[5,4],[6,3],[9,1]]\nOutput: [2.00000,1.00000,1.50000,-1.00000]\n\n\n
\n
Constraints:
\n\n1 <= cars.length <= 105
1 <= positioni, speedi <= 106
positioni < positioni+1
\u5728\u4e00\u6761\u5355\u8f66\u9053\u4e0a\u6709 n
\u00a0\u8f86\u8f66\uff0c\u5b83\u4eec\u671d\u7740\u540c\u6837\u7684\u65b9\u5411\u884c\u9a76\u3002\u7ed9\u4f60\u4e00\u4e2a\u957f\u5ea6\u4e3a n
\u00a0\u7684\u6570\u7ec4 cars
\u00a0\uff0c\u5176\u4e2d\u00a0cars[i] = [positioni, speedi]
\u00a0\uff0c\u5b83\u8868\u793a\uff1a
positioni
\u00a0\u662f\u7b2c i
\u00a0\u8f86\u8f66\u548c\u9053\u8def\u8d77\u70b9\u4e4b\u95f4\u7684\u8ddd\u79bb\uff08\u5355\u4f4d\uff1a\u7c73\uff09\u3002\u9898\u76ee\u4fdd\u8bc1\u00a0positioni < positioni+1
\u00a0\u3002speedi
\u00a0\u662f\u7b2c i
\u00a0\u8f86\u8f66\u7684\u521d\u59cb\u901f\u5ea6\uff08\u5355\u4f4d\uff1a\u7c73/\u79d2\uff09\u3002\u7b80\u5355\u8d77\u89c1\uff0c\u6240\u6709\u8f66\u5b50\u53ef\u4ee5\u89c6\u4e3a\u5728\u6570\u8f74\u4e0a\u79fb\u52a8\u7684\u70b9\u3002\u5f53\u4e24\u8f86\u8f66\u5360\u636e\u540c\u4e00\u4e2a\u4f4d\u7f6e\u65f6\uff0c\u6211\u4eec\u79f0\u5b83\u4eec\u76f8\u9047\u4e86\u3002\u4e00\u65e6\u4e24\u8f86\u8f66\u76f8\u9047\uff0c\u5b83\u4eec\u4f1a\u5408\u5e76\u6210\u4e00\u4e2a\u8f66\u961f\uff0c\u8fd9\u4e2a\u8f66\u961f\u91cc\u7684\u8f66\u6709\u7740\u540c\u6837\u7684\u4f4d\u7f6e\u548c\u76f8\u540c\u7684\u901f\u5ea6\uff0c\u901f\u5ea6\u4e3a\u8fd9\u4e2a\u8f66\u961f\u91cc\u00a0\u6700\u6162\u00a0\u4e00\u8f86\u8f66\u7684\u901f\u5ea6\u3002
\n\n\u8bf7\u4f60\u8fd4\u56de\u4e00\u4e2a\u6570\u7ec4\u00a0answer
\u00a0\uff0c\u5176\u4e2d\u00a0answer[i]
\u00a0\u662f\u7b2c i
\u00a0\u8f86\u8f66\u4e0e\u4e0b\u4e00\u8f86\u8f66\u76f8\u9047\u7684\u65f6\u95f4\uff08\u5355\u4f4d\uff1a\u79d2\uff09\uff0c\u5982\u679c\u8fd9\u8f86\u8f66\u4e0d\u4f1a\u4e0e\u4e0b\u4e00\u8f86\u8f66\u76f8\u9047\uff0c\u5219 answer[i]
\u00a0\u4e3a -1
\u00a0\u3002\u7b54\u6848\u7cbe\u5ea6\u8bef\u5dee\u9700\u5728 10-5
\u00a0\u4ee5\u5185\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1acars = [[1,2],[2,1],[4,3],[7,2]]\n\u8f93\u51fa\uff1a[1.00000,-1.00000,3.00000,-1.00000]\n\u89e3\u91ca\uff1a\u7ecf\u8fc7\u6070\u597d 1 \u79d2\u4ee5\u540e\uff0c\u7b2c\u4e00\u8f86\u8f66\u4f1a\u4e0e\u7b2c\u4e8c\u8f86\u8f66\u76f8\u9047\uff0c\u5e76\u5f62\u6210\u4e00\u4e2a 1 m/s \u7684\u8f66\u961f\u3002\u7ecf\u8fc7\u6070\u597d 3 \u79d2\u4ee5\u540e\uff0c\u7b2c\u4e09\u8f86\u8f66\u4f1a\u4e0e\u7b2c\u56db\u8f86\u8f66\u76f8\u9047\uff0c\u5e76\u5f62\u6210\u4e00\u4e2a 2 m/s \u7684\u8f66\u961f\u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1acars = [[3,4],[5,4],[6,3],[9,1]]\n\u8f93\u51fa\uff1a[2.00000,1.00000,1.50000,-1.00000]\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= cars.length <= 105
1 <= positioni, speedi <= 106
positioni < positioni+1
You are given two arrays of integers nums1
and nums2
, possibly of different lengths. The values in the arrays are between 1
and 6
, inclusive.
In one operation, you can change any integer's value in any of the arrays to any value between 1
and 6
, inclusive.
Return the minimum number of operations required to make the sum of values in nums1
equal to the sum of values in nums2
. Return -1
\u200b\u200b\u200b\u200b\u200b if it is not possible to make the sum of the two arrays equal.
\n
Example 1:
\n\n\nInput: nums1 = [1,2,3,4,5,6], nums2 = [1,1,2,2,2,2]\nOutput: 3\nExplanation: You can make the sums of nums1 and nums2 equal with 3 operations. All indices are 0-indexed.\n- Change nums2[0] to 6. nums1 = [1,2,3,4,5,6], nums2 = [6,1,2,2,2,2].\n- Change nums1[5] to 1. nums1 = [1,2,3,4,5,1], nums2 = [6,1,2,2,2,2].\n- Change nums1[2] to 2. nums1 = [1,2,2,4,5,1], nums2 = [6,1,2,2,2,2].\n\n\n
Example 2:
\n\n\nInput: nums1 = [1,1,1,1,1,1,1], nums2 = [6]\nOutput: -1\nExplanation: There is no way to decrease the sum of nums1 or to increase the sum of nums2 to make them equal.\n\n\n
Example 3:
\n\n\nInput: nums1 = [6,6], nums2 = [1]\nOutput: 3\nExplanation: You can make the sums of nums1 and nums2 equal with 3 operations. All indices are 0-indexed. \n- Change nums1[0] to 2. nums1 = [2,6], nums2 = [1].\n- Change nums1[1] to 2. nums1 = [2,2], nums2 = [1].\n- Change nums2[0] to 4. nums1 = [2,2], nums2 = [4].\n\n\n
\n
Constraints:
\n\n1 <= nums1.length, nums2.length <= 105
1 <= nums1[i], nums2[i] <= 6
\u7ed9\u4f60\u4e24\u4e2a\u957f\u5ea6\u53ef\u80fd\u4e0d\u7b49\u7684\u6574\u6570\u6570\u7ec4\u00a0nums1
\u548c\u00a0nums2
\u00a0\u3002\u4e24\u4e2a\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u503c\u90fd\u5728\u00a01
\u00a0\u5230\u00a06
\u00a0\u4e4b\u95f4\uff08\u5305\u542b\u00a01
\u00a0\u548c\u00a06
\uff09\u3002
\u6bcf\u6b21\u64cd\u4f5c\u4e2d\uff0c\u4f60\u53ef\u4ee5\u9009\u62e9 \u4efb\u610f\u00a0\u6570\u7ec4\u4e2d\u7684\u4efb\u610f\u4e00\u4e2a\u6574\u6570\uff0c\u5c06\u5b83\u53d8\u6210 1
\u00a0\u5230 6
\u00a0\u4e4b\u95f4 \u4efb\u610f\u00a0\u7684\u503c\uff08\u5305\u542b 1
\u00a0\u548c 6
\uff09\u3002
\u8bf7\u4f60\u8fd4\u56de\u4f7f nums1
\u00a0\u4e2d\u6240\u6709\u6570\u7684\u548c\u4e0e\u00a0nums2
\u00a0\u4e2d\u6240\u6709\u6570\u7684\u548c\u76f8\u7b49\u7684\u6700\u5c11\u64cd\u4f5c\u6b21\u6570\u3002\u5982\u679c\u65e0\u6cd5\u4f7f\u4e24\u4e2a\u6570\u7ec4\u7684\u548c\u76f8\u7b49\uff0c\u8bf7\u8fd4\u56de -1
\u00a0\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1anums1 = [1,2,3,4,5,6], nums2 = [1,1,2,2,2,2]\n\u8f93\u51fa\uff1a3\n\u89e3\u91ca\uff1a\u4f60\u53ef\u4ee5\u901a\u8fc7 3 \u6b21\u64cd\u4f5c\u4f7f nums1 \u4e2d\u6240\u6709\u6570\u7684\u548c\u4e0e nums2 \u4e2d\u6240\u6709\u6570\u7684\u548c\u76f8\u7b49\u3002\u4ee5\u4e0b\u6570\u7ec4\u4e0b\u6807\u90fd\u4ece 0 \u5f00\u59cb\u3002\n- \u5c06 nums2[0] \u53d8\u4e3a 6 \u3002 nums1 = [1,2,3,4,5,6], nums2 = [6,1,2,2,2,2] \u3002\n- \u5c06 nums1[5] \u53d8\u4e3a 1 \u3002 nums1 = [1,2,3,4,5,1], nums2 = [6,1,2,2,2,2] \u3002\n- \u5c06 nums1[2] \u53d8\u4e3a 2 \u3002 nums1 = [1,2,2,4,5,1], nums2 = [6,1,2,2,2,2] \u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1anums1 = [1,1,1,1,1,1,1], nums2 = [6]\n\u8f93\u51fa\uff1a-1\n\u89e3\u91ca\uff1a\u6ca1\u6709\u529e\u6cd5\u51cf\u5c11 nums1 \u7684\u548c\u6216\u8005\u589e\u52a0 nums2 \u7684\u548c\u4f7f\u4e8c\u8005\u76f8\u7b49\u3002\n\n\n
\u793a\u4f8b 3\uff1a
\n\n\u8f93\u5165\uff1anums1 = [6,6], nums2 = [1]\n\u8f93\u51fa\uff1a3\n\u89e3\u91ca\uff1a\u4f60\u53ef\u4ee5\u901a\u8fc7 3 \u6b21\u64cd\u4f5c\u4f7f nums1 \u4e2d\u6240\u6709\u6570\u7684\u548c\u4e0e nums2 \u4e2d\u6240\u6709\u6570\u7684\u548c\u76f8\u7b49\u3002\u4ee5\u4e0b\u6570\u7ec4\u4e0b\u6807\u90fd\u4ece 0 \u5f00\u59cb\u3002\n- \u5c06 nums1[0] \u53d8\u4e3a 2 \u3002 nums1 = [2,6], nums2 = [1] \u3002\n- \u5c06 nums1[1] \u53d8\u4e3a 2 \u3002 nums1 = [2,2], nums2 = [1] \u3002\n- \u5c06 nums2[0] \u53d8\u4e3a 4 \u3002 nums1 = [2,2], nums2 = [4] \u3002\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= nums1.length, nums2.length <= 105
1 <= nums1[i], nums2[i] <= 6
You would like to make dessert and are preparing to buy the ingredients. You have n
ice cream base flavors and m
types of toppings to choose from. You must follow these rules when making your dessert:
You are given three inputs:
\n\nbaseCosts
, an integer array of length n
, where each baseCosts[i]
represents the price of the ith
ice cream base flavor.toppingCosts
, an integer array of length m
, where each toppingCosts[i]
is the price of one of the ith
topping.target
, an integer representing your target price for dessert.You want to make a dessert with a total cost as close to target
as possible.
Return the closest possible cost of the dessert to target
. If there are multiple, return the lower one.
\n
Example 1:
\n\n\nInput: baseCosts = [1,7], toppingCosts = [3,4], target = 10\nOutput: 10\nExplanation: Consider the following combination (all 0-indexed):\n- Choose base 1: cost 7\n- Take 1 of topping 0: cost 1 x 3 = 3\n- Take 0 of topping 1: cost 0 x 4 = 0\nTotal: 7 + 3 + 0 = 10.\n\n\n
Example 2:
\n\n\nInput: baseCosts = [2,3], toppingCosts = [4,5,100], target = 18\nOutput: 17\nExplanation: Consider the following combination (all 0-indexed):\n- Choose base 1: cost 3\n- Take 1 of topping 0: cost 1 x 4 = 4\n- Take 2 of topping 1: cost 2 x 5 = 10\n- Take 0 of topping 2: cost 0 x 100 = 0\nTotal: 3 + 4 + 10 + 0 = 17. You cannot make a dessert with a total cost of 18.\n\n\n
Example 3:
\n\n\nInput: baseCosts = [3,10], toppingCosts = [2,5], target = 9\nOutput: 8\nExplanation: It is possible to make desserts with cost 8 and 10. Return 8 as it is the lower cost.\n\n\n
Example 4:
\n\n\nInput: baseCosts = [10], toppingCosts = [1], target = 1\nOutput: 10\nExplanation: Notice that you don't have to have any toppings, but you must have exactly one base.\n\n
\n
Constraints:
\n\nn == baseCosts.length
m == toppingCosts.length
1 <= n, m <= 10
1 <= baseCosts[i], toppingCosts[i] <= 104
1 <= target <= 104
\u4f60\u6253\u7b97\u505a\u751c\u70b9\uff0c\u73b0\u5728\u9700\u8981\u8d2d\u4e70\u914d\u6599\u3002\u76ee\u524d\u5171\u6709 n
\u79cd\u51b0\u6fc0\u51cc\u57fa\u6599\u548c m
\u79cd\u914d\u6599\u53ef\u4f9b\u9009\u8d2d\u3002\u800c\u5236\u4f5c\u751c\u70b9\u9700\u8981\u9075\u5faa\u4ee5\u4e0b\u51e0\u6761\u89c4\u5219\uff1a
\u7ed9\u4f60\u4ee5\u4e0b\u4e09\u4e2a\u8f93\u5165\uff1a
\n\nbaseCosts
\uff0c\u4e00\u4e2a\u957f\u5ea6\u4e3a n
\u7684\u6574\u6570\u6570\u7ec4\uff0c\u5176\u4e2d\u6bcf\u4e2a baseCosts[i]
\u8868\u793a\u7b2c i
\u79cd\u51b0\u6fc0\u51cc\u57fa\u6599\u7684\u4ef7\u683c\u3002toppingCosts
\uff0c\u4e00\u4e2a\u957f\u5ea6\u4e3a m
\u7684\u6574\u6570\u6570\u7ec4\uff0c\u5176\u4e2d\u6bcf\u4e2a toppingCosts[i]
\u8868\u793a \u4e00\u4efd \u7b2c i
\u79cd\u51b0\u6fc0\u51cc\u914d\u6599\u7684\u4ef7\u683c\u3002target
\uff0c\u4e00\u4e2a\u6574\u6570\uff0c\u8868\u793a\u4f60\u5236\u4f5c\u751c\u70b9\u7684\u76ee\u6807\u4ef7\u683c\u3002\u4f60\u5e0c\u671b\u81ea\u5df1\u505a\u7684\u751c\u70b9\u603b\u6210\u672c\u5c3d\u53ef\u80fd\u63a5\u8fd1\u76ee\u6807\u4ef7\u683c target
\u3002
\u8fd4\u56de\u6700\u63a5\u8fd1 target
\u7684\u751c\u70b9\u6210\u672c\u3002\u5982\u679c\u6709\u591a\u79cd\u65b9\u6848\uff0c\u8fd4\u56de\u00a0\u6210\u672c\u76f8\u5bf9\u8f83\u4f4e \u7684\u4e00\u79cd\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1abaseCosts = [1,7], toppingCosts = [3,4], target = 10\n\u8f93\u51fa\uff1a10\n\u89e3\u91ca\uff1a\u8003\u8651\u4e0b\u9762\u7684\u65b9\u6848\u7ec4\u5408\uff08\u6240\u6709\u4e0b\u6807\u5747\u4ece 0 \u5f00\u59cb\uff09\uff1a\n- \u9009\u62e9 1 \u53f7\u57fa\u6599\uff1a\u6210\u672c 7\n- \u9009\u62e9 1 \u4efd 0 \u53f7\u914d\u6599\uff1a\u6210\u672c 1 x 3 = 3\n- \u9009\u62e9 0 \u4efd 1 \u53f7\u914d\u6599\uff1a\u6210\u672c 0 x 4 = 0\n\u603b\u6210\u672c\uff1a7 + 3 + 0 = 10 \u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1abaseCosts = [2,3], toppingCosts = [4,5,100], target = 18\n\u8f93\u51fa\uff1a17\n\u89e3\u91ca\uff1a\u8003\u8651\u4e0b\u9762\u7684\u65b9\u6848\u7ec4\u5408\uff08\u6240\u6709\u4e0b\u6807\u5747\u4ece 0 \u5f00\u59cb\uff09\uff1a\n- \u9009\u62e9 1 \u53f7\u57fa\u6599\uff1a\u6210\u672c 3\n- \u9009\u62e9 1 \u4efd 0 \u53f7\u914d\u6599\uff1a\u6210\u672c 1 x 4 = 4\n- \u9009\u62e9 2 \u4efd 1 \u53f7\u914d\u6599\uff1a\u6210\u672c 2 x 5 = 10\n- \u9009\u62e9 0 \u4efd 2 \u53f7\u914d\u6599\uff1a\u6210\u672c 0 x 100 = 0\n\u603b\u6210\u672c\uff1a3 + 4 + 10 + 0 = 17 \u3002\u4e0d\u5b58\u5728\u603b\u6210\u672c\u4e3a 18 \u7684\u751c\u70b9\u5236\u4f5c\u65b9\u6848\u3002\n\n\n
\u793a\u4f8b 3\uff1a
\n\n\n\u8f93\u5165\uff1abaseCosts = [3,10], toppingCosts = [2,5], target = 9\n\u8f93\u51fa\uff1a8\n\u89e3\u91ca\uff1a\u53ef\u4ee5\u5236\u4f5c\u603b\u6210\u672c\u4e3a 8 \u548c 10 \u7684\u751c\u70b9\u3002\u8fd4\u56de 8 \uff0c\u56e0\u4e3a\u8fd9\u662f\u6210\u672c\u66f4\u4f4e\u7684\u65b9\u6848\u3002\n\n\n
\u793a\u4f8b 4\uff1a
\n\n\n\u8f93\u5165\uff1abaseCosts = [10], toppingCosts = [1], target = 1\n\u8f93\u51fa\uff1a10\n\u89e3\u91ca\uff1a\u6ce8\u610f\uff0c\u4f60\u53ef\u4ee5\u9009\u62e9\u4e0d\u6dfb\u52a0\u4efb\u4f55\u914d\u6599\uff0c\u4f46\u4f60\u5fc5\u987b\u9009\u62e9\u4e00\u79cd\u57fa\u6599\u3002\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\nn == baseCosts.length
m == toppingCosts.length
1 <= n, m <= 10
1 <= baseCosts[i], toppingCosts[i] <= 104
1 <= target <= 104
You are given an array items
, where each items[i] = [typei, colori, namei]
describes the type, color, and name of the ith
item. You are also given a rule represented by two strings, ruleKey
and ruleValue
.
The ith
item is said to match the rule if one of the following is true:
ruleKey == "type"
and ruleValue == typei
.ruleKey == "color"
and ruleValue == colori
.ruleKey == "name"
and ruleValue == namei
.Return the number of items that match the given rule.
\n\n\n
Example 1:
\n\n\nInput: items = [["phone","blue","pixel"],["computer","silver","lenovo"],["phone","gold","iphone"]], ruleKey = "color", ruleValue = "silver"\nOutput: 1\nExplanation: There is only one item matching the given rule, which is ["computer","silver","lenovo"].\n\n\n
Example 2:
\n\n\nInput: items = [["phone","blue","pixel"],["computer","silver","phone"],["phone","gold","iphone"]], ruleKey = "type", ruleValue = "phone"\nOutput: 2\nExplanation: There are only two items matching the given rule, which are ["phone","blue","pixel"] and ["phone","gold","iphone"]. Note that the item ["computer","silver","phone"] does not match.\n\n
\n
Constraints:
\n\n1 <= items.length <= 104
1 <= typei.length, colori.length, namei.length, ruleValue.length <= 10
ruleKey
is equal to either "type"
, "color"
, or "name"
.\u7ed9\u4f60\u4e00\u4e2a\u6570\u7ec4 items
\uff0c\u5176\u4e2d\u00a0items[i] = [typei, colori, namei]
\uff0c\u63cf\u8ff0\u7b2c i
\u4ef6\u7269\u54c1\u7684\u7c7b\u578b\u3001\u989c\u8272\u4ee5\u53ca\u540d\u79f0\u3002
\u53e6\u7ed9\u4f60\u4e00\u6761\u7531\u4e24\u4e2a\u5b57\u7b26\u4e32\u00a0ruleKey
\u548c ruleValue
\u8868\u793a\u7684\u68c0\u7d22\u89c4\u5219\u3002
\u5982\u679c\u7b2c i
\u4ef6\u7269\u54c1\u80fd\u6ee1\u8db3\u4e0b\u8ff0\u6761\u4ef6\u4e4b\u4e00\uff0c\u5219\u8ba4\u4e3a\u8be5\u7269\u54c1\u4e0e\u7ed9\u5b9a\u7684\u68c0\u7d22\u89c4\u5219 \u5339\u914d \uff1a
ruleKey == \"type\"
\u4e14 ruleValue == typei
\u3002ruleKey == \"color\"
\u4e14 ruleValue == colori
\u3002ruleKey == \"name\"
\u4e14 ruleValue == namei
\u3002\u7edf\u8ba1\u5e76\u8fd4\u56de \u5339\u914d\u68c0\u7d22\u89c4\u5219\u7684\u7269\u54c1\u6570\u91cf \u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1aitems = [[\"phone\",\"blue\",\"pixel\"],[\"computer\",\"silver\",\"lenovo\"],[\"phone\",\"gold\",\"iphone\"]], ruleKey = \"color\", ruleValue = \"silver\"\n\u8f93\u51fa\uff1a1\n\u89e3\u91ca\uff1a\u53ea\u6709\u4e00\u4ef6\u7269\u54c1\u5339\u914d\u68c0\u7d22\u89c4\u5219\uff0c\u8fd9\u4ef6\u7269\u54c1\u662f [\"computer\",\"silver\",\"lenovo\"] \u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1aitems = [[\"phone\",\"blue\",\"pixel\"],[\"computer\",\"silver\",\"phone\"],[\"phone\",\"gold\",\"iphone\"]], ruleKey = \"type\", ruleValue = \"phone\"\n\u8f93\u51fa\uff1a2\n\u89e3\u91ca\uff1a\u53ea\u6709\u4e24\u4ef6\u7269\u54c1\u5339\u914d\u68c0\u7d22\u89c4\u5219\uff0c\u8fd9\u4e24\u4ef6\u7269\u54c1\u5206\u522b\u662f [\"phone\",\"blue\",\"pixel\"] \u548c [\"phone\",\"gold\",\"iphone\"] \u3002\u6ce8\u610f\uff0c[\"computer\",\"silver\",\"phone\"] \u672a\u5339\u914d\u68c0\u7d22\u89c4\u5219\u3002\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= items.length <= 104
1 <= typei.length, colori.length, namei.length, ruleValue.length <= 10
ruleKey
\u7b49\u4e8e \"type\"
\u3001\"color\"
\u6216 \"name\"
Table: LogInfo
\r\n+-------------+----------+\r\n| Column Name | Type |\r\n+-------------+----------+\r\n| account_id | int |\r\n| ip_address | int |\r\n| login | datetime |\r\n| logout | datetime |\r\n+-------------+----------+\r\nThere is no primary key for this table, and it may contain duplicates.\r\nThe table contains information about the login and logout dates of Leetflex accounts. It also contains the IP address from which the account logged in and out.\r\nIt is guaranteed that the logout time is after the login time.\r\n\r\n\r\n
\r\n\r\n
Write an SQL query to find the account_id
of the accounts that should be banned from Leetflex. An account should be banned if it was logged in at some moment from two different IP addresses.
Return the result table in any order.
\r\n\r\nThe query result format is in the following example:
\r\n\r\n\r\n\r\n
\r\nLogInfo table:\r\n+------------+------------+---------------------+---------------------+\r\n| account_id | ip_address | login | logout |\r\n+------------+------------+---------------------+---------------------+\r\n| 1 | 1 | 2021-02-01 09:00:00 | 2021-02-01 09:30:00 |\r\n| 1 | 2 | 2021-02-01 08:00:00 | 2021-02-01 11:30:00 |\r\n| 2 | 6 | 2021-02-01 20:30:00 | 2021-02-01 22:00:00 |\r\n| 2 | 7 | 2021-02-02 20:30:00 | 2021-02-02 22:00:00 |\r\n| 3 | 9 | 2021-02-01 16:00:00 | 2021-02-01 16:59:59 |\r\n| 3 | 13 | 2021-02-01 17:00:00 | 2021-02-01 17:59:59 |\r\n| 4 | 10 | 2021-02-01 16:00:00 | 2021-02-01 17:00:00 |\r\n| 4 | 11 | 2021-02-01 17:00:00 | 2021-02-01 17:59:59 |\r\n+------------+------------+---------------------+---------------------+\r\n\r\nResult table:\r\n+------------+\r\n| account_id |\r\n+------------+\r\n| 1 |\r\n| 4 |\r\n+------------+\r\nAccount ID 1 --> The account was active from "2021-02-01 09:00:00" to "2021-02-01 09:30:00" with two different IP addresses (1 and 2). It should be banned.\r\nAccount ID 2 --> The account was active from two different addresses (6, 7) but in two different times.\r\nAccount ID 3 --> The account was active from two different addresses (9, 13) on the same day but they do not intersect at any moment.\r\nAccount ID 4 --> The account was active from "2021-02-01 17:00:00" to "2021-02-01 17:00:00" with two different IP addresses (10 and 11). It should be banned.\r\n", "content_cn": "
\u8868: LogInfo
+-------------+----------+\n| Column Name | Type |\n+-------------+----------+\n| account_id | int |\n| ip_address | int |\n| login | datetime |\n| logout | datetime |\n+-------------+----------+\n\u8be5\u8868\u662f\u6ca1\u6709\u4e3b\u952e\u7684\uff0c\u5b83\u53ef\u80fd\u5305\u542b\u91cd\u590d\u9879\u3002\n\u8be5\u8868\u5305\u542b\u6709\u5173Leetflex\u5e10\u6237\u7684\u767b\u5f55\u548c\u6ce8\u9500\u65e5\u671f\u7684\u4fe1\u606f\u3002 \u5b83\u8fd8\u5305\u542b\u4e86\u8be5\u8d26\u6237\u7528\u4e8e\u767b\u5f55\u548c\u6ce8\u9500\u7684\u7f51\u7edc\u5730\u5740\u7684\u4fe1\u606f\u3002\n\u9898\u76ee\u786e\u4fdd\u6bcf\u4e00\u4e2a\u6ce8\u9500\u65f6\u95f4\u90fd\u5728\u767b\u5f55\u65f6\u95f4\u4e4b\u540e\u3002\n\n\n
\u00a0
\n\n\u7f16\u5199\u4e00\u4e2aSQL\u67e5\u8be2\u8bed\u53e5\uff0c\u67e5\u627e\u90a3\u4e9b\u5e94\u8be5\u88ab\u7981\u6b62\u7684Leetflex\u5e10\u6237\u7f16\u53f7account_id\u3002 \u5982\u679c\u67d0\u4e2a\u5e10\u6237\u5728\u67d0\u4e00\u65f6\u523b\u4ece\u4e24\u4e2a\u4e0d\u540c\u7684\u7f51\u7edc\u5730\u5740\u767b\u5f55\u4e86\uff0c\u5219\u8fd9\u4e2a\u5e10\u6237\u5e94\u8be5\u88ab\u7981\u6b62\u3002
\n\n\u53ef\u4ee5\u4ee5\u4efb\u4f55\u987a\u5e8f\u8fd4\u56de\u7ed3\u679c\u3002
\n\n\u67e5\u8be2\u7ed3\u679c\u683c\u5f0f\u5982\u4e0b\u4f8b\u6240\u793a:
\n\n\u00a0
\n\nLogInfo table:\n+------------+------------+---------------------+---------------------+\n| account_id | ip_address | login | logout |\n+------------+------------+---------------------+---------------------+\n| 1 | 1 | 2021-02-01 09:00:00 | 2021-02-01 09:30:00 |\n| 1 | 2 | 2021-02-01 08:00:00 | 2021-02-01 11:30:00 |\n| 2 | 6 | 2021-02-01 20:30:00 | 2021-02-01 22:00:00 |\n| 2 | 7 | 2021-02-02 20:30:00 | 2021-02-02 22:00:00 |\n| 3 | 9 | 2021-02-01 16:00:00 | 2021-02-01 16:59:59 |\n| 3 | 13 | 2021-02-01 17:00:00 | 2021-02-01 17:59:59 |\n| 4 | 10 | 2021-02-01 16:00:00 | 2021-02-01 17:00:00 |\n| 4 | 11 | 2021-02-01 17:00:00 | 2021-02-01 17:59:59 |\n+------------+------------+---------------------+---------------------+\n\nResult table:\n+------------+\n| account_id |\n+------------+\n| 1 |\n| 4 |\n+------------+\nAccount ID 1 --> \u8be5\u8d26\u6237\u4ece \"2021-02-01 09:00:00\" \u5230 \"2021-02-01 09:30:00\" \u5728\u4e24\u4e2a\u4e0d\u540c\u7684\u7f51\u7edc\u5730\u5740(1 and 2)\u4e0a\u6fc0\u6d3b\u4e86\u3002\u5b83\u5e94\u8be5\u88ab\u7981\u6b62.\nAccount ID 2 --> \u8be5\u8d26\u6237\u5728\u4e24\u4e2a\u4e0d\u540c\u7684\u7f51\u7edc\u5730\u5740 (6, 7) \u6fc0\u6d3b\u4e86\uff0c\u4f46\u5728\u4e0d\u540c\u7684\u65f6\u95f4\u4e0a.\nAccount ID 3 --> \u8be5\u8d26\u6237\u5728\u4e24\u4e2a\u4e0d\u540c\u7684\u7f51\u7edc\u5730\u5740 (9, 13) \u6fc0\u6d3b\u4e86\uff0c\u867d\u7136\u662f\u540c\u4e00\u5929\uff0c\u4f46\u65f6\u95f4\u4e0a\u6ca1\u6709\u4ea4\u96c6.\nAccount ID 4 --> \u8be5\u8d26\u6237\u4ece \"2021-02-01 17:00:00\" \u5230 \"2021-02-01 17:00:00\" \u5728\u4e24\u4e2a\u4e0d\u540c\u7684\u7f51\u7edc\u5730\u5740 (10 and 11)\u4e0a\u6fc0\u6d3b\u4e86\u3002\u5b83\u5e94\u8be5\u88ab\u7981\u6b62.\n\n", "tags_en": [], "tags_cn": [], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "MySQL", "langSlug": "mysql", "code": "# Write your MySQL query statement below", "__typename": "CodeSnippetNode"}, {"lang": "MS SQL Server", "langSlug": "mssql", "code": "/* Write your T-SQL query statement below */", "__typename": "CodeSnippetNode"}, {"lang": "Oracle", "langSlug": "oraclesql", "code": "/* Write your PL/SQL query statement below */", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1747](https://leetcode-cn.com/problems/leetflex-banned-accounts)", "[\u5e94\u8be5\u88ab\u7981\u6b62\u7684Leetflex\u8d26\u6237](/solution/1700-1799/1747.Leetflex%20Banned%20Accounts/README.md)", "", "\u4e2d\u7b49", "\ud83d\udd12"], "md_table_row_en": ["[1747](https://leetcode.com/problems/leetflex-banned-accounts)", "[Leetflex Banned Accounts](/solution/1700-1799/1747.Leetflex%20Banned%20Accounts/README_EN.md)", "", "Medium", "\ud83d\udd12"]}, {"question_id": "1897", "frontend_question_id": "1771", "paid_only": false, "paid_only_cn": false, "url_cn": "https://leetcode-cn.com/problems/maximize-palindrome-length-from-subsequences", "url_en": "https://leetcode.com/problems/maximize-palindrome-length-from-subsequences", "relative_path_cn": "/solution/1700-1799/1771.Maximize%20Palindrome%20Length%20From%20Subsequences/README.md", "relative_path_en": "/solution/1700-1799/1771.Maximize%20Palindrome%20Length%20From%20Subsequences/README_EN.md", "title_cn": "\u7531\u5b50\u5e8f\u5217\u6784\u9020\u7684\u6700\u957f\u56de\u6587\u4e32\u7684\u957f\u5ea6", "title_en": "Maximize Palindrome Length From Subsequences", "question_title_slug": "maximize-palindrome-length-from-subsequences", "content_en": "
You are given two strings, word1
and word2
. You want to construct a string in the following manner:
subsequence1
from word1
.subsequence2
from word2
.subsequence1 + subsequence2
, to make the string.Return the length of the longest palindrome that can be constructed in the described manner. If no palindromes can be constructed, return 0
.
A subsequence of a string s
is a string that can be made by deleting some (possibly none) characters from s
without changing the order of the remaining characters.
A palindrome is a string that reads the same forward as well as backward.
\n\n\n
Example 1:
\n\n\nInput: word1 = "cacb", word2 = "cbba"\nOutput: 5\nExplanation: Choose "ab" from word1 and "cba" from word2 to make "abcba", which is a palindrome.\n\n
Example 2:
\n\n\nInput: word1 = "ab", word2 = "ab"\nOutput: 3\nExplanation: Choose "ab" from word1 and "a" from word2 to make "aba", which is a palindrome.\n\n
Example 3:
\n\n\nInput: word1 = "aa", word2 = "bb"\nOutput: 0\nExplanation: You cannot construct a palindrome from the described method, so return 0.\n\n
\n
Constraints:
\n\n1 <= word1.length, word2.length <= 1000
word1
and word2
consist of lowercase English letters.\u7ed9\u4f60\u4e24\u4e2a\u5b57\u7b26\u4e32 word1
\u548c word2
\uff0c\u8bf7\u4f60\u6309\u4e0b\u8ff0\u65b9\u6cd5\u6784\u9020\u4e00\u4e2a\u5b57\u7b26\u4e32\uff1a
word1
\u4e2d\u9009\u51fa\u67d0\u4e2a \u975e\u7a7a \u5b50\u5e8f\u5217 subsequence1
\u3002word2
\u4e2d\u9009\u51fa\u67d0\u4e2a \u975e\u7a7a \u5b50\u5e8f\u5217 subsequence2
\u3002subsequence1 + subsequence2
\uff0c\u5f97\u5230\u5b57\u7b26\u4e32\u3002\u8fd4\u56de\u53ef\u6309\u4e0a\u8ff0\u65b9\u6cd5\u6784\u9020\u7684\u6700\u957f \u56de\u6587\u4e32 \u7684 \u957f\u5ea6 \u3002\u5982\u679c\u65e0\u6cd5\u6784\u9020\u56de\u6587\u4e32\uff0c\u8fd4\u56de 0
\u3002
\u5b57\u7b26\u4e32 s
\u7684\u4e00\u4e2a \u5b50\u5e8f\u5217 \u662f\u901a\u8fc7\u4ece s
\u4e2d\u5220\u9664\u4e00\u4e9b\uff08\u4e5f\u53ef\u80fd\u4e0d\u5220\u9664\uff09\u5b57\u7b26\u800c\u4e0d\u66f4\u6539\u5176\u4f59\u5b57\u7b26\u7684\u987a\u5e8f\u751f\u6210\u7684\u5b57\u7b26\u4e32\u3002
\u56de\u6587\u4e32 \u662f\u6b63\u7740\u8bfb\u548c\u53cd\u7740\u8bfb\u7ed3\u679c\u4e00\u81f4\u7684\u5b57\u7b26\u4e32\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1aword1 = \"cacb\", word2 = \"cbba\"\n\u8f93\u51fa\uff1a5\n\u89e3\u91ca\uff1a\u4ece word1 \u4e2d\u9009\u51fa \"ab\" \uff0c\u4ece word2 \u4e2d\u9009\u51fa \"cba\" \uff0c\u5f97\u5230\u56de\u6587\u4e32 \"abcba\" \u3002\n\n
\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1aword1 = \"ab\", word2 = \"ab\"\n\u8f93\u51fa\uff1a3\n\u89e3\u91ca\uff1a\u4ece word1 \u4e2d\u9009\u51fa \"ab\" \uff0c\u4ece word2 \u4e2d\u9009\u51fa \"a\" \uff0c\u5f97\u5230\u56de\u6587\u4e32 \"aba\" \u3002\n\n
\u793a\u4f8b 3\uff1a
\n\n\u8f93\u5165\uff1aword1 = \"aa\", word2 = \"bb\"\n\u8f93\u51fa\uff1a0\n\u89e3\u91ca\uff1a\u65e0\u6cd5\u6309\u9898\u9762\u6240\u8ff0\u65b9\u6cd5\u6784\u9020\u56de\u6587\u4e32\uff0c\u6240\u4ee5\u8fd4\u56de 0 \u3002\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= word1.length, word2.length <= 1000
word1
\u548c word2
\u7531\u5c0f\u5199\u82f1\u6587\u5b57\u6bcd\u7ec4\u6210You are given two integer arrays nums
and multipliers
of size n
and m
respectively, where n >= m
. The arrays are 1-indexed.
You begin with a score of 0
. You want to perform exactly m
operations. On the ith
operation (1-indexed), you will:
x
from either the start or the end of the array nums
.multipliers[i] * x
to your score.x
from the array nums
.Return the maximum score after performing m
operations.
\n
Example 1:
\n\n\nInput: nums = [1,2,3], multipliers = [3,2,1]\nOutput: 14\nExplanation: An optimal solution is as follows:\n- Choose from the end, [1,2,3], adding 3 * 3 = 9 to the score.\n- Choose from the end, [1,2], adding 2 * 2 = 4 to the score.\n- Choose from the end, [1], adding 1 * 1 = 1 to the score.\nThe total score is 9 + 4 + 1 = 14.\n\n
Example 2:
\n\n\nInput: nums = [-5,-3,-3,-2,7,1], multipliers = [-10,-5,3,4,6]\nOutput: 102\nExplanation: An optimal solution is as follows:\n- Choose from the start, [-5,-3,-3,-2,7,1], adding -5 * -10 = 50 to the score.\n- Choose from the start, [-3,-3,-2,7,1], adding -3 * -5 = 15 to the score.\n- Choose from the start, [-3,-2,7,1], adding -3 * 3 = -9 to the score.\n- Choose from the end, [-2,7,1], adding 1 * 4 = 4 to the score.\n- Choose from the end, [-2,7], adding 7 * 6 = 42 to the score. \nThe total score is 50 + 15 - 9 + 4 + 42 = 102.\n\n\n
\n
Constraints:
\n\nn == nums.length
m == multipliers.length
1 <= m <= 103
m <= n <= 105
-1000 <= nums[i], multipliers[i] <= 1000
\u7ed9\u4f60\u4e24\u4e2a\u957f\u5ea6\u5206\u522b n
\u548c m
\u7684\u6574\u6570\u6570\u7ec4 nums
\u548c multipliers
\uff0c\u5176\u4e2d n >= m
\uff0c\u6570\u7ec4\u4e0b\u6807 \u4ece 1 \u5f00\u59cb \u8ba1\u6570\u3002
\u521d\u59cb\u65f6\uff0c\u4f60\u7684\u5206\u6570\u4e3a 0
\u3002\u4f60\u9700\u8981\u6267\u884c\u6070\u597d m
\u6b65\u64cd\u4f5c\u3002\u5728\u7b2c i
\u6b65\u64cd\u4f5c\uff08\u4ece 1 \u5f00\u59cb \u8ba1\u6570\uff09\u4e2d\uff0c\u9700\u8981\uff1a
nums
\u5f00\u5934\u5904\u6216\u8005\u672b\u5c3e\u5904 \u7684\u6574\u6570 x
\u3002multipliers[i] * x
\u5206\uff0c\u5e76\u7d2f\u52a0\u5230\u4f60\u7684\u5206\u6570\u4e2d\u3002x
\u4ece\u6570\u7ec4 nums
\u4e2d\u79fb\u9664\u3002\u5728\u6267\u884c m
\u6b65\u64cd\u4f5c\u540e\uff0c\u8fd4\u56de \u6700\u5927 \u5206\u6570\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1anums = [1,2,3], multipliers = [3,2,1]\n\u8f93\u51fa\uff1a14\n\u89e3\u91ca\uff1a\u4e00\u79cd\u6700\u4f18\u89e3\u51b3\u65b9\u6848\u5982\u4e0b\uff1a\n- \u9009\u62e9\u672b\u5c3e\u5904\u7684\u6574\u6570 3 \uff0c[1,2,3] \uff0c\u5f97 3 * 3 = 9 \u5206\uff0c\u7d2f\u52a0\u5230\u5206\u6570\u4e2d\u3002\n- \u9009\u62e9\u672b\u5c3e\u5904\u7684\u6574\u6570 2 \uff0c[1,2] \uff0c\u5f97 2 * 2 = 4 \u5206\uff0c\u7d2f\u52a0\u5230\u5206\u6570\u4e2d\u3002\n- \u9009\u62e9\u672b\u5c3e\u5904\u7684\u6574\u6570 1 \uff0c[1] \uff0c\u5f97 1 * 1 = 1 \u5206\uff0c\u7d2f\u52a0\u5230\u5206\u6570\u4e2d\u3002\n\u603b\u5206\u6570\u4e3a 9 + 4 + 1 = 14 \u3002\n\n
\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1anums = [-5,-3,-3,-2,7,1], multipliers = [-10,-5,3,4,6]\n\u8f93\u51fa\uff1a102\n\u89e3\u91ca\uff1a\u4e00\u79cd\u6700\u4f18\u89e3\u51b3\u65b9\u6848\u5982\u4e0b\uff1a\n- \u9009\u62e9\u5f00\u5934\u5904\u7684\u6574\u6570 -5 \uff0c[-5,-3,-3,-2,7,1] \uff0c\u5f97 -5 * -10 = 50 \u5206\uff0c\u7d2f\u52a0\u5230\u5206\u6570\u4e2d\u3002\n- \u9009\u62e9\u5f00\u5934\u5904\u7684\u6574\u6570 -3 \uff0c[-3,-3,-2,7,1] \uff0c\u5f97 -3 * -5 = 15 \u5206\uff0c\u7d2f\u52a0\u5230\u5206\u6570\u4e2d\u3002\n- \u9009\u62e9\u5f00\u5934\u5904\u7684\u6574\u6570 -3 \uff0c[-3,-2,7,1] \uff0c\u5f97 -3 * 3 = -9 \u5206\uff0c\u7d2f\u52a0\u5230\u5206\u6570\u4e2d\u3002\n- \u9009\u62e9\u672b\u5c3e\u5904\u7684\u6574\u6570 1 \uff0c[-2,7,1] \uff0c\u5f97 1 * 4 = 4 \u5206\uff0c\u7d2f\u52a0\u5230\u5206\u6570\u4e2d\u3002\n- \u9009\u62e9\u672b\u5c3e\u5904\u7684\u6574\u6570 7 \uff0c[-2,7] \uff0c\u5f97 7 * 6 = 42 \u5206\uff0c\u7d2f\u52a0\u5230\u5206\u6570\u4e2d\u3002\n\u603b\u5206\u6570\u4e3a 50 + 15 - 9 + 4 + 42 = 102 \u3002\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\nn == nums.length
m == multipliers.length
1 <= m <= 103
m <= n <= 105
-1000 <= nums[i], multipliers[i] <= 1000
You have n
boxes. You are given a binary string boxes
of length n
, where boxes[i]
is '0'
if the ith
box is empty, and '1'
if it contains one ball.
In one operation, you can move one ball from a box to an adjacent box. Box i
is adjacent to box j
if abs(i - j) == 1
. Note that after doing so, there may be more than one ball in some boxes.
Return an array answer
of size n
, where answer[i]
is the minimum number of operations needed to move all the balls to the ith
box.
Each answer[i]
is calculated considering the initial state of the boxes.
\n
Example 1:
\n\n\nInput: boxes = "110"\nOutput: [1,1,3]\nExplanation: The answer for each box is as follows:\n1) First box: you will have to move one ball from the second box to the first box in one operation.\n2) Second box: you will have to move one ball from the first box to the second box in one operation.\n3) Third box: you will have to move one ball from the first box to the third box in two operations, and move one ball from the second box to the third box in one operation.\n\n\n
Example 2:
\n\n\nInput: boxes = "001011"\nOutput: [11,8,5,4,3,4]\n\n
\n
Constraints:
\n\nn == boxes.length
1 <= n <= 2000
boxes[i]
is either '0'
or '1'
.\u6709 n
\u4e2a\u76d2\u5b50\u3002\u7ed9\u4f60\u4e00\u4e2a\u957f\u5ea6\u4e3a n
\u7684\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32 boxes
\uff0c\u5176\u4e2d boxes[i]
\u7684\u503c\u4e3a '0'
\u8868\u793a\u7b2c i
\u4e2a\u76d2\u5b50\u662f \u7a7a \u7684\uff0c\u800c boxes[i]
\u7684\u503c\u4e3a '1'
\u8868\u793a\u76d2\u5b50\u91cc\u6709 \u4e00\u4e2a \u5c0f\u7403\u3002
\u5728\u4e00\u6b65\u64cd\u4f5c\u4e2d\uff0c\u4f60\u53ef\u4ee5\u5c06 \u4e00\u4e2a \u5c0f\u7403\u4ece\u67d0\u4e2a\u76d2\u5b50\u79fb\u52a8\u5230\u4e00\u4e2a\u4e0e\u4e4b\u76f8\u90bb\u7684\u76d2\u5b50\u4e2d\u3002\u7b2c i
\u4e2a\u76d2\u5b50\u548c\u7b2c j
\u4e2a\u76d2\u5b50\u76f8\u90bb\u9700\u6ee1\u8db3 abs(i - j) == 1
\u3002\u6ce8\u610f\uff0c\u64cd\u4f5c\u6267\u884c\u540e\uff0c\u67d0\u4e9b\u76d2\u5b50\u4e2d\u53ef\u80fd\u4f1a\u5b58\u5728\u4e0d\u6b62\u4e00\u4e2a\u5c0f\u7403\u3002
\u8fd4\u56de\u4e00\u4e2a\u957f\u5ea6\u4e3a n
\u7684\u6570\u7ec4 answer
\uff0c\u5176\u4e2d answer[i]
\u662f\u5c06\u6240\u6709\u5c0f\u7403\u79fb\u52a8\u5230\u7b2c i
\u4e2a\u76d2\u5b50\u6240\u9700\u7684 \u6700\u5c0f \u64cd\u4f5c\u6570\u3002
\u6bcf\u4e2a answer[i]
\u90fd\u9700\u8981\u6839\u636e\u76d2\u5b50\u7684 \u521d\u59cb\u72b6\u6001 \u8fdb\u884c\u8ba1\u7b97\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1aboxes = \"110\"\n\u8f93\u51fa\uff1a[1,1,3]\n\u89e3\u91ca\uff1a\u6bcf\u4e2a\u76d2\u5b50\u5bf9\u5e94\u7684\u6700\u5c0f\u64cd\u4f5c\u6570\u5982\u4e0b\uff1a\n1) \u7b2c 1 \u4e2a\u76d2\u5b50\uff1a\u5c06\u4e00\u4e2a\u5c0f\u7403\u4ece\u7b2c 2 \u4e2a\u76d2\u5b50\u79fb\u52a8\u5230\u7b2c 1 \u4e2a\u76d2\u5b50\uff0c\u9700\u8981 1 \u6b65\u64cd\u4f5c\u3002\n2) \u7b2c 2 \u4e2a\u76d2\u5b50\uff1a\u5c06\u4e00\u4e2a\u5c0f\u7403\u4ece\u7b2c 1 \u4e2a\u76d2\u5b50\u79fb\u52a8\u5230\u7b2c 2 \u4e2a\u76d2\u5b50\uff0c\u9700\u8981 1 \u6b65\u64cd\u4f5c\u3002\n3) \u7b2c 3 \u4e2a\u76d2\u5b50\uff1a\u5c06\u4e00\u4e2a\u5c0f\u7403\u4ece\u7b2c 1 \u4e2a\u76d2\u5b50\u79fb\u52a8\u5230\u7b2c 3 \u4e2a\u76d2\u5b50\uff0c\u9700\u8981 2 \u6b65\u64cd\u4f5c\u3002\u5c06\u4e00\u4e2a\u5c0f\u7403\u4ece\u7b2c 2 \u4e2a\u76d2\u5b50\u79fb\u52a8\u5230\u7b2c 3 \u4e2a\u76d2\u5b50\uff0c\u9700\u8981 1 \u6b65\u64cd\u4f5c\u3002\u5171\u8ba1 3 \u6b65\u64cd\u4f5c\u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1aboxes = \"001011\"\n\u8f93\u51fa\uff1a[11,8,5,4,3,4]\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\nn == boxes.length
1 <= n <= 2000
boxes[i]
\u4e3a '0'
\u6216 '1'
You are given two strings word1
and word2
. Merge the strings by adding letters in alternating order, starting with word1
. If a string is longer than the other, append the additional letters onto the end of the merged string.
Return the merged string.
\r\n\r\n\r\n
Example 1:
\r\n\r\n\r\nInput: word1 = "abc", word2 = "pqr"\r\nOutput: "apbqcr"\r\nExplanation: The merged string will be merged as so:\r\nword1: a b c\r\nword2: p q r\r\nmerged: a p b q c r\r\n\r\n\r\n
Example 2:
\r\n\r\n\r\nInput: word1 = "ab", word2 = "pqrs"\r\nOutput: "apbqrs"\r\nExplanation: Notice that as word2 is longer, "rs" is appended to the end.\r\nword1: a b \r\nword2: p q r s\r\nmerged: a p b q r s\r\n\r\n\r\n
Example 3:
\r\n\r\n\r\nInput: word1 = "abcd", word2 = "pq"\r\nOutput: "apbqcd"\r\nExplanation: Notice that as word1 is longer, "cd" is appended to the end.\r\nword1: a b c d\r\nword2: p q \r\nmerged: a p b q c d\r\n\r\n\r\n
\r\n
Constraints:
\r\n\r\n1 <= word1.length, word2.length <= 100
word1
and word2
consist of lowercase English letters.\u7ed9\u4f60\u4e24\u4e2a\u5b57\u7b26\u4e32 word1
\u548c word2
\u3002\u8bf7\u4f60\u4ece word1
\u5f00\u59cb\uff0c\u901a\u8fc7\u4ea4\u66ff\u6dfb\u52a0\u5b57\u6bcd\u6765\u5408\u5e76\u5b57\u7b26\u4e32\u3002\u5982\u679c\u4e00\u4e2a\u5b57\u7b26\u4e32\u6bd4\u53e6\u4e00\u4e2a\u5b57\u7b26\u4e32\u957f\uff0c\u5c31\u5c06\u591a\u51fa\u6765\u7684\u5b57\u6bcd\u8ffd\u52a0\u5230\u5408\u5e76\u540e\u5b57\u7b26\u4e32\u7684\u672b\u5c3e\u3002
\u8fd4\u56de \u5408\u5e76\u540e\u7684\u5b57\u7b26\u4e32 \u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1aword1 = \"abc\", word2 = \"pqr\"\n\u8f93\u51fa\uff1a\"apbqcr\"\n\u89e3\u91ca\uff1a\u5b57\u7b26\u4e32\u5408\u5e76\u60c5\u51b5\u5982\u4e0b\u6240\u793a\uff1a\nword1\uff1a a b c\nword2\uff1a p q r\n\u5408\u5e76\u540e\uff1a a p b q c r\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1aword1 = \"ab\", word2 = \"pqrs\"\n\u8f93\u51fa\uff1a\"apbqrs\"\n\u89e3\u91ca\uff1a\u6ce8\u610f\uff0cword2 \u6bd4 word1 \u957f\uff0c\"rs\" \u9700\u8981\u8ffd\u52a0\u5230\u5408\u5e76\u540e\u5b57\u7b26\u4e32\u7684\u672b\u5c3e\u3002\nword1\uff1a a b \nword2\uff1a p q r s\n\u5408\u5e76\u540e\uff1a a p b q r s\n\n\n
\u793a\u4f8b 3\uff1a
\n\n\n\u8f93\u5165\uff1aword1 = \"abcd\", word2 = \"pq\"\n\u8f93\u51fa\uff1a\"apbqcd\"\n\u89e3\u91ca\uff1a\u6ce8\u610f\uff0cword1 \u6bd4 word2 \u957f\uff0c\"cd\" \u9700\u8981\u8ffd\u52a0\u5230\u5408\u5e76\u540e\u5b57\u7b26\u4e32\u7684\u672b\u5c3e\u3002\nword1\uff1a a b c d\nword2\uff1a p q \n\u5408\u5e76\u540e\uff1a a p b q c d\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= word1.length, word2.length <= 100
word1
\u548c word2
\u7531\u5c0f\u5199\u82f1\u6587\u5b57\u6bcd\u7ec4\u6210You are given an integer array nums
. You must perform exactly one operation where you can replace one element nums[i]
with nums[i] * nums[i]
.
Return the maximum possible subarray sum after exactly one operation. The subarray must be non-empty.
\r\n\r\n\r\n
Example 1:
\r\n\r\n\r\nInput: nums = [2,-1,-4,-3]\r\nOutput: 17\r\nExplanation: You can perform the operation on index 2 (0-indexed) to make nums = [2,-1,16,-3]. Now, the maximum subarray sum is 2 + -1 + 16 = 17.\r\n\r\n
Example 2:
\r\n\r\n\r\nInput: nums = [1,-1,1,1,-1,-1,1]\r\nOutput: 4\r\nExplanation: You can perform the operation on index 1 (0-indexed) to make nums = [1,1,1,1,-1,-1,1]. Now, the maximum subarray sum is 1 + 1 + 1 + 1 = 4.\r\n\r\n
\r\n
Constraints:
\r\n\r\n1 <= nums.length <= 105
-104 <= nums[i] <= 104
\u4f60\u6709\u4e00\u4e2a\u6574\u6570\u6570\u7ec4\u00a0nums
\u3002\u4f60\u53ea\u80fd\u5c06\u4e00\u4e2a\u5143\u7d20\u00a0nums[i]
\u66ff\u6362\u4e3a\u00a0nums[i] * nums[i]
\u3002
\u8fd4\u56de\u66ff\u6362\u540e\u7684\u6700\u5927\u5b50\u6570\u7ec4\u548c\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1anums = [2,-1,-4,-3]\n\u8f93\u51fa\uff1a17\n\u89e3\u91ca\uff1a\u4f60\u53ef\u4ee5\u628a-4\u66ff\u6362\u4e3a16(-4*(-4))\uff0c\u4f7fnums = [2,-1,16,-3]. \u73b0\u5728\uff0c\u6700\u5927\u5b50\u6570\u7ec4\u548c\u4e3a 2 + -1 + 16 = 17.\n\n
\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1anums = [1,-1,1,1,-1,-1,1]\n\u8f93\u51fa\uff1a4\n\u89e3\u91ca\uff1a\u4f60\u53ef\u4ee5\u628a\u7b2c\u4e00\u4e2a-1\u66ff\u6362\u4e3a1\uff0c\u4f7f nums = [1,1,1,1,-1,-1,1]. \u73b0\u5728\uff0c\u6700\u5927\u5b50\u6570\u7ec4\u548c\u4e3a 1 + 1 + 1 + 1 = 4.\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= nums.length <= 105
-104\u00a0<= nums[i] <= 104
Table: Employees
\n+-------------+------+\n| Column Name | Type |\n+-------------+------+\n| emp_id | int |\n| event_day | date |\n| in_time | int |\n| out_time | int |\n+-------------+------+\n(emp_id, event_day, in_time) is the primary key of this table.\nThe table shows the employees' entries and exits in an office.\nevent_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.\nin_time and out_time are between 1 and 1440.\nIt is guaranteed that no two events on the same day intersect in time, and in_time < out_time.\n\n\n
\n\n
Write an SQL query to calculate the total time in minutes spent by each employee on each day at the office. Note that within one day, an employee can enter and leave more than once. The time spent in the office for a single entry is out_time - in_time
.
Return the result table in any order.
\n\nThe query result format is in the following example:
\n\n\nEmployees table:\n+--------+------------+---------+----------+\n| emp_id | event_day | in_time | out_time |\n+--------+------------+---------+----------+\n| 1 | 2020-11-28 | 4 | 32 |\n| 1 | 2020-11-28 | 55 | 200 |\n| 1 | 2020-12-03 | 1 | 42 |\n| 2 | 2020-11-28 | 3 | 33 |\n| 2 | 2020-12-09 | 47 | 74 |\n+--------+------------+---------+----------+\nResult table:\n+------------+--------+------------+\n| day | emp_id | total_time |\n+------------+--------+------------+\n| 2020-11-28 | 1 | 173 |\n| 2020-11-28 | 2 | 30 |\n| 2020-12-03 | 1 | 41 |\n| 2020-12-09 | 2 | 27 |\n+------------+--------+------------+\nEmployee 1 has three events: two on day 2020-11-28 with a total of (32 - 4) + (200 - 55) = 173, and one on day 2020-12-03 with a total of (42 - 1) = 41.\nEmployee 2 has two events: one on day 2020-11-28 with a total of (33 - 3) = 30, and one on day 2020-12-09 with a total of (74 - 47) = 27.\n\n", "content_cn": "
\u8868: Employees
+-------------+------+\n| Column Name | Type |\n+-------------+------+\n| emp_id | int |\n| event_day | date |\n| in_time | int |\n| out_time | int |\n+-------------+------+\n(emp_id, event_day, in_time) \u662f\u8fd9\u4e2a\u8868\u7684\u4e3b\u952e\u3002\n\u8be5\u8868\u663e\u793a\u4e86\u5458\u5de5\u5728\u529e\u516c\u5ba4\u7684\u51fa\u5165\u60c5\u51b5\u3002\nevent_day \u662f\u6b64\u4e8b\u4ef6\u53d1\u751f\u7684\u65e5\u671f\uff0cin_time \u662f\u5458\u5de5\u8fdb\u5165\u529e\u516c\u5ba4\u7684\u65f6\u95f4\uff0c\u800c out_time \u662f\u4ed6\u4eec\u79bb\u5f00\u529e\u516c\u5ba4\u7684\u65f6\u95f4\u3002\nin_time \u548c out_time \u7684\u53d6\u503c\u57281\u52301440\u4e4b\u95f4\u3002\n\u9898\u76ee\u4fdd\u8bc1\u540c\u4e00\u5929\u6ca1\u6709\u4e24\u4e2a\u4e8b\u4ef6\u5728\u65f6\u95f4\u4e0a\u662f\u76f8\u4ea4\u7684\uff0c\u5e76\u4e14\u4fdd\u8bc1 in_time \u5c0f\u4e8e out_time\u3002\n\n\n
\u00a0
\n\n\u7f16\u5199\u4e00\u4e2aSQL\u67e5\u8be2\u4ee5\u8ba1\u7b97\u6bcf\u4f4d\u5458\u5de5\u6bcf\u5929\u5728\u529e\u516c\u5ba4\u82b1\u8d39\u7684\u603b\u65f6\u95f4\uff08\u4ee5\u5206\u949f\u4e3a\u5355\u4f4d\uff09\u3002 \u8bf7\u6ce8\u610f\uff0c\u5728\u4e00\u5929\u4e4b\u5185\uff0c\u540c\u4e00\u5458\u5de5\u662f\u53ef\u4ee5\u591a\u6b21\u8fdb\u5165\u548c\u79bb\u5f00\u529e\u516c\u5ba4\u7684\u3002 \u5728\u529e\u516c\u5ba4\u91cc\u4e00\u6b21\u8fdb\u51fa\u6240\u82b1\u8d39\u7684\u65f6\u95f4\u4e3aout_time \u51cf\u53bb in_time\u3002
\n\n\u8fd4\u56de\u7ed3\u679c\u8868\u5355\u7684\u987a\u5e8f\u65e0\u8981\u6c42\u3002
\n\u67e5\u8be2\u7ed3\u679c\u7684\u683c\u5f0f\u5982\u4e0b\uff1a
Employees table:\n+--------+------------+---------+----------+\n| emp_id | event_day | in_time | out_time |\n+--------+------------+---------+----------+\n| 1 | 2020-11-28 | 4 | 32 |\n| 1 | 2020-11-28 | 55 | 200 |\n| 1 | 2020-12-03 | 1 | 42 |\n| 2 | 2020-11-28 | 3 | 33 |\n| 2 | 2020-12-09 | 47 | 74 |\n+--------+------------+---------+----------+\nResult table:\n+------------+--------+------------+\n| day | emp_id | total_time |\n+------------+--------+------------+\n| 2020-11-28 | 1 | 173 |\n| 2020-11-28 | 2 | 30 |\n| 2020-12-03 | 1 | 41 |\n| 2020-12-09 | 2 | 27 |\n+------------+--------+------------+\n\u96c7\u5458 1 \u6709\u4e09\u6b21\u8fdb\u51fa: \u6709\u4e24\u6b21\u53d1\u751f\u5728 2020-11-28 \u82b1\u8d39\u7684\u65f6\u95f4\u4e3a (32 - 4) + (200 - 55) = 173, \u6709\u4e00\u6b21\u53d1\u751f\u5728 2020-12-03 \u82b1\u8d39\u7684\u65f6\u95f4\u4e3a (42 - 1) = 41\u3002\n\u96c7\u5458 2 \u6709\u4e24\u6b21\u8fdb\u51fa: \u6709\u4e00\u6b21\u53d1\u751f\u5728 2020-11-28 \u82b1\u8d39\u7684\u65f6\u95f4\u4e3a (33 - 3) = 30, \u6709\u4e00\u6b21\u53d1\u751f\u5728 2020-12-09 \u82b1\u8d39\u7684\u65f6\u95f4\u4e3a (74 - 47) = 27\u3002\n\n", "tags_en": [], "tags_cn": [], "difficulty_en": "Easy", "difficulty_cn": "\u7b80\u5355", "code_snippets": [{"lang": "MySQL", "langSlug": "mysql", "code": "# Write your MySQL query statement below", "__typename": "CodeSnippetNode"}, {"lang": "MS SQL Server", "langSlug": "mssql", "code": "/* Write your T-SQL query statement below */", "__typename": "CodeSnippetNode"}, {"lang": "Oracle", "langSlug": "oraclesql", "code": "/* Write your PL/SQL query statement below */", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1741](https://leetcode-cn.com/problems/find-total-time-spent-by-each-employee)", "[\u67e5\u627e\u6bcf\u4e2a\u5458\u5de5\u82b1\u8d39\u7684\u603b\u65f6\u95f4](/solution/1700-1799/1741.Find%20Total%20Time%20Spent%20by%20Each%20Employee/README.md)", "", "\u7b80\u5355", "\ud83d\udd12"], "md_table_row_en": ["[1741](https://leetcode.com/problems/find-total-time-spent-by-each-employee)", "[Find Total Time Spent by Each Employee](/solution/1700-1799/1741.Find%20Total%20Time%20Spent%20by%20Each%20Employee/README_EN.md)", "", "Easy", "\ud83d\udd12"]}, {"question_id": "1891", "frontend_question_id": "1782", "paid_only": false, "paid_only_cn": false, "url_cn": "https://leetcode-cn.com/problems/count-pairs-of-nodes", "url_en": "https://leetcode.com/problems/count-pairs-of-nodes", "relative_path_cn": "/solution/1700-1799/1782.Count%20Pairs%20Of%20Nodes/README.md", "relative_path_en": "/solution/1700-1799/1782.Count%20Pairs%20Of%20Nodes/README_EN.md", "title_cn": "\u7edf\u8ba1\u70b9\u5bf9\u7684\u6570\u76ee", "title_en": "Count Pairs Of Nodes", "question_title_slug": "count-pairs-of-nodes", "content_en": "
You are given an undirected graph represented by an integer n
, which is the number of nodes, and edges
, where edges[i] = [ui, vi]
which indicates that there is an undirected edge between ui
and vi
. You are also given an integer array queries
.
The answer to the jth
query is the number of pairs of nodes (a, b)
that satisfy the following conditions:
a < b
cnt
is strictly greater than queries[j]
, where cnt
is the number of edges incident to a
or b
.Return an array answers
such that answers.length == queries.length
and answers[j]
is the answer of the jth
query.
Note that there can be repeated edges.
\n\n\n
Example 1:
\n\nInput: n = 4, edges = [[1,2],[2,4],[1,3],[2,3],[2,1]], queries = [2,3]\nOutput: [6,5]\nExplanation: The number of edges incident to at least one of each pair is shown above.\n\n\n
Example 2:
\n\n\nInput: n = 5, edges = [[1,5],[1,5],[3,4],[2,5],[1,3],[5,1],[2,3],[2,5]], queries = [1,2,3,4,5]\nOutput: [10,10,9,8,6]\n\n\n
\n
Constraints:
\n\n2 <= n <= 2 * 104
1 <= edges.length <= 105
1 <= ui, vi <= n
ui != vi
1 <= queries.length <= 20
0 <= queries[j] < edges.length
\u7ed9\u4f60\u4e00\u4e2a\u65e0\u5411\u56fe\uff0c\u65e0\u5411\u56fe\u7531\u6574\u6570\u00a0n
\u00a0\u00a0\uff0c\u8868\u793a\u56fe\u4e2d\u8282\u70b9\u7684\u6570\u76ee\uff0c\u548c\u00a0edges
\u00a0\u7ec4\u6210\uff0c\u5176\u4e2d\u00a0edges[i] = [ui, vi]
\u00a0\u8868\u793a\u00a0ui
\u548c\u00a0vi
\u00a0\u4e4b\u95f4\u6709\u4e00\u6761\u65e0\u5411\u8fb9\u3002\u540c\u65f6\u7ed9\u4f60\u4e00\u4e2a\u4ee3\u8868\u67e5\u8be2\u7684\u6574\u6570\u6570\u7ec4\u00a0queries
\u00a0\u3002
\u7b2c j
\u4e2a\u67e5\u8be2\u7684\u7b54\u6848\u662f\u6ee1\u8db3\u5982\u4e0b\u6761\u4ef6\u7684\u70b9\u5bf9 (a, b)
\u7684\u6570\u76ee\uff1a
a < b
cnt
\u00a0\u662f\u4e0e a
\u00a0\u6216\u8005\u00a0b
\u00a0\u76f8\u8fde\u7684\u8fb9\u7684\u6570\u76ee\uff0c\u4e14 cnt
\u00a0\u4e25\u683c\u5927\u4e8e\u00a0queries[j]
\u00a0\u3002\u8bf7\u4f60\u8fd4\u56de\u4e00\u4e2a\u6570\u7ec4\u00a0answers
\u00a0\uff0c\u5176\u4e2d\u00a0answers.length == queries.length
\u4e14\u00a0answers[j]
\u00a0\u662f\u7b2c j
\u00a0\u4e2a\u67e5\u8be2\u7684\u7b54\u6848\u3002
\u8bf7\u6ce8\u610f\uff0c\u56fe\u4e2d\u53ef\u80fd\u4f1a\u6709 \u91cd\u590d\u8fb9\u00a0\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1an = 4, edges = [[1,2],[2,4],[1,3],[2,3],[2,1]], queries = [2,3]\n\u8f93\u51fa\uff1a[6,5]\n\u89e3\u91ca\uff1a\u6bcf\u4e2a\u70b9\u5bf9\u4e2d\uff0c\u4e0e\u81f3\u5c11\u4e00\u4e2a\u70b9\u76f8\u8fde\u7684\u8fb9\u7684\u6570\u76ee\u5982\u4e0a\u56fe\u6240\u793a\u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1an = 5, edges = [[1,5],[1,5],[3,4],[2,5],[1,3],[5,1],[2,3],[2,5]], queries = [1,2,3,4,5]\n\u8f93\u51fa\uff1a[10,10,9,8,6]\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n2 <= n <= 2 * 104
1 <= edges.length <= 105
1 <= ui, vi <= n
ui != vi
1 <= queries.length <= 20
0 <= queries[j] < edges.length
The beauty of a string is the difference in frequencies between the most frequent and least frequent characters.
\n\n"abaacc"
is 3 - 1 = 2
.Given a string s
, return the sum of beauty of all of its substrings.
\n
Example 1:
\n\n\nInput: s = "aabcb"\nOutput: 5\nExplanation: The substrings with non-zero beauty are ["aab","aabc","aabcb","abcb","bcb"], each with beauty equal to 1.\n\n
Example 2:
\n\n\nInput: s = "aabcbaa"\nOutput: 17\n\n\n
\n
Constraints:
\n\n1 <= s.length <= 500
s
consists of only lowercase English letters.\u4e00\u4e2a\u5b57\u7b26\u4e32\u7684 \u7f8e\u4e3d\u503c\u00a0\u5b9a\u4e49\u4e3a\uff1a\u51fa\u73b0\u9891\u7387\u6700\u9ad8\u5b57\u7b26\u4e0e\u51fa\u73b0\u9891\u7387\u6700\u4f4e\u5b57\u7b26\u7684\u51fa\u73b0\u6b21\u6570\u4e4b\u5dee\u3002
\n\n\"abaacc\"
\u00a0\u7684\u7f8e\u4e3d\u503c\u4e3a\u00a03 - 1 = 2
\u00a0\u3002\u7ed9\u4f60\u4e00\u4e2a\u5b57\u7b26\u4e32\u00a0s
\u00a0\uff0c\u8bf7\u4f60\u8fd4\u56de\u5b83\u6240\u6709\u5b50\u5b57\u7b26\u4e32\u7684\u00a0\u7f8e\u4e3d\u503c\u00a0\u4e4b\u548c\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1as = \"aabcb\"\n\u8f93\u51fa\uff1a5\n\u89e3\u91ca\uff1a\u7f8e\u4e3d\u503c\u4e0d\u4e3a\u96f6\u7684\u5b57\u7b26\u4e32\u5305\u62ec [\"aab\",\"aabc\",\"aabcb\",\"abcb\",\"bcb\"] \uff0c\u6bcf\u4e00\u4e2a\u5b57\u7b26\u4e32\u7684\u7f8e\u4e3d\u503c\u90fd\u4e3a 1 \u3002\n\n
\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1as = \"aabcbaa\"\n\u8f93\u51fa\uff1a17\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= s.length <= 500
s
\u00a0\u53ea\u5305\u542b\u5c0f\u5199\u82f1\u6587\u5b57\u6bcd\u3002Given an integer n
, return true
if it is possible to represent n
as the sum of distinct powers of three. Otherwise, return false
.
An integer y
is a power of three if there exists an integer x
such that y == 3x
.
\n
Example 1:
\n\n\nInput: n = 12\nOutput: true\nExplanation: 12 = 31 + 32\n\n\n
Example 2:
\n\n\nInput: n = 91\nOutput: true\nExplanation: 91 = 30 + 32 + 34\n\n\n
Example 3:
\n\n\nInput: n = 21\nOutput: false\n\n\n
\n
Constraints:
\n\n1 <= n <= 107
\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u00a0n
\u00a0\uff0c\u5982\u679c\u4f60\u53ef\u4ee5\u5c06\u00a0n
\u00a0\u8868\u793a\u6210\u82e5\u5e72\u4e2a\u4e0d\u540c\u7684\u4e09\u7684\u5e42\u4e4b\u548c\uff0c\u8bf7\u4f60\u8fd4\u56de\u00a0true
\u00a0\uff0c\u5426\u5219\u8bf7\u8fd4\u56de false
\u00a0\u3002
\u5bf9\u4e8e\u4e00\u4e2a\u6574\u6570 y
\u00a0\uff0c\u5982\u679c\u5b58\u5728\u6574\u6570 x
\u00a0\u6ee1\u8db3 y == 3x
\u00a0\uff0c\u6211\u4eec\u79f0\u8fd9\u4e2a\u6574\u6570 y
\u00a0\u662f\u4e09\u7684\u5e42\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1an = 12\n\u8f93\u51fa\uff1atrue\n\u89e3\u91ca\uff1a12 = 31 + 32\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1an = 91\n\u8f93\u51fa\uff1atrue\n\u89e3\u91ca\uff1a91 = 30 + 32 + 34\n\n\n
\u793a\u4f8b 3\uff1a
\n\n\u8f93\u5165\uff1an = 21\n\u8f93\u51fa\uff1afalse\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= n <= 107
You are given two integers, x
and y
, which represent your current location on a Cartesian grid: (x, y)
. You are also given an array points
where each points[i] = [ai, bi]
represents that a point exists at (ai, bi)
. A point is valid if it shares the same x-coordinate or the same y-coordinate as your location.
Return the index (0-indexed) of the valid point with the smallest Manhattan distance from your current location. If there are multiple, return the valid point with the smallest index. If there are no valid points, return -1
.
The Manhattan distance between two points (x1, y1)
and (x2, y2)
is abs(x1 - x2) + abs(y1 - y2)
.
\n
Example 1:
\n\n\nInput: x = 3, y = 4, points = [[1,2],[3,1],[2,4],[2,3],[4,4]]\nOutput: 2\nExplanation: Of all the points, only [3,1], [2,4] and [4,4] are valid. Of the valid points, [2,4] and [4,4] have the smallest Manhattan distance from your current location, with a distance of 1. [2,4] has the smallest index, so return 2.\n\n
Example 2:
\n\n\nInput: x = 3, y = 4, points = [[3,4]]\nOutput: 0\nExplanation: The answer is allowed to be on the same location as your current location.\n\n
Example 3:
\n\n\nInput: x = 3, y = 4, points = [[2,3]]\nOutput: -1\nExplanation: There are no valid points.\n\n
\n
Constraints:
\n\n1 <= points.length <= 104
points[i].length == 2
1 <= x, y, ai, bi <= 104
\u7ed9\u4f60\u4e24\u4e2a\u6574\u6570\u00a0x
\u548c\u00a0y
\u00a0\uff0c\u8868\u793a\u4f60\u5728\u4e00\u4e2a\u7b1b\u5361\u5c14\u5750\u6807\u7cfb\u4e0b\u7684\u00a0(x, y)
\u00a0\u5904\u3002\u540c\u65f6\uff0c\u5728\u540c\u4e00\u4e2a\u5750\u6807\u7cfb\u4e0b\u7ed9\u4f60\u4e00\u4e2a\u6570\u7ec4\u00a0points
\u00a0\uff0c\u5176\u4e2d\u00a0points[i] = [ai, bi]
\u00a0\u8868\u793a\u5728\u00a0(ai, bi)
\u00a0\u5904\u6709\u4e00\u4e2a\u70b9\u3002\u5f53\u4e00\u4e2a\u70b9\u4e0e\u4f60\u6240\u5728\u7684\u4f4d\u7f6e\u6709\u76f8\u540c\u7684 x \u5750\u6807\u6216\u8005\u76f8\u540c\u7684 y \u5750\u6807\u65f6\uff0c\u6211\u4eec\u79f0\u8fd9\u4e2a\u70b9\u662f \u6709\u6548\u7684\u00a0\u3002
\u8bf7\u8fd4\u56de\u8ddd\u79bb\u4f60\u5f53\u524d\u4f4d\u7f6e\u00a0\u66fc\u54c8\u987f\u8ddd\u79bb\u00a0\u6700\u8fd1\u7684\u00a0\u6709\u6548\u00a0\u70b9\u7684\u4e0b\u6807\uff08\u4e0b\u6807\u4ece 0 \u5f00\u59cb\uff09\u3002\u5982\u679c\u6709\u591a\u4e2a\u6700\u8fd1\u7684\u6709\u6548\u70b9\uff0c\u8bf7\u8fd4\u56de\u4e0b\u6807\u00a0\u6700\u5c0f\u00a0\u7684\u4e00\u4e2a\u3002\u5982\u679c\u6ca1\u6709\u6709\u6548\u70b9\uff0c\u8bf7\u8fd4\u56de\u00a0-1
\u00a0\u3002
\u4e24\u4e2a\u70b9 (x1, y1)
\u00a0\u548c (x2, y2)
\u00a0\u4e4b\u95f4\u7684 \u66fc\u54c8\u987f\u8ddd\u79bb\u00a0\u4e3a\u00a0abs(x1 - x2) + abs(y1 - y2)
\u00a0\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1ax = 3, y = 4, points = [[1,2],[3,1],[2,4],[2,3],[4,4]]\n\u8f93\u51fa\uff1a2\n\u89e3\u91ca\uff1a\u6240\u6709\u70b9\u4e2d\uff0c[3,1]\uff0c[2,4] \u548c [4,4] \u662f\u6709\u6548\u70b9\u3002\u6709\u6548\u70b9\u4e2d\uff0c[2,4] \u548c [4,4] \u8ddd\u79bb\u4f60\u5f53\u524d\u4f4d\u7f6e\u7684\u66fc\u54c8\u987f\u8ddd\u79bb\u6700\u5c0f\uff0c\u90fd\u4e3a 1 \u3002[2,4] \u7684\u4e0b\u6807\u6700\u5c0f\uff0c\u6240\u4ee5\u8fd4\u56de 2 \u3002\n\n
\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1ax = 3, y = 4, points = [[3,4]]\n\u8f93\u51fa\uff1a0\n\u63d0\u793a\uff1a\u7b54\u6848\u53ef\u4ee5\u4e0e\u4f60\u5f53\u524d\u6240\u5728\u4f4d\u7f6e\u5750\u6807\u76f8\u540c\u3002\n\n
\u793a\u4f8b 3\uff1a
\n\n\u8f93\u5165\uff1ax = 3, y = 4, points = [[2,3]]\n\u8f93\u51fa\uff1a-1\n\u89e3\u91ca\uff1a\u6ca1\u6709\u6709\u6548\u70b9\u3002\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= points.length <= 104
points[i].length == 2
1 <= x, y, ai, bi <= 104
You are given an undirected graph. You are given an integer n
which is the number of nodes in the graph and an array edges
, where each edges[i] = [ui, vi]
indicates that there is an undirected edge between ui
and vi
.
A connected trio is a set of three nodes where there is an edge between every pair of them.
\n\nThe degree of a connected trio is the number of edges where one endpoint is in the trio, and the other is not.
\n\nReturn the minimum degree of a connected trio in the graph, or -1
if the graph has no connected trios.
\n
Example 1:
\n\nInput: n = 6, edges = [[1,2],[1,3],[3,2],[4,1],[5,2],[3,6]]\nOutput: 3\nExplanation: There is exactly one trio, which is [1,2,3]. The edges that form its degree are bolded in the figure above.\n\n\n
Example 2:
\n\nInput: n = 7, edges = [[1,3],[4,1],[4,3],[2,5],[5,6],[6,7],[7,5],[2,6]]\nOutput: 0\nExplanation: There are exactly three trios:\n1) [1,4,3] with degree 0.\n2) [2,5,6] with degree 2.\n3) [5,6,7] with degree 2.\n\n\n
\n
Constraints:
\n\n2 <= n <= 400
edges[i].length == 2
1 <= edges.length <= n * (n-1) / 2
1 <= ui, vi <= n
ui != vi
\u7ed9\u4f60\u4e00\u4e2a\u65e0\u5411\u56fe\uff0c\u6574\u6570 n
\u00a0\u8868\u793a\u56fe\u4e2d\u8282\u70b9\u7684\u6570\u76ee\uff0cedges
\u00a0\u6570\u7ec4\u8868\u793a\u56fe\u4e2d\u7684\u8fb9\uff0c\u5176\u4e2d\u00a0edges[i] = [ui, vi]
\u00a0\uff0c\u8868\u793a\u00a0ui
\u548c\u00a0vi
\u00a0\u4e4b\u95f4\u6709\u4e00\u6761\u65e0\u5411\u8fb9\u3002
\u4e00\u4e2a \u8fde\u901a\u4e09\u5143\u7ec4\u00a0\u6307\u7684\u662f \u4e09\u4e2a\u00a0\u8282\u70b9\u7ec4\u6210\u7684\u96c6\u5408\u4e14\u8fd9\u4e09\u4e2a\u70b9\u4e4b\u95f4 \u4e24\u4e24\u00a0\u6709\u8fb9\u3002
\n\n\u8fde\u901a\u4e09\u5143\u7ec4\u7684\u5ea6\u6570\u00a0\u662f\u6240\u6709\u6ee1\u8db3\u6b64\u6761\u4ef6\u7684\u8fb9\u7684\u6570\u76ee\uff1a\u4e00\u4e2a\u9876\u70b9\u5728\u8fd9\u4e2a\u4e09\u5143\u7ec4\u5185\uff0c\u800c\u53e6\u4e00\u4e2a\u9876\u70b9\u4e0d\u5728\u8fd9\u4e2a\u4e09\u5143\u7ec4\u5185\u3002
\n\n\u8bf7\u4f60\u8fd4\u56de\u6240\u6709\u8fde\u901a\u4e09\u5143\u7ec4\u4e2d\u5ea6\u6570\u7684 \u6700\u5c0f\u503c\u00a0\uff0c\u5982\u679c\u56fe\u4e2d\u6ca1\u6709\u8fde\u901a\u4e09\u5143\u7ec4\uff0c\u90a3\u4e48\u8fd4\u56de -1
\u00a0\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1an = 6, edges = [[1,2],[1,3],[3,2],[4,1],[5,2],[3,6]]\n\u8f93\u51fa\uff1a3\n\u89e3\u91ca\uff1a\u53ea\u6709\u4e00\u4e2a\u4e09\u5143\u7ec4 [1,2,3] \u3002\u6784\u6210\u5ea6\u6570\u7684\u8fb9\u5728\u4e0a\u56fe\u4e2d\u5df2\u88ab\u52a0\u7c97\u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1an = 7, edges = [[1,3],[4,1],[4,3],[2,5],[5,6],[6,7],[7,5],[2,6]]\n\u8f93\u51fa\uff1a0\n\u89e3\u91ca\uff1a\u6709 3 \u4e2a\u4e09\u5143\u7ec4\uff1a\n1) [1,4,3]\uff0c\u5ea6\u6570\u4e3a 0 \u3002\n2) [2,5,6]\uff0c\u5ea6\u6570\u4e3a 2 \u3002\n3) [5,6,7]\uff0c\u5ea6\u6570\u4e3a 2 \u3002\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n2 <= n <= 400
edges[i].length == 2
1 <= edges.length <= n * (n-1) / 2
1 <= ui, vi <= n
ui != vi
You are given an integer array nums
where the ith
bag contains nums[i]
balls. You are also given an integer maxOperations
.
You can perform the following operation at most maxOperations
times:
5
balls can become two new bags of 1
and 4
balls, or two new bags of 2
and 3
balls.Your penalty is the maximum number of balls in a bag. You want to minimize your penalty after the operations.
\n\nReturn the minimum possible penalty after performing the operations.
\n\n\n
Example 1:
\n\n\nInput: nums = [9], maxOperations = 2\nOutput: 3\nExplanation: \n- Divide the bag with 9 balls into two bags of sizes 6 and 3. [9] -> [6,3].\n- Divide the bag with 6 balls into two bags of sizes 3 and 3. [6,3] -> [3,3,3].\nThe bag with the most number of balls has 3 balls, so your penalty is 3 and you should return 3.\n\n\n
Example 2:
\n\n\nInput: nums = [2,4,8,2], maxOperations = 4\nOutput: 2\nExplanation:\n- Divide the bag with 8 balls into two bags of sizes 4 and 4. [2,4,8,2] -> [2,4,4,4,2].\n- Divide the bag with 4 balls into two bags of sizes 2 and 2. [2,4,4,4,2] -> [2,2,2,4,4,2].\n- Divide the bag with 4 balls into two bags of sizes 2 and 2. [2,2,2,4,4,2] -> [2,2,2,2,2,4,2].\n- Divide the bag with 4 balls into two bags of sizes 2 and 2. [2,2,2,2,2,4,2] -> [2,2,2,2,2,2,2,2].\nThe bag with the most number of balls has 2 balls, so your penalty is 2 an you should return 2.\n\n\n
Example 3:
\n\n\nInput: nums = [7,17], maxOperations = 2\nOutput: 7\n\n\n
\n
Constraints:
\n\n1 <= nums.length <= 105
1 <= maxOperations, nums[i] <= 109
\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u6570\u7ec4\u00a0nums
\u00a0\uff0c\u5176\u4e2d\u00a0nums[i]
\u00a0\u8868\u793a\u7b2c\u00a0i
\u00a0\u4e2a\u888b\u5b50\u91cc\u7403\u7684\u6570\u76ee\u3002\u540c\u65f6\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u00a0maxOperations
\u00a0\u3002
\u4f60\u53ef\u4ee5\u8fdb\u884c\u5982\u4e0b\u64cd\u4f5c\u81f3\u591a\u00a0maxOperations
\u00a0\u6b21\uff1a
5
\u00a0\u4e2a\u7403\uff0c\u4f60\u53ef\u4ee5\u628a\u5b83\u4eec\u5206\u5230\u4e24\u4e2a\u65b0\u888b\u5b50\u91cc\uff0c\u5206\u522b\u6709 1
\u00a0\u4e2a\u548c 4
\u00a0\u4e2a\u7403\uff0c\u6216\u8005\u5206\u522b\u6709 2
\u00a0\u4e2a\u548c 3
\u00a0\u4e2a\u7403\u3002\u4f60\u7684\u5f00\u9500\u662f\u5355\u4e2a\u888b\u5b50\u91cc\u7403\u6570\u76ee\u7684 \u6700\u5927\u503c\u00a0\uff0c\u4f60\u60f3\u8981 \u6700\u5c0f\u5316\u00a0\u5f00\u9500\u3002
\n\n\u8bf7\u4f60\u8fd4\u56de\u8fdb\u884c\u4e0a\u8ff0\u64cd\u4f5c\u540e\u7684\u6700\u5c0f\u5f00\u9500\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1anums = [9], maxOperations = 2\n\u8f93\u51fa\uff1a3\n\u89e3\u91ca\uff1a\n- \u5c06\u88c5\u6709 9 \u4e2a\u7403\u7684\u888b\u5b50\u5206\u6210\u88c5\u6709 6 \u4e2a\u548c 3 \u4e2a\u7403\u7684\u888b\u5b50\u3002[9] -> [6,3] \u3002\n- \u5c06\u88c5\u6709 6 \u4e2a\u7403\u7684\u888b\u5b50\u5206\u6210\u88c5\u6709 3 \u4e2a\u548c 3 \u4e2a\u7403\u7684\u888b\u5b50\u3002[6,3] -> [3,3,3] \u3002\n\u88c5\u6709\u6700\u591a\u7403\u7684\u888b\u5b50\u91cc\u88c5\u6709 3 \u4e2a\u7403\uff0c\u6240\u4ee5\u5f00\u9500\u4e3a 3 \u5e76\u8fd4\u56de 3 \u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1anums = [2,4,8,2], maxOperations = 4\n\u8f93\u51fa\uff1a2\n\u89e3\u91ca\uff1a\n- \u5c06\u88c5\u6709 8 \u4e2a\u7403\u7684\u888b\u5b50\u5206\u6210\u88c5\u6709 4 \u4e2a\u548c 4 \u4e2a\u7403\u7684\u888b\u5b50\u3002[2,4,8,2] -> [2,4,4,4,2] \u3002\n- \u5c06\u88c5\u6709 4 \u4e2a\u7403\u7684\u888b\u5b50\u5206\u6210\u88c5\u6709 2 \u4e2a\u548c 2 \u4e2a\u7403\u7684\u888b\u5b50\u3002[2,4,4,4,2] -> [2,2,2,4,4,2] \u3002\n- \u5c06\u88c5\u6709 4 \u4e2a\u7403\u7684\u888b\u5b50\u5206\u6210\u88c5\u6709 2 \u4e2a\u548c 2 \u4e2a\u7403\u7684\u888b\u5b50\u3002[2,2,2,4,4,2] -> [2,2,2,2,2,4,2] \u3002\n- \u5c06\u88c5\u6709 4 \u4e2a\u7403\u7684\u888b\u5b50\u5206\u6210\u88c5\u6709 2 \u4e2a\u548c 2 \u4e2a\u7403\u7684\u888b\u5b50\u3002[2,2,2,2,2,4,2] -> [2,2,2,2,2,2,2,2] \u3002\n\u88c5\u6709\u6700\u591a\u7403\u7684\u888b\u5b50\u91cc\u88c5\u6709 2 \u4e2a\u7403\uff0c\u6240\u4ee5\u5f00\u9500\u4e3a 2 \u5e76\u8fd4\u56de 2 \u3002\n\n\n
\u793a\u4f8b 3\uff1a
\n\n\n\u8f93\u5165\uff1anums = [7,17], maxOperations = 2\n\u8f93\u51fa\uff1a7\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= nums.length <= 105
1 <= maxOperations, nums[i] <= 109
Given a string s
, return the number of homogenous substrings of s
. Since the answer may be too large, return it modulo 109 + 7
.
A string is homogenous if all the characters of the string are the same.
\r\n\r\nA substring is a contiguous sequence of characters within a string.
\r\n\r\n\r\n
Example 1:
\r\n\r\n\r\nInput: s = "abbcccaa"\r\nOutput: 13\r\nExplanation: The homogenous substrings are listed as below:\r\n"a" appears 3 times.\r\n"aa" appears 1 time.\r\n"b" appears 2 times.\r\n"bb" appears 1 time.\r\n"c" appears 3 times.\r\n"cc" appears 2 times.\r\n"ccc" appears 1 time.\r\n3 + 1 + 2 + 1 + 3 + 2 + 1 = 13.\r\n\r\n
Example 2:
\r\n\r\n\r\nInput: s = "xy"\r\nOutput: 2\r\nExplanation: The homogenous substrings are "x" and "y".\r\n\r\n
Example 3:
\r\n\r\n\r\nInput: s = "zzzzz"\r\nOutput: 15\r\n\r\n\r\n
\r\n
Constraints:
\r\n\r\n1 <= s.length <= 105
s
consists of lowercase letters.\u7ed9\u4f60\u4e00\u4e2a\u5b57\u7b26\u4e32 s
\uff0c\u8fd4\u56de s
\u4e2d \u540c\u6784\u5b50\u5b57\u7b26\u4e32 \u7684\u6570\u76ee\u3002\u7531\u4e8e\u7b54\u6848\u53ef\u80fd\u5f88\u5927\uff0c\u53ea\u9700\u8fd4\u56de\u5bf9 109 + 7
\u53d6\u4f59 \u540e\u7684\u7ed3\u679c\u3002
\u540c\u6784\u5b57\u7b26\u4e32 \u7684\u5b9a\u4e49\u4e3a\uff1a\u5982\u679c\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u5b57\u7b26\u90fd\u76f8\u540c\uff0c\u90a3\u4e48\u8be5\u5b57\u7b26\u4e32\u5c31\u662f\u540c\u6784\u5b57\u7b26\u4e32\u3002
\n\n\u5b50\u5b57\u7b26\u4e32 \u662f\u5b57\u7b26\u4e32\u4e2d\u7684\u4e00\u4e2a\u8fde\u7eed\u5b57\u7b26\u5e8f\u5217\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1as = \"abbcccaa\"\n\u8f93\u51fa\uff1a13\n\u89e3\u91ca\uff1a\u540c\u6784\u5b50\u5b57\u7b26\u4e32\u5982\u4e0b\u6240\u5217\uff1a\n\"a\" \u51fa\u73b0 3 \u6b21\u3002\n\"aa\" \u51fa\u73b0 1 \u6b21\u3002\n\"b\" \u51fa\u73b0 2 \u6b21\u3002\n\"bb\" \u51fa\u73b0 1 \u6b21\u3002\n\"c\" \u51fa\u73b0 3 \u6b21\u3002\n\"cc\" \u51fa\u73b0 2 \u6b21\u3002\n\"ccc\" \u51fa\u73b0 1 \u6b21\u3002\n3 + 1 + 2 + 1 + 3 + 2 + 1 = 13\n\n
\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1as = \"xy\"\n\u8f93\u51fa\uff1a2\n\u89e3\u91ca\uff1a\u540c\u6784\u5b50\u5b57\u7b26\u4e32\u662f \"x\" \u548c \"y\" \u3002\n\n
\u793a\u4f8b 3\uff1a
\n\n\u8f93\u5165\uff1as = \"zzzzz\"\n\u8f93\u51fa\uff1a15\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= s.length <= 105
s
\u7531\u5c0f\u5199\u5b57\u7b26\u4e32\u7ec4\u6210You are given a string s
consisting only of the characters '0'
and '1'
. In one operation, you can change any '0'
to '1'
or vice versa.
The string is called alternating if no two adjacent characters are equal. For example, the string "010"
is alternating, while the string "0100"
is not.
Return the minimum number of operations needed to make s
alternating.
\n
Example 1:
\n\n\nInput: s = "0100"\nOutput: 1\nExplanation: If you change the last character to '1', s will be "0101", which is alternating.\n\n\n
Example 2:
\n\n\nInput: s = "10"\nOutput: 0\nExplanation: s is already alternating.\n\n\n
Example 3:
\n\n\nInput: s = "1111"\nOutput: 2\nExplanation: You need two operations to reach "0101" or "1010".\n\n\n
\n
Constraints:
\n\n1 <= s.length <= 104
s[i]
is either '0'
or '1'
.\u7ed9\u4f60\u4e00\u4e2a\u4ec5\u7531\u5b57\u7b26 '0'
\u548c '1'
\u7ec4\u6210\u7684\u5b57\u7b26\u4e32 s
\u3002\u4e00\u6b65\u64cd\u4f5c\u4e2d\uff0c\u4f60\u53ef\u4ee5\u5c06\u4efb\u4e00 '0'
\u53d8\u6210 '1'
\uff0c\u6216\u8005\u5c06 '1'
\u53d8\u6210 '0'
\u3002
\u4ea4\u66ff\u5b57\u7b26\u4e32 \u5b9a\u4e49\u4e3a\uff1a\u5982\u679c\u5b57\u7b26\u4e32\u4e2d\u4e0d\u5b58\u5728\u76f8\u90bb\u4e24\u4e2a\u5b57\u7b26\u76f8\u7b49\u7684\u60c5\u51b5\uff0c\u90a3\u4e48\u8be5\u5b57\u7b26\u4e32\u5c31\u662f\u4ea4\u66ff\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff0c\u5b57\u7b26\u4e32 \"010\"
\u662f\u4ea4\u66ff\u5b57\u7b26\u4e32\uff0c\u800c\u5b57\u7b26\u4e32 \"0100\"
\u4e0d\u662f\u3002
\u8fd4\u56de\u4f7f s
\u53d8\u6210 \u4ea4\u66ff\u5b57\u7b26\u4e32 \u6240\u9700\u7684 \u6700\u5c11 \u64cd\u4f5c\u6570\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1as = \"0100\"\n\u8f93\u51fa\uff1a1\n\u89e3\u91ca\uff1a\u5982\u679c\u5c06\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u53d8\u4e3a '1' \uff0cs \u5c31\u53d8\u6210 \"0101\" \uff0c\u5373\u7b26\u5408\u4ea4\u66ff\u5b57\u7b26\u4e32\u5b9a\u4e49\u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1as = \"10\"\n\u8f93\u51fa\uff1a0\n\u89e3\u91ca\uff1as \u5df2\u7ecf\u662f\u4ea4\u66ff\u5b57\u7b26\u4e32\u3002\n\n\n
\u793a\u4f8b 3\uff1a
\n\n\u8f93\u5165\uff1as = \"1111\"\n\u8f93\u51fa\uff1a2\n\u89e3\u91ca\uff1a\u9700\u8981 2 \u6b65\u64cd\u4f5c\u5f97\u5230 \"0101\" \u6216 \"1010\" \u3002\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= s.length <= 104
s[i]
\u662f '0'
\u6216 '1'
Given the root of a binary tree and two integers p
and q
, return the distance between the nodes of value p
and value q
in the tree.
The distance between two nodes is the number of edges on the path from one to the other.
\n\n\n
Example 1:
\n\nInput: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 0\nOutput: 3\nExplanation: There are 3 edges between 5 and 0: 5-3-1-0.\n\n
Example 2:
\n\nInput: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 7\nOutput: 2\nExplanation: There are 2 edges between 5 and 7: 5-2-7.\n\n
Example 3:
\n\nInput: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 5\nOutput: 0\nExplanation: The distance between a node and itself is 0.\n\n
\n
Constraints:
\n\n[1, 104]
.0 <= Node.val <= 109
Node.val
are unique.p
and q
are values in the tree.\u7ed9\u5b9a\u4e00\u68f5\u4e8c\u53c9\u6811\u7684\u6839\u8282\u70b9 root
\u4ee5\u53ca\u4e24\u4e2a\u6574\u6570 p
\u548c q
\uff0c\u8fd4\u56de\u8be5\u4e8c\u53c9\u6811\u4e2d\u503c\u4e3a p
\u7684\u7ed3\u70b9\u4e0e\u503c\u4e3a q
\u7684\u7ed3\u70b9\u95f4\u7684 \u8ddd\u79bb \u3002
\u4e24\u4e2a\u7ed3\u70b9\u95f4\u7684 \u8ddd\u79bb \u5c31\u662f\u4ece\u4e00\u4e2a\u7ed3\u70b9\u5230\u53e6\u4e00\u4e2a\u7ed3\u70b9\u7684\u8def\u5f84\u4e0a\u8fb9\u7684\u6570\u76ee\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1aroot = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 0\n\u8f93\u51fa\uff1a3\n\u89e3\u91ca\uff1a\u5728 5 \u548c 0 \u4e4b\u95f4\u6709 3 \u6761\u8fb9\uff1a5-3-1-0\n\n
\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1aroot = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 7\n\u8f93\u51fa\uff1a2\n\u89e3\u91ca\uff1a\u5728 5 \u548c 7 \u4e4b\u95f4\u6709 2 \u6761\u8fb9\uff1a5-2-7\n\n
\u793a\u4f8b 3\uff1a
\n\n\u8f93\u5165\uff1aroot = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 5\n\u8f93\u51fa\uff1a0\n\u89e3\u91ca\uff1a\u4e00\u4e2a\u7ed3\u70b9\u4e0e\u5b83\u672c\u8eab\u4e4b\u95f4\u7684\u8ddd\u79bb\u4e3a 0\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n[1, 104]
.0 <= Node.val <= 109
p
\u548cq
\u662f\u6811\u4e2d\u7ed3\u70b9\u7684\u503c.Table: Employees
\n+-------------+----------+\n| Column Name | Type |\n+-------------+----------+\n| employee_id | int |\n| name | varchar |\n| reports_to | int |\n| age | int |\n+-------------+----------+\nemployee_id is the primary key for this table.\nThis table contains information about the employees and the id of the manager they report to. Some employees do not report to anyone (reports_to is null). \n\n\n
\n\n
For this problem, we will consider a manager an employee who has at least 1 other employee reporting to them.
\n\nWrite an SQL query to report the ids and the names of all managers, the number of employees who report directly to them, and the average age of the reports rounded to the nearest integer.
\n\nReturn the result table ordered by employee_id
.
The query result format is in the following example:
\n\n\n\n
\nEmployees table:\n+-------------+---------+------------+-----+\n| employee_id | name | reports_to | age |\n+-------------+---------+------------+-----+\n| 9 | Hercy | null | 43 |\n| 6 | Alice | 9 | 41 |\n| 4 | Bob | 9 | 36 |\n| 2 | Winston | null | 37 |\n+-------------+---------+------------+-----+\n\nResult table:\n+-------------+-------+---------------+-------------+\n| employee_id | name | reports_count | average_age |\n+-------------+-------+---------------+-------------+\n| 9 | Hercy | 2 | 39 |\n+-------------+-------+---------------+-------------+\nHercy has 2 people report directly to him, Alice and Bob. Their average age is (41+36)/2 = 38.5, which is 39 after rounding it to the nearest integer.\n\n", "content_cn": "
Table: Employees
+-------------+----------+\n| Column Name | Type |\n+-------------+----------+\n| employee_id | int |\n| name | varchar |\n| reports_to | int |\n| age | int |\n+-------------+----------+\nemployee_id \u662f\u8fd9\u4e2a\u8868\u7684\u4e3b\u952e.\n\u8be5\u8868\u5305\u542b\u5458\u5de5\u4ee5\u53ca\u9700\u8981\u542c\u53d6\u4ed6\u4eec\u6c47\u62a5\u7684\u4e0a\u7ea7\u7ecf\u7406\u7684ID\u7684\u4fe1\u606f\u3002 \u6709\u4e9b\u5458\u5de5\u4e0d\u9700\u8981\u5411\u4efb\u4f55\u4eba\u6c47\u62a5\uff08reports_to \u4e3a\u7a7a\uff09\u3002\n\n\n
\u00a0
\n\n\u5bf9\u4e8e\u6b64\u95ee\u9898\uff0c\u6211\u4eec\u5c06\u81f3\u5c11\u6709\u4e00\u4e2a\u5176\u4ed6\u5458\u5de5\u9700\u8981\u5411\u4ed6\u6c47\u62a5\u7684\u5458\u5de5\uff0c\u89c6\u4e3a\u4e00\u4e2a\u7ecf\u7406\u3002
\n\n\u7f16\u5199SQL\u67e5\u8be2\u9700\u8981\u542c\u53d6\u6c47\u62a5\u7684\u6240\u6709\u7ecf\u7406\u7684ID\u3001\u540d\u79f0\u3001\u76f4\u63a5\u5411\u8be5\u7ecf\u7406\u6c47\u62a5\u7684\u5458\u5de5\u4eba\u6570\uff0c\u4ee5\u53ca\u8fd9\u4e9b\u5458\u5de5\u7684\u5e73\u5747\u5e74\u9f84\uff0c\u5176\u4e2d\u8be5\u5e73\u5747\u5e74\u9f84\u9700\u8981\u56db\u820d\u4e94\u5165\u5230\u6700\u63a5\u8fd1\u7684\u6574\u6570\u3002
\n\n\u8fd4\u56de\u7684\u7ed3\u679c\u96c6\u9700\u8981\u6309\u7167\u00a0employee_id
\u8fdb\u884c\u6392\u5e8f\u3002
\u67e5\u8be2\u7ed3\u679c\u7684\u683c\u5f0f\u5982\u4e0b\uff1a
\n\n\u00a0
\n\nEmployees table:\n+-------------+---------+------------+-----+\n| employee_id | name | reports_to | age |\n+-------------+---------+------------+-----+\n| 9 | Hercy | null | 43 |\n| 6 | Alice | 9 | 41 |\n| 4 | Bob | 9 | 36 |\n| 2 | Winston | null | 37 |\n+-------------+---------+------------+-----+\n\nResult table:\n+-------------+-------+---------------+-------------+\n| employee_id | name | reports_count | average_age |\n+-------------+-------+---------------+-------------+\n| 9 | Hercy | 2 | 39 |\n+-------------+-------+---------------+-------------+\nHercy \u6709\u4e24\u4e2a\u9700\u8981\u5411\u4ed6\u6c47\u62a5\u7684\u5458\u5de5, \u4ed6\u4eec\u662f Alice and Bob. \u4ed6\u4eec\u7684\u5e73\u5747\u5e74\u9f84\u662f (41+36)/2 = 38.5, \u56db\u820d\u4e94\u5165\u7684\u7ed3\u679c\u662f 39.\n\n", "tags_en": [], "tags_cn": [], "difficulty_en": "Easy", "difficulty_cn": "\u7b80\u5355", "code_snippets": [{"lang": "MySQL", "langSlug": "mysql", "code": "# Write your MySQL query statement below", "__typename": "CodeSnippetNode"}, {"lang": "MS SQL Server", "langSlug": "mssql", "code": "/* Write your T-SQL query statement below */", "__typename": "CodeSnippetNode"}, {"lang": "Oracle", "langSlug": "oraclesql", "code": "/* Write your PL/SQL query statement below */", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1731](https://leetcode-cn.com/problems/the-number-of-employees-which-report-to-each-employee)", "[\u6bcf\u4f4d\u7ecf\u7406\u7684\u4e0b\u5c5e\u5458\u5de5\u6570\u91cf](/solution/1700-1799/1731.The%20Number%20of%20Employees%20Which%20Report%20to%20Each%20Employee/README.md)", "", "\u7b80\u5355", "\ud83d\udd12"], "md_table_row_en": ["[1731](https://leetcode.com/problems/the-number-of-employees-which-report-to-each-employee)", "[The Number of Employees Which Report to Each Employee](/solution/1700-1799/1731.The%20Number%20of%20Employees%20Which%20Report%20to%20Each%20Employee/README_EN.md)", "", "Easy", "\ud83d\udd12"]}, {"question_id": "1881", "frontend_question_id": "1755", "paid_only": false, "paid_only_cn": false, "url_cn": "https://leetcode-cn.com/problems/closest-subsequence-sum", "url_en": "https://leetcode.com/problems/closest-subsequence-sum", "relative_path_cn": "/solution/1700-1799/1755.Closest%20Subsequence%20Sum/README.md", "relative_path_en": "/solution/1700-1799/1755.Closest%20Subsequence%20Sum/README_EN.md", "title_cn": "\u6700\u63a5\u8fd1\u76ee\u6807\u503c\u7684\u5b50\u5e8f\u5217\u548c", "title_en": "Closest Subsequence Sum", "question_title_slug": "closest-subsequence-sum", "content_en": "
You are given an integer array nums
and an integer goal
.
You want to choose a subsequence of nums
such that the sum of its elements is the closest possible to goal
. That is, if the sum of the subsequence's elements is sum
, then you want to minimize the absolute difference abs(sum - goal)
.
Return the minimum possible value of abs(sum - goal)
.
Note that a subsequence of an array is an array formed by removing some elements (possibly all or none) of the original array.
\n\n\n
Example 1:
\n\n\nInput: nums = [5,-7,3,5], goal = 6\nOutput: 0\nExplanation: Choose the whole array as a subsequence, with a sum of 6.\nThis is equal to the goal, so the absolute difference is 0.\n\n\n
Example 2:
\n\n\nInput: nums = [7,-9,15,-2], goal = -5\nOutput: 1\nExplanation: Choose the subsequence [7,-9,-2], with a sum of -4.\nThe absolute difference is abs(-4 - (-5)) = abs(1) = 1, which is the minimum.\n\n\n
Example 3:
\n\n\nInput: nums = [1,2,3], goal = -7\nOutput: 7\n\n\n
\n
Constraints:
\n\n1 <= nums.length <= 40
-107 <= nums[i] <= 107
-109 <= goal <= 109
\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 nums
\u548c\u4e00\u4e2a\u76ee\u6807\u503c goal
\u3002
\u4f60\u9700\u8981\u4ece nums
\u4e2d\u9009\u51fa\u4e00\u4e2a\u5b50\u5e8f\u5217\uff0c\u4f7f\u5b50\u5e8f\u5217\u5143\u7d20\u603b\u548c\u6700\u63a5\u8fd1 goal
\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5982\u679c\u5b50\u5e8f\u5217\u5143\u7d20\u548c\u4e3a sum
\uff0c\u4f60\u9700\u8981 \u6700\u5c0f\u5316\u7edd\u5bf9\u5dee abs(sum - goal)
\u3002
\u8fd4\u56de abs(sum - goal)
\u53ef\u80fd\u7684 \u6700\u5c0f\u503c \u3002
\u6ce8\u610f\uff0c\u6570\u7ec4\u7684\u5b50\u5e8f\u5217\u662f\u901a\u8fc7\u79fb\u9664\u539f\u59cb\u6570\u7ec4\u4e2d\u7684\u67d0\u4e9b\u5143\u7d20\uff08\u53ef\u80fd\u5168\u90e8\u6216\u65e0\uff09\u800c\u5f62\u6210\u7684\u6570\u7ec4\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1anums = [5,-7,3,5], goal = 6\n\u8f93\u51fa\uff1a0\n\u89e3\u91ca\uff1a\u9009\u62e9\u6574\u4e2a\u6570\u7ec4\u4f5c\u4e3a\u9009\u51fa\u7684\u5b50\u5e8f\u5217\uff0c\u5143\u7d20\u548c\u4e3a 6 \u3002\n\u5b50\u5e8f\u5217\u548c\u4e0e\u76ee\u6807\u503c\u76f8\u7b49\uff0c\u6240\u4ee5\u7edd\u5bf9\u5dee\u4e3a 0 \u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1anums = [7,-9,15,-2], goal = -5\n\u8f93\u51fa\uff1a1\n\u89e3\u91ca\uff1a\u9009\u51fa\u5b50\u5e8f\u5217 [7,-9,-2] \uff0c\u5143\u7d20\u548c\u4e3a -4 \u3002\n\u7edd\u5bf9\u5dee\u4e3a abs(-4 - (-5)) = abs(1) = 1 \uff0c\u662f\u53ef\u80fd\u7684\u6700\u5c0f\u503c\u3002\n\n\n
\u793a\u4f8b 3\uff1a
\n\n\u8f93\u5165\uff1anums = [1,2,3], goal = -7\n\u8f93\u51fa\uff1a7\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= nums.length <= 40
-107 <= nums[i] <= 107
-109 <= goal <= 109
You are given two strings word1
and word2
. You want to construct a string merge
in the following way: while either word1
or word2
are non-empty, choose one of the following options:
word1
is non-empty, append the first character in word1
to merge
and delete it from word1
.\n\n\tword1 = "abc"
and merge = "dv"
, then after choosing this operation, word1 = "bc"
and merge = "dva"
.word2
is non-empty, append the first character in word2
to merge
and delete it from word2
.\n\tword2 = "abc"
and merge = ""
, then after choosing this operation, word2 = "bc"
and merge = "a"
.Return the lexicographically largest merge
you can construct.
A string a
is lexicographically larger than a string b
(of the same length) if in the first position where a
and b
differ, a
has a character strictly larger than the corresponding character in b
. For example, "abcd"
is lexicographically larger than "abcc"
because the first position they differ is at the fourth character, and d
is greater than c
.
\n
Example 1:
\n\n\nInput: word1 = "cabaa", word2 = "bcaaa"\nOutput: "cbcabaaaaa"\nExplanation: One way to get the lexicographically largest merge is:\n- Take from word1: merge = "c", word1 = "abaa", word2 = "bcaaa"\n- Take from word2: merge = "cb", word1 = "abaa", word2 = "caaa"\n- Take from word2: merge = "cbc", word1 = "abaa", word2 = "aaa"\n- Take from word1: merge = "cbca", word1 = "baa", word2 = "aaa"\n- Take from word1: merge = "cbcab", word1 = "aa", word2 = "aaa"\n- Append the remaining 5 a's from word1 and word2 at the end of merge.\n\n\n
Example 2:
\n\n\nInput: word1 = "abcabc", word2 = "abdcaba"\nOutput: "abdcabcabcaba"\n\n\n
\n
Constraints:
\n\n1 <= word1.length, word2.length <= 3000
word1
and word2
consist only of lowercase English letters.\u7ed9\u4f60\u4e24\u4e2a\u5b57\u7b26\u4e32 word1
\u548c word2
\u3002\u4f60\u9700\u8981\u6309\u4e0b\u8ff0\u65b9\u5f0f\u6784\u9020\u4e00\u4e2a\u65b0\u5b57\u7b26\u4e32 merge
\uff1a\u5982\u679c word1
\u6216 word2
\u975e\u7a7a\uff0c\u9009\u62e9 \u4e0b\u9762\u9009\u9879\u4e4b\u4e00 \u7ee7\u7eed\u64cd\u4f5c\uff1a
word1
\u975e\u7a7a\uff0c\u5c06 word1
\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5b57\u7b26\u9644\u52a0\u5230 merge
\u7684\u672b\u5c3e\uff0c\u5e76\u5c06\u5176\u4ece word1
\u4e2d\u79fb\u9664\u3002\n\n\tword1 = \"abc\"
\u4e14 merge = \"dv\"
\uff0c\u5728\u6267\u884c\u6b64\u9009\u9879\u64cd\u4f5c\u4e4b\u540e\uff0cword1 = \"bc\"
\uff0c\u540c\u65f6 merge = \"dva\"
\u3002word2
\u975e\u7a7a\uff0c\u5c06 word2
\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5b57\u7b26\u9644\u52a0\u5230 merge
\u7684\u672b\u5c3e\uff0c\u5e76\u5c06\u5176\u4ece word2
\u4e2d\u79fb\u9664\u3002\n\tword2 = \"abc\"
\u4e14 merge = \"\"
\uff0c\u5728\u6267\u884c\u6b64\u9009\u9879\u64cd\u4f5c\u4e4b\u540e\uff0cword2 = \"bc\"
\uff0c\u540c\u65f6 merge = \"a\"
\u3002\u8fd4\u56de\u4f60\u53ef\u4ee5\u6784\u9020\u7684\u5b57\u5178\u5e8f \u6700\u5927 \u7684\u5408\u5e76\u5b57\u7b26\u4e32 merge
\u3002
\u957f\u5ea6\u76f8\u540c\u7684\u4e24\u4e2a\u5b57\u7b26\u4e32 a
\u548c b
\u6bd4\u8f83\u5b57\u5178\u5e8f\u5927\u5c0f\uff0c\u5982\u679c\u5728 a
\u548c b
\u51fa\u73b0\u4e0d\u540c\u7684\u7b2c\u4e00\u4e2a\u4f4d\u7f6e\uff0ca
\u4e2d\u5b57\u7b26\u5728\u5b57\u6bcd\u8868\u4e2d\u7684\u51fa\u73b0\u987a\u5e8f\u4f4d\u4e8e b
\u4e2d\u76f8\u5e94\u5b57\u7b26\u4e4b\u540e\uff0c\u5c31\u8ba4\u4e3a\u5b57\u7b26\u4e32 a
\u6309\u5b57\u5178\u5e8f\u6bd4\u5b57\u7b26\u4e32 b
\u66f4\u5927\u3002\u4f8b\u5982\uff0c\"abcd\"
\u6309\u5b57\u5178\u5e8f\u6bd4 \"abcc\"
\u66f4\u5927\uff0c\u56e0\u4e3a\u4e24\u4e2a\u5b57\u7b26\u4e32\u51fa\u73b0\u4e0d\u540c\u7684\u7b2c\u4e00\u4e2a\u4f4d\u7f6e\u662f\u7b2c\u56db\u4e2a\u5b57\u7b26\uff0c\u800c d
\u5728\u5b57\u6bcd\u8868\u4e2d\u7684\u51fa\u73b0\u987a\u5e8f\u4f4d\u4e8e c
\u4e4b\u540e\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1aword1 = \"cabaa\", word2 = \"bcaaa\"\n\u8f93\u51fa\uff1a\"cbcabaaaaa\"\n\u89e3\u91ca\uff1a\u6784\u9020\u5b57\u5178\u5e8f\u6700\u5927\u7684\u5408\u5e76\u5b57\u7b26\u4e32\uff0c\u53ef\u884c\u7684\u4e00\u79cd\u65b9\u6cd5\u5982\u4e0b\u6240\u793a\uff1a\n- \u4ece word1 \u4e2d\u53d6\u7b2c\u4e00\u4e2a\u5b57\u7b26\uff1amerge = \"c\"\uff0cword1 = \"abaa\"\uff0cword2 = \"bcaaa\"\n- \u4ece word2 \u4e2d\u53d6\u7b2c\u4e00\u4e2a\u5b57\u7b26\uff1amerge = \"cb\"\uff0cword1 = \"abaa\"\uff0cword2 = \"caaa\"\n- \u4ece word2 \u4e2d\u53d6\u7b2c\u4e00\u4e2a\u5b57\u7b26\uff1amerge = \"cbc\"\uff0cword1 = \"abaa\"\uff0cword2 = \"aaa\"\n- \u4ece word1 \u4e2d\u53d6\u7b2c\u4e00\u4e2a\u5b57\u7b26\uff1amerge = \"cbca\"\uff0cword1 = \"baa\"\uff0cword2 = \"aaa\"\n- \u4ece word1 \u4e2d\u53d6\u7b2c\u4e00\u4e2a\u5b57\u7b26\uff1amerge = \"cbcab\"\uff0cword1 = \"aa\"\uff0cword2 = \"aaa\"\n- \u5c06 word1 \u548c word2 \u4e2d\u5269\u4e0b\u7684 5 \u4e2a a \u9644\u52a0\u5230 merge \u7684\u672b\u5c3e\u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1aword1 = \"abcabc\", word2 = \"abdcaba\"\n\u8f93\u51fa\uff1a\"abdcabcabcaba\"\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= word1.length, word2.length <= 3000
word1
\u548c word2
\u4ec5\u7531\u5c0f\u5199\u82f1\u6587\u7ec4\u6210You are playing a solitaire game with three piles of stones of sizes a
\u200b\u200b\u200b\u200b\u200b\u200b, b
,\u200b\u200b\u200b\u200b\u200b\u200b and c
\u200b\u200b\u200b\u200b\u200b\u200b respectively. Each turn you choose two different non-empty piles, take one stone from each, and add 1
point to your score. The game stops when there are fewer than two non-empty piles (meaning there are no more available moves).
Given three integers a
\u200b\u200b\u200b\u200b\u200b, b
,\u200b\u200b\u200b\u200b\u200b and c
\u200b\u200b\u200b\u200b\u200b, return the maximum score you can get.
\n
Example 1:
\n\n\nInput: a = 2, b = 4, c = 6\nOutput: 6\nExplanation: The starting state is (2, 4, 6). One optimal set of moves is:\n- Take from 1st and 3rd piles, state is now (1, 4, 5)\n- Take from 1st and 3rd piles, state is now (0, 4, 4)\n- Take from 2nd and 3rd piles, state is now (0, 3, 3)\n- Take from 2nd and 3rd piles, state is now (0, 2, 2)\n- Take from 2nd and 3rd piles, state is now (0, 1, 1)\n- Take from 2nd and 3rd piles, state is now (0, 0, 0)\nThere are fewer than two non-empty piles, so the game ends. Total: 6 points.\n\n\n
Example 2:
\n\n\nInput: a = 4, b = 4, c = 6\nOutput: 7\nExplanation: The starting state is (4, 4, 6). One optimal set of moves is:\n- Take from 1st and 2nd piles, state is now (3, 3, 6)\n- Take from 1st and 3rd piles, state is now (2, 3, 5)\n- Take from 1st and 3rd piles, state is now (1, 3, 4)\n- Take from 1st and 3rd piles, state is now (0, 3, 3)\n- Take from 2nd and 3rd piles, state is now (0, 2, 2)\n- Take from 2nd and 3rd piles, state is now (0, 1, 1)\n- Take from 2nd and 3rd piles, state is now (0, 0, 0)\nThere are fewer than two non-empty piles, so the game ends. Total: 7 points.\n\n\n
Example 3:
\n\n\nInput: a = 1, b = 8, c = 8\nOutput: 8\nExplanation: One optimal set of moves is to take from the 2nd and 3rd piles for 8 turns until they are empty.\nAfter that, there are fewer than two non-empty piles, so the game ends.\n\n\n
\n
Constraints:
\n\n1 <= a, b, c <= 105
\u4f60\u6b63\u5728\u73a9\u4e00\u4e2a\u5355\u4eba\u6e38\u620f\uff0c\u9762\u524d\u653e\u7f6e\u7740\u5927\u5c0f\u5206\u522b\u4e3a a
\u200b\u200b\u200b\u200b\u200b\u200b\u3001b
\u548c c
\u200b\u200b\u200b\u200b\u200b\u200b \u7684 \u4e09\u5806 \u77f3\u5b50\u3002
\u6bcf\u56de\u5408\u4f60\u90fd\u8981\u4ece\u4e24\u4e2a \u4e0d\u540c\u7684\u975e\u7a7a\u5806 \u4e2d\u53d6\u51fa\u4e00\u9897\u77f3\u5b50\uff0c\u5e76\u5728\u5f97\u5206\u4e0a\u52a0 1
\u5206\u3002\u5f53\u5b58\u5728 \u4e24\u4e2a\u6216\u66f4\u591a \u7684\u7a7a\u5806\u65f6\uff0c\u6e38\u620f\u505c\u6b62\u3002
\u7ed9\u4f60\u4e09\u4e2a\u6574\u6570 a
\u3001b
\u548c c
\uff0c\u8fd4\u56de\u53ef\u4ee5\u5f97\u5230\u7684 \u6700\u5927\u5206\u6570 \u3002
\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1aa = 2, b = 4, c = 6\n\u8f93\u51fa\uff1a6\n\u89e3\u91ca\uff1a\u77f3\u5b50\u8d77\u59cb\u72b6\u6001\u662f (2, 4, 6) \uff0c\u6700\u4f18\u7684\u4e00\u7ec4\u64cd\u4f5c\u662f\uff1a\n- \u4ece\u7b2c\u4e00\u548c\u7b2c\u4e09\u5806\u53d6\uff0c\u77f3\u5b50\u72b6\u6001\u73b0\u5728\u662f (1, 4, 5)\n- \u4ece\u7b2c\u4e00\u548c\u7b2c\u4e09\u5806\u53d6\uff0c\u77f3\u5b50\u72b6\u6001\u73b0\u5728\u662f (0, 4, 4)\n- \u4ece\u7b2c\u4e8c\u548c\u7b2c\u4e09\u5806\u53d6\uff0c\u77f3\u5b50\u72b6\u6001\u73b0\u5728\u662f (0, 3, 3)\n- \u4ece\u7b2c\u4e8c\u548c\u7b2c\u4e09\u5806\u53d6\uff0c\u77f3\u5b50\u72b6\u6001\u73b0\u5728\u662f (0, 2, 2)\n- \u4ece\u7b2c\u4e8c\u548c\u7b2c\u4e09\u5806\u53d6\uff0c\u77f3\u5b50\u72b6\u6001\u73b0\u5728\u662f (0, 1, 1)\n- \u4ece\u7b2c\u4e8c\u548c\u7b2c\u4e09\u5806\u53d6\uff0c\u77f3\u5b50\u72b6\u6001\u73b0\u5728\u662f (0, 0, 0)\n\u603b\u5206\uff1a6 \u5206 \u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1aa = 4, b = 4, c = 6\n\u8f93\u51fa\uff1a7\n\u89e3\u91ca\uff1a\u77f3\u5b50\u8d77\u59cb\u72b6\u6001\u662f (4, 4, 6) \uff0c\u6700\u4f18\u7684\u4e00\u7ec4\u64cd\u4f5c\u662f\uff1a\n- \u4ece\u7b2c\u4e00\u548c\u7b2c\u4e8c\u5806\u53d6\uff0c\u77f3\u5b50\u72b6\u6001\u73b0\u5728\u662f (3, 3, 6)\n- \u4ece\u7b2c\u4e00\u548c\u7b2c\u4e09\u5806\u53d6\uff0c\u77f3\u5b50\u72b6\u6001\u73b0\u5728\u662f (2, 3, 5)\n- \u4ece\u7b2c\u4e00\u548c\u7b2c\u4e09\u5806\u53d6\uff0c\u77f3\u5b50\u72b6\u6001\u73b0\u5728\u662f (1, 3, 4)\n- \u4ece\u7b2c\u4e00\u548c\u7b2c\u4e09\u5806\u53d6\uff0c\u77f3\u5b50\u72b6\u6001\u73b0\u5728\u662f (0, 3, 3)\n- \u4ece\u7b2c\u4e8c\u548c\u7b2c\u4e09\u5806\u53d6\uff0c\u77f3\u5b50\u72b6\u6001\u73b0\u5728\u662f (0, 2, 2)\n- \u4ece\u7b2c\u4e8c\u548c\u7b2c\u4e09\u5806\u53d6\uff0c\u77f3\u5b50\u72b6\u6001\u73b0\u5728\u662f (0, 1, 1)\n- \u4ece\u7b2c\u4e8c\u548c\u7b2c\u4e09\u5806\u53d6\uff0c\u77f3\u5b50\u72b6\u6001\u73b0\u5728\u662f (0, 0, 0)\n\u603b\u5206\uff1a7 \u5206 \u3002\n\n\n
\u793a\u4f8b 3\uff1a
\n\n\n\u8f93\u5165\uff1aa = 1, b = 8, c = 8\n\u8f93\u51fa\uff1a8\n\u89e3\u91ca\uff1a\u6700\u4f18\u7684\u4e00\u7ec4\u64cd\u4f5c\u662f\u8fde\u7eed\u4ece\u7b2c\u4e8c\u548c\u7b2c\u4e09\u5806\u53d6 8 \u56de\u5408\uff0c\u76f4\u5230\u5c06\u5b83\u4eec\u53d6\u7a7a\u3002\n\u6ce8\u610f\uff0c\u7531\u4e8e\u7b2c\u4e8c\u548c\u7b2c\u4e09\u5806\u5df2\u7ecf\u7a7a\u4e86\uff0c\u6e38\u620f\u7ed3\u675f\uff0c\u4e0d\u80fd\u7ee7\u7eed\u4ece\u7b2c\u4e00\u5806\u4e2d\u53d6\u77f3\u5b50\u3002\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= a, b, c <= 105
Given an array nums
, return true
if the array was originally sorted in non-decreasing order, then rotated some number of positions (including zero). Otherwise, return false
.
There may be duplicates in the original array.
\n\nNote: An array A
rotated by x
positions results in an array B
of the same length such that A[i] == B[(i+x) % A.length]
, where %
is the modulo operation.
\n
Example 1:
\n\n\nInput: nums = [3,4,5,1,2]\nOutput: true\nExplanation: [1,2,3,4,5] is the original sorted array.\nYou can rotate the array by x = 3 positions to begin on the the element of value 3: [3,4,5,1,2].\n\n\n
Example 2:
\n\n\nInput: nums = [2,1,3,4]\nOutput: false\nExplanation: There is no sorted array once rotated that can make nums.\n\n\n
Example 3:
\n\n\nInput: nums = [1,2,3]\nOutput: true\nExplanation: [1,2,3] is the original sorted array.\nYou can rotate the array by x = 0 positions (i.e. no rotation) to make nums.\n\n\n
Example 4:
\n\n\nInput: nums = [1,1,1]\nOutput: true\nExplanation: [1,1,1] is the original sorted array.\nYou can rotate any number of positions to make nums.\n\n\n
Example 5:
\n\n\nInput: nums = [2,1]\nOutput: true\nExplanation: [1,2] is the original sorted array.\nYou can rotate the array by x = 5 positions to begin on the element of value 2: [2,1].\n\n\n
\n
Constraints:
\n\n1 <= nums.length <= 100
1 <= nums[i] <= 100
\u7ed9\u4f60\u4e00\u4e2a\u6570\u7ec4 nums
\u3002nums
\u7684\u6e90\u6570\u7ec4\u4e2d\uff0c\u6240\u6709\u5143\u7d20\u4e0e nums
\u76f8\u540c\uff0c\u4f46\u6309\u975e\u9012\u51cf\u987a\u5e8f\u6392\u5217\u3002
\u5982\u679c\u00a0nums
\u80fd\u591f\u7531\u6e90\u6570\u7ec4\u8f6e\u8f6c\u82e5\u5e72\u4f4d\u7f6e\uff08\u5305\u62ec 0 \u4e2a\u4f4d\u7f6e\uff09\u5f97\u5230\uff0c\u5219\u8fd4\u56de true
\uff1b\u5426\u5219\uff0c\u8fd4\u56de false
\u3002
\u6e90\u6570\u7ec4\u4e2d\u53ef\u80fd\u5b58\u5728 \u91cd\u590d\u9879 \u3002
\n\n\u6ce8\u610f\uff1a\u6211\u4eec\u79f0\u6570\u7ec4 A
\u5728\u8f6e\u8f6c x
\u4e2a\u4f4d\u7f6e\u540e\u5f97\u5230\u957f\u5ea6\u76f8\u540c\u7684\u6570\u7ec4 B
\uff0c\u5f53\u5b83\u4eec\u6ee1\u8db3 A[i] == B[(i+x) % A.length]
\uff0c\u5176\u4e2d %
\u4e3a\u53d6\u4f59\u8fd0\u7b97\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1anums = [3,4,5,1,2]\n\u8f93\u51fa\uff1atrue\n\u89e3\u91ca\uff1a[1,2,3,4,5] \u4e3a\u6709\u5e8f\u7684\u6e90\u6570\u7ec4\u3002\n\u53ef\u4ee5\u8f6e\u8f6c x = 3 \u4e2a\u4f4d\u7f6e\uff0c\u4f7f\u65b0\u6570\u7ec4\u4ece\u503c\u4e3a 3 \u7684\u5143\u7d20\u5f00\u59cb\uff1a[3,4,5,1,2] \u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1anums = [2,1,3,4]\n\u8f93\u51fa\uff1afalse\n\u89e3\u91ca\uff1a\u6e90\u6570\u7ec4\u65e0\u6cd5\u7ecf\u8f6e\u8f6c\u5f97\u5230 nums \u3002\n\n\n
\u793a\u4f8b 3\uff1a
\n\n\n\u8f93\u5165\uff1anums = [1,2,3]\n\u8f93\u51fa\uff1atrue\n\u89e3\u91ca\uff1a[1,2,3] \u4e3a\u6709\u5e8f\u7684\u6e90\u6570\u7ec4\u3002\n\u53ef\u4ee5\u8f6e\u8f6c x = 0 \u4e2a\u4f4d\u7f6e\uff08\u5373\u4e0d\u8f6e\u8f6c\uff09\u5f97\u5230 nums \u3002\n\n\n
\u793a\u4f8b 4\uff1a
\n\n\n\u8f93\u5165\uff1anums = [1,1,1]\n\u8f93\u51fa\uff1atrue\n\u89e3\u91ca\uff1a[1,1,1] \u4e3a\u6709\u5e8f\u7684\u6e90\u6570\u7ec4\u3002\n\u8f6e\u8f6c\u4efb\u610f\u4e2a\u4f4d\u7f6e\u90fd\u53ef\u4ee5\u5f97\u5230 nums \u3002\n\n\n
\u793a\u4f8b 5\uff1a
\n\n\n\u8f93\u5165\uff1anums = [2,1]\n\u8f93\u51fa\uff1atrue\n\u89e3\u91ca\uff1a[1,2] \u4e3a\u6709\u5e8f\u7684\u6e90\u6570\u7ec4\u3002\n\u53ef\u4ee5\u8f6e\u8f6c x = 5 \u4e2a\u4f4d\u7f6e\uff0c\u4f7f\u65b0\u6570\u7ec4\u4ece\u503c\u4e3a 2 \u7684\u5143\u7d20\u5f00\u59cb\uff1a[2,1] \u3002\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\n1 <= nums.length <= 100
1 <= nums[i] <= 100
Table: Followers
\n+-------------+------+\n| Column Name | Type |\n+-------------+------+\n| user_id | int |\n| follower_id | int |\n+-------------+------+\n(user_id, follower_id) is the primary key for this table.\nThis table contains the IDs of a user and a follower in a social media app where the follower follows the user.\n\n
Write an SQL query that will, for each user, return the number of followers.
\n\nReturn the result table ordered by user_id
.
The query result format is in the following example:
\n\n\n\n
\nFollowers table:\n+---------+-------------+\n| user_id | follower_id |\n+---------+-------------+\n| 0 | 1 |\n| 1 | 0 |\n| 2 | 0 |\n| 2 | 1 |\n+---------+-------------+\nResult table:\n+---------+----------------+\n| user_id | followers_count|\n+---------+----------------+\n| 0 | 1 |\n| 1 | 1 |\n| 2 | 2 |\n+---------+----------------+\nThe followers of 0 are {1}\nThe followers of 1 are {0}\nThe followers of 2 are {0,1}\n\n\n
\n", "content_cn": "
\u8868\uff1a\u00a0Followers
+-------------+------+\n| Column Name | Type |\n+-------------+------+\n| user_id | int |\n| follower_id | int |\n+-------------+------+\n(user_id, follower_id) \u662f\u8fd9\u4e2a\u8868\u7684\u4e3b\u952e\u3002\n\u8be5\u8868\u5305\u542b\u4e00\u4e2a\u5173\u6ce8\u5173\u7cfb\u4e2d\u5173\u6ce8\u8005\u548c\u7528\u6237\u7684\u7f16\u53f7\uff0c\u5176\u4e2d\u5173\u6ce8\u8005\u5173\u6ce8\u7528\u6237\u3002\n\n
\u5199\u51fa SQL \u8bed\u53e5\uff0c\u5bf9\u4e8e\u6bcf\u4e00\u4e2a\u7528\u6237\uff0c\u8fd4\u56de\u8be5\u7528\u6237\u7684\u5173\u6ce8\u8005\u6570\u91cf\u3002
\n\n\u6309\u00a0user_id
\u00a0\u7684\u987a\u5e8f\u8fd4\u56de\u7ed3\u679c\u8868\u3002
\u67e5\u8be2\u7ed3\u679c\u7684\u683c\u5f0f\u5982\u4e0b\u793a\u4f8b\u6240\u793a\uff1a
\n\n\u00a0
\n\nFollowers \u8868\uff1a\n+---------+-------------+\n| user_id | follower_id |\n+---------+-------------+\n| 0 | 1 |\n| 1 | 0 |\n| 2 | 0 |\n| 2 | 1 |\n+---------+-------------+\n\u7ed3\u679c\u8868\uff1a\n+---------+----------------+\n| user_id | followers_count|\n+---------+----------------+\n| 0 | 1 |\n| 1 | 1 |\n| 2 | 2 |\n+---------+----------------+\n0 \u7684\u5173\u6ce8\u8005\u6709 {1}\n1 \u7684\u5173\u6ce8\u8005\u6709 {0}\n2 \u7684\u5173\u6ce8\u8005\u6709 {0,1}\n\n\n
\u00a0
\n", "tags_en": [], "tags_cn": [], "difficulty_en": "Easy", "difficulty_cn": "\u7b80\u5355", "code_snippets": [{"lang": "MySQL", "langSlug": "mysql", "code": "# Write your MySQL query statement below", "__typename": "CodeSnippetNode"}, {"lang": "MS SQL Server", "langSlug": "mssql", "code": "/* Write your T-SQL query statement below */", "__typename": "CodeSnippetNode"}, {"lang": "Oracle", "langSlug": "oraclesql", "code": "/* Write your PL/SQL query statement below */", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1729](https://leetcode-cn.com/problems/find-followers-count)", "[\u6c42\u5173\u6ce8\u8005\u7684\u6570\u91cf](/solution/1700-1799/1729.Find%20Followers%20Count/README.md)", "", "\u7b80\u5355", "\ud83d\udd12"], "md_table_row_en": ["[1729](https://leetcode.com/problems/find-followers-count)", "[Find Followers Count](/solution/1700-1799/1729.Find%20Followers%20Count/README_EN.md)", "", "Easy", "\ud83d\udd12"]}, {"question_id": "1876", "frontend_question_id": "1765", "paid_only": false, "paid_only_cn": false, "url_cn": "https://leetcode-cn.com/problems/map-of-highest-peak", "url_en": "https://leetcode.com/problems/map-of-highest-peak", "relative_path_cn": "/solution/1700-1799/1765.Map%20of%20Highest%20Peak/README.md", "relative_path_en": "/solution/1700-1799/1765.Map%20of%20Highest%20Peak/README_EN.md", "title_cn": "\u5730\u56fe\u4e2d\u7684\u6700\u9ad8\u70b9", "title_en": "Map of Highest Peak", "question_title_slug": "map-of-highest-peak", "content_en": "You are given an integer matrix isWater
of size m x n
that represents a map of land and water cells.
isWater[i][j] == 0
, cell (i, j)
is a land cell.isWater[i][j] == 1
, cell (i, j)
is a water cell.You must assign each cell a height in a way that follows these rules:
\n\n0
.1
. A cell is adjacent to another cell if the former is directly north, east, south, or west of the latter (i.e., their sides are touching).Find an assignment of heights such that the maximum height in the matrix is maximized.
\n\nReturn an integer matrix height
of size m x n
where height[i][j]
is cell (i, j)
's height. If there are multiple solutions, return any of them.
\n
Example 1:
\n\n\nInput: isWater = [[0,1],[0,0]]\nOutput: [[1,0],[2,1]]\nExplanation: The image shows the assigned heights of each cell.\nThe blue cell is the water cell, and the green cells are the land cells.\n\n\n
Example 2:
\n\n\nInput: isWater = [[0,0,1],[1,0,0],[0,0,0]]\nOutput: [[1,1,0],[0,1,1],[1,2,2]]\nExplanation: A height of 2 is the maximum possible height of any assignment.\nAny height assignment that has a maximum height of 2 while still meeting the rules will also be accepted.\n\n\n
\n
Constraints:
\n\nm == isWater.length
n == isWater[i].length
1 <= m, n <= 1000
isWater[i][j]
is 0
or 1
.\u7ed9\u4f60\u4e00\u4e2a\u5927\u5c0f\u4e3a\u00a0m x n
\u00a0\u7684\u6574\u6570\u77e9\u9635\u00a0isWater
\u00a0\uff0c\u5b83\u4ee3\u8868\u4e86\u4e00\u4e2a\u7531 \u9646\u5730\u00a0\u548c \u6c34\u57df\u00a0\u5355\u5143\u683c\u7ec4\u6210\u7684\u5730\u56fe\u3002
isWater[i][j] == 0
\u00a0\uff0c\u683c\u5b50\u00a0(i, j)
\u00a0\u662f\u4e00\u4e2a \u9646\u5730\u00a0\u683c\u5b50\u3002isWater[i][j] == 1
\u00a0\uff0c\u683c\u5b50\u00a0(i, j)
\u00a0\u662f\u4e00\u4e2a \u6c34\u57df\u00a0\u683c\u5b50\u3002\u4f60\u9700\u8981\u6309\u7167\u5982\u4e0b\u89c4\u5219\u7ed9\u6bcf\u4e2a\u5355\u5143\u683c\u5b89\u6392\u9ad8\u5ea6\uff1a
\n\n0
\u00a0\u30021
\u00a0\u3002\u5f53\u4e24\u4e2a\u683c\u5b50\u5728\u6b63\u4e1c\u3001\u5357\u3001\u897f\u3001\u5317\u65b9\u5411\u4e0a\u76f8\u4e92\u7d27\u6328\u7740\uff0c\u5c31\u79f0\u5b83\u4eec\u4e3a\u76f8\u90bb\u7684\u683c\u5b50\u3002\uff08\u4e5f\u5c31\u662f\u8bf4\u5b83\u4eec\u6709\u4e00\u6761\u516c\u5171\u8fb9\uff09\u627e\u5230\u4e00\u79cd\u5b89\u6392\u9ad8\u5ea6\u7684\u65b9\u6848\uff0c\u4f7f\u5f97\u77e9\u9635\u4e2d\u7684\u6700\u9ad8\u9ad8\u5ea6\u503c\u00a0\u6700\u5927\u00a0\u3002
\n\n\u8bf7\u4f60\u8fd4\u56de\u4e00\u4e2a\u5927\u5c0f\u4e3a\u00a0m x n
\u00a0\u7684\u6574\u6570\u77e9\u9635 height
\u00a0\uff0c\u5176\u4e2d height[i][j]
\u00a0\u662f\u683c\u5b50 (i, j)
\u00a0\u7684\u9ad8\u5ea6\u3002\u5982\u679c\u6709\u591a\u79cd\u89e3\u6cd5\uff0c\u8bf7\u8fd4\u56de\u00a0\u4efb\u610f\u4e00\u4e2a\u00a0\u3002
\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1aisWater = [[0,1],[0,0]]\n\u8f93\u51fa\uff1a[[1,0],[2,1]]\n\u89e3\u91ca\uff1a\u4e0a\u56fe\u5c55\u793a\u4e86\u7ed9\u5404\u4e2a\u683c\u5b50\u5b89\u6392\u7684\u9ad8\u5ea6\u3002\n\u84dd\u8272\u683c\u5b50\u662f\u6c34\u57df\u683c\uff0c\u7eff\u8272\u683c\u5b50\u662f\u9646\u5730\u683c\u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1aisWater = [[0,0,1],[1,0,0],[0,0,0]]\n\u8f93\u51fa\uff1a[[1,1,0],[0,1,1],[1,2,2]]\n\u89e3\u91ca\uff1a\u6240\u6709\u5b89\u6392\u65b9\u6848\u4e2d\uff0c\u6700\u9ad8\u53ef\u884c\u9ad8\u5ea6\u4e3a 2 \u3002\n\u4efb\u610f\u5b89\u6392\u65b9\u6848\u4e2d\uff0c\u53ea\u8981\u6700\u9ad8\u9ad8\u5ea6\u4e3a 2 \u4e14\u7b26\u5408\u4e0a\u8ff0\u89c4\u5219\u7684\uff0c\u90fd\u4e3a\u53ef\u884c\u65b9\u6848\u3002\n\n\n
\u00a0
\n\n\u63d0\u793a\uff1a
\n\nm == isWater.length
n == isWater[i].length
1 <= m, n <= 1000
isWater[i][j]
\u00a0\u8981\u4e48\u662f\u00a00
\u00a0\uff0c\u8981\u4e48\u662f\u00a01
\u00a0\u3002There is a tree (i.e., a connected, undirected graph that has no cycles) consisting of n
nodes numbered from 0
to n - 1
and exactly n - 1
edges. Each node has a value associated with it, and the root of the tree is node 0
.
To represent this tree, you are given an integer array nums
and a 2D array edges
. Each nums[i]
represents the ith
node's value, and each edges[j] = [uj, vj]
represents an edge between nodes uj
and vj
in the tree.
Two values x
and y
are coprime if gcd(x, y) == 1
where gcd(x, y)
is the greatest common divisor of x
and y
.
An ancestor of a node i
is any other node on the shortest path from node i
to the root. A node is not considered an ancestor of itself.
Return an array ans
of size n
, where ans[i]
is the closest ancestor to node i
such that nums[i]
and nums[ans[i]]
are coprime, or -1
if there is no such ancestor.
\n
Example 1:
\n\n\nInput: nums = [2,3,3,2], edges = [[0,1],[1,2],[1,3]]\nOutput: [-1,0,0,1]\nExplanation: In the above figure, each node's value is in parentheses.\n- Node 0 has no coprime ancestors.\n- Node 1 has only one ancestor, node 0. Their values are coprime (gcd(2,3) == 1).\n- Node 2 has two ancestors, nodes 1 and 0. Node 1's value is not coprime (gcd(3,3) == 3), but node 0's\n value is (gcd(2,3) == 1), so node 0 is the closest valid ancestor.\n- Node 3 has two ancestors, nodes 1 and 0. It is coprime with node 1 (gcd(3,2) == 1), so node 1 is its\n closest valid ancestor.\n\n\n
Example 2:
\n\n\nInput: nums = [5,6,10,2,3,6,15], edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]]\nOutput: [-1,0,-1,0,0,0,-1]\n\n\n
\n
Constraints:
\n\nnums.length == n
1 <= nums[i] <= 50
1 <= n <= 10