Find Kth Bit in Nth Binary String - Leetcode 1545 - Python

  Рет қаралды 8,160

NeetCodeIO

NeetCodeIO

Күн бұрын

Пікірлер: 31
@AlfredPros
@AlfredPros Ай бұрын
A solution that I arrived with is by using two pointers. I noticed a repeating pattern as the n increases. The main idea is string after the middle of string will always be the same for the next n. n=2 -- 01**1** n=3 -- 0111**001** n=4 -- 011100110110**001** meaning that we only have to compute the inversion from the current middle pointer to the previous n middle pointer. def findKthBit(self, n: int, k: int) -> str: cur = "011" length = 3 left = 2 right = 2 while length < k: length = length*2 + 1 right *= 2 cur += "1" + cur[:left-2:-1].replace('1', '2').replace('0', '1').replace('2', '0') + cur[left:] left = right return cur[k-1] To prevent crash, I started at n=2. With addition to early stop to the loop, this solution beats 100% (11ms) on my end.
@debrajkundu2780
@debrajkundu2780 Ай бұрын
I cached all strings outside the class and Runtime reduced from 537ms -> 51ms
@prajwals8203
@prajwals8203 Ай бұрын
you are a freaking genius
@dampdigits.69
@dampdigits.69 Ай бұрын
I didn't and my run-time was 0ms in python
@baetz2
@baetz2 Ай бұрын
you can also hardcode the result as a big number for n=20 and return the kth bit of that number in 0ms, it will work for every n from 1 to 20
@prajwals8203
@prajwals8203 Ай бұрын
@@baetz2 Java?
@ThinkOutsideTheBox12345
@ThinkOutsideTheBox12345 Ай бұрын
Thanks for this video, the way you explain problems is brilliant!
@mitratiwari5035
@mitratiwari5035 Ай бұрын
I am a beginner and started doing leetcode recently, your videos really helped me understand and write efficient code. Thanks 😄
@MP-ny3ep
@MP-ny3ep Ай бұрын
Terrific explanation !
@pastori2672
@pastori2672 Ай бұрын
this is log(k) both space and time idk why its not in the editorial: def dfs(i): if i == 1: return 0 if math.log2(i) == math.ceil(math.log2(i)): return 1 mid = 2 ** math.floor(math.log2(i)) new_i = 2 * mid - i return 1 if dfs(new_i) == 0 else 0 return str(dfs(k))
@ksaigon
@ksaigon Ай бұрын
Bro must be predicting these daily questions there's no way
@hithambasheir3283
@hithambasheir3283 Ай бұрын
Well, it's predictable, if you check the editorial that's being added to problems you'll notice the new problems editorials, and for ones that already have editorial, you can get an idea of them by checking the monthly topics section, each week focuses on a set of topics
@IK-xk7ex
@IK-xk7ex Ай бұрын
Inversion function is f(x) = 1-x where x={0,1}
@tejas1531
@tejas1531 Ай бұрын
Thanku soooooooooo much!!
@ThinkOutsideTheBox12345
@ThinkOutsideTheBox12345 Ай бұрын
Great video!
@bobyoutube2117
@bobyoutube2117 Ай бұрын
Just simulating works as well
@rahulsihara8946
@rahulsihara8946 Ай бұрын
what going with quick uploads ? i didn't even get time to read the question.
@abhivardhan395
@abhivardhan395 Ай бұрын
Best.....
@kitt-u8r
@kitt-u8r Ай бұрын
beats 100% !!!!! ,hurehhhhh
@amitgoyal8760
@amitgoyal8760 Ай бұрын
Wtf man this speed is
@princeakhil208
@princeakhil208 Ай бұрын
Wow this is super fast
@higuys450
@higuys450 Ай бұрын
I just woke up bro
@albin_joby
@albin_joby Ай бұрын
val = ["0"] def convert(s): return ["0" if num == "1" else "1" for num in s][::-1] for _ in range(n): prev = val[:] val = val + ["1"] + convert(prev) return val[k-1]
@SidhanthSanil
@SidhanthSanil Ай бұрын
beauty
@bhanunani1307
@bhanunani1307 Ай бұрын
how to be like him😬😭
@Graveness4920
@Graveness4920 Ай бұрын
How did you know the question beforehand to release the video exactly at 00:00 UTC?
@31redorange08
@31redorange08 Ай бұрын
He activated cheats.
@zweitekonto9654
@zweitekonto9654 Ай бұрын
Ain't know way what
@vuduongcong1320
@vuduongcong1320 Ай бұрын
you can change the timezone and get the question bf your midnight
@blue5887
@blue5887 Ай бұрын
he's already solved these questions, when it comes up again as a daily problem he just uploads the video that he's already made on that problem
@aayushtheapple
@aayushtheapple Ай бұрын
Leetcode posts editorials for daily problems some time before they release it as a daily problem, you can filter the editorials get recent published editorials to get know which problem will be the daily problem next.
MAXIMUM SWAP | LEETCODE 670 | PYTHON SOLUTION
15:12
Cracking FAANG
Рет қаралды 6 М.
Why no RONALDO?! 🤔⚽️
00:28
Celine Dept
Рет қаралды 86 МЛН
ТЮРЕМЩИК В БОКСЕ! #shorts
00:58
HARD_MMA
Рет қаралды 2,7 МЛН
УДИВИЛ ВСЕХ СВОИМ УХОДОМ!😳 #shorts
00:49
HARD_MMA
Рет қаралды 3,3 МЛН
When Cucumbers Meet PVC Pipe The Results Are Wild! 🤭
00:44
Crafty Buddy
Рет қаралды 60 МЛН
Making an Algorithm Faster
30:08
NeetCodeIO
Рет қаралды 150 М.
Remove Sub-Folders from the Filesystem - Leetcode 1233 - Python
19:29
Minimum Array End - Leetcode 3133 - Python
23:22
NeetCodeIO
Рет қаралды 10 М.
Fast Inverse Square Root - A Quake III Algorithm
20:08
Nemean
Рет қаралды 5 МЛН
I Solved 1583 Leetcode Questions  Here's What I Learned
20:37
ThePrimeTime
Рет қаралды 740 М.
C Programming Tutorial for Beginners
3:46:13
freeCodeCamp.org
Рет қаралды 14 МЛН
Most Beautiful Item for Each Query - Leetcode 2070 - Python
17:17
Why no RONALDO?! 🤔⚽️
00:28
Celine Dept
Рет қаралды 86 МЛН