Skip to content
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
2 changes: 0 additions & 2 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ node_modules/
/solution/bash_problem_readme_template_en.md
/solution/0100-0199/0177.Nth Highest Salary/Solution.sql
/solution/0600-0699/0627.Swap Salary/Solution.sql
/solution/1000-1099/1076.Project Employees II/Solution.sql
/solution/1000-1099/1082.Sales Analysis I/Solution.sql
/solution/1100-1199/1173.Immediate Food Delivery I/Solution.sql
/solution/1400-1499/1454.Active Users/Solution.sql
/solution/1400-1499/1484.Group Sold Products By The Date/Solution.sql
Expand Down
27 changes: 15 additions & 12 deletions solution/1000-1099/1076.Project Employees II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,26 +74,29 @@ Result table:

```sql
# Write your MySQL query statement below
SELECT project_id
FROM Project p
GROUP BY project_id
HAVING COUNT(employee_id) >= all(
SELECT COUNT(employee_id)
SELECT project_id
FROM Project
GROUP BY project_id )
GROUP BY 1
HAVING
count(1) >= all(
SELECT count(1)
FROM Project
GROUP BY project_id
);
```

```sql
# Write your MySQL query statement below
SELECT project_id
FROM
(
WITH
T AS (
SELECT
project_id,
dense_rank() OVER (ORDER BY COUNT(employee_id) DESC) AS rk
rank() OVER (ORDER BY count(employee_id) DESC) AS rk
FROM Project
GROUP BY project_id
) AS t
GROUP BY 1
)
SELECT project_id
FROM T
WHERE rk = 1;
```

Expand Down
27 changes: 15 additions & 12 deletions solution/1000-1099/1076.Project Employees II/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,26 +83,29 @@ Employee table:

```sql
# Write your MySQL query statement below
SELECT project_id
FROM Project p
GROUP BY project_id
HAVING COUNT(employee_id) >= all(
SELECT COUNT(employee_id)
SELECT project_id
FROM Project
GROUP BY project_id )
GROUP BY 1
HAVING
count(1) >= all(
SELECT count(1)
FROM Project
GROUP BY project_id
);
```

```sql
# Write your MySQL query statement below
SELECT project_id
FROM
(
WITH
T AS (
SELECT
project_id,
dense_rank() OVER (ORDER BY COUNT(employee_id) DESC) AS rk
rank() OVER (ORDER BY count(employee_id) DESC) AS rk
FROM Project
GROUP BY project_id
) AS t
GROUP BY 1
)
SELECT project_id
FROM T
WHERE rk = 1;
```

Expand Down
18 changes: 11 additions & 7 deletions solution/1000-1099/1076.Project Employees II/Solution.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# Write your MySQL query statement below
SELECT project_id
FROM Project p
GROUP BY project_id
HAVING COUNT(employee_id) >= all(
SELECT COUNT(employee_id)
FROM Project
GROUP BY project_id )
WITH
T AS (
SELECT
project_id,
rank() OVER (ORDER BY count(employee_id) DESC) AS rk
FROM Project
GROUP BY 1
)
SELECT project_id
FROM T
WHERE rk = 1;
27 changes: 15 additions & 12 deletions solution/1000-1099/1077.Project Employees III/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,28 +74,31 @@ employee_id 为 1 和 3 的员工在 project_id 为 1 的项目中拥有最丰

<!-- 这里可写通用的实现逻辑 -->

**方法一:内连接 + 窗口函数**

我们先将 `Project` 表和 `Employee` 表进行内连接,然后使用窗口函数 `rank()` 对 `Project` 表进行分组,按照 `experience_years` 降序排列,最后取出每个项目中经验最丰富的雇员。

<!-- tabs:start -->

### **SQL**

```sql
# Write your MySQL query statement below
SELECT
project_id,
employee_id
FROM
(
WITH
T AS (
SELECT
p.project_id,
p.employee_id,
project_id,
employee_id,
rank() OVER (
PARTITION BY p.project_id
ORDER BY e.experience_years DESC
PARTITION BY project_id
ORDER BY experience_years DESC
) AS rk
FROM
Project AS p
LEFT JOIN Employee AS e ON p.employee_id = e.employee_id
) AS t
Project
JOIN Employee USING (employee_id)
)
SELECT project_id, employee_id
FROM T
WHERE rk = 1;
```

Expand Down
23 changes: 11 additions & 12 deletions solution/1000-1099/1077.Project Employees III/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,22 +85,21 @@ Employee table:

```sql
# Write your MySQL query statement below
SELECT
project_id,
employee_id
FROM
(
WITH
T AS (
SELECT
p.project_id,
p.employee_id,
project_id,
employee_id,
rank() OVER (
PARTITION BY p.project_id
ORDER BY e.experience_years DESC
PARTITION BY project_id
ORDER BY experience_years DESC
) AS rk
FROM
Project AS p
LEFT JOIN Employee AS e ON p.employee_id = e.employee_id
) AS t
Project
JOIN Employee USING (employee_id)
)
SELECT project_id, employee_id
FROM T
WHERE rk = 1;
```

Expand Down
23 changes: 11 additions & 12 deletions solution/1000-1099/1077.Project Employees III/Solution.sql
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
# Write your MySQL query statement below
SELECT
project_id,
employee_id
FROM
(
WITH
T AS (
SELECT
p.project_id,
p.employee_id,
project_id,
employee_id,
rank() OVER (
PARTITION BY p.project_id
ORDER BY e.experience_years DESC
PARTITION BY project_id
ORDER BY experience_years DESC
) AS rk
FROM
Project AS p
LEFT JOIN Employee AS e ON p.employee_id = e.employee_id
) AS t
Project
JOIN Employee USING (employee_id)
)
SELECT project_id, employee_id
FROM T
WHERE rk = 1;
30 changes: 24 additions & 6 deletions solution/1000-1099/1082.Sales Analysis I/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,31 @@ Product 表:

```sql
# Write your MySQL query statement below
SELECT seller_id
SELECT seller_id
FROM Sales
GROUP BY seller_id
HAVING SUM(price) >= ALL (
SELECT SUM(price)
FROM Sales
GROUP BY seller_id )
GROUP BY seller_id
HAVING
SUM(price) >= ALL(
SELECT SUM(price)
FROM Sales
GROUP BY seller_id
);
```

```sql
# Write your MySQL query statement below
WITH
T AS (
SELECT
seller_id,
sum(price) AS tot,
rank() OVER (ORDER BY sum(price) DESC) AS rk
FROM Sales
GROUP BY seller_id
)
SELECT seller_id
FROM T
WHERE rk = 1;
```

<!-- tabs:end -->
30 changes: 24 additions & 6 deletions solution/1000-1099/1082.Sales Analysis I/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,31 @@ Sales table:

```sql
# Write your MySQL query statement below
SELECT seller_id
SELECT seller_id
FROM Sales
GROUP BY seller_id
HAVING SUM(price) >= ALL (
SELECT SUM(price)
FROM Sales
GROUP BY seller_id )
GROUP BY seller_id
HAVING
SUM(price) >= ALL(
SELECT SUM(price)
FROM Sales
GROUP BY seller_id
);
```

```sql
# Write your MySQL query statement below
WITH
T AS (
SELECT
seller_id,
sum(price) AS tot,
rank() OVER (ORDER BY sum(price) DESC) AS rk
FROM Sales
GROUP BY seller_id
)
SELECT seller_id
FROM T
WHERE rk = 1;
```

<!-- tabs:end -->
20 changes: 12 additions & 8 deletions solution/1000-1099/1082.Sales Analysis I/Solution.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# Write your MySQL query statement below
SELECT seller_id
FROM Sales
GROUP BY seller_id
HAVING SUM(price) >= ALL (
SELECT SUM(price)
FROM Sales
GROUP BY seller_id )
WITH
T AS (
SELECT
seller_id,
sum(price) AS tot,
rank() OVER (ORDER BY sum(price) DESC) AS rk
FROM Sales
GROUP BY seller_id
)
SELECT seller_id
FROM T
WHERE rk = 1;
10 changes: 9 additions & 1 deletion solution/2500-2599/2544.Alternating Digit Sum/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@

**方法一:模拟**

从最高有效位开始,每次取出一位数字,根据其相邻数字的符号,决定当前数字的符号,然后将当前数字加入答案。
直接根据题目描述模拟即可。

我们定义一个初始符号 $sign=1$,然后从最高有效位开始,每次取出一位数字 $x$,与 $sign$ 相乘,将结果加到答案中,然后将 $sign$ 取反,继续处理下一位数字,直到处理完所有数字。

时间复杂度 $O(\log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为给定数字。

Expand All @@ -66,6 +68,12 @@

<!-- 这里可写当前语言的特殊实现逻辑 -->

```python
class Solution:
def alternateDigitSum(self, n: int) -> int:
return sum((-1) ** i * int(x) for i, x in enumerate(str(n)))
```

```python
class Solution:
def alternateDigitSum(self, n: int) -> int:
Expand Down
6 changes: 6 additions & 0 deletions solution/2500-2599/2544.Alternating Digit Sum/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@

### **Python3**

```python
class Solution:
def alternateDigitSum(self, n: int) -> int:
return sum((-1) ** i * int(x) for i, x in enumerate(str(n)))
```

```python
class Solution:
def alternateDigitSum(self, n: int) -> int:
Expand Down
11 changes: 3 additions & 8 deletions solution/2500-2599/2544.Alternating Digit Sum/Solution.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
class Solution:
def alternateDigitSum(self, n: int) -> int:
ans, sign = 0, 1
for c in str(n):
x = int(c)
ans += sign * x
sign *= -1
return ans
class Solution:
def alternateDigitSum(self, n: int) -> int:
return sum((-1) ** i * int(x) for i, x in enumerate(str(n)))