← Home

1537. Get the Maximum Score

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
straightforward implementationC++Markdown
153

Let's make this one less mysterious. For 1537. Get the Maximum Score, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

Before optimizing anything, pin down what information is still useful after each move. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: straightforward implementation.

Guide

When?

Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are maxSum.

Guide

Why?

The solution works because it narrows the problem until every update has a clear reason to exist.

  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Initialize the memory or helper structure.
  2. Process candidates in the order the invariant expects.
  3. Update the answer only when the current state is valid.
  4. Return the value that represents the fully processed input.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 244 ms, faster than 100.00% of C++ online submissions for Get the Maximum Score.
02//Memory Usage: 55.8 MB, less than 100.00% of C++ online submissions for Get the Maximum Score.
03class Solution {
04public:
05    int maxSum(vector<int>& nums1, vector<int>& nums2) {
06        int MOD = 1e9+7;
07        int n1 = nums1.size(), n2 = nums2.size();
08        int i = 0, j = 0;
09        
10        vector<pair<int, int>> splits;
11        
12        while(i < n1 && j < n2){
13            if(nums1[i] == nums2[j]){
14                splits.push_back({i, j});
15                ++i;
16                ++j;
17            }else if(nums1[i] < nums2[j]){
18                ++i;
19            }else{
20                ++j;
21            }
22        }
23        
24        splits.push_back({n1-1,n2-1});
25        
26        int ans = 0;
27        int li = -1, lj = -1;
28        for(int k = 0; k < splits.size(); ++k){
29            int i = splits[k].first;
30            int j = splits[k].second;
31            long seg = max(accumulate(nums1.begin()+li+1, nums1.begin()+i+1, 0L),
32                       accumulate(nums2.begin()+lj+1, nums2.begin()+j+1, 0L));
33            ans = (ans + seg%MOD) % MOD;
34            li = i;
35            lj = j;
36        }
37        
38        return ans;
39    }
40};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.