Stack 1 Self
Stack 1 Self
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
def top(self) :
if self.is_empty() :
print("Stack is empty nothing is on the top")
return
return self.__stack[-1]
def pop(self) :
if self.is_empty() :
print("Stack is empty cannot pop from empty stack")
return
return self.__stack.pop()
In [ ]:
In [ ]:
class stackUsingLL() :
def __init__(self) :
self.head = None
self.size = 0
def push(data) :
newNode = Node(data)
if self.head == None :
self.head = newnode
self.size += 1
return f"added {data} to the stack"
else :
newNode.next = self.head
self.head = newNode
return f"added {data} to the stack"
def top(self) :
def pop(self) :
if self.head is None or self.size == 0 :
return "cannot pop on value from empty stack"
ans = self.head.data
self.head = self.head.next
self.size -= 1
return ans
def is_empty(self) :
return self.size == 0
def lengthLL(self) :
return self.size
In [22]: mystack = stackUsingLL()
In [23]: print(mystack.is_empty())
print(mystack.lengthLL())
print(mystack.push(10))
print(mystack.push(20))
print(mystack.push(30))
print(mystack.push(40))
print(mystack.is_empty())
print(mystack.pop())
print(mystack.pop())
print(mystack.size())
print(mystack.top())
True
0
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[23], line 3
1 print(mystack.is_empty())
2 print(mystack.lengthLL())
----> 3 print(mystack.push(10))
4 print(mystack.push(20))
5 print(mystack.push(30))
In [ ]:
if (s.is_empty()) :
ans.insert(0,-1)
s.push(a[n - i - 1])
return ans
pass
for i in range(len(heights)) :
left = i
right = i
while left >= 0 and heights[left] >= heights[i] :
left -= 1
while right < len(heights) and heights[right] >= heights[i] :
right += 1
areas.append((right - left - 1 ) * heights[i])
return max(areas)
pass
Out[12]: 10
def prev_smalller_index(arr) :
ps = []
stack = Stack()
for i in range(len(arr)) :
while stack.is_empty == False :
if stack.top() > arr[i] :
stack.pop()
else :
ps.append(i)
stack.push(arr[i])
if stack.is_empty() :
stack.push(arr[i])
ps.append(-1)
return ps
def next_smaller_index(arr) :
ns = []
stack = Stack()
for i in range(len(arr)) :
while stack.is_empty == False :
if stack.top() > arr[len(arr) - i - 1] :
stack.pop()
else :
ns.insert(0,i)
stack.push(arr[len(arr) - i - 1])
if stack.is_empty() :
ns.insert(0, len(arr))
stack.push(arr[len(arr) - i - 1])
return ns
def largestRectangleArea(heights):
# Implement your solution here
# ******************************************************************************
# for i in range(len(heights)) :
# left = i
# right = i
# while left >= 0 and heights[left] >= heights[i] :
# left -= 1
# while right < len(heights) and heights[right] >= heights[i] :
# right += 1
# areas.append((right - left - 1 ) * heights[i])
# return max(areas)
# ********************************************************************************
ps = prev_smalller_index(heights)
ns = next_smaller_index(heights)
print(ps)
print(ns)
max_area = 0
for i in range(heights) :
pass
[-1]
[6]
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[16], line 1
----> 1 largestRectangleArea( [2, 1, 5, 6, 2, 3] )
def prev_smalller_index(arr) :
ps = []
stack = Stack()
for i in range(len(arr)) :
while stack.is_empty == False :
return ps
[2]
Out[25]: [-1]
In [ ]:
In [ ]:
In [ ]: