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Ай бұрын
I cached all strings outside the class and Runtime reduced from 537ms -> 51ms
@prajwals8203Ай бұрын
you are a freaking genius
@dampdigits.69Ай бұрын
I didn't and my run-time was 0ms in python
@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Ай бұрын
@@baetz2 Java?
@ThinkOutsideTheBox12345Ай бұрын
Thanks for this video, the way you explain problems is brilliant!
@mitratiwari5035Ай бұрын
I am a beginner and started doing leetcode recently, your videos really helped me understand and write efficient code. Thanks 😄
@MP-ny3epАй бұрын
Terrific explanation !
@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Ай бұрын
Bro must be predicting these daily questions there's no way
@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Ай бұрын
Inversion function is f(x) = 1-x where x={0,1}
@tejas1531Ай бұрын
Thanku soooooooooo much!!
@ThinkOutsideTheBox12345Ай бұрын
Great video!
@bobyoutube2117Ай бұрын
Just simulating works as well
@rahulsihara8946Ай бұрын
what going with quick uploads ? i didn't even get time to read the question.
@abhivardhan395Ай бұрын
Best.....
@kitt-u8rАй бұрын
beats 100% !!!!! ,hurehhhhh
@amitgoyal8760Ай бұрын
Wtf man this speed is
@princeakhil208Ай бұрын
Wow this is super fast
@higuys450Ай бұрын
I just woke up bro
@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Ай бұрын
beauty
@bhanunani1307Ай бұрын
how to be like him😬😭
@Graveness4920Ай бұрын
How did you know the question beforehand to release the video exactly at 00:00 UTC?
@31redorange08Ай бұрын
He activated cheats.
@zweitekonto9654Ай бұрын
Ain't know way what
@vuduongcong1320Ай бұрын
you can change the timezone and get the question bf your midnight
@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Ай бұрын
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.