Skip to content

Commit e93770e

Browse files
authored
feat: add solutions to lc problems: No.3061,3064 (doocs#2393)
* No.3061.Calculate Trapping Rain Water * No.3064.Guess the Number Using Bitwise Questions I
1 parent 1d40ed1 commit e93770e

File tree

11 files changed

+280
-8
lines changed

11 files changed

+280
-8
lines changed

solution/3000-3099/3061.Calculate Trapping Rain Water/README.md

+24-1
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,35 @@ The elevation map depicted above (in the black section) is graphically represent
6363

6464
## 解法
6565

66-
### 方法一
66+
### 方法一:窗口函数 + 求和
67+
68+
我们使用窗口函数 `MAX(height) OVER (ORDER BY id)` 来计算每个位置及其左边的最大高度,使用 `MAX(height) OVER (ORDER BY id DESC)` 来计算每个位置及其右边的最大高度,分别记为 `l``r`。那么每个位置上的蓄水量就是 `min(l, r) - height`,最后求和即可。
6769

6870
<!-- tabs:start -->
6971

7072
```sql
73+
# Write your MySQL query statement below
74+
WITH
75+
T AS (
76+
SELECT
77+
*,
78+
MAX(height) OVER (ORDER BY id) AS l,
79+
MAX(height) OVER (ORDER BY id DESC) AS r
80+
FROM Heights
81+
)
82+
SELECT SUM(LEAST(l, r) - height) AS total_trapped_water
83+
FROM T;
84+
```
85+
86+
```python
87+
import pandas as pd
88+
7189

90+
def calculate_trapped_rain_water(heights: pd.DataFrame) -> pd.DataFrame:
91+
heights["l"] = heights["height"].cummax()
92+
heights["r"] = heights["height"][::-1].cummax()[::-1]
93+
heights["trapped_water"] = heights[["l", "r"]].min(axis=1) - heights["height"]
94+
return pd.DataFrame({"total_trapped_water": [heights["trapped_water"].sum()]})
7295
```
7396

7497
<!-- tabs:end -->

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

+24-1
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,35 @@ The elevation map depicted above (in the black section) is graphically represent
6161

6262
## Solutions
6363

64-
### Solution 1
64+
### Solution 1: Window Function + Summation
65+
66+
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.
6567

6668
<!-- tabs:start -->
6769

6870
```sql
71+
# Write your MySQL query statement below
72+
WITH
73+
T AS (
74+
SELECT
75+
*,
76+
MAX(height) OVER (ORDER BY id) AS l,
77+
MAX(height) OVER (ORDER BY id DESC) AS r
78+
FROM Heights
79+
)
80+
SELECT SUM(LEAST(l, r) - height) AS total_trapped_water
81+
FROM T;
82+
```
83+
84+
```python
85+
import pandas as pd
86+
6987

88+
def calculate_trapped_rain_water(heights: pd.DataFrame) -> pd.DataFrame:
89+
heights["l"] = heights["height"].cummax()
90+
heights["r"] = heights["height"][::-1].cummax()[::-1]
91+
heights["trapped_water"] = heights[["l", "r"]].min(axis=1) - heights["height"]
92+
return pd.DataFrame({"total_trapped_water": [heights["trapped_water"].sum()]})
7093
```
7194

7295
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import pandas as pd
2+
3+
4+
def calculate_trapped_rain_water(heights: pd.DataFrame) -> pd.DataFrame:
5+
heights["l"] = heights["height"].cummax()
6+
heights["r"] = heights["height"][::-1].cummax()[::-1]
7+
heights["trapped_water"] = heights[["l", "r"]].min(axis=1) - heights["height"]
8+
return pd.DataFrame({"total_trapped_water": [heights["trapped_water"].sum()]})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Write your MySQL query statement below
2+
WITH
3+
T AS (
4+
SELECT
5+
*,
6+
MAX(height) OVER (ORDER BY id) AS l,
7+
MAX(height) OVER (ORDER BY id DESC) AS r
8+
FROM Heights
9+
)
10+
SELECT SUM(LEAST(l, r) - height) AS total_trapped_water
11+
FROM T;

solution/3000-3099/3064.Guess the Number Using Bitwise Questions I/README.md

+73-3
Original file line numberDiff line numberDiff line change
@@ -46,24 +46,94 @@
4646

4747
## 解法
4848

49-
### 方法一
49+
### 方法一:枚举
50+
51+
我们可以枚举 $2$ 的幂次方,然后调用 `commonSetBits` 方法,如果返回值大于 $0$,则说明 $n$ 的二进制表示中的对应位是 $1$。
52+
53+
时间复杂度 $O(\log n)$,本题中 $n \le 2^{30}$。空间复杂度 $O(1)$。
5054

5155
<!-- tabs:start -->
5256

5357
```python
58+
# Definition of commonSetBits API.
59+
# def commonSetBits(num: int) -> int:
60+
5461

62+
class Solution:
63+
def findNumber(self) -> int:
64+
return sum(1 << i for i in range(32) if commonSetBits(1 << i))
5565
```
5666

5767
```java
58-
68+
/**
69+
* Definition of commonSetBits API (defined in the parent class Problem).
70+
* int commonSetBits(int num);
71+
*/
72+
73+
public class Solution extends Problem {
74+
public int findNumber() {
75+
int n = 0;
76+
for (int i = 0; i < 32; ++i) {
77+
if (commonSetBits(1 << i) > 0) {
78+
n |= 1 << i;
79+
}
80+
}
81+
return n;
82+
}
83+
}
5984
```
6085

6186
```cpp
62-
87+
/**
88+
* Definition of commonSetBits API.
89+
* int commonSetBits(int num);
90+
*/
91+
92+
class Solution {
93+
public:
94+
int findNumber() {
95+
int n = 0;
96+
for (int i = 0; i < 32; ++i) {
97+
if (commonSetBits(1 << i)) {
98+
n |= 1 << i;
99+
}
100+
}
101+
return n;
102+
}
103+
};
63104
```
64105
65106
```go
107+
/**
108+
* Definition of commonSetBits API.
109+
* func commonSetBits(num int) int;
110+
*/
111+
112+
func findNumber() (n int) {
113+
for i := 0; i < 32; i++ {
114+
if commonSetBits(1<<i) > 0 {
115+
n |= 1 << i
116+
}
117+
}
118+
return
119+
}
120+
```
66121

122+
```ts
123+
/**
124+
* Definition of commonSetBits API.
125+
* var commonSetBits = function(num: number): number {}
126+
*/
127+
128+
function findNumber(): number {
129+
let n = 0;
130+
for (let i = 0; i < 32; ++i) {
131+
if (commonSetBits(1 << i)) {
132+
n |= 1 << i;
133+
}
134+
}
135+
return n;
136+
}
67137
```
68138

69139
<!-- tabs:end -->

solution/3000-3099/3064.Guess the Number Using Bitwise Questions I/README_EN.md

+73-3
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,94 @@
4444

4545
## Solutions
4646

47-
### Solution 1
47+
### Solution 1: Enumeration
48+
49+
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.
50+
51+
The time complexity is $O(\log n)$, where $n \le 2^{30}$ in this problem. The space complexity is $O(1)$.
4852

4953
<!-- tabs:start -->
5054

5155
```python
56+
# Definition of commonSetBits API.
57+
# def commonSetBits(num: int) -> int:
58+
5259

60+
class Solution:
61+
def findNumber(self) -> int:
62+
return sum(1 << i for i in range(32) if commonSetBits(1 << i))
5363
```
5464

5565
```java
56-
66+
/**
67+
* Definition of commonSetBits API (defined in the parent class Problem).
68+
* int commonSetBits(int num);
69+
*/
70+
71+
public class Solution extends Problem {
72+
public int findNumber() {
73+
int n = 0;
74+
for (int i = 0; i < 32; ++i) {
75+
if (commonSetBits(1 << i) > 0) {
76+
n |= 1 << i;
77+
}
78+
}
79+
return n;
80+
}
81+
}
5782
```
5883

5984
```cpp
60-
85+
/**
86+
* Definition of commonSetBits API.
87+
* int commonSetBits(int num);
88+
*/
89+
90+
class Solution {
91+
public:
92+
int findNumber() {
93+
int n = 0;
94+
for (int i = 0; i < 32; ++i) {
95+
if (commonSetBits(1 << i)) {
96+
n |= 1 << i;
97+
}
98+
}
99+
return n;
100+
}
101+
};
61102
```
62103
63104
```go
105+
/**
106+
* Definition of commonSetBits API.
107+
* func commonSetBits(num int) int;
108+
*/
109+
110+
func findNumber() (n int) {
111+
for i := 0; i < 32; i++ {
112+
if commonSetBits(1<<i) > 0 {
113+
n |= 1 << i
114+
}
115+
}
116+
return
117+
}
118+
```
64119

120+
```ts
121+
/**
122+
* Definition of commonSetBits API.
123+
* var commonSetBits = function(num: number): number {}
124+
*/
125+
126+
function findNumber(): number {
127+
let n = 0;
128+
for (let i = 0; i < 32; ++i) {
129+
if (commonSetBits(1 << i)) {
130+
n |= 1 << i;
131+
}
132+
}
133+
return n;
134+
}
65135
```
66136

67137
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Definition of commonSetBits API.
3+
* int commonSetBits(int num);
4+
*/
5+
6+
class Solution {
7+
public:
8+
int findNumber() {
9+
int n = 0;
10+
for (int i = 0; i < 32; ++i) {
11+
if (commonSetBits(1 << i)) {
12+
n |= 1 << i;
13+
}
14+
}
15+
return n;
16+
}
17+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Definition of commonSetBits API.
3+
* func commonSetBits(num int) int;
4+
*/
5+
6+
func findNumber() (n int) {
7+
for i := 0; i < 32; i++ {
8+
if commonSetBits(1<<i) > 0 {
9+
n |= 1 << i
10+
}
11+
}
12+
return
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Definition of commonSetBits API (defined in the parent class Problem).
3+
* int commonSetBits(int num);
4+
*/
5+
6+
public class Solution extends Problem {
7+
public int findNumber() {
8+
int n = 0;
9+
for (int i = 0; i < 32; ++i) {
10+
if (commonSetBits(1 << i) > 0) {
11+
n |= 1 << i;
12+
}
13+
}
14+
return n;
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Definition of commonSetBits API.
2+
# def commonSetBits(num: int) -> int:
3+
4+
5+
class Solution:
6+
def findNumber(self) -> int:
7+
return sum(1 << i for i in range(32) if commonSetBits(1 << i))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Definition of commonSetBits API.
3+
* var commonSetBits = function(num: number): number {}
4+
*/
5+
6+
function findNumber(): number {
7+
let n = 0;
8+
for (let i = 0; i < 32; ++i) {
9+
if (commonSetBits(1 << i)) {
10+
n |= 1 << i;
11+
}
12+
}
13+
return n;
14+
}

0 commit comments

Comments
 (0)