Skip to content

feat: add solutions to lc problem: No.2041 #3099

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 1 commit into from
Jun 13, 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
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ Rounds table:

<!-- solution:start -->

### 方法一
### 方法一:连接表 + 分组 + 过滤

我们可以将 `Candidates` 表和 `Rounds` 表按照 `interview_id` 进行连接,筛选出工作年限至少为 2 年的候选人,然后按照 `candidate_id` 进行分组,计算每个候选人的总分,最后筛选出总分大于 15 分的候选人。

<!-- tabs:start -->

Expand All @@ -116,13 +118,26 @@ Rounds table:
# Write your MySQL query statement below
SELECT candidate_id
FROM
Candidates AS c
LEFT JOIN Rounds AS r ON c.interview_id = r.interview_id
Candidates
JOIN Rounds USING (interview_id)
WHERE years_of_exp >= 2
GROUP BY c.interview_id
GROUP BY 1
HAVING SUM(score) > 15;
```

#### Pandas

```python
import pandas as pd


def accepted_candidates(candidates: pd.DataFrame, rounds: pd.DataFrame) -> pd.DataFrame:
merged_df = pd.merge(candidates, rounds, on="interview_id")
filtered_df = merged_df[merged_df["years_of_exp"] >= 2]
grouped_df = filtered_df.groupby("candidate_id").agg({"score": "sum"})
return grouped_df[grouped_df["score"] > 15].reset_index()[["candidate_id"]]
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ Rounds table:

<!-- solution:start -->

### Solution 1
### Solution 1: Join Tables + Grouping + Filtering

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.

<!-- tabs:start -->

Expand All @@ -115,13 +117,26 @@ Rounds table:
# Write your MySQL query statement below
SELECT candidate_id
FROM
Candidates AS c
LEFT JOIN Rounds AS r ON c.interview_id = r.interview_id
Candidates
JOIN Rounds USING (interview_id)
WHERE years_of_exp >= 2
GROUP BY c.interview_id
GROUP BY 1
HAVING SUM(score) > 15;
```

#### Pandas

```python
import pandas as pd


def accepted_candidates(candidates: pd.DataFrame, rounds: pd.DataFrame) -> pd.DataFrame:
merged_df = pd.merge(candidates, rounds, on="interview_id")
filtered_df = merged_df[merged_df["years_of_exp"] >= 2]
grouped_df = filtered_df.groupby("candidate_id").agg({"score": "sum"})
return grouped_df[grouped_df["score"] > 15].reset_index()[["candidate_id"]]
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import pandas as pd


def accepted_candidates(candidates: pd.DataFrame, rounds: pd.DataFrame) -> pd.DataFrame:
merged_df = pd.merge(candidates, rounds, on="interview_id")
filtered_df = merged_df[merged_df["years_of_exp"] >= 2]
grouped_df = filtered_df.groupby("candidate_id").agg({"score": "sum"})
return grouped_df[grouped_df["score"] > 15].reset_index()[["candidate_id"]]
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Write your MySQL query statement below
SELECT candidate_id
FROM
Candidates AS c
LEFT JOIN Rounds AS r ON c.interview_id = r.interview_id
Candidates
JOIN Rounds USING (interview_id)
WHERE years_of_exp >= 2
GROUP BY c.interview_id
GROUP BY 1
HAVING SUM(score) > 15;
Original file line number Diff line number Diff line change
Expand Up @@ -241,35 +241,4 @@ bool areNumbersAscending(char* s) {

<!-- solution:end -->

<!-- solution:start -->

### 方法二

<!-- tabs:start -->

#### Python3

```python
class Solution:
def areNumbersAscending(self, s: str) -> bool:
pre = i = 0
n = len(s)
while i < n:
if s[i].isdigit():
cur = 0
while i < n and s[i].isdigit():
cur = cur * 10 + int(s[i])
i += 1
if pre >= cur:
return False
pre = cur
else:
i += 1
return True
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -226,35 +226,4 @@ bool areNumbersAscending(char* s) {

<!-- solution:end -->

<!-- solution:start -->

### Solution 2

<!-- tabs:start -->

#### Python3

```python
class Solution:
def areNumbersAscending(self, s: str) -> bool:
pre = i = 0
n = len(s)
while i < n:
if s[i].isdigit():
cur = 0
while i < n and s[i].isdigit():
cur = cur * 10 + int(s[i])
i += 1
if pre >= cur:
return False
pre = cur
else:
i += 1
return True
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->

This file was deleted.