diff --git a/solution/0500-0599/0511.Game Play Analysis I/README.md b/solution/0500-0599/0511.Game Play Analysis I/README.md index 8521589a8115a..bd38ee1441458 100644 --- a/solution/0500-0599/0511.Game Play Analysis I/README.md +++ b/solution/0500-0599/0511.Game Play Analysis I/README.md @@ -54,7 +54,7 @@ Result 表: -**方法一:GROUP BY** +**方法一:分组求最小值** 我们可以用 `GROUP BY` 对 `player_id` 进行分组,然后取每一组中最小的 `event_date` 作为玩家第一次登录平台的日期。 @@ -66,7 +66,7 @@ Result 表: # Write your MySQL query statement below SELECT player_id, min(event_date) AS first_login FROM Activity -GROUP BY player_id; +GROUP BY 1; ``` diff --git a/solution/0500-0599/0511.Game Play Analysis I/README_EN.md b/solution/0500-0599/0511.Game Play Analysis I/README_EN.md index 9bc82bdfe5eed..93c17d98f12cc 100644 --- a/solution/0500-0599/0511.Game Play Analysis I/README_EN.md +++ b/solution/0500-0599/0511.Game Play Analysis I/README_EN.md @@ -55,6 +55,10 @@ Activity table: ## Solutions +**Solution 1: Group By + Min Function** + +We can use `GROUP BY` to group the `player_id` and then take the minimum `event_date` in each group as the date when the player first logged into the platform. + ### **SQL** @@ -63,7 +67,7 @@ Activity table: # Write your MySQL query statement below SELECT player_id, min(event_date) AS first_login FROM Activity -GROUP BY player_id; +GROUP BY 1; ``` diff --git a/solution/0500-0599/0511.Game Play Analysis I/Solution.sql b/solution/0500-0599/0511.Game Play Analysis I/Solution.sql index d84f2256c0cc1..883f271c5424a 100644 --- a/solution/0500-0599/0511.Game Play Analysis I/Solution.sql +++ b/solution/0500-0599/0511.Game Play Analysis I/Solution.sql @@ -1,4 +1,4 @@ # Write your MySQL query statement below SELECT player_id, min(event_date) AS first_login FROM Activity -GROUP BY player_id; +GROUP BY 1; diff --git a/solution/0500-0599/0586.Customer Placing the Largest Number of Orders/README.md b/solution/0500-0599/0586.Customer Placing the Largest Number of Orders/README.md index 5ee3f6548b7c4..f0ff6183ef92f 100644 --- a/solution/0500-0599/0586.Customer Placing the Largest Number of Orders/README.md +++ b/solution/0500-0599/0586.Customer Placing the Largest Number of Orders/README.md @@ -61,6 +61,10 @@ customer_number 为 '3' 的顾客有两个订单,比顾客 '1' 或者 '2' 都 +**方法一:分组 + 排序** + +我们可以使用 `GROUP BY` 将数据按照 `customer_number` 进行分组,然后按照 `count(1)` 进行降序排序,最后取第一条记录的 `customer_number` 即可。 + ### **SQL** diff --git a/solution/0500-0599/0586.Customer Placing the Largest Number of Orders/README_EN.md b/solution/0500-0599/0586.Customer Placing the Largest Number of Orders/README_EN.md index cdcc82cac32b7..95dde303f80d3 100644 --- a/solution/0500-0599/0586.Customer Placing the Largest Number of Orders/README_EN.md +++ b/solution/0500-0599/0586.Customer Placing the Largest Number of Orders/README_EN.md @@ -55,6 +55,10 @@ So the result is customer_number 3. ## Solutions +**Solution 1: Group By + Sorting** + +We can use `GROUP BY` to group the data by `customer_number`, and then sort the groups in descending order by `count(1)`. Finally, we can take the `customer_number` of the first record as the result. + ### **SQL** diff --git a/solution/1200-1299/1212.Team Scores in Football Tournament/README.md b/solution/1200-1299/1212.Team Scores in Football Tournament/README.md index e7491550b79fb..85ab972efba73 100644 --- a/solution/1200-1299/1212.Team Scores in Football Tournament/README.md +++ b/solution/1200-1299/1212.Team Scores in Football Tournament/README.md @@ -95,6 +95,18 @@ Teams table: +**方法一:左连接 + 分组 + CASE 表达式** + +我们可以通过左连接,将 `Teams` 表和 `Matches` 表连接起来,连接的条件为 `team_id = host_team OR team_id = guest_team`,这样就可以得到每个球队的所有比赛信息。 + +接下来,我们按照 `team_id` 分组,然后使用 `CASE` 表达式计算每个球队的积分,计算规则如下: + +- 如果球队是主队,且主队进球数大于客队进球数,则积分加 $3$ 分; +- 如果球队是客队,且客队进球数大于主队进球数,则积分加 $3$ 分; +- 如果主队和客队进球数相同,则积分加 $1$ 分; + +最后,我们按照积分降序排序,如果积分相同,则按照 `team_id` 升序排序。 + ### **SQL** @@ -102,23 +114,23 @@ Teams table: ```sql # Write your MySQL query statement below SELECT - t.team_id, - t.team_name, - SUM( + team_id, + team_name, + sum( CASE - WHEN t.team_id = m.host_team - AND m.host_goals > m.guest_goals THEN 3 - WHEN m.host_goals = m.guest_goals THEN 1 - WHEN t.team_id = m.guest_team - AND m.guest_goals > m.host_goals THEN 3 + WHEN team_id = host_team + AND host_goals > guest_goals THEN 3 + WHEN team_id = guest_team + AND guest_goals > host_goals THEN 3 + WHEN host_goals = guest_goals THEN 1 ELSE 0 END ) AS num_points FROM - Teams AS t - LEFT JOIN Matches AS m ON t.team_id = m.host_team OR t.team_id = m.guest_team -GROUP BY t.team_id -ORDER BY num_points DESC, team_id; + Teams + LEFT JOIN Matches ON team_id = host_team OR team_id = guest_team +GROUP BY 1 +ORDER BY 3 DESC, 1; ``` diff --git a/solution/1200-1299/1212.Team Scores in Football Tournament/README_EN.md b/solution/1200-1299/1212.Team Scores in Football Tournament/README_EN.md index 604897cb2482a..0a0d043714aa3 100644 --- a/solution/1200-1299/1212.Team Scores in Football Tournament/README_EN.md +++ b/solution/1200-1299/1212.Team Scores in Football Tournament/README_EN.md @@ -90,6 +90,18 @@ Matches table: ## Solutions +**Solution 1: Left Join + Group By + Case Expression** + +We can join the `Teams` table and the `Matches` table using a left join, where the join condition is `team_id = host_team OR team_id = guest_team`, to obtain all the match information for each team. + +Next, we group by `team_id` and use a `CASE` expression to calculate the points for each team according to the following rules: + +- If the team is the host team and has more goals than the guest team, add $3$ points to the team's score. +- If the team is the guest team and has more goals than the host team, add $3$ points to the team's score. +- If the host team and the guest team have the same number of goals, add $1$ point to the team's score. + +Finally, we sort the result by points in descending order, and if the points are the same, we sort by `team_id` in ascending order. + ### **SQL** @@ -97,23 +109,23 @@ Matches table: ```sql # Write your MySQL query statement below SELECT - t.team_id, - t.team_name, - SUM( + team_id, + team_name, + sum( CASE - WHEN t.team_id = m.host_team - AND m.host_goals > m.guest_goals THEN 3 - WHEN m.host_goals = m.guest_goals THEN 1 - WHEN t.team_id = m.guest_team - AND m.guest_goals > m.host_goals THEN 3 + WHEN team_id = host_team + AND host_goals > guest_goals THEN 3 + WHEN team_id = guest_team + AND guest_goals > host_goals THEN 3 + WHEN host_goals = guest_goals THEN 1 ELSE 0 END ) AS num_points FROM - Teams AS t - LEFT JOIN Matches AS m ON t.team_id = m.host_team OR t.team_id = m.guest_team -GROUP BY t.team_id -ORDER BY num_points DESC, team_id; + Teams + LEFT JOIN Matches ON team_id = host_team OR team_id = guest_team +GROUP BY 1 +ORDER BY 3 DESC, 1; ``` diff --git a/solution/1200-1299/1212.Team Scores in Football Tournament/Solution.sql b/solution/1200-1299/1212.Team Scores in Football Tournament/Solution.sql index a2fc39a920922..536374a7f73b3 100644 --- a/solution/1200-1299/1212.Team Scores in Football Tournament/Solution.sql +++ b/solution/1200-1299/1212.Team Scores in Football Tournament/Solution.sql @@ -1,19 +1,19 @@ # Write your MySQL query statement below SELECT - t.team_id, - t.team_name, - SUM( + team_id, + team_name, + sum( CASE - WHEN t.team_id = m.host_team - AND m.host_goals > m.guest_goals THEN 3 - WHEN m.host_goals = m.guest_goals THEN 1 - WHEN t.team_id = m.guest_team - AND m.guest_goals > m.host_goals THEN 3 + WHEN team_id = host_team + AND host_goals > guest_goals THEN 3 + WHEN team_id = guest_team + AND guest_goals > host_goals THEN 3 + WHEN host_goals = guest_goals THEN 1 ELSE 0 END ) AS num_points FROM - Teams AS t - LEFT JOIN Matches AS m ON t.team_id = m.host_team OR t.team_id = m.guest_team -GROUP BY t.team_id -ORDER BY num_points DESC, team_id; + Teams + LEFT JOIN Matches ON team_id = host_team OR team_id = guest_team +GROUP BY 1 +ORDER BY 3 DESC, 1; diff --git a/solution/1400-1499/1440.Evaluate Boolean Expression/README.md b/solution/1400-1499/1440.Evaluate Boolean Expression/README.md index 2c848803ed29d..810098e7538cb 100644 --- a/solution/1400-1499/1440.Evaluate Boolean Expression/README.md +++ b/solution/1400-1499/1440.Evaluate Boolean Expression/README.md @@ -90,6 +90,10 @@ Expressions 表: +**方法一:等值连接 + CASE 表达式** + +我们可以通过等值连接,将 `Expressions` 表中的每一行与 `Variables` 表中的两行进行关联,关联的条件是 `left_operand = name` 和 `right_operand = name`,然后通过 `CASE` 表达式来判断布尔表达式的值。如果 `operator` 为 `=`,则判断两个值是否相等;如果 `operator` 为 `>`,则判断左值是否大于右值;如果 `operator` 为 `<`,则判断左值是否小于右值。若是,那么布尔表达式的值为 `true`,否则为 `false`。 + ### **SQL** @@ -102,16 +106,16 @@ SELECT right_operand, CASE WHEN ( - (e.operator = '=' AND v1.value = v2.value) - OR (e.operator = '>' AND v1.value > v2.value) - OR (e.operator = '<' AND v1.value < v2.value) + (operator = '=' AND v1.value = v2.value) + OR (operator = '>' AND v1.value > v2.value) + OR (operator = '<' AND v1.value < v2.value) ) THEN 'true' ELSE 'false' END AS value FROM Expressions AS e - LEFT JOIN Variables AS v1 ON e.left_operand = v1.name - LEFT JOIN Variables AS v2 ON e.right_operand = v2.name; + JOIN Variables AS v1 ON e.left_operand = v1.name + JOIN Variables AS v2 ON e.right_operand = v2.name; ``` diff --git a/solution/1400-1499/1440.Evaluate Boolean Expression/README_EN.md b/solution/1400-1499/1440.Evaluate Boolean Expression/README_EN.md index 64c863b9fd700..0de49de7f6b0d 100644 --- a/solution/1400-1499/1440.Evaluate Boolean Expression/README_EN.md +++ b/solution/1400-1499/1440.Evaluate Boolean Expression/README_EN.md @@ -83,6 +83,10 @@ As shown, you need to find the value of each boolean expression in the table usi ## Solutions +**Solution 1: Equi-Join + CASE Expression** + +We can associate each row in the `Expressions` table with two rows in the `Variables` table using an equi-join, where the conditions for the association are `left_operand = name` and `right_operand = name`. Then, we can use a `CASE` expression to determine the value of the boolean expression. If the `operator` is `=`, we check if the two values are equal. If the `operator` is `>`, we check if the left value is greater than the right value. If the `operator` is `<`, we check if the left value is less than the right value. If the condition is true, the boolean expression evaluates to `true`, otherwise it evaluates to `false`. + ### **SQL** @@ -95,16 +99,16 @@ SELECT right_operand, CASE WHEN ( - (e.operator = '=' AND v1.value = v2.value) - OR (e.operator = '>' AND v1.value > v2.value) - OR (e.operator = '<' AND v1.value < v2.value) + (operator = '=' AND v1.value = v2.value) + OR (operator = '>' AND v1.value > v2.value) + OR (operator = '<' AND v1.value < v2.value) ) THEN 'true' ELSE 'false' END AS value FROM Expressions AS e - LEFT JOIN Variables AS v1 ON e.left_operand = v1.name - LEFT JOIN Variables AS v2 ON e.right_operand = v2.name; + JOIN Variables AS v1 ON e.left_operand = v1.name + JOIN Variables AS v2 ON e.right_operand = v2.name; ``` diff --git a/solution/1400-1499/1440.Evaluate Boolean Expression/Solution.sql b/solution/1400-1499/1440.Evaluate Boolean Expression/Solution.sql index 837cd6bc06ea3..cc61b2c26fc22 100644 --- a/solution/1400-1499/1440.Evaluate Boolean Expression/Solution.sql +++ b/solution/1400-1499/1440.Evaluate Boolean Expression/Solution.sql @@ -5,13 +5,13 @@ SELECT right_operand, CASE WHEN ( - (e.operator = '=' AND v1.value = v2.value) - OR (e.operator = '>' AND v1.value > v2.value) - OR (e.operator = '<' AND v1.value < v2.value) + (operator = '=' AND v1.value = v2.value) + OR (operator = '>' AND v1.value > v2.value) + OR (operator = '<' AND v1.value < v2.value) ) THEN 'true' ELSE 'false' END AS value FROM Expressions AS e - LEFT JOIN Variables AS v1 ON e.left_operand = v1.name - LEFT JOIN Variables AS v2 ON e.right_operand = v2.name; + JOIN Variables AS v1 ON e.left_operand = v1.name + JOIN Variables AS v2 ON e.right_operand = v2.name; diff --git a/solution/1500-1599/1571.Warehouse Manager/README.md b/solution/1500-1599/1571.Warehouse Manager/README.md index 1a35c5f8dbedd..d5650e8f82cb8 100644 --- a/solution/1500-1599/1571.Warehouse Manager/README.md +++ b/solution/1500-1599/1571.Warehouse Manager/README.md @@ -96,6 +96,10 @@ Id为4的商品(LC-T-Shirt)的存货量为 4x10x20 = 800 +**方法一:等值连接 + 分组求和** + +我们可以使用等值连接将 `Warehouse` 表和 `Products` 表按照 `product_id` 进行连接,并按照仓库名称进行分组,然后使用 `SUM` 函数计算每个仓库的存货量。 + ### **SQL** @@ -104,11 +108,11 @@ Id为4的商品(LC-T-Shirt)的存货量为 4x10x20 = 800 # Write your MySQL query statement below SELECT name AS warehouse_name, - SUM(units * Width * Length * Height) AS volume + sum(width * length * height * units) AS volume FROM - Warehouse AS w - JOIN Products AS p ON w.product_id = p.product_id -GROUP BY name; + Warehouse + JOIN Products USING (product_id) +GROUP BY 1; ``` diff --git a/solution/1500-1599/1571.Warehouse Manager/README_EN.md b/solution/1500-1599/1571.Warehouse Manager/README_EN.md index bf22795cf06d1..4ceb5429571f2 100644 --- a/solution/1500-1599/1571.Warehouse Manager/README_EN.md +++ b/solution/1500-1599/1571.Warehouse Manager/README_EN.md @@ -92,6 +92,10 @@ LCHouse3: 1 unit of LC-T-Shirt. ## Solutions +**Solution 1: Inner Join + Group By + Sum Function** + +We can use an inner join to join the `Warehouse` table and the `Products` table on the condition of `product_id`, and then group by warehouse name to calculate the inventory of each warehouse using the `SUM` function. + ### **SQL** @@ -100,11 +104,11 @@ LCHouse3: 1 unit of LC-T-Shirt. # Write your MySQL query statement below SELECT name AS warehouse_name, - SUM(units * Width * Length * Height) AS volume + sum(width * length * height * units) AS volume FROM - Warehouse AS w - JOIN Products AS p ON w.product_id = p.product_id -GROUP BY name; + Warehouse + JOIN Products USING (product_id) +GROUP BY 1; ``` diff --git a/solution/1500-1599/1571.Warehouse Manager/Solution.sql b/solution/1500-1599/1571.Warehouse Manager/Solution.sql index b7f3b38bd1887..85c6d2d0d22c1 100644 --- a/solution/1500-1599/1571.Warehouse Manager/Solution.sql +++ b/solution/1500-1599/1571.Warehouse Manager/Solution.sql @@ -1,8 +1,8 @@ # Write your MySQL query statement below SELECT name AS warehouse_name, - SUM(units * Width * Length * Height) AS volume + sum(width * length * height * units) AS volume FROM - Warehouse AS w - JOIN Products AS p ON w.product_id = p.product_id -GROUP BY name; + Warehouse + JOIN Products USING (product_id) +GROUP BY 1; diff --git a/solution/1700-1799/1741.Find Total Time Spent by Each Employee/README.md b/solution/1700-1799/1741.Find Total Time Spent by Each Employee/README.md index 18cbf121c32e6..4e3cd04fb8aa5 100644 --- a/solution/1700-1799/1741.Find Total Time Spent by Each Employee/README.md +++ b/solution/1700-1799/1741.Find Total Time Spent by Each Employee/README.md @@ -65,17 +65,19 @@ Employees table: +**方法一:分组求和** + +我们可以先按照 `emp_id` 和 `event_day` 进行分组,然后计算每个分组的总时间。总时间等于每个分组的 `out_time` 减去 `in_time` 的和。 + ### **SQL** ```sql -SELECT - event_day AS day, - emp_id, - SUM(out_time - in_time) AS total_time +# Write your MySQL query statement below +SELECT event_day AS day, emp_id, sum(out_time - in_time) AS total_time FROM Employees -GROUP BY emp_id, event_day; +GROUP BY 1, 2; ``` diff --git a/solution/1700-1799/1741.Find Total Time Spent by Each Employee/README_EN.md b/solution/1700-1799/1741.Find Total Time Spent by Each Employee/README_EN.md index 9e15debc48392..848fa18a71396 100644 --- a/solution/1700-1799/1741.Find Total Time Spent by Each Employee/README_EN.md +++ b/solution/1700-1799/1741.Find Total Time Spent by Each Employee/README_EN.md @@ -61,17 +61,19 @@ Employee 2 has two events: one on day 2020-11-28 with a total of (33 - 3) = 30, ## Solutions +**Solution 1: Group By + Sum Function** + +We can first group by `emp_id` and `event_day`, and then calculate the total time for each group. The total time is equal to the sum of the differences between `out_time` and `in_time` for each record in the group. + ### **SQL** ```sql -SELECT - event_day AS day, - emp_id, - SUM(out_time - in_time) AS total_time +# Write your MySQL query statement below +SELECT event_day AS day, emp_id, sum(out_time - in_time) AS total_time FROM Employees -GROUP BY emp_id, event_day; +GROUP BY 1, 2; ``` diff --git a/solution/1700-1799/1741.Find Total Time Spent by Each Employee/Solution.sql b/solution/1700-1799/1741.Find Total Time Spent by Each Employee/Solution.sql index 8a9a8ea5b6fdc..81a2b3361c5cc 100644 --- a/solution/1700-1799/1741.Find Total Time Spent by Each Employee/Solution.sql +++ b/solution/1700-1799/1741.Find Total Time Spent by Each Employee/Solution.sql @@ -1,6 +1,4 @@ -SELECT - event_day AS day, - emp_id, - SUM(out_time - in_time) AS total_time +# Write your MySQL query statement below +SELECT event_day AS day, emp_id, sum(out_time - in_time) AS total_time FROM Employees -GROUP BY emp_id, event_day; +GROUP BY 1, 2; diff --git a/solution/1700-1799/1742.Maximum Number of Balls in a Box/README.md b/solution/1700-1799/1742.Maximum Number of Balls in a Box/README.md index 7c44bbe99f6fa..8be6270a46148 100644 --- a/solution/1700-1799/1742.Maximum Number of Balls in a Box/README.md +++ b/solution/1700-1799/1742.Maximum Number of Balls in a Box/README.md @@ -60,9 +60,9 @@ **方法一:数组 + 模拟** -观察题目的数据范围,小球的编号最大不超过 $10^5$,那么每个编号的各个位数之和的最大值小于 $50$。因此,我们可以直接开一个长度为 $50$ 的数组 `cnt` 来统计每个编号的各个位数之和的数量。 +观察题目的数据范围,小球的编号最大不超过 $10^5$,那么每个编号的各个位数之和的最大值小于 $50$。因此,我们可以直接开一个长度为 $50$ 的数组 $cnt$ 来统计每个编号的各个位数之和的数量。 -答案就是数组 `cnt` 中的最大值。 +答案就是数组 $cnt$ 中的最大值。 时间复杂度 $O(n \times \log_{10}m)$。其中 $n = highLimit - lowLimit + 1$,而 $m = highLimit$。 @@ -144,6 +144,22 @@ func countBalls(lowLimit int, highLimit int) (ans int) { } ``` +### **TypeScript** + +```ts +function countBalls(lowLimit: number, highLimit: number): number { + const cnt: number[] = Array(50).fill(0); + for (let i = lowLimit; i <= highLimit; ++i) { + let y = 0; + for (let x = i; x; x = Math.floor(x / 10)) { + y += x % 10; + } + ++cnt[y]; + } + return Math.max(...cnt); +} +``` + ### **...** ``` diff --git a/solution/1700-1799/1742.Maximum Number of Balls in a Box/README_EN.md b/solution/1700-1799/1742.Maximum Number of Balls in a Box/README_EN.md index 2dbca821c2cbd..10bec9c0f03cf 100644 --- a/solution/1700-1799/1742.Maximum Number of Balls in a Box/README_EN.md +++ b/solution/1700-1799/1742.Maximum Number of Balls in a Box/README_EN.md @@ -52,6 +52,14 @@ Box 10 has the most number of balls with 2 balls. ## Solutions +**Solution 1: Array + Simulation** + +Observing the data range of the problem, the maximum number of the ball does not exceed $10^5$, so the maximum value of the sum of each digit of the number is less than $50$. Therefore, we can directly create an array $cnt$ with a length of $50$ to count the number of each digit sum of each number. + +The answer is the maximum value in the array $cnt$. + +The time complexity is $O(n \times \log_{10}m)$. Here, $n = highLimit - lowLimit + 1$, and $m = highLimit$. + ### **Python3** @@ -126,6 +134,22 @@ func countBalls(lowLimit int, highLimit int) (ans int) { } ``` +### **TypeScript** + +```ts +function countBalls(lowLimit: number, highLimit: number): number { + const cnt: number[] = Array(50).fill(0); + for (let i = lowLimit; i <= highLimit; ++i) { + let y = 0; + for (let x = i; x; x = Math.floor(x / 10)) { + y += x % 10; + } + ++cnt[y]; + } + return Math.max(...cnt); +} +``` + ### **...** ``` diff --git a/solution/1700-1799/1742.Maximum Number of Balls in a Box/Solution.ts b/solution/1700-1799/1742.Maximum Number of Balls in a Box/Solution.ts new file mode 100644 index 0000000000000..2d680701a47f3 --- /dev/null +++ b/solution/1700-1799/1742.Maximum Number of Balls in a Box/Solution.ts @@ -0,0 +1,11 @@ +function countBalls(lowLimit: number, highLimit: number): number { + const cnt: number[] = Array(50).fill(0); + for (let i = lowLimit; i <= highLimit; ++i) { + let y = 0; + for (let x = i; x; x = Math.floor(x / 10)) { + y += x % 10; + } + ++cnt[y]; + } + return Math.max(...cnt); +} diff --git a/solution/1800-1899/1890.The Latest Login in 2020/README.md b/solution/1800-1899/1890.The Latest Login in 2020/README.md index 944ddaf275844..b1013a731dff1 100644 --- a/solution/1800-1899/1890.The Latest Login in 2020/README.md +++ b/solution/1800-1899/1890.The Latest Login in 2020/README.md @@ -65,6 +65,10 @@ Logins 表: +**方法一:分组求最大值** + +我们可以先筛选出 2020 年的登录记录,并且按照 `user_id` 分组,然后利用 `max` 函数求出每个用户的最大登录时间。 + ### **SQL** @@ -72,12 +76,11 @@ Logins 表: ```sql -SELECT - user_id, - MAX(time_stamp) AS last_stamp +# Write your MySQL query statement below +SELECT user_id, max(time_stamp) AS last_stamp FROM Logins -WHERE YEAR(time_stamp) = 2020 -GROUP BY user_id; +WHERE year(time_stamp) = 2020 +GROUP BY 1; ``` diff --git a/solution/1800-1899/1890.The Latest Login in 2020/README_EN.md b/solution/1800-1899/1890.The Latest Login in 2020/README_EN.md index bc5ce51fdd225..c1bdf282b6aa9 100644 --- a/solution/1800-1899/1890.The Latest Login in 2020/README_EN.md +++ b/solution/1800-1899/1890.The Latest Login in 2020/README_EN.md @@ -61,17 +61,20 @@ User 14 did not login in 2020, so we do not include them in the result table. ## Solutions +**Solution 1: Group By + Max Function** + +We can first filter out the login records in 2020, and then group by `user_id`, and use the `max` function to calculate the maximum login time for each user. + ### **SQL** ```sql -SELECT - user_id, - MAX(time_stamp) AS last_stamp +# Write your MySQL query statement below +SELECT user_id, max(time_stamp) AS last_stamp FROM Logins -WHERE YEAR(time_stamp) = 2020 -GROUP BY user_id; +WHERE year(time_stamp) = 2020 +GROUP BY 1; ``` diff --git a/solution/1800-1899/1890.The Latest Login in 2020/Solution.sql b/solution/1800-1899/1890.The Latest Login in 2020/Solution.sql index 6040ad16ffd63..fb82458255255 100644 --- a/solution/1800-1899/1890.The Latest Login in 2020/Solution.sql +++ b/solution/1800-1899/1890.The Latest Login in 2020/Solution.sql @@ -1,6 +1,5 @@ -SELECT - user_id, - MAX(time_stamp) AS last_stamp +# Write your MySQL query statement below +SELECT user_id, max(time_stamp) AS last_stamp FROM Logins -WHERE YEAR(time_stamp) = 2020 -GROUP BY user_id; +WHERE year(time_stamp) = 2020 +GROUP BY 1; diff --git a/solution/2500-2599/2512.Reward Top K Students/README.md b/solution/2500-2599/2512.Reward Top K Students/README.md index f9b6df3f85196..ce582a2993428 100644 --- a/solution/2500-2599/2512.Reward Top K Students/README.md +++ b/solution/2500-2599/2512.Reward Top K Students/README.md @@ -57,6 +57,16 @@ +**方法一:哈希表 + 排序** + +我们可以将正面的单词存入哈希表 $ps$ 中,将负面的单词存入哈希表 $ns$ 中。 + +然后遍历 $report$,对于每个学生,我们将其得分存入数组 $arr$ 中,数组中的每个元素为一个二元组 $(t, sid)$,其中 $t$ 表示学生的得分,而 $sid$ 表示学生的 ID。 + +最后我们对数组 $arr$ 按照得分从高到低排序,如果得分相同则按照 ID 从小到大排序,然后取出前 $k$ 个学生的 ID 即可。 + +时间复杂度 $O(n \times \log n + (|ps| + |ns| + n) \times |s|)$,空间复杂度 $O((|ps|+|ns|) \times |s| + n)$。其中 $n$ 为学生数量,$|ps|$ 和 $|ns|$ 分别为正面和负面单词的数量,$|s|$ 为单词的平均长度。 + ### **Python3** @@ -105,7 +115,7 @@ class Solution { ns.add(s); } int n = report.length; - int[][] arr = new int[n][2]; + int[][] arr = new int[n][0]; for (int i = 0; i < n; ++i) { int sid = student_id[i]; int t = 0; diff --git a/solution/2500-2599/2512.Reward Top K Students/README_EN.md b/solution/2500-2599/2512.Reward Top K Students/README_EN.md index 9c37979b5d5ef..c9853482e2910 100644 --- a/solution/2500-2599/2512.Reward Top K Students/README_EN.md +++ b/solution/2500-2599/2512.Reward Top K Students/README_EN.md @@ -53,6 +53,16 @@ Since student 2 has more points, [2,1] is returned. ## Solutions +**Solution 1: Hash Table + Sorting** + +We can store the positive words in a hash table $ps$ and the negative words in a hash table $ns$. + +Then, we traverse the $report$ and for each student, we store their score in an array $arr$, where each element is a tuple $(t, sid)$, where $t$ represents the student's score and $sid$ represents the student's ID. + +Finally, we sort the array $arr$ in descending order by score, and if the scores are the same, we sort by ID in ascending order. Then, we take the IDs of the top $k$ students. + +The time complexity is $O(n \times \log n + (|ps| + |ns| + n) \times |s|)$, and the space complexity is $O((|ps|+|ns|) \times |s| + n)$. Here, $n$ is the number of students, $|ps|$ and $|ns|$ are the number of positive and negative words, respectively, and $|s|$ is the average length of a word. + ### **Python3** @@ -97,7 +107,7 @@ class Solution { ns.add(s); } int n = report.length; - int[][] arr = new int[n][2]; + int[][] arr = new int[n][0]; for (int i = 0; i < n; ++i) { int sid = student_id[i]; int t = 0; diff --git a/solution/2500-2599/2512.Reward Top K Students/Solution.java b/solution/2500-2599/2512.Reward Top K Students/Solution.java index dea1d0823987e..e75191ead3e5f 100644 --- a/solution/2500-2599/2512.Reward Top K Students/Solution.java +++ b/solution/2500-2599/2512.Reward Top K Students/Solution.java @@ -1,33 +1,33 @@ -class Solution { - public List topStudents(String[] positive_feedback, String[] negative_feedback, - String[] report, int[] student_id, int k) { - Set ps = new HashSet<>(); - Set ns = new HashSet<>(); - for (var s : positive_feedback) { - ps.add(s); - } - for (var s : negative_feedback) { - ns.add(s); - } - int n = report.length; - int[][] arr = new int[n][2]; - for (int i = 0; i < n; ++i) { - int sid = student_id[i]; - int t = 0; - for (var s : report[i].split(" ")) { - if (ps.contains(s)) { - t += 3; - } else if (ns.contains(s)) { - t -= 1; - } - } - arr[i] = new int[] {t, sid}; - } - Arrays.sort(arr, (a, b) -> a[0] == b[0] ? a[1] - b[1] : b[0] - a[0]); - List ans = new ArrayList<>(); - for (int i = 0; i < k; ++i) { - ans.add(arr[i][1]); - } - return ans; - } +class Solution { + public List topStudents(String[] positive_feedback, String[] negative_feedback, + String[] report, int[] student_id, int k) { + Set ps = new HashSet<>(); + Set ns = new HashSet<>(); + for (var s : positive_feedback) { + ps.add(s); + } + for (var s : negative_feedback) { + ns.add(s); + } + int n = report.length; + int[][] arr = new int[n][0]; + for (int i = 0; i < n; ++i) { + int sid = student_id[i]; + int t = 0; + for (var s : report[i].split(" ")) { + if (ps.contains(s)) { + t += 3; + } else if (ns.contains(s)) { + t -= 1; + } + } + arr[i] = new int[] {t, sid}; + } + Arrays.sort(arr, (a, b) -> a[0] == b[0] ? a[1] - b[1] : b[0] - a[0]); + List ans = new ArrayList<>(); + for (int i = 0; i < k; ++i) { + ans.add(arr[i][1]); + } + return ans; + } } \ No newline at end of file