Skip to content

Commit 31bb9d1

Browse files
authoredJul 6, 2023
feat: add solutions to lc problems (doocs#1152)
* No.0612.Shortest Distance in a Plane * No.2118.Build the Equation * No.2177.Find Three Consecutive Integers That Sum to a Given Number * No.2178.Maximum Split of Positive Even Integers * No.2238.Number of Times a Driver Was a Passenger
1 parent 989c1a4 commit 31bb9d1

File tree

19 files changed

+227
-37
lines changed

19 files changed

+227
-37
lines changed
 

‎.prettierignore

+1
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ node_modules/
2525
/solution/1600-1699/1635.Hopper Company Queries I/Solution.sql
2626
/solution/1600-1699/1651.Hopper Company Queries III/Solution.sql
2727
/solution/1700-1799/1767.Find the Subtasks That Did Not Execute/Solution.sql
28+
/solution/2100-2199/2118.Build the Equation/Solution.sql
2829
/solution/2200-2299/2205.The Number of Users That Are Eligible for Discount/Solution.sql

‎solution/0600-0699/0612.Shortest Distance in a Plane/README.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,14 @@ Point2D table:
6565
### **SQL**
6666

6767
```sql
68-
68+
# Write your MySQL query statement below
69+
SELECT round(sqrt(pow(p1.x - p2.x, 2) + pow(p1.y - p2.y, 2)), 2) AS shortest
70+
FROM
71+
Point2D AS p1,
72+
Point2D AS p2
73+
WHERE p1.x != p2.x OR p1.y != p2.y
74+
ORDER BY 1
75+
LIMIT 1;
6976
```
7077

7178
<!-- tabs:end -->

‎solution/0600-0699/0612.Shortest Distance in a Plane/README_EN.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,14 @@ Point2D table:
5454
### **SQL**
5555

5656
```sql
57-
57+
# Write your MySQL query statement below
58+
SELECT round(sqrt(pow(p1.x - p2.x, 2) + pow(p1.y - p2.y, 2)), 2) AS shortest
59+
FROM
60+
Point2D AS p1,
61+
Point2D AS p2
62+
WHERE p1.x != p2.x OR p1.y != p2.y
63+
ORDER BY 1
64+
LIMIT 1;
5865
```
5966

6067
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Write your MySQL query statement below
2+
SELECT round(sqrt(pow(p1.x - p2.x, 2) + pow(p1.y - p2.y, 2)), 2) AS shortest
3+
FROM
4+
Point2D AS p1,
5+
Point2D AS p2
6+
WHERE p1.x != p2.x OR p1.y != p2.y
7+
ORDER BY 1
8+
LIMIT 1;

‎solution/2100-2199/2118.Build the Equation/README.md

+22-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,28 @@ Terms 表:
109109
<!-- 这里可写当前语言的特殊实现逻辑 -->
110110

111111
```sql
112-
112+
# Write your MySQL query statement below
113+
WITH
114+
T AS (
115+
SELECT
116+
power,
117+
CASE power
118+
WHEN 0 THEN IF(factor > 0, concat('+', factor), factor)
119+
WHEN 1 THEN concat(
120+
IF(factor > 0, concat('+', factor), factor),
121+
'X'
122+
)
123+
ELSE concat(
124+
IF(factor > 0, concat('+', factor), factor),
125+
'X^',
126+
power
127+
)
128+
END AS it
129+
FROM Terms
130+
)
131+
SELECT
132+
concat(group_concat(it ORDER BY power DESC SEPARATOR ""), '=0') AS equation
133+
FROM T;
113134
```
114135

115136
<!-- tabs:end -->

‎solution/2100-2199/2118.Build the Equation/README_EN.md

+22-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,28 @@ Terms table:
101101
### **SQL**
102102

103103
```sql
104-
104+
# Write your MySQL query statement below
105+
WITH
106+
T AS (
107+
SELECT
108+
power,
109+
CASE power
110+
WHEN 0 THEN IF(factor > 0, concat('+', factor), factor)
111+
WHEN 1 THEN concat(
112+
IF(factor > 0, concat('+', factor), factor),
113+
'X'
114+
)
115+
ELSE concat(
116+
IF(factor > 0, concat('+', factor), factor),
117+
'X^',
118+
power
119+
)
120+
END AS it
121+
FROM Terms
122+
)
123+
SELECT
124+
concat(group_concat(it ORDER BY power DESC SEPARATOR ""), '=0') AS equation
125+
FROM T;
105126
```
106127

107128
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Write your MySQL query statement below
2+
WITH
3+
T AS (
4+
SELECT
5+
power,
6+
CASE power
7+
WHEN 0 THEN IF(factor > 0, concat('+', factor), factor)
8+
WHEN 1 THEN concat(
9+
IF(factor > 0, concat('+', factor), factor),
10+
'X'
11+
)
12+
ELSE concat(
13+
IF(factor > 0, concat('+', factor), factor),
14+
'X^',
15+
power
16+
)
17+
END AS it
18+
FROM Terms
19+
)
20+
SELECT
21+
concat(group_concat(it ORDER BY power DESC SEPARATOR ""), '=0') AS equation
22+
FROM T;

‎solution/2100-2199/2177.Find Three Consecutive Integers That Sum to a Given Number/README.md

+16-4
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@
3939

4040
**方法一:数学**
4141

42+
假设三个连续的整数分别为 $x-1$, $x$, $x+1$,则它们的和为 $3x$,因此 $num$ 必须是 $3$ 的倍数。如果 $num$ 不是 $3$ 的倍数,则无法表示成三个连续整数的和,返回空数组。否则,令 $x = \frac{num}{3}$,则 $x-1$, $x$, $x+1$ 就是三个连续整数,它们的和为 $num$。
43+
44+
时间复杂度 $O(1)$,空间复杂度 $O(1)$。
45+
4246
<!-- tabs:start -->
4347

4448
### **Python3**
@@ -48,8 +52,8 @@
4852
```python
4953
class Solution:
5054
def sumOfThree(self, num: int) -> List[int]:
51-
a, b = divmod(num, 3)
52-
return [] if b else [a - 1, a, a + 1]
55+
x, mod = divmod(num, 3)
56+
return [] if mod else [x - 1, x, x + 1]
5357
```
5458

5559
### **Java**
@@ -74,7 +78,9 @@ class Solution {
7478
class Solution {
7579
public:
7680
vector<long long> sumOfThree(long long num) {
77-
if (num % 3) return {};
81+
if (num % 3) {
82+
return {};
83+
}
7884
long long x = num / 3;
7985
return {x - 1, x, x + 1};
8086
}
@@ -96,7 +102,13 @@ func sumOfThree(num int64) []int64 {
96102
### **TypeScript**
97103

98104
```ts
99-
105+
function sumOfThree(num: number): number[] {
106+
if (num % 3) {
107+
return [];
108+
}
109+
const x = Math.floor(num / 3);
110+
return [x - 1, x, x + 1];
111+
}
100112
```
101113

102114
### **...**

‎solution/2100-2199/2177.Find Three Consecutive Integers That Sum to a Given Number/README_EN.md

+12-4
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@
4040
```python
4141
class Solution:
4242
def sumOfThree(self, num: int) -> List[int]:
43-
a, b = divmod(num, 3)
44-
return [] if b else [a - 1, a, a + 1]
43+
x, mod = divmod(num, 3)
44+
return [] if mod else [x - 1, x, x + 1]
4545
```
4646

4747
### **Java**
@@ -64,7 +64,9 @@ class Solution {
6464
class Solution {
6565
public:
6666
vector<long long> sumOfThree(long long num) {
67-
if (num % 3) return {};
67+
if (num % 3) {
68+
return {};
69+
}
6870
long long x = num / 3;
6971
return {x - 1, x, x + 1};
7072
}
@@ -86,7 +88,13 @@ func sumOfThree(num int64) []int64 {
8688
### **TypeScript**
8789

8890
```ts
89-
91+
function sumOfThree(num: number): number[] {
92+
if (num % 3) {
93+
return [];
94+
}
95+
const x = Math.floor(num / 3);
96+
return [x - 1, x, x + 1];
97+
}
9098
```
9199

92100
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
class Solution {
2-
public:
3-
vector<long long> sumOfThree(long long num) {
4-
if (num % 3) return {};
5-
long long x = num / 3;
6-
return {x - 1, x, x + 1};
7-
}
1+
class Solution {
2+
public:
3+
vector<long long> sumOfThree(long long num) {
4+
if (num % 3) {
5+
return {};
6+
}
7+
long long x = num / 3;
8+
return {x - 1, x, x + 1};
9+
}
810
};
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
class Solution:
2-
def sumOfThree(self, num: int) -> List[int]:
3-
a, b = divmod(num, 3)
4-
return [] if b else [a - 1, a, a + 1]
1+
class Solution:
2+
def sumOfThree(self, num: int) -> List[int]:
3+
x, mod = divmod(num, 3)
4+
return [] if mod else [x - 1, x, x + 1]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function sumOfThree(num: number): number[] {
2+
if (num % 3) {
3+
return [];
4+
}
5+
const x = Math.floor(num / 3);
6+
return [x - 1, x, x + 1];
7+
}

‎solution/2100-2199/2178.Maximum Split of Positive Even Integers/README.md

+28-4
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@
5959

6060
**方法一:贪心**
6161

62+
如果 $finalSum$ 是奇数,那么无法拆分成若干个互不相同的正偶数之和,直接返回空数组。
63+
64+
否则,我们可以贪心地按照 $2, 4, 6, \cdots$ 的顺序拆分 $finalSum$,直到 $finalSum$ 无法再拆分出一个不同的正偶数为止,此时我们将剩余的 $finalSum$ 加到最后一个正偶数上即可。
65+
66+
时间复杂度 $O(\sqrt{finalSum})$,忽略答案数组的空间消耗,空间复杂度 $O(1)$。
67+
6268
<!-- tabs:start -->
6369

6470
### **Python3**
@@ -122,17 +128,16 @@ public:
122128
### **Go**
123129
124130
```go
125-
func maximumEvenSplit(finalSum int64) []int64 {
126-
ans := []int64{}
131+
func maximumEvenSplit(finalSum int64) (ans []int64) {
127132
if finalSum%2 == 1 {
128-
return ans
133+
return
129134
}
130135
for i := int64(2); i <= finalSum; i += 2 {
131136
ans = append(ans, i)
132137
finalSum -= i
133138
}
134139
ans[len(ans)-1] += finalSum
135-
return ans
140+
return
136141
}
137142
```
138143

@@ -153,6 +158,25 @@ function maximumEvenSplit(finalSum: number): number[] {
153158
}
154159
```
155160

161+
### **C#**
162+
163+
```cs
164+
public class Solution {
165+
public IList<long> MaximumEvenSplit(long finalSum) {
166+
IList<long> ans = new List<long>();
167+
if (finalSum % 2 == 1) {
168+
return ans;
169+
}
170+
for (long i = 2; i <= finalSum; i += 2) {
171+
ans.Add(i);
172+
finalSum -= i;
173+
}
174+
ans[ans.Count - 1] += finalSum;
175+
return ans;
176+
}
177+
}
178+
```
179+
156180
### **...**
157181

158182
```

‎solution/2100-2199/2178.Maximum Split of Positive Even Integers/README_EN.md

+22-4
Original file line numberDiff line numberDiff line change
@@ -110,17 +110,16 @@ public:
110110
### **Go**
111111
112112
```go
113-
func maximumEvenSplit(finalSum int64) []int64 {
114-
ans := []int64{}
113+
func maximumEvenSplit(finalSum int64) (ans []int64) {
115114
if finalSum%2 == 1 {
116-
return ans
115+
return
117116
}
118117
for i := int64(2); i <= finalSum; i += 2 {
119118
ans = append(ans, i)
120119
finalSum -= i
121120
}
122121
ans[len(ans)-1] += finalSum
123-
return ans
122+
return
124123
}
125124
```
126125

@@ -141,6 +140,25 @@ function maximumEvenSplit(finalSum: number): number[] {
141140
}
142141
```
143142

143+
### **C#**
144+
145+
```cs
146+
public class Solution {
147+
public IList<long> MaximumEvenSplit(long finalSum) {
148+
IList<long> ans = new List<long>();
149+
if (finalSum % 2 == 1) {
150+
return ans;
151+
}
152+
for (long i = 2; i <= finalSum; i += 2) {
153+
ans.Add(i);
154+
finalSum -= i;
155+
}
156+
ans[ans.Count - 1] += finalSum;
157+
return ans;
158+
}
159+
}
160+
```
161+
144162
### **...**
145163

146164
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public class Solution {
2+
public IList<long> MaximumEvenSplit(long finalSum) {
3+
IList<long> ans = new List<long>();
4+
if (finalSum % 2 == 1) {
5+
return ans;
6+
}
7+
for (long i = 2; i <= finalSum; i += 2) {
8+
ans.Add(i);
9+
finalSum -= i;
10+
}
11+
ans[ans.Count - 1] += finalSum;
12+
return ans;
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
func maximumEvenSplit(finalSum int64) []int64 {
2-
ans := []int64{}
1+
func maximumEvenSplit(finalSum int64) (ans []int64) {
32
if finalSum%2 == 1 {
4-
return ans
3+
return
54
}
65
for i := int64(2); i <= finalSum; i += 2 {
76
ans = append(ans, i)
87
finalSum -= i
98
}
109
ans[len(ans)-1] += finalSum
11-
return ans
10+
return
1211
}

‎solution/2200-2299/2238.Number of Times a Driver Was a Passenger/README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,13 @@ ID = 11 的司机从来不是乘客。</pre>
6868
<!-- 这里可写当前语言的特殊实现逻辑 -->
6969

7070
```sql
71-
71+
# Write your MySQL query statement below
72+
WITH T AS (SELECT DISTINCT driver_id FROM Rides)
73+
SELECT t.driver_id, count(passenger_id) AS cnt
74+
FROM
75+
T AS t
76+
LEFT JOIN Rides AS r ON t.driver_id = r.passenger_id
77+
GROUP BY 1;
7278
```
7379

7480
<!-- tabs:end -->

‎solution/2200-2299/2238.Number of Times a Driver Was a Passenger/README_EN.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,13 @@ The driver with ID = 11 was never a passenger.
6363
### **SQL**
6464

6565
```sql
66-
66+
# Write your MySQL query statement below
67+
WITH T AS (SELECT DISTINCT driver_id FROM Rides)
68+
SELECT t.driver_id, count(passenger_id) AS cnt
69+
FROM
70+
T AS t
71+
LEFT JOIN Rides AS r ON t.driver_id = r.passenger_id
72+
GROUP BY 1;
6773
```
6874

6975
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Write your MySQL query statement below
2+
WITH T AS (SELECT DISTINCT driver_id FROM Rides)
3+
SELECT t.driver_id, count(passenger_id) AS cnt
4+
FROM
5+
T AS t
6+
LEFT JOIN Rides AS r ON t.driver_id = r.passenger_id
7+
GROUP BY 1;

0 commit comments

Comments
 (0)
Please sign in to comment.