Skip to content

Commit 718e62a

Browse files
committed
add leetcode: offer-14.2
1 parent aa55334 commit 718e62a

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Algorithm/src/offer/_14_2CutRope.java

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package offer;
2+
3+
/**
4+
* @author : CodeWater
5+
* @create :2022-07-24-23:45
6+
* @Function Description :14.2剪绳子
7+
*/
8+
public class _14_2CutRope {
9+
class Solution {
10+
// 三等分最优(贪心)
11+
public int cuttingRope(int n) {
12+
if( n < 4 ) return n - 1;
13+
int p = (int) 1e9 + 7 ;
14+
long res = 1L;
15+
while( n > 4 ){
16+
res = res * 3 % p;
17+
n -= 3;
18+
}
19+
return (int) (res * n % p );
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)