返回介绍

solution / 3000-3099 / 3061.Calculate Trapping Rain Water / README_EN

发布于 2024-06-17 01:02:57 字数 3281 浏览 0 评论 0 收藏 0

3061. Calculate Trapping Rain Water

中文文档

Description

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.

Solutions

Solution 1: Window Function + Summation

We use the window function MAX(height) OVER (ORDER BY id) to calculate the maximum height for each position and its left side, and use MAX(height) OVER (ORDER BY id DESC) to calculate the maximum height for each position and its right side, denoted as l and r respectively. Then, the amount of water stored at each position is min(l, r) - height. Finally, we sum them up.

# Write your MySQL query statement below
WITH
  T AS (
    SELECT
      *,
      MAX(height) OVER (ORDER BY id) AS l,
      MAX(height) OVER (ORDER BY id DESC) AS r
    FROM Heights
  )
SELECT SUM(LEAST(l, r) - height) AS total_trapped_water
FROM T;
import pandas as pd


def calculate_trapped_rain_water(heights: pd.DataFrame) -> pd.DataFrame:
  heights["l"] = heights["height"].cummax()
  heights["r"] = heights["height"][::-1].cummax()[::-1]
  heights["trapped_water"] = heights[["l", "r"]].min(axis=1) - heights["height"]
  return pd.DataFrame({"total_trapped_water": [heights["trapped_water"].sum()]})

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文