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/n-th-tribonacci-number/discuss/2547199/EASY-PYTHON3-SOLUTION | class Solution:
def tribonacci(self, n: int) -> int:
if n == 0:
return 0
# this is our base cases
x1, x2, x3 = 0, 1, 1
# we end at n+1 because we need to include the "n" itself
for i in range(3, n+1):
# this is just a short cut instead of using temp variables to... | n-th-tribonacci-number | 🔥 EASY PYTHON3 SOLUTION 🔥 | rajukommula | 0 | 24 | n th tribonacci number | 1,137 | 0.633 | Easy | 17,700 |
https://leetcode.com/problems/n-th-tribonacci-number/discuss/2395407/python3-or-Memoization | class Solution:
def tribonacci(self, n: int) -> int:
cache = {0: 0, 1: 1, 2: 1}
return self.recurse(n, cache)
def recurse(self, n, cache):
if n in cache:
return cache[n]
if n not in cache:
cache[n] ... | n-th-tribonacci-number | python3 | Memoization | Gilbert770 | 0 | 18 | n th tribonacci number | 1,137 | 0.633 | Easy | 17,701 |
https://leetcode.com/problems/n-th-tribonacci-number/discuss/2258750/4-line-of-Code-orororor-Python-Simple-Python-Solution-ororororor-100-Optimal-Solution | class Solution:
def tribonacci(self, n: int) -> int:
a = [0, 1, 1]
for i in range(3,n+1):
a.append(sum(a[i-3:i]))
return a[n] | n-th-tribonacci-number | 4 line of Code |||| [ Python ] ✅ Simple Python Solution ✅✅ ||||| ✅100% Optimal Solution | vaibhav0077 | 0 | 43 | n th tribonacci number | 1,137 | 0.633 | Easy | 17,702 |
https://leetcode.com/problems/n-th-tribonacci-number/discuss/2232018/Fast-and-easy-solution | class Solution:
def tribonacci(self, n: int) -> int:
nums = [0,1,1]
nums1,nums2,nums3,new = 0,1,1,0
for i in range(n):
new = nums1 + nums2 + nums3
nums1 = nums2
nums2 = nums3
nums3 = new
nums.append(new)
return nums[n] | n-th-tribonacci-number | Fast and easy solution | kollee001 | 0 | 32 | n th tribonacci number | 1,137 | 0.633 | Easy | 17,703 |
https://leetcode.com/problems/n-th-tribonacci-number/discuss/2169538/Python-runtime-51.86-memory-58.81 | class Solution:
def tribonacci(self, n: int) -> int:
d = {0:0, 1:1, 2:1}
return self.recur(n, d)
def recur(self, n, d):
if n >= 3:
d[n] = self.recur(n-1, d) + d[n-2] + d[n-3]
return d[n]
return d[n] | n-th-tribonacci-number | Python, runtime 51.86%, memory 58.81% | tsai00150 | 0 | 54 | n th tribonacci number | 1,137 | 0.633 | Easy | 17,704 |
https://leetcode.com/problems/n-th-tribonacci-number/discuss/2169538/Python-runtime-51.86-memory-58.81 | class Solution:
def tribonacci(self, n: int) -> int:
d = {0:0, 1:1, 2:1}
if n <= 2:
return d[n]
for i in range(3, n+1):
d[i] = d[i-1] + d[i-2] + d[i-3]
return d[n] | n-th-tribonacci-number | Python, runtime 51.86%, memory 58.81% | tsai00150 | 0 | 54 | n th tribonacci number | 1,137 | 0.633 | Easy | 17,705 |
https://leetcode.com/problems/n-th-tribonacci-number/discuss/2157435/Python-N-th-Tribonacci-Number-(Easy-way) | class Solution:
def tribonacci(self, n: int) -> int:
a, b, c = 0, 1, 1
# T_3 = 0 + 1 + 1 = 2
# T_4 = 1 + 1 + 2 = 4
# T-5 = 1 + 2 + 4 = 7
if n == 0:
return 0
for i in range(n - 2):
a, b, c = b, c, a + b + c
return c | n-th-tribonacci-number | [Python] N-th Tribonacci Number (Easy way) | YangJenHao | 0 | 26 | n th tribonacci number | 1,137 | 0.633 | Easy | 17,706 |
https://leetcode.com/problems/n-th-tribonacci-number/discuss/2118346/Python-simple-solution | class Solution:
def tribonacci(self, n: int) -> int:
arr = [0,1,1]
for i in range(2,n):
arr.append(sum(arr[-3:]))
return arr[n] | n-th-tribonacci-number | Python simple solution | StikS32 | 0 | 75 | n th tribonacci number | 1,137 | 0.633 | Easy | 17,707 |
https://leetcode.com/problems/n-th-tribonacci-number/discuss/2070395/Python-space-optimization-O(1)-Space | class Solution:
def tribonacci(self, n: int) -> int:
if n == 0:
return 0
elif n==1 or n == 2:
return 1
a,b,c = 0,1,1
for i in range(3,n+1):
a,b,c = b,c, a+b+c
return c | n-th-tribonacci-number | Python space optimization O(1) Space | dc_devesh7 | 0 | 46 | n th tribonacci number | 1,137 | 0.633 | Easy | 17,708 |
https://leetcode.com/problems/n-th-tribonacci-number/discuss/1941865/Python-Solution | class Solution:
def tribonacci(self, n: int) -> int:
arr = [0,1,1]
if n <= 2:
return arr[n]
for i in range(3,n+1):
arr.append(arr[i-1]+arr[i-2]+arr[i-3])
return arr[n] | n-th-tribonacci-number | Python Solution | MS1301 | 0 | 45 | n th tribonacci number | 1,137 | 0.633 | Easy | 17,709 |
https://leetcode.com/problems/n-th-tribonacci-number/discuss/1925050/Python-Solution-using-Array | class Solution:
def tribonacci(self, n: int) -> int:
arr = [0, 1, 1]
if n < 3:
return arr[n]
i = 3
while i <= n and i <= 37:
arr.append(arr[i-1] + arr[i-2] + arr[i-3])
i += 1
return arr[-1] | n-th-tribonacci-number | Python Solution using Array | sayantanis23 | 0 | 15 | n th tribonacci number | 1,137 | 0.633 | Easy | 17,710 |
https://leetcode.com/problems/n-th-tribonacci-number/discuss/1914203/Python-Multiple-Solutions-Clean-and-Simple! | class Solution:
def tribonacci(self, n):
return 0 if n==0 else 1 if n<3 else self.tribonacci(n-1)+self.tribonacci(n-2)+self.tribonacci(n-3) | n-th-tribonacci-number | Python - Multiple Solutions - Clean and Simple! | domthedeveloper | 0 | 43 | n th tribonacci number | 1,137 | 0.633 | Easy | 17,711 |
https://leetcode.com/problems/n-th-tribonacci-number/discuss/1914203/Python-Multiple-Solutions-Clean-and-Simple! | class Solution:
@cache
def tribonacci(self, n):
return 0 if n==0 else 1 if n<3 else self.tribonacci(n-1)+self.tribonacci(n-2)+self.tribonacci(n-3) | n-th-tribonacci-number | Python - Multiple Solutions - Clean and Simple! | domthedeveloper | 0 | 43 | n th tribonacci number | 1,137 | 0.633 | Easy | 17,712 |
https://leetcode.com/problems/n-th-tribonacci-number/discuss/1914203/Python-Multiple-Solutions-Clean-and-Simple! | class Solution:
def tribonacci(self, n):
q = deque([0,1,1], maxlen=3)
for i in range(n): q.append(q.popleft()+q[-1]+q[-2])
return q.popleft() | n-th-tribonacci-number | Python - Multiple Solutions - Clean and Simple! | domthedeveloper | 0 | 43 | n th tribonacci number | 1,137 | 0.633 | Easy | 17,713 |
https://leetcode.com/problems/n-th-tribonacci-number/discuss/1914203/Python-Multiple-Solutions-Clean-and-Simple! | class Solution:
def tribonacci(self, n):
a, b, c = 0, 1, 1
for i in range(n):
a, b, c = b, c, a+b+c
return a | n-th-tribonacci-number | Python - Multiple Solutions - Clean and Simple! | domthedeveloper | 0 | 43 | n th tribonacci number | 1,137 | 0.633 | Easy | 17,714 |
https://leetcode.com/problems/n-th-tribonacci-number/discuss/1914203/Python-Multiple-Solutions-Clean-and-Simple! | class Solution:
def tribonacci(self, n):
return reduce(lambda x,_:(x[1],x[2],x[0]+x[1]+x[2]), range(n), (0, 1, 1))[0] | n-th-tribonacci-number | Python - Multiple Solutions - Clean and Simple! | domthedeveloper | 0 | 43 | n th tribonacci number | 1,137 | 0.633 | Easy | 17,715 |
https://leetcode.com/problems/n-th-tribonacci-number/discuss/1896216/Python-easy-solution-for-beginners | class Solution:
def tribonacci(self, n: int) -> int:
if n == 0:
return 0
elif n == 1 or n == 2:
return 1
else:
a = 0
b = 1
c = 1
for i in range(n-2):
d = a + b + c
a = b
b ... | n-th-tribonacci-number | Python easy solution for beginners | alishak1999 | 0 | 43 | n th tribonacci number | 1,137 | 0.633 | Easy | 17,716 |
https://leetcode.com/problems/n-th-tribonacci-number/discuss/1832775/Python3-Dynamic-programming | class Solution:
def tribonacci(self, n: int) -> int:
if n <= 1: return n
dp = [0] * (n + 1)
dp[0] = 0
dp[1] = 1
dp[2] = 1
for i in range(3,n + 1):
dp[i] = dp[i - 1] + dp[i - 2] + dp[i - 3]
retur... | n-th-tribonacci-number | [Python3] Dynamic programming | zhanweiting | 0 | 80 | n th tribonacci number | 1,137 | 0.633 | Easy | 17,717 |
https://leetcode.com/problems/n-th-tribonacci-number/discuss/1804955/Python3-bottom-up-dp-solution. | class Solution:
def BottomUp(self, n):
if n == 0:
return 0
elif n == 1:
return 1
elif n == 2:
return 1
first = 0
second = 1
third = 1
for i in range(3, n+1):
current = first + second + third
... | n-th-tribonacci-number | Python3 bottom up dp solution. | sourav-saha | 0 | 38 | n th tribonacci number | 1,137 | 0.633 | Easy | 17,718 |
https://leetcode.com/problems/n-th-tribonacci-number/discuss/1804923/Python3-top-down-dp-solution-. | class Solution:
dp = [-1]*38
def TopDown(self, n):
#base case
if n == 0:
return 0
elif n == 1:
return 1
elif n == 2:
return 1
elif self.dp[n] == -1:
self.dp[n] = self.TopDown(n-3) + self.TopDown(n-2) + self.TopDown(n-1)
... | n-th-tribonacci-number | Python3 top down dp solution . | sourav-saha | 0 | 16 | n th tribonacci number | 1,137 | 0.633 | Easy | 17,719 |
https://leetcode.com/problems/n-th-tribonacci-number/discuss/1789495/Python-or-Simple-Solution-or-Using-lru_cache | class Solution:
@lru_cache(maxsize=None)
def tribonacci(self, n: int) -> int:
if n == 0:
return 0
if n == 1 or n == 2:
return 1
return self.tribonacci(n - 3) + self.tribonacci(n - 2) + self.tribonacci(n - 1) | n-th-tribonacci-number | [Python] | Simple Solution | Using lru_cache | tejeshreddy111 | 0 | 38 | n th tribonacci number | 1,137 | 0.633 | Easy | 17,720 |
https://leetcode.com/problems/n-th-tribonacci-number/discuss/1771794/Simple-Python-Solution-or-Easy-To-Understand-or-O(n)-Time-or-O(1)-Space | class Solution:
def tribonacci(self, n: int) -> int:
a, b, c, i = 0, 1, 1, 3
if n in [0,1]:
return n
while(i<=n):
temp = a + b + c
a = b
b = c
c = temp
i += 1
return (c) | n-th-tribonacci-number | ✔Simple Python Solution | Easy To Understand | O(n) Time | O(1) Space | Coding_Tan3 | 0 | 62 | n th tribonacci number | 1,137 | 0.633 | Easy | 17,721 |
https://leetcode.com/problems/n-th-tribonacci-number/discuss/1712266/Solution-with-DP-Top-Down-with-memoization | class Solution:
def tribonacci(self, n: int) -> int:
memo = {}
memo[0] = 0
memo[1] = 1
memo[2] = 1
def dp(i):
if i in memo:
return memo[i]
memo[i] = dp(i-1) + dp(i-2) + dp(i-3)
return memo[i]
dp(n)
return mem... | n-th-tribonacci-number | Solution with DP - Top Down with memoization | alessiogatto | 0 | 68 | n th tribonacci number | 1,137 | 0.633 | Easy | 17,722 |
https://leetcode.com/problems/n-th-tribonacci-number/discuss/1584654/easy-Python-solution-linear-dp | class Solution:
def tribonacci(self, n: int) -> int:
f = [None]*38
f[0] = 0
f[1] = 1
f[2] = 1
for i in range(3,n+1):
f[i] = f[i-1] + f[i-2] + f[i-3]
return f[n] | n-th-tribonacci-number | easy Python solution- linear dp | pheraram | 0 | 109 | n th tribonacci number | 1,137 | 0.633 | Easy | 17,723 |
https://leetcode.com/problems/n-th-tribonacci-number/discuss/1561276/Python3-or-Easy-or-No-Recursive-or-Loop-Solution | class Solution:
def tribonacci(self, n: int) -> int:
fnum = 0
snum = 1
tnum = 1
for i in range(n):
fnum,snum = snum,fnum+snum
snum,tnum = tnum,snum+tnum
return fnum | n-th-tribonacci-number | Python3 | Easy | No Recursive | Loop Solution | infinityhawk | 0 | 50 | n th tribonacci number | 1,137 | 0.633 | Easy | 17,724 |
https://leetcode.com/problems/n-th-tribonacci-number/discuss/1532980/Python3-Solution | class Solution:
def tribonacci(self, n: int,dp={}) -> int:
if n<=1:
return n
if n==2:
return 1
if n not in dp:
dp[n]=self.tribonacci(n-1, dp) + self.tribonacci(n-2, dp) + self.tribonacci(n-3, dp)
return dp[n] | n-th-tribonacci-number | Python3 Solution | kirtipurohit025 | 0 | 51 | n th tribonacci number | 1,137 | 0.633 | Easy | 17,725 |
https://leetcode.com/problems/n-th-tribonacci-number/discuss/1483290/Python3-DP-solution | class Solution:
def tribonacci(self, n: int) -> int:
dp = [0] * (n + 4)
dp[0] = 0
dp[1] = 1
dp[2] = 1
for i in range(3, n + 1):
dp[i] = dp[i - 1] + dp[i - 2] + dp[i - 3]
return dp[n] | n-th-tribonacci-number | [Python3] DP solution | maosipov11 | 0 | 16 | n th tribonacci number | 1,137 | 0.633 | Easy | 17,726 |
https://leetcode.com/problems/n-th-tribonacci-number/discuss/1483145/Without-using-Dynammic-Programming-with-Python3 | class Solution:
@cache
def tribonacci(self, n: int) -> int:
if n == 0 or n == 1:
return n
if n == 2: return 1
return self.tribonacci(n - 1) + self.tribonacci(n - 2) + self.tribonacci(n - 3) | n-th-tribonacci-number | Without using Dynammic Programming with Python3 | guptaanshik1 | 0 | 10 | n th tribonacci number | 1,137 | 0.633 | Easy | 17,727 |
https://leetcode.com/problems/n-th-tribonacci-number/discuss/1445608/Simple-Python-Solution | class Solution:
def tribonacci(self, n: int) -> int:
arr=[0,1,1]
for i in range(3,n+1):
arr.append(arr[i-1]+arr[i-2]+arr[i-3])
return arr[n] | n-th-tribonacci-number | Simple Python Solution | bazman | 0 | 40 | n th tribonacci number | 1,137 | 0.633 | Easy | 17,728 |
https://leetcode.com/problems/n-th-tribonacci-number/discuss/1436849/Python-Simple-DP-Faster-than-98.75 | class Solution:
def __init__(self):
self.memo = [None] * 38
self.memo[0] = 0
self.memo[1] = 1
self.memo[2] = 1
def tribonacci(self, n: int) -> int:
if self.memo[n] is not None: return self.memo[n]
number = self.tribonacci(n - 1) + self.tribonacc... | n-th-tribonacci-number | Python Simple DP Faster than 98.75% | rizwanmustafa0000 | 0 | 92 | n th tribonacci number | 1,137 | 0.633 | Easy | 17,729 |
https://leetcode.com/problems/n-th-tribonacci-number/discuss/1419738/Python3-Iterative-No-Memory-Less-Than-90.54 | class Solution:
def tribonacci(self, n: int) -> int:
if n < 3:
if n == 0:
return 0
if n == 1 or n == 2:
return 1
t0, t1, t2 = 0, 1, 1
for i in range(3, n + 1):
t = t0 + t1 + t2
t0 = t1
... | n-th-tribonacci-number | Python3 Iterative No Memory [Less Than 90.54%] | Hejita | 0 | 26 | n th tribonacci number | 1,137 | 0.633 | Easy | 17,730 |
https://leetcode.com/problems/alphabet-board-path/discuss/837601/Python-3-or-Straight-forward-solution-or-Explanations | class Solution:
def __init__(self):
board = ["abcde", "fghij", "klmno", "pqrst", "uvwxy", "z"]
self.d = {c:(i, j) for i, row in enumerate(board) for j, c in enumerate(row)}
def alphabetBoardPath(self, target: str) -> str:
ans, prev = '', (0, 0)
for c in target:
... | alphabet-board-path | Python 3 | Straight forward solution | Explanations | idontknoooo | 2 | 184 | alphabet board path | 1,138 | 0.523 | Medium | 17,731 |
https://leetcode.com/problems/alphabet-board-path/discuss/2516299/Python3-or-Map-characters-or-simulate-the-moves | class Solution:
def alphabetBoardPath(self, target: str) -> str:
# Map row and column for all characters
board = ["abcde", "fghij", "klmno", "pqrst", "uvwxy", "z"]
map_row = {char: r for r, characters in enumerate(board) for c, char in enumerate(characters)}
map_col =... | alphabet-board-path | Python3 | Map characters | simulate the moves | Ploypaphat | 0 | 21 | alphabet board path | 1,138 | 0.523 | Medium | 17,732 |
https://leetcode.com/problems/alphabet-board-path/discuss/2218323/PYTHON-SOL-or-LINEAR-TIME-or-VERY-EASY-or-EXPLAINED-WITH-PICTURE-or | class Solution:
def alphabetBoardPath(self, target: str) -> str:
def shortestPath(r,c,tr,tc):
path = ""
pr = r
while r > tr:
path += 'U'
r -= 1
while r < tr:
path += 'D'
r += 1
if tr =... | alphabet-board-path | PYTHON SOL | LINEAR TIME | VERY EASY | EXPLAINED WITH PICTURE | | reaper_27 | 0 | 35 | alphabet board path | 1,138 | 0.523 | Medium | 17,733 |
https://leetcode.com/problems/alphabet-board-path/discuss/1628963/Brute-force-with-my-notes | class Solution:
def alphabetBoardPath(self, target: str) -> str:
board = ["abcde", "fghij", "klmno", "pqrst", "uvwxy", "z"]
# greedy because starting from 0,0 no matter
# the next letter the best move is always just going
# directly to it in terms of distance between
... | alphabet-board-path | Brute force with my notes | normalpersontryingtopayrent | 0 | 33 | alphabet board path | 1,138 | 0.523 | Medium | 17,734 |
https://leetcode.com/problems/alphabet-board-path/discuss/1447483/Simple-Python-O(n)-hashmap-solution | class Solution:
def alphabetBoardPath(self, target: str) -> str:
# find mapping from letter to index
board = ["abcde", "fghij", "klmno", "pqrst", "uvwxy", "z"]
letter2idx = {}
for i in range(len(board)):
for j in range(len(board[i])):
letter2idx[board[i][j... | alphabet-board-path | Simple Python O(n) hashmap solution | Charlesl0129 | 0 | 131 | alphabet board path | 1,138 | 0.523 | Medium | 17,735 |
https://leetcode.com/problems/alphabet-board-path/discuss/1169851/99.79-Faster-Solution-Python3-(16ms).-Easy-Readable-Code | class Solution:
def getPosition(self,alpha:str):
ascii_code = ord(alpha)-ord('a')
q = ascii_code//5
r = ascii_code%5
return [q,r]
def getDirection(self,tx,ty,px,py):
direction = ''
if tx == 5 and ty >= 0:
diff_x = 4-px
... | alphabet-board-path | 99.79% Faster Solution Python3 (16ms). Easy Readable Code | tgoel219 | 0 | 37 | alphabet board path | 1,138 | 0.523 | Medium | 17,736 |
https://leetcode.com/problems/alphabet-board-path/discuss/1168530/Python3-linear-sweep | class Solution:
def alphabetBoardPath(self, target: str) -> str:
ans = []
x = y = 0
for c in target:
xx, yy = divmod(ord(c)-97, 5)
if x > xx: ans.append((x-xx)*"U")
if y > yy: ans.append((y-yy)*"L")
if x < xx: ans.append((xx-x)*"D")
... | alphabet-board-path | [Python3] linear sweep | ye15 | 0 | 33 | alphabet board path | 1,138 | 0.523 | Medium | 17,737 |
https://leetcode.com/problems/alphabet-board-path/discuss/1069469/Python3-solution-faster-than-96.45-in-time-and-less-than-94.90-in-memory | class Solution:
def alphabetBoardPath(self, target):
hashTable = [[0, 0], [0, 1], [0, 2], [0, 3], [0, 4], [1, 0], [1, 1], [1, 2], [1, 3], [1, 4], [2, 0], [2, 1], [2, 2], [2, 3], [2, 4], [3, 0], [3, 1], [3, 2], [3, 3], [3, 4], [4, 0], [4, 1], [4, 2], [4, 3], [4, 4], [5, 0]]
OFFSET, current = 97, 0
... | alphabet-board-path | Python3 solution, faster than 96.45% in time and less than 94.90% in memory | danieltseng | 0 | 51 | alphabet board path | 1,138 | 0.523 | Medium | 17,738 |
https://leetcode.com/problems/largest-1-bordered-square/discuss/1435087/Python-3-or-Prefix-sum-DP-O(N3)-or-Explanation | class Solution:
def largest1BorderedSquare(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
dp = [[(0, 0)] * (n) for _ in range((m))]
for i in range(m): # calculate prefix-sum as `hint` section suggested
for j in range(n):
... | largest-1-bordered-square | Python 3 | Prefix-sum, DP, O(N^3) | Explanation | idontknoooo | 2 | 347 | largest 1 bordered square | 1,139 | 0.501 | Medium | 17,739 |
https://leetcode.com/problems/largest-1-bordered-square/discuss/1187472/python-dp-faster-than-98%2B | class Solution:
def largest1BorderedSquare(self, grid: List[List[int]]) -> int:
width = len(grid[0])
height = len(grid)
dp = [[(0, 0)] * width for x in range(height)]
max_len = 0
for i in range(height):
for j in range(width):
... | largest-1-bordered-square | python, dp, faster than 98+% | dustlihy | 2 | 292 | largest 1 bordered square | 1,139 | 0.501 | Medium | 17,740 |
https://leetcode.com/problems/largest-1-bordered-square/discuss/2218430/PYTHON-or-EXPLAINED-or-VERY-EASY-or-O(N*M*MAX(N*M))-or-SIMPLE-or | class Solution:
def largest1BorderedSquare(self, grid: List[List[int]]) -> int:
ans = 0
n,m = len(grid) , len(grid[0])
for sr in range(n):
for sc in range(m):
er,ec = sr,sc
while True:
if grid[sr][ec] == 0 or grid[er][sc] == 0: ... | largest-1-bordered-square | PYTHON | EXPLAINED | VERY EASY | O(N*M*MAX(N*M)) | SIMPLE | | reaper_27 | 0 | 66 | largest 1 bordered square | 1,139 | 0.501 | Medium | 17,741 |
https://leetcode.com/problems/largest-1-bordered-square/discuss/1168617/Python3-horizontal-and-vertical-precipitation | class Solution:
def largest1BorderedSquare(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0]) # dimensions
hori = deepcopy(grid)
vert = deepcopy(grid)
for i in range(m):
for j in range(n):
if grid[i][j]:
if ... | largest-1-bordered-square | [Python3] horizontal & vertical precipitation | ye15 | 0 | 84 | largest 1 bordered square | 1,139 | 0.501 | Medium | 17,742 |
https://leetcode.com/problems/stone-game-ii/discuss/793881/python-DP-Thought-process-explained | class Solution:
def stoneGameII(self, piles: List[int]) -> int:
suffix_sum = self._suffix_sum(piles)
@lru_cache(None)
def dfs(pile: int, M: int, turn: bool) -> Tuple[int, int]:
# turn: true - alex, false - lee
sum_alex, sum_lee = suffix_sum[pile], suffix_sum[pile]
... | stone-game-ii | [python] DP Thought process explained | omgitspavel | 36 | 1,900 | stone game ii | 1,140 | 0.649 | Medium | 17,743 |
https://leetcode.com/problems/stone-game-ii/discuss/793881/python-DP-Thought-process-explained | class Solution:
def stoneGameII(self, piles: List[int]) -> int:
suffix_sum = self._suffix_sum(piles)
@lru_cache(None)
def dfs(pile: int, M: int) -> int:
sum_next_player = suffix_sum[pile]
for next_pile in range(pile + 1, min(pile + 2 * M + 1, len(piles) + 1)):
... | stone-game-ii | [python] DP Thought process explained | omgitspavel | 36 | 1,900 | stone game ii | 1,140 | 0.649 | Medium | 17,744 |
https://leetcode.com/problems/stone-game-ii/discuss/793881/python-DP-Thought-process-explained | class Solution:
def stoneGameII(self, piles: List[int]) -> int:
suffix_sum = self._suffix_sum(piles)
dp = [[0] * (len(piles) + 1) for _ in range(len(piles) + 1)]
for pile in reversed(range(len(piles))):
for M in reversed(range(len(piles))):
sum_next_player = suf... | stone-game-ii | [python] DP Thought process explained | omgitspavel | 36 | 1,900 | stone game ii | 1,140 | 0.649 | Medium | 17,745 |
https://leetcode.com/problems/stone-game-ii/discuss/808305/Python-DP-memoization-with-explanation | class Solution:
def stoneGameII(self, piles: List[int]) -> int:
N = len(piles)
self.dp = {}
def recursiveStoneGame(start, M):
if start >= N:
return 0
# take all if possible
if N - start <= 2*M:
retu... | stone-game-ii | [Python] DP memoization with explanation | xjiang2020 | 15 | 934 | stone game ii | 1,140 | 0.649 | Medium | 17,746 |
https://leetcode.com/problems/stone-game-ii/discuss/851536/beat-100-intuitive-code-with-explanation | class Solution:
def stoneGameII(self, piles):
# accumulated sum table of the rest of all stores reversely for quick check
a = [*accumulate(piles[::-1])][::-1]
# dp cache
@lru_cache(None)
def game(i, m):
# i: current index, m: current maximal move
# if p... | stone-game-ii | beat 100%, intuitive code with explanation | MarcoChang | 5 | 755 | stone game ii | 1,140 | 0.649 | Medium | 17,747 |
https://leetcode.com/problems/stone-game-ii/discuss/485246/RZ-Top-down-solution-from-top-voted-solution-and-corresponding-bottom-up-solution-in-Python | class Solution:
def stoneGameII(self, piles: List[int]) -> int:
if not piles:
return 0
n = len(piles)
postSum = [0] * n
postSum[n - 1] = piles[n - 1]
for i in range(n - 2, -1, -1):
postSum[i] = postSum[i + 1] + piles[i]
return... | stone-game-ii | [RZ] Top down solution from top voted solution and corresponding bottom up solution in Python | theflyingemini | 3 | 629 | stone game ii | 1,140 | 0.649 | Medium | 17,748 |
https://leetcode.com/problems/stone-game-ii/discuss/485246/RZ-Top-down-solution-from-top-voted-solution-and-corresponding-bottom-up-solution-in-Python | class Solution:
def stoneGameII(self, piles: List[int]) -> int:
if not piles:
return 0
n = len(piles)
postSum = [0] * n
postSum[n - 1] = piles[n - 1]
for i in range(n - 2, -1, -1):
postSum[i] = postSum[i + 1] + piles[i]
f = [[... | stone-game-ii | [RZ] Top down solution from top voted solution and corresponding bottom up solution in Python | theflyingemini | 3 | 629 | stone game ii | 1,140 | 0.649 | Medium | 17,749 |
https://leetcode.com/problems/stone-game-ii/discuss/2223050/PYTHON-or-WELL-WRITTEN-or-COMMENT-%2B-EXPLANATION-or-DP-or-EASY-or | class Solution:
def stoneGameII(self, piles: List[int]) -> int:
n = len(piles)
dp = {}
def recursion(index,M):
# if we reached to the end we cannot score any value
if index == n:
return 0
# we search if we have solved the same case earlier... | stone-game-ii | PYTHON | WELL WRITTEN | COMMENT + EXPLANATION | DP | EASY | | reaper_27 | 1 | 126 | stone game ii | 1,140 | 0.649 | Medium | 17,750 |
https://leetcode.com/problems/stone-game-ii/discuss/1331567/Intuitive-DFS-DP-with-and-without-bool-flag-for-current-player-optimal-strategy-of-min-max | class Solution:
def stoneGameII(self, piles: List[int]) -> int:
@functools.cache
def dp(l, m, i_am_alice): # lets return alice count
if l == len(piles): return 0
left = []
taking_now = 0
for x in range(2*m):
if l+x == len(... | stone-game-ii | Intuitive DFS DP with & without bool flag for current player, optimal strategy of min max | yozaam | 0 | 276 | stone game ii | 1,140 | 0.649 | Medium | 17,751 |
https://leetcode.com/problems/longest-common-subsequence/discuss/2331817/Python3-or-Java-or-C%2B%2B-or-DP-or-O(nm)-or-BottomUp-(Tabulation) | class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
dp = []
# Fill the matrix
for _ in range(len(text1)+1):
row = []
for _ in range(len(text2)+1):
row.append(0)
dp.append(row)
... | longest-common-subsequence | Python3 | Java | C++ | DP | O(nm) | BottomUp (Tabulation) | khaydaraliev99 | 10 | 488 | longest common subsequence | 1,143 | 0.588 | Medium | 17,752 |
https://leetcode.com/problems/longest-common-subsequence/discuss/2041271/Python-or-DP-or-5-Approaches-or | class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
n = len(text1)
m = len(text2)
dp = [[-1 for i in range(m+1)] for i in range(n+1)]
for j in range(m+1):
dp[0][j] = 0
for i in range(n+1):
dp[i][0] =... | longest-common-subsequence | Python | DP | 5 Approaches | | LittleMonster23 | 9 | 526 | longest common subsequence | 1,143 | 0.588 | Medium | 17,753 |
https://leetcode.com/problems/longest-common-subsequence/discuss/2041271/Python-or-DP-or-5-Approaches-or | class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
n = len(text1)
m = len(text2)
prev = [0 for i in range(m+1)]
cur = [0 for i in range(m+1)]
for ind1 in range(1,n+1):
for ind2 in range(1,m+1):
if(t... | longest-common-subsequence | Python | DP | 5 Approaches | | LittleMonster23 | 9 | 526 | longest common subsequence | 1,143 | 0.588 | Medium | 17,754 |
https://leetcode.com/problems/longest-common-subsequence/discuss/598687/PythonJSC%2B%2B-O(-m*n-)-2D-DP.-w-Hint | class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
# padding one space for empty string representation
text1 = ' ' + text1
text2 = ' ' + text2
w, h = len(text1), len(text2)
dp_table = [ [ 0 for x in range(w) ] for y in range(h) ]
... | longest-common-subsequence | Python/JS/C++ O( m*n ) 2D DP. [w/ Hint] | brianchiang_tw | 7 | 1,300 | longest common subsequence | 1,143 | 0.588 | Medium | 17,755 |
https://leetcode.com/problems/longest-common-subsequence/discuss/598687/PythonJSC%2B%2B-O(-m*n-)-2D-DP.-w-Hint | class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
@cache
def dp(i, j):
if i == -1 or j == -1:
# Any string compare to empty string has no common sequence
return 0
elif text1[i]... | longest-common-subsequence | Python/JS/C++ O( m*n ) 2D DP. [w/ Hint] | brianchiang_tw | 7 | 1,300 | longest common subsequence | 1,143 | 0.588 | Medium | 17,756 |
https://leetcode.com/problems/longest-common-subsequence/discuss/534345/Python3-top-down-and-bottom-up-dp | class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
m, n = len(text1), len(text2)
dp = [[0]*(n+1) for _ in range(m+1)]
for i in range(m-1, -1, -1):
for j in range(n-1, -1, -1):
if text1[i] == text2[j]: dp[i][j] = 1 + dp[i+1][j+1]
... | longest-common-subsequence | [Python3] top-down & bottom-up dp | ye15 | 3 | 233 | longest common subsequence | 1,143 | 0.588 | Medium | 17,757 |
https://leetcode.com/problems/longest-common-subsequence/discuss/534345/Python3-top-down-and-bottom-up-dp | class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
m, n = len(text1), len(text2)
ans = [0]*(n+1)
for i in range(m):
tmp = ans.copy()
for j in range(n):
if text1[i] == text2[j]: ans[j+1] = 1 + tmp[j]
... | longest-common-subsequence | [Python3] top-down & bottom-up dp | ye15 | 3 | 233 | longest common subsequence | 1,143 | 0.588 | Medium | 17,758 |
https://leetcode.com/problems/longest-common-subsequence/discuss/534345/Python3-top-down-and-bottom-up-dp | class Solution:
def longestPalindromeSubseq(self, s: str) -> int:
@lru_cache(None)
def lcs(i, j):
"""Return longest common subsequence of text1[i:] and text2[j:]."""
if i == len(s) or j == len(s): return 0
if s[i] == s[~j]: return 1 + lcs(i+1, j+1)
... | longest-common-subsequence | [Python3] top-down & bottom-up dp | ye15 | 3 | 233 | longest common subsequence | 1,143 | 0.588 | Medium | 17,759 |
https://leetcode.com/problems/longest-common-subsequence/discuss/534345/Python3-top-down-and-bottom-up-dp | class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
m, n = len(text1), len(text2)
dp = [0]*(n+1)
for i in reversed(range(m)):
prev = curr = 0
for j in reversed(range(n)):
curr = dp[j]
if text1[i] == text2[... | longest-common-subsequence | [Python3] top-down & bottom-up dp | ye15 | 3 | 233 | longest common subsequence | 1,143 | 0.588 | Medium | 17,760 |
https://leetcode.com/problems/longest-common-subsequence/discuss/2074674/100-or-LIS-or-Python3-or-Avg-nlogn | class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
text1_dict = defaultdict(deque)
for idx1, char1 in enumerate(text1):
if char1 in set(text2):
text1_dict[char1].appendleft(idx1)
# print(text1_dict)
# defaultdict(<class 'collectio... | longest-common-subsequence | 100% | LIS | Python3 | Avg nlogn | yzhao156 | 2 | 58 | longest common subsequence | 1,143 | 0.588 | Medium | 17,761 |
https://leetcode.com/problems/longest-common-subsequence/discuss/2074674/100-or-LIS-or-Python3-or-Avg-nlogn | class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
text1_dict, nums, lis = defaultdict(deque), [], []
for idx1, char1 in enumerate(text1):
if char1 in set(text2):
text1_dict[char1].appendleft(idx1)
for char2 in text2:
if... | longest-common-subsequence | 100% | LIS | Python3 | Avg nlogn | yzhao156 | 2 | 58 | longest common subsequence | 1,143 | 0.588 | Medium | 17,762 |
https://leetcode.com/problems/longest-common-subsequence/discuss/2342233/python3-EASY-FAST-6-Line-Sol | class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
dp = [[0 for j in range(len(text2) + 1)] for i in range(len(text1) + 1)]
for i in range(len(text1) - 1, -1, -1):
for j in range(len(text2) - 1, -1, -1):
if text1[i] == text2[j]:
... | longest-common-subsequence | python3 EASY FAST 6 Line Sol | soumyadexter7 | 1 | 155 | longest common subsequence | 1,143 | 0.588 | Medium | 17,763 |
https://leetcode.com/problems/longest-common-subsequence/discuss/2049815/Python-DP-Solution | class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
n, m = len(text1), len(text2)
dp = [[0 for i in range(1 + m)] for j in range(1 + n)]
for i in range(1, n + 1):
for j in range(1, m + 1):
if text1[i-1] == text2[j-1]:
... | longest-common-subsequence | Python DP Solution | dbansal18 | 1 | 51 | longest common subsequence | 1,143 | 0.588 | Medium | 17,764 |
https://leetcode.com/problems/longest-common-subsequence/discuss/2029014/Pythonor-Easy-DPor-Bottom-Up-Approach | class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
dp = [[0 for j in range(len(text2) + 1)] for i in range(len(text1) + 1)]
for i in range(len(text1) - 1, -1, -1):
for j in range(len(text2) - 1, -1, -1):
if text1[i] == text2[j]:
... | longest-common-subsequence | Python| Easy DP| Bottom Up Approach | shikha_pandey | 1 | 147 | longest common subsequence | 1,143 | 0.588 | Medium | 17,765 |
https://leetcode.com/problems/longest-common-subsequence/discuss/1813374/Python-easy-to-read-and-understand-or-DP | class Solution:
def lcs(self, a, b):
M, N = len(a), len(b)
t = [[0 for _ in range(N+1)] for _ in range(M+1)]
for m in range(1, M+1):
for n in range(1, N+1):
if a[m-1] == b[n-1]:
t[m][n] = 1 + t[m-1][n-1]
else:
... | longest-common-subsequence | Python easy to read and understand | DP | sanial2001 | 1 | 245 | longest common subsequence | 1,143 | 0.588 | Medium | 17,766 |
https://leetcode.com/problems/longest-common-subsequence/discuss/1792110/Python-Brute-Force-Top-Down-(Memo)-and-Bottom-Up-(DP) | class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
# Brute Force
# Time: O(2^(t1+t2), Space: O(t1+t2)
return self.lcsBruteForce(text1, text2, 0, 0)
# Top-Down
# Time: O(t1*t2, Space: O(t1*t2)
memo = [[0 for _ in range(len(text2))] for _ in range(len(t... | longest-common-subsequence | Python Brute Force, Top-Down (Memo), and Bottom-Up (DP) | doubleimpostor | 1 | 201 | longest common subsequence | 1,143 | 0.588 | Medium | 17,767 |
https://leetcode.com/problems/longest-common-subsequence/discuss/1633349/Python3-top-down-DP-with-%40cache-cheating. | class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
# obviously it has to be common tldr exist in both text1 and text2 so already the length can eliminate all the non matching
# then from there, you need it in the same relative order.
# longest, max etc. dp??
... | longest-common-subsequence | Python3 top down DP with @cache cheating. | normalpersontryingtopayrent | 1 | 87 | longest common subsequence | 1,143 | 0.588 | Medium | 17,768 |
https://leetcode.com/problems/longest-common-subsequence/discuss/1463354/PyPy3-Solution-with-memoization-w-comments | class Solution:
def longestCommonSubsequence(self, x: str, y: str) -> int:
# Recursive solution with memoization
def lcs(n: int, m: int, t=dict()) -> int:
# Base Conidtion: If any of the string
# empty the lenght of longest common
# subsequen... | longest-common-subsequence | [Py/Py3] Solution with memoization w/ comments | ssshukla26 | 1 | 184 | longest common subsequence | 1,143 | 0.588 | Medium | 17,769 |
https://leetcode.com/problems/longest-common-subsequence/discuss/929510/Python3-Longest-Common-Subsequence.-**All-3-concepts-Easy-Code** | class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
return self.commonLen(text1,text2,len(text1),len(text2))
def commonLen(self,x,y,n,m):
if n==0 or m==0:
return 0
if x[n-1] == y[m-1]:
ret... | longest-common-subsequence | [Python3] Longest Common Subsequence. **All 3 concepts Easy Code** | tilak_ | 1 | 208 | longest common subsequence | 1,143 | 0.588 | Medium | 17,770 |
https://leetcode.com/problems/longest-common-subsequence/discuss/929510/Python3-Longest-Common-Subsequence.-**All-3-concepts-Easy-Code** | class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
return self.commonLen(text1,text2,len(text1),len(text2))
def commonLen(self,x,y,n,m):
dp = [[-1]*(m+1) for i in range(n+1)]
if n==0 or m==0:
return 0
if dp[n][m]!=-1:
... | longest-common-subsequence | [Python3] Longest Common Subsequence. **All 3 concepts Easy Code** | tilak_ | 1 | 208 | longest common subsequence | 1,143 | 0.588 | Medium | 17,771 |
https://leetcode.com/problems/longest-common-subsequence/discuss/929510/Python3-Longest-Common-Subsequence.-**All-3-concepts-Easy-Code** | class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
return self.commonLen(text1,text2,len(text1),len(text2))
def commonLen(self,x,y,n,m):
dp = [[-1]*(m+1) for i in range(n+1)]
for i in range(n+1):
for j in range(m+1):
... | longest-common-subsequence | [Python3] Longest Common Subsequence. **All 3 concepts Easy Code** | tilak_ | 1 | 208 | longest common subsequence | 1,143 | 0.588 | Medium | 17,772 |
https://leetcode.com/problems/longest-common-subsequence/discuss/624707/Simple-DP-Solution-Python | class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
if text1==text2:
return len(text1)
dp=[]
for i in range(len(text1)+1):
x=[]
for j in range(len(text2)+1):
x.append(0)
... | longest-common-subsequence | Simple DP Solution Python | Ayu-99 | 1 | 89 | longest common subsequence | 1,143 | 0.588 | Medium | 17,773 |
https://leetcode.com/problems/longest-common-subsequence/discuss/580183/DP-with-Space-Optimization-(Beats-99.5-in-Time-100-in-Space) | class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
if len(text1) < len(text2):
text1, text2 = text2, text1
n, m = len(text1), len(text2)
table = [0] * (m + 1)
prev_table = [0] * (m + 1)
for c1 in text1:
for i, c2 in enum... | longest-common-subsequence | DP with Space Optimization (Beats 99.5% in Time, 100% in Space) | skarakulak | 1 | 377 | longest common subsequence | 1,143 | 0.588 | Medium | 17,774 |
https://leetcode.com/problems/longest-common-subsequence/discuss/2820313/Python-Solution-Memoization-or-Tabulation | class Solution:
#Memoization
#Time complexity: O(N*M)
#Space complexity: O(N*M) + O(N+M)
def helper(self,ind1,ind2,text1,text2,dp):
if ind1<0 or ind2<0:
return 0
if dp[ind1][ind2]!=-1:
return dp[ind1][ind2]
if text1[ind1]==text2[ind2]:
dp[ind1][ind2] = 1+s... | longest-common-subsequence | Python Solution - Memoization | Tabulation | ankitabudhia42 | 0 | 1 | longest common subsequence | 1,143 | 0.588 | Medium | 17,775 |
https://leetcode.com/problems/longest-common-subsequence/discuss/2820313/Python-Solution-Memoization-or-Tabulation | class Solution:
def longestCommonSubsequence(self, s: str, t: str) -> int:
l1 = len(s)
l2 = len(t)
dp = [[-1]*(l2+1) for i in range(l1+2)]
for i in range(l1+1):
dp[i][0]=0
for j in range(l2+1):
dp[0][j]=0
for i in range(1,l1+1):
... | longest-common-subsequence | Python Solution - Memoization | Tabulation | ankitabudhia42 | 0 | 1 | longest common subsequence | 1,143 | 0.588 | Medium | 17,776 |
https://leetcode.com/problems/longest-common-subsequence/discuss/2778769/python-%2B-DP-%2B-O(n*m) | class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
n = len(text1)
m = len(text2)
temp = [[0 for i in range(m+1)] for j in range(n+1)]
for i in range(1, n+1):
for j in range(1, m+1):
if text1[i-1]== text2[j-1]:
... | longest-common-subsequence | python + DP + O(n*m) | surajsoni | 0 | 4 | longest common subsequence | 1,143 | 0.588 | Medium | 17,777 |
https://leetcode.com/problems/longest-common-subsequence/discuss/2773415/python-soln-using-tabulation-method | class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
m=len(text1)
n=len(text2)
dp=[[-1]*(n+1) for _ in range(m+1)]
for i in range(m+1):
for j in range(n+1):
if i==0 or j==0:
dp[i][j]=0
elif t... | longest-common-subsequence | python soln using tabulation method | Nischay_2003 | 0 | 2 | longest common subsequence | 1,143 | 0.588 | Medium | 17,778 |
https://leetcode.com/problems/longest-common-subsequence/discuss/2767182/Longest-Common-Subsequence-oror-Python3-ororDP | class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
dp=[[-1 for i in range(len(text2)+1)]for j in range(len(text1)+1)]
m=len(text1)
n=len(text2)
return lcs(dp,text1,text2,m,n)
def lcs(dp,s1,s2,m,n):
if m==0 or n==0:
dp[m][n]=0
return dp... | longest-common-subsequence | Longest Common Subsequence || Python3 ||DP | shagun_pandey | 0 | 5 | longest common subsequence | 1,143 | 0.588 | Medium | 17,779 |
https://leetcode.com/problems/longest-common-subsequence/discuss/2762985/Python%3A-DP(matrix)-%2B-Memoization-technique-oror-Recurrsive-Approach | class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
m=len(text1)
n=len(text2)
dp=[[-1 for i in range(n)] for j in range(m)]
def lcs(m,n):
if m<0 or n<0:
return 0
if dp[m][n]!=-1:
return dp... | longest-common-subsequence | Python: DP(matrix) + Memoization technique || Recurrsive Approach | utsa_gupta | 0 | 9 | longest common subsequence | 1,143 | 0.588 | Medium | 17,780 |
https://leetcode.com/problems/longest-common-subsequence/discuss/2762258/A-clean-efficient-DP-python-solution-that-beats-90.78-runtime-and-88.88-memory | class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
rows = len(text1)
cols = len(text2)
memo = [[None] * (cols + 1) for i in range((rows + 1))] # type: List[List[Optional[int]]]
for row in range(rows + 1):
for col in range(cols + 1):
... | longest-common-subsequence | A clean efficient DP python solution that beats 90.78% runtime and 88.88% memory | myaser | 0 | 2 | longest common subsequence | 1,143 | 0.588 | Medium | 17,781 |
https://leetcode.com/problems/longest-common-subsequence/discuss/2739389/Python3-or-Solved-Using-Top-Down-DP-%2B-Memoization | class Solution:
#Time-Complexity: O(2^min(length1, length2)), since branching factor in worst case is 2 where
#two compared characters do not match and longest path from root to leaf in rec. tree is
#of length min(length1, length2), since either i or j will hit length1 or length2 to go
#out of bounds i... | longest-common-subsequence | Python3 | Solved Using Top-Down DP + Memoization | JOON1234 | 0 | 3 | longest common subsequence | 1,143 | 0.588 | Medium | 17,782 |
https://leetcode.com/problems/longest-common-subsequence/discuss/2737547/Python3-Straightforward-2D-DP-(with-comments) | class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
dp = [[0] * (len(text2)+1) for _ in range(len(text1)+1)]
for i in range(len(text1)-1, -1, -1):
for j in range(len(text2)-1, -1, -1):
if text1[i] == text2[j]: # match, increase LCS
... | longest-common-subsequence | Python3 Straightforward 2D DP (with comments) | jonathanbrophy47 | 0 | 3 | longest common subsequence | 1,143 | 0.588 | Medium | 17,783 |
https://leetcode.com/problems/longest-common-subsequence/discuss/2730174/python-dp-top-down-easy-and-short-solution | class Solution:
t1,t2=None,None
@cache
def dynamic(self,i,j):
if i >= len(self.t1) or j >= len(self.t2):
return 0
if self.t1[i] == self.t2[j]:
return 1 + self.dynamic(i+1, j+1)
return max(self.dynamic(i+1,j), self.dynamic(i,j+1))
def longestCommonSubsequen... | longest-common-subsequence | python dp top down easy and short solution | benon | 0 | 9 | longest common subsequence | 1,143 | 0.588 | Medium | 17,784 |
https://leetcode.com/problems/longest-common-subsequence/discuss/2728201/PYTHON-SOLUTION-LCS | class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
def f(ind1,ind2,dp):
if ind1<0 or ind2<0:
return 0
if dp[ind1][ind2]!=-1:
return dp[ind1][ind2]
if text1[ind1]==text2[ind2]:
dp[ind1][ind2]=1... | longest-common-subsequence | PYTHON SOLUTION LCS | shashank_2000 | 0 | 7 | longest common subsequence | 1,143 | 0.588 | Medium | 17,785 |
https://leetcode.com/problems/longest-common-subsequence/discuss/2673141/python-easy-solution-using-DP-bottom-up-approach | class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
m=len(text1)
n=len(text2)
dp=[]
for i in range (m+1):
dp.append([0]*(n+1))
for i in range (1,m+1):
for j in range (1,n+1):
if text1[i-1]==text2[... | longest-common-subsequence | python easy solution using DP bottom up approach | tush18 | 0 | 81 | longest common subsequence | 1,143 | 0.588 | Medium | 17,786 |
https://leetcode.com/problems/longest-common-subsequence/discuss/2668012/Python-Accepted | class Solution:
def longestCommonSubsequence(self, t1: str, t2: str) -> int:
matrix = [[0 for j in range(len(t2)+1)] for i in range(len(t1)+1)]
for i in range(len(t1)-1, -1, -1):
for j in range(len(t2)-1, -1, -1):
if t1[i]==t2[j]:
... | longest-common-subsequence | Python Accepted ✅ | Khacker | 0 | 35 | longest common subsequence | 1,143 | 0.588 | Medium | 17,787 |
https://leetcode.com/problems/longest-common-subsequence/discuss/2588160/nlogn-solution-can-anyone-explain | class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
text1_dict, nums, lis = defaultdict(list), [], []
text1_set, text2_set = set(text1), set(text2)
for idx1, char1 in enumerate(text1):
if char1 in text2_set:
text1_dict[char1].append(... | longest-common-subsequence | nlogn solution, can anyone explain? | yzhao156 | 0 | 15 | longest common subsequence | 1,143 | 0.588 | Medium | 17,788 |
https://leetcode.com/problems/longest-common-subsequence/discuss/2395873/Weird-issue-with-dp-list-and-list-comprehension | class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
useListComprehension=True
cache = [[-1 for i in range(len(text2))] for i in range(len(text1))] if useListComprehension else [[-1]*(len(text2))]*(len(text1))
return self.solve(text1,text2, 0, 0, 0, cache)
... | longest-common-subsequence | Weird issue with dp list and list comprehension | shane911 | 0 | 10 | longest common subsequence | 1,143 | 0.588 | Medium | 17,789 |
https://leetcode.com/problems/longest-common-subsequence/discuss/2305677/Python3-Solution-with-using-dp | class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
dp = [[0] * (len(text2) + 1) for _ in range(len(text1) + 1)]
for i in range(1, len(text1) + 1):
for j in range(1, len(text2) + 1):
if text1[i - 1] == text2[j - 1]:
... | longest-common-subsequence | [Python3] Solution with using dp | maosipov11 | 0 | 41 | longest common subsequence | 1,143 | 0.588 | Medium | 17,790 |
https://leetcode.com/problems/longest-common-subsequence/discuss/2267683/Python-DP-with-full-working-explanation | class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int: # Time: O(m * n) and Space: O(m * n)
# creating a 2D array with text1+1 rows and text2+1 columns filled with 0
dp = [[0] * (len(text2) + 1) for _ in range(len(text1) + 1)]
# we start from i+1 and j+1 because... | longest-common-subsequence | Python DP with full working explanation | DanishKhanbx | 0 | 111 | longest common subsequence | 1,143 | 0.588 | Medium | 17,791 |
https://leetcode.com/problems/longest-common-subsequence/discuss/2197284/Easy-python-solution-using-2D-Dynamic-programming | class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
dp = [[0 for j in range(len(text2) + 1)] for i in range(len(text1) + 1)]
for i in range(len(text1) - 1,-1,-1):
for j in range(len(text2) - 1, -1, -1):
if text1[i] == text2[j]:
... | longest-common-subsequence | Easy python solution using 2D Dynamic programming | nishanrahman1994 | 0 | 90 | longest common subsequence | 1,143 | 0.588 | Medium | 17,792 |
https://leetcode.com/problems/longest-common-subsequence/discuss/2183535/LCS-oror-DP-table-oror-Easy-to-understand | class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
# Standard dp LCS problem
# T(n)=O(M*N)
# S(n)=O(M*N)
m=len(text2)
n=len(text1)
dp=[[None for j in range(m+1)] for i in range(n+1)]
for i in range(n+1):
for j in ran... | longest-common-subsequence | LCS || DP table || Easy to understand | Aniket_liar07 | 0 | 64 | longest common subsequence | 1,143 | 0.588 | Medium | 17,793 |
https://leetcode.com/problems/longest-common-subsequence/discuss/2158671/Python-a-classic-DP-problem | class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
curr = [0] * (len(text2) + 1)
for i1 in range(len(text1)):
curr, prev = [0] * (len(text2) + 1), curr
for i2 in range(len(text2)):
if text1[i1] == text2[i2]:
... | longest-common-subsequence | Python, a classic DP problem | blue_sky5 | 0 | 44 | longest common subsequence | 1,143 | 0.588 | Medium | 17,794 |
https://leetcode.com/problems/longest-common-subsequence/discuss/2151525/99-efficient-time-and-space | class Solution:
def longestCommonSubsequence(self, word1: str, word2: str) -> int:
if len(word1)>len(word2):
word2,word1=word1,word2
m,n=len(word1),len(word2)
prev=[0] * (m+1)
for i in range(n-1, -1, -1):
curr=[0] * (m+1)
... | longest-common-subsequence | 99% efficient time and space | vivekbharti900 | 0 | 29 | longest common subsequence | 1,143 | 0.588 | Medium | 17,795 |
https://leetcode.com/problems/longest-common-subsequence/discuss/2086656/Python-solution-oror-DP | class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
a=text1
b=text2
m=len(text1)
n=len(text2)
dp =([[0 for i in range(n + 1)] for i in range(m + 1)])
for i in range(m+1):
for j in range(n+1):
if i==0 or j==0:
... | longest-common-subsequence | Python solution || DP | a_dityamishra | 0 | 49 | longest common subsequence | 1,143 | 0.588 | Medium | 17,796 |
https://leetcode.com/problems/longest-common-subsequence/discuss/2077447/O(NM) | class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
dp = [[0 for col in range(len(text2) + 1)] for row in range(len(text1) + 1)]
for i in range(len(text1) -1, -1, -1):
for j in range(len(text2) -1, -1, -1):
... | longest-common-subsequence | O(N^M) | andrewnerdimo | 0 | 28 | longest common subsequence | 1,143 | 0.588 | Medium | 17,797 |
https://leetcode.com/problems/longest-common-subsequence/discuss/2062568/python3-dp-solution | class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
r, c = len(text1) + 1, len(text2) + 1
lcs = [[0] * c for _ in range(r)]
for i in range(1, r):
for j in range(1, c):
lcs[i][j] = lcs[i-1][j-1] + 1 if text1[i-1] == text2[j-1... | longest-common-subsequence | python3 dp solution | user2613C | 0 | 40 | longest common subsequence | 1,143 | 0.588 | Medium | 17,798 |
https://leetcode.com/problems/longest-common-subsequence/discuss/2002450/Python3-Dynamic-Programming-method | class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
dp = [[0 for _ in range(len(text2) + 1)] for _ in range(len(text1) + 1)]
for i in range(len(text1) - 1, -1, -1):
for j in range(len(text2) - 1, -1, -1):
if text1[i] == text2[j]:
... | longest-common-subsequence | Python3 - Dynamic Programming method | dayaniravi123 | 0 | 66 | longest common subsequence | 1,143 | 0.588 | Medium | 17,799 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.