The Skyline Problem

// 超时
class Solution {
public:
    vector<pair<int, int> > getSkyline(vector<vector<int>>& buildings) {
        vector<pair<int, int> > r;
        if (buildings.size() == 0) {
            return r;
        }
        r.push_back(make_pair(buildings[0][0], buildings[0][2]));
        set<int> tmp;
        for (int i = 0; i < buildings.size(); ++i) {
            tmp.insert(buildings[i][0]);
            tmp.insert(buildings[i][1]);
        }
        set<int>::iterator a = tmp.begin();
        set<int>::iterator b = ++a;
        for (; b != tmp.end(); ++b, ++a) {
            int max = 0;
            for (int i = 0; i < buildings.size(); ++i) {
                if (buildings[i][1] < *a) {
                    continue;
                }
                if (buildings[i][0] > *b) {
                    break;
                }
                max = max > buildings[i][2] ? max : buildings[i][2];
            }
            if (max != r.back().second) {
                r.push_back(make_pair(*b, max));
            }
        }
        return r;
    }
};

你可能感兴趣的:(The Skyline Problem)