Skip to content

Commit c69e42f

Browse files
committed
feat: update solutions to lcof problem: No.14
面试题14-I.剪绳子
1 parent 9f562e9 commit c69e42f

File tree

6 files changed

+98
-93
lines changed

6 files changed

+98
-93
lines changed

lcof/面试题14- I. 剪绳子/README.md

+55-46
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,27 @@
22

33
## 题目描述
44

5-
给你一根长度为 `n` 的绳子,请把绳子剪成整数长度的 `m` 段(m、n 都是整数,n>1 并且 m>1),每段绳子的长度记为 `k[0],k[1]...k[m-1]` 。请问 `k[0]*k[1]*...*k[m-1]` 可能的最大乘积是多少?例如,当绳子的长度是 8 时,我们把它剪成长度分别为 2、3、3 的三段,此时得到的最大乘积是 18。
5+
<p>给你一根长度为 <code>n</code> 的绳子,请把绳子剪成整数长度的 <code>m</code> 段(m、n都是整数,n&gt;1并且m&gt;1),每段绳子的长度记为 <code>k[0],k[1]...k[m-1]</code> 。请问 <code>k[0]*k[1]*...*k[m-1]</code> 可能的最大乘积是多少?例如,当绳子的长度是8时,我们把它剪成长度分别为2、3、3的三段,此时得到的最大乘积是18。</p>
66

7-
**示例 1:**
7+
<p><strong>示例 1:</strong></p>
88

9-
```
10-
输入: 2
11-
输出: 1
12-
解释: 2 = 1 + 1, 1 × 1 = 1
13-
```
9+
<pre><strong>输入: </strong>2
10+
<strong>输出: </strong>1
11+
<strong>解释: </strong>2 = 1 + 1, 1 &times; 1 = 1</pre>
1412

15-
**示例  2:**
13+
<p><strong>示例&nbsp;2:</strong></p>
1614

17-
```
18-
输入: 10
19-
输出: 36
20-
解释: 10 = 3 + 3 + 4, 3 × 3 × 4 = 36
21-
```
15+
<pre><strong>输入: </strong>10
16+
<strong>输出: </strong>36
17+
<strong>解释: </strong>10 = 3 + 3 + 4, 3 &times;&nbsp;3 &times;&nbsp;4 = 36</pre>
18+
19+
<p><strong>提示:</strong></p>
2220

23-
**提示:**
21+
<ul>
22+
<li><code>2 &lt;= n &lt;= 58</code></li>
23+
</ul>
2424

25-
- `2 <= n <= 58`
25+
<p>注意:本题与主站 343 题相同:<a href="https://leetcode-cn.com/problems/integer-break/">https://leetcode-cn.com/problems/integer-break/</a></p>
2626

2727
## 解法
2828

@@ -37,28 +37,29 @@ class Solution:
3737
def cuttingRope(self, n: int) -> int:
3838
if n < 4:
3939
return n - 1
40-
res = 1
40+
ans = 1
4141
while n > 4:
42-
res *= 3
42+
ans *= 3
4343
n -= 3
44-
if n == 4:
45-
return res << 2
46-
return res * n
44+
ans *= n
45+
return ans
4746
```
4847

4948
### **Java**
5049

5150
```java
5251
class Solution {
5352
public int cuttingRope(int n) {
54-
if (n < 4) return n - 1;
55-
int res = 1;
53+
if (n < 4) {
54+
return n - 1;
55+
}
56+
int ans = 1;
5657
while (n > 4) {
57-
res *= 3;
58+
ans *= 3;
5859
n -= 3;
5960
}
60-
if (n == 4) return res << 2;
61-
return res * n;
61+
ans *= n;
62+
return ans;
6263
}
6364
}
6465
```
@@ -71,40 +72,31 @@ class Solution {
7172
* @return {number}
7273
*/
7374
var cuttingRope = function (n) {
74-
// 数学方法
75-
if (n <= 3) return n - 1;
76-
let a = ~~(n / 3);
77-
let b = n % 3;
78-
if (b === 1) {
79-
return 3 ** (a - 1) * 2 * 2;
75+
if (n < 4) return n - 1;
76+
let ans = 1;
77+
while (n > 4) {
78+
ans *= 3;
79+
n -= 3;
8080
}
81-
if (b === 0) return 3 ** a;
82-
return 3 ** a * b;
83-
// dp 方法
84-
// let dp = new Array(n+1).fill(0)
85-
// dp[0] = 1
86-
// for(let i=1;i<n;i++) {
87-
// for(let j=i;j<=n;j++) {
88-
// dp[j] = Math.max(dp[j],dp[j-i] * i)
89-
// }
90-
// }
91-
// return dp[n]
81+
ans *= n;
82+
return ans;
9283
};
9384
```
9485

9586
### **Go**
9687

9788
```go
9889
func cuttingRope(n int) int {
99-
if n <= 3 {
90+
if n < 4 {
10091
return n - 1
10192
}
102-
sum := 1
93+
ans := 1
10394
for n > 4 {
104-
sum *= 3
95+
ans *= 3
10596
n -= 3
10697
}
107-
return sum * n
98+
ans *= n
99+
return ans
108100
}
109101
```
110102

@@ -126,6 +118,23 @@ public:
126118
};
127119
```
128120
121+
```cpp
122+
class Solution {
123+
public:
124+
int cuttingRope(int n) {
125+
if (n < 4) return n - 1;
126+
int ans = 1;
127+
while (n > 4)
128+
{
129+
ans *= 3;
130+
n -= 3;
131+
}
132+
ans *= n;
133+
return ans;
134+
}
135+
};
136+
```
137+
129138
### **...**
130139

131140
```
+5-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
func cuttingRope(n int) int {
2-
if n <= 3 {
2+
if n < 4 {
33
return n - 1
44
}
5-
sum := 1
5+
ans := 1
66
for n > 4 {
7-
sum *= 3
7+
ans *= 3
88
n -= 3
99
}
10-
return sum * n
10+
ans *= n
11+
return ans
1112
}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
class Solution {
22
public int cuttingRope(int n) {
3-
if (n < 4) return n - 1;
4-
int res = 1;
3+
if (n < 4) {
4+
return n - 1;
5+
}
6+
int ans = 1;
57
while (n > 4) {
6-
res *= 3;
8+
ans *= 3;
79
n -= 3;
810
}
9-
if (n == 4) return res << 2;
10-
return res * n;
11+
ans *= n;
12+
return ans;
1113
}
1214
}

lcof/面试题14- I. 剪绳子/Solution.js

+7-17
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,12 @@
33
* @return {number}
44
*/
55
var cuttingRope = function (n) {
6-
// 数学方法
7-
if (n <= 3) return n - 1;
8-
let a = ~~(n / 3);
9-
let b = n % 3;
10-
if (b === 1) {
11-
return 3 ** (a - 1) * 2 * 2;
6+
if (n < 4) return n - 1;
7+
let ans = 1;
8+
while (n > 4) {
9+
ans *= 3;
10+
n -= 3;
1211
}
13-
if (b === 0) return 3 ** a;
14-
return 3 ** a * b;
15-
// dp 方法
16-
// let dp = new Array(n+1).fill(0)
17-
// dp[0] = 1
18-
// for(let i=1;i<n;i++) {
19-
// for(let j=i;j<=n;j++) {
20-
// dp[j] = Math.max(dp[j],dp[j-i] * i)
21-
// }
22-
// }
23-
// return dp[n]
12+
ans *= n;
13+
return ans;
2414
};

lcof/面试题14- I. 剪绳子/Solution.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ class Solution:
22
def cuttingRope(self, n: int) -> int:
33
if n < 4:
44
return n - 1
5-
res = 1
5+
ans = 1
66
while n > 4:
7-
res *= 3
7+
ans *= 3
88
n -= 3
9-
if n == 4:
10-
return res << 2
11-
return res * n
9+
ans *= n
10+
return ans

lcof/面试题14- II. 剪绳子 II/README.md

+20-16
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,33 @@
22

33
## 题目描述
44

5-
给你一根长度为 `n` 的绳子,请把绳子剪成整数长度的 `m` 段(m、n 都是整数,n>1 并且 m>1),每段绳子的长度记为 `k[0],k[1]...k[m-1]` 。请问 `k[0]*k[1]*...*k[m-1]` 可能的最大乘积是多少?例如,当绳子的长度是 8 时,我们把它剪成长度分别为 2、3、3 的三段,此时得到的最大乘积是 18。
5+
<p>给你一根长度为 <code>n</code> 的绳子,请把绳子剪成整数长度的 <code>m</code>&nbsp;段(m、n都是整数,n&gt;1并且m&gt;1),每段绳子的长度记为 <code>k[0],k[1]...k[m - 1]</code> 。请问 <code>k[0]*k[1]*...*k[m - 1]</code> 可能的最大乘积是多少?例如,当绳子的长度是8时,我们把它剪成长度分别为2、3、3的三段,此时得到的最大乘积是18。</p>
66

7-
答案需要取模 1e9+7(1000000007),如计算初始结果为:1000000008,请返回 1。
7+
<p>答案需要取模 1e9+7(1000000007),如计算初始结果为:1000000008,请返回 1。</p>
88

9-
**示例 1:**
9+
<p>&nbsp;</p>
1010

11-
```
12-
输入: 2
13-
输出: 1
14-
解释: 2 = 1 + 1, 1 × 1 = 1
15-
```
11+
<p><strong>示例 1:</strong></p>
1612

17-
**示例  2:**
13+
<pre><strong>输入: </strong>2
14+
<strong>输出: </strong>1
15+
<strong>解释: </strong>2 = 1 + 1, 1 &times; 1 = 1</pre>
1816

19-
```
20-
输入: 10
21-
输出: 36
22-
解释: 10 = 3 + 3 + 4, 3 × 3 × 4 = 36
23-
```
17+
<p><strong>示例&nbsp;2:</strong></p>
18+
19+
<pre><strong>输入: </strong>10
20+
<strong>输出: </strong>36
21+
<strong>解释: </strong>10 = 3 + 3 + 4, 3 &times;&nbsp;3 &times;&nbsp;4 = 36</pre>
22+
23+
<p>&nbsp;</p>
24+
25+
<p><strong>提示:</strong></p>
2426

25-
**提示:**
27+
<ul>
28+
<li><code>2 &lt;= n &lt;= 1000</code></li>
29+
</ul>
2630

27-
- `2 <= n <= 1000`
31+
<p>注意:本题与主站 343 题相同:<a href="https://leetcode-cn.com/problems/integer-break/">https://leetcode-cn.com/problems/integer-break/</a></p>
2832

2933
## 解法
3034

0 commit comments

Comments
 (0)