Skip to content

Commit ff998b5

Browse files
authored
Merge pull request #847 from 0xff-dev/553
Add solution and test-cases for problem 553
2 parents a52b3ea + 83f07dd commit ff998b5

File tree

3 files changed

+89
-23
lines changed

3 files changed

+89
-23
lines changed

leetcode/501-600/0553.Optimal-Division/README.md

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,39 @@
11
# [553.Optimal Division][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given an integer array `nums`. The adjacent integers in `nums` will perform the float division.
5+
6+
- For example, for `nums = [2,3,4]`, we will evaluate the expression `"2/3/4"`.
7+
8+
However, you can add any number of parenthesis at any position to change the priority of operations. You want to add these parentheses such the value of the expression after the evaluation is maximum.
9+
10+
Return the corresponding expression that has the maximum value in string format.
11+
12+
**Note**: your expression should not contain redundant parenthesis.
713

814
**Example 1:**
915

1016
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
17+
Input: nums = [1000,100,10,2]
18+
Output: "1000/(100/10/2)"
19+
Explanation: 1000/(100/10/2) = 1000/((100/10)/2) = 200
20+
However, the bold parenthesis in "1000/((100/10)/2)" are redundant since they do not influence the operation priority.
21+
So you should return "1000/(100/10/2)".
22+
Other cases:
23+
1000/(100/10)/2 = 50
24+
1000/(100/(10/2)) = 50
25+
1000/100/10/2 = 0.5
26+
1000/100/(10/2) = 2
1327
```
1428

15-
## 题意
16-
> ...
17-
18-
## 题解
29+
**Example 2:**
1930

20-
### 思路1
21-
> ...
22-
Optimal Division
23-
```go
2431
```
25-
32+
Input: nums = [2,3,4]
33+
Output: "2/(3/4)"
34+
Explanation: (2/(3/4)) = 8/3 = 2.667
35+
It can be shown that after trying all possibilities, we cannot get an expression with evaluation greater than 2.667
36+
```
2637

2738
## 结语
2839

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,61 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "fmt"
4+
5+
type mm struct {
6+
max, min float32
7+
8+
maxStr, minStr string
9+
}
10+
11+
func Solution(nums []int) string {
12+
if len(nums) == 1 {
13+
return fmt.Sprintf("%d", nums[0])
14+
}
15+
n := len(nums)
16+
dp := make([][]mm, n)
17+
for i := 0; i < n; i++ {
18+
dp[i] = make([]mm, n)
19+
s := fmt.Sprintf("%d", nums[i])
20+
dp[i][i] = mm{max: float32(nums[i]), min: float32(nums[i]), maxStr: s, minStr: s}
21+
}
22+
23+
for l := 2; l <= n; l++ {
24+
for end := l - 1; end < n; end++ {
25+
start := end - l + 1
26+
for pre := end; pre > start; pre-- {
27+
m1 := dp[start][pre-1].max
28+
m2 := dp[pre][end].min
29+
if r := m1 / m2; r > dp[start][end].max {
30+
dp[start][end].max = r
31+
b := dp[pre][end].minStr
32+
if pre != end {
33+
b = "(" + b + ")"
34+
}
35+
dp[start][end].maxStr = dp[start][pre-1].maxStr + "/" + b
36+
}
37+
}
38+
39+
for pre := end; pre > start; pre-- {
40+
m1 := dp[start][pre-1].min
41+
m2 := dp[pre][end].max
42+
if r := m1 / m2; dp[start][end].min == 0 || r < dp[start][end].min {
43+
dp[start][end].min = r
44+
b := dp[pre][end].maxStr
45+
if pre != end {
46+
b = "(" + b + ")"
47+
}
48+
dp[start][end].minStr = dp[start][pre-1].minStr + "/" + b
49+
}
50+
}
51+
}
52+
}
53+
/*
54+
for i := 0; i < n; i++ {
55+
for j := i; j < n; j++ {
56+
fmt.Printf("----- %d - %d, %+v\n", i, j, dp[i][j])
57+
}
58+
}
59+
*/
60+
return dp[0][n-1].maxStr
561
}

leetcode/501-600/0553.Optimal-Division/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
expect string
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{1000, 100, 10, 2}, "1000/(100/10/2)"},
17+
{"TestCase2", []int{2, 3, 4}, "2/(3/4)"},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)