Pascal's Triangle II - Leetcode 119 - Python

แชร์
ฝัง
  • เผยแพร่เมื่อ 3 ธ.ค. 2024

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

  • @m.kamalali
    @m.kamalali ปีที่แล้ว +6

    We can do the follow up using only 1 array by updating the array backward
    row=[1]
    for i in range(rowIndex):
    row.append(1)
    for j in range(i,0,-1):
    row[j]+=row[j-1]
    return row

    • @commonguy7
      @commonguy7 4 หลายเดือนก่อน +2

      fkin genious bro

  • @VidyaBhandary
    @VidyaBhandary ปีที่แล้ว +1

    A Q&A video and two problems .. you are on a roll. Thanks for making these videos !

  • @YehorKozyr
    @YehorKozyr ปีที่แล้ว +5

    This solution works, but it doesn't cover the mathematical meaning of Pascal's triangle and therefore can be simplified into a time and space O(rowIndex) function with a calculation of binomial coefficients.

    • @pbenikovszky1
      @pbenikovszky1 2 หลายเดือนก่อน

      sure, and the cpu will easily handle rowindex 1000 :D

    • @sickboydroid
      @sickboydroid 4 วันที่ผ่านมา

      @@pbenikovszky1 there is a easier way to calculate binomial coefficients without calculating factorials. Here is js version:
      function nCr(n, r) {
      if(r === 0 || n === r) return 1;
      if(r > n/2) r = n-r;
      let res = 1;
      for(let i = 1; i

  • @rajanmaheshwari
    @rajanmaheshwari ปีที่แล้ว +2

    Just did this few hours back 😃

  • @ashesh8085
    @ashesh8085 5 หลายเดือนก่อน

    My approach to this problem was to try to figure out a function such that:
    f(n, i) = ans[i] ;
    Meaning there would be a function which when given the rowIndex would be able to calculate the value of i th element in the row. Does a function like this exist? Is it even possible? This to me would seem much more efficient.
    Here are some examples of this functions:
    f(5,5) = 1
    f(4,2) = 6
    f(5,2) = 10
    f(5,3) = 10
    f(n, 0) = 1
    f(n, n) = 1 .... etc.
    Update: This is the binomial coefficient function

  • @Mercer80
    @Mercer80 ปีที่แล้ว +1

    C++
    class Solution {
    public:
    vector getRow(int rowIndex) {
    vectorprev(rowIndex+1);
    for(int r=0;r

  • @krateskim4169
    @krateskim4169 ปีที่แล้ว

    Awesome solution

  • @xaceffulgent
    @xaceffulgent ปีที่แล้ว

    Discovered this channel recently and am learning a lot! Thank you! Beginner question: why do you put your sol in a class?

    • @chuckle_pugz96
      @chuckle_pugz96 ปีที่แล้ว

      'cause leetcode is passing all the inputs to this class in the background. Though some sites just use a function.

  • @anjalivas1111
    @anjalivas1111 4 หลายเดือนก่อน

    hi @NeetCodeIO code isn't working for current format of the question Leetcode 119. Code returns [1] always. Can you please check? Thank you

    • @AustinWeeks
      @AustinWeeks 2 หลายเดือนก่อน

      my friend you have to figure it out on your own sometimes

  • @infoknow3278
    @infoknow3278 ปีที่แล้ว +1

    Easiest to come up with
    class Solution:
    def getRow(self, rowIndex: int) -> List[int]:
    res = [[1]]
    for i in range(rowIndex):
    tmp = [0] + res[-1] + [0]
    row = []
    for j in range(len(res[-1])+1):
    row.append(tmp[j]+tmp[j+1])
    res.append(row)
    return res[rowIndex]

  • @reqzonegaming7157
    @reqzonegaming7157 ปีที่แล้ว +1

    Please Bro
    Make video for
    2905. Find Indices With Index and Value Difference II

    • @MrBomb72
      @MrBomb72 ปีที่แล้ว

      There’s a good quality one here: th-cam.com/video/IL1PB8jnfhE/w-d-xo.htmlfeature=shared (found this from the top rated post under the solutions tab)

  • @sickboydroid
    @sickboydroid 4 วันที่ผ่านมา

    for math nerds:
    ----------------------------------------------------------------
    TC: O(rowIndex)
    SC: O(rowIndex)
    ----------------------------------------------------------------
    var getRow = function(rowIndex) {
    if(rowIndex === 0) return [1];
    const row = new Array(rowIndex+1);
    for(let i = 0; i n/2) r = n-r;
    let res = 1;
    for(let i = 1; i