We can rewrite any integer as a sum of products by powers of 10: 996969 = 9*10⁵ + 9*10⁴ + 6*10³ + 9*10² + 6*10¹ + 9*10⁰. As 9>6, we can write 999999 (9*10⁵+...+9*10⁰). The difference between the digit and the "first" 6 being non zero will tell us which to change: 999999 - 996969 = 3030 which means that the 3rd (6-4 +1) digit must be changed. Thefore the code can be written to be O(1) instead of O(n): def maximum69Number (self, num: int) -> int s = str(num) max99 = int('9'*length(s)) #maximal int of the same length if max99 - num == 0: #then it's the max return num else: diff_length = length(str(max99 - num)) i = length(s) - diff_length return int(s[:i] + '9' + s[i+1:])
one of my biggest problems when I started practicing questions like this is that I'd always try to solve them using exponents and logarithms instead of the simple string manipulation solution
It would be, Probably because this is already happening under the hood in the string conversion, Better than having to allocate more memory for the string then turning digits into their ASCII repesentation and storing them. All that can be avoided.
@@darkopzthere is a difference between "pre mature optimization" and "doing it not stupidly" and holy shit it feels weird to watch these videos and know more than them now, when I grew up it was the other way around
I am getting these a whole lot more recently, but I think it would be nice if the solution came with print statements at each iteration to understand what's going on visually. Just seeing lines of codes and the returned value is really unhelpful.
Not to discourage but this is just a basic for loop with a if condition in it. If you have problems understanding this then you need to revisit how for loop works.
I feel like you should teach more of Algorithms rather than just using some libraries or functions sir. You should be telling about how to_string works etc. Because I am an Intermediate programmer and A lot of my beginner Friends suffer through the sense of logic and computer science because of this
We can rewrite any integer as a sum of products by powers of 10: 996969 = 9*10⁵ + 9*10⁴ + 6*10³ + 9*10² + 6*10¹ + 9*10⁰. As 9>6, we can write 999999 (9*10⁵+...+9*10⁰). The difference between the digit and the "first" 6 being non zero will tell us which to change: 999999 - 996969 = 3030 which means that the 3rd (6-4 +1) digit must be changed. Therore the code can be written to be O(1) instead of O(n): def maximum69Number (self, num: int) -> int s = str(num) max99 = int('9'*length(s)) #maximal int of the same length if max99 - num == 0: #then it's the max return num else: diff_length = length(str(max99 - num)) i = length(s) - diff_length return int(s[:i] + '9' + s[i+1:])
Master Data Structures & Algorithms For FREE at AlgoMap.io!
We can rewrite any integer as a sum of products by powers of 10:
996969 = 9*10⁵ + 9*10⁴ + 6*10³ + 9*10² + 6*10¹ + 9*10⁰.
As 9>6, we can write 999999 (9*10⁵+...+9*10⁰). The difference between the digit and the "first" 6 being non zero will tell us which to change:
999999 - 996969 = 3030 which means that the 3rd (6-4 +1) digit must be changed.
Thefore the code can be written to be O(1) instead of O(n):
def maximum69Number (self, num: int) -> int
s = str(num)
max99 = int('9'*length(s)) #maximal int of the same length
if max99 - num == 0: #then it's the max
return num
else:
diff_length = length(str(max99 - num))
i = length(s) - diff_length
return int(s[:i] + '9' + s[i+1:])
Missed opportunity to say "nice" at the end
This one actually looks useful. Just replace the 69 with 1 and 0s
I thought the task was to find the maximum number of '69's in the string
one of my biggest problems when I started practicing questions like this is that I'd always try to solve them using exponents and logarithms instead of the simple string manipulation solution
you should resize your screenshots so we can at least read the whole question
It'd be more memory efficient to mod by increasing powers of 10 to get the digits instead of converting to a string.
I've seen a few videos on this chanel, the author is far away from writing good performance code.
It would be, Probably because this is already happening under the hood in the string conversion, Better than having to allocate more memory for the string then turning digits into their ASCII repesentation and storing them. All that can be avoided.
Yeah this guy always shows up on my feed and I think he'd be lucky to pass an entry level coding interview
@@darkopzthere is a difference between "pre mature optimization" and "doing it not stupidly"
and holy shit it feels weird to watch these videos and know more than them now, when I grew up it was the other way around
I want to reach the level of understanding and question solving ability like yours. I am right now learning C as my first language
Go for c++
it's just "start from the left and change first encountered 6 into 9"
Dayummm, c as first language? I started on html and JavaScript. But i wish you luck!
- CS student who's been coding since 15
Sounds good, I'm sure you're well on your way! I have probably just solved more problems haha it just takes time and commitment
Plain code discussion is good enough 😂
I am getting these a whole lot more recently, but I think it would be nice if the solution came with print statements at each iteration to understand what's going on visually. Just seeing lines of codes and the returned value is really unhelpful.
Not to discourage but this is just a basic for loop with a if condition in it. If you have problems understanding this then you need to revisit how for loop works.
Pretty sure this code would not be correct. It does work for a number with only 9's, but will not give the correct result for a number of only sixes.
Given an array with only 6's, the digit in the first index will be replaced with 9, we call the "break", then return the modified number
@@darknight3613right, but there would be no 9 to replace the 6 with in the original number?
@@lucsol2255 the question is not explained very well but based on the algorithm you can understand that just turning the first 6 to a 9 is valid
What software are you using for illustration
A stack can be used hear to solve , pop when peek is 6 and we are about to push 9 at last we have to check size of stack
so
number.toString().replace('6','9').toInt()
assuming replace only replaces once (like js)
Thanks
It would be nice to see a transformer inplementation tutorial there are very few on The internet.
Does python not have indexOf? Just index of 6, if there isnt one, you are done
Awwww, he lives at home and is telling you all how to pass job exams.... OKAAAAY
I'm sorry but who doesn't live at home, would it be more convincing if I lived on the street?
The funnier thing is that you believe that voice is a woman’s voice lol.
even funnier that three hours later you're still thinking about this... time to leave mom's basement, I think@@arsheyajain7055
@@GregHogg yup. Way more. Terry Davis my boi. And me.
i hate that im here, but i have an interview in a week
There should be an alternative 🤔💭
Bro is fine 🥰
Hi ! What is that website ?
Thank you in advance 😊
Leet code
Now it seems only right that another with 420 is added
I feel like you should teach more of Algorithms rather than just using some libraries or functions sir. You should be telling about how to_string works etc. Because I am an Intermediate programmer and A lot of my beginner Friends suffer through the sense of logic and computer science because of this
Bro never done 69. Me neither
I bet this interview question is from only fan or p*rnhub companies 😂😂
Nice 😏
Nice :)
Lol
We can rewrite any integer as a sum of products by powers of 10:
996969 = 9*10⁵ + 9*10⁴ + 6*10³ + 9*10² + 6*10¹ + 9*10⁰.
As 9>6, we can write 999999 (9*10⁵+...+9*10⁰). The difference between the digit and the "first" 6 being non zero will tell us which to change:
999999 - 996969 = 3030 which means that the 3rd (6-4 +1) digit must be changed.
Therore the code can be written to be O(1) instead of O(n):
def maximum69Number (self, num: int) -> int
s = str(num)
max99 = int('9'*length(s)) #maximal int of the same length
if max99 - num == 0: #then it's the max
return num
else:
diff_length = length(str(max99 - num))
i = length(s) - diff_length
return int(s[:i] + '9' + s[i+1:])