diff --git a/solution/2800-2899/2819.Minimum Relative Loss After Buying Chocolates/README.md b/solution/2800-2899/2819.Minimum Relative Loss After Buying Chocolates/README.md new file mode 100644 index 0000000000000..065618e92e034 --- /dev/null +++ b/solution/2800-2899/2819.Minimum Relative Loss After Buying Chocolates/README.md @@ -0,0 +1,109 @@ +# [2819. Minimum Relative Loss After Buying Chocolates](https://leetcode.cn/problems/minimum-relative-loss-after-buying-chocolates) + +[English Version](/solution/2800-2899/2819.Minimum%20Relative%20Loss%20After%20Buying%20Chocolates/README_EN.md) + +## 题目描述 + + + +
You are given an integer array prices
, which shows the chocolate prices and a 2D integer array queries
, where queries[i] = [ki, mi]
.
Alice and Bob went to buy some chocolates, and Alice suggested a way to pay for them, and Bob agreed.
+ +The terms for each query are as follows:
+ +ki
, Bob pays for it.ki
of it, and Alice pays the rest.Bob wants to select exactly mi
chocolates such that his relative loss is minimized, more formally, if, in total, Alice has paid ai
and Bob has paid bi
, Bob wants to minimize bi - ai
.
Return an integer array ans
where ans[i]
is Bob's minimum relative loss possible for queries[i]
.
+
Example 1:
+ ++Input: prices = [1,9,22,10,19], queries = [[18,4],[5,2]] +Output: [34,-21] +Explanation: For the 1st query Bob selects the chocolates with prices [1,9,10,22]. He pays 1 + 9 + 10 + 18 = 38 and Alice pays 0 + 0 + 0 + 4 = 4. So Bob's relative loss is 38 - 4 = 34. +For the 2nd query Bob selects the chocolates with prices [19,22]. He pays 5 + 5 = 10 and Alice pays 14 + 17 = 31. So Bob's relative loss is 10 - 31 = -21. +It can be shown that these are the minimum possible relative losses.+ +
Example 2:
+ ++Input: prices = [1,5,4,3,7,11,9], queries = [[5,4],[5,7],[7,3],[4,5]] +Output: [4,16,7,1] +Explanation: For the 1st query Bob selects the chocolates with prices [1,3,9,11]. He pays 1 + 3 + 5 + 5 = 14 and Alice pays 0 + 0 + 4 + 6 = 10. So Bob's relative loss is 14 - 10 = 4. +For the 2nd query Bob has to select all the chocolates. He pays 1 + 5 + 4 + 3 + 5 + 5 + 5 = 28 and Alice pays 0 + 0 + 0 + 0 + 2 + 6 + 4 = 12. So Bob's relative loss is 28 - 12 = 16. +For the 3rd query Bob selects the chocolates with prices [1,3,11] and he pays 1 + 3 + 7 = 11 and Alice pays 0 + 0 + 4 = 4. So Bob's relative loss is 11 - 4 = 7. +For the 4th query Bob selects the chocolates with prices [1,3,7,9,11] and he pays 1 + 3 + 4 + 4 + 4 = 16 and Alice pays 0 + 0 + 3 + 5 + 7 = 15. So Bob's relative loss is 16 - 15 = 1. +It can be shown that these are the minimum possible relative losses. ++ +
Example 3:
+ ++Input: prices = [5,6,7], queries = [[10,1],[5,3],[3,3]] +Output: [5,12,0] +Explanation: For the 1st query Bob selects the chocolate with price 5 and he pays 5 and Alice pays 0. So Bob's relative loss is 5 - 0 = 5. +For the 2nd query Bob has to select all the chocolates. He pays 5 + 5 + 5 = 15 and Alice pays 0 + 1 + 2 = 3. So Bob's relative loss is 15 - 3 = 12. +For the 3rd query Bob has to select all the chocolates. He pays 3 + 3 + 3 = 9 and Alice pays 2 + 3 + 4 = 9. So Bob's relative loss is 9 - 9 = 0. +It can be shown that these are the minimum possible relative losses. ++ +
+
Constraints:
+ +1 <= prices.length == n <= 105
1 <= prices[i] <= 109
1 <= queries.length <= 105
queries[i].length == 2
1 <= ki <= 109
1 <= mi <= n
You are given an integer array prices
, which shows the chocolate prices and a 2D integer array queries
, where queries[i] = [ki, mi]
.
Alice and Bob went to buy some chocolates, and Alice suggested a way to pay for them, and Bob agreed.
+ +The terms for each query are as follows:
+ +ki
, Bob pays for it.ki
of it, and Alice pays the rest.Bob wants to select exactly mi
chocolates such that his relative loss is minimized, more formally, if, in total, Alice has paid ai
and Bob has paid bi
, Bob wants to minimize bi - ai
.
Return an integer array ans
where ans[i]
is Bob's minimum relative loss possible for queries[i]
.
+
Example 1:
+ ++Input: prices = [1,9,22,10,19], queries = [[18,4],[5,2]] +Output: [34,-21] +Explanation: For the 1st query Bob selects the chocolates with prices [1,9,10,22]. He pays 1 + 9 + 10 + 18 = 38 and Alice pays 0 + 0 + 0 + 4 = 4. So Bob's relative loss is 38 - 4 = 34. +For the 2nd query Bob selects the chocolates with prices [19,22]. He pays 5 + 5 = 10 and Alice pays 14 + 17 = 31. So Bob's relative loss is 10 - 31 = -21. +It can be shown that these are the minimum possible relative losses.+ +
Example 2:
+ ++Input: prices = [1,5,4,3,7,11,9], queries = [[5,4],[5,7],[7,3],[4,5]] +Output: [4,16,7,1] +Explanation: For the 1st query Bob selects the chocolates with prices [1,3,9,11]. He pays 1 + 3 + 5 + 5 = 14 and Alice pays 0 + 0 + 4 + 6 = 10. So Bob's relative loss is 14 - 10 = 4. +For the 2nd query Bob has to select all the chocolates. He pays 1 + 5 + 4 + 3 + 5 + 5 + 5 = 28 and Alice pays 0 + 0 + 0 + 0 + 2 + 6 + 4 = 12. So Bob's relative loss is 28 - 12 = 16. +For the 3rd query Bob selects the chocolates with prices [1,3,11] and he pays 1 + 3 + 7 = 11 and Alice pays 0 + 0 + 4 = 4. So Bob's relative loss is 11 - 4 = 7. +For the 4th query Bob selects the chocolates with prices [1,3,7,9,11] and he pays 1 + 3 + 4 + 4 + 4 = 16 and Alice pays 0 + 0 + 3 + 5 + 7 = 15. So Bob's relative loss is 16 - 15 = 1. +It can be shown that these are the minimum possible relative losses. ++ +
Example 3:
+ ++Input: prices = [5,6,7], queries = [[10,1],[5,3],[3,3]] +Output: [5,12,0] +Explanation: For the 1st query Bob selects the chocolate with price 5 and he pays 5 and Alice pays 0. So Bob's relative loss is 5 - 0 = 5. +For the 2nd query Bob has to select all the chocolates. He pays 5 + 5 + 5 = 15 and Alice pays 0 + 1 + 2 = 3. So Bob's relative loss is 15 - 3 = 12. +For the 3rd query Bob has to select all the chocolates. He pays 3 + 3 + 3 = 9 and Alice pays 2 + 3 + 4 = 9. So Bob's relative loss is 9 - 9 = 0. +It can be shown that these are the minimum possible relative losses. ++ +
+
Constraints:
+ +1 <= prices.length == n <= 105
1 <= prices[i] <= 109
1 <= queries.length <= 105
queries[i].length == 2
1 <= ki <= 109
1 <= mi <= n
Table: Votes
++-------------+---------+ +| Column Name | Type | ++-------------+---------+ +| voter | varchar | +| candidate | varchar | ++-------------+---------+ +(voter, candidate) is the primary key for this table. +Each row of this table contains name of the voter and their candidate. ++ +
The election is conducted in a city where everyone can vote for one or more candidates or choose not to vote. Each person has 1
vote so if they vote for multiple candidates, their vote gets equally split across them. For example, if a person votes for 2
candidates, these candidates receive an equivalent of 0.5
votes each.
Write an SQL query to find candidate
who got the most votes and won the election. Output the name of the candidate or If multiple candidates have an equal number of votes, display the names of all of them.
Return the result table ordered by candidate
in ascending order.
The query result format is in the following example.
+ ++
Example 1:
+ ++Input: +Votes table: ++----------+-----------+ +| voter | candidate | ++----------+-----------+ +| Kathy | null | +| Charles | Ryan | +| Charles | Christine | +| Charles | Kathy | +| Benjamin | Christine | +| Anthony | Ryan | +| Edward | Ryan | +| Terry | null | +| Evelyn | Kathy | +| Arthur | Christine | ++----------+-----------+ +Output: ++-----------+ +| candidate | ++-----------+ +| Christine | +| Ryan | ++-----------+ +Explanation: +- Kathy and Terry opted not to participate in voting, resulting in their votes being recorded as 0. Charles distributed his vote among three candidates, equating to 0.33 for each candidate. On the other hand, Benjamin, Arthur, Anthony, Edward, and Evelyn each cast their votes for a single candidate. +- Collectively, Candidate Ryan and Christine amassed a total of 2.33 votes, while Kathy received a combined total of 1.33 votes. +Since Ryan and Christine received an equal number of votes, we will display their names in ascending order.+ +## 解法 + + + +**方法一:窗口函数 + 分组统计** + +我们可以使用窗口函数 `count` 计算每个投票人投给的候选人的票数,然后再使用分组统计函数 `sum` 计算每个候选人的总票数,最后使用窗口函数 `rank` 计算每个候选人的排名,最后筛选出排名第一的候选人即可。 + +注意,结果集中可能会有多个排名第一的候选人,因此我们需要使用 `order by` 对候选人进行排序。 + + + +### **SQL** + + + +```sql +# Write your MySQL query statement below +WITH + T AS ( + SELECT candidate, sum(vote) AS tot + FROM + ( + SELECT + candidate, + 1 / (count(candidate) OVER (PARTITION BY voter)) AS vote + FROM Votes + WHERE candidate IS NOT NULL + ) AS t + GROUP BY 1 + ), + P AS ( + SELECT + candidate, + rank() OVER (ORDER BY tot DESC) AS rk + FROM T + ) +SELECT candidate +FROM P +WHERE rk = 1 +ORDER BY 1; +``` + + diff --git a/solution/2800-2899/2820.Election Results/README_EN.md b/solution/2800-2899/2820.Election Results/README_EN.md new file mode 100644 index 0000000000000..1c43e496cece8 --- /dev/null +++ b/solution/2800-2899/2820.Election Results/README_EN.md @@ -0,0 +1,93 @@ +# [2820. Election Results](https://leetcode.com/problems/election-results) + +[中文文档](/solution/2800-2899/2820.Election%20Results/README.md) + +## Description + +
Table: Votes
++-------------+---------+ +| Column Name | Type | ++-------------+---------+ +| voter | varchar | +| candidate | varchar | ++-------------+---------+ +(voter, candidate) is the primary key for this table. +Each row of this table contains name of the voter and their candidate. ++ +
The election is conducted in a city where everyone can vote for one or more candidates or choose not to vote. Each person has 1
vote so if they vote for multiple candidates, their vote gets equally split across them. For example, if a person votes for 2
candidates, these candidates receive an equivalent of 0.5
votes each.
Write an SQL query to find candidate
who got the most votes and won the election. Output the name of the candidate or If multiple candidates have an equal number of votes, display the names of all of them.
Return the result table ordered by candidate
in ascending order.
The query result format is in the following example.
+ ++
Example 1:
+ ++Input: +Votes table: ++----------+-----------+ +| voter | candidate | ++----------+-----------+ +| Kathy | null | +| Charles | Ryan | +| Charles | Christine | +| Charles | Kathy | +| Benjamin | Christine | +| Anthony | Ryan | +| Edward | Ryan | +| Terry | null | +| Evelyn | Kathy | +| Arthur | Christine | ++----------+-----------+ +Output: ++-----------+ +| candidate | ++-----------+ +| Christine | +| Ryan | ++-----------+ +Explanation: +- Kathy and Terry opted not to participate in voting, resulting in their votes being recorded as 0. Charles distributed his vote among three candidates, equating to 0.33 for each candidate. On the other hand, Benjamin, Arthur, Anthony, Edward, and Evelyn each cast their votes for a single candidate. +- Collectively, Candidate Ryan and Christine amassed a total of 2.33 votes, while Kathy received a combined total of 1.33 votes. +Since Ryan and Christine received an equal number of votes, we will display their names in ascending order.+ +## Solutions + + + +### **SQL** + +```sql +# Write your MySQL query statement below +WITH + T AS ( + SELECT candidate, sum(vote) AS tot + FROM + ( + SELECT + candidate, + 1 / (count(candidate) OVER (PARTITION BY voter)) AS vote + FROM Votes + WHERE candidate IS NOT NULL + ) AS t + GROUP BY 1 + ), + P AS ( + SELECT + candidate, + rank() OVER (ORDER BY tot DESC) AS rk + FROM T + ) +SELECT candidate +FROM P +WHERE rk = 1 +ORDER BY 1; +``` + + diff --git a/solution/2800-2899/2820.Election Results/Solution.sql b/solution/2800-2899/2820.Election Results/Solution.sql new file mode 100644 index 0000000000000..94472ef7e693c --- /dev/null +++ b/solution/2800-2899/2820.Election Results/Solution.sql @@ -0,0 +1,24 @@ +# Write your MySQL query statement below +WITH + T AS ( + SELECT candidate, sum(vote) AS tot + FROM + ( + SELECT + candidate, + 1 / (count(candidate) OVER (PARTITION BY voter)) AS vote + FROM Votes + WHERE candidate IS NOT NULL + ) AS t + GROUP BY 1 + ), + P AS ( + SELECT + candidate, + rank() OVER (ORDER BY tot DESC) AS rk + FROM T + ) +SELECT candidate +FROM P +WHERE rk = 1 +ORDER BY 1; diff --git a/solution/2800-2899/2821.Delay the Resolution of Each Promise/README.md b/solution/2800-2899/2821.Delay the Resolution of Each Promise/README.md new file mode 100644 index 0000000000000..3a954348d6c68 --- /dev/null +++ b/solution/2800-2899/2821.Delay the Resolution of Each Promise/README.md @@ -0,0 +1,74 @@ +# [2821. Delay the Resolution of Each Promise](https://leetcode.cn/problems/delay-the-resolution-of-each-promise) + +[English Version](/solution/2800-2899/2821.Delay%20the%20Resolution%20of%20Each%20Promise/README_EN.md) + +## 题目描述 + + + +
Given an array functions
and a number ms
, return a new array of functions.
functions
is an array of functions that return promises.ms
represents the delay duration in milliseconds. It determines the amount of time to wait before resolving each promise in the new array.Each function in the new array should return a promise that resolves after a delay of ms
milliseconds, preserving the order of the original functions
array. The delayAll
function should ensure that each promise from functions
is executed with a delay, forming the new array of functions returning delayed promises.
+
Example 1:
+ ++Input: +functions = [ + () => new Promise((resolve) => setTimeout(resolve, 30)) +], +ms = 50 +Output: [80] +Explanation: The promise from the array would have resolved after 30 ms, but it was delayed by 50 ms, thus 30 ms + 50 ms = 80 ms. ++ +
Example 2:
+ ++Input: +functions = [ + () => new Promise((resolve) => setTimeout(resolve, 50)), + () => new Promise((resolve) => setTimeout(resolve, 80)) +], +ms = 70 +Output: [120,150] +Explanation: The promises from the array would have resolved after 50 ms and 80 ms, but they were delayed by 70 ms, thus 50 ms + 70 ms = 120 ms and 80 ms + 70 ms = 150 ms. ++ +
+
Constraints:
+ +functions
is an array of functions that return promises10 <= ms <= 500
1 <= functions.length <= 10
Given an array functions
and a number ms
, return a new array of functions.
functions
is an array of functions that return promises.ms
represents the delay duration in milliseconds. It determines the amount of time to wait before resolving each promise in the new array.Each function in the new array should return a promise that resolves after a delay of ms
milliseconds, preserving the order of the original functions
array. The delayAll
function should ensure that each promise from functions
is executed with a delay, forming the new array of functions returning delayed promises.
+
Example 1:
+ ++Input: +functions = [ + () => new Promise((resolve) => setTimeout(resolve, 30)) +], +ms = 50 +Output: [80] +Explanation: The promise from the array would have resolved after 30 ms, but it was delayed by 50 ms, thus 30 ms + 50 ms = 80 ms. ++ +
Example 2:
+ ++Input: +functions = [ + () => new Promise((resolve) => setTimeout(resolve, 50)), + () => new Promise((resolve) => setTimeout(resolve, 80)) +], +ms = 70 +Output: [120,150] +Explanation: The promises from the array would have resolved after 50 ms and 80 ms, but they were delayed by 70 ms, thus 50 ms + 70 ms = 120 ms and 80 ms + 70 ms = 150 ms. ++ +
+
Constraints:
+ +functions
is an array of functions that return promises10 <= ms <= 500
1 <= functions.length <= 10
Given an object obj
, return an inverted object invertedObj
.
The invertedObj
should have the keys of obj
as values and the values of obj
as keys. It is guaranteed that the values in obj
are only strings. The function should handle duplicates, meaning that if there are multiple keys in obj
with the same value, the invertedObj
should map the value to an array containing all corresponding keys.
+
Example 1:
+ ++Input: obj = {"a": "1", "b": "2", "c": "3", "d": "4"} +Output: invertedObj = {"1": "a", "2": "b", "3": "c", "4": "d"} +Explanation: The keys from obj become the values in invertedObj, and the values from obj become the keys in invertedObj. ++ +
Example 2:
+ ++Input: obj = {"a": "1", "b": "2", "c": "2", "d": "4"} +Output: invertedObj = {"1": "a", "2": ["b", "c"], "4": "d"} +Explanation: There are two keys in obj with the same value, the invertedObj mapped the value to an array containing all corresponding keys.+ +
Example 3:
+ ++Input: obj = ["1", "2", "3", "4"] +Output: invertedObj = {"1": "0", "2": "1", "3": "2", "4": "3"} +Explanation: Arrays are also objects therefore array has changed to an object and the keys (indices) from obj become the values in invertedObj, and the values from obj become the keys in invertedObj. ++ +
+
Constraints:
+ +obj
is a valid JSON objecttypeof obj[key] === "string"
2 <= JSON.stringify(obj).length <= 10**5
Given an object obj
, return an inverted object invertedObj
.
The invertedObj
should have the keys of obj
as values and the values of obj
as keys. It is guaranteed that the values in obj
are only strings. The function should handle duplicates, meaning that if there are multiple keys in obj
with the same value, the invertedObj
should map the value to an array containing all corresponding keys.
+
Example 1:
+ ++Input: obj = {"a": "1", "b": "2", "c": "3", "d": "4"} +Output: invertedObj = {"1": "a", "2": "b", "3": "c", "4": "d"} +Explanation: The keys from obj become the values in invertedObj, and the values from obj become the keys in invertedObj. ++ +
Example 2:
+ ++Input: obj = {"a": "1", "b": "2", "c": "2", "d": "4"} +Output: invertedObj = {"1": "a", "2": ["b", "c"], "4": "d"} +Explanation: There are two keys in obj with the same value, the invertedObj mapped the value to an array containing all corresponding keys.+ +
Example 3:
+ ++Input: obj = ["1", "2", "3", "4"] +Output: invertedObj = {"1": "0", "2": "1", "3": "2", "4": "3"} +Explanation: Arrays are also objects therefore array has changed to an object and the keys (indices) from obj become the values in invertedObj, and the values from obj become the keys in invertedObj. ++ +
+
Constraints:
+ +obj
is a valid JSON objecttypeof obj[key] === "string"
2 <= JSON.stringify(obj).length <= 10**5
Given an object obj
and a function fn
, return a filtered object filteredObject
.
Function deepFilter
should perform a deep filter operation on the object obj
. The deep filter operation should remove properties for which the output of the filter function fn
is false
, as well as any empty objects or arrays that remain after the keys have been removed.
If the deep filter operation results in an empty object or array, with no remaining properties, deepFilter
should return undefined
to indicate that there is no valid data left in the filteredObject
.
+
Example 1:
+ ++Input: +obj = [-5, -4, -3, -2, -1, 0, 1], +fn = (x) => x > 0 +Output: [1] +Explanation: All values that were not greater than 0 were removed. ++ +
Example 2:
+ ++Input: +obj = {"a": 1, "b": "2", "c": 3, "d": "4", "e": 5, "f": 6, "g": {"a": 1}}, +fn = (x) => typeof x === "string" +Output: {"b":"2","d":"4"} +Explanation: All keys with values that were not a string were removed. When the object keys were removed during the filtering process, any resulting empty objects were also removed. ++ +
Example 3:
+ ++Input: +obj = [-1, [-1, -1, 5, -1, 10], -1, [-1], [-5]], +fn = (x) => x > 0 +Output: [[5,10]] +Explanation: All values that were not greater than 0 were removed. When the values were removed during the filtering process, any resulting empty arrays were also removed.+ +
Example 4:
+ ++Input: +obj = [[[[5]]]], +fn = (x) => Array.isArray(x) +Output: undefined ++ +
+
Constraints:
+ +fn
is a function that returns a boolean valueobj
is a valid JSON object2 <= JSON.stringify(obj).length <= 10**5
Given an object obj
and a function fn
, return a filtered object filteredObject
.
Function deepFilter
should perform a deep filter operation on the object obj
. The deep filter operation should remove properties for which the output of the filter function fn
is false
, as well as any empty objects or arrays that remain after the keys have been removed.
If the deep filter operation results in an empty object or array, with no remaining properties, deepFilter
should return undefined
to indicate that there is no valid data left in the filteredObject
.
+
Example 1:
+ ++Input: +obj = [-5, -4, -3, -2, -1, 0, 1], +fn = (x) => x > 0 +Output: [1] +Explanation: All values that were not greater than 0 were removed. ++ +
Example 2:
+ ++Input: +obj = {"a": 1, "b": "2", "c": 3, "d": "4", "e": 5, "f": 6, "g": {"a": 1}}, +fn = (x) => typeof x === "string" +Output: {"b":"2","d":"4"} +Explanation: All keys with values that were not a string were removed. When the object keys were removed during the filtering process, any resulting empty objects were also removed. ++ +
Example 3:
+ ++Input: +obj = [-1, [-1, -1, 5, -1, 10], -1, [-1], [-5]], +fn = (x) => x > 0 +Output: [[5,10]] +Explanation: All values that were not greater than 0 were removed. When the values were removed during the filtering process, any resulting empty arrays were also removed.+ +
Example 4:
+ ++Input: +obj = [[[[5]]]], +fn = (x) => Array.isArray(x) +Output: undefined ++ +
+
Constraints:
+ +fn
is a function that returns a boolean valueobj
is a valid JSON object2 <= JSON.stringify(obj).length <= 10**5