-
Notifications
You must be signed in to change notification settings - Fork 43
/
optimal-division.py
201 lines (176 loc) · 5.71 KB
/
optimal-division.py
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
"""
553. Optimal Division
Medium
You are given an integer array nums. The adjacent integers in nums will perform the float division.
For example, for nums = [2,3,4], we will evaluate the expression "2/3/4".
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.
Return the corresponding expression that has the maximum value in string format.
Note: your expression should not contain redundant parenthesis.
Example 1:
Input: nums = [1000,100,10,2]
Output: "1000/(100/10/2)"
Explanation:
1000/(100/10/2) = 1000/((100/10)/2) = 200
However, the bold parenthesis in "1000/((100/10)/2)" are redundant, since they don't influence the operation priority. So you should return "1000/(100/10/2)".
Other cases:
1000/(100/10)/2 = 50
1000/(100/(10/2)) = 50
1000/100/10/2 = 0.5
1000/100/(10/2) = 2
Example 2:
Input: nums = [2,3,4]
Output: "2/(3/4)"
Example 3:
Input: nums = [2]
Output: "2"
Constraints:
1 <= nums.length <= 10
2 <= nums[i] <= 1000
There is only one optimal division for the given iput.
"""
# V0
# IDEA : MATH
class Solution(object):
def optimalDivision(self, nums):
nums = [ str(x) for x in nums ]
if len(nums) <= 2:
return '/'.join(nums)
return str(nums[0]) + "/" + "(" + "/".join( str(i) for i in nums[1:]) + ")"
# V0'
# IDEA : MATH
class Solution(object):
def optimalDivision(self, nums):
nums = [ str(x) for x in nums ]
if len(nums) <= 2:
return '/'.join(nums)
return '{}/({})'.format(nums[0], '/'.join(nums[1:]))
# V0''
# IDEA : string op
# IDEA : only 1st "/" need to be "/",
# -> other "/" can be transformed into "*"
class Solution(object):
def optimalDivision(self, nums):
# edge case
if not nums:
return ""
if len(nums) == 1:
return str(nums[0])
if len(nums) == 2:
return "{}/{}".format(nums[0], nums[1])
res = ""
pres = str(nums[0]) + "/"
_nums = nums[1:]
for i in range(len(_nums)):
tmp = _nums[i]
if i == 0:
res += str(tmp)
else:
res += ("/" + str(tmp))
_res = pres + "(" + res + ")"
return _res
# V0'''
class Solution(object):
def optimalDivision(self, nums):
"""
:type nums: List[int]
:rtype: str
"""
if len(nums) == 1:
return str(nums[0])
if len(nums) == 2:
return str(nums[0]) + "/" + str(nums[1])
result = [str(nums[0]) + "/(" + str(nums[1])]
for i in range(2, len(nums)):
result += "/" + str(nums[i])
result += ")"
return "".join(result)
# V1
# https://blog.csdn.net/fuxuemingzhu/article/details/79403723
# ideas:
# case 1 : return nums if there is only one number
# case 2 : return nums[0]/nums[1] if there are only 2 numbers
# case 3 : return nums[0]/(nums[1]*nums[2]*....*nums[N]) if there are N numbers
class Solution(object):
def optimalDivision(self, nums):
"""
:type nums: List[int]
:rtype: str
"""
nums = list(map(str, nums)) # adapt here for python 3
if len(nums) <= 2:
return '/'.join(nums)
return '{}/({})'.format(nums[0], '/'.join(nums[1:]))
### Test case :
s=Solution()
assert s.optimalDivision([1000,100,10,2]) == "1000/(100/10/2)"
assert s.optimalDivision([1000]) == "1000"
assert s.optimalDivision([1000, 10]) == "1000/10"
assert s.optimalDivision([]) == ""
assert s.optimalDivision([1000,100, 99,98, 97, 96]) == '1000/(100/99/98/97/96)'
assert s.optimalDivision([ _ for _ in range(50, 40, -1)]) == '50/(49/48/47/46/45/44/43/42/41)'
# V1'
# http://bookshadow.com/weblog/2017/04/16/leetcode-optimal-division/
# IDEA : MATH
class Solution(object):
def optimalDivision(self, nums):
"""
:type nums: List[int]
:rtype: str
"""
nums = map(str, nums)
return len(nums) < 3 and '/'.join(nums) or nums[0] + '/(' + '/'.join(nums[1:]) + ')'
# V1''
# http://bookshadow.com/weblog/2017/04/16/leetcode-optimal-division/
# IDEA : BRUTE FORCE
class Solution(object):
def optimalDivision(self, nums):
"""
:type nums: List[int]
:rtype: str
"""
def solve(snums):
if len(snums) == 1: yield snums[0]
for x in range(1, len(snums)):
left, right = snums[:x], snums[x:]
l = '/'.join(left)
if len(right) == 1:
yield l + '/' + right[0]
else:
for r in solve(right):
yield l + '/(' + r + ')'
ans, best = '', 0
for expr in solve(map(str, nums)):
val = eval(expr.replace('/', '.0/'))
if val > best: ans, best = expr, val
return ans
# V1'''
# https://blog.csdn.net/orientliu96/article/details/104304944
class Solution(object):
def optimalDivision(self, nums):
"""
:type nums: List[int]
:rtype: str
"""
nums = list(map(str, nums))
if len(nums) > 2:
nums[1] = "(" + nums[1]
nums[-1] = nums[-1] + ")"
return "/".join(nums)
# V2
# Time: O(n)
# Space: O(1)
class Solution(object):
def optimalDivision(self, nums):
"""
:type nums: List[int]
:rtype: str
"""
if len(nums) == 1:
return str(nums[0])
if len(nums) == 2:
return str(nums[0]) + "/" + str(nums[1])
result = [str(nums[0]) + "/(" + str(nums[1])]
for i in range(2, len(nums)):
result += "/" + str(nums[i])
result += ")"
return "".join(result)