Get the Maximum Score LeetCode solution in Python

1
from itertools import accumulate
2

3

4
class Solution:
5
def maxSum(self, nums1: List[int], nums2: List[int]) -> int:
6
acc1 = list(accumulate(nums1, initial=0))
7
acc2 = list(accumulate(nums2, initial=0))
8
i, j = len(nums1) - 1, len(nums2) - 1
9
previ, prevj = len(nums1), len(nums2)
10
prev_maxscore = 0
11
while i >= 0 and j >= 0:
12
while i >= 0 and j >= 0 and nums1[i] < nums2[j]:
13
j -= 1
14
if i >= 0 and j >= 0 and nums1[i] == nums2[j]:
15
prev_maxscore += max(acc1[previ] - acc1[i], acc2[prevj] - acc2[j])
16
previ, prevj = i, j
17
i -= 1
18
j -= 1
19
while i >= 0 and j >= 0 and nums2[j] < nums1[i]:
20
i -= 1
21
if i >= 0 and j >= 0 and nums1[i] == nums2[j]:
22
prev_maxscore += max(acc1[previ] - acc1[i], acc2[prevj] - acc2[j])
23
previ, prevj = i, j
24
i -= 1
25
j -= 1
26
prev_maxscore += max(acc1[previ] - acc1[0], acc2[prevj] - acc2[0])
27
return prev_maxscore % (10**9 + 7)

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0