Dashboard Temp Share Shortlinks Frames API

HTMLify

stock-span-problem.py
Views: 33 | Author: prakhardoneria
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
class Solution:
    def calculateSpan(self, arr):
        n = len(arr)
        span = [0] * n
        stack = []
        
        for i in range(n):
            while stack and arr[stack[-1]] <= arr[i]:
                stack.pop()
            
            if not stack:
                span[i] = i + 1
            else:
                span[i] = i - stack[-1]
            
            stack.append(i)
            
        return span