Largest Odd Number in String - Leetcode 1903 - Python
VloĆŸit
- Äas pĆidĂĄn 9. 07. 2024
- đ neetcode.io/ - A better way to prepare for Coding Interviews
đ§âđŒ LinkedIn: / navdeep-singh-3aaa14161
đ„· Discord: / discord
đŠ Twitter: / neetcode1
đź Support the channel: / neetcode
â BLIND-75 PLAYLIST: âą Two Sum - Leetcode 1 -...
đĄ DYNAMIC PROGRAMMING PLAYLIST: âą House Robber - Leetco...
Problem Link: leetcode.com/problems/largest...
0:00 - Read the problem
0:30 - Drawing Explanation
4:10 - Coding Explanation
leetcode 1903
#neetcode #leetcode #python
r = len(num) - 1
res = ''
if len(num) == 0:
return res
while r >=0 and int(num[r])%2 == 0:
r -= 1
return res + num[:r+1]
Thank you
Thanks, finally I did good solution before your video, yeah it's just an easy problem, but I am happy)
Can we use heap for the following?
If we start from 0->n(cpp)
It shows memory limit exceeded.
Why???
Looks like you're out of bounds, try n-1 :)
Easy one
def largestOddNumber(self, num):
for i in range(len(num)-1,-1,-1):
if int(num[i])%2==1:
return num[:i+1]
return ""
Way too easy problem it won't be asked đ
I thought list slicing was O(N) so why is it not O(N^2)
You only do list slicing once
What? Your answer is in your question.
Yeah in the code there's only one slice, which makes it o(n) :)
it would only be N^2 if you were to do a slicing operation for every iteration of the loop of the input parameter. But in this case, it is only done once
A more elegant and pretentious way to check if a number is odd is n & 1.
yes it is lol