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/minimum-falling-path-sum/discuss/2678642/minFallingPathSum
class Solution: def minFallingPathSum(self, matrix: List[List[int]]) -> int: if not matrix[0]: return 0 dp = matrix[0] for i in range(1, len(matrix)): dp_new = matrix[i] for j in range(len(matrix[i])): if j == 0: if j+1 ...
minimum-falling-path-sum
minFallingPathSum
langtianyuyu
0
3
minimum falling path sum
931
0.685
Medium
15,100
https://leetcode.com/problems/minimum-falling-path-sum/discuss/2676742/DPpython
class Solution: def minFallingPathSum(self, matrix: List[List[int]]) -> int: n = len(matrix) res = [] memo = [[-1 for _ in range(n)]for _ in range(n)] def dp(i,j): if(i > n-1 or j > n-1 or i < 0 or j < 0): return float('inf') if memo[i][j] != -...
minimum-falling-path-sum
[DP]python
kuroko_6668
0
18
minimum falling path sum
931
0.685
Medium
15,101
https://leetcode.com/problems/minimum-falling-path-sum/discuss/2665707/Python3-or-DP-or-Modular-approach-or-Memory-efficient
class Solution: def minFallingPathSum(self, matrix: List[List[int]]) -> int: rows, cols = len(matrix), len(matrix[0]) if rows == 1: return min(matrix[0]) prev = [num for num in matrix[0]] cur = [sys.maxsize for _ in range(cols)] def getPrevSum(col): ...
minimum-falling-path-sum
Python3 | DP | Modular approach | Memory efficient
Ploypaphat
0
2
minimum falling path sum
931
0.685
Medium
15,102
https://leetcode.com/problems/minimum-falling-path-sum/discuss/2650131/Python-DP-Space-Optimized
class Solution: def minFallingPathSum(self, matrix: List[List[int]]) -> int: n = len(matrix) prev = [-1] * n for j in range(n): prev[j] = matrix[0][j] for i in range(1, n): cur = [-1] * n for j in range(n): ld, rd = 1e9, 1...
minimum-falling-path-sum
Python - DP - Space Optimized
kritikaparmar
0
4
minimum falling path sum
931
0.685
Medium
15,103
https://leetcode.com/problems/minimum-falling-path-sum/discuss/2602803/python-dp-button-up-solution
class Solution: def minFallingPathSum(self, matrix: List[List[int]]) -> int: n,m=len(matrix),len(matrix[0]) for i in range(1,n): for j in range(m): move=[] if j-1>=0: move.append(matrix[i-1][j-1]) if j+1<m: ...
minimum-falling-path-sum
python dp button up solution
benon
0
20
minimum falling path sum
931
0.685
Medium
15,104
https://leetcode.com/problems/minimum-falling-path-sum/discuss/2452008/Python-Simple-Python-Solution-100-Optimal-Solution-3-Different-Ans
class Solution: def minFallingPathSum(self, matrix: List[List[int]]) -> int: dp = [[-1 for i in range(len(matrix[0]))] for j in range(len(matrix))] def sol(i, j): # Base Case if dp[i][j] != -1: return dp[i][j] if i < 0: return 0 # ...
minimum-falling-path-sum
[ Python ] βœ… Simple Python Solution βœ…βœ… βœ…100% Optimal Solution 3 Different Ans
vaibhav0077
0
19
minimum falling path sum
931
0.685
Medium
15,105
https://leetcode.com/problems/minimum-falling-path-sum/discuss/2452008/Python-Simple-Python-Solution-100-Optimal-Solution-3-Different-Ans
class Solution: def minFallingPathSum(self, matrix: List[List[int]]) -> int: dp = [[-1 for i in range(len(matrix[0]))] for j in range(len(matrix))] # Base Case for i in range(len(matrix[0])): dp[0][i] = matrix[0][i] for i in range(1 ,len(matrix)): for j in...
minimum-falling-path-sum
[ Python ] βœ… Simple Python Solution βœ…βœ… βœ…100% Optimal Solution 3 Different Ans
vaibhav0077
0
19
minimum falling path sum
931
0.685
Medium
15,106
https://leetcode.com/problems/minimum-falling-path-sum/discuss/2452008/Python-Simple-Python-Solution-100-Optimal-Solution-3-Different-Ans
class Solution: def minFallingPathSum(self, matrix: List[List[int]]) -> int: # Base Case prev = [0 for i in range(len(matrix[0]))] cur = [0 for i in range(len(matrix[0]))] # Logic for j in range(len(matrix[0])): prev[j] = matrix[0][j] ...
minimum-falling-path-sum
[ Python ] βœ… Simple Python Solution βœ…βœ… βœ…100% Optimal Solution 3 Different Ans
vaibhav0077
0
19
minimum falling path sum
931
0.685
Medium
15,107
https://leetcode.com/problems/minimum-falling-path-sum/discuss/2421757/PYTHON-or-IN-PLACE-or-DP-or-EASY
class Solution: def minFallingPathSum(self, matrix: List[List[int]]) -> int: r = len(matrix) c = len(matrix[0]) for i in range(len(matrix)-2,-1,-1): m = 999999999 for j in range(0,len(matrix[0])): if i+1<r and j-1>=0: m = m...
minimum-falling-path-sum
PYTHON | IN PLACE | DP | EASY
Brillianttyagi
0
8
minimum falling path sum
931
0.685
Medium
15,108
https://leetcode.com/problems/minimum-falling-path-sum/discuss/2387466/Python3-Bottom-Up-w-Tabulation
class Solution: def minFallingPathSum(self, matrix: List[List[int]]) -> int: n = len(matrix) tabu = matrix[-1] for i in range(n-2, -1, -1): newTabu = matrix[i] newTabu[0] += min(tabu[0], tabu[1]) newTabu[-1] += min(tabu[-1], tabu[-2]) for j in ...
minimum-falling-path-sum
[Python3] Bottom-Up w/ Tabulation
ruosengao
0
6
minimum falling path sum
931
0.685
Medium
15,109
https://leetcode.com/problems/minimum-falling-path-sum/discuss/2322912/Python-soln-(recursion-%2B-memoization)
class Solution: def minFallingPathSum(self, matrix: List[List[int]]) -> int: m, n = len(matrix), len(matrix[0]) lookup = {} def minFp(i, j, lookup): if j<0 or j>=n: return float('inf') if i == 0: return matrix[i][j] ...
minimum-falling-path-sum
Python soln (recursion + memoization)
logeshsrinivasans
0
24
minimum falling path sum
931
0.685
Medium
15,110
https://leetcode.com/problems/minimum-falling-path-sum/discuss/2080336/python-3-oror-dp-oror-O(n2)-O(1)
class Solution: def minFallingPathSum(self, matrix: List[List[int]]) -> int: n = len(matrix) for i in range(1, n): matrix[i][0] += min(matrix[i - 1][0], matrix[i - 1][1]) matrix[i][-1] += min(matrix[i - 1][-2], matrix[i - 1][-1]) for j in range(1, n - 1):...
minimum-falling-path-sum
python 3 || dp || O(n^2) / O(1)
dereky4
0
35
minimum falling path sum
931
0.685
Medium
15,111
https://leetcode.com/problems/minimum-falling-path-sum/discuss/2066999/Python3-DP-Solution-Explained
class Solution: def minFallingPathSum(self, matrix: List[List[int]]) -> int: length = len(matrix) memo = [[20000] * length for _ in range(length)] def dp(i, j): nonlocal memo if i < 0 or i >= length or j < 0 or j >= length: return 100000 i...
minimum-falling-path-sum
Python3 DP Solution Explained
TongHeartYes
0
20
minimum falling path sum
931
0.685
Medium
15,112
https://leetcode.com/problems/minimum-falling-path-sum/discuss/2066790/Python-or-DP
class Solution: def minFallingPathSum(self, ma: List[List[int]]) -> int: for i in range(len(ma)-2,-1,-1): for j in range(len(ma[0])): if j==0: ma[i][j] += min(ma[i+1][j],ma[i+1][j+1]) elif j==len(ma[0])-1: ma[i][j] += min(ma...
minimum-falling-path-sum
Python | DP
Shivamk09
0
19
minimum falling path sum
931
0.685
Medium
15,113
https://leetcode.com/problems/minimum-falling-path-sum/discuss/2030273/Python-Solution-or-Easy-To-Understand-or-Explanation-or-Dynamic-Programming
class Solution: def minFallingPathSum(self, matrix: List[List[int]]) -> int: rows = len(matrix) cols = len(matrix[0]) for r in range(rows): for c in range(cols): # Non edge column if r > 0 and 0 <= c - 1 and c + 1 < cols: ...
minimum-falling-path-sum
Python Solution | Easy To Understand | Explanation | Dynamic Programming
e_claire
0
18
minimum falling path sum
931
0.685
Medium
15,114
https://leetcode.com/problems/minimum-falling-path-sum/discuss/1887414/WEEB-DOES-PYTHONC%2B%2B-DP-MEMOIZATION
class Solution: def minFallingPathSum(self, matrix: List[List[int]]) -> int: row, col = len(matrix), len(matrix[0]) dp = [] result = float("inf") # edge case if row == 1: return matrix[0][0] for i in range(row): temp = [] for j in range(col): if i == 0: temp.append(matrix[i][j]) el...
minimum-falling-path-sum
WEEB DOES PYTHON/C++ DP MEMOIZATION
Skywalker5423
0
53
minimum falling path sum
931
0.685
Medium
15,115
https://leetcode.com/problems/minimum-falling-path-sum/discuss/1884846/PYTHON-SOL-oror-QUADRATIC-TIME-oror-EASY-oror-EXPLAINED-oror
class Solution: def minFallingPathSum(self, matrix: List[List[int]]) -> int: n = len(matrix) for i in range(1,n): for j in range(n): up = matrix[i-1][j] left = matrix[i-1][j-1] if j>0 else float('inf') right = matrix[i-1][j+1] if j+1<n else...
minimum-falling-path-sum
PYTHON SOL || QUADRATIC TIME || EASY || EXPLAINED ||
reaper_27
0
32
minimum falling path sum
931
0.685
Medium
15,116
https://leetcode.com/problems/minimum-falling-path-sum/discuss/1833022/python-solution-with-comment
class Solution: def minFallingPathSum(self, matrix: List[List[int]]) -> int: m, n = len(matrix), len(matrix[0]) prev, dp = matrix[0], [sys.maxsize for i in range(n)] # dp is going to store the res of mixing previous row and # current row for r in range(1...
minimum-falling-path-sum
python solution with comment
byroncharly3
0
27
minimum falling path sum
931
0.685
Medium
15,117
https://leetcode.com/problems/minimum-falling-path-sum/discuss/1768814/Java-Python3-Simple-DP-Solution-(Top-Down-and-Bottom-Up)
class Solution: def minFallingPathSum(self, matrix: List[List[int]]) -> int: m = len(matrix) n = len(matrix[0]) @lru_cache(None) def dp(row: int, column: int) -> int: ways = matrix[row][column] if row == 0: return ways if column > ...
minimum-falling-path-sum
βœ… [Java / Python3] Simple DP Solution (Top-Down & Bottom-Up)
JawadNoor
0
46
minimum falling path sum
931
0.685
Medium
15,118
https://leetcode.com/problems/minimum-falling-path-sum/discuss/1768814/Java-Python3-Simple-DP-Solution-(Top-Down-and-Bottom-Up)
class Solution: def minFallingPathSum(self, matrix: List[List[int]]) -> int: m, n = len(matrix), len(matrix[0]) dp = [[0]*n for _ in range(m)] for i in range(n): dp[0][i] = matrix[0][i] for row in range(1, m): for column in range(n): dp[row][c...
minimum-falling-path-sum
βœ… [Java / Python3] Simple DP Solution (Top-Down & Bottom-Up)
JawadNoor
0
46
minimum falling path sum
931
0.685
Medium
15,119
https://leetcode.com/problems/minimum-falling-path-sum/discuss/1723460/Python-3-or-Recursive-and-Iterative-Solutions
class Solution: def minFallingPathSum(self, m: List[List[int]]) -> int: #Recursive Solution n = len(m) memo = {} def rec(i, j) : if (i,j) in memo : return memo[(i,j)] if i == n-1 : return m[i]...
minimum-falling-path-sum
Python 3 | Recursive and Iterative Solutions
abhijeetgupto
0
24
minimum falling path sum
931
0.685
Medium
15,120
https://leetcode.com/problems/minimum-falling-path-sum/discuss/1529057/99.77-less-memory-usage(but-quite-slow)
class Solution: def minFallingPathSum(self, matrix: List[List[int]]) -> int: minEle = 101 for i in range(len(matrix)-1, 0, -1): for j in range(len(matrix[0])): minEle = matrix[i][j] if j-1 >= 0: minEle = min(matrix[i][j-1], minEle) if j+1 < len(matrix...
minimum-falling-path-sum
99.77% less memory usage(but quite slow)
siddp6
0
42
minimum falling path sum
931
0.685
Medium
15,121
https://leetcode.com/problems/minimum-falling-path-sum/discuss/1513968/Python-Solution-using-Matrix-Chain-Multiplication
class Solution: def minFallingPathSum(self, matrix: List[List[int]]) -> int: dp=[[None for i in range(len(matrix))] for j in range(len(matrix))] def solve(matrix, i,j): nonlocal dp if i<0 or j<0 or j==len(matrix): return float('inf') if dp[i][j]: return dp[i][j] if i==len(matrix)-1: dp[...
minimum-falling-path-sum
Python Solution using Matrix Chain Multiplication
OkabeRintaro
0
59
minimum falling path sum
931
0.685
Medium
15,122
https://leetcode.com/problems/minimum-falling-path-sum/discuss/1498345/Python-O(n2)-time-O(1)-space-solution
class Solution: def minFallingPathSum(self, arr: List[List[int]]) -> int: n = len(arr) for i in range(1, n): for j in range(0, n): if j == 0: arr[i][j] = min(arr[i-1][j], arr[i-1][j+1]) + arr[i][j] elif j >0 and j < n-1: ...
minimum-falling-path-sum
Python O(n^2) time, O(1) space solution
byuns9334
0
56
minimum falling path sum
931
0.685
Medium
15,123
https://leetcode.com/problems/minimum-falling-path-sum/discuss/1476279/Easy-DP-solution-with-explanationoror-Python3oror-Dynamic-Programming
class Solution: def minFallingPathSum(self, mat: List[List[int]]) -> int: # Creating the DP array dp = [[0 for _ in range(len(mat))] for _ in range(len(mat))] # Storing the elements of first row for j in range(len(mat)): dp[0][j] = mat[0][j] ...
minimum-falling-path-sum
Easy DP solution with explanation|| Python3|| Dynamic Programming
ce17b127
0
36
minimum falling path sum
931
0.685
Medium
15,124
https://leetcode.com/problems/minimum-falling-path-sum/discuss/1434839/Python-3-beginner-friendly-solution-easy-and-concise
class Solution: def gP(self,matrix,ri,ci): coords = [] if ci==0: coords = [ ci,ci+1 ] elif ci==len(matrix)-1: coords = [ ci-1,ci ] else: coords = [ ci-1,ci,ci+1 ] points=[] for cc in coords: try: points.a...
minimum-falling-path-sum
Python 3 beginner friendly solution easy and concise
mathur17021play
0
57
minimum falling path sum
931
0.685
Medium
15,125
https://leetcode.com/problems/minimum-falling-path-sum/discuss/1325475/Python3-solution-using-dynamic-programming
class Solution: def minFallingPathSum(self, matrix: List[List[int]]) -> int: for i in range(len(matrix)-2,-1,-1): for j in range(len(matrix)-1,-1,-1): if j == 0: matrix[i][j] = matrix[i][j] + min(matrix[i+1][j], matrix[i+1][j+1]) elif j == len(...
minimum-falling-path-sum
Python3 solution using dynamic programming
EklavyaJoshi
0
20
minimum falling path sum
931
0.685
Medium
15,126
https://leetcode.com/problems/minimum-falling-path-sum/discuss/1113047/Python-simple-4-line-DP-solution
class Solution: def minFallingPathSum(self, matrix: List[List[int]]) -> int: for r in range(1, len(matrix)): for c in range(len(matrix[0])): matrix[r][c] += min(matrix[r-1][max(0, c-1):c+2]) return min(matrix[-1])
minimum-falling-path-sum
Python simple 4 line DP solution
stom1407
0
85
minimum falling path sum
931
0.685
Medium
15,127
https://leetcode.com/problems/minimum-falling-path-sum/discuss/1070925/Python-Easy-Solution-88-memory-efficient-65-faster
class Solution: def minFallingPathSum(self, matrix: List[List[int]]) -> int: n = len(matrix) dp = [[0] * n for _ in range(n)] dp[n-1] = [matrix[n-1][j] for j in range(n)] for i in reversed(range(n-1)): for j in range(n): minimumPath = dp[i+1][j] ...
minimum-falling-path-sum
Python Easy Solution 88% memory efficient 65% faster
reachrishav1
0
40
minimum falling path sum
931
0.685
Medium
15,128
https://leetcode.com/problems/minimum-falling-path-sum/discuss/750330/Python-Breakdown-and-optimization
class Solution: def minFallingPathSum(self, A: List[List[int]]) -> int: # these are the possible moves moves = [(-1, 0), (-1, -1), (-1, 1)] rows = len(A) cols = len(A[0]) # usual bs if rows == 0: return 0 if rows == 1: return A[0][0] # for saving the minimum cost a...
minimum-falling-path-sum
[Python] Breakdown and optimization
ikouchiha47
0
67
minimum falling path sum
931
0.685
Medium
15,129
https://leetcode.com/problems/minimum-falling-path-sum/discuss/750330/Python-Breakdown-and-optimization
class Solution: def minFallingPathSum(self, A: List[List[int]]) -> int: # this is the resultant array, initially set to cache the top row # because top row has no movemenent to compute min_sums = A[0] # helpers # clamp the value to 0 def clamp_zero(i): return max(i, 0) # get the minimu...
minimum-falling-path-sum
[Python] Breakdown and optimization
ikouchiha47
0
67
minimum falling path sum
931
0.685
Medium
15,130
https://leetcode.com/problems/minimum-falling-path-sum/discuss/461783/Python3-simple-fast-solution
class Solution: def minFallingPathSum(self, A: List[List[int]]) -> int: for i in range(1,len(A)): for j in range(len(A[0])): left,right = A[i-1][j-1] if j-1>=0 else float("inf"),A[i-1][j+1] if j+1<len(A[0]) else float("inf") A[i][j] += min(A[i-1][j],left,right) return min(A[-1])
minimum-falling-path-sum
Python3 simple fast solution
jb07
-1
46
minimum falling path sum
931
0.685
Medium
15,131
https://leetcode.com/problems/minimum-falling-path-sum/discuss/390878/Solution-in-Python-3-(beats-~99)-(three-lines)-(DP)-(-O(1)-space-)
class Solution: def minFallingPathSum(self, A: List[List[int]]) -> int: L, A, m = len(A), [[math.inf] + i + [math.inf] for i in A], math.inf for i,j in itertools.product(range(1,L),range(1,L+1)): A[i][j] += min(A[i-1][j-1],A[i-1][j],A[i-1][j+1]) return min(A[-1]) - Junaid Mansuri (LeetCode ID)@...
minimum-falling-path-sum
Solution in Python 3 (beats ~99%) (three lines) (DP) ( O(1) space )
junaidmansuri
-1
125
minimum falling path sum
931
0.685
Medium
15,132
https://leetcode.com/problems/beautiful-array/discuss/1368125/Detailed-Explanation-with-Diagrams.-A-Collection-of-Ideas-from-Multiple-Posts.-Python3
class Solution: def recurse(self, nums): if len(nums) <= 2: return nums return self.recurse(nums[::2]) + self.recurse(nums[1::2]) def beautifulArray(self, n: int) -> List[int]: return self.recurse([i for i in range(1, n+1)])
beautiful-array
Detailed Explanation with Diagrams. A Collection of Ideas from Multiple Posts. [Python3]
chaudhary1337
45
1,500
beautiful array
932
0.651
Medium
15,133
https://leetcode.com/problems/beautiful-array/discuss/1368125/Detailed-Explanation-with-Diagrams.-A-Collection-of-Ideas-from-Multiple-Posts.-Python3
class Solution: def beautifulArray(self, n: int) -> List[int]: return sorted(range(1, n+1), key=lambda x: bin(x)[:1:-1])
beautiful-array
Detailed Explanation with Diagrams. A Collection of Ideas from Multiple Posts. [Python3]
chaudhary1337
45
1,500
beautiful array
932
0.651
Medium
15,134
https://leetcode.com/problems/beautiful-array/discuss/644612/Python3-solution-with-detailed-explanation-Beautiful-Array
class Solution: def beautifulArray(self, N: int) -> List[int]: nums = list(range(1, N+1)) def helper(nums) -> List[int]: if len(nums) < 3: return nums even = nums[::2] odd = nums[1::2] return helper(even) + helper(old) ...
beautiful-array
Python3 solution with detailed explanation - Beautiful Array
r0bertz
13
1,100
beautiful array
932
0.651
Medium
15,135
https://leetcode.com/problems/beautiful-array/discuss/1184882/Python3-divide-and-conquer
class Solution: def beautifulArray(self, N: int) -> List[int]: def fn(nums): """Return beautiful array by rearraning elements in nums.""" if len(nums) <= 1: return nums return fn(nums[::2]) + fn(nums[1::2]) return fn(list(range(1, N+1)))
beautiful-array
[Python3] divide & conquer
ye15
3
329
beautiful array
932
0.651
Medium
15,136
https://leetcode.com/problems/beautiful-array/discuss/1184882/Python3-divide-and-conquer
class Solution: def beautifulArray(self, n: int) -> List[int]: ans = [1] while len(ans) < n: ans = [2*x-1 for x in ans] + [2*x for x in ans] return [x for x in ans if x <= n]
beautiful-array
[Python3] divide & conquer
ye15
3
329
beautiful array
932
0.651
Medium
15,137
https://leetcode.com/problems/beautiful-array/discuss/1368199/Python3-recursive-one-liner
class Solution: def beautifulArray(self, n: int) -> List[int]: return ( [1, 2][:n] if n < 3 else [x * 2 - 1 for x in self.beautifulArray((n + 1) // 2)] + [x * 2 for x in self.beautifulArray(n // 2)] )
beautiful-array
Python3, recursive one-liner
MihailP
2
247
beautiful array
932
0.651
Medium
15,138
https://leetcode.com/problems/shortest-bridge/discuss/958926/Python3-DFS-and-BFS
class Solution: def shortestBridge(self, A: List[List[int]]) -> int: m, n = len(A), len(A[0]) i, j = next((i, j) for i in range(m) for j in range(n) if A[i][j]) # dfs stack = [(i, j)] seen = set(stack) while stack: i, j = stack.pop() ...
shortest-bridge
[Python3] DFS & BFS
ye15
7
512
shortest bridge
934
0.54
Medium
15,139
https://leetcode.com/problems/shortest-bridge/discuss/1885160/PYTHON-SOL-oror-BFS-%2B-DFS-oror-WELL-WRITTEN-oror-EXPLAINED-oror
class Solution: def shortestBridge(self, grid: List[List[int]]) -> int: n = len(grid) island = [] def dfs(row,col): grid[row][col] = 2 island.append((row,col,0)) moves = ((row+1,col),(row-1,col),(row,col+1),(row,col-1)) for x,y in move...
shortest-bridge
PYTHON SOL || BFS + DFS || WELL WRITTEN || EXPLAINED ||
reaper_27
2
277
shortest bridge
934
0.54
Medium
15,140
https://leetcode.com/problems/shortest-bridge/discuss/1674552/Python-Easy-understanding-solution-Find-smallest-distance-between-2-disconnected-components
class Solution: def isBoundary(self, grid, point) -> bool: x,y = point[0], point[1] if x-1 < 0 or y-1 < 0 or x+1 >= len(grid) or y+1 >= len(grid[x]): return True if grid[x-1][y] == 0: return True if grid[x+1][y] == 0: return True if grid[x][y-1] == 0: return True if g...
shortest-bridge
[Python] Easy-understanding solution - Find smallest distance between 2 disconnected components
freetochoose
2
247
shortest bridge
934
0.54
Medium
15,141
https://leetcode.com/problems/shortest-bridge/discuss/2286355/DFS-to-get-island1-and-BFS-to-get-the-steps(similar-to-rotten-oranges)
class Solution: def shortestBridge(self, grid: List[List[int]]) -> int: n, result, queue = len(grid), 0, [] def dfs(i,j): queue.append((i,j)) grid[i][j] = -1 for x,y in [(0,-1), (0,1), (-1, 0), (1,0)]: xi,yj = x+i,y+j if 0<...
shortest-bridge
πŸ“Œ DFS to get island1 and BFS to get the steps(similar to rotten oranges)
Dark_wolf_jss
1
53
shortest bridge
934
0.54
Medium
15,142
https://leetcode.com/problems/shortest-bridge/discuss/2260402/oror-Python-oror-SPLIT-ISLAND-METHODoror-logic-explainedoror-Explanation-and-comments
class Solution: def shortestBridge(self, grid: List[List[int]]) -> int: n=len(grid) #to print the grid current state #def printgrid(): #for i in range(n): #for j in range(n): #print(grid[i][j],end=" ") #print() ...
shortest-bridge
βœ…|| Python || SPLIT ISLAND METHOD|| logic explained|| Explanation and comments
HarshVardhan71
1
74
shortest bridge
934
0.54
Medium
15,143
https://leetcode.com/problems/shortest-bridge/discuss/1474075/WEEB-DOES-PYTHON-BFS
class Solution: def shortestBridge(self, grid: List[List[int]]) -> int: row, col = len(grid), len(grid[0]) queue1, queue2 = deque([]), deque([]) count = 0 for x in range(row): if count == 1: break for y in range(col): if grid[x][y] == 1: count+=1 queue1.append((x,y)) queue2.append((x,...
shortest-bridge
WEEB DOES PYTHON BFS
Skywalker5423
1
207
shortest bridge
934
0.54
Medium
15,144
https://leetcode.com/problems/shortest-bridge/discuss/2166714/Unique-Solution-oror-No-Expanding-Required-oror-Only-DFS-oror-No-BFS-oror-Manhattan-Distance
class Solution: def isBoundary(self, grid, point): x = point[0] y = point[1] n = len(grid) if x - 1 < 0 or y - 1 < 0 or x + 1 >= n or y + 1 >= n: return True if grid[x-1][y] == 0: return True elif grid[x+1][y] == 0: return ...
shortest-bridge
Unique Solution || No Expanding Required || Only DFS || No BFS || Manhattan Distance
Vaibhav7860
0
52
shortest bridge
934
0.54
Medium
15,145
https://leetcode.com/problems/shortest-bridge/discuss/1905045/Python-Bread-First-Search-for-Graph
class Solution: def shortestBridge(self, grid: List[List[int]]) -> int: # ==== use BFS obtain positions of one land. Start from the land, and do BFS to find another land. Layer cost is the minimum distance ==== directions = [(0,1), (0,-1), (1,0), (-1,0)] # right, left, up, down len...
shortest-bridge
Python - Bread First Search for Graph
wanzelin007
0
91
shortest bridge
934
0.54
Medium
15,146
https://leetcode.com/problems/shortest-bridge/discuss/1817649/Python-BFS
class Solution: def shortestBridge(self, grid: List[List[int]]) -> int: N = len(grid) def get_neighbors(i, j): for ni, nj in ((i + 1, j), (i - 1, j), (i, j + 1), (i, j - 1)): if 0 <= ni < N and 0 <= nj < N: yield ni, nj # Find a piece of an i...
shortest-bridge
Python BFS
mjgallag
0
110
shortest bridge
934
0.54
Medium
15,147
https://leetcode.com/problems/shortest-bridge/discuss/1811777/Python-DFS-and-Multi-Source-BFS
class Solution: def shortestBridge(self, grid: List[List[int]]) -> int: rows, cols = len(grid), len(grid[0]) Coordinate = namedtuple('Coordinate', ['x', 'y']) def withinBounds(pos): return 0 <= pos.x < rows and 0 <= pos.y < cols def dfs(pos, island, mark...
shortest-bridge
Python DFS and Multi-Source BFS
Rush_P
0
42
shortest bridge
934
0.54
Medium
15,148
https://leetcode.com/problems/shortest-bridge/discuss/1313900/python-dfs-to-mark-first-island-then-bfs-to-expand-from-it
class Solution: def shortestBridge(self, grid: List[List[int]]) -> int: rows, cols = len(grid), len(grid[0]) # Mark the first island with '#'s def dfs(row, col): if row not in range(rows) or col not in range(cols) or grid[row][col] != 1: return False grid[row][col] ...
shortest-bridge
python dfs to mark first island then bfs to expand from it
uzumaki01
0
222
shortest bridge
934
0.54
Medium
15,149
https://leetcode.com/problems/knight-dialer/discuss/1544986/Python-simple-dp-O(n)-time-O(1)-space
class Solution: def knightDialer(self, n: int) -> int: arr = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1] for _ in range(n-1): dp = [0 for _ in range(10)] dp[0] = arr[5] + arr[7] dp[1] = arr[6] + arr[8] dp[2] = arr[3] + arr[7] dp[3] = a...
knight-dialer
Python simple dp, O(n) time O(1) space
byuns9334
3
493
knight dialer
935
0.5
Medium
15,150
https://leetcode.com/problems/knight-dialer/discuss/2761128/Python-DP.-Time%3A-O(N)-Space%3A-O(1)
class Solution: def knightDialer(self, n: int) -> int: dp = [1] * 10 moves = [[4, 6], [6, 8], [7, 9], [4, 8], [3, 9, 0], [], [1, 7, 0], [2, 6], [1, 3], [2, 4]] for _ in range(n-1): dp_next = [0] * 10 for digit in range(10): for move_di...
knight-dialer
Python, DP. Time: O(N), Space: O(1)
blue_sky5
2
145
knight dialer
935
0.5
Medium
15,151
https://leetcode.com/problems/knight-dialer/discuss/2136453/Python-somewhat-ok-solution
class Solution: def knightDialer(self, n: int) -> int: Mod = 10**9 + 7 pad = [ (4, 6), (8, 6), (7, 9), (4, 8), (3, 9, 0), (), (1, 7, 0), (2, 6), (1, 3), (2, 4) ] @cache def dfs(i, n): # search reached the end, found 1 solution ...
knight-dialer
Python somewhat ok solution
t136280
1
98
knight dialer
935
0.5
Medium
15,152
https://leetcode.com/problems/knight-dialer/discuss/1923343/python3-DP-Top-Down
class Solution: def knightDialer(self, n: int) -> int: MOD = 10**9 + 7 adj = { 0: [6, 4], 1: [6, 8], 2: [9, 7], 3: [4, 8], 4: [9, 3, 0], 5: [], 6: [7, 1, 0], 7: [6, 2], ...
knight-dialer
python3 DP Top-Down
DheerajGadwala
1
149
knight dialer
935
0.5
Medium
15,153
https://leetcode.com/problems/knight-dialer/discuss/958867/Python3-dp-O(N)
class Solution: def knightDialer(self, n: int) -> int: mp = {0: [4, 6], 1: [6, 8], 2: [7, 9], 3: [4, 8], 4: [0, 3, 9], 5: [], 6: [0, 1, 7], 7: [2, 6], 8: [1, 3], 9: [2, 4]} @lru_cache(None) def fn(n, k): """Return """ if n == 1: return 1 ...
knight-dialer
[Python3] dp O(N)
ye15
1
202
knight dialer
935
0.5
Medium
15,154
https://leetcode.com/problems/knight-dialer/discuss/958867/Python3-dp-O(N)
class Solution: def knightDialer(self, n: int) -> int: mp = {0: [4, 6], 1: [6, 8], 2: [7, 9], 3: [4, 8], 4: [0, 3, 9], 5: [], 6: [0, 1, 7], 7: [2, 6], 8: [1, 3], 9: [2, 4]} ans = [1]*10 for _ in range(n-1): temp = [0]*10 for i in range(10): ...
knight-dialer
[Python3] dp O(N)
ye15
1
202
knight dialer
935
0.5
Medium
15,155
https://leetcode.com/problems/knight-dialer/discuss/2476420/Python-or-MEMO-or-Solution
class Solution: def knightDialer(self, n: int) -> int: li = [(4, 6), (8, 6), (7, 9), (4, 8), (3, 9, 0),(), (1, 7, 0), (2, 6), (1, 3), (2, 4)] Mod = 10**9 + 7 def dp(crt,memo,s): if s==0: return 1 elif (crt,s) in memo: return memo[(crt,s...
knight-dialer
Python | MEMO | Solution
Brillianttyagi
0
112
knight dialer
935
0.5
Medium
15,156
https://leetcode.com/problems/knight-dialer/discuss/1885287/PYTHON-SOL-oror-RECURSION-%2B-MEMO-oror-EASY-oror-EXPLAINED-oror
class Solution: def knightDialer(self, n: int) -> int: can_go = {0:(4,6),1:(8,6),2:(7,9),3:(4,8),4:(0,3,9),\ 5:(),6:(0,1,7),7:(2,6),8:(1,3),9:(2,4)} dp = {} def recursion(size,cr): if size == 0:return 1 if (size,cr) in dp:return dp[(size,cr)] ...
knight-dialer
PYTHON SOL || RECURSION + MEMO || EASY || EXPLAINED ||
reaper_27
0
225
knight dialer
935
0.5
Medium
15,157
https://leetcode.com/problems/knight-dialer/discuss/1868495/Python3-RECURSION
class Solution: def knightDialer(self, n: int) -> int: if n == 1: return 10 MOD = 10**9 + 7 adj = { 1: (6, 8), 2: (9, 7), 3: (4, 8), 4: (0, 3, 9), 5: (), 6: (0, 1, 7), 7: (2, 6), 8: (1, 3...
knight-dialer
[Python3] RECURSION
artod
0
137
knight dialer
935
0.5
Medium
15,158
https://leetcode.com/problems/knight-dialer/discuss/1850220/Clean-Python-DP
class Solution: def knightDialer(self, n: int) -> int: table = {'-1': '1234567890', '0': '46', '1': '86', '2': '79', '3': '48', '4': '390', '5': '', '6': '170', '7': '26', '8': '13', '9': '24'} @cache def dp(i, cnt): if cnt == n: ...
knight-dialer
Clean Python DP
r_vaghefi
0
128
knight dialer
935
0.5
Medium
15,159
https://leetcode.com/problems/knight-dialer/discuss/1078579/python3-%3A-recursion-%2B-memos-into-tabulation!
class Solution: def knightDialer1(self, n: int) -> int: # observe pattern of knight moves and store in lookup table # 0 -> 4, 6 # 1 -> 6, 8 # 2 -> 7, 9 # 3 -> 4, 8 # 4 -> 3, 9, 0 # 5 -> - # 6 -> 1, 7, 0 # 7 -> 2, 6 # 8 -> 1, 3 ...
knight-dialer
python3 : recursion + memos into tabulation!
dachwadachwa
0
134
knight dialer
935
0.5
Medium
15,160
https://leetcode.com/problems/knight-dialer/discuss/792986/Clean-Recursive-Solution-with-Memoization.-Help-needed!!
class Solution: NEIGHBORS_MAP = { 1: (6, 8), 2: (7, 9), 3: (4, 8), 4: (3, 9, 0), 5: tuple(), 6: (1, 7, 0), 7: (2, 6), 8: (1, 3), 9: (2, 4), 0: (4, 6), } def getNeighbors(self,...
knight-dialer
Clean Recursive Solution with Memoization. Help needed!!
faizulhai
0
110
knight dialer
935
0.5
Medium
15,161
https://leetcode.com/problems/stamping-the-sequence/discuss/1888562/PYTHON-SOL-oror-WELL-EXPLAINED-oror-SIMPLE-ITERATION-oror-EASIEST-YOU-WILL-FIND-EVER-!!-oror
class Solution: def movesToStamp(self, stamp: str, target: str) -> List[int]: N,M = len(target),len(stamp) move = 0 maxmove = 10*N ans = [] def check(string): for i in range(M): if string[i] == stamp[i] or string[i] == '?': cont...
stamping-the-sequence
PYTHON SOL || WELL EXPLAINED || SIMPLE ITERATION || EASIEST YOU WILL FIND EVER !! ||
reaper_27
7
258
stamping the sequence
936
0.633
Hard
15,162
https://leetcode.com/problems/stamping-the-sequence/discuss/1136481/Python-Simple-Greedy-Solution
class Solution: def movesToStamp(self, s: str, t: str) -> List[int]: options = {i*'*' + s[i:j] + (len(s)-j)*'*' for i in range(len(s)) for j in range(i, len(s)+1)} - {'*'*len(s)} res = [] target = list(t) updates = -1 while updates: i = updates = 0 ...
stamping-the-sequence
[Python] Simple Greedy Solution
rowe1227
6
204
stamping the sequence
936
0.633
Hard
15,163
https://leetcode.com/problems/stamping-the-sequence/discuss/2456466/Stamping-The-Sequence
class Solution: def movesToStamp(self, s: str, t: str) -> List[int]: options = {i*'*' + s[i:j] + (len(s)-j)*'*' for i in range(len(s)) for j in range(i, len(s)+1)} - {'*'*len(s)} res = [] target = list(t) updates = -1 while updates: i = updates = 0 ...
stamping-the-sequence
Stamping The Sequence
klu_2100031497
1
194
stamping the sequence
936
0.633
Hard
15,164
https://leetcode.com/problems/stamping-the-sequence/discuss/2814981/Python-with-stamp-covers-and-the-use-of-bounding-variables-beats-90-on-average
class Solution: def movesToStamp(self, stamp: str, target: str) -> List[int]: # edge case consideration if stamp == target : return [0] # get stamp and target length stamp_length, target_length = len(stamp), len(target) result = [] # keep count and max...
stamping-the-sequence
Python with stamp covers and the use of bounding variables beats 90% on average
laichbr
0
1
stamping the sequence
936
0.633
Hard
15,165
https://leetcode.com/problems/stamping-the-sequence/discuss/2681350/Python-or-Same-as-everyone-else
class Solution: def movesToStamp(self, stamp: str, target: str) -> List[int]: l_s, l_t = len(stamp), len(target) s = '?'*l_t res = [] perm = set() for i in range(l_s): for j in range(l_s-i): perm.add('?'*i + stamp[i:l_s-j] + '?'*j) while ...
stamping-the-sequence
Python | Same as everyone else
jainsiddharth99
0
5
stamping the sequence
936
0.633
Hard
15,166
https://leetcode.com/problems/stamping-the-sequence/discuss/2472845/reverse-approach
class Solution: def movesToStamp(self, stamp: str, target: str) -> List[int]: indices = []; turn = 0 t = [c for c in target] L = len(stamp); T = len(target) while turn <= 10*T and any(c != '?' for c in t): i = T - L while i >= 0 : if a...
stamping-the-sequence
reverse approach
sinha_meenu
0
11
stamping the sequence
936
0.633
Hard
15,167
https://leetcode.com/problems/stamping-the-sequence/discuss/2460996/Easy-and-Clear-Solution-Python3
class Solution: def movesToStamp(self, stamp: str, target: str) -> List[int]: res=[] sl=len(stamp) tl=len(target) done=tl*'*' klem=[] for i in range(sl): for j in range(sl - i): klem.append('*' * i + stamp[i:sl-j] + '*' * j) ...
stamping-the-sequence
Easy and Clear Solution Python3
moazmar
0
10
stamping the sequence
936
0.633
Hard
15,168
https://leetcode.com/problems/stamping-the-sequence/discuss/2460359/Python3-oror-Easy-own-code-oror-92-Faster-oror-Explained
class Solution: def corresponds(self, stamp, target, idx): for s in stamp: if s == target[idx] or target[idx] == '*': idx += 1 else: return False return True def movesToStamp(self, stamp: str, target: str) -> List[int]: n...
stamping-the-sequence
Python3 🐍|| Easy own code || 92% Faster πŸ”₯🧯 || Explained
Dewang_Patil
0
20
stamping the sequence
936
0.633
Hard
15,169
https://leetcode.com/problems/stamping-the-sequence/discuss/2459906/Python3-oror-98ms-Fast-and-easy-Solution-to-%22Stamping-The-sequence-%3A)'
class Solution: def movesToStamp(self, stamp: str, target: str) -> List[int]: slen, tlen = len(stamp), len(target) res = [] s_covers = set() for i in range(slen): for j in range(slen - i): s_covers.add('#' * i + stamp[i:slen-j] + '#' * j) # p...
stamping-the-sequence
Python3 || 98ms Fast and easy Solution to "Stamping The sequence :)'
WhiteBeardPirate
0
15
stamping the sequence
936
0.633
Hard
15,170
https://leetcode.com/problems/stamping-the-sequence/discuss/2457954/Sliding-window-python3-solution
class Solution: # O(n(n-m) * m) time, # O(n-m) space, # Approach: sliding window, def movesToStamp(self, stamp: str, target: str) -> List[int]: n = len(target) m = len(stamp) target = list(target) ans = [] vstd_indexes = set() def isStam...
stamping-the-sequence
Sliding window python3 solution
destifo
0
16
stamping the sequence
936
0.633
Hard
15,171
https://leetcode.com/problems/reorder-data-in-log-files/discuss/1135934/Python3-simple-solution
class Solution: def reorderLogFiles(self, logs: List[str]) -> List[str]: l = [] d = [] for i in logs: if i.split()[1].isdigit(): d.append(i) else: l.append(i) l.sort(key = lambda x : x.split()[0]) l.sort(key = lambda x :...
reorder-data-in-log-files
Python3 simple solution
EklavyaJoshi
12
495
reorder data in log files
937
0.564
Medium
15,172
https://leetcode.com/problems/reorder-data-in-log-files/discuss/382667/Solution-in-Python-3-(beats-~100)-(five-lines)
class Solution: def reorderLogFiles(self, G: List[str]) -> List[str]: A, B, G = [], [], [i.split() for i in G] for g in G: if g[1].isnumeric(): B.append(g) else: A.append(g) return [" ".join(i) for i in sorted(A, key = lambda x: x[1:]+[x[0]]) + B] - Junaid Mansuri (LeetCode ID)@hotm...
reorder-data-in-log-files
Solution in Python 3 (beats ~100%) (five lines)
junaidmansuri
8
3,700
reorder data in log files
937
0.564
Medium
15,173
https://leetcode.com/problems/reorder-data-in-log-files/discuss/694018/PythonBests-99-O(1)-Space-and-O(NLogN)-Time-Readable-with-comments
class Solution: def reorderLogFiles(self, logs: List[str]) -> List[str]: return logs.sort(key= lambda log: self.customSort(log)) def customSort(self, log: str) -> tuple: #seperate identifer and suffix into two seperate arrays log_info = log.split(" ", 1) #check if log is a letter or...
reorder-data-in-log-files
PythonBests 99% O(1) Space and O(NLogN) Time - Readable with comments
Prince_Zamunda
4
1,300
reorder data in log files
937
0.564
Medium
15,174
https://leetcode.com/problems/reorder-data-in-log-files/discuss/1366672/Easily-understandable-Python-Code!!
class Solution: def reorderLogFiles(self, logs: List[str]) -> List[str]: if not logs: return logs_l = [] logs_d = [] logs_sorted = [] for log in logs: if log.split()[1].isdigit(): logs_d.append(log) else: ...
reorder-data-in-log-files
Easily understandable Python Code!!
adityarichhariya7879
3
476
reorder data in log files
937
0.564
Medium
15,175
https://leetcode.com/problems/reorder-data-in-log-files/discuss/2580452/Python-Solution
class Solution: def reorderLogFiles(self, logs: List[str]) -> List[str]: digit = [] letter = [] for log in logs: if log[-1].isdigit(): digit.append(log) else: letter.append(log) letter = [x.split(" ", m...
reorder-data-in-log-files
Python Solution
MushroomRice
1
74
reorder data in log files
937
0.564
Medium
15,176
https://leetcode.com/problems/reorder-data-in-log-files/discuss/1958488/Intuitive-Approach
class Solution(object): def reorderLogFiles(self, logs): """ :type logs: List[str] :rtype: List[str] """ all_letter_logs = [] all_digit_logs = [] for log in logs: temp = log.split() if all(map(str.isdigit, temp[1:])): ...
reorder-data-in-log-files
Intuitive Approach
rishav-ish
1
121
reorder data in log files
937
0.564
Medium
15,177
https://leetcode.com/problems/reorder-data-in-log-files/discuss/1912127/Easy-Python-beats-93.96
class Solution: def reorderLogFiles(self, logs: List[str]) -> List[str]: digLog = [log for log in logs if log.split()[1].isdigit()] letLog = [log for log in logs if log not in digLog] letLog.sort(key=lambda x: (x.split()[1:], x.split()[0])) return letLog + digLog
reorder-data-in-log-files
Easy Python beats 93.96%
weiting-ho
1
330
reorder data in log files
937
0.564
Medium
15,178
https://leetcode.com/problems/reorder-data-in-log-files/discuss/1871835/Simple-one-liner-sort
class Solution: def reorderLogFiles(self, logs: List[str]) -> List[str]: a = [] b = [] for i in logs: if i.split()[1].isalpha(): a.append(i) else: b.append(i) a.sort(key=lambda x:(x.split()[1:len(x)],x.split()[0])) retur...
reorder-data-in-log-files
Simple one liner sort
sushmitha0127
1
203
reorder data in log files
937
0.564
Medium
15,179
https://leetcode.com/problems/reorder-data-in-log-files/discuss/1605196/Python3-simple-solution
class Solution: def reorderLogFiles(self, logs: List[str]) -> List[str]: letter_list = [] digit_list = [] for i in range(len(logs)): tokens = logs[i].split() if tokens[1].isalpha(): letter_list.append(logs[i]) else: ...
reorder-data-in-log-files
Python3 simple solution
evancao1429
1
215
reorder data in log files
937
0.564
Medium
15,180
https://leetcode.com/problems/reorder-data-in-log-files/discuss/1557101/Super-easy-to-understand-python-3-beats-93
class Solution: def reorderLogFiles(self, logs: List[str]) -> List[str]: # filter out all the logs where the second part of the log is a letter # (because first part is an identifier than can be anything) ll = list(filter(lambda x: x.split()[1].isalpha(), logs)) # filter out all the logs where the sec...
reorder-data-in-log-files
Super easy to understand python 3 beats 93%
Daniele122898
1
238
reorder data in log files
937
0.564
Medium
15,181
https://leetcode.com/problems/reorder-data-in-log-files/discuss/1444650/Python3Python-Solution-using-isdigit-method-and-sorting-w-comments
class Solution: def reorderLogFiles(self, logs: List[str]) -> List[str]: # Init list to contain letter and digit logs letter_logs = [] digit_logs = [] # For each log separate and put them into separate logs for log in logs: l = log.split(" ") ...
reorder-data-in-log-files
[Python3/Python] Solution using isdigit method and sorting w/ comments
ssshukla26
1
304
reorder data in log files
937
0.564
Medium
15,182
https://leetcode.com/problems/reorder-data-in-log-files/discuss/469546/Python%3A-Easy-to-understand-solution
class Solution: def reorderLogFiles(self, logs: List[str]) -> List[str]: letterLogs = [] letterDict = {} digitLogs = [] for log in logs: split_list = log.split(" ") # If the second word is alphabetic, add it to a dictionary, # replacing whitespaces with "," and ap...
reorder-data-in-log-files
Python: Easy to understand solution
rafaelvalle
1
508
reorder data in log files
937
0.564
Medium
15,183
https://leetcode.com/problems/reorder-data-in-log-files/discuss/2847725/Separating-logs-sorting-then-combining
class Solution: def reorderLogFiles(self, logs: List[str]) -> List[str]: # create two seperate data structures for letter logs and digits logs # iterate through logs and see if it's a letter log or a digit log. # if it's a digit log append it to the dlogs array to maintain it's order ...
reorder-data-in-log-files
Separating logs, sorting, then combining
andrewnerdimo
0
1
reorder data in log files
937
0.564
Medium
15,184
https://leetcode.com/problems/reorder-data-in-log-files/discuss/2678189/easy-python-ssolution
class Solution: def reorderLogFiles(self, logs: List[str]) -> List[str]: dig = [] let = [] for log in logs: if log.rsplit(" ",1)[-1].isnumeric(): dig.append(log) else: let.append(log.split(" ",1)) let.sort(key= lambda x:...
reorder-data-in-log-files
easy python ssolution
abhi2411
0
8
reorder data in log files
937
0.564
Medium
15,185
https://leetcode.com/problems/reorder-data-in-log-files/discuss/2646704/Python-Easy-Custom-sort
class Solution: def reorderLogFiles(self, logs: List[str]) -> List[str]: digit = [] letter = [] for i in logs: if i[-1].isdigit(): digit.append(i) else: letter.append(i) letter = [x.split(' ',maxsplit=1) for x ...
reorder-data-in-log-files
Python Easy Custom sort
Brillianttyagi
0
45
reorder data in log files
937
0.564
Medium
15,186
https://leetcode.com/problems/reorder-data-in-log-files/discuss/2542973/Python-runtime-O(mn-logn)-memory-O(mn)
class Solution: def reorderLogFiles(self, logs: List[str]) -> List[str]: letterLog = [] digitLog = [] for i, log in enumerate(logs): s = log.split(" ") if s[1].isdigit(): digitLog.append(log) else: letterLog.append((s[1:], ...
reorder-data-in-log-files
Python, runtime O(mn logn), memory O(mn)
tsai00150
0
79
reorder data in log files
937
0.564
Medium
15,187
https://leetcode.com/problems/reorder-data-in-log-files/discuss/2066933/Python3-Custom-Comparator-Concise
class Solution: def reorderLogFiles(self, logs: List[str]) -> List[str]: digit_logs = [] letter_logs_indices = [] letter_log_map = {} for i, log in enumerate(logs): if log[-1] >= 'a'and log[-1] <= 'z': words = log.split() ...
reorder-data-in-log-files
Python3 Custom Comparator Concise
shtanriverdi
0
121
reorder data in log files
937
0.564
Medium
15,188
https://leetcode.com/problems/reorder-data-in-log-files/discuss/2036399/SIMPLE-PYTHON-SOLUTION-USING-SORTED
class Solution: def reorderLogFiles(self, logs: List[str]) -> List[str]: nl = sorted(logs,key = lambda x:self.srt(x)) return nl def srt(self, item): ig = item.split() i = list(ig) idd = i[0] if i[1][0] in ['0','1','2','3','4','...
reorder-data-in-log-files
SIMPLE PYTHON SOLUTION USING SORTED
byyoung3
0
172
reorder data in log files
937
0.564
Medium
15,189
https://leetcode.com/problems/reorder-data-in-log-files/discuss/1968290/Python-3-Solution
class Solution: def reorderLogFiles(self, logs: List[str]) -> List[str]: digit_log = [] letter_log = [] d = {} for log in logs: for i in range(len(log)): if log[i] == ' ': if log[i+1] in '0123456789': digit_log.a...
reorder-data-in-log-files
Python 3 Solution
DietCoke777
0
173
reorder data in log files
937
0.564
Medium
15,190
https://leetcode.com/problems/reorder-data-in-log-files/discuss/1934320/Python3
class Solution: def reorderLogFiles(self, logs: List[str]) -> List[str]: letter_logs = [] digit_logs = [] for log in logs: elems = log.split(' ') if elems[1].isalpha(): heapq.heappush(letter_logs, (elems[1:], elems[0], log)) ...
reorder-data-in-log-files
Python3
AlphaMonkey9
0
166
reorder data in log files
937
0.564
Medium
15,191
https://leetcode.com/problems/reorder-data-in-log-files/discuss/1453221/My-approach-using-Python3
class Solution: def sortHelperKey(self, log): return log.split()[0] def sortHelperContent(self, log): return log.split()[1:] def reorderLogFiles(self, logs: List[str]) -> List[str]: letter_logs = [] digit_logs = [] for log in logs: curre...
reorder-data-in-log-files
My approach using Python3
pcv
0
337
reorder data in log files
937
0.564
Medium
15,192
https://leetcode.com/problems/reorder-data-in-log-files/discuss/1420277/Python3-Faster-Than-96.86-Memory-Less-Than-96.72
class Solution: def reorderLogFiles(self, logs: List[str]) -> List[str]: digit, words = [], [] for i in logs: if i.split()[1].isnumeric(): digit += [i] else: words += [i] return sorted(words, key = lambda x : (x.split...
reorder-data-in-log-files
Python3 Faster Than 96.86%, Memory Less Than 96.72%
Hejita
0
142
reorder data in log files
937
0.564
Medium
15,193
https://leetcode.com/problems/reorder-data-in-log-files/discuss/1316336/Python3-Solution-using-sorting-and-split
class Solution: def reorderLogFiles(self, logs: List[str]) -> List[str]: letters=[] digits=[] for x in logs: xx=x.split() ident=xx[1] t=" ".join(xx[1:]) if ident.isdigit(): digits.append(x) else: lett...
reorder-data-in-log-files
Python3 Solution using sorting and split
atm1504
0
113
reorder data in log files
937
0.564
Medium
15,194
https://leetcode.com/problems/reorder-data-in-log-files/discuss/315213/Python-solution-using-dictionary
class Solution: def reorderLogFiles(self, logs: List[str]) -> List[str]: res=[] digi=[] letter=[] for i in logs: k=i.split() if k[1].isdigit(): digi.append(i) else: letter.append(i) d={} for i in lett...
reorder-data-in-log-files
Python solution using dictionary
ketan35
0
570
reorder data in log files
937
0.564
Medium
15,195
https://leetcode.com/problems/range-sum-of-bst/discuss/1627963/Python3-ITERATIVE-BFS-Explained
class Solution: def rangeSumBST(self, root: Optional[TreeNode], lo: int, hi: int) -> int: res = 0 q = deque([root]) while q: c = q.popleft() v, l, r = c.val, c.left, c.right if lo <= v and v <= hi: res += v ...
range-sum-of-bst
βœ”οΈ [Python3] ITERATIVE BFS, Explained
artod
3
237
range sum of bst
938
0.854
Easy
15,196
https://leetcode.com/problems/range-sum-of-bst/discuss/1627797/Python3-Clean-or-7-Lines-or-O(n)-Time-(beats-94.28-)-or-O(n)-Space-or-DFS
class Solution: def rangeSumBST(self, root: Optional[TreeNode], low: int, high: int) -> int: if not root: return 0 res = root.val if low <= root.val <= high else 0 if root.val <= low: return res + self.rangeSumBST(root.right, low, high) if root.val >= high: return res + self.rangeSum...
range-sum-of-bst
[Python3] Clean | 7 Lines | O(n) Time (beats 94.28 %) | O(n) Space | DFS
PatrickOweijane
3
353
range sum of bst
938
0.854
Easy
15,197
https://leetcode.com/problems/range-sum-of-bst/discuss/1438473/Recursive-88-speed
class Solution: def rangeSumBST(self, root: Optional[TreeNode], low: int, high: int) -> int: ans = 0 def traverse(node: Optional[TreeNode]): nonlocal ans if node: if low <= node.val <= high: ans += node.val if node.left and...
range-sum-of-bst
Recursive, 88% speed
EvgenySH
2
303
range sum of bst
938
0.854
Easy
15,198
https://leetcode.com/problems/range-sum-of-bst/discuss/1198589/Python-3-or-DFS-or-Easy-Understand
class Solution: def rangeSumBST(self, root: TreeNode, low: int, high: int) -> int: result = [] self.dfs(root, low, high, result) return sum(result) def dfs(self, root, low, high, result): if root: if root.val < low: self.dfs(...
range-sum-of-bst
Python 3 | DFS | Easy Understand
itachieve
2
120
range sum of bst
938
0.854
Easy
15,199