楼主: Myron2017
跳转到指定楼层
上一主题 下一主题
收起左侧

刷题记录帖子

🔗
 楼主| Myron2017 2026-11-23 12:09:03 | 只看该作者
全局:
3052. Maximize Items
Hard
Topics
SQL Schema
Pandas Schema
Table: Inventory

+----------------+---------+
| Column Name    | Type    |
+----------------+---------+
| item_id        | int     |
| item_type      | varchar |
| item_category  | varchar |
| square_footage | decimal |
+----------------+---------+
item_id is the column of unique values for this table.
Each row includes item id, item type, item category and sqaure footage.
Leetcode warehouse wants to maximize the number of items it can stock in a 500,000 square feet warehouse. It wants to stock as many prime items as possible, and afterwards use the remaining square footage to stock the most number of non-prime items.

Write a solution to find the number of prime and non-prime items that can be stored in the 500,000 square feet warehouse. Output the item type with prime_eligible followed by not_prime and the maximum number of items that can be stocked.

Note:

Item count must be a whole number (integer).
If the count for the not_prime category is 0, you should output 0 for that particular category.
Return the result table ordered by item count in descending order.

The result format is in the following example.



Example 1:

Input:
Inventory table:
+---------+----------------+---------------+----------------+
| item_id | item_type      | item_category | square_footage |
+---------+----------------+---------------+----------------+
| 1374    | prime_eligible | Watches       | 68.00          |
| 4245    | not_prime      | Art           | 26.40          |
| 5743    | prime_eligible | Software      | 325.00         |
| 8543    | not_prime      | Clothing      | 64.50          |  
| 2556    | not_prime      | Shoes         | 15.00          |
| 2452    | prime_eligible | Scientific    | 85.00          |
| 3255    | not_prime      | Furniture     | 22.60          |
| 1672    | prime_eligible | Beauty        | 8.50           |  
| 4256    | prime_eligible | Furniture     | 55.50          |
| 6325    | prime_eligible | Food          | 13.20          |
+---------+----------------+---------------+----------------+
Output:
+----------------+-------------+
| item_type      | item_count  |
+----------------+-------------+
| prime_eligible | 5400        |
| not_prime      | 8           |
+----------------+-------------+
Explanation:
- The prime-eligible category comprises a total of 6 items, amounting to a combined square footage of 555.20 (68 + 325 + 85 + 8.50 + 55.50 + 13.20). It is possible to store 900 combinations of these 6 items, totaling 5400 items and occupying 499,680 square footage.
- In the not_prime category, there are a total of 4 items with a combined square footage of 128.50. After deducting the storage used by prime-eligible items (500,000 - 499,680 = 320), there is room for 2 combinations of non-prime items, accommodating a total of 8 non-prime items within the available 320 square footage.
Output table is ordered by item count in descending order.
  1. # Write your MySQL query statement below
  2. WITH prime AS (
  3.     SELECT
  4.         COUNT(*) AS cnt,
  5.         COALESCE(SUM(square_footage), 0) AS sqft
  6.     FROM Inventory
  7.     WHERE item_type = 'prime_eligible'
  8. ),
  9. non_prime AS (
  10.     SELECT
  11.         COUNT(*) AS cnt,
  12.         COALESCE(SUM(square_footage), 0) AS sqft
  13.     FROM Inventory
  14.     WHERE item_type = 'not_prime'
  15. ),
  16. prime_combos AS (
  17.     SELECT
  18.         cnt,
  19.         sqft,
  20.         IF(sqft > 0, FLOOR(500000 / sqft), 0) AS combos
  21.     FROM prime
  22. )
  23. SELECT
  24.     'prime_eligible' AS item_type,
  25.     pc.combos * pc.cnt AS item_count
  26. FROM prime_combos pc

  27. UNION ALL

  28. SELECT
  29.     'not_prime' AS item_type,
  30.     IF(n.sqft > 0,
  31.        FLOOR((500000 - pc.combos * pc.sqft) / n.sqft),
  32.        0) * n.cnt AS item_count
  33. FROM prime_combos pc, non_prime n

  34. ORDER BY item_count DESC;
复制代码
回复

使用道具 举报

🔗
 楼主| Myron2017 2026-11-23 12:09:59 | 只看该作者
全局:
3053. Classifying Triangles by Lengths
Easy
Topics
SQL Schema
Pandas Schema
Table: Triangles

+-------------+------+
| Column Name | Type |
+-------------+------+
| A           | int  |
| B           | int  |
| C           | int  |
+-------------+------+
(A, B, C) is the primary key for this table.
Each row include the lengths of each of a triangle's three sides.
Write a query to find the type of triangle. Output one of the following for each row:

Equilateral: It's a triangle with 3 sides of equal length.
Isosceles: It's a triangle with 2 sides of equal length.
Scalene: It's a triangle with 3 sides of differing lengths.
Not A Triangle: The given values of A, B, and C don't form a triangle.
Return the result table in any order.

The result format is in the following example.



Example 1:

Input:
Triangles table:
+----+----+----+
| A  | B  | C  |
+----+----+----+
| 20 | 20 | 23 |
| 20 | 20 | 20 |
| 20 | 21 | 22 |
| 13 | 14 | 30 |
+----+----+----+
Output:
+----------------+
| triangle_type  |
+----------------+
| Isosceles      |
| Equilateral    |
| Scalene        |
| Not A Triangle |
+----------------+
Explanation:
- Values in the first row from an Isosceles triangle, because A = B.
- Values in the second row from an Equilateral triangle, because A = B = C.
- Values in the third row from an Scalene triangle, because A != B != C.
- Values in the fourth row cannot form a triangle, because the combined value of sides A and B is not larger than that of side C.
  1. # Write your MySQL query statement below
  2. SELECT
  3.     CASE
  4.         WHEN A + B <= C OR A + C <= B OR B + C <= A THEN 'Not A Triangle'
  5.         WHEN A = B AND B = C                         THEN 'Equilateral'
  6.         WHEN A = B OR A = C OR B = C                 THEN 'Isosceles'
  7.         ELSE 'Scalene'
  8.     END AS triangle_type
  9. FROM Triangles;
复制代码
回复

使用道具 举报

🔗
 楼主| Myron2017 2026-11-23 12:10:58 | 只看该作者
全局:
3054. Binary Tree Nodes
Medium
Topics
SQL Schema
Pandas Schema
Table: Tree

+-------------+------+
| Column Name | Type |
+-------------+------+
| N           | int  |
| P           | int  |
+-------------+------+
N is the column of unique values for this table.
Each row includes N and P, where N represents the value of a node in Binary Tree, and P is the parent of N.
Write a solution to find the node type of the Binary Tree. Output one of the following for each node:

Root: if the node is the root node.
Leaf: if the node is the leaf node.
Inner: if the node is neither root nor leaf node.
Return the result table ordered by node value in ascending order.

The result format is in the following example.



Example 1:

Input:
Tree table:
+---+------+
| N | P    |
+---+------+
| 1 | 2    |
| 3 | 2    |
| 6 | 8    |
| 9 | 8    |
| 2 | 5    |
| 8 | 5    |
| 5 | null |
+---+------+
Output:
+---+-------+
| N | Type  |
+---+-------+
| 1 | Leaf  |
| 2 | Inner |
| 3 | Leaf  |
| 5 | Root  |
| 6 | Leaf  |
| 8 | Inner |
| 9 | Leaf  |   
+---+-------+
Explanation:
- Node 5 is the root node since it has no parent node.
- Nodes 1, 3, 6, and 9 are leaf nodes because they don't have any child nodes.
- Nodes 2, and 8 are inner nodes as they serve as parents to some of the nodes in the structure.
  1. # Write your MySQL query statement below
  2. SELECT
  3.     N,
  4.     CASE
  5.         WHEN P IS NULL THEN 'Root'
  6.         WHEN N IN (SELECT P FROM Tree WHERE P IS NOT NULL) THEN 'Inner'
  7.         ELSE 'Leaf'
  8.     END AS Type
  9. FROM Tree
  10. ORDER BY N;
复制代码
回复

使用道具 举报

🔗
 楼主| Myron2017 2026-11-23 12:12:02 | 只看该作者
全局:
3055. Top Percentile Fraud
Medium
Topics
SQL Schema
Pandas Schema
Table: Fraud

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| policy_id   | int     |
| state       | varchar |
| fraud_score | int     |
+-------------+---------+
policy_id is column of unique values for this table.
This table contains policy id, state, and fraud score.
The Leetcode Insurance Corp has developed an ML-driven predictive model to detect the likelihood of fraudulent claims. Consequently, they allocate their most seasoned claim adjusters to address the top 5% of claims flagged by this model.

Write a solution to find the top 5 percentile of claims from each state.

Return the result table ordered by state in ascending order, fraud_score in descending order, and policy_id in ascending order.

The result format is in the following example.



Example 1:

Input:
Fraud table:
+-----------+------------+-------------+
| policy_id | state      | fraud_score |
+-----------+------------+-------------+
| 1         | California | 0.92        |
| 2         | California | 0.68        |   
| 3         | California | 0.17        |
| 4         | New York   | 0.94        |
| 5         | New York   | 0.81        |
| 6         | New York   | 0.77        |  
| 7         | Texas      | 0.98        |  
| 8         | Texas      | 0.97        |
| 9         | Texas      | 0.96        |
| 10        | Florida    | 0.97        |  
| 11        | Florida    | 0.98        |
| 12        | Florida    | 0.78        |
| 13        | Florida    | 0.88        |
| 14        | Florida    | 0.66        |
+-----------+------------+-------------+
Output:
+-----------+------------+-------------+
| policy_id | state      | fraud_score |
+-----------+------------+-------------+
| 1         | California | 0.92        |
| 11        | Florida    | 0.98        |
| 4         | New York   | 0.94        |
| 7         | Texas      | 0.98        |  
+-----------+------------+-------------+
Explanation
- For the state of California, only policy ID 1, with a fraud score of 0.92, falls within the top 5 percentile for this state.
- For the state of Florida, only policy ID 11, with a fraud score of 0.98, falls within the top 5 percentile for this state.
- For the state of New York, only policy ID 4, with a fraud score of 0.94, falls within the top 5 percentile for this state.
- For the state of Texas, only policy ID 7, with a fraud score of 0.98, falls within the top 5 percentile for this state.
Output table is ordered by state in ascending order, fraud score in descending order, and policy ID in ascending order.
  1. # Write your MySQL query statement below
  2. WITH t AS (
  3.     SELECT
  4.         policy_id,
  5.         state,
  6.         fraud_score,
  7.         PERCENT_RANK() OVER (
  8.             PARTITION BY state
  9.             ORDER BY fraud_score DESC
  10.         ) AS pr
  11.     FROM Fraud
  12. )
  13. SELECT policy_id, state, fraud_score
  14. FROM t
  15. WHERE pr <= 0.05
  16. ORDER BY state ASC, fraud_score DESC, policy_id ASC;
复制代码
回复

使用道具 举报

🔗
 楼主| Myron2017 2026-11-23 12:12:48 | 只看该作者
全局:
3056. Snaps Analysis
Medium
Topics
SQL Schema
Pandas Schema
Table: Activities

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| activity_id   | int     |
| user_id       | int     |
| activity_type | enum    |
| time_spent    | decimal |
+---------------+---------+
activity_id is column of unique values for this table.
activity_type is an ENUM (category) type of ('send', 'open').
This table contains activity id, user id, activity type and time spent.
Table: Age

+-------------+------+
| Column Name | Type |
+-------------+------+
| user_id     | int  |
| age_bucket  | enum |
+-------------+------+
user_id is the column of unique values for this table.
age_bucket is an ENUM (category) type of ('21-25', '26-30', '31-35').
This table contains user id and age group.
Write a solution to calculate the percentage of the total time spent on sending and opening snaps for each age group. Precentage should be rounded to 2 decimal places.

Return the result table in any order.

The result format is in the following example.



Example 1:

Input:
Activities table:
+-------------+---------+---------------+------------+
| activity_id | user_id | activity_type | time_spent |
+-------------+---------+---------------+------------+
| 7274        | 123     | open          | 4.50       |
| 2425        | 123     | send          | 3.50       |
| 1413        | 456     | send          | 5.67       |
| 2536        | 456     | open          | 3.00       |
| 8564        | 456     | send          | 8.24       |
| 5235        | 789     | send          | 6.24       |
| 4251        | 123     | open          | 1.25       |
| 1435        | 789     | open          | 5.25       |
+-------------+---------+---------------+------------+
Age table:
+---------+------------+
| user_id | age_bucket |
+---------+------------+
| 123     | 31-35      |
| 789     | 21-25      |
| 456     | 26-30      |
+---------+------------+
Output:
+------------+-----------+-----------+
| age_bucket | send_perc | open_perc |
+------------+-----------+-----------+
| 31-35      | 37.84     | 62.16     |
| 26-30      | 82.26     | 17.74     |
| 21-25      | 54.31     | 45.69     |
+------------+-----------+-----------+
Explanation:
For age group 31-35:
  - There is only one user belonging to this group with the user ID 123.
  - The total time spent on sending snaps by this user is 3.50, and the time spent on opening snaps is 4.50 + 1.25 = 5.75.
  - The overall time spent by this user is 3.50 + 5.75 = 9.25.
  - Therefore, the sending snap percentage will be (3.50 / 9.25) * 100 = 37.84, and the opening snap percentage will be (5.75 / 9.25) * 100 = 62.16.
For age group 26-30:
  - There is only one user belonging to this group with the user ID 456.
  - The total time spent on sending snaps by this user is 5.67 + 8.24 = 13.91, and the time spent on opening snaps is 3.00.
  - The overall time spent by this user is 13.91 + 3.00 = 16.91.
  - Therefore, the sending snap percentage will be (13.91 / 16.91) * 100 = 82.26, and the opening snap percentage will be (3.00 / 16.91) * 100 = 17.74.
For age group 21-25:
  - There is only one user belonging to this group with the user ID 789.
  - The total time spent on sending snaps by this user is 6.24, and the time spent on opening snaps is 5.25.
  - The overall time spent by this user is 6.24 + 5.25 = 11.49.
  - Therefore, the sending snap percentage will be (6.24 / 11.49) * 100 = 54.31, and the opening snap percentage will be (5.25 / 11.49) * 100 = 45.69.
All percentages in output table rounded to the two decimal places.
  1. # Write your MySQL query statement below
  2. SELECT
  3.     a.age_bucket,
  4.     ROUND(SUM(IF(ac.activity_type = 'send', ac.time_spent, 0)) / SUM(ac.time_spent) * 100, 2) AS send_perc,
  5.     ROUND(SUM(IF(ac.activity_type = 'open', ac.time_spent, 0)) / SUM(ac.time_spent) * 100, 2) AS open_perc
  6. FROM Activities ac
  7. JOIN Age a ON ac.user_id = a.user_id
  8. GROUP BY a.age_bucket;
复制代码
回复

使用道具 举报

🔗
 楼主| Myron2017 2026-11-23 12:13:28 | 只看该作者
全局:
3057. Employees Project Allocation
Hard
Topics
SQL Schema
Pandas Schema
Table: Project

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| project_id  | int     |
| employee_id | int     |
| workload    | int     |
+-------------+---------+
employee_id is the primary key (column with unique values) of this table.
employee_id is a foreign key (reference column) to Employee table.
Each row of this table indicates that the employee with employee_id is working on the project with project_id and the workload of the project.
Table: Employees

+------------------+---------+
| Column Name      | Type    |
+------------------+---------+
| employee_id      | int     |
| name             | varchar |
| team             | varchar |
+------------------+---------+
employee_id is the primary key (column with unique values) of this table.
Each row of this table contains information about one employee.
Write a solution to find the employees who are allocated to projects with a workload that exceeds the average workload of all employees for their respective teams

Return the result table ordered by employee_id, project_id in ascending order.

The result format is in the following example.



Example 1:

Input:
Project table:
+-------------+-------------+----------+
| project_id  | employee_id | workload |
+-------------+-------------+----------+
| 1           | 1           |  45      |
| 1           | 2           |  90      |
| 2           | 3           |  12      |
| 2           | 4           |  68      |
+-------------+-------------+----------+
Employees table:
+-------------+--------+------+
| employee_id | name   | team |
+-------------+--------+------+
| 1           | Khaled | A    |
| 2           | Ali    | B    |
| 3           | John   | B    |
| 4           | Doe    | A    |
+-------------+--------+------+
Output:
+-------------+------------+---------------+------------------+
| employee_id | project_id | employee_name | project_workload |
+-------------+------------+---------------+------------------+  
| 2           | 1          | Ali           | 90               |
| 4           | 2          | Doe           | 68               |
+-------------+------------+---------------+------------------+
Explanation:
- Employee with ID 1 has a project workload of 45 and belongs to Team A, where the average workload is 56.50. Since his project workload does not exceed the team's average workload, he will be excluded.
- Employee with ID 2 has a project workload of 90 and belongs to Team B, where the average workload is 51.00. Since his project workload does exceed the team's average workload, he will be included.
- Employee with ID 3 has a project workload of 12 and belongs to Team B, where the average workload is 51.00. Since his project workload does not exceed the team's average workload, he will be excluded.
- Employee with ID 4 has a project workload of 68 and belongs to Team A, where the average workload is 56.50. Since his project workload does exceed the team's average workload, he will be included.
Result table orderd by employee_id, project_id in ascending order.
  1. # Write your MySQL query statement below
  2. WITH t AS (
  3.     SELECT
  4.         p.employee_id,
  5.         p.project_id,
  6.         e.name AS employee_name,
  7.         p.workload AS project_workload,
  8.         AVG(p.workload) OVER (PARTITION BY e.team) AS team_avg
  9.     FROM Project p
  10.     JOIN Employees e ON p.employee_id = e.employee_id
  11. )
  12. SELECT employee_id, project_id, employee_name, project_workload
  13. FROM t
  14. WHERE project_workload > team_avg
  15. ORDER BY employee_id, project_id;
复制代码
回复

使用道具 举报

🔗
 楼主| Myron2017 2026-11-23 12:14:17 | 只看该作者
全局:
3058. Friends With No Mutual Friends
Medium
Topics
SQL Schema
Pandas Schema
Table: Friends

+-------------+------+
| Column Name | Type |
+-------------+------+
| user_id1    | int  |
| user_id2    | int  |
+-------------+------+
(user_id1, user_id2) is the primary key (combination of columns with unique values) for this table.
Each row contains user id1, user id2, both of whom are friends with each other.
Write a solution to find all pairs of users who are friends with each other and have no mutual friends.

Return the result table ordered by user_id1, user_id2 in ascending order.

The result format is in the following example.



Example 1:

Input:
Friends table:
+----------+----------+
| user_id1 | user_id2 |
+----------+----------+
| 1        | 2        |
| 2        | 3        |
| 2        | 4        |
| 1        | 5        |
| 6        | 7        |
| 3        | 4        |
| 2        | 5        |
| 8        | 9        |
+----------+----------+
Output:
+----------+----------+
| user_id1 | user_id2 |
+----------+----------+
| 6        | 7        |
| 8        | 9        |
+----------+----------+
Explanation:
- Users 1 and 2 are friends with each other, but they share a mutual friend with user ID 5, so this pair is not included.
- Users 2 and 3 are friends, they both share a mutual friend with user ID 4, resulting in exclusion, similarly for users 2 and 4 who share a mutual friend with user ID 3, hence not included.
- Users 1 and 5 are friends with each other, but they share a mutual friend with user ID 2, so this pair is not included.
- Users 6 and 7, as well as users 8 and 9, are friends with each other, and they don't have any mutual friends, hence included.
- Users 3 and 4 are friends with each other, but their mutual connection with user ID 2 means they are not included, similarly for users 2 and 5 are friends but are excluded due to their mutual connection with user ID 1.
Output table is ordered by user_id1 in ascending order.
  1. # Write your MySQL query statement below
  2. WITH e AS (
  3.     SELECT user_id1 AS u, user_id2 AS v FROM Friends
  4.     UNION ALL
  5.     SELECT user_id2 AS u, user_id1 AS v FROM Friends
  6. )
  7. SELECT f.user_id1, f.user_id2
  8. FROM Friends f
  9. WHERE NOT EXISTS (
  10.     SELECT 1
  11.     FROM e a
  12.     JOIN e b ON a.v = b.v          -- 同一个人 c 同时是 a 和 b 的好友
  13.     WHERE a.u = f.user_id1
  14.       AND b.u = f.user_id2
  15. )
  16. ORDER BY f.user_id1, f.user_id2;
复制代码
回复

使用道具 举报

无效楼层,该帖已经被删除
🔗
 楼主| Myron2017 2026-11-23 12:15:56 | 只看该作者
全局:
3060. User Activities within Time Bounds
Hard
Topics
SQL Schema
Pandas Schema
Table: Sessions

+---------------+----------+
| Column Name   | Type     |
+---------------+----------+
| user_id       | int      |
| session_start | datetime |
| session_end   | datetime |
| session_id    | int      |
| session_type  | enum     |
+---------------+----------+
session_id is column of unique values for this table.
session_type is an ENUM (category) type of (Viewer, Streamer).
This table contains user id, session start, session end, session id and session type.
Write a solution to find the the users who have had at least two session of the same type (either 'Viewer' or 'Streamer') with a maximum gap of 12 hours between sessions.

Return the result table ordered by user_id in ascending order.

The result format is in the following example.



Example:

Input:
Sessions table:
+---------+---------------------+---------------------+------------+--------------+
| user_id | session_start       | session_end         | session_id | session_type |
+---------+---------------------+---------------------+------------+--------------+
| 101     | 2023-11-01 08:00:00 | 2023-11-01 09:00:00 | 1          | Viewer       |  
| 101     | 2023-11-01 10:00:00 | 2023-11-01 11:00:00 | 2          | Streamer     |   
| 102     | 2023-11-01 13:00:00 | 2023-11-01 14:00:00 | 3          | Viewer       |
| 102     | 2023-11-01 15:00:00 | 2023-11-01 16:00:00 | 4          | Viewer       |
| 101     | 2023-11-02 09:00:00 | 2023-11-02 10:00:00 | 5          | Viewer       |
| 102     | 2023-11-02 12:00:00 | 2023-11-02 13:00:00 | 6          | Streamer     |
| 101     | 2023-11-02 13:00:00 | 2023-11-02 14:00:00 | 7          | Streamer     |
| 102     | 2023-11-02 16:00:00 | 2023-11-02 17:00:00 | 8          | Viewer       |
| 103     | 2023-11-01 08:00:00 | 2023-11-01 09:00:00 | 9          | Viewer       |
| 103     | 2023-11-02 20:00:00 | 2023-11-02 23:00:00 | 10         | Viewer       |
| 103     | 2023-11-03 09:00:00 | 2023-11-03 10:00:00 | 11         | Viewer       |
+---------+---------------------+---------------------+------------+--------------+
Output:
+---------+
| user_id |
+---------+
| 102     |
| 103     |
+---------+
Explanation:
- User ID 101 will not be included in the final output as they do not have any two sessions of the same session type.
- User ID 102 will be included in the final output as they had two viewer sessions with session IDs 3 and 4, respectively, and the time gap between them was less than 12 hours.
- User ID 103 participated in two viewer sessions with a gap of less than 12 hours between them, identified by session IDs 10 and 11. Therefore, user 103 will be included in the final output.
Output table is ordered by user_id in increasing order.
  1. # Write your MySQL query statement below
  2. WITH t AS (
  3.     SELECT
  4.         user_id,
  5.         session_start,
  6.         LAG(session_end) OVER (
  7.             PARTITION BY user_id, session_type
  8.             ORDER BY session_start
  9.         ) AS prev_end
  10.     FROM Sessions
  11. )
  12. SELECT DISTINCT user_id
  13. FROM t
  14. WHERE prev_end IS NOT NULL
  15.   AND session_start <= prev_end + INTERVAL 12 HOUR
  16. ORDER BY user_id;
复制代码
回复

使用道具 举报

🔗
 楼主| Myron2017 2026-11-23 12:17:13 | 只看该作者
全局:
3061. Calculate Trapping Rain Water
Hard
Topics
SQL Schema
Pandas Schema
Table: Heights

+-------------+------+
| Column Name | Type |
+-------------+------+
| id          | int  |
| height      | int  |
+-------------+------+
id is the primary key (column with unique values) for this table, and it is guaranteed to be in sequential order.
Each row of this table contains an id and height.
Write a solution to calculate the amount of rainwater can be trapped between the bars in the landscape, considering that each bar has a width of 1 unit.

Return the result table in any order.

The result format is in the following example.



Example 1:

Input:
Heights table:
+-----+--------+
| id  | height |
+-----+--------+
| 1   | 0      |
| 2   | 1      |
| 3   | 0      |
| 4   | 2      |
| 5   | 1      |
| 6   | 0      |
| 7   | 1      |
| 8   | 3      |
| 9   | 2      |
| 10  | 1      |
| 11  | 2      |
| 12  | 1      |
+-----+--------+
Output:
+---------------------+
| total_trapped_water |
+---------------------+
| 6                   |
+---------------------+
Explanation:


The elevation map depicted above (in the black section) is graphically represented with the x-axis denoting the id and the y-axis representing the heights [0,1,0,2,1,0,1,3,2,1,2,1]. In this scenario, 6 units of rainwater are trapped within the blue section.
  1. # Write your MySQL query statement below
  2. SELECT SUM(LEAST(left_max, right_max) - height) AS total_trapped_water
  3. FROM (
  4.     SELECT
  5.         height,
  6.         MAX(height) OVER (
  7.             ORDER BY id
  8.             ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
  9.         ) AS left_max,
  10.         MAX(height) OVER (
  11.             ORDER BY id
  12.             ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
  13.         ) AS right_max
  14.     FROM Heights
  15. ) t;
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表