diff --git a/solution/3000-3099/3061.Calculate Trapping Rain Water/README.md b/solution/3000-3099/3061.Calculate Trapping Rain Water/README.md index 1f12b3962a790..2408661c42cca 100644 --- a/solution/3000-3099/3061.Calculate Trapping Rain Water/README.md +++ b/solution/3000-3099/3061.Calculate Trapping Rain Water/README.md @@ -63,12 +63,35 @@ The elevation map depicted above (in the black section) is graphically represent ## 解法 -### 方法一 +### 方法一:窗口函数 + 求和 + +我们使用窗口函数 `MAX(height) OVER (ORDER BY id)` 来计算每个位置及其左边的最大高度,使用 `MAX(height) OVER (ORDER BY id DESC)` 来计算每个位置及其右边的最大高度,分别记为 `l` 和 `r`。那么每个位置上的蓄水量就是 `min(l, r) - height`,最后求和即可。 ```sql +# 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; +``` + +```python +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()]}) ``` diff --git a/solution/3000-3099/3061.Calculate Trapping Rain Water/README_EN.md b/solution/3000-3099/3061.Calculate Trapping Rain Water/README_EN.md index 2a388d99b838e..0186bc1081b49 100644 --- a/solution/3000-3099/3061.Calculate Trapping Rain Water/README_EN.md +++ b/solution/3000-3099/3061.Calculate Trapping Rain Water/README_EN.md @@ -61,12 +61,35 @@ The elevation map depicted above (in the black section) is graphically represent ## Solutions -### Solution 1 +### 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. ```sql +# 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; +``` + +```python +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()]}) ``` diff --git a/solution/3000-3099/3061.Calculate Trapping Rain Water/Solution.py b/solution/3000-3099/3061.Calculate Trapping Rain Water/Solution.py new file mode 100644 index 0000000000000..c3972dd014a08 --- /dev/null +++ b/solution/3000-3099/3061.Calculate Trapping Rain Water/Solution.py @@ -0,0 +1,8 @@ +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()]}) diff --git a/solution/3000-3099/3061.Calculate Trapping Rain Water/Solution.sql b/solution/3000-3099/3061.Calculate Trapping Rain Water/Solution.sql new file mode 100644 index 0000000000000..e268938546208 --- /dev/null +++ b/solution/3000-3099/3061.Calculate Trapping Rain Water/Solution.sql @@ -0,0 +1,11 @@ +# 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; diff --git a/solution/3000-3099/3064.Guess the Number Using Bitwise Questions I/README.md b/solution/3000-3099/3064.Guess the Number Using Bitwise Questions I/README.md index e48c136d0e2f1..b55e5e4b6e026 100644 --- a/solution/3000-3099/3064.Guess the Number Using Bitwise Questions I/README.md +++ b/solution/3000-3099/3064.Guess the Number Using Bitwise Questions I/README.md @@ -46,24 +46,94 @@ ## 解法 -### 方法一 +### 方法一:枚举 + +我们可以枚举 $2$ 的幂次方,然后调用 `commonSetBits` 方法,如果返回值大于 $0$,则说明 $n$ 的二进制表示中的对应位是 $1$。 + +时间复杂度 $O(\log n)$,本题中 $n \le 2^{30}$。空间复杂度 $O(1)$。 ```python +# Definition of commonSetBits API. +# def commonSetBits(num: int) -> int: + +class Solution: + def findNumber(self) -> int: + return sum(1 << i for i in range(32) if commonSetBits(1 << i)) ``` ```java - +/** + * Definition of commonSetBits API (defined in the parent class Problem). + * int commonSetBits(int num); + */ + +public class Solution extends Problem { + public int findNumber() { + int n = 0; + for (int i = 0; i < 32; ++i) { + if (commonSetBits(1 << i) > 0) { + n |= 1 << i; + } + } + return n; + } +} ``` ```cpp - +/** + * Definition of commonSetBits API. + * int commonSetBits(int num); + */ + +class Solution { +public: + int findNumber() { + int n = 0; + for (int i = 0; i < 32; ++i) { + if (commonSetBits(1 << i)) { + n |= 1 << i; + } + } + return n; + } +}; ``` ```go +/** + * Definition of commonSetBits API. + * func commonSetBits(num int) int; + */ + +func findNumber() (n int) { + for i := 0; i < 32; i++ { + if commonSetBits(1< 0 { + n |= 1 << i + } + } + return +} +``` +```ts +/** + * Definition of commonSetBits API. + * var commonSetBits = function(num: number): number {} + */ + +function findNumber(): number { + let n = 0; + for (let i = 0; i < 32; ++i) { + if (commonSetBits(1 << i)) { + n |= 1 << i; + } + } + return n; +} ``` diff --git a/solution/3000-3099/3064.Guess the Number Using Bitwise Questions I/README_EN.md b/solution/3000-3099/3064.Guess the Number Using Bitwise Questions I/README_EN.md index f7f6891c84969..d804b20e549b8 100644 --- a/solution/3000-3099/3064.Guess the Number Using Bitwise Questions I/README_EN.md +++ b/solution/3000-3099/3064.Guess the Number Using Bitwise Questions I/README_EN.md @@ -44,24 +44,94 @@ ## Solutions -### Solution 1 +### Solution 1: Enumeration + +We can enumerate the powers of 2, and then call the `commonSetBits` method. If the return value is greater than 0, it means that the corresponding bit in the binary representation of `n` is 1. + +The time complexity is $O(\log n)$, where $n \le 2^{30}$ in this problem. The space complexity is $O(1)$. ```python +# Definition of commonSetBits API. +# def commonSetBits(num: int) -> int: + +class Solution: + def findNumber(self) -> int: + return sum(1 << i for i in range(32) if commonSetBits(1 << i)) ``` ```java - +/** + * Definition of commonSetBits API (defined in the parent class Problem). + * int commonSetBits(int num); + */ + +public class Solution extends Problem { + public int findNumber() { + int n = 0; + for (int i = 0; i < 32; ++i) { + if (commonSetBits(1 << i) > 0) { + n |= 1 << i; + } + } + return n; + } +} ``` ```cpp - +/** + * Definition of commonSetBits API. + * int commonSetBits(int num); + */ + +class Solution { +public: + int findNumber() { + int n = 0; + for (int i = 0; i < 32; ++i) { + if (commonSetBits(1 << i)) { + n |= 1 << i; + } + } + return n; + } +}; ``` ```go +/** + * Definition of commonSetBits API. + * func commonSetBits(num int) int; + */ + +func findNumber() (n int) { + for i := 0; i < 32; i++ { + if commonSetBits(1< 0 { + n |= 1 << i + } + } + return +} +``` +```ts +/** + * Definition of commonSetBits API. + * var commonSetBits = function(num: number): number {} + */ + +function findNumber(): number { + let n = 0; + for (let i = 0; i < 32; ++i) { + if (commonSetBits(1 << i)) { + n |= 1 << i; + } + } + return n; +} ``` diff --git a/solution/3000-3099/3064.Guess the Number Using Bitwise Questions I/Solution.cpp b/solution/3000-3099/3064.Guess the Number Using Bitwise Questions I/Solution.cpp new file mode 100644 index 0000000000000..61f78ca73e69e --- /dev/null +++ b/solution/3000-3099/3064.Guess the Number Using Bitwise Questions I/Solution.cpp @@ -0,0 +1,17 @@ +/** + * Definition of commonSetBits API. + * int commonSetBits(int num); + */ + +class Solution { +public: + int findNumber() { + int n = 0; + for (int i = 0; i < 32; ++i) { + if (commonSetBits(1 << i)) { + n |= 1 << i; + } + } + return n; + } +}; \ No newline at end of file diff --git a/solution/3000-3099/3064.Guess the Number Using Bitwise Questions I/Solution.go b/solution/3000-3099/3064.Guess the Number Using Bitwise Questions I/Solution.go new file mode 100644 index 0000000000000..dbad9a719ebde --- /dev/null +++ b/solution/3000-3099/3064.Guess the Number Using Bitwise Questions I/Solution.go @@ -0,0 +1,13 @@ +/** + * Definition of commonSetBits API. + * func commonSetBits(num int) int; + */ + +func findNumber() (n int) { + for i := 0; i < 32; i++ { + if commonSetBits(1< 0 { + n |= 1 << i + } + } + return +} \ No newline at end of file diff --git a/solution/3000-3099/3064.Guess the Number Using Bitwise Questions I/Solution.java b/solution/3000-3099/3064.Guess the Number Using Bitwise Questions I/Solution.java new file mode 100644 index 0000000000000..ba66fed98b348 --- /dev/null +++ b/solution/3000-3099/3064.Guess the Number Using Bitwise Questions I/Solution.java @@ -0,0 +1,16 @@ +/** + * Definition of commonSetBits API (defined in the parent class Problem). + * int commonSetBits(int num); + */ + +public class Solution extends Problem { + public int findNumber() { + int n = 0; + for (int i = 0; i < 32; ++i) { + if (commonSetBits(1 << i) > 0) { + n |= 1 << i; + } + } + return n; + } +} \ No newline at end of file diff --git a/solution/3000-3099/3064.Guess the Number Using Bitwise Questions I/Solution.py b/solution/3000-3099/3064.Guess the Number Using Bitwise Questions I/Solution.py new file mode 100644 index 0000000000000..34426b54bf160 --- /dev/null +++ b/solution/3000-3099/3064.Guess the Number Using Bitwise Questions I/Solution.py @@ -0,0 +1,7 @@ +# Definition of commonSetBits API. +# def commonSetBits(num: int) -> int: + + +class Solution: + def findNumber(self) -> int: + return sum(1 << i for i in range(32) if commonSetBits(1 << i)) diff --git a/solution/3000-3099/3064.Guess the Number Using Bitwise Questions I/Solution.ts b/solution/3000-3099/3064.Guess the Number Using Bitwise Questions I/Solution.ts new file mode 100644 index 0000000000000..a5d706a8165f9 --- /dev/null +++ b/solution/3000-3099/3064.Guess the Number Using Bitwise Questions I/Solution.ts @@ -0,0 +1,14 @@ +/** + * Definition of commonSetBits API. + * var commonSetBits = function(num: number): number {} + */ + +function findNumber(): number { + let n = 0; + for (let i = 0; i < 32; ++i) { + if (commonSetBits(1 << i)) { + n |= 1 << i; + } + } + return n; +}