clueless coding // TODO: be smarter

LeetCode Find Right Interval: CPP Solution



TL;DR Code Solution


vector<int> findRightInterval(vector<Interval>& intervals) {
    map<int, int> hash;
    vector<int> res;
    int n = intervals.size();
    for (int i = 0; i < n; ++i)
        hash[intervals[i].start] = i;
    for (auto in : intervals) {
        auto itr = hash.lower_bound(in.end);
        if (itr == hash.end()) res.push_back(-1);
        else res.push_back(itr->second);
    }
    return res;
}