-
Notifications
You must be signed in to change notification settings - Fork 0
/
1770.py
27 lines (23 loc) · 1.05 KB
/
1770.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
class Solution:
def maximumScore(self, nums: List[int], multipliers: List[int]) -> int:
# i, how many multiplications done
# left, how many left operations done (also index for left side)
# (i - left) how many right operations done
# right = n - 1- (i - left), index for right side
memo = {}
def dp(i, left):
if i == len(multipliers):
if (i,left) not in memo:
memo[(i,left)] = 0
return memo[(i,left)]
else:
right = len(nums) - 1 - (i - left)
if (i+1,left+1) not in memo:
memo[(i+1,left+1)] = dp(i+1,left+1)
if (i+1,left) not in memo:
memo[(i+1,left)] = dp(i+1,left)
if (i,left) not in memo:
memo[(i,left)] = max(nums[left]*multipliers[i]+memo[(i+1,left+1)], \
nums[right]*multipliers[i] + memo[(i+1,left)])
return memo[(i,left)]
return dp(0,0)