← Home

1499. Max Value of Equation

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
heap / priority queueC++Markdown
149

This problem looks busy at first, but the accepted solution is built around one steady invariant. For 1499. Max Value of Equation, the solution in this repository is mainly a heap / priority queue 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: heap / priority queue.

The notes already sitting in the source point us in the right direction:

  • priority queue
  • https://leetcode.com/problems/max-value-of-equation/discuss/709231/Python-Stack-O(N)

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 findMaxValueOfEquation.

Guide

Why?

The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.

  • The queue gives the solution a level-by-level or frontier-style traversal.
  • The heap keeps the best candidate available without sorting the whole world every time.
  • The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
  • 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. Start from the smallest reliable state.
  2. Expand one legal move at a time.
  3. Cache, count, or merge information as soon as it becomes settled.
  4. Let the final stored value answer the original question.

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//priority queue
02//https://leetcode.com/problems/max-value-of-equation/discuss/709231/Python-Stack-O(N)
03//Runtime: 896 ms, faster than 29.81% of C++ online submissions for Max Value of Equation.
04//Memory Usage: 112.3 MB, less than 100.00% of C++ online submissions for Max Value of Equation.
05class Solution {
06public:
07    int findMaxValueOfEquation(vector<vector<int>>& points, int k) {
08        //(y-x, x)
09        priority_queue<vector<int>, vector<vector<int>>> pq;
10        
11        int ans = INT_MIN;
12        
13        for(vector<int>& point : points){
14            //current point serves as points[j]
15            int x = point[0], y = point[1];
16            
17            /*
18            we only need points[i] s.t. |x_i - x_j| <= k,
19            and because points is already sorted by x,
20            so x_i must <= x_j, the inequality can be expressed as
21            x_j - x_i <= k.
22            we will discard points[i] s.t. x_j - x_i > k
23            */
24            while(!pq.empty() && x - pq.top()[1] > k){
25                pq.pop();
26            }
27            
28            if(!pq.empty()){
29                /*
30                y_i + y_j + |x_i - x_j|
31                = y_i + y_j + x_j - x_i
32                = (y_i - x_i) + (y_j + x_j)
33                
34                in which y_j + x_j is solely determined
35                by current point(it serves as points[j]),
36                so what we want to find is the points[i]
37                s.t. i < j and with max (y_i - x_i)
38                */
39                ans = max(ans, pq.top()[0] + (y + x));
40            }
41            
42            pq.push({y-x, x});
43        }
44        
45        return ans;
46    }
47};
48
49//monotonic deque
50//https://leetcode.com/problems/max-value-of-equation/discuss/709231/Python-Stack-O(N)
51//Runtime: 700 ms, faster than 52.36% of C++ online submissions for Max Value of Equation.
52//Memory Usage: 109.1 MB, less than 100.00% of C++ online submissions for Max Value of Equation.
53class Solution {
54public:
55    int findMaxValueOfEquation(vector<vector<int>>& points, int k) {
56        //(y-x, x)
57        deque<vector<int>> deq;
58        
59        int ans = INT_MIN;
60        
61        for(vector<int>& point : points){
62            int x = point[0], y = point[1];
63            
64            while(!deq.empty() && x - deq.front()[1] > k){
65                deq.pop_front();
66            }
67            
68            if(!deq.empty()){
69                ans = max(ans, deq.front()[0] + (y + x));
70            }
71            
72            /*
73            keep the deque decreasing(fronter element has larger y-x)
74            the element before current point must have larger y-x
75            */
76            while(!deq.empty() && deq.back()[0] <= y - x){
77                deq.pop_back();
78            }
79            
80            deq.push_back({y-x, x});
81        }
82        
83        return ans;
84    }
85};

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.