Skip to content

Instantly share code, notes, and snippets.

@ZhouYang1993
ZhouYang1993 / LeetCode 844 Backspace String Compare.py
Created May 8, 2020 01:13
Algorithms for Interview 1: Stack
class Solution:
def backspaceCompare(self, S: str, T: str) -> bool:
def get_stack(string: str):
stack = []
for s in string:
if s != '#':
stack.append(s)
else:
if stack:
stack.pop()
class Solution:
def removeDuplicates(self, S: str) -> str:
stack = []
for char in S:
if (not stack) or (stack[-1]!=char):
stack.append(char)
else:
while stack and stack[-1] == char:
stack.pop()
return ''.join(stack)
@ZhouYang1993
ZhouYang1993 / example3.py
Created May 8, 2020 01:25
Algorithms for Interview 1: Stack
class Solution:
def removeDuplicates(self, S: str) -> str:
stack = []
for char in S:
if (not stack) or (stack[-1]!=char):
stack.append(char)
else:
while stack and stack[-1] == char:
stack.pop()
return ''.join(stack)
@ZhouYang1993
ZhouYang1993 / example1.py
Created May 8, 2020 02:36
Monotonic Stack
class Solution:
def largestRectangleArea(self, heights: List[int]) -> int:
ret = 0
mono_stack = []
heights.append(0)
for i, v in enumerate(heights):
while mono_stack and heights[mono_stack[-1]] > v:
height = heights[mono_stack[-1]]
length = i - mono_stack[-1]
ret = max(ret, height * length)
@ZhouYang1993
ZhouYang1993 / example1.py
Created May 8, 2020 03:12
Monotonic Stack
class Solution:
def largestRectangleArea(self, heights: List[int]) -> int:
ret = 0
mono_stack = []
heights.append(0)
for i, v in enumerate(heights):
while mono_stack and heights[mono_stack[-1]] > v:
height = heights[mono_stack.pop()]
if mono_stack:
length = i - mono_stack[-1]-1
@ZhouYang1993
ZhouYang1993 / example2.py
Created May 8, 2020 03:27
Monotonic Stack
class Solution:
def trap(self, heights: List[int]) -> int:
stack = [] # a decreasing stack
total = 0
for i, v in enumerate(heights):
while stack and heights[stack[-1]] < v:
popped_idx = stack.pop()
if not stack:
break
height = min(heights[stack[-1]], v) - heights[popped_idx]
@ZhouYang1993
ZhouYang1993 / example1.py
Created May 18, 2020 07:52
Abstract classes in Python
#### example 1
class Animal(ABC):
@abstractmethod
def move(self):
pass
a = Animal()
# TypeError: Can't instantiate abstract class Animal with abstract methods move
#### example 2
@ZhouYang1993
ZhouYang1993 / example.py
Last active May 18, 2020 08:00
Abstract Classes in Python
from abc import ABC, abstractmethod
### Example 1
class Animal(ABC):
@abstractmethod
def move(self):
pass
a = Animal()
# TypeError: Can't instantiate abstract class Animal with abstract methods move
### Example 2
@ZhouYang1993
ZhouYang1993 / example2.py
Created May 18, 2020 08:14
Abstract Classes in Python
from abc import ABC, abstractmethod
class Animal(ABC):
@abstractmethod
def move(self):
print('Animal moves')
pass
class Cat(Animal):
def move(self):
@ZhouYang1993
ZhouYang1993 / example2.py
Last active May 18, 2020 14:32
Abstract Classes in Python
from abc import ABC, abstractmethod
class Animal(ABC):
@abstractmethod
def move(self):
print('Animal moves')
class Cat(Animal):
def move(self):
super().move()