post_href stringlengths 57 213 | python_solutions stringlengths 71 22.3k | slug stringlengths 3 77 | post_title stringlengths 1 100 | user stringlengths 3 29 | upvotes int64 -20 1.2k | views int64 0 60.9k | problem_title stringlengths 3 77 | number int64 1 2.48k | acceptance float64 0.14 0.91 | difficulty stringclasses 3
values | __index_level_0__ int64 0 34k |
|---|---|---|---|---|---|---|---|---|---|---|---|
https://leetcode.com/problems/flood-fill/discuss/2811701/Flood-fill-or-Easy-python-solution | class Solution:
def floodFill(self, image: List[List[int]], sr: int, sc: int, color: int) -> List[List[int]]:
def dfs(image,sr,sc,color,rows,cols,source):
if sr < 0 or sr >= rows or sc < 0 or sc >= cols:
return
elif image[sr][sc] != source:
return
... | flood-fill | Flood fill | Easy python solution | nishanrahman1994 | 0 | 3 | flood fill | 733 | 0.607 | Easy | 12,000 |
https://leetcode.com/problems/flood-fill/discuss/2810558/Python-(Faster-than-97)-or-DFS-solution | class Solution:
def floodFill(self, image: List[List[int]], sr: int, sc: int, color: int) -> List[List[int]]:
rows, cols = len(image), len(image[0])
val = image[sr][sc]
visited = set()
def dfs(r, c):
visited.add((r, c))
image[r][c] = color
for dr, ... | flood-fill | Python (Faster than 97%) | DFS solution | KevinJM17 | 0 | 3 | flood fill | 733 | 0.607 | Easy | 12,001 |
https://leetcode.com/problems/flood-fill/discuss/2802647/O(n)-Solution-in-python | class Solution:
def floodFill(self, image: List[List[int]], row: int, column: int, newColor: int) -> List[List[int]]:
if image[row][column]==newColor or image==None:
return image
self.fill(image, row, column, image[row][column], newColor)
return image
def fill(self,image, row... | flood-fill | O(n) Solution in python | rdlong718 | 0 | 2 | flood fill | 733 | 0.607 | Easy | 12,002 |
https://leetcode.com/problems/flood-fill/discuss/2792963/Flood-Fill-algo-using-BFS-oror-Python | class Solution:
def __init__(self):
self.row = [0,0,-1,1]
self.col = [-1,1,0,0]
def floodFill(self, image: List[List[int]], sr: int, sc: int, color: int) -> List[List[int]]:
if len(image) == 1:
return image
q = []
q.append((sr,sc))
target = i... | flood-fill | Flood Fill algo using BFS || Python | Sai_Chandan_30 | 0 | 1 | flood fill | 733 | 0.607 | Easy | 12,003 |
https://leetcode.com/problems/flood-fill/discuss/2789031/Python-DFS-solution-explained | class Solution:
def floodFill(self, image: List[List[int]], sr: int, sc: int, color: int) -> List[List[int]]:
m,n,colorstart = len(image),len(image[0]),image[sr][sc]
def dfs(i,j,cs):
if i < 0 or j < 0 or i >=m or j >= n or image[i][j]!=cs:
return
# The (i,j) p... | flood-fill | Python DFS solution explained | Pirmil | 0 | 3 | flood fill | 733 | 0.607 | Easy | 12,004 |
https://leetcode.com/problems/flood-fill/discuss/2764881/Python-BFS-easy-solution-(94-runtime-99-memory) | class Solution:
def floodFill(self, image: List[List[int]], sr: int, sc: int, color: int) -> List[List[int]]:
m = len(image)
n = len(image[0])
queue = [(sr, sc)]
visited = set()
val = image[sr][sc]
if val == color:
return image
... | flood-fill | Python BFS easy solution (94% runtime, 99% memory) | sakshampandey273 | 0 | 15 | flood fill | 733 | 0.607 | Easy | 12,005 |
https://leetcode.com/problems/flood-fill/discuss/2743451/Python3-straightforward-recursive-solution | class Solution:
def floodFill(self, image: List[List[int]], sr: int, sc: int, color: int) -> List[List[int]]:
m = len(image)
n = len(image[0])
scolor = image[sr][sc]
# in case of infinite dfs
if scolor == color:
return image
# check row and column
... | flood-fill | Python3 straightforward recursive solution | damaolee | 0 | 6 | flood fill | 733 | 0.607 | Easy | 12,006 |
https://leetcode.com/problems/flood-fill/discuss/2704123/Here-is-my-simple-solution-using-one-function | class Solution:
def floodFill(self, image: List[List[int]], sr: int, sc: int, color: int) -> List[List[int]]:
if sr > len(image) or sc > len(image[0]) or sr == -1 or sc == -1: # do not exceed the matrix
return image
if(image[sr][sc] == color): # check if this element is colored... | flood-fill | Here is my simple solution using one function | user4038B | 0 | 9 | flood fill | 733 | 0.607 | Easy | 12,007 |
https://leetcode.com/problems/flood-fill/discuss/2674237/Python-Beats-98.96-(BFS) | class Solution:
def floodFill(self, image: List[List[int]], sr: int, sc: int, color: int) -> List[List[int]]:
M, N = len(image), len(image[0])
def validIndex(r,c):
nonlocal M, N, image, sr, sc
return 0<=r and r<=M-1 and 0<=c and c<=N-1 and image[r][c] == origin_color
... | flood-fill | Python Beats 98.96% (BFS) | SYLin117 | 0 | 2 | flood fill | 733 | 0.607 | Easy | 12,008 |
https://leetcode.com/problems/flood-fill/discuss/2661430/Super-basic-python-DFS-(O(n)) | class Solution:
def floodFill(self, image: List[List[int]], sr: int, sc: int, color: int) -> List[List[int]]:
# This is not necessary but returns a faster solution
source_color = image[sr][sc]
if source_color == color:
return image
stack = [(sr, sc)]
visi... | flood-fill | Super basic python DFS (O(n)) | AlecLeetcode | 0 | 3 | flood fill | 733 | 0.607 | Easy | 12,009 |
https://leetcode.com/problems/flood-fill/discuss/2657627/Python-Solution-using-DFS-and-Hash-Map | class Solution:
def floodFill(self, image: List[List[int]], sr: int, sc: int, color: int) -> List[List[int]]:
visited = set() # Tuples already traversed in image
NUM_ROW = len(image)
NUM_COL = len(image[0])
START_COLOR = image[sr][sc]
if START_COLOR == color:
r... | flood-fill | Python Solution, using DFS and Hash Map | BrandonTrastoy | 0 | 2 | flood fill | 733 | 0.607 | Easy | 12,010 |
https://leetcode.com/problems/flood-fill/discuss/2632886/Python-easy-solution | class Solution:
img = None
def paint(self, sr: int, sc: int, old_color: int, new_color: int):
if(self.img[sr][sc] == new_color):
return
elif(self.img[sr][sc] == old_color):
self.img[sr][sc] = new_color
else:
return
# up, down, left, right
... | flood-fill | Python easy solution | Jack_Chang | 0 | 35 | flood fill | 733 | 0.607 | Easy | 12,011 |
https://leetcode.com/problems/flood-fill/discuss/2599378/Fast-concise-solution-self-explanatory | class Solution:
def floodFill(self, image: List[List[int]], sr: int, sc: int, color: int) -> List[List[int]]:
def fill_rec(r, c, ref_color, new_color):
# recursive function that paints a pixel and calls itself for the 4 neighbor pixels.
if image[r][c] != ref_color: # must be same color as the ... | flood-fill | Fast concise solution self-explanatory | alexion1 | 0 | 13 | flood fill | 733 | 0.607 | Easy | 12,012 |
https://leetcode.com/problems/flood-fill/discuss/2538141/python-solution-or-space-O(1)-or-time-O(mn) | class Solution:
def floodFill(self, image: List[List[int]], sr: int, sc: int, color: int) -> List[List[int]]:
if image[sr][sc]==color:
return image
set_col=image[sr][sc]
r,c=len(image),len(image[0])
st=[(sr,sc)]
direction=[(1,0),(-1,0),(0,1),(0,-1)]
while ... | flood-fill | python solution | space-O(1) | time - O(mn) | I_am_SOURAV | 0 | 66 | flood fill | 733 | 0.607 | Easy | 12,013 |
https://leetcode.com/problems/flood-fill/discuss/2494794/Python-solution-with-DFS | class Solution:
def DFS(self,i:int , j:int , initialColor:int , newColor:int , image: List[List[int]])->List[List[int]]:
n , m = len(image)-1 , len(image[0])-1
if i<0 or j<0 : return
if i > n or j > m : return
if image[i][j]!= initialColor: return
image[i][j] = newColor
... | flood-fill | Python solution with DFS | ronipaul9972 | 0 | 53 | flood fill | 733 | 0.607 | Easy | 12,014 |
https://leetcode.com/problems/flood-fill/discuss/2491756/Simple-python-solution-or-DFS-or-93-faster | class Solution:
def floodFill(self, image: List[List[int]], sr: int, sc: int, color: int) -> List[List[int]]:
oldColor = image[sr][sc]
n = len(image)
m = len(image[0])
if color == oldColor:
return image
self.floodFillDFS(image, sr, sc, n, m, oldColor, color)
return image
def isValid(self, image, r... | flood-fill | Simple python solution | DFS | 93% faster | nikhitamore | 0 | 39 | flood fill | 733 | 0.607 | Easy | 12,015 |
https://leetcode.com/problems/flood-fill/discuss/2449342/Flood-Fill-oror-Python3-oror-DFS | class Solution:
def floodFill(self, image: List[List[int]], sr: int, sc: int, color: int) -> List[List[int]]:
if(image[sr][sc] == color):
return image
def dfs(sr, sc, src_color, color, image):
image[sr][sc] = color
for n... | flood-fill | Flood Fill || Python3 || DFS | vanshika_2507 | 0 | 62 | flood fill | 733 | 0.607 | Easy | 12,016 |
https://leetcode.com/problems/flood-fill/discuss/2448952/Python-Easy-DFS-Solution-oror-Documented | class Solution:
# Time: O(m * n) Space: O(m * n)
def floodFill(self, image: List[List[int]], sr: int, sc: int, color: int) -> List[List[int]]:
if image[sr][sc] == color: # color is already set
return image
oldColor = image[sr][sc] ... | flood-fill | [Python] Easy DFS Solution || Documented | Buntynara | 0 | 28 | flood fill | 733 | 0.607 | Easy | 12,017 |
https://leetcode.com/problems/flood-fill/discuss/2414184/Different-And-Intuitive-Solution-or-Actually-flood-filling | class Solution:
def floodFill(self, image: List[List[int]], sr: int, sc: int, color: int) -> List[List[int]]:
if image[sr][sc] == color:
return image
m = len(image)
n = len(image[0])
stack = [(sr,sc, image[sr][sc] + color)]
while stack:
x = stack.pop(0... | flood-fill | Different And Intuitive Solution | Actually flood filling | May-i-Code | 0 | 33 | flood fill | 733 | 0.607 | Easy | 12,018 |
https://leetcode.com/problems/flood-fill/discuss/2408774/C%2B%2BPython-Best-Optimized-Solution-with-best-approach | class Solution:
def floodFill(self, image: List[List[int]], sr: int, sc: int, newColor: int) -> List[List[int]]:
m = len(image)
n = len(image[0])
visited = [[None for _ in range(n)] for _ in range(m)]
initialColor = image[sr][sc]
queue = [(sr,sc)]
wh... | flood-fill | C++/Python Best Optimized Solution with best approach | arpit3043 | 0 | 65 | flood fill | 733 | 0.607 | Easy | 12,019 |
https://leetcode.com/problems/flood-fill/discuss/2398563/Easy-Python-BFS | class Solution:
def floodFill(self, image: List[List[int]], sr: int, sc: int, color: int) -> List[List[int]]:
# if starting pixel is already the target color, no change needed
if image[sr][sc] == color:
return image
original_color = image[sr][sc]
q = deque([(sr, sc)])
... | flood-fill | Easy Python BFS | lau125 | 0 | 27 | flood fill | 733 | 0.607 | Easy | 12,020 |
https://leetcode.com/problems/flood-fill/discuss/2366222/Python3-or-Straightforward-BFS-%2B-Queue-%2B-Set-No-Helper-Needed | class Solution:
#Time-Complexity: O(rows*cols)
#Space-Complexity: O(rows*cols + rows*cols) -> O(rows*cols)
def floodFill(self, image: List[List[int]], sr: int, sc: int, color: int) -> List[List[int]]:
#this is a standard bfs problem!
#we will add to queue pixels that share same color as the ... | flood-fill | Python3 | Straightforward BFS + Queue + Set No Helper Needed | JOON1234 | 0 | 41 | flood fill | 733 | 0.607 | Easy | 12,021 |
https://leetcode.com/problems/flood-fill/discuss/2245485/Python%3A-Faster-than-93-using-queue | class Solution:
def add_connected(self, image, e, src_color, color, maxr, maxc, queue):
if e[0] < 0 or e[0] > maxr or e[1] < 0 or e[1] > maxc:
return
if image[e[0]][e[1]] != src_color:
return
queue.append(e)
def floodFill(self, image: List[List[int]]... | flood-fill | Python: Faster than 93% using queue | pradyumna04 | 0 | 62 | flood fill | 733 | 0.607 | Easy | 12,022 |
https://leetcode.com/problems/flood-fill/discuss/2235994/Python-runtime-15.95-memory-65.65 | class Solution:
def floodFill(self, image: List[List[int]], sr: int, sc: int, newColor: int) -> List[List[int]]:
oriColor = image[sr][sc]
if oriColor == newColor:
return image
image[sr][sc] = newColor
for dr, dc in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
if -1<sr+... | flood-fill | Python, runtime 15.95% memory 65.65% | tsai00150 | 0 | 39 | flood fill | 733 | 0.607 | Easy | 12,023 |
https://leetcode.com/problems/flood-fill/discuss/2235994/Python-runtime-15.95-memory-65.65 | class Solution:
def floodFill(self, image: List[List[int]], sr: int, sc: int, newColor: int) -> List[List[int]]:
oriColor = image[sr][sc]
if oriColor == newColor:
return image
queue = [(sr, sc)]
while queue:
row, col = queue.pop()
image[row][col] =... | flood-fill | Python, runtime 15.95% memory 65.65% | tsai00150 | 0 | 39 | flood fill | 733 | 0.607 | Easy | 12,024 |
https://leetcode.com/problems/flood-fill/discuss/2165553/Easy-and-Simple-DFS | class Solution:
def isSafe(self, i,j,grid):
n = len(grid)
m = len(grid[0])
if 0 <= i < n and 0 <= j < m:
return True
else:
return False
def dfs(self,i,j,value,grid,color):
if not self.isSafe(i,j,grid) or grid[i][j] == color:
... | flood-fill | Easy and Simple DFS | Vaibhav7860 | 0 | 86 | flood fill | 733 | 0.607 | Easy | 12,025 |
https://leetcode.com/problems/flood-fill/discuss/2065951/Python-Simple-DFS-Solution | class Solution:
def floodFill(self, image: List[List[int]], sr: int, sc: int, newColor: int) -> List[List[int]]:
self.dfs(image, sr, sc, image[sr][sc], newColor)
return image
def dfs(self, image, m, n, oldValue, newValue):
if m < 0 or m > len(image)-1 or n < 0 or n > len(image[0])-1 or i... | flood-fill | Python Simple DFS Solution | yashchandani98 | 0 | 77 | flood fill | 733 | 0.607 | Easy | 12,026 |
https://leetcode.com/problems/flood-fill/discuss/2061286/Python-Simple-readable-easy-to-understand-DFS-solution-(Faster-than-69) | class Solution:
def floodFill(self, image: List[List[int]], sr: int, sc: int, newColor: int) -> List[List[int]]:
startColor = image[sr][sc]
if startColor != newColor:
self.fill(image, sr, sc, startColor, newColor)
return image
def fill(self, image: ... | flood-fill | [Python] Simple, readable, easy to understand DFS solution (Faster than 69%) | FedMartinez | 0 | 62 | flood fill | 733 | 0.607 | Easy | 12,027 |
https://leetcode.com/problems/flood-fill/discuss/1928649/Easy-and-90ms-Fast-Solution-in-Python-using-Set-T.C-greaterO(n) | class Solution:
def floodFill(self, image: List[List[int]], sr: int, sc: int, newColor: int) -> List[List[int]]:
color=image[sr][sc]
height=len(image)
width=len(image[0])
visit=set()
def dfs(image,sr,sc,newColor,visit):
image[sr][sc]=newColor
... | flood-fill | Easy and 90ms Fast Solution in Python using Set T.C->O(n) | karansinghsnp | 0 | 55 | flood fill | 733 | 0.607 | Easy | 12,028 |
https://leetcode.com/problems/flood-fill/discuss/1892383/Easy-Python3-DFS-Solution-oror-Recursion | class Solution:
def floodFill(self, image: List[List[int]], sr: int, sc: int, newColor: int) -> List[List[int]]:
rows = len(image)
cols = len(image[0])
color_to_change = image[sr][sc]
def dfs(r,c):
if(r<0 or c<0 or r>rows-1 or c>cols-1 or image[r][c] == ... | flood-fill | Easy Python3 DFS Solution || Recursion | darshanraval194 | 0 | 69 | flood fill | 733 | 0.607 | Easy | 12,029 |
https://leetcode.com/problems/asteroid-collision/discuss/646757/Python3-concise-solution-Asteroid-Collision | class Solution:
def asteroidCollision(self, asteroids: List[int]) -> List[int]:
stack = []
for a in asteroids:
if a > 0:
stack.append(a)
else:
while stack and stack[-1] > 0 and stack[-1] + a < 0:
stack.pop()
... | asteroid-collision | Python3 concise solution - Asteroid Collision | r0bertz | 5 | 426 | asteroid collision | 735 | 0.444 | Medium | 12,030 |
https://leetcode.com/problems/asteroid-collision/discuss/1794866/Python-or-Stack-or-Explanation-%2B-Complexity | class Solution:
def asteroidCollision(self, asteroids: List[int]) -> List[int]:
stack = []
for asteroid in asteroids:
# if there's things on the stack, we need to consider if we've got case 4
while stack and stack[-1] > 0 and asteroid < 0:
# determine which asteroids are exploding... | asteroid-collision | Python | Stack | Explanation + Complexity | leetbeet73 | 4 | 241 | asteroid collision | 735 | 0.444 | Medium | 12,031 |
https://leetcode.com/problems/asteroid-collision/discuss/1566988/Extremely-Easy-oror-100-faster-oror-Well-Coded | class Solution:
def asteroidCollision(self, asteroids: List[int]) -> List[int]:
st = []
for a in asteroids:
if a>0:
st.append(a)
else:
while len(st)>0 and st[-1]>0 and st[-1]<abs(a):
st.pop()
if len(st)==0 or st[-1]<0:
st.append(a)
elif st[-1]==abs(a):
st.pop()
return st | asteroid-collision | 📌📌 Extremely Easy || 100% faster || Well-Coded 🐍 | abhi9Rai | 2 | 175 | asteroid collision | 735 | 0.444 | Medium | 12,032 |
https://leetcode.com/problems/asteroid-collision/discuss/2844008/Asteroid-Collisions-oror-Simple-stack-solution | class Solution:
def asteroidCollision(self, asteroids: List[int]) -> List[int]:
stack=[]
for i in asteroids:
if not len(stack): stack.append(i);continue
if stack[-1]<0 or i>0: stack.append(i);continue
while len(stack) and stack[-1]>0 and i<0:
if -i... | asteroid-collision | Asteroid Collisions || Simple stack solution | kushagra2709 | 0 | 1 | asteroid collision | 735 | 0.444 | Medium | 12,033 |
https://leetcode.com/problems/asteroid-collision/discuss/2840525/Python-Other-approach-%3A-Finite-state-machine-or-2-stacks-solution | class Solution:
def asteroidCollision(self, asteroids: List[int]) -> List[int]:
INITIAL, PROCESS = 0, 1
state = INITIAL
s1, s2 = asteroids, []
while True:
if state == INITIAL:
s2.append(s1.pop())
state = PROCESS
elif state == P... | asteroid-collision | [Python] Other approach : Finite state machine | 2 stacks solution | Nezuko-NoBamboo | 0 | 1 | asteroid collision | 735 | 0.444 | Medium | 12,034 |
https://leetcode.com/problems/asteroid-collision/discuss/2797753/Python3-solution-with-continue-and-break | class Solution:
def asteroidCollision(self, asteroids: List[int]) -> List[int]:
# Start with empty stack
output = []
# Add asteroids to stack on the right, one by one.
for asteroid in asteroids: # O(n)
# If new asteroid is going left.
while output and aster... | asteroid-collision | Python3 solution with continue and break | lucieperrotta | 0 | 2 | asteroid collision | 735 | 0.444 | Medium | 12,035 |
https://leetcode.com/problems/asteroid-collision/discuss/2790711/Python3-solution-with-a-more-clear-code-readability-than-official-solution | class Solution:
def asteroidCollision(self, asteroids: List[int]) -> List[int]:
res = []
for a in asteroids:
while res and res[-1] > 0 and a < 0:
l,r = abs(res[-1]), abs(a)
if r < l:
break
elif r > l:
... | asteroid-collision | Python3 solution with a more clear code readability than official solution | ryabkin | 0 | 2 | asteroid collision | 735 | 0.444 | Medium | 12,036 |
https://leetcode.com/problems/asteroid-collision/discuss/2778646/Solution-to-Asteroid-Collison-Using-Stack | class Solution:
def asteroidCollision(self, asteroids: List[int]) -> List[int]:
stack = []
for asteroid in asteroids:
if asteroid < 0:
while stack and stack[-1] > 0 and abs(asteroid) > stack[-1]:
stack.pop()
if stack... | asteroid-collision | Solution to Asteroid Collison Using Stack | aman_22y | 0 | 9 | asteroid collision | 735 | 0.444 | Medium | 12,037 |
https://leetcode.com/problems/asteroid-collision/discuss/2680853/Python-stack-solution-with-comments | class Solution:
def asteroidCollision(self, asteroids: List[int]) -> List[int]:
ans, q, i, n = [], deque(), 0, len(asteroids)
while i < len(asteroids):
should_append = True
while q and q[-1]> 0 and asteroids[i] < 0:
if abs(asteroids[i]) > q[-1]:
... | asteroid-collision | Python - stack solution with comments | phantran197 | 0 | 5 | asteroid collision | 735 | 0.444 | Medium | 12,038 |
https://leetcode.com/problems/asteroid-collision/discuss/2671874/EASY-oror-Using-Stack-oror-SC-and-TC-O(N) | class Solution:
def asteroidCollision(self, asteroids: List[int]) -> List[int]:
stck = []
n = len(asteroids)
for i in range(len(asteroids)-1, -1, -1):
if asteroids[i] < 0:
stck.append(asteroids[i])
else:
while stck and stck[-1] < 0 and ... | asteroid-collision | EASY || Using Stack || SC & TC = O(N) | mihirshah0114 | 0 | 3 | asteroid collision | 735 | 0.444 | Medium | 12,039 |
https://leetcode.com/problems/asteroid-collision/discuss/2475782/Python-runtime-72.35-memory-25.35 | class Solution:
def asteroidCollision(self, asteroids: List[int]) -> List[int]:
stack = []
for e in asteroids:
while stack and stack[-1] > 0 and e < 0:
if stack[-1] < -e:
stack.pop()
elif stack[-1] == -e:
stack.pop()... | asteroid-collision | Python, runtime 72.35%, memory 25.35% | tsai00150 | 0 | 31 | asteroid collision | 735 | 0.444 | Medium | 12,040 |
https://leetcode.com/problems/asteroid-collision/discuss/2333697/Python-Plain-Stack-90-faster | class Solution(object):
def asteroidCollision(self, asteroids):
stack = []
for ch in asteroids:
if ch>0:
stack.append(ch)
elif ch<0:
stack.append(ch)
while len(stack)>1 and stack[-2]>0 and stack[-1]<0:
ch = s... | asteroid-collision | Python Plain Stack 90% faster | Abhi_009 | 0 | 62 | asteroid collision | 735 | 0.444 | Medium | 12,041 |
https://leetcode.com/problems/asteroid-collision/discuss/2332787/Python-oror-Easy-(but-not-efficient)-Recursion | class Solution:
def asteroidCollision(self, asteroids: List[int]) -> List[int]:
if all( asteroids[i] > 0 for i in range(len(asteroids))): return asteroids
if all( asteroids[i] < 0 for i in range(len(asteroids))): return asteroids
if not asteroids: return asteroids
n = len(asteroids)
j = 0
while asteroids[j... | asteroid-collision | Python || Easy (but not efficient) Recursion | morpheusdurden | 0 | 21 | asteroid collision | 735 | 0.444 | Medium | 12,042 |
https://leetcode.com/problems/asteroid-collision/discuss/2270364/stack-solution | class Solution:
def asteroidCollision(self, asteroids: List[int]) -> List[int]:
stack = [asteroids[0]]
for a in asteroids[1:]:
if len(stack) == 0 or a * stack[-1] > 0:
stack.append(a)
else:
while len(stack) > 0 and stack[-1] > 0 and ... | asteroid-collision | stack solution | Mujojo | 0 | 31 | asteroid collision | 735 | 0.444 | Medium | 12,043 |
https://leetcode.com/problems/asteroid-collision/discuss/1807130/Python-Solution | class Solution:
def asteroidCollision(self, asteroids: List[int]) -> List[int]:
stack=[]
for asteroid in asteroids:
if asteroid > 0:
stack.append(asteroid)
else:
append=True
while stack and stack[-1]>0:
if st... | asteroid-collision | Python Solution | Siddharth_singh | 0 | 82 | asteroid collision | 735 | 0.444 | Medium | 12,044 |
https://leetcode.com/problems/asteroid-collision/discuss/1780271/Python-easy-to-read-and-understand-or-stack | class Solution:
def asteroidCollision(self, asteroids: List[int]) -> List[int]:
stack = []
for asteroid in asteroids:
if asteroid > 0:
stack.append(asteroid)
else:
while stack and stack[-1] > 0 and abs(stack[-1]) < abs(asteroid):
... | asteroid-collision | Python easy to read and understand | stack | sanial2001 | 0 | 82 | asteroid collision | 735 | 0.444 | Medium | 12,045 |
https://leetcode.com/problems/asteroid-collision/discuss/1711954/Python-or-Stack | class Solution:
def asteroidCollision(self, asteroids: List[int]) -> List[int]:
stk=[]
for n in asteroids:
if n>0 or not stk or stk[-1]<0:
stk.append(n)
continue
else:
m=-n
if m>=stk[-1]:
whil... | asteroid-collision | Python | Stack | heckt27 | 0 | 33 | asteroid collision | 735 | 0.444 | Medium | 12,046 |
https://leetcode.com/problems/asteroid-collision/discuss/1675846/Python-stack.-Time%3A-O(N)-Space%3A-O(N) | class Solution:
def asteroidCollision(self, asteroids: List[int]) -> List[int]:
left = []
right = []
for a in asteroids:
if a > 0:
right.append(a)
else:
while right and -a > right[-1]:
right.pop() ... | asteroid-collision | Python, stack. Time: O(N), Space: O(N) | blue_sky5 | 0 | 68 | asteroid collision | 735 | 0.444 | Medium | 12,047 |
https://leetcode.com/problems/asteroid-collision/discuss/1670099/WEEB-DOES-PYTHON-USING-STACK-(BEATS-89.93) | class Solution:
def asteroidCollision(self, asteroids: List[int]) -> List[int]:
low, high = 0, 0
result = []
isPositive = False
stack = [] # stores right asteroids
for i in range(len(asteroids)):
if asteroids[i] < 0 and isPositive:
if stack[-1] > abs(asteroids[i]): # right > left
continue
e... | asteroid-collision | WEEB DOES PYTHON USING STACK (BEATS 89.93%) | Skywalker5423 | 0 | 57 | asteroid collision | 735 | 0.444 | Medium | 12,048 |
https://leetcode.com/problems/asteroid-collision/discuss/1621744/Python-90-faster | class Solution:
def asteroidCollision(self, asteroids: List[int]) -> List[int]:
stack = []
for asteroid in asteroids:
if asteroid < 0 and stack and stack[-1] > 0:
while stack and stack[-1] > 0 and asteroid < 0:
if abs(stack[-1]) == abs(asteroid):
... | asteroid-collision | Python 90% faster | daniela-andreea | 0 | 101 | asteroid collision | 735 | 0.444 | Medium | 12,049 |
https://leetcode.com/problems/asteroid-collision/discuss/1536851/Python3-Stack-Time%3A-O(n)-and-Space%3A-O(n) | class Solution:
def asteroidCollision(self, asteroids: List[int]) -> List[int]:
# Time: O(n)
# Space: O(n)
stackRight = []
ans = []
for a in asteroids:
if a < 0:
if len(stackRight) > 0:
val = abs(a)
while st... | asteroid-collision | [Python3] Stack - Time: O(n) & Space: O(n) | jae2021 | 0 | 84 | asteroid collision | 735 | 0.444 | Medium | 12,050 |
https://leetcode.com/problems/asteroid-collision/discuss/1446968/Self-explanatory-python-code | class Solution:
def asteroidCollision(self, asteroids: List[int]) -> List[int]:
# O(n) O(n)
stack = []
res = []
for i in range(len(asteroids)):
collid = False
if asteroids[i] > 0:
stack.append(asteroids[i])
continue
... | asteroid-collision | Self explanatory python code | maflint7 | 0 | 85 | asteroid collision | 735 | 0.444 | Medium | 12,051 |
https://leetcode.com/problems/asteroid-collision/discuss/1441107/Python3-Solution-based-on-stack | class Solution:
def asteroidCollision(self, asteroids: List[int]) -> List[int]:
stack = [0]
for asteroid in asteroids:
if asteroid < 0 and stack[-1] > 0:
while asteroid != 0 and stack[-1] > 0:
to_right = stack.pop()
if to_right > ab... | asteroid-collision | [Python3] Solution based on stack | maosipov11 | 0 | 40 | asteroid collision | 735 | 0.444 | Medium | 12,052 |
https://leetcode.com/problems/asteroid-collision/discuss/958014/Easy-Python3-solution | class Solution:
def asteroidCollision(self, asteroids: List[int]) -> List[int]:
i = 0
while i < len(asteroids)-1:
if i >= 0:
#if right element is negative and current is positive then do further checking
if asteroids[i] > 0 and as... | asteroid-collision | Easy Python3 solution | Pseudocoder_Ravina | 0 | 95 | asteroid collision | 735 | 0.444 | Medium | 12,053 |
https://leetcode.com/problems/asteroid-collision/discuss/602232/Python3-Runtime-easy-to-understand | class Solution:
def asteroidCollision(self, asteroids: List[int]) -> List[int]:
survivalStack = []
for asteroid in asteroids:
while survivalStack and asteroid < 0 < survivalStack[-1]:
challenger = survivalStack[-1]
_challenger, _asteroid = abs(challenger),... | asteroid-collision | Python3 Runtime easy to understand | lastnamehurt | 0 | 102 | asteroid collision | 735 | 0.444 | Medium | 12,054 |
https://leetcode.com/problems/asteroid-collision/discuss/509953/Python-O(n)-Code-with-Clear-Explanation | class Solution:
def asteroidCollision(self, asteroids: List[int]) -> List[int]:
ans = []
for a in asteroids:
if a > 0:
ans.append(a)
else:
appendFlag = True
while ans:
if ans[-1] < 0:
... | asteroid-collision | Python O(n) Code with Clear Explanation | sjtuluo | 0 | 198 | asteroid collision | 735 | 0.444 | Medium | 12,055 |
https://leetcode.com/problems/parse-lisp-expression/discuss/2149196/Python3-Solution | class Solution:
def evaluate(self, expression: str) -> int:
stack = []
parenEnd = {}
# Get the end parenthesis location
for idx, ch in enumerate(expression):
if ch == '(':
stack.append(idx)
if ch == ')':
parenEnd[stack... | parse-lisp-expression | Python3 Solution | krzys2194 | 1 | 113 | parse lisp expression | 736 | 0.515 | Hard | 12,056 |
https://leetcode.com/problems/parse-lisp-expression/discuss/1358397/Python3-recursion | class Solution:
def evaluate(self, expression: str) -> int:
loc = {}
stack = []
for i, x in enumerate(expression):
if x == "(": stack.append(i)
elif x == ")": loc[stack.pop()] = i
def fn(lo, hi, mp):
"""Return value of given expression."... | parse-lisp-expression | [Python3] recursion | ye15 | -1 | 203 | parse lisp expression | 736 | 0.515 | Hard | 12,057 |
https://leetcode.com/problems/monotone-increasing-digits/discuss/921119/Python3-O(N)-via-stack | class Solution:
def monotoneIncreasingDigits(self, N: int) -> int:
nums = [int(x) for x in str(N)] # digits
stack = []
for i, x in enumerate(nums):
while stack and stack[-1] > x: x = stack.pop() - 1
stack.append(x)
if len(stack) <= i: break
ret... | monotone-increasing-digits | [Python3] O(N) via stack | ye15 | 1 | 126 | monotone increasing digits | 738 | 0.471 | Medium | 12,058 |
https://leetcode.com/problems/monotone-increasing-digits/discuss/362053/Solution-in-Python-3-(beats-95) | class Solution:
def monotoneIncreasingDigits(self, n: int) -> int:
N = [int(i) for i in str(n)]
L = len(N)
for I in range(L-1):
if N[I] > N[I+1]: break
if N[I] <= N[I+1]: return n
N[I+1:], N[I] = [9]*(L-I-1), N[I] - 1
for i in range(I,0,-1):
if N[i] >= N[i-1]: break
N... | monotone-increasing-digits | Solution in Python 3 (beats 95%) | junaidmansuri | 1 | 440 | monotone increasing digits | 738 | 0.471 | Medium | 12,059 |
https://leetcode.com/problems/monotone-increasing-digits/discuss/2739671/Python-3-Solution | class Solution:
def monotoneIncreasingDigits(self, n: int) -> int:
res = ""
temp = str(n)[0]
last = str(n)[0]
d = len(str(n))
k = 1
for x in str(n)[1:]:
if int(x)>int(temp):
res += str(temp)*k
k = 1
elif int(x) =... | monotone-increasing-digits | Python 3 Solution | mati44 | 0 | 6 | monotone increasing digits | 738 | 0.471 | Medium | 12,060 |
https://leetcode.com/problems/monotone-increasing-digits/discuss/2672029/Greedy-oror-O(N)-oror-Python | class Solution:
def monotoneIncreasingDigits(self, n: int) -> int:
n = str(n)
n = list(n)
n[0] = int(n[0])
for i in range(1, len(n)):
n[i] = int(n[i])
if n[i] < n[i-1]:
for j in range(i-1, -1, -1):
if n[j+1] < n[j]:
... | monotone-increasing-digits | Greedy || O(N) || Python | mihirshah0114 | 0 | 4 | monotone increasing digits | 738 | 0.471 | Medium | 12,061 |
https://leetcode.com/problems/monotone-increasing-digits/discuss/2667810/Python3-Solution | class Solution:
def monotoneIncreasingDigits(self, n: int) -> int:
digits = [int(d) for d in str(n)]
if len(digits) == 1:
return n
l = 0
for i in range(1,len(digits)):
if digits[i] > digits[i-1]:
l = i
elif digits[i] < digits[i-1]... | monotone-increasing-digits | Python3 Solution | tawaca | 0 | 4 | monotone increasing digits | 738 | 0.471 | Medium | 12,062 |
https://leetcode.com/problems/monotone-increasing-digits/discuss/2284761/Only-change-to-'9's-one-time!-By-Python | class Solution:
def monotoneIncreasingDigits(self, n: int) -> int:
num = list(str(n))
for i in range(len(num)-1):
# Step1: When don't meet the condition, num[i]-=1 and repalce all num left into '9' and directly return
# However, there is the case that num[i-1]==num[i], which ... | monotone-increasing-digits | Only change to '9's one time! By Python | XRFXRF | 0 | 42 | monotone increasing digits | 738 | 0.471 | Medium | 12,063 |
https://leetcode.com/problems/monotone-increasing-digits/discuss/1427954/Short-Python-with-detailed-explaination | class Solution:
def monotoneIncreasingDigits(self, N: int) -> int:
N = str(N)
cur = last = 0
# we scan through the string while keeping track of the
# last index such that its digit is STRICTLY GREATER than
# its next. This is the digits that we'll need to decrement
# and all digits after it... | monotone-increasing-digits | Short Python with detailed explaination | Charlesl0129 | 0 | 107 | monotone increasing digits | 738 | 0.471 | Medium | 12,064 |
https://leetcode.com/problems/monotone-increasing-digits/discuss/1547284/O(n)-98-faster-oror-Simple-Logic-oror-Well-Explained | class Solution:
def monotoneIncreasingDigits(self, n: int) -> int:
s = str(n)
for i in range(len(s)-1,0,-1):
if s[i]<s[i-1]:
n = n-(int(s[i:])+1)
s = str(n)
return n | monotone-increasing-digits | 📌📌 O(n) 98% faster || Simple Logic || Well-Explained 🐍 | abhi9Rai | -3 | 139 | monotone increasing digits | 738 | 0.471 | Medium | 12,065 |
https://leetcode.com/problems/daily-temperatures/discuss/2506436/Python-Stack-97.04-faster-or-Simplest-solution-with-explanation-or-Beg-to-Adv-or-Monotonic-Stack | class Solution:
def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
result = [0] * len(temperatures) # having list with 0`s elements of same lenght as temperature array.
stack = [] # taking empty stack.
for index, temp in enumerate(temperatures): # Traversing through provided... | daily-temperatures | Python Stack 97.04% faster | Simplest solution with explanation | Beg to Adv | Monotonic Stack | rlakshay14 | 6 | 473 | daily temperatures | 739 | 0.666 | Medium | 12,066 |
https://leetcode.com/problems/daily-temperatures/discuss/1574891/Python-Brute-Force-Stack-Solutions-with-Explanation | class Solution:
def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
l = len(temperatures)
result = [0] * l
for i in range(l-1):
for j in range(i+1, l):
if temperatures[j] > temperatures[i]:
result[i] = j-i
bre... | daily-temperatures | [Python] Brute Force / Stack Solutions with Explanation | zayne-siew | 5 | 545 | daily temperatures | 739 | 0.666 | Medium | 12,067 |
https://leetcode.com/problems/daily-temperatures/discuss/1574891/Python-Brute-Force-Stack-Solutions-with-Explanation | class Solution:
def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
l = len(temperatures)
result, stack = [0]*l, deque()
for i in range(l):
while stack and temperatures[stack[-1]] < temperatures[i]:
index = stack.pop()
result[index] ... | daily-temperatures | [Python] Brute Force / Stack Solutions with Explanation | zayne-siew | 5 | 545 | daily temperatures | 739 | 0.666 | Medium | 12,068 |
https://leetcode.com/problems/daily-temperatures/discuss/486692/Python-Single-Pass-(Using-Stack)-6-liner | class Solution:
def dailyTemperatures(self, T: List[int]) -> List[int]:
res, stack = [0] * len(T), []
for i in range(len(T)):
while stack and T[stack[-1]] < T[i]:
res[stack.pop()] = i - stack[-1]
stack.append(i)
return res | daily-temperatures | Python - Single Pass (Using Stack) - 6 liner | mmbhatk | 4 | 1,100 | daily temperatures | 739 | 0.666 | Medium | 12,069 |
https://leetcode.com/problems/daily-temperatures/discuss/1446319/Python3-Decreasing-Stack | class Solution:
def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
# decreasing stack
stack = []
n = len(temperatures)
res = [0] * n
for i in range(n):
t = temperatures[i]
while stack != [] and temperatures[stack[-1]] < t:
... | daily-temperatures | [Python3] Decreasing Stack | zhanweiting | 3 | 274 | daily temperatures | 739 | 0.666 | Medium | 12,070 |
https://leetcode.com/problems/daily-temperatures/discuss/916764/Python3-forward-and-backward-via-mono-stack | class Solution:
def dailyTemperatures(self, T: List[int]) -> List[int]:
ans = [0]*len(T)
stack = []
for i in range(len(T)):
while stack and T[stack[-1]] < T[i]:
ii = stack.pop()
ans[ii] = i - ii
stack.append(i)
return ans | daily-temperatures | [Python3] forward & backward via mono-stack | ye15 | 3 | 159 | daily temperatures | 739 | 0.666 | Medium | 12,071 |
https://leetcode.com/problems/daily-temperatures/discuss/916764/Python3-forward-and-backward-via-mono-stack | class Solution:
def dailyTemperatures(self, T: List[int]) -> List[int]:
ans = [0]*len(T)
stack = []
for i in reversed(range(len(T))):
while stack and T[stack[-1]] <= T[i]: stack.pop() # mono-stack (decreasing)
if stack: ans[i] = stack[-1] - i
stack.append... | daily-temperatures | [Python3] forward & backward via mono-stack | ye15 | 3 | 159 | daily temperatures | 739 | 0.666 | Medium | 12,072 |
https://leetcode.com/problems/daily-temperatures/discuss/1573015/WEEB-DOES-PYTHON-MONOTONIC-STACK | class Solution:
def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
result = [0] * len(temperatures)
stack = [] # stores index
# decreasing stack implementation
for i in range(len(temperatures)):
while stack and temperatures[stack[-1]] < temperatures[i]:
idx = stack.pop()
result[idx]... | daily-temperatures | WEEB DOES PYTHON MONOTONIC STACK | Skywalker5423 | 2 | 136 | daily temperatures | 739 | 0.666 | Medium | 12,073 |
https://leetcode.com/problems/daily-temperatures/discuss/723606/Python-Stack-Solution-Easy-to-Understand | class Solution:
# Time: O(n)
# Space: O(n)
def dailyTemperatures(self, T: List[int]) -> List[int]:
stack = []
res = [0 for _ in range(len(T))]
for i, t1 in enumerate(T):
while stack and t1 > stack[-1][1]:
j, t2 = stack.pop()
res[j] = i - j
... | daily-temperatures | Python Stack Solution Easy to Understand | whissely | 2 | 484 | daily temperatures | 739 | 0.666 | Medium | 12,074 |
https://leetcode.com/problems/daily-temperatures/discuss/312214/Python3-concise-solution-with-stack | class Solution:
def dailyTemperatures(self, T: List[int]) -> List[int]:
stack=[]
res=[0]*len(T)
tem=list(enumerate(T))
for i,j in tem:
while stack and j>T[stack[-1]]:
res[stack[-1]]=i-stack[-1]
stack.pop()
stack.append(i)
return res | daily-temperatures | Python3 concise solution with stack | jasperjoe | 2 | 250 | daily temperatures | 739 | 0.666 | Medium | 12,075 |
https://leetcode.com/problems/daily-temperatures/discuss/2619084/Python3-Solution-or-Stack-or-O(n) | class Solution:
def dailyTemperatures(self, T):
n = len(T)
stack = []
ans = [0] * n
for i in range(n):
while stack and T[stack[-1]] < T[i]:
ans[stack.pop()] = i - stack[-1]
stack.append(i)
return ans | daily-temperatures | ✔ Python3 Solution | Stack | O(n) | satyam2001 | 1 | 64 | daily temperatures | 739 | 0.666 | Medium | 12,076 |
https://leetcode.com/problems/daily-temperatures/discuss/2177822/Easy-Python-Solution-with-steps-and-explanation.-Very-Fast | class Solution(object):
def dailyTemperatures(self, temperatures):
'''
====================================================================================================
1. Initialise result list with zeroes
2. Initialise the stack with the tuple of the first element and i... | daily-temperatures | Easy Python Solution with steps and explanation. Very Fast | arnavjaiswal149 | 1 | 80 | daily temperatures | 739 | 0.666 | Medium | 12,077 |
https://leetcode.com/problems/daily-temperatures/discuss/1975705/Python3-Runtime%3A-1392ms-63.30-Memory%3A-24.9mb-58.78 | class Solution:
def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
waitingList = [0] * len(temperatures)
stack = []
for idx, val in enumerate(temperatures):
while len(stack) > 0 and temperatures[stack[len(stack) - 1]] < val:
current = stac... | daily-temperatures | Python3 Runtime: 1392ms 63.30% Memory: 24.9mb 58.78% | arshergon | 1 | 42 | daily temperatures | 739 | 0.666 | Medium | 12,078 |
https://leetcode.com/problems/daily-temperatures/discuss/1777898/Python-faster-than-80 | class Solution:
def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
answer = [0] * len(temperatures)
maxi = temperatures[-1]
for i in range(len(answer) - 2, -1, -1):
if temperatures[i] >= maxi:
maxi = temperatures[i]
answer[i] = 0
... | daily-temperatures | Python faster than 80% | Swizop | 1 | 214 | daily temperatures | 739 | 0.666 | Medium | 12,079 |
https://leetcode.com/problems/daily-temperatures/discuss/1576457/Python-Solution | class Solution:
def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
N = len(temperatures)
dp = [0] * N
stack = []
for i in range(N-1, -1, -1):
curr_temp, curr_idx = temperatures[i], i
while st... | daily-temperatures | Python Solution | ErikRodriguez-webdev | 1 | 70 | daily temperatures | 739 | 0.666 | Medium | 12,080 |
https://leetcode.com/problems/daily-temperatures/discuss/1360656/Python-using-monotonic-stack | class Solution:
def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
n = len(temperatures)
stack = []
res = [0] * n
for i in range(n - 1, -1, -1):
temp = temperatures[i]
while stack and temperatures[stack[-1]] <= temp:
stack.pop()... | daily-temperatures | Python using monotonic stack | sherryfansf | 1 | 178 | daily temperatures | 739 | 0.666 | Medium | 12,081 |
https://leetcode.com/problems/daily-temperatures/discuss/1235383/Python-Simple-Stack-Solution | class Solution:
def dailyTemperatures(self, T: List[int]) -> List[int]:
stack=[]
res=[0]*len(T)
for x in range(len(T)-1,-1,-1):
while(stack and stack[-1][0]<=T[x]):
stack.pop()
if stack:
res[x]=stack[-1][1]-x
stack.append((T... | daily-temperatures | Python Simple Stack Solution | abhinav4202 | 1 | 399 | daily temperatures | 739 | 0.666 | Medium | 12,082 |
https://leetcode.com/problems/daily-temperatures/discuss/650914/Intuitive-approach-by-using-cache-to-hold-examined-information-to-look-for-warmer-position | class Solution:
def dailyTemperatures(self, T: List[int]) -> List[int]:
ans = [0]
''' answer. The last temperature is always 0'''
cache = [(T[-1], len(T)-1)]
''' cache is used to hold the previous examined values'''
# Algorithm
# Cache is a list to hold a l... | daily-temperatures | Intuitive approach by using cache to hold examined information to look for warmer position | puremonkey2001 | 1 | 63 | daily temperatures | 739 | 0.666 | Medium | 12,083 |
https://leetcode.com/problems/daily-temperatures/discuss/352495/Python-3-(-O(n)-time-)-(five-lines) | class Solution:
def dailyTemperatures(self, T: List[int]) -> List[int]:
N, F = [math.inf]*102, [0]*len(T)
for i in range(len(T)-1,-1,-1):
N[T[i]], m = i, min([N[i] for i in range(T[i]+1,102)])
if m != math.inf: F[i] = m - i
return F
- Junaid Mansuri
- Chicago, IL | daily-temperatures | Python 3 ( O(n) time ) (five lines) | junaidmansuri | 1 | 820 | daily temperatures | 739 | 0.666 | Medium | 12,084 |
https://leetcode.com/problems/daily-temperatures/discuss/2847280/Python-Monotonous-Stack-Solution | class Solution:
def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
n = len(temperatures)
output_array = [0 for i in range(n)]
stack = []
for i in range(n):
while stack and temperatures[stack[-1]] < temperatures[i]:
index = stack.pop()
... | daily-temperatures | Python [Monotonous Stack Solution] | jamesg6198 | 0 | 1 | daily temperatures | 739 | 0.666 | Medium | 12,085 |
https://leetcode.com/problems/daily-temperatures/discuss/2840760/python3-easy-solution-using-stack | class Solution:
def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
stack = []
t = temperatures
n = len(t)
out = []
for i in range(n-1,-1,-1):
if not stack:
out.append(0)
elif s... | daily-temperatures | python3 easy solution using stack | shashank732001 | 0 | 3 | daily temperatures | 739 | 0.666 | Medium | 12,086 |
https://leetcode.com/problems/daily-temperatures/discuss/2807606/Easy-Python-Solution-with-Stack | class Solution:
def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
l=[]
stk=[]
for i in range(len(temperatures)-1,-1,-1):
if(len(stk)<=0):
l.append(0)
stk.append([i,temperatures[i]])
elif(len(stk) and stk[-1][1]>temperat... | daily-temperatures | Easy Python Solution with Stack | liontech_123 | 0 | 8 | daily temperatures | 739 | 0.666 | Medium | 12,087 |
https://leetcode.com/problems/daily-temperatures/discuss/2806120/739.-Daily-Temperatures-oror-Python3-oror-STACK | class Solution:
def dailyTemperatures(self, t: List[int]) -> List[int]:
stack=[]
ans=[0]*len(t)
res=[0]*len(t)
for i in range(len(t)-1,-1,-1):
curr=t[i]
while len(stack)!=0 and stack[-1][1]<=curr:
stack.pop()
#print(stack)
... | daily-temperatures | 739. Daily Temperatures || Python3 || STACK | shagun_pandey | 0 | 5 | daily temperatures | 739 | 0.666 | Medium | 12,088 |
https://leetcode.com/problems/daily-temperatures/discuss/2723032/Python3-Simple-Stack-Solution | class Solution:
def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
stack = []
res = [0] * len(temperatures)
for i, t in enumerate(temperatures):
while stack and t > stack[-1][0]:
_, idx = stack.pop()
res[idx] = i-idx
st... | daily-temperatures | Python3 Simple Stack Solution | jonathanbrophy47 | 0 | 16 | daily temperatures | 739 | 0.666 | Medium | 12,089 |
https://leetcode.com/problems/daily-temperatures/discuss/2715551/Python-Easy-to-Understand-oror-Logic-%2B-Code | class Solution:
def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
'''
This is a classic example of monotonic stack
In this we will check if the temprature in the temp list is greater than the top of the stack , If yes then we will pop the temp and index from the stack and in... | daily-temperatures | [Python] Easy to Understand || Logic + Code | Ron99 | 0 | 30 | daily temperatures | 739 | 0.666 | Medium | 12,090 |
https://leetcode.com/problems/daily-temperatures/discuss/2672188/Stack-oror-next-greater-element-oror-SC-and-TC-O(N) | class Solution:
def dailyTemperatures(self, arr: List[int]) -> List[int]:
stck = []
ans = []
for i in range(len(arr)-1, -1, -1):
counter = 0
while stck and stck[-1][0] <= arr[i]:
counter += stck[-1][1] + 1
stck.pop(-1)
if st... | daily-temperatures | Stack || next greater element || SC & TC - O(N) | mihirshah0114 | 0 | 6 | daily temperatures | 739 | 0.666 | Medium | 12,091 |
https://leetcode.com/problems/daily-temperatures/discuss/2660317/Daily-Temperatures-using-python | class Solution:
def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
stack_temp = [(temperatures[-1], len(temperatures)-1)]
output = [0 for i in range(len(temperatures))]
for i in range(len(temperatures)-2, -1, -1):
while stack_temp and temperatures[i] >= stack_temp... | daily-temperatures | Daily Temperatures- using python | D_Trivedi0607 | 0 | 7 | daily temperatures | 739 | 0.666 | Medium | 12,092 |
https://leetcode.com/problems/daily-temperatures/discuss/2649466/Clean-Monostack-solution-O(n) | class Solution:
def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
mono_stack = []
if len(temperatures) == 1:
return [0]
res = [0]*len(temperatures)
for i,t in enumerate(temperatures):
while len(mono_stack)>0 and t>temperatures[mono_st... | daily-temperatures | Clean Monostack solution, O(n) | Arana | 0 | 93 | daily temperatures | 739 | 0.666 | Medium | 12,093 |
https://leetcode.com/problems/daily-temperatures/discuss/2631619/Python-Solution-or-Monotonic-Stack | class Solution:
def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
n=len(temperatures)
ans=[0]*n
stack=[]
for i in range(n-1, -1, -1):
while stack and stack[-1][0]<=temperatures[i]:
stack.pop()
if stack:
ans[i]=s... | daily-temperatures | Python Solution | Monotonic Stack | Siddharth_singh | 0 | 33 | daily temperatures | 739 | 0.666 | Medium | 12,094 |
https://leetcode.com/problems/daily-temperatures/discuss/2598856/Python-Solution-Stack | class Solution:
def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
# add value to the stack when temp <= prev
# when temp > prev, keep popping from stack until temp <= prev, while updating values in the result array
res = [0] * len(temperatures)
stack = []
... | daily-temperatures | Python Solution Stack | al5861 | 0 | 100 | daily temperatures | 739 | 0.666 | Medium | 12,095 |
https://leetcode.com/problems/daily-temperatures/discuss/2535658/Python3-or-Optimal-Time-Solution-using-Monotonic-Stack | class Solution:
def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
# Monotonic Stack T.C: O(n)
stack = deque()
answer = [0] * len(temperatures)
for i,t in enumerate(temperatures):
while stack and t > stack[-1][0]:
temp, index = st... | daily-temperatures | Python3 | Optimal Time Solution using Monotonic Stack | chawlashivansh | 0 | 177 | daily temperatures | 739 | 0.666 | Medium | 12,096 |
https://leetcode.com/problems/daily-temperatures/discuss/2398648/Python3-solution-O(N)-time-complexity-O(N)-space-complexity-65-faster | class Solution:
def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
stack = []
answer = [0] * len(temperatures)
#loops trough the input list, pair of index-temperature is used
for index, currTemp in enumerate(temperatures):
#until stack is empty or... | daily-temperatures | Python3 solution, O(N) time complexity, O(N) space complexity, 65% faster | matteogianferrari | 0 | 81 | daily temperatures | 739 | 0.666 | Medium | 12,097 |
https://leetcode.com/problems/daily-temperatures/discuss/2195397/Python-or-Stack-Solution-or-TC-O(n)-SC-O(n)-or-Easy-Solution | class Solution:
def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
stack = list()
top = -1
res = [0]*len(temperatures)
#Base Case: For top == -1, pushing 1st elemnt to the stack
stack.append(0)
top += 1
for i in range(1,len(temperature... | daily-temperatures | Python | Stack Solution | TC- O(n) SC- O(n) | Easy Solution | aadyant2019 | 0 | 116 | daily temperatures | 739 | 0.666 | Medium | 12,098 |
https://leetcode.com/problems/daily-temperatures/discuss/2184875/Simple-Beginner-Python-Stack-Solution | class Solution:
def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
stack, output = [], [0] * len(temperatures)
t = enumerate(temperatures)
for i, temp in t:
if not stack:
stack.append((i,temp))
continue
... | daily-temperatures | Simple Beginner Python Stack Solution | 7yler | 0 | 72 | daily temperatures | 739 | 0.666 | Medium | 12,099 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.