MAXIMUM SWAP | LEETCODE 670 | PYTHON SOLUTION

Sdílet
Vložit
  • čas přidán 9. 07. 2024
  • Channel Discord Community: / discord
    Problem Link: leetcode.com/problems/maximum...
    Today we are solving a god awful problem: Maximum Swap. It's a question that is pretty easy to grasp in terms of the intuition of the problem but it's such a chore to actually code up. There's so many annoying nuances in this problem that just add up and give you a headache.
    TIMESTAMPS
    00:00 Intro
    00:20 Question Prompt
    00:28 Basic Example
    01:38 Solution Intuition
    02:57 Coding
    12:30 Time/Space Complexity
    13:55 Outro
  • Věda a technologie

Komentáře • 11

  • @labixiaoxin1307
    @labixiaoxin1307 Před 23 dny

    Thanks for saying this question is a pain in the ass, I feel exactly the same.

  • @lordeagle2000
    @lordeagle2000 Před 4 měsíci +1

    As always, great vid! I just did the problem a few times to figure out how to make it less annoying to code up without screwing up the time/spacing complexity and here's what I came up with (which at least works in JavaScript):
    1. Turn the input integer into an array of *characters* and then a plain split. So for python something like `num_as_arr = list(str(num))`that turns 6 init lines into 1 without impacting readability
    2. You don't need to use integers: comparing ASCII characters/strings (because they are of exactly 1 character) will work just as well. So you can perform the swap with chars
    3. Only change is at the end to remember to parse each char back to an int (via `num = num * 10 + ord(cur) - ord('0'))
    This shaves off almost half the "basic" code without, IMO, affecting readability much.

    • @letsgetyucky
      @letsgetyucky Před 3 měsíci +1

      This is so clever, thanks for sharing. Still 39 lines (including liberal whitespace) but much easier to grasp and remember.

  • @syafzal273
    @syafzal273 Před 5 měsíci +1

    Super annoying question like you said! Thanks for explaining the algorithm behind it!

    • @crackfaang
      @crackfaang  Před 5 měsíci +1

      No problem, glad the video helped

  • @3rd_iimpact
    @3rd_iimpact Před 5 měsíci +3

    38. Count and Say is another one I hate.

  • @leizhang799
    @leizhang799 Před 2 měsíci

    awesome solution

  • @weeno4331
    @weeno4331 Před 4 měsíci

    I actually like this question, it reminds me of buildings w an ocean view

  • @APudgyPanda96
    @APudgyPanda96 Před 4 měsíci

    As a java user this solution is not it

  • @kadimisettyavinash
    @kadimisettyavinash Před 5 měsíci

    This problem and the next permutation problem - never liked them

    • @crackfaang
      @crackfaang  Před 5 měsíci +2

      Yea absolutely terrible questions. Next Permutation especially