Skip to content

Commit 34a7cd8

Browse files
committed
feat: add solutions to lc problem: No.0553
No.0553.Optimal Division
1 parent 3794a4b commit 34a7cd8

File tree

6 files changed

+217
-30
lines changed

6 files changed

+217
-30
lines changed

solution/0500-0599/0553.Optimal Division/README.md

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,90 @@
3939

4040
<!-- 这里可写通用的实现逻辑 -->
4141

42+
贪心。
43+
44+
要使得除法的结果最大,分子应该尽可能大,而分母应该尽可能小。
45+
46+
分子最大应该是 nums[0],而分母最大是 `nums[1] / nums[2] / ... / nums[n - 1]`,此时的除法结果最大。
47+
4248
<!-- tabs:start -->
4349

4450
### **Python3**
4551

4652
<!-- 这里可写当前语言的特殊实现逻辑 -->
4753

4854
```python
49-
55+
class Solution:
56+
def optimalDivision(self, nums: List[int]) -> str:
57+
n = len(nums)
58+
if n == 1:
59+
return str(nums[0])
60+
if n == 2:
61+
return f'{nums[0]}/{nums[1]}'
62+
return f'{nums[0]}/({"/".join(map(str, nums[1:]))})'
5063
```
5164

5265
### **Java**
5366

5467
<!-- 这里可写当前语言的特殊实现逻辑 -->
5568

5669
```java
70+
class Solution {
71+
public String optimalDivision(int[] nums) {
72+
int n = nums.length;
73+
if (n == 1) {
74+
return nums[0] + "";
75+
}
76+
if (n == 2) {
77+
return nums[0] + "/" + nums[1];
78+
}
79+
StringBuilder ans = new StringBuilder(nums[0] + "/(");
80+
for (int i = 1; i < n - 1; ++i) {
81+
ans.append(nums[i] + "/");
82+
}
83+
ans.append(nums[n - 1] + ")");
84+
return ans.toString();
85+
}
86+
}
87+
```
88+
89+
### **C++**
90+
91+
```cpp
92+
class Solution {
93+
public:
94+
string optimalDivision(vector<int>& nums) {
95+
int n = nums.size();
96+
if (n == 1) return to_string(nums[0]);
97+
if (n == 2) return to_string(nums[0]) + "/" + to_string(nums[1]);
98+
string ans = to_string(nums[0]) + "/(";
99+
for (int i = 1; i < n - 1; i++) ans.append(to_string(nums[i]) + "/");
100+
ans.append(to_string(nums[n - 1]) + ")");
101+
return ans;
102+
}
103+
};
104+
```
57105
106+
### **Go**
107+
108+
```go
109+
func optimalDivision(nums []int) string {
110+
n := len(nums)
111+
if n == 1 {
112+
return strconv.Itoa(nums[0])
113+
}
114+
if n == 2 {
115+
return fmt.Sprintf("%d/%d", nums[0], nums[1])
116+
}
117+
ans := &strings.Builder{}
118+
ans.WriteString(fmt.Sprintf("%d/(", nums[0]))
119+
for _, num := range nums[1 : n-1] {
120+
ans.WriteString(strconv.Itoa(num))
121+
ans.WriteByte('/')
122+
}
123+
ans.WriteString(fmt.Sprintf("%d)", nums[n-1]))
124+
return ans.String()
125+
}
58126
```
59127

60128
### **...**

solution/0500-0599/0553.Optimal Division/README_EN.md

Lines changed: 94 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,53 +4,56 @@
44

55
## Description
66

7-
<p>Given a list of <b>positive integers</b>, the adjacent integers will perform the float division. For example, [2,3,4] -> 2 / 3 / 4.</p>
7+
<p>You are given an integer array <code>nums</code>. The adjacent integers in <code>nums</code> will perform the float division.</p>
88

9-
<p>However, you can add any number of parenthesis at any position to change the priority of operations. You should find out how to add parenthesis to get the <b>maximum</b> result, and return the corresponding expression in string format. <b>Your expression should NOT contain redundant parenthesis.</b></p>
9+
<ul>
10+
<li>For example, for <code>nums = [2,3,4]</code>, we will evaluate the expression <code>&quot;2/3/4&quot;</code>.</li>
11+
</ul>
1012

11-
<p><b>Example:</b><br />
13+
<p>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.</p>
1214

13-
<pre>
14-
15-
<b>Input:</b> [1000,100,10,2]
15+
<p>Return <em>the corresponding expression that has the maximum value in string format</em>.</p>
1616

17-
<b>Output:</b> "1000/(100/10/2)"
17+
<p><strong>Note:</strong> your expression should not contain redundant parenthesis.</p>
1818

19-
<b>Explanation:</b>
19+
<p>&nbsp;</p>
20+
<p><strong>Example 1:</strong></p>
2021

22+
<pre>
23+
<strong>Input:</strong> nums = [1000,100,10,2]
24+
<strong>Output:</strong> &quot;1000/(100/10/2)&quot;
25+
<strong>Explanation:</strong>
2126
1000/(100/10/2) = 1000/((100/10)/2) = 200
22-
23-
However, the bold parenthesis in "1000/(<b>(</b>100/10<b>)</b>/2)" are redundant, <br/>since they don't influence the operation priority. So you should return "1000/(100/10/2)".
24-
25-
26-
27+
However, the bold parenthesis in &quot;1000/((100/10)/2)&quot; are redundant, since they don&#39;t influence the operation priority. So you should return &quot;1000/(100/10/2)&quot;.
2728
Other cases:
28-
2929
1000/(100/10)/2 = 50
30-
3130
1000/(100/(10/2)) = 50
32-
3331
1000/100/10/2 = 0.5
34-
3532
1000/100/(10/2) = 2
36-
3733
</pre>
3834

39-
</p>
40-
41-
<p><b>Note:</b>
42-
43-
<ol>
35+
<p><strong>Example 2:</strong></p>
4436

45-
<li>The length of the input array is [1, 10].</li>
37+
<pre>
38+
<strong>Input:</strong> nums = [2,3,4]
39+
<strong>Output:</strong> &quot;2/(3/4)&quot;
40+
</pre>
4641

47-
<li>Elements in the given array will be in range [2, 1000].</li>
42+
<p><strong>Example 3:</strong></p>
4843

49-
<li>There is only one optimal division for each test case.</li>
44+
<pre>
45+
<strong>Input:</strong> nums = [2]
46+
<strong>Output:</strong> &quot;2&quot;
47+
</pre>
5048

51-
</ol>
49+
<p>&nbsp;</p>
50+
<p><strong>Constraints:</strong></p>
5251

53-
</p>
52+
<ul>
53+
<li><code>1 &lt;= nums.length &lt;= 10</code></li>
54+
<li><code>2 &lt;= nums[i] &lt;= 1000</code></li>
55+
<li>There is only one optimal division for the given iput.</li>
56+
</ul>
5457

5558
## Solutions
5659

@@ -59,13 +62,75 @@ Other cases:
5962
### **Python3**
6063

6164
```python
62-
65+
class Solution:
66+
def optimalDivision(self, nums: List[int]) -> str:
67+
n = len(nums)
68+
if n == 1:
69+
return str(nums[0])
70+
if n == 2:
71+
return f'{nums[0]}/{nums[1]}'
72+
return f'{nums[0]}/({"/".join(map(str, nums[1:]))})'
6373
```
6474

6575
### **Java**
6676

6777
```java
78+
class Solution {
79+
public String optimalDivision(int[] nums) {
80+
int n = nums.length;
81+
if (n == 1) {
82+
return nums[0] + "";
83+
}
84+
if (n == 2) {
85+
return nums[0] + "/" + nums[1];
86+
}
87+
StringBuilder ans = new StringBuilder(nums[0] + "/(");
88+
for (int i = 1; i < n - 1; ++i) {
89+
ans.append(nums[i] + "/");
90+
}
91+
ans.append(nums[n - 1] + ")");
92+
return ans.toString();
93+
}
94+
}
95+
```
96+
97+
### **C++**
98+
99+
```cpp
100+
class Solution {
101+
public:
102+
string optimalDivision(vector<int>& nums) {
103+
int n = nums.size();
104+
if (n == 1) return to_string(nums[0]);
105+
if (n == 2) return to_string(nums[0]) + "/" + to_string(nums[1]);
106+
string ans = to_string(nums[0]) + "/(";
107+
for (int i = 1; i < n - 1; i++) ans.append(to_string(nums[i]) + "/");
108+
ans.append(to_string(nums[n - 1]) + ")");
109+
return ans;
110+
}
111+
};
112+
```
68113
114+
### **Go**
115+
116+
```go
117+
func optimalDivision(nums []int) string {
118+
n := len(nums)
119+
if n == 1 {
120+
return strconv.Itoa(nums[0])
121+
}
122+
if n == 2 {
123+
return fmt.Sprintf("%d/%d", nums[0], nums[1])
124+
}
125+
ans := &strings.Builder{}
126+
ans.WriteString(fmt.Sprintf("%d/(", nums[0]))
127+
for _, num := range nums[1 : n-1] {
128+
ans.WriteString(strconv.Itoa(num))
129+
ans.WriteByte('/')
130+
}
131+
ans.WriteString(fmt.Sprintf("%d)", nums[n-1]))
132+
return ans.String()
133+
}
69134
```
70135

71136
### **...**
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public:
3+
string optimalDivision(vector<int>& nums) {
4+
int n = nums.size();
5+
if (n == 1) return to_string(nums[0]);
6+
if (n == 2) return to_string(nums[0]) + "/" + to_string(nums[1]);
7+
string ans = to_string(nums[0]) + "/(";
8+
for (int i = 1; i < n - 1; i++) ans.append(to_string(nums[i]) + "/");
9+
ans.append(to_string(nums[n - 1]) + ")");
10+
return ans;
11+
}
12+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
func optimalDivision(nums []int) string {
2+
n := len(nums)
3+
if n == 1 {
4+
return strconv.Itoa(nums[0])
5+
}
6+
if n == 2 {
7+
return fmt.Sprintf("%d/%d", nums[0], nums[1])
8+
}
9+
ans := &strings.Builder{}
10+
ans.WriteString(fmt.Sprintf("%d/(", nums[0]))
11+
for _, num := range nums[1 : n-1] {
12+
ans.WriteString(strconv.Itoa(num))
13+
ans.WriteByte('/')
14+
}
15+
ans.WriteString(fmt.Sprintf("%d)", nums[n-1]))
16+
return ans.String()
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public String optimalDivision(int[] nums) {
3+
int n = nums.length;
4+
if (n == 1) {
5+
return nums[0] + "";
6+
}
7+
if (n == 2) {
8+
return nums[0] + "/" + nums[1];
9+
}
10+
StringBuilder ans = new StringBuilder(nums[0] + "/(");
11+
for (int i = 1; i < n - 1; ++i) {
12+
ans.append(nums[i] + "/");
13+
}
14+
ans.append(nums[n - 1] + ")");
15+
return ans.toString();
16+
}
17+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def optimalDivision(self, nums: List[int]) -> str:
3+
n = len(nums)
4+
if n == 1:
5+
return str(nums[0])
6+
if n == 2:
7+
return f'{nums[0]}/{nums[1]}'
8+
return f'{nums[0]}/({"/".join(map(str, nums[1:]))})'

0 commit comments

Comments
 (0)