Skip to content

Commit 8db1bf4

Browse files
authored
feat: add solutions to lc problem: No.1715 (doocs#3106)
No.1715.Count Apples and Oranges
1 parent 2d12a3d commit 8db1bf4

File tree

8 files changed

+68
-57
lines changed

8 files changed

+68
-57
lines changed

solution/1700-1799/1715.Count Apples and Oranges/README.md

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,9 @@ Chests 表:
105105

106106
<!-- solution:start -->
107107

108-
### 方法一
108+
### 方法一:左连接 + 求和
109+
110+
我们可以将 `Boxes` 表和 `Chests` 表按照 `chest_id` 进行左连接,然后分别求出苹果和橘子的总个数。注意,如果某个箱子中没有小盒子,那么对应的 `chest_id``null`,此时我们需要认为该箱子中的小盒子中苹果和橘子的个数为 0。
109111

110112
<!-- tabs:start -->
111113

@@ -118,7 +120,28 @@ SELECT
118120
SUM(IFNULL(b.orange_count, 0) + IFNULL(c.orange_count, 0)) AS orange_count
119121
FROM
120122
Boxes AS b
121-
LEFT JOIN Chests AS c ON b.chest_id = c.chest_id;
123+
LEFT JOIN Chests AS c USING (chest_id);
124+
```
125+
126+
#### Pandas
127+
128+
```python
129+
import pandas as pd
130+
131+
132+
def count_apples_and_oranges(boxes: pd.DataFrame, chests: pd.DataFrame) -> pd.DataFrame:
133+
merged_df = boxes.merge(
134+
chests, on="chest_id", how="left", suffixes=("_box", "_chest")
135+
)
136+
apple_count = (
137+
merged_df["apple_count_box"].fillna(0)
138+
+ merged_df["apple_count_chest"].fillna(0)
139+
).sum()
140+
orange_count = (
141+
merged_df["orange_count_box"].fillna(0)
142+
+ merged_df["orange_count_chest"].fillna(0)
143+
).sum()
144+
return pd.DataFrame({"apple_count": [apple_count], "orange_count": [orange_count]})
122145
```
123146

124147
<!-- tabs:end -->

solution/1700-1799/1715.Count Apples and Oranges/README_EN.md

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,9 @@ Total number of oranges = 15 + 25 + 8 + 28 + 15 + 15 + 17 = 123
105105

106106
<!-- solution:start -->
107107

108-
### Solution 1
108+
### Solution 1: Left Join + Summation
109+
110+
We can perform a left join on the `Boxes` table and the `Chests` table based on `chest_id`, and then calculate the total number of apples and oranges respectively. Note that if a box does not contain any small boxes, then the corresponding `chest_id` will be `null`. In this case, we need to consider the number of apples and oranges in the small boxes within that box to be 0.
109111

110112
<!-- tabs:start -->
111113

@@ -118,7 +120,28 @@ SELECT
118120
SUM(IFNULL(b.orange_count, 0) + IFNULL(c.orange_count, 0)) AS orange_count
119121
FROM
120122
Boxes AS b
121-
LEFT JOIN Chests AS c ON b.chest_id = c.chest_id;
123+
LEFT JOIN Chests AS c USING (chest_id);
124+
```
125+
126+
#### Pandas
127+
128+
```python
129+
import pandas as pd
130+
131+
132+
def count_apples_and_oranges(boxes: pd.DataFrame, chests: pd.DataFrame) -> pd.DataFrame:
133+
merged_df = boxes.merge(
134+
chests, on="chest_id", how="left", suffixes=("_box", "_chest")
135+
)
136+
apple_count = (
137+
merged_df["apple_count_box"].fillna(0)
138+
+ merged_df["apple_count_chest"].fillna(0)
139+
).sum()
140+
orange_count = (
141+
merged_df["orange_count_box"].fillna(0)
142+
+ merged_df["orange_count_chest"].fillna(0)
143+
).sum()
144+
return pd.DataFrame({"apple_count": [apple_count], "orange_count": [orange_count]})
122145
```
123146

124147
<!-- tabs:end -->
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import pandas as pd
2+
3+
4+
def count_apples_and_oranges(boxes: pd.DataFrame, chests: pd.DataFrame) -> pd.DataFrame:
5+
merged_df = boxes.merge(
6+
chests, on="chest_id", how="left", suffixes=("_box", "_chest")
7+
)
8+
apple_count = (
9+
merged_df["apple_count_box"].fillna(0)
10+
+ merged_df["apple_count_chest"].fillna(0)
11+
).sum()
12+
orange_count = (
13+
merged_df["orange_count_box"].fillna(0)
14+
+ merged_df["orange_count_chest"].fillna(0)
15+
).sum()
16+
return pd.DataFrame({"apple_count": [apple_count], "orange_count": [orange_count]})

solution/1700-1799/1715.Count Apples and Oranges/Solution.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ SELECT
44
SUM(IFNULL(b.orange_count, 0) + IFNULL(c.orange_count, 0)) AS orange_count
55
FROM
66
Boxes AS b
7-
LEFT JOIN Chests AS c ON b.chest_id = c.chest_id;
7+
LEFT JOIN Chests AS c USING (chest_id);

solution/1700-1799/1732.Find the Highest Altitude/README.md

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -205,26 +205,4 @@ int largestAltitude(int* gain, int gainSize) {
205205
206206
<!-- solution:end -->
207207
208-
<!-- solution:start -->
209-
210-
### 方法二
211-
212-
<!-- tabs:start -->
213-
214-
#### Python3
215-
216-
```python
217-
class Solution:
218-
def largestAltitude(self, gain: List[int]) -> int:
219-
ans = h = 0
220-
for v in gain:
221-
h += v
222-
ans = max(ans, h)
223-
return ans
224-
```
225-
226-
<!-- tabs:end -->
227-
228-
<!-- solution:end -->
229-
230208
<!-- problem:end -->

solution/1700-1799/1732.Find the Highest Altitude/README_EN.md

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -203,26 +203,4 @@ int largestAltitude(int* gain, int gainSize) {
203203
204204
<!-- solution:end -->
205205
206-
<!-- solution:start -->
207-
208-
### Solution 2
209-
210-
<!-- tabs:start -->
211-
212-
#### Python3
213-
214-
```python
215-
class Solution:
216-
def largestAltitude(self, gain: List[int]) -> int:
217-
ans = h = 0
218-
for v in gain:
219-
h += v
220-
ans = max(ans, h)
221-
return ans
222-
```
223-
224-
<!-- tabs:end -->
225-
226-
<!-- solution:end -->
227-
228206
<!-- problem:end -->

solution/1700-1799/1732.Find the Highest Altitude/Solution2.py

Lines changed: 0 additions & 7 deletions
This file was deleted.

solution/1700-1799/1733.Minimum Number of People to Teach/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ tags:
7979

8080
然后在这个集合 $s$ 中,统计每种语言掌握的人数,获取最大的人数,我们记为 $mx$,那么答案就是 `len(s) - mx`
8181

82-
时间复杂度 $O(m^2\times k)$。其中 $m$ 为语言的数量,而 $k$ 为好友关系的数量。
82+
时间复杂度 $O(m^2 \times k)$。其中 $m$ 为语言的数量,而 $k$ 为好友关系的数量。
8383

8484
<!-- tabs:start -->
8585

0 commit comments

Comments
 (0)