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/pascals-triangle-ii/discuss/1703403/Easy-to-read-and-understand-Python-3-solution | class Solution:
def getRow(self, rowIndex: int) -> List[int]:
previousRow = []
for currentRowIndex in range(rowIndex + 1):
currentRow = []
for i in range(currentRowIndex + 1):
if i - 1 < 0 or i == len(previousRow):
currentRow.append(1)
else:
currentRow.append(previousRow[i - 1] + previousRow[i])
previousRow = currentRow
rowIndex -= 1
return previousRow | pascals-triangle-ii | Easy to read and understand Python 3 solution | fourcommas | 1 | 98 | pascals triangle ii | 119 | 0.598 | Easy | 1,100 |
https://leetcode.com/problems/pascals-triangle-ii/discuss/1670923/Easy-and-faster-than-96.5-Python3-solution | class Solution:
def getRow(self, rowIndex: int) -> List[int]:
row = [1]
for x in range(max(rowIndex, 0)):
row = [l + r for l, r in zip(row + [0], [0] + row)]
return row | pascals-triangle-ii | Easy and faster than 96.5% Python3 solution | y-arjun-y | 1 | 139 | pascals triangle ii | 119 | 0.598 | Easy | 1,101 |
https://leetcode.com/problems/pascals-triangle-ii/discuss/1660465/Simple-Python-Solution | class Solution:
def getRow(self, row_index: int) -> list[int]:
current = [1]
for _ in range(row_index):
current = [1] + [num1 + num2 for num1, num2 in zip(current, current[1:])] + [1]
return current | pascals-triangle-ii | Simple Python Solution | migash | 1 | 96 | pascals triangle ii | 119 | 0.598 | Easy | 1,102 |
https://leetcode.com/problems/pascals-triangle-ii/discuss/1626241/Python-easy-understanding-simple-solution | class Solution:
def getRow(self, n: int) -> List[int]:
final=[]
for i in range(1,n+2):
res=[1]*i
if i>2:
for k in range(1,i-1):
res[k]=final[i-2][k-1]+final[i-2][k]
final.append(res)
return final[n] | pascals-triangle-ii | Python easy understanding simple solution | diksha_choudhary | 1 | 93 | pascals triangle ii | 119 | 0.598 | Easy | 1,103 |
https://leetcode.com/problems/pascals-triangle-ii/discuss/1482547/Easy-Python-solution-in-O(n)-time | class Solution:
def getRow(self, n: int) -> List[int]:
n=n+1
arr = [1]
a=1
b=1
for i in range (1,n):
a=a*(n-i)
b=b*(i)
res=a//b
arr.append(res)
return arr | pascals-triangle-ii | Easy Python solution in O(n) time | vaibhav24012000 | 1 | 182 | pascals triangle ii | 119 | 0.598 | Easy | 1,104 |
https://leetcode.com/problems/pascals-triangle-ii/discuss/1371313/WEEB-DOES-PYTHON | class Solution:
def getRow(self, rowIndex: int) -> List[int]:
answer, prev = [], []
for i in range(1,rowIndex+2):
row = [1] * i
if len(row) > 2:
for j in range(len(prev)-1):
row[j+1] = prev[j] + prev[j+1]
prev = row
return row | pascals-triangle-ii | WEEB DOES PYTHON | Skywalker5423 | 1 | 161 | pascals triangle ii | 119 | 0.598 | Easy | 1,105 |
https://leetcode.com/problems/pascals-triangle-ii/discuss/693071/Python3-dp | class Solution:
def getRow(self, rowIndex: int) -> List[int]:
ans = [1]
for i in range(rowIndex): # n choose k
ans.append(ans[-1]*(rowIndex-i)//(i+1))
return ans | pascals-triangle-ii | [Python3] dp | ye15 | 1 | 106 | pascals triangle ii | 119 | 0.598 | Easy | 1,106 |
https://leetcode.com/problems/pascals-triangle-ii/discuss/693071/Python3-dp | class Solution:
def getRow(self, rowIndex: int) -> List[int]:
ans = []
for _ in range(rowIndex+1):
ans.append(1)
for i in reversed(range(1, len(ans)-1)): ans[i] += ans[i-1]
return ans | pascals-triangle-ii | [Python3] dp | ye15 | 1 | 106 | pascals triangle ii | 119 | 0.598 | Easy | 1,107 |
https://leetcode.com/problems/pascals-triangle-ii/discuss/693071/Python3-dp | class Solution:
def getRow(self, rowIndex: int) -> List[int]:
@lru_cache(None)
def fn(i, j):
"""Return (i, j)th number on Pascal's triangle"""
if j in (0, i): return 1
return fn(i-1, j-1) + fn(i-1, j)
return [fn(rowIndex, j) for j in range(0, rowIndex+1)] | pascals-triangle-ii | [Python3] dp | ye15 | 1 | 106 | pascals triangle ii | 119 | 0.598 | Easy | 1,108 |
https://leetcode.com/problems/pascals-triangle-ii/discuss/2839924/there-are-two-python-solutions-.-But-recursive-is-interrupted-on-24th-cases.-Dynamic-is-Ok | class Solution:
def getRow(self, row: int) -> List[int]:
'''def pascal(i,j):
if j==0 :
return 1
if i == j :
return 1
return pascal(i-1,j-1) + pascal(i-1,j)
a = []
if row % 2 == 1 :
for i in range((row+1)//2):
tmp = pascal(row,i)
a.append(tmp)
b = list(reversed(a))
else :
for i in range(row//2 + 1):
tmp = pascal(row,i)
a.append(tmp)
b = list(reversed(a[:len(a)-1]))
a.extend(b)
return a '''
if row == 0:
return [1]
if row == 1 :
return [1,1]
arr_2d = [ [None for j in range(row+1)] for i in range(row+1)]
for i in range(row+1):
arr_2d[i][0] = 1
arr_2d[i][i] = 1
for i in range(2,row+1):
for j in range(1,i):
arr_2d[i][j] = arr_2d[i-1][j] + arr_2d[i-1][j-1]
return arr_2d[row] | pascals-triangle-ii | there are two python solutions . But recursive is interrupted on 24th cases. Dynamic is Ok | Cosmodude | 0 | 1 | pascals triangle ii | 119 | 0.598 | Easy | 1,109 |
https://leetcode.com/problems/pascals-triangle-ii/discuss/2821820/Python-50-ms-beats-70 | class Solution:
def getRow(self, rowIndex: int) -> List[int]:
d={0:[1],1:[1,1]}
for i in range(2,rowIndex+1):
l=[1]
for j in range(i-1):
l.append(d[i-1][j]+d[i-1][j+1])
l.append(1)
d[i]=l
return d[rowIndex] | pascals-triangle-ii | Python 50 ms beats 70% | varunmuthannaka | 0 | 3 | pascals triangle ii | 119 | 0.598 | Easy | 1,110 |
https://leetcode.com/problems/pascals-triangle-ii/discuss/2818666/pascal's-triangle-2 | class Solution:
def getRow(self, rowIndex: int) -> List[int]:
row=[1,1]
if rowIndex==0: return [1]
if rowIndex==1: return row
for _ in range(rowIndex-1):
new_row=[1]
for i in range(len(row)):
try:
new_row.append(row[i]+row[i+1])
except IndexError:
break
new_row.append(1)
row=new_row
return row | pascals-triangle-ii | pascal's triangle 2 | emresvd | 0 | 5 | pascals triangle ii | 119 | 0.598 | Easy | 1,111 |
https://leetcode.com/problems/pascals-triangle-ii/discuss/2785954/Python-beats-96.5-!! | class Solution:
def getRow(self, rowIndex: int) -> List[int]:
numRows = rowIndex+1
dp=[1]*numRows
for i in range(numRows):
dp[i]=[1]*(i+1)
for j in range(1,i):
dp[i][j]=dp[i-1][j-1]+dp[i-1][j]
return dp[rowIndex] | pascals-triangle-ii | Python, beats 96.5% !! | Vivek_Pandith | 0 | 5 | pascals triangle ii | 119 | 0.598 | Easy | 1,112 |
https://leetcode.com/problems/pascals-triangle-ii/discuss/2783989/Standart-recursive-approach | class Solution:
def getRow(self, rowIndex: int) -> List[int]:
if rowIndex == 0:
return [1]
else:
r = self.getRow(rowIndex-1)
return [((r[i-1]+r[i]) if i > 0 and i < rowIndex else 1) for i in range(rowIndex+1) ] | pascals-triangle-ii | Standart recursive approach | adnull | 0 | 3 | pascals triangle ii | 119 | 0.598 | Easy | 1,113 |
https://leetcode.com/problems/pascals-triangle-ii/discuss/2781284/python-speed-solve | class Solution:
def getRow(self, rowIndex: int) -> List[int]:
arr = [[1], [1, 1]]
for i in range(2, rowIndex+1):
dp = [1]
for j in range(1, i):
dp.append(arr[i-1][j-1] + arr[i-1][j])
dp.append(1)
arr.append(dp)
return arr[rowIndex] | pascals-triangle-ii | python speed solve | parinyasa | 0 | 5 | pascals triangle ii | 119 | 0.598 | Easy | 1,114 |
https://leetcode.com/problems/pascals-triangle-ii/discuss/2739118/Python3-Dynamic-Programming-Solution | class Solution:
def getRow(self, rowIndex: int) -> List[int]:
pascal = [[1]]
for i in range(rowIndex):
temp = [0] + pascal[-1] + [0]
row = []
for j in range(len(pascal[-1]) + 1):
row.append(temp[j] + temp[j + 1])
pascal.append(row)
return pascal[-1] | pascals-triangle-ii | Python3 Dynamic Programming Solution | paul1202 | 0 | 6 | pascals triangle ii | 119 | 0.598 | Easy | 1,115 |
https://leetcode.com/problems/pascals-triangle-ii/discuss/2707562/Python-one-liner-using-binomial-coefficients! | class Solution:
def getRow(self, rowIndex: int) -> List[int]:
from math import comb
return [comb(rowIndex, i) for i in range(rowIndex+1)] | pascals-triangle-ii | Python one liner using binomial coefficients! | pierce314159 | 0 | 3 | pascals triangle ii | 119 | 0.598 | Easy | 1,116 |
https://leetcode.com/problems/pascals-triangle-ii/discuss/2701626/Python-or-Simple-solution | class Solution:
def getRow(self, rowIndex: int) -> List[int]:
dp = [[] for _ in range(rowIndex + 1)]
dp[0] = [1]
for i in range(1, rowIndex + 1):
dp[i] = [1] + [0]*(i - 1) + [1]
for i in range(2, rowIndex + 1):
for j in range(1, i):
dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j]
return dp[rowIndex] | pascals-triangle-ii | Python | Simple solution | LordVader1 | 0 | 6 | pascals triangle ii | 119 | 0.598 | Easy | 1,117 |
https://leetcode.com/problems/pascals-triangle-ii/discuss/2683063/Simple-brute-force-solution-python-3 | class Solution:
def getRow(self, rowIndex: int) -> List[int]:
rows = {}
rows[0] = [1]
rows[1] = [1,1]
if rowIndex > 1:
for i in range(2, rowIndex+1):
rows[i] = [1]
for j in range(0, i-1):
rows[i].append(rows[i-1][j] + rows[i-1][j+1])
rows[i].append(1)
return rows[rowIndex] | pascals-triangle-ii | Simple brute force solution python 3 | vegancyberpunk | 0 | 2 | pascals triangle ii | 119 | 0.598 | Easy | 1,118 |
https://leetcode.com/problems/pascals-triangle-ii/discuss/2673876/Python-solution | class Solution:
def getRow(self, rowIndex: int) -> List[int]:
listof=[]
if rowIndex == 0:
listof.append(1)
return listof
if rowIndex== 1:
listof.append(1)
listof.append(1)
return listof
if rowIndex>= 2:
pre =[1,1]
for j in range(2,rowIndex+1):
nextt =[1,1]
for i in range(1,j):
summ =pre[i-1] + pre[i]
nextt.insert(i,summ)
pre=nextt
return nextt | pascals-triangle-ii | Python solution | Sheeza | 0 | 2 | pascals triangle ii | 119 | 0.598 | Easy | 1,119 |
https://leetcode.com/problems/pascals-triangle-ii/discuss/2668028/python-solution-faster-then-90 | class Solution:
def getRow(self, rowIndex: int) -> List[int]:
a = []
b= []
for i in range(rowIndex+1):
for j in range(i+1):
a.append(factorial(i)//(factorial(j)*factorial(i-j)))
for i in range(rowIndex+1):
b.append(a.pop())
return b | pascals-triangle-ii | python solution faster then 90% | pranjalmishra334 | 0 | 10 | pascals triangle ii | 119 | 0.598 | Easy | 1,120 |
https://leetcode.com/problems/pascals-triangle-ii/discuss/2658653/Python-SImple-Solution | class Solution:
def getRow(self, n: int) -> List[int]:
dp = [1]
for i in range(n):
dp.append(1)
for j in range(i, 0, -1):
dp[j] += dp[j-1]
return dp | pascals-triangle-ii | Python SImple Solution | lokeshsenthilkumar | 0 | 37 | pascals triangle ii | 119 | 0.598 | Easy | 1,121 |
https://leetcode.com/problems/pascals-triangle-ii/discuss/2594290/Python-4-Liner | class Solution:
def getRow(self, rowIndex: int) -> List[int]:
ans = []
for i in range(rowIndex+1):
ans.append(comb(rowIndex,i))
return ans | pascals-triangle-ii | Python 4-Liner | kareem_alzahal | 0 | 34 | pascals triangle ii | 119 | 0.598 | Easy | 1,122 |
https://leetcode.com/problems/pascals-triangle-ii/discuss/2589652/Combinatoric-python-solution | class Solution(object):
def getRow(self, rowIndex):
def factorial(n):
return 1 if (n == 1 or n == 0) else n * factorial(n - 1)
def comb(n, k):
return factorial(n) / (factorial(k) * factorial(n - k))
return [comb(rowIndex, x) for x in range(rowIndex + 1)] | pascals-triangle-ii | Combinatoric python solution | shel18 | 0 | 29 | pascals triangle ii | 119 | 0.598 | Easy | 1,123 |
https://leetcode.com/problems/pascals-triangle-ii/discuss/2503335/Python-and-Go | class Solution:
def getRow(self, rowIndex: int) -> List[int]:
triangle = self.pascal(rowIndex + 1)
return triangle[rowIndex]
def pascal(self, depth: int) -> List[List[int]]:
# 全部 1 で埋める
# [[1], [1, 1], [1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1]]
triangle = [[1] * i for i in range(1, depth + 1)]
# 3列目以降のトライアングル構築
for row in range(2, depth):
for i in range(1, len(triangle[row]) - 1):
triangle[row][i] = triangle[row - 1][i - 1] + triangle[row - 1][i]
return triangle
if __name__ == '__main__':
S = Solution()
print(S.getRow(5))
# >>> [1, 5, 10, 10, 5, 1] | pascals-triangle-ii | Python and Go答え | namashin | 0 | 21 | pascals triangle ii | 119 | 0.598 | Easy | 1,124 |
https://leetcode.com/problems/pascals-triangle-ii/discuss/2419485/Solution-using-O(rowIndex)-space-or-python | class Solution:
def getRow(self, rowIndex: int) -> List[int]:
if rowIndex == 0:
return [1]
if rowIndex == 1:
return [1,1]
previousRow = self.getRow(rowIndex -1)
newRow = [None]*(rowIndex +1)
newRow[0] = newRow[-1] = 1
for i in range(1,rowIndex):
newRow[i] = previousRow[i-1] + previousRow[i]
return newRow | pascals-triangle-ii | Solution using O(rowIndex) space | python | Abhi_-_- | 0 | 64 | pascals triangle ii | 119 | 0.598 | Easy | 1,125 |
https://leetcode.com/problems/pascals-triangle-ii/discuss/2414650/python | class Solution:
def getRow(self, rowIndex: int) -> List[int]:
prev = []
ans = []
for i in range(0 , rowIndex + 1):
ans = []
for j in range(0 , i + 1):
if i == j or j == 0:
ans.append(1)
else:
ans.append(prev[j] + prev[j-1])
prev = ans
return ans | pascals-triangle-ii | python | akashp2001 | 0 | 27 | pascals triangle ii | 119 | 0.598 | Easy | 1,126 |
https://leetcode.com/problems/pascals-triangle-ii/discuss/2389202/Simple-DP-Bottom-UP-python-solution-or-faster-95 | class Solution:
def getRow(self, rowIndex: int) -> List[int]:
if rowIndex == 0:
return [1]
if rowIndex == 1:
return [1, 1]
ans = [[1], [1, 1]]
for x in range(1, rowIndex):
tmp = [1]
for k in range(len(ans[x]) - 1):
tmp.append(ans[x][k] + ans[x][k + 1])
tmp.append(1)
ans.append(tmp)
return ans[-1] | pascals-triangle-ii | Simple DP Bottom-UP python solution | faster 95% | BorisDvorkin | 0 | 55 | pascals triangle ii | 119 | 0.598 | Easy | 1,127 |
https://leetcode.com/problems/pascals-triangle-ii/discuss/2387609/Python-or-Clear-Solution | class Solution:
def getRow(self, rowIndex: int) -> List[int]:
temp = []
for i in range(rowIndex+1):
res = []
for j in range(i+1):
if j == 0 or j == i:
res.append(1)
else:
res.append(temp[i-1][j]+ temp[i-1][j-1])
temp.append(res)
return res | pascals-triangle-ii | Python | Clear Solution | Yauhenish | 0 | 39 | pascals triangle ii | 119 | 0.598 | Easy | 1,128 |
https://leetcode.com/problems/pascals-triangle-ii/discuss/2348098/Python-Top-95-Simple-One-Liner-with-Explanation | class Solution:
def getRow(self, rowIndex: int) -> List[int]:
return [int(factorial(rowIndex) / (factorial(rowIndex-i)*factorial(i))) for i in range(rowIndex + 1)] | pascals-triangle-ii | Python Top 95% Simple One-Liner with Explanation | drblessing | 0 | 67 | pascals triangle ii | 119 | 0.598 | Easy | 1,129 |
https://leetcode.com/problems/pascals-triangle-ii/discuss/2323154/O(n)timeBEATS-99.97-MEMORYSPEED-0ms-MAY-2022 | class Solution:
def getRow(self, rowIndex: int) -> List[int]:
res=[]
for i in range(rowIndex+1):
res.append([])
for j in range(i+1):
if j == 0 or j == i:
res[i].append(1)
else:
res[i].append(res[i - 1][j - 1] + res[i - 1][j])
return res[rowIndex] | pascals-triangle-ii | O(n)time/BEATS 99.97% MEMORY/SPEED 0ms MAY 2022 | cucerdariancatalin | 0 | 36 | pascals triangle ii | 119 | 0.598 | Easy | 1,130 |
https://leetcode.com/problems/pascals-triangle-ii/discuss/2312995/Python3-oror-96.53-Time-Efficient | class Solution:
def getRow(self, rowIndex: int) -> List[int]:
l=[[1]*(i+1) for i in range(rowIndex+1)]
if(rowIndex==0):
l[0]=[1]
else:
l[0]=[1]
l[1]=[1,1]
for i in range(2,len(l)):
for j in range(0,len(l[i])):
if(j==0 or j==len(l[i])+1):
continue
else:
l[i][j]=sum(l[i-1][j-1:j+1])
return l[-1] | pascals-triangle-ii | Python3 || 96.53% Time Efficient | KaushikDhola | 0 | 43 | pascals triangle ii | 119 | 0.598 | Easy | 1,131 |
https://leetcode.com/problems/triangle/discuss/2144882/Python-In-place-DP-with-Explanation | class Solution:
def minimumTotal(self, triangle: List[List[int]]) -> int:
for i in range(1, len(triangle)): # for each row in triangle (skipping the first),
for j in range(i+1): # loop through each element in the row
triangle[i][j] += min(triangle[i-1][j-(j==i)], # minimum sum from coordinate (x-1, y)
triangle[i-1][j-(j>0)]) # minimum sum from coordinate (x-1, y-1)
return min(triangle[-1]) # obtain minimum sum from last row | triangle | [Python] In-place DP with Explanation | zayne-siew | 18 | 1,400 | triangle | 120 | 0.54 | Medium | 1,132 |
https://leetcode.com/problems/triangle/discuss/2144882/Python-In-place-DP-with-Explanation | class Solution:
def minimumTotal(self, triangle: List[List[int]]) -> int:
for i in range(len(triangle)-2, -1, -1): # for each row in triangle (skipping the last),
for j in range(i+1): # loop through each element in the row
triangle[i][j] += min(triangle[i+1][j], # minimum sum from coordinate (x+1, y)
triangle[i+1][j+1]) # minimum sum from coordinate (x+1, y+1)
return triangle[0][0] | triangle | [Python] In-place DP with Explanation | zayne-siew | 18 | 1,400 | triangle | 120 | 0.54 | Medium | 1,133 |
https://leetcode.com/problems/triangle/discuss/2145142/Python-Easy-4-lines-or-two-approaches | class Solution:
def minimumTotal(self, a: List[List[int]]) -> int:
@cache
def dfs(level, i):
return 0 if level >= len(a) else a[level][i] + min(dfs(level + 1, i), dfs(level + 1, i+1))
return dfs(0, 0) | triangle | Python Easy 4 lines | two approaches ✅ | constantine786 | 16 | 1,200 | triangle | 120 | 0.54 | Medium | 1,134 |
https://leetcode.com/problems/triangle/discuss/2145142/Python-Easy-4-lines-or-two-approaches | class Solution:
def minimumTotal(self, a: List[List[int]]) -> int:
n=len(a)
dp=[[0] * (n+1) for _ in range(n+1)]
for level in range(n-1,-1,-1):
for i in range(level+1):
dp[level][i]=a[level][i] + min(dp[level+1][i], dp[level+1][i+1])
return dp[0][0] | triangle | Python Easy 4 lines | two approaches ✅ | constantine786 | 16 | 1,200 | triangle | 120 | 0.54 | Medium | 1,135 |
https://leetcode.com/problems/triangle/discuss/1520400/Python-In-place-DP%3A-Easy-to-understand-w-Explanation | class Solution:
def minimumTotal(self, triangle: List[List[int]]) -> int:
for i in range(1, len(triangle)): # for each row in triangle (skipping the first),
for j in range(i+1): # loop through each element in the row
triangle[i][j] += min(triangle[i-1][j-(j==i)], # minimum sum from coordinate (x-1, y)
triangle[i-1][j-(j>0)]) # minimum sum from coordinate (x-1, y-1)
return min(triangle[-1]) # obtain minimum sum from last row | triangle | Python In-place DP: Easy-to-understand w Explanation | zayne-siew | 6 | 286 | triangle | 120 | 0.54 | Medium | 1,136 |
https://leetcode.com/problems/triangle/discuss/2042819/Simple-Dynamic-Python-Solution-oror-Explained | class Solution(object):
def minimumTotal(self, triangle):
for i in range(len(triangle)-2,-1,-1):
for j in range(len(triangle[i])):
triangle[i][j] += min(triangle[i+1][j], triangle[i+1][j+1])
return triangle[0][0] | triangle | Simple Dynamic Python Solution || Explained | NathanPaceydev | 3 | 108 | triangle | 120 | 0.54 | Medium | 1,137 |
https://leetcode.com/problems/triangle/discuss/1791066/Python-3-(100ms)-or-Bottom-Up-DP-Solution | class Solution:
def minimumTotal(self, triangle: List[List[int]]) -> int:
if not triangle:
return
res = triangle[-1]
for i in range(len(triangle)-2, -1, -1):
for j in range(len(triangle[i])):
res[j] = min(res[j], res[j+1]) + triangle[i][j]
return res[0] | triangle | Python 3 (100ms) | Bottom Up DP Solution | MrShobhit | 3 | 120 | triangle | 120 | 0.54 | Medium | 1,138 |
https://leetcode.com/problems/triangle/discuss/2145497/Python-Simple-Solution-or-DP | class Solution:
def minimumTotal(self, triangle: List[List[int]]) -> int:
n, m = len(triangle), 1
@cache
def solve(c,r):
# base condition when we move further from the last row.
if r == n:
return 0
# base condition when we go out of bounds
if c < 0 or c >= len(triangle[r]):
return float('inf')
# after selecting the cell in the current row, then in the next row, we can select either the col with the same index or index + 1
return triangle[r][c] + min(solve(c, r+1),solve(c+1, r+1))
return solve(0,0) | triangle | ✅ Python Simple Solution | DP | Nk0311 | 2 | 142 | triangle | 120 | 0.54 | Medium | 1,139 |
https://leetcode.com/problems/triangle/discuss/2167674/DP-and-DFS-or-Beats-96-Time-or-Very-Clearly-Explained! | class Solution:
def minimumTotal(self, triangle: List[List[int]]) -> int:
n = len(triangle)
dp = [[0] * n for _ in range(n)]
dp[n-1] = triangle[n-1].copy()
for row in range(n-2,-1,-1):
for col in range(len(triangle[row])):
dp[row][col] = min(dp[row+1][col], dp[row+1][col+1]) + triangle[row][col]
return dp[0][0] | triangle | 🔥 DP and DFS | Beats 96% Time | Very Clearly Explained! 🔥 | PythonerAlex | 1 | 116 | triangle | 120 | 0.54 | Medium | 1,140 |
https://leetcode.com/problems/triangle/discuss/2167674/DP-and-DFS-or-Beats-96-Time-or-Very-Clearly-Explained! | class Solution:
def minimumTotal(self, triangle: List[List[int]]) -> int:
n = len(triangle)
@cache
def dfs(row: int, col: int) -> int:
if row == n - 1:
return triangle[row][col]
return triangle[row][col] + min(dfs(row+1, col), dfs(row+1, col+1))
return dfs(0, 0) | triangle | 🔥 DP and DFS | Beats 96% Time | Very Clearly Explained! 🔥 | PythonerAlex | 1 | 116 | triangle | 120 | 0.54 | Medium | 1,141 |
https://leetcode.com/problems/triangle/discuss/2166004/Easy-to-understand-no-fancy-one-liners. | class Solution:
def minimumTotal(self, triangle: List[List[int]]) -> int:
# dynamic programming - bottom up
if not triangle:
return
# traversing from second last row of triangle to top
for row in reversed(range(len(triangle)-1)):
for col in range(len(triangle[row])): # **
# compare value with its bottom value of left and right
# triangle = [[11], [9, 10], [7, 6, 10], [4, 1, 8, 3]]
triangle[row][col] += min(triangle[row+1][col],
triangle[row+1][col+1])
return triangle[0][0] | triangle | Easy to understand, no fancy one liners. | AJsenpai | 1 | 37 | triangle | 120 | 0.54 | Medium | 1,142 |
https://leetcode.com/problems/triangle/discuss/2165484/Python-3-easy-solution-DP | class Solution:
def minimumTotal(self, triangle: List[List[int]]) -> int:
dp = [0] * (len(triangle) + 1)
for num in triangle[::-1]:
for i in range(len(num)):
dp[i] = num[i] + min(dp[i],dp[i+1])
return dp[0] | triangle | Python 3 easy solution DP | notxkaran | 1 | 42 | triangle | 120 | 0.54 | Medium | 1,143 |
https://leetcode.com/problems/triangle/discuss/2147611/O(n)-space-92-sspace-efficient | class Solution:
def minimumTotal(self, triangle: List[List[int]]) -> int:
if not triangle:
return
res = triangle[-1]
for i in range(len(triangle)-2, -1, -1):
for j in range(len(triangle[i])):
res[j] = min(res[j], res[j+1]) + triangle[i][j]
return res[0] | triangle | O(n) space 92% sspace efficient | anuvabtest | 1 | 20 | triangle | 120 | 0.54 | Medium | 1,144 |
https://leetcode.com/problems/triangle/discuss/2147126/Python3-Solution-with-using-dynamic-programming | class Solution:
def minimumTotal(self, triangle: List[List[int]]) -> int:
for i in range(1, len(triangle)):
for j in range(len(triangle[i])):
if j == 0:
triangle[i][j] += triangle[i - 1][0]
elif j == len(triangle[i]) - 1:
triangle[i][j] += triangle[i - 1][-1]
else:
triangle[i][j] += min(triangle[i - 1][j - 1], triangle[i - 1][j])
return min(triangle[-1]) | triangle | [Python3] Solution with using dynamic programming | maosipov11 | 1 | 16 | triangle | 120 | 0.54 | Medium | 1,145 |
https://leetcode.com/problems/triangle/discuss/2146181/Python-3-Using-DP-and-a-Greedy-Approach | class Solution:
def minimumTotal(self, triangle: List[List[int]]) -> int:
for idx, nums in enumerate(triangle[1:], start=1):
for idx2, num in enumerate(nums):
if idx2 == 0:
triangle[idx][idx2] += triangle[idx - 1][0]
elif idx2 == len(nums) - 1:
triangle[idx][idx2] += triangle[idx - 1][-1]
else:
triangle[idx][idx2] += min(triangle[idx - 1][idx2 - 1], triangle[idx - 1][idx2])
return min(triangle[-1]) | triangle | [Python 3] Using DP and a Greedy Approach | seankala | 1 | 22 | triangle | 120 | 0.54 | Medium | 1,146 |
https://leetcode.com/problems/triangle/discuss/2145041/Python3-or-DP-or-easy-to-understand-or-simple-approach | class Solution:
def minimumTotal(self, triangle: List[List[int]]) -> int:
n = len(triangle)
dp = [[10e9 for _ in range(i+1)] for i in range(n)]
dp[0][0] = triangle[0][0]
for i in range(1, n):
for j in range(i):
dp[i][j] = dp[i-1][j] + triangle[i][j]
for j in range(1, i+1):
dp[i][j] = min(dp[i][j], triangle[i][j] + dp[i-1][j-1])
return min(dp[-1]) | triangle | Python3 | DP | easy to understand | simple approach | H-R-S | 1 | 75 | triangle | 120 | 0.54 | Medium | 1,147 |
https://leetcode.com/problems/triangle/discuss/2109891/Python3-Runtime%3A-66ms-94.29-memory%3A-15mb-36.36 | class Solution:
# 66ms 94.29%
def minimumTotal(self, triangle: List[List[int]]) -> int:
if not triangle:
return 0
dp = [0] * (len(triangle) + 1)
for i in reversed(range(len(triangle))):
for idx, val in enumerate(triangle[i]):
dp[idx] = min(dp[idx], dp[idx+1]) + val
return dp[0] | triangle | Python3 Runtime: 66ms 94.29%; memory: 15mb 36.36% | arshergon | 1 | 62 | triangle | 120 | 0.54 | Medium | 1,148 |
https://leetcode.com/problems/triangle/discuss/1169483/Python%3A-My-5-lines-neat-solution-with-O(n)-space-(without-messing-up-data) | class Solution:
def minimumTotal(self, triangle: List[List[int]]) -> int:
bottom = list(triangle[-1])
for row in reversed(triangle[:-1]):
for j in range(len(row)):
bottom[j] = row[j] + min(bottom[j], bottom[j+1])
return bottom[0] | triangle | Python: My 5 lines neat solution with O(n) space (without messing up data) | willy1222 | 1 | 82 | triangle | 120 | 0.54 | Medium | 1,149 |
https://leetcode.com/problems/triangle/discuss/693825/Python3-dynamic-programming | class Solution:
def minimumTotal(self, triangle: List[List[int]]) -> int:
@lru_cache(None)
def fn(i, j):
"""Return min path sum ending at (i, j)"""
if i < 0: return 0
if j < 0 or j > i: return inf
return triangle[i][j] + min(fn(i-1, j-1), fn(i-1, j))
n =len(triangle)
return min(fn(n-1, j) for j in range(n+1)) | triangle | [Python3] dynamic programming | ye15 | 1 | 69 | triangle | 120 | 0.54 | Medium | 1,150 |
https://leetcode.com/problems/triangle/discuss/693825/Python3-dynamic-programming | class Solution:
def minimumTotal(self, triangle: List[List[int]]) -> int:
ans = [0]*len(triangle)
for i, row in enumerate(triangle):
for j in reversed(range(len(row))):
if j == 0: ans[j] += row[j]
elif j == len(triangle[i])-1: ans[j] = ans[j-1] + row[j]
else: ans[j] = min(ans[j-1], ans[j]) + row[j]
return min(ans) | triangle | [Python3] dynamic programming | ye15 | 1 | 69 | triangle | 120 | 0.54 | Medium | 1,151 |
https://leetcode.com/problems/triangle/discuss/693825/Python3-dynamic-programming | class Solution:
def minimumTotal(self, triangle: List[List[int]]) -> int:
for i in reversed(range(len(triangle)-1)):
for j in range(i+1):
triangle[i][j] += min(triangle[i+1][j], triangle[i+1][j+1])
return triangle[0][0] | triangle | [Python3] dynamic programming | ye15 | 1 | 69 | triangle | 120 | 0.54 | Medium | 1,152 |
https://leetcode.com/problems/triangle/discuss/693825/Python3-dynamic-programming | class Solution:
def minimumTotal(self, triangle: List[List[int]]) -> int:
for i in range(1, len(triangle)):
for j in range(len(triangle[i])):
triangle[i][j] += min(triangle[i-1][max(0, j-1):j+1])
return min(triangle[-1]) | triangle | [Python3] dynamic programming | ye15 | 1 | 69 | triangle | 120 | 0.54 | Medium | 1,153 |
https://leetcode.com/problems/triangle/discuss/2845612/easy-python-solution | class Solution:
def minimumTotal(self, triangle: List[List[int]]) -> int:
for row in range(len(triangle) - 2, -1, -1):
next_row = row + 1
for i in range(len(triangle[row])):
triangle[row][i] = min(triangle[row][i] + triangle[next_row][i], triangle[row][i] + triangle[next_row][i + 1])
return min(triangle[0]) | triangle | easy python solution | PratikGehlot | 0 | 1 | triangle | 120 | 0.54 | Medium | 1,154 |
https://leetcode.com/problems/triangle/discuss/2826419/Top-Down-Solution-Python | class Solution:
def minimumTotal(self, triangle: List[List[int]]) -> int:
dp = [[-1 for _ in range(202)] for _ in range(202)]
def solve(i,j):
if dp[i][j] != -1:
return dp[i][j]
if i==len(triangle)-1:
return triangle[i][j]
dp[i][j] = triangle[i][j] + min(solve(i+1,j),solve(i+1,j+1))
return dp[i][j]
return solve(0,0) | triangle | Top Down Solution Python | omkarghugarkar7 | 0 | 2 | triangle | 120 | 0.54 | Medium | 1,155 |
https://leetcode.com/problems/triangle/discuss/2800176/python-5-lines-solution | class Solution:
def minimumTotal(self, triangle: List[List[int]]) -> int:
for i in range(len(triangle)-2, -1, -1):
for j in range(len(triangle[i])):
triangle[i][j] = min(triangle[i+1][j], triangle[i+1][j+1])+triangle[i][j]
return triangle[0][0] | triangle | python 5-lines solution | h3030666 | 0 | 5 | triangle | 120 | 0.54 | Medium | 1,156 |
https://leetcode.com/problems/triangle/discuss/2792502/Recursive-Python-Solution-with-Memoization-and-Space-Optimization | class Solution:
def minimumTotal(self, triangle: List[List[int]]) -> int:
global memo
memo = {}
def recurse(inp, i, j, n):
global memo
if (i,j) in memo:
return memo[(i,j)]
at_this_point = inp[j][i]
if j == n-1:
pass
else:
at_this_point += min([recurse(inp, i,j+1,n), recurse(inp,i+1,j+1,n)])
memo.pop((i,j+1))
memo[(i,j)] = at_this_point
return memo[(i,j)]
recurse(triangle, 0, 0, len(triangle))
return memo[(0,0)] | triangle | Recursive Python Solution with Memoization and Space Optimization | navrozcharania | 0 | 2 | triangle | 120 | 0.54 | Medium | 1,157 |
https://leetcode.com/problems/triangle/discuss/2763849/PYTHON-oror-easy-solu-using-oror-DP-oror-bottomup-faster-than-100 | class Solution:
def minimumTotal(self, triangle: List[List[int]]) -> int:
n=len(triangle)
m=n+1
dp=[]
for i in range (m):
dp.append([0]*m)
for i in range (n-2,-1,-1):
for j in range (i+1):
dp[i][j]=min(dp[i+1][j],dp[i+1][j+1])+triangle[i][j]
return dp[0][0] | triangle | PYTHON || easy solu using || DP || bottomup faster than 100% | tush18 | 0 | 2 | triangle | 120 | 0.54 | Medium | 1,158 |
https://leetcode.com/problems/triangle/discuss/2724224/simple-python-dynamic-programming | class Solution:
def minimumTotal(self, triangle: List[List[int]]) -> int:
dp=triangle
dp[0][0]=triangle[0][0]
for i in range(1,len(triangle)):
for j in range(len(triangle[i])):
if j==0:
dp[i][j]=dp[i-1][j]+triangle[i][j]
elif j==len(triangle[i])-1:
dp[i][j]=dp[i-1][j-1]+triangle[i][j]
else:
dp[i][j]=min(dp[i-1][j-1],dp[i-1][j])+triangle[i][j]
return min(dp[len(triangle)-1]) | triangle | simple python dynamic programming | awasthidevesh02 | 0 | 3 | triangle | 120 | 0.54 | Medium | 1,159 |
https://leetcode.com/problems/triangle/discuss/2715768/Python-or-2-d-dynamic-programming-solution | class Solution:
def minimumTotal(self, t: List[List[int]]) -> int:
dp = [[0 for _ in range(len(t[j]))] for j in range(len(t))]
# Initialize top of dp matrix
dp[0][0] = t[0][0]
for i in range(1, len(t)):
for j in range(len(t[i])):
# On each step we choose minimum number from above cell or from cell (row - 1, col - 1)
dp[i][j] = min(dp[i - 1][min(j, len(t[i - 1]) - 1)], dp[i - 1][max(j - 1, 0)]) + t[i][j]
# Finally answer is the minimum number from last row!
return min(dp[-1]) | triangle | Python | 2-d dynamic programming solution | LordVader1 | 0 | 9 | triangle | 120 | 0.54 | Medium | 1,160 |
https://leetcode.com/problems/triangle/discuss/2686640/python-easy-working-solution-or-dp-or-99 | class Solution:
def minimumTotal(self, t: List[List[int]]) -> int:
for i in range(len(t)-2,-1,-1):
for j in range(len(t[i])):
t[i][j] = t[i][j] + min(t[i+1][j] , t[i+1][j+1])
return t[0][0] | triangle | python easy working solution | dp | 99% | Sayyad-Abdul-Latif | 0 | 22 | triangle | 120 | 0.54 | Medium | 1,161 |
https://leetcode.com/problems/triangle/discuss/2670901/DP-with-recursion. | class Solution:
def minimumTotal(self, triangle: List[List[int]]) -> int:
mem = {}
def recurse(i, j):
if (i, j) in mem:
return mem[i,j]
if i > len(triangle) - 1:
return 0
mem[i,j] = min(recurse(i+1, j) + triangle[i][j], recurse(i+1, j+1) + triangle[i][j])
return mem[i,j]
return recurse(0, 0) | triangle | DP with recursion. | michaelniki | 0 | 4 | triangle | 120 | 0.54 | Medium | 1,162 |
https://leetcode.com/problems/triangle/discuss/2670901/DP-with-recursion. | class Solution:
def minimumTotal(self, triangle: List[List[int]]) -> int:
dp = triangle[-1]
for i in range(len(triangle) - 1, -1, -1):
print(dp)
for j in range(i):
dp[j] = min(dp[j], dp[j+1]) + triangle[i-1][j]
return dp[0] | triangle | DP with recursion. | michaelniki | 0 | 4 | triangle | 120 | 0.54 | Medium | 1,163 |
https://leetcode.com/problems/triangle/discuss/2670901/DP-with-recursion. | class Solution:
def minimumTotal(self, triangle: List[List[int]]) -> int:
mem = {}
def recurse(i, j, curr):
if (i, j) in mem:
return mem[i, j] # adding mem results in wrong answer.
if i > len(triangle) - 1:
return curr
mem[i,j] = min(recurse(i+1, j, curr + triangle[i][j]), recurse(i+1, j+1, curr + triangle[i][j]))
print(mem[i,j], i, j)
# position (3, 1) is calculated and memorized before the minimum path is found.
return mem[i,j]
return recurse(0, 0, 0) | triangle | DP with recursion. | michaelniki | 0 | 4 | triangle | 120 | 0.54 | Medium | 1,164 |
https://leetcode.com/problems/triangle/discuss/2663058/Python-and-Golang | class Solution:
def make_triangle(self, depth: int = 5) -> List[List[int]]:
"""
If you want to use
:param depth: triangle depth
:return: triangle made by random number
"""
triangle = []
for i in range(1, depth + 1):
line = []
for j in range(i):
line.append(random.randint(0, 9))
triangle.append(line)
return triangle
def minimumTotal(self, triangle: List[List[int]]) -> int:
for i in range(1, len(triangle)):
for j in range(len(triangle[i])):
if j == 0:
triangle[i][j] += triangle[i - 1][j]
elif j == len(triangle[i]) - 1:
triangle[i][j] += triangle[i - 1][j - 1]
else:
triangle[i][j] += min(triangle[i - 1][j], triangle[i - 1][j - 1])
return min(triangle[-1]) | triangle | Python and Golang答え | namashin | 0 | 10 | triangle | 120 | 0.54 | Medium | 1,165 |
https://leetcode.com/problems/triangle/discuss/2577030/python-easy-peasy!! | class Solution:
def minimumTotal(self, triangle: List[List[int]]) -> int:
dp={}
def dynamic(col,row):
if row==len(triangle)-1:
return triangle[row][col]
if (col,row) in dp:
return dp[(col,row)]
dp[(col,row)]=triangle[row][col]+min(dynamic(col,row+1),dynamic(col+1,row+1))
return dp[(col,row)]
return dynamic(0,0) | triangle | python easy-peasy!! | benon | 0 | 61 | triangle | 120 | 0.54 | Medium | 1,166 |
https://leetcode.com/problems/triangle/discuss/2503942/python3-dp-solution-easy-understanding | class Solution:
def minimumTotal(self, triangle: List[List[int]]) -> int:
@cache
def dp(i :int,j :int) -> int:
if i==0:
return triangle[0][0]
if j==0:
return triangle[i][j]+dp(i-1,j)
if j==i:
return triangle[i][j]+dp(i-1,j-1)
return triangle[i][j]+min(dp(i-1,j-1),dp(i-1,j))
return min(dp(len(triangle)-1,i) for i in range(len(triangle))) | triangle | python3 dp solution easy-understanding | kyoko0810 | 0 | 25 | triangle | 120 | 0.54 | Medium | 1,167 |
https://leetcode.com/problems/triangle/discuss/2317906/Python3-3-Approaches | class Solution:
def minimumTotal(self, triangle: List[List[int]]) -> int:
numRow = len(triangle)
def recurHelper(r, c) -> int:
if r == numRow-1:
return triangle[r][c]
return triangle[r][c] + min(recurHelper(r+1, c),
recurHelper(r+1, c+1))
return recurHelper(0, 0) | triangle | [Python3] 3 Approaches | ruosengao | 0 | 21 | triangle | 120 | 0.54 | Medium | 1,168 |
https://leetcode.com/problems/triangle/discuss/2317906/Python3-3-Approaches | class Solution:
def minimumTotal(self, triangle: List[List[int]]) -> int:
numRow = len(triangle)
memo = dict()
def recurHelper(r, c) -> int:
if (r,c) in memo:
return memo[(r,c)]
elif r == numRow-1:
res = triangle[r][c]
else:
res = triangle[r][c] + min(recurHelper(r+1, c),
recurHelper(r+1, c+1))
memo[(r,c)] = res
return res
return recurHelper(0, 0) | triangle | [Python3] 3 Approaches | ruosengao | 0 | 21 | triangle | 120 | 0.54 | Medium | 1,169 |
https://leetcode.com/problems/triangle/discuss/2317906/Python3-3-Approaches | class Solution:
def minimumTotal(self, triangle: List[List[int]]) -> int:
n = len(triangle)
memo = {i:triangle[-1][i] for i in range(n)}
r = n-2
while r >= 0:
for c,cost in enumerate(triangle[r]):
memo[c] = cost + min(memo[c], memo[c+1])
r -= 1
return memo[0] | triangle | [Python3] 3 Approaches | ruosengao | 0 | 21 | triangle | 120 | 0.54 | Medium | 1,170 |
https://leetcode.com/problems/triangle/discuss/2304077/Python3-oror-DP-solution-easy-to-understand | class Solution:
def minimumTotal(self, triangle: List[List[int]]) -> int:
rows = len(triangle)
if rows == 1:
return triangle[0][0]
for r in range(1, rows):
cols = r+1
for c in range(cols):
left = triangle[r-1][c-1] if c-1 >= 0 else triangle[r-1][c]
right = triangle[r-1][c] if c+1 < cols else triangle[r-1][c-1]
triangle[r][c] += min(left, right)
return min(triangle[-1]) | triangle | Python3 || DP solution easy to understand | sagarhasan273 | 0 | 12 | triangle | 120 | 0.54 | Medium | 1,171 |
https://leetcode.com/problems/triangle/discuss/2147188/Python3-DP-(topdown)-approach | class Solution:
def minimumTotal(self, triangle) -> int:
depth = len(triangle) - 2
while depth != -1:
for i in range(len(triangle[depth])):
triangle[depth][i] = triangle[depth][i] + min(triangle[depth + 1][i], triangle[depth + 1][i + 1])
depth -= 1
return triangle[0][0] | triangle | Python3 DP-(top/down) approach | _mKamal | 0 | 8 | triangle | 120 | 0.54 | Medium | 1,172 |
https://leetcode.com/problems/triangle/discuss/2146955/Dp | class Solution:
def minimumTotal(self, triangle: List[List[int]]) -> int:
if len(triangle) == 1:
return triangle[0][0]
dp =[ [0 for i in range(len(triangle[j]))] for j in range(len(triangle))]
for i in range(len(triangle[-1])):
dp[-1][i] = triangle[-1][i]
for j in range(len(triangle)-2, -1, -1):
for i in range(len(triangle[j])):
dp[j][i] = min(triangle[j][i] + dp[j+1][i],triangle[j][i] + dp[j+1][i+1])
return dp[0][0] | triangle | Dp | Ashi_garg | 0 | 9 | triangle | 120 | 0.54 | Medium | 1,173 |
https://leetcode.com/problems/triangle/discuss/2146950/Python-C%2B%2B-4-Liner-*SIMPLE* | class Solution:
def minimumTotal(self, triangle: List[List[int]]) -> int:
# iterate from the second last row to the top
for row in range(len(triangle)-2, -1, -1):
for col in range(0, row+1):
# add the lower of the 2 values below our current position to the current value
# 2 7
# / \ -> / \
# 5 10 5 10
triangle[row][col] += min(triangle[row+1][col], triangle[row+1][col+1])
return triangle[0][0] | triangle | Python / C++ 4 Liner *SIMPLE* | manuel_lemos | 0 | 12 | triangle | 120 | 0.54 | Medium | 1,174 |
https://leetcode.com/problems/triangle/discuss/2146733/python-simple-87-time-92-space | class Solution:
def minimumTotal(self, triangle: List[List[int]]) -> int:
for i in range(1,len(triangle)):
triangle[i][0] += triangle[i-1][0]
triangle[i][-1] += triangle[i-1][-1]
for i in range(2,len(triangle)):
for j in range(1,i):
triangle[i][j] += min(triangle[i-1][j-1], triangle[i-1][j])
return min(triangle[-1]) | triangle | python, simple, 87% time, 92% space | pjy953 | 0 | 7 | triangle | 120 | 0.54 | Medium | 1,175 |
https://leetcode.com/problems/triangle/discuss/2146588/Python3-Recursive-greater-DP-Recursive-greater-DP-Iterative-greater-DP-In-Place | class Solution:
# Follow-up Solution DP Iterative, Time = O(N), Space = O(1), In-Place Algorithm
def minimumTotal(self, triangle: List[List[int]]) -> int:
for lvl in range(len(triangle) - 2, -1, -1):
for idx in range(len(triangle[lvl])):
triangle[lvl][idx] += min(triangle[lvl + 1][idx], triangle[lvl + 1][idx + 1])
return triangle[0][0]
# DP Iterative, Time = O(N), Space = O(triangle)
# def minimumTotal(self, triangle: List[List[int]]) -> int:
# sum_cp = triangle
# for lvl in range(len(triangle) - 2, -1, -1):
# for idx in range(len(triangle[lvl])):
# sum_cp[lvl][idx] += min(sum_cp[lvl + 1][idx], sum_cp[lvl + 1][idx + 1])
# return sum_cp[0][0]
# DP Recursion, Time = O(N), Space = O(triangle)
# def minimumTotal(self, triangle: List[List[int]]) -> int:
# sum_dp = [[[False, 0] for j in range(len(triangle[i]))] for i in range(len(triangle))]
# self.rec(triangle, sum_dp, 0, 0, len(triangle) - 1)
# return sum_dp[0][0][1]
#
# def rec(self, triangle, sum_dp, lvl, idx, hgt):
# if sum_dp[lvl][idx][0] == True:
# return sum_dp[lvl][idx][1]
# elif lvl == hgt:
# sum_dp[lvl][idx][0] = True
# sum_dp[lvl][idx][1] = triangle[lvl][idx]
# else:
# sum_dp[lvl][idx][0] = True
# sum_dp[lvl][idx][1] = min(self.rec(triangle, sum_dp, lvl + 1, idx, hgt), self.rec(triangle, sum_dp, lvl + 1, idx + 1, hgt)) + triangle[lvl][idx]
# return sum_dp[lvl][idx][1]
# Simple Recursion, Time = O(2^N), Space = O(2^N), TLE
# def minimumTotal(self, triangle: List[List[int]]) -> int:
# return self.rec(triangle, 0, 0, len(triangle) - 1)
#
# def rec(self, triangle, lvl, idx, hgt):
# if lvl == hgt:
# return triangle[lvl][idx]
# return min(self.rec(triangle, lvl + 1, idx, hgt), self.rec(triangle, lvl + 1, idx + 1, hgt)) + triangle[lvl][idx] | triangle | [Python3] Recursive -> DP-Recursive -> DP-Iterative -> DP-In-Place | betaRobin | 0 | 4 | triangle | 120 | 0.54 | Medium | 1,176 |
https://leetcode.com/problems/triangle/discuss/2146304/Python-3-Solution-oror-Depth-First-Search-oror-Dynamic-Programming-Solution | class Solution:
def minimumTotal(self, triangle: List[List[int]]) -> int:
"""
An array of 0's for leaf node sum
[[2],
[3,4],
[6,5,7],
[4,1,8,3]]
[0,0,0,0,0]
"""
dp = [0]*(len(triangle)+1)
"""
Explanation :
Considering [4,1,8,3] (row 4)
we are at 4 currently i.e (row4[i]) and i = 0, so we will check =>
min(4+dp[i], 4+dp[i+1]) ==> min(4+0, 4+0)
And then replace dp[i] = min(4+dp[i], 4+dp[i+1])
After this inner loop iteration our dp will transform into
[4,1,8,3,0]
_ _ _ _
After Step 2 iteration dp will look like => [7,6,10,3,0]
updated values====> _ _ _
After Step 3 itertaion dp will be => [9,10,10,3,0]
updated values====> _ _
After step 4 iteration dp will become => [11,10,10,3,0]
updated values====> __
And in this way at the end our answer will be at 0th position
"""
# We will loop through array in reverse order for performing bottom up DFS
for row in triangle[::-1]:
# Now we will loop through every value in that list and find minimum sum with value in dp
for i, n in enumerate(row):
dp[i] = n+min(dp[i],dp[i+1])
return dp[0] | triangle | ✅ Python 3 Solution || Depth First Search || Dynamic Programming Solution | mitchell000 | 0 | 13 | triangle | 120 | 0.54 | Medium | 1,177 |
https://leetcode.com/problems/triangle/discuss/2145933/python-java-iterative-DP | class Solution:
def minimumTotal(self, triangle: List[List[int]]) -> int:
for i in range(1, len(triangle)) :
triangle[i][0] += triangle[i-1][0]
L = len(triangle[i]) - 1
for j in range (1, L):
triangle[i][j] += min(triangle[i-1][j-1], triangle[i-1][j])
triangle[i][L] += triangle[i-1][L-1]
return min(triangle[-1]) | triangle | python, java - iterative DP | ZX007java | 0 | 15 | triangle | 120 | 0.54 | Medium | 1,178 |
https://leetcode.com/problems/triangle/discuss/2145501/Python-Solution-using-Memoization | class Solution:
def minimumTotal(self, triangle: List[List[int]]) -> int:
length = len(triangle)
dp = [[None for j in range(length)] for i in range(length)]
def dfs(i,j):
if i == length:
return 0
if dp[i][j] != None:
return dp[i][j]
dp[i][j] = triangle[i][j] + min(dfs(i+1,j), dfs(i+1,j+1))
return dp[i][j]
return dfs(0,0) | triangle | Python Solution using Memoization | ritik_chail | 0 | 17 | triangle | 120 | 0.54 | Medium | 1,179 |
https://leetcode.com/problems/triangle/discuss/2144922/Python-DP-in-place | class Solution:
def minimumTotal(self, triangle: List[List[int]]) -> int:
for r in range(len(triangle)-2, -1, -1):
for c in range(r, -1, -1):
triangle[r][c] += min(triangle[r+1][c], triangle[r+1][c+1])
return triangle[0][0] | triangle | Python, DP in-place | blue_sky5 | 0 | 15 | triangle | 120 | 0.54 | Medium | 1,180 |
https://leetcode.com/problems/triangle/discuss/2144922/Python-DP-in-place | class Solution:
def minimumTotal(self, triangle: List[List[int]]) -> int:
for r in range(1, len(triangle)):
triangle[r][0] += triangle[r-1][0]
triangle[r][-1] += triangle[r-1][-1]
for c in range(1, r):
triangle[r][c] += min(triangle[r-1][c], triangle[r-1][c-1])
return min(triangle[-1]) | triangle | Python, DP in-place | blue_sky5 | 0 | 15 | triangle | 120 | 0.54 | Medium | 1,181 |
https://leetcode.com/problems/triangle/discuss/2088211/Python-O(1)-space-bottom-up-approach | class Solution:
def minimumTotal(self, triangle: List[List[int]]) -> int:
for j in range(len(triangle)-2, -1, -1):
for i in range(j+1):
k = triangle[j][i]
triangle[j][i] = min(k + triangle[j+1][i], k + triangle[j+1][i+1])
return triangle[0][0] | triangle | Python O(1) space bottom-up approach | S-c-r-a-t-c-h-y | 0 | 14 | triangle | 120 | 0.54 | Medium | 1,182 |
https://leetcode.com/problems/triangle/discuss/2066807/Python-or-DP | class Solution:
def minimumTotal(self, tr: List[List[int]]) -> int:
for i in range(len(tr)-2,-1,-1):
for j in range(i+1):
tr[i][j] += min(tr[i+1][j],tr[i+1][j+1])
return tr[0][0] | triangle | Python | DP | Shivamk09 | 0 | 26 | triangle | 120 | 0.54 | Medium | 1,183 |
https://leetcode.com/problems/triangle/discuss/2050022/Dp-Solution-in-Python | class Solution:
def minimumTotal(self, triangle: List[List[int]]) -> int:
n=len(triangle)
for i in range(n-2,-1,-1):
for j in range(0,i+1):
triangle[i][j]=triangle[i][j]+min(triangle[i+1][j],triangle[i+1][j+1])
return triangle[0][0] | triangle | Dp Solution in Python | jayagupta752 | 0 | 52 | triangle | 120 | 0.54 | Medium | 1,184 |
https://leetcode.com/problems/triangle/discuss/2036354/Python-simple-solution | class Solution:
def minimumTotal(self, triangle: List[List[int]]) -> int:
pre = [0] * len(triangle[-1])
while triangle:
cur = triangle.pop()
cur = [p+c for p, c in zip(pre, cur)]
for i in range(len(cur)-1):
cur[i] = min(cur[i], cur[i+1])
pre = cur
return min(pre) | triangle | Python simple solution | Takahiro_Yokoyama | 0 | 30 | triangle | 120 | 0.54 | Medium | 1,185 |
https://leetcode.com/problems/triangle/discuss/1991717/Iterative-DP-Fast-solution(Time-Comp%3A-O(n)-Space-Comp(1))-No-extra-space | class Solution:
def minimumTotal(self, triangle: List[List[int]]) -> int:
n = len(triangle)
if n == 1:
return triangle[0][0]
for i in range (n-2, -1, -1):
for j in range(0, i+1):
triangle[i][j] += min(triangle[i+1][j], triangle[i+1][j+1])
print(triangle)
return triangle[0][0] | triangle | Iterative DP Fast solution(Time Comp: O(n), Space Comp(1)), No extra space | dbansal18 | 0 | 24 | triangle | 120 | 0.54 | Medium | 1,186 |
https://leetcode.com/problems/triangle/discuss/1991717/Iterative-DP-Fast-solution(Time-Comp%3A-O(n)-Space-Comp(1))-No-extra-space | class Solution:
def solution(self, triangle: List[List[int]], dp: List[List[int]], i: int, j: int, n: int) -> int:
if i == (n-1):
dp[i][j] = triangle[i][j]
return triangle[i][j]
if i >= n:
return 0
if dp[i][j] != -1:
return dp[i][j]
smallAns1 = triangle[i][j] + self.solution(triangle, dp, i+1, j, n)
smallAns2 = triangle[i][j] + self.solution(triangle, dp, i+1, j+1, n)
dp[i][j] = min(smallAns1, smallAns2)
return dp[i][j]
def minimumTotal(self, triangle: List[List[int]]) -> int:
n = len(triangle)
dp = [[-1 for i in range(1,j+1)] for j in range(1,n+1)]
ans = self.solution(triangle, dp, 0, 0, n)
return ans | triangle | Iterative DP Fast solution(Time Comp: O(n), Space Comp(1)), No extra space | dbansal18 | 0 | 24 | triangle | 120 | 0.54 | Medium | 1,187 |
https://leetcode.com/problems/triangle/discuss/1926282/DP-solution-in-python3-easy-to-understand | class Solution:
def minimumTotal(self, triangle: List[List[int]]) -> int:
@cache
def path(r,c):
if r>=len(triangle):
return 0
if c>=len(triangle[r]):
return 0
return triangle[r][c]+min(path(r+1,c),path(r+1,c+1))
ans=path(0,0)
return ans
``` | triangle | DP solution in python3, easy to understand | prateek4463 | 0 | 18 | triangle | 120 | 0.54 | Medium | 1,188 |
https://leetcode.com/problems/triangle/discuss/1840338/Python-oror-Tabulation | class Solution:
def minimumTotal(self, triangle: List[List[int]]) -> int:
n = len(triangle)
dp = [[-1] * n for x in range(n)]
for j in range(n):
dp[n-1][j] = triangle[n-1][j]
i = n-2
while(i>=0):
j = i
while(j>=0):
down = triangle[i][j] + dp[i+1][j]
dia = triangle[i][j] + dp[i+1][j+1]
dp[i][j] = min(down,dia)
j -= 1
i -= 1
return dp[0][0] | triangle | Python || Tabulation | LittleMonster23 | 0 | 44 | triangle | 120 | 0.54 | Medium | 1,189 |
https://leetcode.com/problems/best-time-to-buy-and-sell-stock/discuss/1545423/Python-easy-to-understand-solution-with-explanation-%3A-Tracking-and-Dynamic-programming | class Solution(object):
def maxProfit(self, prices):
n = len(prices)
dp = [0]*n # initializing the dp table
dp[0] = [prices[0],0] # filling the the first dp table --> low_price = prices[0] max_profit=0
min_price = max_profit = 0
# Note that ---> indixing the dp table --> dp[i-1][0] stores minimum price and dp[i-1][1] stores maximum profit
for i in range(1,n):
min_price = min(dp[i-1][0], prices[i]) # min(previous_min_price, cur_min_price)
max_profit = max(dp[i-1][1], prices[i]-dp[i-1][0]) # max(previoius_max_profit, current_profit)
dp[i] =[min_price,max_profit]
return dp[n-1][1]
#Runtime: 1220 ms,
#Memory Usage: 32.4 MB, | best-time-to-buy-and-sell-stock | Python easy to understand solution with explanation : Tracking and Dynamic programming | Abeni | 58 | 3,200 | best time to buy and sell stock | 121 | 0.544 | Easy | 1,190 |
https://leetcode.com/problems/best-time-to-buy-and-sell-stock/discuss/2433981/Easy-oror-100-oror-Kadane's-Algorithm-(Java-C%2B%2B-Python-JS-Python3)-oror-Min-so-far | class Solution(object):
def maxProfit(self, prices):
if len(prices) == 0: return 0
else:
profit = 0
minBuy = prices[0]
for i in range(len(prices)):
profit = max(prices[i] - minBuy, profit)
minBuy = min(minBuy, prices[i])
return profit | best-time-to-buy-and-sell-stock | Easy || 100% || Kadane's Algorithm (Java, C++, Python, JS, Python3) || Min so far | PratikSen07 | 48 | 2,900 | best time to buy and sell stock | 121 | 0.544 | Easy | 1,191 |
https://leetcode.com/problems/best-time-to-buy-and-sell-stock/discuss/2433981/Easy-oror-100-oror-Kadane's-Algorithm-(Java-C%2B%2B-Python-JS-Python3)-oror-Min-so-far | class Solution:
def maxProfit(self, prices: List[int]) -> int:
if len(prices) == 0: return 0
else:
profit = 0
minBuy = prices[0]
for i in range(len(prices)):
profit = max(prices[i] - minBuy, profit)
minBuy = min(minBuy, prices[i])
return profit | best-time-to-buy-and-sell-stock | Easy || 100% || Kadane's Algorithm (Java, C++, Python, JS, Python3) || Min so far | PratikSen07 | 48 | 2,900 | best time to buy and sell stock | 121 | 0.544 | Easy | 1,192 |
https://leetcode.com/problems/best-time-to-buy-and-sell-stock/discuss/2040282/O(n)timeBEATS-99.97-MEMORYSPEED-0ms-MAY-2022 | class Solution:
def maxProfit(self,prices):
left = 0 #Buy
right = 1 #Sell
max_profit = 0
while right < len(prices):
currentProfit = prices[right] - prices[left] #our current Profit
if prices[left] < prices[right]:
max_profit =max(currentProfit,max_profit)
else:
left = right
right += 1
return max_profit | best-time-to-buy-and-sell-stock | O(n)time/BEATS 99.97% MEMORY/SPEED 0ms MAY 2022 | cucerdariancatalin | 28 | 3,200 | best time to buy and sell stock | 121 | 0.544 | Easy | 1,193 |
https://leetcode.com/problems/best-time-to-buy-and-sell-stock/discuss/1161290/Simple-O(n)-python-solution | class Solution:
def maxProfit(self, prices: List[int]) -> int:
max_profit = 0
best_price_to_buy = prices[0]
for price in prices:
if price < best_price_to_buy:
best_price_to_buy = price
elif price > best_price_to_buy:
profit = price - best_price_to_buy
if profit > max_profit:
max_profit = profit
return max_profit | best-time-to-buy-and-sell-stock | Simple O(n) python solution | kuanyshbekuly | 22 | 2,300 | best time to buy and sell stock | 121 | 0.544 | Easy | 1,194 |
https://leetcode.com/problems/best-time-to-buy-and-sell-stock/discuss/400573/Python3-beats-93.61-with-comments | class Solution:
def maxProfit(self, prices: List[int]) -> int:
# Max profit and price to buy
max_profit = 0
high_buy = 0
# Reverse the array of prices
prices = prices[::-1]
# Check each price to sell at compared to the highest buy price ahead of it
for price in prices:
# Update highest buy price in front of price
if price > high_buy:
high_buy = price
# Check if this sale make higher profit than sales later in original array
if high_buy - price > max_profit:
max_profit = high_buy - price
# Return the highest profit achieved
return max_profit | best-time-to-buy-and-sell-stock | Python3 beats 93.61% with comments | jhacker | 13 | 4,700 | best time to buy and sell stock | 121 | 0.544 | Easy | 1,195 |
https://leetcode.com/problems/best-time-to-buy-and-sell-stock/discuss/1735814/Python-3-O(n)-SImple-Solution-Explained | class Solution:
def maxProfit(self, prices: List[int]) -> int:
# tracking the minimum while traverse
# and maximizing the variable
current_min, max_so_far = float('inf'), 0
for price in prices:
current_min = min(current_min, price)
max_so_far = max(max_so_far, price-current_min)
return max_so_far | best-time-to-buy-and-sell-stock | [Python 3] O(n) SImple Solution, Explained | codesankalp | 6 | 484 | best time to buy and sell stock | 121 | 0.544 | Easy | 1,196 |
https://leetcode.com/problems/best-time-to-buy-and-sell-stock/discuss/341798/Solution-in-Python-3 | class Solution:
def maxProfit(self, prices: List[int]) -> int:
m, mp = float('inf'), 0
for p in prices:
if p < m: m = p
if p - m > mp: mp = p - m
return mp
- Python 3
- Junaid Mansuri | best-time-to-buy-and-sell-stock | Solution in Python 3 | junaidmansuri | 6 | 3,000 | best time to buy and sell stock | 121 | 0.544 | Easy | 1,197 |
https://leetcode.com/problems/best-time-to-buy-and-sell-stock/discuss/2505473/Python3%3A-Easy-O(N)-solution-one-pass-method | class Solution:
def maxProfit(self, prices: List[int]) -> int:
cheap = prices[0]
res = 0
for i in range (1, len(prices)):
cheap = min(cheap, prices[i])
res = max(res, prices[i] - cheap)
return res | best-time-to-buy-and-sell-stock | Python3: Easy O(N) solution --one pass method | KohsukeIde | 5 | 368 | best time to buy and sell stock | 121 | 0.544 | Easy | 1,198 |
https://leetcode.com/problems/best-time-to-buy-and-sell-stock/discuss/2692912/Python-2-Easy-Way-To-Solve-or-95-Faster-or-Fast-and-Simple-Solution | class Solution:
def maxProfit(self, prices: List[int]) -> int:
maxSoFar = 0
buy = prices[0]
for i in range(1, len(prices)):
if prices[i]-buy > maxSoFar:
maxSoFar = prices[i]-buy
buy = min(buy, prices[i])
return maxSoFar | best-time-to-buy-and-sell-stock | ✔️ Python 2 Easy Way To Solve | 95% Faster | Fast and Simple Solution | pniraj657 | 4 | 538 | best time to buy and sell stock | 121 | 0.544 | Easy | 1,199 |
Subsets and Splits