Skip to content

Commit 29d3201

Browse files
authored
feat: add solutions to lc problems: No.0670,3010 (doocs#2245)
* No.0670.Maximum Swap * No.3010.Divide an Array Into Subarrays With Minimum Cost I
1 parent d15457c commit 29d3201

File tree

9 files changed

+207
-13
lines changed

9 files changed

+207
-13
lines changed

solution/0600-0699/0670.Maximum Swap/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@
3434

3535
### 方法一:贪心
3636

37-
先将数字转为字符串 `s`,然后从右往左遍历字符串 `s`,用数组或哈希表 `d` 记录每个数字右侧的最大数字的位置(可以是字符本身的位置)。
37+
我们先将数字转为字符串 $s$,然后从右往左遍历字符串 $s$,用数组或哈希表 $d$ 记录每个数字右侧的最大数字的位置(可以是数字本身的位置)。
3838

39-
接着从左到右遍历 `d`,如果 `s[i] < s[d[i]]`,则进行交换,并退出遍历的过程。
39+
接着从左到右遍历 $d$,如果 $s[i] \lt s[d[i]]$,则进行交换,并退出遍历的过程。
4040

41-
最后将字符串 `s` 转为数字,即为答案。
41+
最后将字符串 $s$ 转为数字,即为答案。
4242

43-
时间复杂度 $O(\log C)$,空间复杂度 $O(\log C)$。其中 $C$ 是数字 `num` 的值域
43+
时间复杂度 $O(\log M)$,空间复杂度 $O(\log M)$。其中 $M$ 是数字 $num$ 的取值范围
4444

4545
<!-- tabs:start -->
4646

solution/0600-0699/0670.Maximum Swap/README_EN.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,15 @@
3434

3535
## Solutions
3636

37-
### Solution 1
37+
### Solution 1: Greedy Algorithm
38+
39+
First, we convert the number into a string $s$. Then, we traverse the string $s$ from right to left, using an array or hash table $d$ to record the position of the maximum number to the right of each number (it can be the position of the number itself).
40+
41+
Next, we traverse $d$ from left to right. If $s[i] < s[d[i]]$, we swap them and exit the traversal process.
42+
43+
Finally, we convert the string $s$ back into a number, which is the answer.
44+
45+
The time complexity is $O(\log M)$, and the space complexity is $O(\log M)$. Here, $M$ is the range of the number $num$.
3846

3947
<!-- tabs:start -->
4048

solution/3000-3099/3010.Divide an Array Into Subarrays With Minimum Cost I/README.md

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,24 +56,87 @@
5656

5757
## 解法
5858

59-
### 方法一
59+
### 方法一:遍历找最小值和次小值
60+
61+
我们记数组 $nums$ 的第一个元素为 $a$,其余元素中最小的元素为 $b$,次小的元素为 $c$,那么答案就是 $a+b+c$。
62+
63+
时间复杂度 $O(n)$,其中 $n$ 是数组 $nums$ 的长度。空间复杂度 $O(1)$。
6064

6165
<!-- tabs:start -->
6266

6367
```python
64-
68+
class Solution:
69+
def minimumCost(self, nums: List[int]) -> int:
70+
a, b, c = nums[0], inf, inf
71+
for x in nums[1:]:
72+
if x < b:
73+
c, b = b, x
74+
elif x < c:
75+
c = x
76+
return a + b + c
6577
```
6678

6779
```java
68-
80+
class Solution {
81+
public int minimumCost(int[] nums) {
82+
int a = nums[0], b = 100, c = 100;
83+
for (int i = 1; i < nums.length; ++i) {
84+
if (nums[i] < b) {
85+
c = b;
86+
b = nums[i];
87+
} else if (nums[i] < c) {
88+
c = nums[i];
89+
}
90+
}
91+
return a + b + c;
92+
}
93+
}
6994
```
7095

7196
```cpp
72-
97+
class Solution {
98+
public:
99+
int minimumCost(vector<int>& nums) {
100+
int a = nums[0], b = 100, c = 100;
101+
for (int i = 1; i < nums.size(); ++i) {
102+
if (nums[i] < b) {
103+
c = b;
104+
b = nums[i];
105+
} else if (nums[i] < c) {
106+
c = nums[i];
107+
}
108+
}
109+
return a + b + c;
110+
}
111+
};
73112
```
74113
75114
```go
115+
func minimumCost(nums []int) int {
116+
a, b, c := nums[0], 100, 100
117+
for _, x := range nums[1:] {
118+
if x < b {
119+
b, c = x, b
120+
} else if x < c {
121+
c = x
122+
}
123+
}
124+
return a + b + c
125+
}
126+
```
76127

128+
```ts
129+
function minimumCost(nums: number[]): number {
130+
let [a, b, c] = [nums[0], 100, 100];
131+
for (const x of nums.slice(1)) {
132+
if (x < b) {
133+
[b, c] = [x, b];
134+
} else if (x < c) {
135+
c = x;
136+
}
137+
}
138+
return a + b + c;
139+
}
77140
```
78141

79142
<!-- tabs:end -->

solution/3000-3099/3010.Divide an Array Into Subarrays With Minimum Cost I/README_EN.md

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,24 +52,87 @@ It can be shown that 12 is the minimum cost achievable.
5252

5353
## Solutions
5454

55-
### Solution 1
55+
### Solution 1: Traverse to Find the Smallest and Second Smallest Values
56+
57+
We set the first element of the array $nums$ as $a$, the smallest element among the remaining elements as $b$, and the second smallest element as $c$. The answer is $a+b+c$.
58+
59+
The time complexity is $O(n)$, where $n$ is the length of the array $nums$. The space complexity is $O(1)$.
5660

5761
<!-- tabs:start -->
5862

5963
```python
60-
64+
class Solution:
65+
def minimumCost(self, nums: List[int]) -> int:
66+
a, b, c = nums[0], inf, inf
67+
for x in nums[1:]:
68+
if x < b:
69+
c, b = b, x
70+
elif x < c:
71+
c = x
72+
return a + b + c
6173
```
6274

6375
```java
64-
76+
class Solution {
77+
public int minimumCost(int[] nums) {
78+
int a = nums[0], b = 100, c = 100;
79+
for (int i = 1; i < nums.length; ++i) {
80+
if (nums[i] < b) {
81+
c = b;
82+
b = nums[i];
83+
} else if (nums[i] < c) {
84+
c = nums[i];
85+
}
86+
}
87+
return a + b + c;
88+
}
89+
}
6590
```
6691

6792
```cpp
68-
93+
class Solution {
94+
public:
95+
int minimumCost(vector<int>& nums) {
96+
int a = nums[0], b = 100, c = 100;
97+
for (int i = 1; i < nums.size(); ++i) {
98+
if (nums[i] < b) {
99+
c = b;
100+
b = nums[i];
101+
} else if (nums[i] < c) {
102+
c = nums[i];
103+
}
104+
}
105+
return a + b + c;
106+
}
107+
};
69108
```
70109
71110
```go
111+
func minimumCost(nums []int) int {
112+
a, b, c := nums[0], 100, 100
113+
for _, x := range nums[1:] {
114+
if x < b {
115+
b, c = x, b
116+
} else if x < c {
117+
c = x
118+
}
119+
}
120+
return a + b + c
121+
}
122+
```
72123

124+
```ts
125+
function minimumCost(nums: number[]): number {
126+
let [a, b, c] = [nums[0], 100, 100];
127+
for (const x of nums.slice(1)) {
128+
if (x < b) {
129+
[b, c] = [x, b];
130+
} else if (x < c) {
131+
c = x;
132+
}
133+
}
134+
return a + b + c;
135+
}
73136
```
74137

75138
<!-- tabs:end -->
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
int minimumCost(vector<int>& nums) {
4+
int a = nums[0], b = 100, c = 100;
5+
for (int i = 1; i < nums.size(); ++i) {
6+
if (nums[i] < b) {
7+
c = b;
8+
b = nums[i];
9+
} else if (nums[i] < c) {
10+
c = nums[i];
11+
}
12+
}
13+
return a + b + c;
14+
}
15+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
func minimumCost(nums []int) int {
2+
a, b, c := nums[0], 100, 100
3+
for _, x := range nums[1:] {
4+
if x < b {
5+
b, c = x, b
6+
} else if x < c {
7+
c = x
8+
}
9+
}
10+
return a + b + c
11+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public int minimumCost(int[] nums) {
3+
int a = nums[0], b = 100, c = 100;
4+
for (int i = 1; i < nums.length; ++i) {
5+
if (nums[i] < b) {
6+
c = b;
7+
b = nums[i];
8+
} else if (nums[i] < c) {
9+
c = nums[i];
10+
}
11+
}
12+
return a + b + c;
13+
}
14+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def minimumCost(self, nums: List[int]) -> int:
3+
a, b, c = nums[0], inf, inf
4+
for x in nums[1:]:
5+
if x < b:
6+
c, b = b, x
7+
elif x < c:
8+
c = x
9+
return a + b + c
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function minimumCost(nums: number[]): number {
2+
let [a, b, c] = [nums[0], 100, 100];
3+
for (const x of nums.slice(1)) {
4+
if (x < b) {
5+
[b, c] = [x, b];
6+
} else if (x < c) {
7+
c = x;
8+
}
9+
}
10+
return a + b + c;
11+
}

0 commit comments

Comments
 (0)