Number of Steps to Reduce a Number in Binary Representation to One - Leetcode 1404 - Python

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 ก.ย. 2024

ความคิดเห็น • 57

  • @yogeshvanzara5553
    @yogeshvanzara5553 3 หลายเดือนก่อน +4

    17:47
    today's Quote :-
    I know this part is very confusing when you think about it , so best thing to do just not to think about it 🙂

  • @bst-vf5su
    @bst-vf5su 3 หลายเดือนก่อน

    For the first solution, are we able to factor in the "out of bound conditions" in the while loop?

  • @solaimanjawad5015
    @solaimanjawad5015 3 หลายเดือนก่อน +1

    Collatz conjecture for the win haha. The odd operation might not be exact but the derivative still follows similarly

    • @zweitekonto9654
      @zweitekonto9654 3 หลายเดือนก่อน

      Is it still a conjecture?

    • @asagiai4965
      @asagiai4965 3 หลายเดือนก่อน

      nice observation

  • @chien-yuyeh9386
    @chien-yuyeh9386 3 หลายเดือนก่อน +1

    I like this question, pretty interesting

  • @bhavasagar977
    @bhavasagar977 3 หลายเดือนก่อน

    Thank you for the explanation... ❤

  • @EduarteBDO
    @EduarteBDO 3 หลายเดือนก่อน

    if you convert the string into a deque at the start the simulation solution will be o(n) too, but the memory will be o(n)

  • @CS_n00b
    @CS_n00b 3 หลายเดือนก่อน

    i dont really get why the first is n^2? you mentioned that string manipulation is o(n) but we are working with lists?

  • @InquisitiveLittleSteps
    @InquisitiveLittleSteps 3 หลายเดือนก่อน

    Thank you.. this is a nice approach

  • @user-vk1tc6cu5t
    @user-vk1tc6cu5t 3 หลายเดือนก่อน +2

    Pleasre Reply
    why dont't we Just convert the binary to decimal then check for odd event and count the steps

    • @PhanNghia-fk5rv
      @PhanNghia-fk5rv 3 หลายเดือนก่อน

      it's has 500 length string

    • @NeetCodeIO
      @NeetCodeIO  3 หลายเดือนก่อน +3

      it works for python but not for other languages

  • @ap2s2000
    @ap2s2000 3 หลายเดือนก่อน +1

    I love leetcode

    • @jayberry6557
      @jayberry6557 3 หลายเดือนก่อน

      Stockholm syndrome 😂

  • @aakashs1806
    @aakashs1806 3 หลายเดือนก่อน

    I got one problem in one interview on binary tree permutation
    To create all possible combinations of numbers adding the right side of tree, you have to swap the left and right child and create these combinations and store in a array

    • @DJSTEVE42
      @DJSTEVE42 3 หลายเดือนก่อน

      Do you have the LC link for it ?

  • @karthiksuryadevara2546
    @karthiksuryadevara2546 3 หลายเดือนก่อน +4

    Why not convert the binary to a number and do the simulation? Wouldn't that be simpler

    • @jayberry6557
      @jayberry6557 3 หลายเดือนก่อน +3

      I think you may run into overflow if the number is very very big

    • @premsinghrathore9691
      @premsinghrathore9691 3 หลายเดือนก่อน +1

      @@jayberry6557 Not in Python apparently

    • @jagrutitiwari2551
      @jagrutitiwari2551 3 หลายเดือนก่อน

      I tried the same. One of my test cases failed. I don't know why.

    • @sdemji
      @sdemji 3 หลายเดือนก่อน

      Used bigint and worked fine

    • @karthiksuryadevara2546
      @karthiksuryadevara2546 3 หลายเดือนก่อน

      @@sdemji can you please share the solution here

  • @shivangikhemka4379
    @shivangikhemka4379 3 หลายเดือนก่อน +1

    Can't we just convert the binary number to decimal and then run while loop? It will come as O(N) right?

    • @vikashkotteeswaran700
      @vikashkotteeswaran700 3 หลายเดือนก่อน +1

      We could but in this case binary rep could be of length 500 even I suppose. Can’t convert that to a single decimal.

  • @sri_harsha_dv
    @sri_harsha_dv 3 หลายเดือนก่อน +1

    We can do xor operation instead of finding the actual value in 2nd solution.
    if (s[i] == "1") ^ carry:
    steps += 2
    carry = 1
    else:
    steps += 1

    • @pastori2672
      @pastori2672 3 หลายเดือนก่อน +2

      that's literally the exact same solution except a bit harder if you're not familiar with xor. so he's solution simpler.

  • @Uwgsu8
    @Uwgsu8 3 หลายเดือนก่อน

    I love leetcode

  • @devin9544
    @devin9544 3 หลายเดือนก่อน

    why do u stop before the first element?

  • @satyamjha68
    @satyamjha68 3 หลายเดือนก่อน

    Solved it!

  • @ish90917
    @ish90917 3 หลายเดือนก่อน

    What if in simulation solution we convert the binary string into an integer. Will it still be an O(n**2) times solution or will it become O(n) ?

    • @MykolaPavluchynskyi
      @MykolaPavluchynskyi 3 หลายเดือนก่อน

      500 digits (from constraints) cannot fit integer

  • @asagiai4965
    @asagiai4965 3 หลายเดือนก่อน

    This doesn't relate directly to the code, but isn't the output explanation sounds like a form of Collatz Conjecture?

  • @jakkaabhinav4859
    @jakkaabhinav4859 3 หลายเดือนก่อน

    Nice❤

  • @rabbyhossain6150
    @rabbyhossain6150 3 หลายเดือนก่อน

    Isn't it a O(n) solution?
    class Solution:
    def numSteps(self, s: str) -> int:
    decimal = int(s, 2) # convert to decimal
    steps = 0
    while True:
    if decimal == 1:
    return steps
    if decimal % 2 == 0:
    decimal = decimal // 2
    else:
    decimal += 1
    steps += 1

    • @user-yj2ju9up8o
      @user-yj2ju9up8o 3 หลายเดือนก่อน

      I suppose, you wouldn't be allowed to convert input string to an integer in a real interview. Because it dramatically simplifies the task.

    • @rabbyhossain6150
      @rabbyhossain6150 3 หลายเดือนก่อน

      @@user-yj2ju9up8o true but it's not mentioned on the problem

  • @s8x.
    @s8x. 3 หลายเดือนก่อน

    do u do this everyday? or are these old recordings

    • @adityamwagh
      @adityamwagh 3 หลายเดือนก่อน

      He does the daily questions everyday and then posts the solutions if he hasn’t posted one already.

  • @adityamwagh
    @adityamwagh 3 หลายเดือนก่อน

    Hey everyone, welcome back and let’s write some NeetCode today!

    • @shahzodshafizod
      @shahzodshafizod 3 หลายเดือนก่อน +1

      some more neetcode?

  • @chrischika7026
    @chrischika7026 3 หลายเดือนก่อน +2

    lol too easy
    steps = 0
    num = int(s, 2)
    while num != 1:
    if num % 2 == 0:
    num //= 2
    elif num % 2 == 1:
    num += 1
    steps += 1
    return steps
    /s

    • @Munchen888
      @Munchen888 3 หลายเดือนก่อน

      The same solution without bit manipulations )

    • @Munchen888
      @Munchen888 3 หลายเดือนก่อน

      Here you can in now time count += 1 and use if / else

    • @hawkinlock
      @hawkinlock 3 หลายเดือนก่อน

      num //= 2 could be replaced with num /= 2 since it's always going to be applied to even numbers as per the problem's premise.
      You could even replace it with num >>=2 if you wanna be fancy.

    • @MohitKarangiya
      @MohitKarangiya 3 หลายเดือนก่อน

      Input has 500 length string. Will work for only for python but not for other languages

    • @billiejean1304
      @billiejean1304 3 หลายเดือนก่อน

      I know you should use the tools you have to the fullest but this solution misses the whole point of the exercise

  • @adityarajora7219
    @adityarajora7219 3 หลายเดือนก่อน

    your videos are getting big, please do not do that...

    • @JamesLi-nx9lp
      @JamesLi-nx9lp 3 หลายเดือนก่อน +7

      disagree, some people would like to explore different solutions and the intuition behind each

    • @protodimbo
      @protodimbo 3 หลายเดือนก่อน +2

      There are time codes in the video. You can also stop at the first solution. For example, I want to see different solutions, and I’m thankful to Navdeep for that possibility

    • @michael._.
      @michael._. 3 หลายเดือนก่อน +2

      the bigger the better