↓Skip to main content
  1. LeetCode/

LeetCode 1537: Get the Maximum Score

·3 mins· ·
LeetCode Hard Dynamic-Programming Two-Pointers Greedy Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: hard First Attempt: 2026-02-01

  • Submissions: WA ×2 → TLE ×2 → AC

Approach
#

Both arrays are sorted with no repeats inside each. Switching to the other array is allowed only at values present in both.

Walk forward with two pointers as in a merge, keeping running sums sum1, sum2:

  • nums1[i] < nums2[j]: only path 1 passes it, sum1 += nums1[i].
  • nums1[i] > nums2[j]: sum2 += nums2[j].
  • Equal: a meeting point; up to here either path can take the better route, so sum1 = sum2 = max(sum1, sum2) + value.

The answer is max(sum1, sum2) with the modulo. Apply the modulo only at the end; reducing midway breaks the comparisons.

My Attempts
#

VersionProblem
Attempt 1, WAFound meeting points with x not in nums2 and forgot the modulo, so large answers were wrong
Attempt 2, WAAdded the modulo as % 10 ** 9 + 7, which evaluates as (x % 10**9) + 7. For nums1 = [3], nums2 = [7], the answer is 7; it returned 14
Two TLEsFixed the parentheses, but x not in nums2 searches a list, O(m) each time, O(nm) overall
ACSwitched to a two-pointer merge, where comparing both sides reveals meeting points

Solution
#

class Solution:
    def maxSum(self, nums1: List[int], nums2: List[int]) -> int:
        MOD = 10 ** 9 + 7
        i = j = 0
        sum1 = sum2 = 0
        n, m = len(nums1), len(nums2)
        while i < n or j < m:
            if j == m or (i < n and nums1[i] < nums2[j]):
                sum1 += nums1[i]
                i += 1
            elif i == n or nums1[i] > nums2[j]:
                sum2 += nums2[j]
                j += 1
            else:                                  # meeting point: take the better path
                sum1 = sum2 = max(sum1, sum2) + nums1[i]
                i += 1
                j += 1
        return max(sum1, sum2) % MOD               # modulo only at the end

Complexity
#

Time O(n + m), space O(1).

Mistakes to Avoid
#

  • % 10 ** 9 + 7 needs parentheses: % (10 ** 9 + 7).
  • in on a list is O(n); the arrays are sorted, so compare with two pointers.
  • No modulo midway: reduced values cannot be compared.

Interview Explanation
#

Both arrays are sorted and switching happens only at shared values. I merge with two pointers, keeping a running sum per path; at a shared value I take the larger of the two sums so far and set both to it plus the shared value. The answer is the larger sum with the modulo applied only at the end. Time O(n + m).

Related

LeetCode 1793: Maximum Score of a Good Subarray
·3 mins
LeetCode Hard Two-Pointers Greedy Binary-Search Array Monotonic-Stack
LeetCode 1793 note: expand from k toward the larger neighbor each step so the minimum stays as large as possible for each length; at a boundary, only the other side can grow
LeetCode 42: Trapping Rain Water
·2 mins
LeetCode Hard Array Two-Pointers Dynamic-Programming Stack
LeetCode 42 note: each cell holds min(tallest on the left, tallest on the right) - its own height; use prefix and suffix maxima or two pointers
LeetCode 1989: Maximum Number of People That Can Be Caught in Tag
·3 mins
LeetCode Medium Two-Pointers Greedy Array
LeetCode 1989 note: process the “it” players left to right, each catching the leftmost uncaught person in range, with a right-only pointer to the next candidate
LeetCode 2122: Recover the Original Array
·3 mins
LeetCode Hard Two-Pointers Sorting Hash-Table Enumeration Array
LeetCode 2122 note: the smallest number is always in lower, so enumerating its partner fixes k; after sorting, two pointers pair each smallest unmatched number with the number 2k above it
LeetCode 948: Bag of Tokens
·2 mins
LeetCode Medium Two-Pointers Greedy Sorting Array
LeetCode 948 note: after sorting, buy score with the cheapest token and buy power with the most expensive one; trade for power only when score > 0, and stop when neither move works
LeetCode 1458: Max Dot Product of Two Subsequences
·2 mins
LeetCode Hard Dynamic-Programming Longest-Common-Subsequence Array
LeetCode 1458 note: a two-prefix DP; dp[i][j] either pairs nums1[i] with nums2[j] (extending dp[i-1][j-1] or starting fresh) or skips one of them