MAXIMUM SWAP | LEETCODE 670 | PYTHON SOLUTION
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
Thanks for saying this question is a pain in the ass, I feel exactly the same.
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.
This is so clever, thanks for sharing. Still 39 lines (including liberal whitespace) but much easier to grasp and remember.
Super annoying question like you said! Thanks for explaining the algorithm behind it!
No problem, glad the video helped
38. Count and Say is another one I hate.
awesome solution
I actually like this question, it reminds me of buildings w an ocean view
As a java user this solution is not it
This problem and the next permutation problem - never liked them
Yea absolutely terrible questions. Next Permutation especially