Skyline Problem

Поделиться
HTML-код
  • Опубликовано: 7 фев 2025
  • / tusharroy25
    github.com/mis...
    github.com/mis...
    www.educative....
    You are given a set of n rectangles in no particular order. They have varying widths and heights, but their bottom edges are collinear, so that they look like buildings on a skyline. For each rectangle, you’re given the x position of the left edge, the x position of the right edge, and the height. Your task is to draw an outline around the set of rectangles so that you can see what the skyline would look like when silhouetted at night.

Комментарии • 194

  • @ChirangiviBhat-i2g
    @ChirangiviBhat-i2g Год назад +6

    I understood the effort you put in the video when you said in 5:57: "At point thhwu before the shthhart of the building, the maximum value was thhfree"

  • @usrenaem
    @usrenaem 2 года назад +1

    When I saw this task for the first time I have absolutely no idea how to solve it, no technics/algorithms I knew including priority queue looked like a possible solution. Thanks a lot for that clear explanation!

  • @hunarjain4867
    @hunarjain4867 4 года назад +25

    I am here because of LeetCode Daily Challenge and Tushar never fails to deliver..😄🙌🏿🙌🏿🙌🏿

  • @shrn
    @shrn Год назад

    I've looked at 5-6 tutorials but did not understand what to do, but after watching this video, it became very simple

  • @ranga400
    @ranga400 4 года назад +4

    Just Brilliant. Appreciate your patience in going through the code line by line and using the visual to make things clear. Hats off

  • @a1988ditya
    @a1988ditya 5 лет назад +9

    First of all , would like to say - top class videos and explanation. Really helpful thank u.
    1) You dont have to delete items and PQ does not support Log(n) deletes
    2) i would keep a heap of objects rather than height alone , as it takes care of case where multiple buildings have same height. Coming to delete , you can ignore deletion - when you do pop_heap you can figure out if that building is stale or not by checking if its x end position is less that the current x position. If its stale just pop it out
    3) This is just a line sweep algo application problem

  • @SunilPatel-sf5ng
    @SunilPatel-sf5ng 3 года назад +1

    Thank you so much sir. I've a got a job because of you. Please continue to deliver awesome algorithms and techniques.

  • @123chen9
    @123chen9 3 года назад

    I have to say this is the simplest explaination I have ever seen! Thank you

  • @casperauto
    @casperauto 6 лет назад +7

    Cannot be more clear! It was a lot of work to make this. Thank you.

  • @ArijitDebYoutubeChannel
    @ArijitDebYoutubeChannel 4 года назад +12

    Great explanation! Also, using TreeMap instead of PriorityQueue to improve the remove(Object) operation time complexity from O(N) to O(logN) is a brilliant idea.

  • @BackToBackSWE
    @BackToBackSWE 5 лет назад +3

    You are the og whiteboard youtuber

  • @pradeepbalasundaram
    @pradeepbalasundaram 4 года назад +10

    Hi Tushar, great videos. Learned a lot. For your future videos I was hoping if you could talk a few minutes about how to develop an intuition in to solving problems. For example, at first the skyline problems seems almost insurmountable even as I think about it in the leisure of my living room. In a pressure cooker situation that is the interview, all my prefrontal functions seem to come to a standstill and there is no hope of even remotely coming close to an approach , let alone write the code for it. It would be of great if you can break down the process of developing an intuition for problems like these. Like for example. How do I solve it by brute force, is there a key insight that can help me solve it better. Why some approaches will work for a certain type of problem and why others wont. How to quickly come up with a subset of approaches that might work. How not to get overwhelmed by the question. For example, the skyline problem can be solved by a heap, or by divide and conquer. Now, how do I come up with either of these approches as if I had never seen this problem before ? - Teach a man to fish and he will eat for a lifetime

    • @Bswarup
      @Bswarup Год назад

      I totally agree...We can start with Brute force approach first and then can come to optimized sol..

  • @Jeremy0Sea
    @Jeremy0Sea 8 лет назад

    This is the best explanation on skyline problem I have ever seen so far.
    Great Job Tushar!

  • @dargy2368
    @dargy2368 5 лет назад +3

    You literally saved my life thank you so much. (I understand the homework thanks to you 4 hour before due date).

  • @Tejas8272
    @Tejas8272 2 года назад

    Finally able to solve with your explanation after almost 2 days searching debugging others solution /code
    Thank you👍🏻

  • @snehalbedmutha1895
    @snehalbedmutha1895 4 года назад +1

    Thank you for this excellent explanation. I wrote your code in Python and I'm getting TLE for one test case (69/70 passed).

  • @Young10489
    @Young10489 3 года назад +1

    Tushar has become my favorite Indian .

  • @syafzal273
    @syafzal273 6 лет назад +3

    Great video. I like that you went over the algorithm and why it works unlike some of the DP videos where the answer is explained but now how it was arrived at​. Also, love that you have python code!

  • @rahul38474
    @rahul38474 3 года назад

    I got this question in a final round for an internship and even though I barely understood the question I still somehow managed to come up with this solution, I didn’t even understand the answer I was giving the interviewer.

  • @sidharthbihary2475
    @sidharthbihary2475 6 лет назад +1

    beautifully explained. thanks Tushar. the best part of problem solving is you discuss the idea of the solution before jumping directly into the algorithm part.

  • @saurabhdsawant
    @saurabhdsawant 8 лет назад +16

    Very useful! Great efforts. Appreciate it Tushar Roy. Thank you for uploading.
    The code part helps a lot also would be perfect if we can just discuss naive approaches for the problems at the start. Thanks again.

  • @larry1285
    @larry1285 5 лет назад +1

    nice explanation, the only one I can understand among the tutorials I have found so far

    • @larry1285
      @larry1285 5 лет назад +1

      I cannot find a built-in method to delete a specific item from priority queue in python. Could you tell me what should I do to solve the problem using the method taught in the video? Thank you so much

    • @larry1285
      @larry1285 5 лет назад +1

      since your python version does not pass the test 35 either, I know you use dict and use max(queue.keys()) to get the maximum

  • @josebenardi1554
    @josebenardi1554 7 лет назад +1

    Your videos were really helpful to me during this last semester, thank you for making this available.

  • @chenhaofeng4842
    @chenhaofeng4842 2 года назад

    The best video for skyline problem

  • @hannanathar3627
    @hannanathar3627 11 месяцев назад

    00:02 Merge overlapping buildings to form skyline view
    02:35 Algorithm for finding skyline points
    04:53 Tracking building heights to determine skyline changes.
    07:11 Process the input points and prioritize based on start and end values
    09:39 Understanding edge cases in the Skyline Problem
    12:00 Examining time complexity of the problem.
    14:06 Ordering rules for buildings based on heights
    16:14 Updating building heights and recording the maximum height
    18:03 Tracking and updating the current max height
    19:58 Explanation of adding and deleting building points based on conditions
    21:53 Deleting a single count from map affects height calculation

  • @sungbokang1492
    @sungbokang1492 5 лет назад +2

    The easiest approach I've seen. Thanks!

  • @kyryloreznykov4959
    @kyryloreznykov4959 8 лет назад +5

    The best channel about algorithms on youtube! Thank you.

    • @raghav28489
      @raghav28489 5 лет назад

      nope, many of his videos are simply mugged up intuition-less explanations

  • @briankarcher8338
    @briankarcher8338 3 года назад +1

    This is a crazy awesome explanation of a really hard problem. Hats off to you. You should get back to the RUclips game :)

  • @benpurcell591
    @benpurcell591 6 месяцев назад

    3 mins in , very clear, great explanation

  • @rookieexplorer007
    @rookieexplorer007 4 года назад +1

    Tushar bhai....Tussi great ho!

  • @tsukisos
    @tsukisos 8 лет назад +1

    Hey, thanks a lot Tushar! Your explaination for the edge cases helped me solve the bugs I've been having for a whole day, LOL.. Keep it up buddy!

  • @piyushbansal8004
    @piyushbansal8004 4 года назад +1

    Great work Tushar...You made it very clear especially the edge cases.
    For those who write in c++ the compare function for sorting the buildings is
    bool operator()(bb a, bb b) {
    if (a.x != b.x) {
    return a.x < b.x;
    }
    else if (a.isStart and b.isStart) {
    return a.y > b.y; //building with larger height
    }
    else if (!a.isStart and !b.isStart) {
    return a.y < b.y; //building with smaller height
    }
    else {
    return a.isStart; //if end and another start we will consider start building first
    }
    }

  • @zhangbrian102
    @zhangbrian102 2 года назад

    Cannot thank more. Do appreciate your great explanation!

  • @ignaciogomez1816
    @ignaciogomez1816 9 лет назад +2

    Pretty simple! I saw almost all your videos and I have to say that you made a great job.
    It would be helpful if you can share your interview experiences too
    Congratulations!!!

  • @cyf6412
    @cyf6412 7 лет назад

    Tushar's youtube video is really efficient to learn. Thanks!

  • @baurks
    @baurks 4 года назад

    This is great. Finally I am able to solve the problem following your video. Thanks so much!

  • @manojamrutharaj9071
    @manojamrutharaj9071 5 лет назад

    Great explanation Tushar, content is clear with enough Algo tracing examples. I always learn some stuff from your videos. Thanks!

  • @nirmalgurjar8181
    @nirmalgurjar8181 9 месяцев назад

    All 3 edge cases are covered if we mark -ve height for start the building and now if starts are same then height can be matched.

  • @mbuchove
    @mbuchove 4 года назад

    Consistently excellent explanations!

  • @kakkwxt3653
    @kakkwxt3653 8 лет назад +41

    youtube上讲算法最清楚的频道了。。。膝盖收下

    • @yuhaokong
      @yuhaokong 7 лет назад

      加我一個

    • @anshuman1964
      @anshuman1964 6 лет назад +1

      你是对的

    • @Charles-rn3ke
      @Charles-rn3ke 5 лет назад +8

      这哥们在难题上讲的都比华语的频道清楚。

    • @BullishBuddy
      @BullishBuddy 5 лет назад +7

      translate: This is the clearest explanation among all algo videos on RUclips. Please accept my knees.

    • @pullrequest1296
      @pullrequest1296 5 лет назад +1

      如果能吧口音纠正一下,就完美了

  • @firenutz698
    @firenutz698 4 года назад

    Tushar you explained really well I just wish I could see this solution as easy as you did lol

  • @yangli6597
    @yangli6597 4 года назад

    Veryyyyyy clear about this problem!!! Thanks a lot.

  • @junnunsafoan3977
    @junnunsafoan3977 3 года назад

    Tushar is LC Legend.

  • @biboswanroy6699
    @biboswanroy6699 4 года назад

    There is also a divide and conquer approach. This approach is nice. But in cpp I had to use multimap and create a priority queue according to the requirement for removing any node. In an interview it seems to be very lengthy

  • @prajaktakarandikar3459
    @prajaktakarandikar3459 4 года назад +1

    Anytime the height changes, add the x coordinate and the current max height to the answer.

  • @deathbombs
    @deathbombs 2 года назад

    If we wanted to use priorityQueue instead of TreeMap, would this help optimize? PQ
    Current solution to clarify is:
    TreeMap

  • @deathbombs
    @deathbombs 2 года назад

    The sorting algorithm for buildingPoints class is key here for finding start, end, and skyline height:
    (this.isStart ? -this.height : this.height - o.isStart ? -o.height : o.height)
    if same x, sort the start points by height, ignore the end points
    if not same x, sort by x

  • @sengineer2554
    @sengineer2554 6 лет назад +1

    Dude this is amazing, thanks for sharing it with us

  • @Atpugtihsrah
    @Atpugtihsrah 3 года назад

    Can't thank you enough for this great explanation!

  • @meghna1320
    @meghna1320 3 года назад

    Thanks Tushar, this is very helpful!

  • @darthvader_
    @darthvader_ 3 года назад

    Great Explanation

  • @miketsubasa3611
    @miketsubasa3611 4 года назад

    Great Explanation.your explanations are always superb.concept explanation,then edge cases,and then code explanation makes them complete

  • @herculean6748
    @herculean6748 2 года назад

    Great explanation!!

  • @aj_prakash
    @aj_prakash 6 лет назад +1

    Thank you very much for this amazing explanation. Super clear and explained all the edge cases well.

  • @lallu12343
    @lallu12343 5 лет назад

    Thanks Tushar sir for explaining, Heap is not the most appropriate data structure to be used in this problem, this is a classic use case for ordered_map, I was silly to think this could be replaced with ordered_set but didn't consider the case of duplicate heights.
    TreeMap() is the solution.

  • @nairchannel3753
    @nairchannel3753 4 года назад

    Couldn't be done better . Thanks .

  • @TheAsltech
    @TheAsltech 9 лет назад

    great work Tushar..

  • @chrisniuniu
    @chrisniuniu 8 лет назад +1

    Thank you very much Tushar sir, you are the best!!!

  • @kaichenghu3826
    @kaichenghu3826 5 лет назад

    hey man, this is crystal clear

  • @indrajitbanerjee5131
    @indrajitbanerjee5131 4 года назад

    Nice explanation.

  • @mylotundinho
    @mylotundinho 5 лет назад +1

    Top man! This is great!

  • @reshmichowdhury5189
    @reshmichowdhury5189 7 лет назад

    Great video. Many thanks for taking time to upload this great video.

  • @sabeernitb
    @sabeernitb 8 лет назад

    Awesome explanation Tushar!!

  • @kanthravivvn
    @kanthravivvn 5 лет назад

    clear explanation Tushar. Keep up the good work !!

  • @mercuriallabs9
    @mercuriallabs9 4 года назад

    good solution, but I feel the time complexity depends a lot on the priority queue implementation. I have not come across any implementation of priority queue that supports delete operation in real world in O(logn). So lets suppose deletion is supported in O(x). Then time complexity of solution becomes O(n.x).

  • @joshuamelo2010
    @joshuamelo2010 5 лет назад

    Very good video

  • @phoenix2623
    @phoenix2623 5 лет назад +2

    Thanks for the video, amazing explanation!! Another case handled inadvertently by the comparator, and is worth mentioning is that, if this.x == other.x && this. height == other.height, the point that has isStart = true comes before the point with isStart = false, in order to avoid errorenously flipping the height back to lower and making a wrong critical point in result. Thanks!

    • @Madeinchinaagain
      @Madeinchinaagain 2 года назад +2

      Thanks for pointing this out! I was wondering about this case, too. 2 years later, your comment came in handy!

  • @vijay19841000
    @vijay19841000 8 лет назад

    Thanks a lot Tushar.
    Your explanation was really helpful.

  • @vijendrakumarsharma5250
    @vijendrakumarsharma5250 4 года назад +1

    c++ : can be done via multiset and few implementation changes.. idea is same as above

  • @AishwaryaRadhakrishnan34
    @AishwaryaRadhakrishnan34 4 года назад

    Amazing! Very Helpful !

  • @mukhtarbimurat5106
    @mukhtarbimurat5106 5 лет назад

    Thank you very match! Very clear, especially edge cases!

  • @bobsunboring
    @bobsunboring 4 года назад

    Best and clearest! TY!

  • @smanjunath14
    @smanjunath14 6 лет назад

    Great explanation thanks for sharing

  • @Kavishkhullar
    @Kavishkhullar 3 года назад

    saw the compareTo method you wrote. It's a great implementation.

  • @talivanov93
    @talivanov93 4 года назад

    Great explanation. Thank you!

  • @shreyas.kulkarni
    @shreyas.kulkarni 7 лет назад +1

    Have a feeling that PQ/heap is an overfit for this problem. If the given input of n intervals is exploded to 2n entries with x1:y and (x2-1):y, then sorted on x, and then deduped for x and y both (with different rules) and expanded for missing entries in that deduping pass itself, we can still get to an O(nlogn) worst case.

  • @lakshaydulani
    @lakshaydulani 6 лет назад

    nice question..i thought of the same algo.. happy to see it getting verified here

  • @poojaprasanthi1272
    @poojaprasanthi1272 3 года назад

    Thanks Tushar

  • @yh28789
    @yh28789 8 лет назад

    This is a really good video!

  • @sj_wanders
    @sj_wanders 6 лет назад

    Thank you, Tushar. It's an old video but still so useful.

  • @swapneshchaubal351
    @swapneshchaubal351 6 лет назад

    Thanks for the videos dude, appreciate all the hard work you do. Cheers!

  • @tanufive
    @tanufive 8 лет назад +2

    if we use priorityQueue then there is no way to maintain a counter for height repetition and i guess its required to have that. So PQ is not an option

    • @RagazzoKZ
      @RagazzoKZ 5 лет назад +1

      You are absolutely right!

  • @motivation_hubPJ
    @motivation_hubPJ 4 года назад

    Thanks a lot tushar

  • @yunierperez2680
    @yunierperez2680 8 лет назад

    Excellent explanation! Are you going to include a divide & conquer solution for this problem in some other video?

  • @VuNguyen-hi3fu
    @VuNguyen-hi3fu 8 месяцев назад

    does the algorithm stays the same when buildings are extended to n-dimension space

  • @cashflow9156
    @cashflow9156 9 лет назад +2

    Great video

  • @wanghaochen3515
    @wanghaochen3515 8 лет назад +4

    Just one thing, how are you supposed to remove an element from a priority queue if it wasn't the head of it? There's no way to iterate a c++ STL or Java queue. I think a better explanation is a max heap not a priority queue.

    • @tsukisos
      @tsukisos 8 лет назад +4

      In c++ you can use std::multiset

  • @itsjensenkuo
    @itsjensenkuo 9 лет назад

    Awesome tutorial.

  • @SudeeptaSood
    @SudeeptaSood 4 года назад

    thanks tushar!

  • @ashrafulfuad2967
    @ashrafulfuad2967 6 лет назад +1

    you can use extra microphone to record your voice
    please it will be helpful for viewers and listeners

  • @qiwang8640
    @qiwang8640 5 лет назад +1

    Thanks a lot! Really Helpful!

  • @shashankkumar1974
    @shashankkumar1974 3 года назад

    Can we use multiset like data structure in C++

  • @jasonng3194
    @jasonng3194 6 лет назад +1

    There is one thing I dont understand. Is there a need to use a Queue? As you have suggested, you can also use TreeMap. but how about using a linked list?

    • @BuffPomsky
      @BuffPomsky 6 лет назад

      access to find and delete will not be around constant time but linear then

    • @miracledoh4020
      @miracledoh4020 6 лет назад

      if you use linkedlist, wouldn't you have to sort it every time you add a new value?that's some poor performance

  • @Rachitagupta11
    @Rachitagupta11 3 года назад

    how much time did it take for you to solve this?

  • @pianochannel100
    @pianochannel100 4 года назад

    God bless you, you magnificent bastard.

  • @marriagedance8657
    @marriagedance8657 4 года назад

    great video!

  • @patyogesh20
    @patyogesh20 7 лет назад +1

    It's not clear what will be the KEY & VALUE in TreeMap though? And, thank you for great explanation

  • @crystinaxinyuzhang3621
    @crystinaxinyuzhang3621 4 года назад +1

    yet how do u remove a non-largest element from the height max-heap?

    • @briankarcher8338
      @briankarcher8338 3 года назад

      That's the million dollar question. When to, and not to, use a particular data class.

  • @tomislavzoricic5656
    @tomislavzoricic5656 8 лет назад

    Great explanation!

  • @jaideeppyne1880
    @jaideeppyne1880 4 года назад

    Does this pass for the case where input array is [[0,2,3],[2,5,3]]? Implemented in C++ its not passing