← Home

436. Find Right Interval

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
greedyC++Markdown
436

Let's make this one less mysterious. For 436. Find Right Interval, the solution in this repository is mainly a greedy 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: greedy.

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

  • lower_bound

Guide

When?

Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are findRightInterval.

Guide

Why?

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

  • Sorting is used to make local choices comparable, so the later scan does not have to rediscover order.
  • A map keeps the lookup side cheap; the code pays a little memory to avoid repeated searching.
  • 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. 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//lower_bound
02//Runtime: 236 ms, faster than 19.59% of C++ online submissions for Find Right Interval.
03//Memory Usage: 29.9 MB, less than 20.00% of C++ online submissions for Find Right Interval.
04class Solution {
05public:
06    vector<int> findRightInterval(vector<vector<int>>& intervals) {
07        int n = intervals.size();
08        vector<vector<int>> si(n); //(start point, index)
09        for(int i = 0; i < intervals.size(); i++){
10            si[i] = {intervals[i][0], i};
11        }
12        
13        sort(si.begin(), si.end());
14        
15        vector<int> ans(n);
16        
17        for(int i = 0; i < n; i++){
18            /*
19            dummy variable just used for binary search
20            set dummy variable's starting point as intervals[i]'s end point, 
21            just for comparison purpose
22            */
23            vector<int> dummy = {intervals[i][1], -1};
24            //find interval whose starting point <= current end point
25            auto it = lower_bound(si.begin(), si.end(), dummy,
26                [](const vector<int>& v1, const vector<int>& v2){
27                    return v1[0] < v2[0];
28                    });
29            if(it == si.end()){
30                ans[i] = -1;
31            }else{
32                ans[i] = (*it)[1];
33            }
34        }
35        
36        return ans;
37    }
38};
39
40//map::lower_bound
41//https://leetcode.com/problems/find-right-interval/discuss/91819/C%2B%2B-map-solution
42//Runtime: 160 ms, faster than 40.67% of C++ online submissions for Find Right Interval.
43//Memory Usage: 27.8 MB, less than 20.00% of C++ online submissions for Find Right Interval.
44class Solution {
45public:
46    vector<int> findRightInterval(vector<vector<int>>& intervals) {
47        int n = intervals.size();
48        map<int, int> si; //(start point, index)
49        for(int i = 0; i < intervals.size(); i++){
50            si[intervals[i][0]] = i;
51        }
52        
53        vector<int> ans(n);
54        
55        for(int i = 0; i < n; i++){
56            auto it = si.lower_bound(intervals[i][1]);
57            if(it == si.end()){
58                ans[i] = -1;
59            }else{
60                ans[i] = it->second;
61            }
62        }
63        
64        return ans;
65    }
66};

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.