Product of Array Except Self - Leetcode 238 - Arrays & Strings (Python)

Поделиться
HTML-код
  • Опубликовано: 8 янв 2024
  • The Python code for this problem can be found at my GitHub repo here: github.com/gahogg/Leetcode-So...
    Please check my playlists for free DSA problem solutions:
    • Array & String Questions
    • 2 Pointers Questions
    • Sliding Window Questions
    • Binary Search Questions
    • Stack Questions
    • Linked List Questions
    • Tree Questions
    • Heap Questions
    • Recursive Backtracking...
    • Graph Questions
    • Dynamic Programming (D...
    Learn Python and Data Science at mlnow.ai :)
    Best Courses for Analytics:
    ---------------------------------------------------------------------------------------------------------
    + IBM Data Science (Python): bit.ly/3Rn00ZA
    + Google Analytics (R): bit.ly/3cPikLQ
    + SQL Basics: bit.ly/3Bd9nFu
    Best Courses for Programming:
    ---------------------------------------------------------------------------------------------------------
    + Data Science in R: bit.ly/3RhvfFp
    + Python for Everybody: bit.ly/3ARQ1Ei
    + Data Structures & Algorithms: bit.ly/3CYR6wR
    Best Courses for Machine Learning:
    ---------------------------------------------------------------------------------------------------------
    + Math Prerequisites: bit.ly/3ASUtTi
    + Machine Learning: bit.ly/3d1QATT
    + Deep Learning: bit.ly/3KPfint
    + ML Ops: bit.ly/3AWRrxE
    Best Courses for Statistics:
    ---------------------------------------------------------------------------------------------------------
    + Introduction to Statistics: bit.ly/3QkEgvM
    + Statistics with Python: bit.ly/3BfwejF
    + Statistics with R: bit.ly/3QkicBJ
    Best Courses for Big Data:
    ---------------------------------------------------------------------------------------------------------
    + Google Cloud Data Engineering: bit.ly/3RjHJw6
    + AWS Data Science: bit.ly/3TKnoBS
    + Big Data Specialization: bit.ly/3ANqSut
    More Courses:
    ---------------------------------------------------------------------------------------------------------
    + Tableau: bit.ly/3q966AN
    + Excel: bit.ly/3RBxind
    + Computer Vision: bit.ly/3esxVS5
    + Natural Language Processing: bit.ly/3edXAgW
    + IBM Dev Ops: bit.ly/3RlVKt2
    + IBM Full Stack Cloud: bit.ly/3x0pOm6
    + Object Oriented Programming (Java): bit.ly/3Bfjn0K
    + TensorFlow Advanced Techniques: bit.ly/3BePQV2
    + TensorFlow Data and Deployment: bit.ly/3BbC5Xb
    + Generative Adversarial Networks / GANs (PyTorch): bit.ly/3RHQiRj
    Become a Member of the Channel! bit.ly/3oOMrVH
    Follow me on LinkedIn! / greghogg
    Full Disclosure:
    Please note that I may earn a commission for purchases made at the above sites! I strongly believe in the material provided; I only recommend what I truly think is great. If you do choose to make purchases through these links; thank you for supporting the channel, it helps me make more free content like this!

Комментарии • 16

  • @shreehari2589
    @shreehari2589 3 месяца назад +2

    The way you explain stuff is awesome, keep up the good work and please also upload hard questions too

  • @aimaalakhume
    @aimaalakhume 5 месяцев назад +1

    Thanks, I love this solution! It was really easy to follow and I appreciate the concise code. I also never knew the zip() function existed

  • @annas8308
    @annas8308 2 месяца назад +2

    Hi Greg! What is the app you use to draw and teach us? I find it very easy to use and would like to take notes while viewing your videos.

    • @GregHogg
      @GregHogg  2 месяца назад +1

      I use miro! It's really good for drawing:)

    • @annas8308
      @annas8308 2 месяца назад

      @@GregHogg Thank you, Greg!

  • @arsheyajain7055
    @arsheyajain7055 4 месяца назад +1

    Literally the only video that helped!!! Thank you

    • @GregHogg
      @GregHogg  4 месяца назад +1

      Glad to hear it 😎

  • @jarodlatta6185
    @jarodlatta6185 3 месяца назад

    I think you could get a constant time and space improvement by doing two passes through the array: first construct the product of all elements (unless they're 0), then iterate through again and divide the total by the current element. You do have go handle for 0s as a special case though, which might make it just as complex

    • @kiattim2100
      @kiattim2100 2 месяца назад

      You can't use division. That's the rule in leetcode 238

    • @jarodlatta6185
      @jarodlatta6185 2 месяца назад

      @@kiattim2100 ah okay, that makes sense

  • @SpaceTalon
    @SpaceTalon 5 месяцев назад +2

    Thanks!

    • @GregHogg
      @GregHogg  5 месяцев назад +1

      Very welcome :)

  • @Sanjay-bx6xb
    @Sanjay-bx6xb Месяц назад +1

    bro how do you come up with this logic

    • @GregHogg
      @GregHogg  Месяц назад +1

      Sometimes I might have figured it out, sometimes I might have just learned it elsewhere.

  • @arshiailaty
    @arshiailaty 5 месяцев назад

    Could you please let me know what you think of this solution? I should mention that there is a problem in the output when we have only one zero in numbers. The second if returns an array with all zeros.
    import numpy
    class Solution:
    def productExceptSelf(self, nums: List[int]) -> List[int]:
    ans = []
    ind = numpy.where(nums == 0)[0]
    if len(ind) > 1:
    return [0]*len(ind)
    if len(ind) == 1:
    i = ind[0]
    res = numpy.prod(nums[:i]) * numpy.prod(nums[i+1:])
    ans = [0]*len(nums)
    ans[i] = res
    return ans
    product = numpy.prod(nums)
    ans = [product // num for num in nums]
    return ans

    • @rishishah835
      @rishishah835 18 дней назад

      What's the need to import numpy, the product function? Also, we're not allowed to use division, otherwise we could just iterate once -- get the total product, and then iterate through just dividing by each value in nums (unless 0)