Skip to content
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

feat: add solutions to lc problems #1152

Merged
merged 4 commits into from
Jul 6, 2023
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
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ node_modules/
/solution/1600-1699/1635.Hopper Company Queries I/Solution.sql
/solution/1600-1699/1651.Hopper Company Queries III/Solution.sql
/solution/1700-1799/1767.Find the Subtasks That Did Not Execute/Solution.sql
/solution/2100-2199/2118.Build the Equation/Solution.sql
/solution/2200-2299/2205.The Number of Users That Are Eligible for Discount/Solution.sql
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,14 @@ Point2D table:
### **SQL**

```sql

# Write your MySQL query statement below
SELECT round(sqrt(pow(p1.x - p2.x, 2) + pow(p1.y - p2.y, 2)), 2) AS shortest
FROM
Point2D AS p1,
Point2D AS p2
WHERE p1.x != p2.x OR p1.y != p2.y
ORDER BY 1
LIMIT 1;
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,14 @@ Point2D table:
### **SQL**

```sql

# Write your MySQL query statement below
SELECT round(sqrt(pow(p1.x - p2.x, 2) + pow(p1.y - p2.y, 2)), 2) AS shortest
FROM
Point2D AS p1,
Point2D AS p2
WHERE p1.x != p2.x OR p1.y != p2.y
ORDER BY 1
LIMIT 1;
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Write your MySQL query statement below
SELECT round(sqrt(pow(p1.x - p2.x, 2) + pow(p1.y - p2.y, 2)), 2) AS shortest
FROM
Point2D AS p1,
Point2D AS p2
WHERE p1.x != p2.x OR p1.y != p2.y
ORDER BY 1
LIMIT 1;
23 changes: 22 additions & 1 deletion solution/2100-2199/2118.Build the Equation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,28 @@ Terms 表:
<!-- 这里可写当前语言的特殊实现逻辑 -->

```sql

# Write your MySQL query statement below
WITH
T AS (
SELECT
power,
CASE power
WHEN 0 THEN IF(factor > 0, concat('+', factor), factor)
WHEN 1 THEN concat(
IF(factor > 0, concat('+', factor), factor),
'X'
)
ELSE concat(
IF(factor > 0, concat('+', factor), factor),
'X^',
power
)
END AS it
FROM Terms
)
SELECT
concat(group_concat(it ORDER BY power DESC SEPARATOR ""), '=0') AS equation
FROM T;
```

<!-- tabs:end -->
23 changes: 22 additions & 1 deletion solution/2100-2199/2118.Build the Equation/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,28 @@ Terms table:
### **SQL**

```sql

# Write your MySQL query statement below
WITH
T AS (
SELECT
power,
CASE power
WHEN 0 THEN IF(factor > 0, concat('+', factor), factor)
WHEN 1 THEN concat(
IF(factor > 0, concat('+', factor), factor),
'X'
)
ELSE concat(
IF(factor > 0, concat('+', factor), factor),
'X^',
power
)
END AS it
FROM Terms
)
SELECT
concat(group_concat(it ORDER BY power DESC SEPARATOR ""), '=0') AS equation
FROM T;
```

<!-- tabs:end -->
22 changes: 22 additions & 0 deletions solution/2100-2199/2118.Build the Equation/Solution.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Write your MySQL query statement below
WITH
T AS (
SELECT
power,
CASE power
WHEN 0 THEN IF(factor > 0, concat('+', factor), factor)
WHEN 1 THEN concat(
IF(factor > 0, concat('+', factor), factor),
'X'
)
ELSE concat(
IF(factor > 0, concat('+', factor), factor),
'X^',
power
)
END AS it
FROM Terms
)
SELECT
concat(group_concat(it ORDER BY power DESC SEPARATOR ""), '=0') AS equation
FROM T;
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@

**方法一:数学**

假设三个连续的整数分别为 $x-1$, $x$, $x+1$,则它们的和为 $3x$,因此 $num$ 必须是 $3$ 的倍数。如果 $num$ 不是 $3$ 的倍数,则无法表示成三个连续整数的和,返回空数组。否则,令 $x = \frac{num}{3}$,则 $x-1$, $x$, $x+1$ 就是三个连续整数,它们的和为 $num$。

时间复杂度 $O(1)$,空间复杂度 $O(1)$。

<!-- tabs:start -->

### **Python3**
Expand All @@ -48,8 +52,8 @@
```python
class Solution:
def sumOfThree(self, num: int) -> List[int]:
a, b = divmod(num, 3)
return [] if b else [a - 1, a, a + 1]
x, mod = divmod(num, 3)
return [] if mod else [x - 1, x, x + 1]
```

### **Java**
Expand All @@ -74,7 +78,9 @@ class Solution {
class Solution {
public:
vector<long long> sumOfThree(long long num) {
if (num % 3) return {};
if (num % 3) {
return {};
}
long long x = num / 3;
return {x - 1, x, x + 1};
}
Expand All @@ -96,7 +102,13 @@ func sumOfThree(num int64) []int64 {
### **TypeScript**

```ts

function sumOfThree(num: number): number[] {
if (num % 3) {
return [];
}
const x = Math.floor(num / 3);
return [x - 1, x, x + 1];
}
```

### **...**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
```python
class Solution:
def sumOfThree(self, num: int) -> List[int]:
a, b = divmod(num, 3)
return [] if b else [a - 1, a, a + 1]
x, mod = divmod(num, 3)
return [] if mod else [x - 1, x, x + 1]
```

### **Java**
Expand All @@ -64,7 +64,9 @@ class Solution {
class Solution {
public:
vector<long long> sumOfThree(long long num) {
if (num % 3) return {};
if (num % 3) {
return {};
}
long long x = num / 3;
return {x - 1, x, x + 1};
}
Expand All @@ -86,7 +88,13 @@ func sumOfThree(num int64) []int64 {
### **TypeScript**

```ts

function sumOfThree(num: number): number[] {
if (num % 3) {
return [];
}
const x = Math.floor(num / 3);
return [x - 1, x, x + 1];
}
```

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

**方法一:贪心**

如果 $finalSum$ 是奇数,那么无法拆分成若干个互不相同的正偶数之和,直接返回空数组。

否则,我们可以贪心地按照 $2, 4, 6, \cdots$ 的顺序拆分 $finalSum$,直到 $finalSum$ 无法再拆分出一个不同的正偶数为止,此时我们将剩余的 $finalSum$ 加到最后一个正偶数上即可。

时间复杂度 $O(\sqrt{finalSum})$,忽略答案数组的空间消耗,空间复杂度 $O(1)$。

<!-- tabs:start -->

### **Python3**
Expand Down Expand Up @@ -122,17 +128,16 @@ public:
### **Go**

```go
func maximumEvenSplit(finalSum int64) []int64 {
ans := []int64{}
func maximumEvenSplit(finalSum int64) (ans []int64) {
if finalSum%2 == 1 {
return ans
return
}
for i := int64(2); i <= finalSum; i += 2 {
ans = append(ans, i)
finalSum -= i
}
ans[len(ans)-1] += finalSum
return ans
return
}
```

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

### **C#**

```cs
public class Solution {
public IList<long> MaximumEvenSplit(long finalSum) {
IList<long> ans = new List<long>();
if (finalSum % 2 == 1) {
return ans;
}
for (long i = 2; i <= finalSum; i += 2) {
ans.Add(i);
finalSum -= i;
}
ans[ans.Count - 1] += finalSum;
return ans;
}
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,16 @@ public:
### **Go**

```go
func maximumEvenSplit(finalSum int64) []int64 {
ans := []int64{}
func maximumEvenSplit(finalSum int64) (ans []int64) {
if finalSum%2 == 1 {
return ans
return
}
for i := int64(2); i <= finalSum; i += 2 {
ans = append(ans, i)
finalSum -= i
}
ans[len(ans)-1] += finalSum
return ans
return
}
```

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

### **C#**

```cs
public class Solution {
public IList<long> MaximumEvenSplit(long finalSum) {
IList<long> ans = new List<long>();
if (finalSum % 2 == 1) {
return ans;
}
for (long i = 2; i <= finalSum; i += 2) {
ans.Add(i);
finalSum -= i;
}
ans[ans.Count - 1] += finalSum;
return ans;
}
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
public class Solution {
public IList<long> MaximumEvenSplit(long finalSum) {
IList<long> ans = new List<long>();
if (finalSum % 2 == 1) {
return ans;
}
for (long i = 2; i <= finalSum; i += 2) {
ans.Add(i);
finalSum -= i;
}
ans[ans.Count - 1] += finalSum;
return ans;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
func maximumEvenSplit(finalSum int64) []int64 {
ans := []int64{}
func maximumEvenSplit(finalSum int64) (ans []int64) {
if finalSum%2 == 1 {
return ans
return
}
for i := int64(2); i <= finalSum; i += 2 {
ans = append(ans, i)
finalSum -= i
}
ans[len(ans)-1] += finalSum
return ans
return
}
Loading