Skip to content

feat: add solutions to lc problems: No.3061,3064 #2393

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion solution/3000-3099/3061.Calculate Trapping Rain Water/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`,最后求和即可。

<!-- tabs:start -->

```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()]})
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.

<!-- tabs:start -->

```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()]})
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -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()]})
11 changes: 11 additions & 0 deletions solution/3000-3099/3061.Calculate Trapping Rain Water/Solution.sql
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,94 @@

## 解法

### 方法一
### 方法一:枚举

我们可以枚举 $2$ 的幂次方,然后调用 `commonSetBits` 方法,如果返回值大于 $0$,则说明 $n$ 的二进制表示中的对应位是 $1$。

时间复杂度 $O(\log n)$,本题中 $n \le 2^{30}$。空间复杂度 $O(1)$。

<!-- tabs:start -->

```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<<i) > 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;
}
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)$.

<!-- tabs:start -->

```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<<i) > 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;
}
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
};
Original file line number Diff line number Diff line change
@@ -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<<i) > 0 {
n |= 1 << i
}
}
return
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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))
Original file line number Diff line number Diff line change
@@ -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;
}