Reputation: 21
Hi i have following problem which i want to implement:
I have two arrays n integers array and n - 1 operations array
integers[n]
// n integersoperations[n - 1]
// n - 1 operations (eg. + - * /)numbers array is fixed while the operator array can be used in any permutation in order to get the maximum value (Thank you @grek40)
example 1:
given array of integers: 1 2 7 5 1 2
and array of operations: + + + + *
I want to find maximum adjacent product sum subtract division i.e. 1+2+7*5+1+2 = 41
example 2:
given array of integers: 1 3 5 6 9 10 12 14
and array of operations: + + * * - / /
I want to find maximum adjacent product sum subtract division i.e. 1-3/5/6+9+10*12*14 = 1689.9
I am beginner in dynamic programming. I am unable to figure out the recurrence relation for the following problem.
Can anyone please suggest something?
thanks!
I try to use Enumeration methods, Time complexity is n!
for operators in allOperators:
op = list(operators)
op.append(' ')
operation = ""
for i, number in enumerate(inputNumbers):
count += 1
operation += number + op[i]
output = eval(operation)
if output > maxOutput:
maxOutput = output
Upvotes: 2
Views: 1178
Reputation: 167
You can follow similar approach as this question :
Given a list of integer numbers, a list of symbols [+,-,*,/] and a target number N ,
Only difference is it talks about matching the number N, You can find Maximum sum.
The idea here is to generate combinations of the operators as long as the length of the array.
map<operator, count>
Getresult(expression)
std::next_permutation:
current_max
, and expression_max
for max.current_max
and expression_max
Upvotes: 0
Reputation: 3037
Code will better than word for this problem.The state transition equation is clear in the code.
import functools
test1 = (1, 2, 7, 5, 1, 2)
test2 = (1, 3, 5, 6 ,9 ,10, 12, 14)
@functools.lru_cache()
def BaseSolution(nums, add, sub, mul, div, length):
if add<0 or sub<0 or mul<0 or div<0 or length<0:
return float("-inf")
if length == 0:
return nums[length]
else:
return max(
BaseSolution(nums, add-1, sub, mul, div, length-1) + nums[length],
BaseSolution(nums, add, sub-1, mul, div, length-1) - nums[length],
MulOrDivSolution(nums, add, sub, mul-1, div, length-1, '*'+str(nums[length])),
MulOrDivSolution(nums, add, sub, mul, div-1, length-1, '/'+str(nums[length])),
)
def MulOrDivSolution(nums, add, sub, mul, div, length, lzayVal):
if add<0 or sub<0 or mul<0 or div<0 or length<0:
return float("-inf")
if length == 0:
return eval( str(nums[length])+lzayVal)
else:
return max(
BaseSolution(nums, add-1, sub, mul, div, length-1) + eval( str(nums[length])+lzayVal),
BaseSolution(nums, add, sub-1, mul, div, length-1) - eval( str(nums[length])+lzayVal),
MulOrDivSolution(nums, add, sub, mul-1, div, length-1, '*'+str(nums[length])+lzayVal),
MulOrDivSolution(nums, add, sub, mul, div-1, length-1, '/'+str(nums[length])+lzayVal),
)
if __name__ == '__main__':
print(BaseSolution(test1,4,0,0,1,5))
print(BaseSolution(test2,2,1,2,2,7))
Upvotes: 1