An interesting problem! But isn't very easy at first glance. You need to think very clear on how would a keypoint be generated. Since I only learn from others ' solutions and am still unable to give my personal explanations, I would suggest the Se solutions:c++ priority_queue solution, Python HEAPQ solution. Of course, there is still a divide-and-conquer solution on geeksforgeeks.com, which are much more difficult to understand: -)
Well, I just rewrite the code in the first solution as follows, by giving those variables more meaning names to help under Stand it.
1 classSolution {2 Public:3vector<pair<int,int>> Getskyline (vector<vector<int>>&buildings) {4 intIDX =0, start, height, n =buildings.size ();5vector<pair<int,int> >Skyline;6priority_queue<pair<int,int> >livebuildings;7 while(IDX < n | |!)Livebuildings.empty ()) {8 if(Livebuildings.empty () | | | IDX < n && buildings[idx][0] <=Livebuildings.top (). Second) {9Start = buildings[idx][0];Ten while(IDX < n && buildings[idx][0] ==start) { OneLivebuildings.push (Make_pair (buildings[idx][2], buildings[idx][1])); Aidx++; - } - } the Else { -Start =Livebuildings.top (). Second; - while(!livebuildings.empty () && livebuildings.top (). Second <=start) - Livebuildings.pop (); + } -Height = Livebuildings.empty ()?0: Livebuildings.top (). First; + if(Skyline.empty () | | skyline.back (). Second! =height) A Skyline.push_back (Make_pair (start, height)); at } - returnSkyline; - } -};
[Leetcode] The Skyline problem