Stack solution without copying


#1
vector<int> Solution::solve(vector<int> &pos, vector<int> &height) {
    int n = pos.size();
    vector<int> idx(n), res(n);
    iota(idx.begin(), idx.end(), 0);
    sort(idx.begin(), idx.end(), [&pos](int a, int b){return pos[a] < pos[b];});
    stack<int> s;
    for (int i = n - 1; i >= 0; i--) {
        int j = idx[i];
        res[j] = 1;
        while (!s.empty() && pos[s.top()] <= pos[j] + height[j] - 1) {
            res[j] += res[s.top()];
            s.pop();
        }
        s.push(j);
    }
    return res;
}