Find the Skyline Problem with C++ Solution Explained

Поділитися
Вставка
  • Опубліковано 19 гру 2024

КОМЕНТАРІ • 82

  • @szymoniak75
    @szymoniak75 3 роки тому +180

    5:57 ahh yes verbose function naming

    • @mCoding
      @mCoding  3 роки тому +76

      I spent a long time trying to think of a descriptive shorter name, but alas I did not succeed. Sometimes that's just how it goes.

    • @aBigBadWolf
      @aBigBadWolf 3 роки тому +15

      @@mCoding function name != function description

    • @neur303
      @neur303 3 роки тому +1

      @Elias Productions Exactly just use a (maybe a little verbose) name instead of a comment. If something too verbose it's also a good indicator for a second look for refactoring.
      Also a verbose name is inconvenient when used often. In that case again, maybe a candidate for refactoring.
      Also I noticed that I often wrote comments at the call site where I used it (e.g. because expect weird side effects). Using the name makes that redundant too.

  • @AndiAbrudan
    @AndiAbrudan 3 роки тому +45

    Your gig might be python, but I see a bright future in your C++ content. Keep it up

    • @mCoding
      @mCoding  3 роки тому +11

      Thank you very much for your kind words! I will definitely do my best to keep it up.

  • @jovanxin6762
    @jovanxin6762 3 роки тому +26

    Loving this sort of content, more competitive programming/alg/ds lectures would be appreciated!

  • @zzzeval
    @zzzeval 3 роки тому +23

    The quality of your videos is superb. I truly don't understand how you don't have more subscribers. Cheers.

    • @mCoding
      @mCoding  3 роки тому +12

      Thanks so much! I know I haven't figured out how to grab a general audience's attention for very long. Working on it and always open to suggestions!

    • @justingolden21
      @justingolden21 3 роки тому +1

      I was thinking the same thing like half a year ago. He'll get there. He was at just thousand or so subs before and I was in shock. If he keeps making great vids, getting views, likes, comments, he'll get 100k in no time.

    • @Al-.-ex
      @Al-.-ex Рік тому

      @@justingolden21 you were right!!

  • @tonysax7464
    @tonysax7464 3 роки тому +33

    Could you work on making the visuals appear alongside the code sometimes? Especially for explaining logic portions like when you explain popTallestUntilRight...()

    • @karimm.elsayad9539
      @karimm.elsayad9539 2 роки тому +4

      Yeah this is where the video lost me. I try to visualize everything, but at some point I couldn't anymore.

  • @topperthehorse
    @topperthehorse 3 роки тому +34

    Is putting the return type on its own line something typical? I'm pretty new to C++ and I've not seen that style before. Thanks for the example, I'll give it a try.

    • @mCoding
      @mCoding  3 роки тому +27

      This is "needs to fit on screen even when the font is large" style. Not sure if anyone does this in normal code

    • @mCoding
      @mCoding  3 роки тому +33

      Apparently this is some old school C formatting www.gnu.org/prep/standards/standards.html#Formatting

    • @megaing1322
      @megaing1322 3 роки тому +6

      As example, this is the coding style used in CPython. So it is somewhat common at least.

    • @resilientbit
      @resilientbit 3 роки тому +3

      That's from the GNU style guide for Linux application programming, so that you don't have search for function names while looking at someone else's code.

    • @GalizurHaMalakh
      @GalizurHaMalakh 3 роки тому +2

      C89 style function returns. Modern way to return (C++11 and up, if your compiler supports it) is
      auto FunctionName(param a, param b) -> return_type
      { // code }
      (capitalization may vary according to your style)

  • @theawakened7739
    @theawakened7739 3 роки тому +40

    YT algorithm boost

  • @niconeuman
    @niconeuman 3 роки тому +15

    @mCoding, this is a great video as always! You mentioned in a comment that you are looking for ideas for a more general audience. IMHO the more basic or introductory the content the more people interested in it. Of course defining that is complicated. Basic C++ may be harder than intermediate Python, idk. But making intro tutorials of C++ and/or Python (or Julia) maybe a good way to reach a wider audience. I, for example, would love to see a tutorial on some graphical library in C++. How to do in C++ the kind of plots that can be done in Python or MATLAB or Octave, etc. Just an idea.

    • @mCoding
      @mCoding  3 роки тому +6

      Thanks I appreciate the ideas!

  • @glorytoarstotzka330
    @glorytoarstotzka330 3 роки тому +4

    that excited voice at the end was amazing

  • @vinvic1578
    @vinvic1578 3 роки тому +5

    Hi , I'm a bit of a cpp newbie, why does he use noexcept for the getter functions ?

    • @mCoding
      @mCoding  3 роки тому +5

      Hi! This gives a hint to the compiler that I believe an exception will never be thrown during the execution of the function. It allows for additional optimizations by the compiler because that means the compiler doesn't have to add any extra code preparing for stack-unwinding when a potential exception is thrown. If you mark a function noexcept and it does end up throwing an exception though, your program is terminated by calling std::terminate or std::unexpected.

    • @vinvic1578
      @vinvic1578 3 роки тому +1

      @@mCoding I see ! So I should always use noexcept for very simple functions that I know will never throw an exception, like the getters ? Thanks so much for the response :)

  • @ganelonhb
    @ganelonhb 3 роки тому +12

    Gonna try this when I get home! I haven’t done C++ in a long while. What’s a better ice breaker than a good ol’ algorithm

  • @eliseuantonio6652
    @eliseuantonio6652 3 роки тому +6

    Although I only know Python, I appreciated your detailed explanations. Great video!

  • @VivekYadav-ds8oz
    @VivekYadav-ds8oz 8 місяців тому +1

    Here's how I did it:
    Put all the buildings in a priority_queue. Keep track of the tallest building till now. If you encounter a "collision" with the tallest building till now, either you are taller than it, or you are shorter, or equal.
    If you are taller, congrats! Push yourself into the solution, and now you're the tallest building till now.
    If you're shorter, well maybe not for long! We "trim" the building (building.startX = tallest_building.endX) and push it back into the priority_queue because it may become a contender of the skyline later on.
    If you're equal in height, then nothing to push, but you're the tallest building till now.
    Oh and also in case where you are taller, you need to "trim" the ex-tallest building too as it may also become tallest again later on (ofcourse only trim if it is wider and stretches beyond the newest tallest building's right endpoint).

    • @VivekYadav-ds8oz
      @VivekYadav-ds8oz 8 місяців тому +1

      I got 100% in runtime (7ms) and 98% in memory

  • @john.dough.
    @john.dough. 3 роки тому +4

    I love your visualization!

  • @realGeobor
    @realGeobor 3 роки тому +3

    Why is everything marked as static? I would think that this class is a single instance class so static doesn't seem that useful. Is there some optimization benefit to making the members static?

    • @mCoding
      @mCoding  3 роки тому +6

      The algorithm does not maintain any state so logically it is static. The real answer here though is that it shouldnt even be inside a class, it should just be free functions. But leetcode wants it in a class.

  • @akshaypairaikar3128
    @akshaypairaikar3128 3 роки тому +1

    Interesting content, looking forward to seeing more C++ on your channel!

  • @see8chsee
    @see8chsee 3 роки тому +3

    What code editor/IDE are you using in your videos?

    • @mCoding
      @mCoding  3 роки тому +1

      Vscode in this video

    • @see8chsee
      @see8chsee 3 роки тому +1

      @@mCoding Thank you. I see you also say PyCharm in another video. I like the theme.

  • @luizchagasjardim
    @luizchagasjardim 3 роки тому +4

    Using a vector for what is clearly a struct is not only suboptimal, it also makes the code less clear. At first I thought these vectors were the whole list of inputs.

    • @mCoding
      @mCoding  3 роки тому +2

      I had the same misconception when I first saw the problem. I had the biggest visible confusion face in a long time after realizing it was just left right height

  • @Cubinator73
    @Cubinator73 3 роки тому +3

    5:57 popTallestUntilRightExceedsCurrentTallestRightAndUpdateSkyline :D

    • @mCoding
      @mCoding  3 роки тому +2

      Unashamed :D

  • @drel9300
    @drel9300 3 роки тому +1

    My first reaction when I saw the Building with 3 fields being represented as an std::vector, was also "Seriously why, LeetCode?" Thanks for pointing that out!

  • @atrus3823
    @atrus3823 3 роки тому +2

    Big fan of the channel, and am a somewhat avid Pythonista, but am interested in learning C++. I'm already fairly proficient in C. Any advice on where to start learning?

  • @progamefixers
    @progamefixers 3 роки тому +1

    Waiting for more videos like this :)

  • @LuckyST
    @LuckyST 3 роки тому

    how hard would it be to include "concave" shapes, "flying objects" and/or balcony above the camera?

  • @chasehiatt5595
    @chasehiatt5595 3 роки тому +4

    I don't know, I think relative performance is much more important to leetcode than the literal number of milliseconds. Because of this, forcing the use of slower data types isn't an issue IMO

    • @mCoding
      @mCoding  3 роки тому +14

      I have to disagree, performance in C++ often (and certainly in this case) comes down to counting heap allocations and cache misses. If you replaced vector with array in everyone's answer you would not find that everyone's answer kept it's relative position.

  • @eccentricOrange
    @eccentricOrange 3 роки тому +7

    If this was a competition about just number of steps scaling (like how we count big O) rather than time, I guess vectors/arrays won't matter?

  • @vilkillian
    @vilkillian 3 роки тому +1

    But how you can be sure that buildings are sorted? This whole algorithm collapses if they are not sorted

    • @mCoding
      @mCoding  3 роки тому +1

      The buildings being sorted by left coordinate was one of the assumptions of the problem. If you want to allow unsorted buildings, then just sort them first and proceeed.

    • @vilkillian
      @vilkillian 3 роки тому +1

      @@mCoding i've read description and didn't saw this assumption :(

  • @theawakened7739
    @theawakened7739 3 роки тому +3

    Yoh do multiple comments from one person help with the UA-cam algorithm boost ?

    • @mCoding
      @mCoding  3 роки тому +5

      Given what I know about how the neural network from 2016 was built, I'm guessing only one comment per user counts, but it probably doesn't hurt!

  • @Victor_Marius
    @Victor_Marius 3 роки тому

    What is the "use" keyword doing?

  • @ivanjermakov
    @ivanjermakov 3 роки тому

    I'm not sure was this done for performance or not, but test code is so copy-pasted. I believe you could use a single function to run all tests, passing an array of tuples "input -> output".

    • @mCoding
      @mCoding  3 роки тому

      You're totally right, this was done out of pure laziness, not for performance :)

  • @Dogberto999
    @Dogberto999 3 роки тому +1

    algo boost comment for good channel & video.

  • @pseudounknow5559
    @pseudounknow5559 3 роки тому +2

    Feeding the YT algorithm

  • @garryiglesias4074
    @garryiglesias4074 3 роки тому +1

    Solving skyline ? Easy -> scanline...:)

  • @zyzzbodybuilding
    @zyzzbodybuilding 3 роки тому

    Snakes all over the world have a moment of silence.

  • @castelan3419
    @castelan3419 3 роки тому

    No curves noted in the skyline

  • @chilly2171
    @chilly2171 2 роки тому

    this is so much easier in Python.

  • @robertbrummayer4908
    @robertbrummayer4908 3 роки тому

    Excellent

    • @mCoding
      @mCoding  3 роки тому

      Thank you! Cheers!

  • @MithicSpirit
    @MithicSpirit 3 роки тому +8

    Discord gang

    • @mCoding
      @mCoding  3 роки тому +4

      Let's get some new members!

  • @spencerbills1125
    @spencerbills1125 3 роки тому

    Discord gang by the way?

  • @markcuello5
    @markcuello5 2 роки тому

    Help me

  • @pinch-of-salt
    @pinch-of-salt 3 роки тому +8

    C/C++ >>>>>>> Python
    Any day!

    • @mCoding
      @mCoding  3 роки тому +3

      Gotta go fast!

    • @osolomons
      @osolomons 3 роки тому +1

      Is that like a "extra right shift" operator? :D

    • @giraculum9981
      @giraculum9981 3 роки тому +1

      @@osolomons It's so unsigned, it ignores signs you've never even heard of.

    • @Cubinator73
      @Cubinator73 3 роки тому +4

      Except when development time is short and runtime not important.

  • @mba4677
    @mba4677 3 роки тому

    Boooooost

  • @zxyz2191
    @zxyz2191 3 роки тому +2

    I will never not be impressed by just how consistently ugly C++ is.

  • @ahmadbodayr7203
    @ahmadbodayr7203 2 роки тому

    Great Vid Bro if you are not muslim Read About Islam❤

  • @esbensloth
    @esbensloth 2 роки тому

    This code will return an incorrect skyline if buildings are not sorted by left wall positions.