-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLCR132-CuttingBambooII.go
More file actions
65 lines (57 loc) · 1.78 KB
/
LCR132-CuttingBambooII.go
File metadata and controls
65 lines (57 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
// LCR 132. 砍竹子 II
// 现需要将一根长为正整数 bamboo_len 的竹子砍为若干段,每段长度均为 正整数。请返回每段竹子长度的 最大乘积 是多少。
// 答案需要取模 1e9+7(1000000007),如计算初始结果为:1000000008,请返回 1。
// 示例 1:
// 输入:bamboo_len = 12
// 输出:81
// 提示:
// 2 <= bamboo_len <= 1000
import "fmt"
// dp
func cuttingBamboo(bamboo_len int) int {
dp := make([]int, bamboo_len + 1)
dp[0], dp[1] = 1, 1
max := func (x, y int) int { if x > y { return x; }; return y; }
for i := 1; i <= bamboo_len; i++ {
for j := 1; j < i; j++ {
dp[i] = max(dp[i], (j * max(dp[i-j], i-j)) % 1_000_000_007 )
}
}
return dp[bamboo_len]
}
func cuttingBamboo1(bamboo_len int) int {
if bamboo_len <= 3 {
return bamboo_len - 1
}
min := func (x, y int) int { if x < y { return x; }; return y; }
res := 1
for bamboo_len > 0 {
if bamboo_len > 4 {
res = (res * min(bamboo_len, 3)) % 1_000_000_007
bamboo_len -= 3
} else {
res = (res * bamboo_len) % 1_000_000_007
bamboo_len = 0
}
}
return res
}
func main() {
// Example 1:
// Input: n = 2
// Output: 1
// Explanation: 2 = 1 + 1, 1 × 1 = 1.
fmt.Println(cuttingBamboo(2)) // 1
// Example 2:
// Input: n = 10
// Output: 36
// Explanation: 10 = 3 + 3 + 4, 3 × 3 × 4 = 36.
fmt.Println(cuttingBamboo(10)) // 36
fmt.Println(cuttingBamboo(12)) // 81
fmt.Println(cuttingBamboo(120)) // 953271190
fmt.Println(cuttingBamboo1(2)) // 1
fmt.Println(cuttingBamboo1(10)) // 36
fmt.Println(cuttingBamboo1(12)) // 81
fmt.Println(cuttingBamboo1(120)) // 953271190
}