Skip to content

Commit 730e1c2

Browse files
committed
feat: add solutions to lc problems: No.0343,1806
* No.0343.Integer Break * No.1806.Minimum Number of Operations to Reinitialize a Permutation
1 parent 9886834 commit 730e1c2

File tree

18 files changed

+537
-69
lines changed

18 files changed

+537
-69
lines changed

solution/0300-0399/0343.Integer Break/README.md

+92-2
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,21 @@
4040

4141
**方法一:动态规划**
4242

43-
dp[i] 表示正整数 n 能获得的最大乘积,初始化 `dp[1] = 1`
43+
我们定义 $dp[i]$ 表示正整数 $n$ 能获得的最大乘积,初始化 $dp[1] = 1$。答案即为 $dp[n]$
4444

45-
`i >= 2` 时,`dp[i] = max(dp[i], dp[i - j] * j, (i - j) * j)``j∈[0, i)`)。
45+
状态转移方程为:
46+
47+
$$
48+
dp[i] = max(dp[i], dp[i - j] * j, (i - j) * j) \quad (j \in [0, i))
49+
$$
50+
51+
时间复杂度 $O(n^2)$,空间复杂度 $O(n)$。其中 $n$ 为正整数 $n$。
52+
53+
**方法二:数学**
54+
55+
当 $n \lt 4$ 时,$n$ 不能拆分成至少两个正整数的和,因此 $n - 1$ 是最大乘积。当 $n \ge 4$ 时,我们尽可能多地拆分 $3$,当剩下的最后一段为 $4$ 时,我们将其拆分为 $2 + 2$,这样乘积最大。
56+
57+
时间复杂度 $O(1)$,空间复杂度 $O(1)$。
4658

4759
<!-- tabs:start -->
4860

@@ -60,6 +72,18 @@ class Solution:
6072
return dp[n]
6173
```
6274

75+
```python
76+
class Solution:
77+
def integerBreak(self, n: int) -> int:
78+
if n < 4:
79+
return n - 1
80+
if n % 3 == 0:
81+
return pow(3, n // 3)
82+
if n % 3 == 1:
83+
return pow(3, n // 3 - 1) * 4
84+
return pow(3, n // 3) * 2
85+
```
86+
6387
### **Java**
6488

6589
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -79,6 +103,23 @@ class Solution {
79103
}
80104
```
81105

106+
```java
107+
class Solution {
108+
public int integerBreak(int n) {
109+
if (n < 4) {
110+
return n - 1;
111+
}
112+
if (n % 3 == 0) {
113+
return (int) Math.pow(3, n / 3);
114+
}
115+
if (n % 3 == 1) {
116+
return (int) Math.pow(3, n / 3 - 1) * 4;
117+
}
118+
return (int) Math.pow(3, n / 3) * 2;
119+
}
120+
}
121+
```
122+
82123
### **C++**
83124

84125
```cpp
@@ -97,6 +138,24 @@ public:
97138
};
98139
```
99140
141+
```cpp
142+
class Solution {
143+
public:
144+
int integerBreak(int n) {
145+
if (n < 4) {
146+
return n - 1;
147+
}
148+
if (n % 3 == 0) {
149+
return pow(3, n / 3);
150+
}
151+
if (n % 3 == 1) {
152+
return pow(3, n / 3 - 1) * 4;
153+
}
154+
return pow(3, n / 3) * 2;
155+
}
156+
};
157+
```
158+
100159
### **Go**
101160

102161
```go
@@ -119,6 +178,21 @@ func max(a, b int) int {
119178
}
120179
```
121180

181+
```go
182+
func integerBreak(n int) int {
183+
if n < 4 {
184+
return n - 1
185+
}
186+
if n%3 == 0 {
187+
return int(math.Pow(3, float64(n/3)))
188+
}
189+
if n%3 == 1 {
190+
return int(math.Pow(3, float64(n/3-1))) * 4
191+
}
192+
return int(math.Pow(3, float64(n/3))) * 2
193+
}
194+
```
195+
122196
### **C**
123197

124198
```c
@@ -159,6 +233,22 @@ function integerBreak(n: number): number {
159233
}
160234
```
161235

236+
```ts
237+
function integerBreak(n: number): number {
238+
if (n < 4) {
239+
return n - 1;
240+
}
241+
const m = Math.floor(n / 3);
242+
if (n % 3 == 0) {
243+
return 3 ** m;
244+
}
245+
if (n % 3 == 1) {
246+
return 3 ** (m - 1) * 4;
247+
}
248+
return 3 ** m * 2;
249+
}
250+
```
251+
162252
### **...**
163253

164254
```

solution/0300-0399/0343.Integer Break/README_EN.md

+78
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@ class Solution:
5050
return dp[n]
5151
```
5252

53+
```python
54+
class Solution:
55+
def integerBreak(self, n: int) -> int:
56+
if n < 4:
57+
return n - 1
58+
if n % 3 == 0:
59+
return pow(3, n // 3)
60+
if n % 3 == 1:
61+
return pow(3, n // 3 - 1) * 4
62+
return pow(3, n // 3) * 2
63+
```
64+
5365
### **Java**
5466

5567
```java
@@ -67,6 +79,23 @@ class Solution {
6779
}
6880
```
6981

82+
```java
83+
class Solution {
84+
public int integerBreak(int n) {
85+
if (n < 4) {
86+
return n - 1;
87+
}
88+
if (n % 3 == 0) {
89+
return (int) Math.pow(3, n / 3);
90+
}
91+
if (n % 3 == 1) {
92+
return (int) Math.pow(3, n / 3 - 1) * 4;
93+
}
94+
return (int) Math.pow(3, n / 3) * 2;
95+
}
96+
}
97+
```
98+
7099
### **C++**
71100

72101
```cpp
@@ -85,6 +114,24 @@ public:
85114
};
86115
```
87116
117+
```cpp
118+
class Solution {
119+
public:
120+
int integerBreak(int n) {
121+
if (n < 4) {
122+
return n - 1;
123+
}
124+
if (n % 3 == 0) {
125+
return pow(3, n / 3);
126+
}
127+
if (n % 3 == 1) {
128+
return pow(3, n / 3 - 1) * 4;
129+
}
130+
return pow(3, n / 3) * 2;
131+
}
132+
};
133+
```
134+
88135
### **Go**
89136

90137
```go
@@ -107,6 +154,21 @@ func max(a, b int) int {
107154
}
108155
```
109156

157+
```go
158+
func integerBreak(n int) int {
159+
if n < 4 {
160+
return n - 1
161+
}
162+
if n%3 == 0 {
163+
return int(math.Pow(3, float64(n/3)))
164+
}
165+
if n%3 == 1 {
166+
return int(math.Pow(3, float64(n/3-1))) * 4
167+
}
168+
return int(math.Pow(3, float64(n/3))) * 2
169+
}
170+
```
171+
110172
### **C**
111173

112174
```c
@@ -147,6 +209,22 @@ function integerBreak(n: number): number {
147209
}
148210
```
149211

212+
```ts
213+
function integerBreak(n: number): number {
214+
if (n < 4) {
215+
return n - 1;
216+
}
217+
const m = Math.floor(n / 3);
218+
if (n % 3 == 0) {
219+
return 3 ** m;
220+
}
221+
if (n % 3 == 1) {
222+
return 3 ** (m - 1) * 4;
223+
}
224+
return 3 ** m * 2;
225+
}
226+
```
227+
150228
### **...**
151229

152230
```
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
class Solution {
22
public:
33
int integerBreak(int n) {
4-
vector<int> dp(n + 1);
5-
dp[1] = 1;
6-
for (int i = 2; i <= n; ++i) {
7-
for (int j = 1; j < i; ++j) {
8-
dp[i] = max(max(dp[i], dp[i - j] * j), (i - j) * j);
9-
}
4+
if (n < 4) {
5+
return n - 1;
106
}
11-
return dp[n];
7+
if (n % 3 == 0) {
8+
return pow(3, n / 3);
9+
}
10+
if (n % 3 == 1) {
11+
return pow(3, n / 3 - 1) * 4;
12+
}
13+
return pow(3, n / 3) * 2;
1214
}
1315
};
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
func integerBreak(n int) int {
2-
dp := make([]int, n+1)
3-
dp[1] = 1
4-
for i := 2; i <= n; i++ {
5-
for j := 1; j < i; j++ {
6-
dp[i] = max(max(dp[i], dp[i-j]*j), (i-j)*j)
7-
}
2+
if n < 4 {
3+
return n - 1
84
}
9-
return dp[n]
10-
}
11-
12-
func max(a, b int) int {
13-
if a > b {
14-
return a
5+
if n%3 == 0 {
6+
return int(math.Pow(3, float64(n/3)))
157
}
16-
return b
8+
if n%3 == 1 {
9+
return int(math.Pow(3, float64(n/3-1))) * 4
10+
}
11+
return int(math.Pow(3, float64(n/3))) * 2
1712
}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
class Solution {
22
public int integerBreak(int n) {
3-
int[] dp = new int[n + 1];
4-
dp[1] = 1;
5-
for (int i = 2; i <= n; ++i) {
6-
for (int j = 1; j < i; ++j) {
7-
dp[i] = Math.max(Math.max(dp[i], dp[i - j] * j), (i - j) * j);
8-
}
3+
if (n < 4) {
4+
return n - 1;
95
}
10-
return dp[n];
6+
if (n % 3 == 0) {
7+
return (int) Math.pow(3, n / 3);
8+
}
9+
if (n % 3 == 1) {
10+
return (int) Math.pow(3, n / 3 - 1) * 4;
11+
}
12+
return (int) Math.pow(3, n / 3) * 2;
1113
}
1214
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
class Solution:
22
def integerBreak(self, n: int) -> int:
3-
dp = [1] * (n + 1)
4-
for i in range(2, n + 1):
5-
for j in range(1, i):
6-
dp[i] = max(dp[i], dp[i - j] * j, (i - j) * j)
7-
return dp[n]
3+
if n < 4:
4+
return n - 1
5+
if n % 3 == 0:
6+
return pow(3, n // 3)
7+
if n % 3 == 1:
8+
return pow(3, n // 3 - 1) * 4
9+
return pow(3, n // 3) * 2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
function integerBreak(n: number): number {
2-
let dp = new Array(n + 1).fill(1);
3-
for (let i = 3; i <= n; i++) {
4-
for (let j = 1; j < i; j++) {
5-
dp[i] = Math.max(dp[i], j * (i - j), j * dp[i - j]);
6-
}
2+
if (n < 4) {
3+
return n - 1;
74
}
8-
return dp.pop();
5+
const m = Math.floor(n / 3);
6+
if (n % 3 == 0) {
7+
return 3 ** m;
8+
}
9+
if (n % 3 == 1) {
10+
return 3 ** (m - 1) * 4;
11+
}
12+
return 3 ** m * 2;
913
}

0 commit comments

Comments
 (0)