class Stack: def__init__(self): self.items = [] #Check to see if the array is empty def is_empty(self): if not self.items: return False else: return True #Push a value to the stack itself def push(self, items): self.items.append(items) #Pop, remove a value off of the stack def pop(self): return self.items.pop() #Returning the index of the item in the array def peek(self): return self.items[len(self.items)-1] #Size, checking the value of the def size(self): return lens(self.items) def computeSpans2(P): S=[] D= Stack() for i in range(len(P)): h = 0 done = False while not (D.is_empty() or done): if P[i] >= P[D.peek()]: D.pop() else: done = True if D.is_empty(): h = -1 else: h = D.peek() S.append(i-h) D.push(i) return S, end prices=[4,3,1,2,1,3,7] print(computeSpans2(prices))
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question