The stock span problem is a financial problem where we have a series of n daily price quotes for a stock and we need to calculate the span of stocks price for all n days. 
The span S_{i} of the stocks price on a given day i is defined as the maximum number of consecutive days just before the given day, for which the price of the stock on the current day is less than or equal to its price on the given day.
For example, if an array of 7 days prices is given as {100, 80, 60, 70, 60, 75, 85}, then the span values for corresponding 7 days are {1, 1, 1, 2, 1, 4, 6}.
Example 1:
Input: 
N = 7, price[] = [100 80 60 70 60 75 85]
Output:
1 1 1 2 1 4 6
Explanation:
Traversing the given input span for 100 
will be 1, 80 is smaller than 100 so the 
span is 1, 60 is smaller than 80 so the 
span is 1, 70 is greater than 60 so the 
span is 2 and so on. Hence the output will 
be 1 1 1 2 1 4 6.
Example 2:
Input: 
N = 6, price[] = [10 4 5 90 120 80]
Output:
1 1 2 4 5 1
Explanation:
Traversing the given input span for 10 
will be 1, 4 is smaller than 10 so the 
span will be 1, 5 is greater than 4 so 
the span will be 2 and so on. Hence, the 
output will be 1 1 2 4 5 1.
User Task:
The task is to complete the function calculateSpan() which takes two parameters, an array price[] denoting the price of stocks, and an integer N denoting the size of the array and number of days. This function finds the span of stock's price for all N days and returns an array of length N denoting the span for the i-th day.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ C[i] ≤ 10^{5}

Difficulty: MEDIUM
Tags: Data structures
Skill types: Data structures
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).

Starter code:
#User function Template for python3





class Solution:

    

    #Function to calculate the span of stockâ€™s price for all n days.

    def calculateSpan(self,a,n):

        #code here

Solutions:
class Solution:

	def calculateSpan(self, a, n):
		stack = []
		ans = []
		i = 0
		while i < n:
			while len(stack) > 0 and stack[-1][1] <= a[i]:
				stack.pop()
			if len(stack) > 0:
				difference = i - stack[-1][0]
				ans.append(difference)
			else:
				ans.append(i + 1)
			stack.append((i, a[i]))
			i += 1
		return ans


class Solution:

	def calculateSpan(self, a, n):
		ans = [1] * n
		stack = [0]
		for i in range(1, n):
			while len(stack) > 0 and a[stack[-1]] <= a[i]:
				stack.pop()
			ans[i] = i + 1 if not stack else i - stack[-1]
			stack.append(i)
		return ans


class Solution:

	def calculateSpan(self, arr, n):
		stack = []
		out = []
		for i in range(n):
			while len(stack) > 0 and arr[stack[-1]] <= arr[i]:
				stack.pop()
			if stack == []:
				out.append(i + 1)
			else:
				out.append(i - stack[-1])
			stack.append(i)
		return out


class Solution:

	def calculateSpan(self, a, n):
		stack = []
		ans = [1] * n
		for cur in range(n):
			while stack and a[cur] >= a[stack[-1]]:
				stack.pop()
			if stack:
				ans[cur] = cur - stack[-1]
			else:
				ans[cur] = cur + 1
			stack.append(cur)
		return ans


class Solution:

	def calculateSpan(self, a, n):
		S = [0 for i in range(n + 1)]
		st = []
		st.append(0)
		S[0] = 1
		for i in range(1, n):
			while len(st) > 0 and a[st[-1]] <= a[i]:
				st.pop()
			S[i] = i + 1 if len(st) <= 0 else i - st[-1]
			st.append(i)
		return S[0:n]


class Solution:

	def calculateSpan(self, arr, n):
		st = [(arr[0], 0)]
		ans = [-1 for i in range(n)]
		for i in range(1, n):
			if arr[i] >= st[-1][0]:
				while len(st) > 0 and arr[i] >= st[-1][0]:
					st.pop()
				if len(st) != 0:
					ans[i] = st[-1][1]
				st.append((arr[i], i))
			else:
				ans[i] = st[-1][1]
				st.append((arr[i], i))
		for i in range(n):
			ans[i] = i - ans[i]
		return ans


class Solution:

	def calculateSpan(self, price, N):
		stack = []
		span = [0] * N
		for i in range(N):
			while stack and price[stack[-1]] <= price[i]:
				stack.pop()
			span[i] = i + 1 if len(stack) == 0 else i - stack[-1]
			stack.append(i)
		return span


class Solution:

	def calculateSpan(self, a, n):
		stack = []
		l = []
		c = 1
		for i in range(n):
			if len(stack) == 0:
				l.append(1)
			elif a[i] < stack[-1][0]:
				l.append(i - stack[-1][1])
			else:
				while len(stack) != 0 and a[i] >= stack[-1][0]:
					stack.pop()
				if len(stack) == 0:
					l.append(i + 1)
				elif len(stack) != 0 and a[i] < stack[-1][0]:
					l.append(i - stack[-1][1])
			stack.append([a[i], i])
		return l


class Solution:

	def calculateSpan(self, arr, n):
		stack = [-1]
		ans = []
		for i in range(n):
			while len(stack) > 1 and arr[stack[-1]] <= arr[i]:
				stack.pop()
			ans.append(i - stack[-1])
			stack.append(i)
		return ans


class Solution:

	def calculateSpan(self, a, n):
		st = []
		ans = []
		for i in range(n):
			count = 1
			while len(st) != 0 and st[-1][0] <= a[i]:
				count += st[-1][1]
				st.pop()
			ans.append(count)
			st.append((a[i], count))
		return ans


class Solution:

	def calculateSpan(self, arr, n):
		inter = [1]
		ans = [1]
		for i in range(1, n):
			count = 1
			r = i - 1
			while r >= 0:
				if arr[r] > arr[i]:
					break
				count += ans[r]
				r -= ans[r]
			ans.append(count)
		return ans


class Solution:

	def calculateSpan(self, a, n):
		res = []
		s = []
		for i in range(n):
			count = 0
			if len(s) == 0:
				count = count + 1
				res.append(count)
			elif a[i] < s[-1][0]:
				count = count + 1
				res.append(count)
			elif a[i] >= s[-1][0]:
				while len(s) != 0 and s[-1][0] <= a[i]:
					count = count + s[-1][1]
					s.pop()
				count = count + 1
				res.append(count)
			s.append([a[i], count])
		return res


class Solution:

	def calculateSpan(self, prices, n):
		stack = []
		span = [1] * n
		for i in range(n):
			while stack and prices[stack[-1]] <= prices[i]:
				stack.pop()
			if not stack:
				span[i] = i + 1
			else:
				span[i] = i - stack[-1]
			stack.append(i)
		return span


class Solution:

	def calculateSpan(self, a, n):
		ans = [0] * n
		stack = []
		for i in range(n - 1, -1, -1):
			ans[i] = i + 1
			while stack and a[stack[-1]] < a[i]:
				ind = stack[-1]
				ans[ind] = ind - i
				stack.pop()
			stack.append(i)
		return ans


class Solution:

	def calculateSpan(self, a, n):
		stack = []
		result = []
		for i in range(n):
			while stack and a[i] >= stack[-1][0]:
				stack.pop()
			result.append(i - stack[-1][1] if stack else i + 1)
			stack.append((a[i], i))
		return result


import copy

class Solution:

	def calculateSpan(self, price, n):
		st = [0]
		S = [1]
		for i in range(1, n):
			while len(st) > 0 and price[st[-1]] <= price[i]:
				st.pop()
			S.append(i + 1 if len(st) == 0 else i - st[-1])
			st.append(i)
		return S


class Solution:

	def calculateSpan(self, a, n):
		stk = []
		stk.append(0)
		res = [0] * n
		res[0] = 1
		for i in range(1, n):
			while stk and a[stk[-1]] <= a[i]:
				stk.pop()
			if not stk:
				res[i] = i + 1
			else:
				res[i] = i - stk[-1]
			stk.append(i)
		return res


class Solution:

	def calculateSpan(self, arr, n):
		temp = [1] * n
		stack = []
		for i in reversed(range(len(arr))):
			while stack and arr[stack[-1]] < arr[i]:
				x = stack.pop()
				temp[x] = x - i
			stack.append(i)
		for i in stack:
			temp[i] += i
		return temp


class Solution:

	def calculateSpan(self, a, n):
		st = []
		span = []
		for i in range(n):
			if not st:
				st.append(i)
			if i == 0:
				span.append(1)
			else:
				while st and a[st[-1]] <= a[i]:
					st.pop()
				if st:
					span.append(i - st[-1])
					st.append(i)
				else:
					span.append(i + 1)
					st.append(i)
		return span


class Solution:

	def calculateSpan(self, a, n):
		stack = [0]
		days = [1]
		stock = [1] * n
		for i in range(1, n):
			stock_days = 1
			while stack:
				if a[i] >= a[stack[-1]]:
					stack.pop()
					stock_days += days.pop()
				else:
					break
			stock[i] = stock_days
			stack.append(i)
			days.append(stock_days)
		return stock


class Solution:

	def calculateSpan(self, a, n):
		a = a[::-1]
		w = [-1 for i in range(n)]
		q = [[a[0], 0]]
		i = 1
		while i < n:
			if len(q) == 0 or q[-1][0] >= a[i]:
				q.append([a[i], i])
				i += 1
			else:
				(x, y) = q.pop()
				w[y] = i - y
		while q:
			(x, y) = q.pop()
			w[y] = n - y
		w[-1] = 1
		return w[::-1]


class Solution:

	def calculateSpan(self, a, n):
		if not a:
			return []
		stack = []
		stock_span = []
		for index in range(n):
			if len(stack) == 0:
				stack.append(index)
				stock_span.append(1)
			else:
				top_index = stack[-1]
				if a[top_index] > a[index]:
					stock_span.append(index - top_index)
					stack.append(index)
				else:
					top = a[stack[-1]]
					while top <= a[index]:
						stack.pop()
						if len(stack) == 0:
							break
						top = a[stack[-1]]
					if len(stack) == 0:
						ans = index + 1
					else:
						ans = index - stack[-1]
					stock_span.append(ans)
					stack.append(index)
		return stock_span


class Node:

	def __init__(self, value, index):
		self.value = value
		self.index = index

class Solution:

	def calculateSpan(self, a, n):
		st = []
		res = [1] * n
		st.append(0)
		i = 1
		while i < n:
			top = a[st[-1]]
			while a[i] >= top:
				st.pop()
				if len(st) != 0:
					top = a[st[-1]]
				else:
					break
			res[i] = i - st[-1] if len(st) != 0 else i + 1
			st.append(i)
			i = i + 1
		return res


from queue import LifoQueue
from collections import deque

class Solution:

	def calculateSpan(self, a, n):
		stack = []
		span = []
		for i in range(0, n):
			while stack != [] and a[stack[-1]] <= a[i]:
				stack.pop()
			if stack == []:
				span.append(i + 1)
			else:
				top = stack[-1]
				span.append(i - top)
			stack.append(i)
		return span


class Solution:

	def calculateSpan(self, a, n):
		s = []
		s1 = []
		s.append(0)
		s1.append(1)
		for i in range(1, n):
			while len(s) != 0 and a[i] >= a[s[-1]]:
				s.pop()
			if len(s) == 0:
				s1.append(i + 1)
			else:
				s1.append(i - s[-1])
			s.append(i)
		return s1


class Solution:

	def calculateSpan(self, a, n):
		s = []
		ans = []
		for i in range(0, n):
			while s != [] and a[s[-1]] <= a[i]:
				s.pop()
			if s == []:
				ans.append(i + 1)
			else:
				top = s[-1]
				ans.append(i - top)
			s.append(i)
		return ans


class Solution:

	def calculateSpan(self, a, n):
		stack = []
		ans = []
		for i in range(n):
			if len(stack) == 0 or a[i] < stack[-1][0]:
				stack.append((a[i], 1))
				ans.append(1)
			else:
				curr = 1
				while a[i] >= stack[-1][0]:
					curr += stack[-1][1]
					stack.pop()
					if len(stack) == 0:
						break
				stack.append((a[i], curr))
				ans.append(curr)
		return ans


class Solution:

	def calculateSpan(self, a, n):
		st = []
		ans = []
		for i in range(n):
			span = 1
			price = a[i]
			while st and price >= st[-1][0]:
				span += st.pop()[1]
			st.append((price, span))
			ans.append(span)
		return ans


class Solution:

	def calculateSpan(self, a, n):
		ans = []
		stack = []
		for i in range(n):
			if stack == []:
				ans.append(1)
				stack.append(i)
				continue
			num = 0
			if a[stack[-1]] > a[i]:
				ans.append(1)
				stack.append(i)
				continue
			try:
				while True:
					if a[stack[-1]] > a[i]:
						ans.append(i - stack[-1])
						stack.append(i)
						break
					else:
						stack.pop()
						num += 1
			except:
				ans.append(i + 1)
				stack.append(i)
		return ans


class Solution:

	def calculateSpan(self, a, n):

		def calculate_span(prices):
			n = len(prices)
			stack = []
			span = [0] * n
			span[0] = 1
			stack.append(0)
			for i in range(1, n):
				while len(stack) > 0 and prices[stack[-1]] <= prices[i]:
					stack.pop()
				if len(stack) == 0:
					span[i] = i + 1
				else:
					span[i] = i - stack[-1]
				stack.append(i)
			return span
		return calculate_span(a)


class Solution:

	def calculateSpan(self, arr, n):
		stk = []
		ans = []
		for i in range(n):
			if len(stk) == 0:
				ans.append(max(i - 0 + 1, 1))
			elif arr[stk[-1]] <= arr[i]:
				while stk and arr[stk[-1]] <= arr[i]:
					stk.pop(-1)
				if len(stk) == 0:
					ans.append(max(i - 0 + 1, 1))
				else:
					ans.append(i - stk[-1])
			else:
				ans.append(i - stk[-1])
			stk.append(i)
		return ans


class Solution:

	def calculateSpan(self, a, n):
		out = [0]
		for i in range(len(a)):
			j = i - 1
			c = 1
			while j > -1 and a[j] <= a[i]:
				c += out[j + 1]
				j -= out[j + 1]
			out.append(c)
		return out[1:]


class Solution:

	def calculateSpan(self, a, n):
		ans = []
		s = []
		for i in range(n):
			if not s:
				ans.append(-1)
			elif s and s[-1][0] > a[i]:
				ans.append(s[-1][1])
			else:
				while s and s[-1][0] <= a[i]:
					s.pop()
				if not s:
					ans.append(-1)
				else:
					ans.append(s[-1][1])
			s.append((a[i], i))
		for j in range(n):
			ans[j] = j - ans[j]
		return ans


class Solution:

	def calculateSpan(self, a, n):
		stack = []
		arr_span = []
		for i in range(n):
			span = 1
			while stack and stack[-1][0] <= a[i]:
				span += stack[-1][1]
				stack.pop()
			stack.append([a[i], span])
			arr_span.append(span)
		return arr_span


from collections import deque

class Solution:

	def calculateSpan(self, arr, n):
		stack = deque()
		stack = [(0, 0)]
		res = []
		ans = [0] * n
		for i in range(n):
			if len(stack) == 0:
				res.append(-1)
			elif len(stack) > 0 and arr[i] >= stack[-1][0]:
				while len(stack) > 0 and arr[i] >= stack[-1][0]:
					stack.pop()
				if len(stack) == 0:
					res.append(-1)
				else:
					res.append(stack[-1][1])
			elif len(stack) > 0 and arr[i] < stack[-1][0]:
				res.append(stack[-1][1])
			stack.append((arr[i], i))
		for i in range(n):
			ans[i] = i - res[i]
		return ans