Тёмный

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

NeetCodeIO
Подписаться 217 тыс.
Просмотров 7 тыс.
50% 1

Опубликовано:

 

21 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 33   
@AlfredPros
@AlfredPros 2 дня назад
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 3 дня назад
I cached all strings outside the class and Runtime reduced from 537ms -> 51ms
@prajwals8203
@prajwals8203 3 дня назад
you are a freaking genius
@dampdigits.69
@dampdigits.69 2 дня назад
I didn't and my run-time was 0ms in python
@baetz2
@baetz2 2 дня назад
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 2 дня назад
@@baetz2 Java?
@ThinkOutsideTheBox12345
@ThinkOutsideTheBox12345 3 дня назад
Thanks for this video, the way you explain problems is brilliant!
@MP-ny3ep
@MP-ny3ep 3 дня назад
Terrific explanation !
@mitratiwari5035
@mitratiwari5035 3 дня назад
I am a beginner and started doing leetcode recently, your videos really helped me understand and write efficient code. Thanks 😄
@pastori2672
@pastori2672 2 дня назад
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))
@ThinkOutsideTheBox12345
@ThinkOutsideTheBox12345 3 дня назад
Great video!
@eamytb
@eamytb 3 дня назад
How about explaining O(1) solution?
@tejas1531
@tejas1531 3 дня назад
Thanku soooooooooo much!!
@IK-xk7ex
@IK-xk7ex 3 дня назад
Inversion function is f(x) = 1-x where x={0,1}
@bobyoutube2117
@bobyoutube2117 3 дня назад
Just simulating works as well
@kevinnguyen4222
@kevinnguyen4222 3 дня назад
Bro must be predicting these daily questions there's no way
@hithambasheir3283
@hithambasheir3283 2 дня назад
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
@abhivardhan395
@abhivardhan395 2 дня назад
Best.....
@kitt-u8r
@kitt-u8r 2 дня назад
beats 100% !!!!! ,hurehhhhh
@rahulsihara8946
@rahulsihara8946 3 дня назад
what going with quick uploads ? i didn't even get time to read the question.
@amitgoyal8760
@amitgoyal8760 3 дня назад
Wtf man this speed is
@nofollowrobotstxt
@nofollowrobotstxt 2 дня назад
menopause
@princeakhil208
@princeakhil208 3 дня назад
Wow this is super fast
@higuys450
@higuys450 3 дня назад
I just woke up bro
@albin_joby
@albin_joby 2 дня назад
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]
@bhanunani1307
@bhanunani1307 3 дня назад
how to be like him😬😭
@SidhanthSanil
@SidhanthSanil 3 дня назад
beauty
@Graveness4920
@Graveness4920 3 дня назад
How did you know the question beforehand to release the video exactly at 00:00 UTC?
@31redorange08
@31redorange08 3 дня назад
He activated cheats.
@zweitekonto9654
@zweitekonto9654 3 дня назад
Ain't know way what
@vuduongcong1320
@vuduongcong1320 3 дня назад
you can change the timezone and get the question bf your midnight
@blue5887
@blue5887 3 дня назад
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 3 дня назад
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.
Далее
aespa 에스파 'Whiplash' MV
03:11
Просмотров 12 млн
10 FORBIDDEN Sorting Algorithms
9:41
Просмотров 893 тыс.
Жадные алгоритмы
11:10
Просмотров 14 тыс.
The Tri Folding Phone Impressions!
11:18
Просмотров 2,9 млн
I reverse engineered Next to find what they are hiding
29:48
Harder Than It Seems? 5 Minute Timer in C++
20:10
Просмотров 186 тыс.
Microservices are Technical Debt
31:59
Просмотров 548 тыс.
Making an Algorithm Faster
30:08
Просмотров 124 тыс.