Leetcode - First Missing Positive (Python)

Sdílet
Vložit
  • čas přidán 29. 09. 2020
  • September 2020 Leetcode Challenge
    Leetcode - First Missing Positive
  • Věda a technologie

Komentáře • 16

  • @6014240
    @6014240 Před 3 lety

    Watching you build upon the previous solutions is what makes the "optimal" solution much easier to be understood. There's another approach that multiplies the "seen" numbers by -1 instead of adding the modulo.
    Thank you for your videos!

    • @timc3406
      @timc3406  Před 3 lety

      You're very welcome! Thanks so much for watching!

  • @lianghe1401
    @lianghe1401 Před 2 lety

    A beauty of this solution is using -1 index which is specific and perfect for Python!

  • @janmichaelaustria620
    @janmichaelaustria620 Před 3 lety

    thanks tim! really appreciate the explanation for the O(1) space! i was wondering if a solution like this is still 0(1) space:
    if not nums:
    return 1
    nums = [num for num in nums if num > 0]
    nums = set(nums)
    if not nums:
    return 1
    candidate = 1
    while candidate

    • @timc3406
      @timc3406  Před 3 lety

      I believe this takes O(N) space because of the nums=set(nums) operation, python needs space to generate that set but the extra candidate part is technically constant!

  • @lianghe1401
    @lianghe1401 Před 2 lety

    Thanks for the nice solution! I have a question about it. Based on the desc of the problem, nums[I] could go as high 2^32-1. So if adding those numbers with N, it would go beyond the MAX integer for many other languages such as Java. I know it is not an issue with Python, but would it still add storage (numbers from 32 bits to 64 bits), thus the memory complexity becomes o(N)?

  • @qazaqempire3828
    @qazaqempire3828 Před rokem +1

    how quickly did u derive the solution urself?

  • @rosefromdead
    @rosefromdead Před 3 měsíci

    what the fuck? How am i supposed to come up with this in the interview bruv?

  • @samarthasthana
    @samarthasthana Před 3 lety

    The last solution is so sneaky, its brilliant!

    • @timc3406
      @timc3406  Před 3 lety +1

      Agreed! Wish I could say I came up with it on my own :)

  • @laofengs
    @laofengs Před 3 lety +1

    There is a bug in you last solution : if nums[i] > 0: check should be : if nums[i]%N > 0:

  • @danielshea5453
    @danielshea5453 Před 3 lety +1

    Please do videos explaining different search algorithms

  • @oooo-rc2yf
    @oooo-rc2yf Před 2 lety

    If you include the question number you'd get better SEO as people copy and paste the whole question title into youtube

  • @othmanwatad9398
    @othmanwatad9398 Před 3 měsíci

    We scc o no
    Oo