My Favorite Stack Question! | Daily Temperatures - Leetcode 739

แชร์
ฝัง
  • เผยแพร่เมื่อ 23 ส.ค. 2024
  • leetcode, coding interview question, data structures, data structures and algorithms, faang

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

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

    Master Data Structures & Algorithms For FREE at AlgoMap.io!

  • @indranilghosh3635
    @indranilghosh3635 8 หลายเดือนก่อน +22

    well you use sliding window approach in this too

  • @art4eigen93
    @art4eigen93 8 หลายเดือนก่อน +7

    Stack always maintains the decreasing order .

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

      Monotonic! :)

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

    Iterating backwards can simplify it further

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

      How

    • @jz77096
      @jz77096 12 วันที่ผ่านมา

      Yup

    • @fastlaner7746
      @fastlaner7746 7 วันที่ผ่านมา

      that's how I just managed to solve it

  • @nimeshpareek953
    @nimeshpareek953 5 หลายเดือนก่อน +2

    Just solved this ques 1 min ago how this is in my shorts 👀👀

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

    I love JS' Array.reduce so much.

    • @GregHogg
      @GregHogg  8 หลายเดือนก่อน

      Can you use this here to pop off from the stack?

  • @deeptanshukumarbehera
    @deeptanshukumarbehera 8 หลายเดือนก่อน

    Nice question

  • @shekarvalluri6457
    @shekarvalluri6457 8 หลายเดือนก่อน

    What software do you use to make a video Greg

  • @BillNice
    @BillNice 8 หลายเดือนก่อน +2

    Just out curiosity, does using a stack provide any benefit as opposed to just using 2 nested loops where i visits the whole string and j always starts at i and counts until it finds a bigger number at which point it update an auxiliary array and moves to the next i?

    • @frolvanya
      @frolvanya 8 หลายเดือนก่อน +12

      Stack solution is O(n) instead of O(n^2)

    • @BillNice
      @BillNice 8 หลายเดือนก่อน

      @@frolvanya I am not familiar with python so the syntax was a bit confusing!
      Now I see it, Thanks

    • @Fazal828
      @Fazal828 8 หลายเดือนก่อน +1

      @BillNice Has nothing to do with Python. The explanation tells you its O(N) and it'll be like that in any language

    • @BillNice
      @BillNice 8 หลายเดือนก่อน

      @@Fazal828 read again what I said!
      -1 indexes on arrays is not something I was familiar with so that kind of confused me a bit , I had to look at it in a language that uses the stack in ways that are more familiar to me

    • @Onyaga
      @Onyaga 6 หลายเดือนก่อน

      This can become o(n) if instead of just moving to the next i you actually use the answer array to jump elements since the answer array will tell you how many days you can jump forward

  • @mathe42
    @mathe42 8 หลายเดือนก่อน

    So complex. Just iterate backwards through the list.
    In js Something like this:
    let currentMaxTemp
    let count = 0
    let first = true
    let out =[]
    for(let i= temps.length -1;i>=0;i--) {
    out.push(count)
    if (first || currentMaxTemp > temps[i]) {
    count = 0
    currentMaxTemp = temps[i]
    }
    first = false
    }
    console.log(out.reverse())

    • @darknight3613
      @darknight3613 8 หลายเดือนก่อน

      Just remember to assing -∞ to currentMaxTemp so when its comparing itself to other temps it has a value

    • @Schnorzel1337
      @Schnorzel1337 8 หลายเดือนก่อน +2

      Your code is clearly wrong.
      You never increase count so the output is [0,0,0,0,0]
      if we add count++ after the if, your code still produces a wrong result.
      --
      If we look at [1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2]
      we get [ 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1, 0 ]
      Great job.

    • @Schnorzel1337
      @Schnorzel1337 8 หลายเดือนก่อน

      This is javascript you can compare undefined to a number. Hell you can compare undefined to string and it gives an answer.@@darknight3613

  • @ShorlanTanzo
    @ShorlanTanzo 8 หลายเดือนก่อน +6

    Such a useless problem.

    • @Fazal828
      @Fazal828 8 หลายเดือนก่อน +1

      Lol is it? You can use the same pattern in solving the "Best time to buy and sell stock" problem so I wouldn't call it useless

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

      This man has 0 imagination and creativity calling this problem useless

  • @johnandrea2111
    @johnandrea2111 8 หลายเดือนก่อน

    Eshew complex code. Instead
    Result = []
    For i, x in enumerate( data[:-1] ):
    Found = 0
    For j, y in enumerate( data[i+1:] ):
    If y > x:
    Found = j + 1
    Break
    Result.append( found )
    Result.append( 0 )
    Print( result )