The Get the Maximum Score coding problem gives you two sorted arrays of distinct integers. You can start traversing from either array. When you encounter an element that exists in both arrays, you are allowed to switch your path to the other array. Your score is the sum of all unique elements you visit. You must find the maximum possible score and return it modulo .
This is a "Hard" difficulty problem asked by Google and Microsoft. It tests a candidate's ability to combine Two Pointers with Dynamic Programming / Greedy logic. It evaluates whether you can process two sequences simultaneously and recognize that "common elements" act as checkpoints where you can choose the optimal path accumulated so far.
This problem is solved using a Two Pointers and Segment Sum approach.
i for nums1 and j for nums2.sum1 and sum2.nums1[i] == nums2[j]), it's a checkpoint.max(sum1, sum2) + common_element to the total score.sum1 and sum2 to 0, advance both pointers, and continue.nums1 = [2, 4, 5, 8, 10], nums2 = [4, 6, 8, 9]
i=0 (2), j=0 (4). 2 < 4, sum1 += 2 = 2. i++.i=1 (4), j=0 (4). Match! Checkpoint at 4.
max(sum1 (2), sum2 (0)) = 2.Total = 2 + 4 = 6.sum1=0, sum2=0. i++, j++.i=2 (5), j=1 (6). 5 < 6, sum1 += 5. i++.i=3 (8), j=2 (8). Match! Checkpoint at 8.
max(sum1 (5), sum2 (6)) = 6.Total = 6 + 6 + 8 = 20.sum1=0, sum2=0. i++, j++.
Result builds up optimally at every intersection.long) for sum1, sum2, and total before applying the modulo at the very end. Applying modulo during the max() comparison will ruin the logic.Treat matching elements in two sorted arrays as "bridges" or "save points." You independently accumulate scores on both roads, and when you reach a bridge, you permanently bank the higher score and start fresh.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Minimum Cost for Cutting Cake I | Medium | Solve | |
| Minimum Number of Taps to Open to Water a Garden | Hard | Solve | |
| Minimum Sideway Jumps | Medium | Solve | |
| Video Stitching | Medium | Solve | |
| Best Time to Buy and Sell Stock with Transaction Fee | Medium | Solve |