Skip to content

Commit 0e66859

Browse files
authored
feat: add solutions to lc problem: No.2041 (doocs#3099)
No.2041.Accepted Candidates From the Interviews
1 parent 071f1b0 commit 0e66859

File tree

7 files changed

+49
-89
lines changed

7 files changed

+49
-89
lines changed

solution/2000-2099/2041.Accepted Candidates From the Interviews/README.md

+19-4
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ Rounds table:
106106

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

109-
### 方法一
109+
### 方法一:连接表 + 分组 + 过滤
110+
111+
我们可以将 `Candidates` 表和 `Rounds` 表按照 `interview_id` 进行连接,筛选出工作年限至少为 2 年的候选人,然后按照 `candidate_id` 进行分组,计算每个候选人的总分,最后筛选出总分大于 15 分的候选人。
110112

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

@@ -116,13 +118,26 @@ Rounds table:
116118
# Write your MySQL query statement below
117119
SELECT candidate_id
118120
FROM
119-
Candidates AS c
120-
LEFT JOIN Rounds AS r ON c.interview_id = r.interview_id
121+
Candidates
122+
JOIN Rounds USING (interview_id)
121123
WHERE years_of_exp >= 2
122-
GROUP BY c.interview_id
124+
GROUP BY 1
123125
HAVING SUM(score) > 15;
124126
```
125127

128+
#### Pandas
129+
130+
```python
131+
import pandas as pd
132+
133+
134+
def accepted_candidates(candidates: pd.DataFrame, rounds: pd.DataFrame) -> pd.DataFrame:
135+
merged_df = pd.merge(candidates, rounds, on="interview_id")
136+
filtered_df = merged_df[merged_df["years_of_exp"] >= 2]
137+
grouped_df = filtered_df.groupby("candidate_id").agg({"score": "sum"})
138+
return grouped_df[grouped_df["score"] > 15].reset_index()[["candidate_id"]]
139+
```
140+
126141
<!-- tabs:end -->
127142

128143
<!-- solution:end -->

solution/2000-2099/2041.Accepted Candidates From the Interviews/README_EN.md

+19-4
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,9 @@ Rounds table:
105105

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

108-
### Solution 1
108+
### Solution 1: Join Tables + Grouping + Filtering
109+
110+
We can join the `Candidates` table and the `Rounds` table based on `interview_id`, filter out candidates with at least 2 years of work experience, then group by `candidate_id` to calculate the total score for each candidate, and finally filter out candidates with a total score greater than 15.
109111

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

@@ -115,13 +117,26 @@ Rounds table:
115117
# Write your MySQL query statement below
116118
SELECT candidate_id
117119
FROM
118-
Candidates AS c
119-
LEFT JOIN Rounds AS r ON c.interview_id = r.interview_id
120+
Candidates
121+
JOIN Rounds USING (interview_id)
120122
WHERE years_of_exp >= 2
121-
GROUP BY c.interview_id
123+
GROUP BY 1
122124
HAVING SUM(score) > 15;
123125
```
124126

127+
#### Pandas
128+
129+
```python
130+
import pandas as pd
131+
132+
133+
def accepted_candidates(candidates: pd.DataFrame, rounds: pd.DataFrame) -> pd.DataFrame:
134+
merged_df = pd.merge(candidates, rounds, on="interview_id")
135+
filtered_df = merged_df[merged_df["years_of_exp"] >= 2]
136+
grouped_df = filtered_df.groupby("candidate_id").agg({"score": "sum"})
137+
return grouped_df[grouped_df["score"] > 15].reset_index()[["candidate_id"]]
138+
```
139+
125140
<!-- tabs:end -->
126141

127142
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import pandas as pd
2+
3+
4+
def accepted_candidates(candidates: pd.DataFrame, rounds: pd.DataFrame) -> pd.DataFrame:
5+
merged_df = pd.merge(candidates, rounds, on="interview_id")
6+
filtered_df = merged_df[merged_df["years_of_exp"] >= 2]
7+
grouped_df = filtered_df.groupby("candidate_id").agg({"score": "sum"})
8+
return grouped_df[grouped_df["score"] > 15].reset_index()[["candidate_id"]]
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Write your MySQL query statement below
22
SELECT candidate_id
33
FROM
4-
Candidates AS c
5-
LEFT JOIN Rounds AS r ON c.interview_id = r.interview_id
4+
Candidates
5+
JOIN Rounds USING (interview_id)
66
WHERE years_of_exp >= 2
7-
GROUP BY c.interview_id
7+
GROUP BY 1
88
HAVING SUM(score) > 15;

solution/2000-2099/2042.Check if Numbers Are Ascending in a Sentence/README.md

-31
Original file line numberDiff line numberDiff line change
@@ -241,35 +241,4 @@ bool areNumbersAscending(char* s) {
241241
242242
<!-- solution:end -->
243243
244-
<!-- solution:start -->
245-
246-
### 方法二
247-
248-
<!-- tabs:start -->
249-
250-
#### Python3
251-
252-
```python
253-
class Solution:
254-
def areNumbersAscending(self, s: str) -> bool:
255-
pre = i = 0
256-
n = len(s)
257-
while i < n:
258-
if s[i].isdigit():
259-
cur = 0
260-
while i < n and s[i].isdigit():
261-
cur = cur * 10 + int(s[i])
262-
i += 1
263-
if pre >= cur:
264-
return False
265-
pre = cur
266-
else:
267-
i += 1
268-
return True
269-
```
270-
271-
<!-- tabs:end -->
272-
273-
<!-- solution:end -->
274-
275244
<!-- problem:end -->

solution/2000-2099/2042.Check if Numbers Are Ascending in a Sentence/README_EN.md

-31
Original file line numberDiff line numberDiff line change
@@ -226,35 +226,4 @@ bool areNumbersAscending(char* s) {
226226
227227
<!-- solution:end -->
228228
229-
<!-- solution:start -->
230-
231-
### Solution 2
232-
233-
<!-- tabs:start -->
234-
235-
#### Python3
236-
237-
```python
238-
class Solution:
239-
def areNumbersAscending(self, s: str) -> bool:
240-
pre = i = 0
241-
n = len(s)
242-
while i < n:
243-
if s[i].isdigit():
244-
cur = 0
245-
while i < n and s[i].isdigit():
246-
cur = cur * 10 + int(s[i])
247-
i += 1
248-
if pre >= cur:
249-
return False
250-
pre = cur
251-
else:
252-
i += 1
253-
return True
254-
```
255-
256-
<!-- tabs:end -->
257-
258-
<!-- solution:end -->
259-
260229
<!-- problem:end -->

solution/2000-2099/2042.Check if Numbers Are Ascending in a Sentence/Solution2.py

-16
This file was deleted.

0 commit comments

Comments
 (0)