[Leetcode] The Skyline problem

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.