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?
@@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
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
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())
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.
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 )
Master Data Structures & Algorithms For FREE at AlgoMap.io!
well you use sliding window approach in this too
Stack always maintains the decreasing order .
Monotonic! :)
Iterating backwards can simplify it further
How
Yup
that's how I just managed to solve it
Just solved this ques 1 min ago how this is in my shorts 👀👀
I love JS' Array.reduce so much.
Can you use this here to pop off from the stack?
Nice question
What software do you use to make a video Greg
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?
Stack solution is O(n) instead of O(n^2)
@@frolvanya I am not familiar with python so the syntax was a bit confusing!
Now I see it, Thanks
@BillNice Has nothing to do with Python. The explanation tells you its O(N) and it'll be like that in any language
@@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
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
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())
Just remember to assing -∞ to currentMaxTemp so when its comparing itself to other temps it has a value
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.
This is javascript you can compare undefined to a number. Hell you can compare undefined to string and it gives an answer.@@darknight3613
Such a useless problem.
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
This man has 0 imagination and creativity calling this problem useless
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 )
tle dude