Leetcode Problem 1537. Get the Maximum Score

1537. Get the Maximum Score

Leetcode Solutions

Two Pointers Approach

  1. Initialize two pointers i and j to point to the start of nums1 and nums2 respectively.
  2. Initialize two variables sum1 and sum2 to keep track of the sum of elements traversed in nums1 and nums2 respectively.
  3. Initialize a variable result to store the maximum score.
  4. While both pointers i and j have not reached the end of their respective arrays: a. If nums1[i] < nums2[j], add nums1[i] to sum1 and increment i. b. If nums1[i] > nums2[j], add nums2[j] to sum2 and increment j. c. If nums1[i] == nums2[j], add the maximum of sum1 and sum2 plus nums1[i] to result, reset sum1 and sum2 to 0, and increment both i and j.
  5. After the loop, add any remaining elements in nums1 to sum1 and in nums2 to sum2.
  6. Add the maximum of sum1 and sum2 to result.
  7. Return result modulo 10^9 + 7.
UML Thumbnail

Dynamic Programming with Memoization

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...