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/partition-equal-subset-sum/discuss/838500/Python3-knapsack-solved-by-top-down-dp | class Solution:
def canPartition(self, nums: List[int]) -> bool:
sm = sum(nums)
if sm & 1: return False
bits = 1
for x in nums: bits |= bits << x
return bool(bits & (1<<sm//2)) | partition-equal-subset-sum | [Python3] knapsack solved by top-down dp | ye15 | 1 | 118 | partition equal subset sum | 416 | 0.466 | Medium | 7,400 |
https://leetcode.com/problems/partition-equal-subset-sum/discuss/2797452/Python-(Simple-Dynamic-Programming) | class Solution:
def canPartition(self, nums):
if sum(nums)%2 != 0:
return False
max_weight = sum(nums)//2
dp = [0]*(max_weight+1)
for i in nums:
for w in range(max_weight,-1,-1):
if w - i >= 0:
dp[w] = max(dp[w], i + dp[w... | partition-equal-subset-sum | Python (Simple Dynamic Programming) | rnotappl | 0 | 5 | partition equal subset sum | 416 | 0.466 | Medium | 7,401 |
https://leetcode.com/problems/partition-equal-subset-sum/discuss/2745906/python3-Recursion-and-sets-dp-sol-for-reference. | class Solution:
def canPartition(self, nums: List[int]) -> bool:
ans = False
N = len(nums)
ts = sum(nums)
ns = set([0])
for i in range(N):
for j in list(ns):
p = j+nums[i]
if ts-p > p:
ns.add(p)
... | partition-equal-subset-sum | [python3] Recursion and sets / dp sol for reference. | vadhri_venkat | 0 | 3 | partition equal subset sum | 416 | 0.466 | Medium | 7,402 |
https://leetcode.com/problems/partition-equal-subset-sum/discuss/2745906/python3-Recursion-and-sets-dp-sol-for-reference. | class Solution:
def canPartition(self, nums: List[int]) -> bool:
ans = False
N = len(nums)
ts = sum(nums)
h = defaultdict(lambda: -1)
@lru_cache(None)
def r(idx, cnt):
nonlocal ans
if idx == N: return False
if ans: return ... | partition-equal-subset-sum | [python3] Recursion and sets / dp sol for reference. | vadhri_venkat | 0 | 3 | partition equal subset sum | 416 | 0.466 | Medium | 7,403 |
https://leetcode.com/problems/partition-equal-subset-sum/discuss/2715144/Python3-DP-Solution | class Solution:
def isSubSet(self, nums, n, dp, total):
if total==0:
return True
if n==-1:
return False
if dp[n][total]!=-1:
return dp[n][total]
if nums[n]>total:
dp[n][total] = self.isSubSet(nums,n-1,dp,total)
dp[n][total] = se... | partition-equal-subset-sum | Python3 DP Solution | paul1202 | 0 | 8 | partition equal subset sum | 416 | 0.466 | Medium | 7,404 |
https://leetcode.com/problems/partition-equal-subset-sum/discuss/2686880/Python-3-solution-faster-than-99 | class Solution:
def canPartition(self, nums: List[int]) -> bool:
target = sum(nums) / 2
if target%1 != 0: # if not even, there can't be a solution
return False
possib = {0} # set to store all the subset sums seen
for num in nums[:-1]:
possib = possib | {x+nu... | partition-equal-subset-sum | Python 3 solution faster than 99% | user6658I | 0 | 22 | partition equal subset sum | 416 | 0.466 | Medium | 7,405 |
https://leetcode.com/problems/partition-equal-subset-sum/discuss/2660341/RECURSION-WITH-MEMOIZATION | class Solution:
def canPartition(self, nums: List[int]) -> bool:
if sum(nums) % 2:
return False
target = sum(nums) // 2
memo = [[-1 for _ in range(target+1)] for _ in range(len(nums)+1)]
def part(nums, target, n):
if target == 0:
retur... | partition-equal-subset-sum | RECURSION WITH MEMOIZATION | leomensah | 0 | 64 | partition equal subset sum | 416 | 0.466 | Medium | 7,406 |
https://leetcode.com/problems/partition-equal-subset-sum/discuss/2634404/Python-DP-faster-than-98-(Easy-Understanding) | class Solution:
def canPartition(self, nums: List[int]) -> bool:
s = sum(nums)
# Bit operation, if s is odd then returns False otherwise True
if s&1:
return False
# The target is to choose a subset of elements where the sum is equal to the sum(s)/2
# dp saves the... | partition-equal-subset-sum | Python DP faster than 98% (Easy Understanding) | mkming1226 | 0 | 45 | partition equal subset sum | 416 | 0.466 | Medium | 7,407 |
https://leetcode.com/problems/partition-equal-subset-sum/discuss/2497705/Cleanest-Simplest-Fastest-Python3-You've-Ever-Seen | class Solution:
def canPartition(self, nums: List[int]) -> bool:
n, sm = len(nums), sum(nums)
if n < 2 or sm % 2:
return False
half = sm // 2
mem = [True] + [False] * half
for i in range(n):
for sum1 in range(half, 0, -1):
if sum1 >... | partition-equal-subset-sum | Cleanest, Simplest, Fastest Python3 You've Ever Seen | ryangrayson | 0 | 48 | partition equal subset sum | 416 | 0.466 | Medium | 7,408 |
https://leetcode.com/problems/partition-equal-subset-sum/discuss/2472800/DP-Subset-Sum-Python3 | class Solution:
def canPartition(self, nums: List[int]) -> bool:
if sum(nums)%2==1:
return False
summ=sum(nums)//2
dp=[[False for i in range(summ+1)]for j in range(len(nums)+1)]
for i in range(len(nums)+1):
dp[i][0]=True
... | partition-equal-subset-sum | DP Subset Sum - Python3 | Prithiviraj1927 | 0 | 76 | partition equal subset sum | 416 | 0.466 | Medium | 7,409 |
https://leetcode.com/problems/partition-equal-subset-sum/discuss/2411618/Python3-or-Solved-Bottom-Up-or-DP-or-Tabulation | class Solution:
#Time-Complexity: O(n * summation), since in worst case the distinct subset sum vals
#can range from 1 to summation! -> We are iterating through n distinct state parameter
#values for index position i!
#Space-Complexity: O(n * summation)
def canPartition(self, nums: List[int]) -... | partition-equal-subset-sum | Python3 | Solved Bottom-Up | DP | Tabulation | JOON1234 | 0 | 34 | partition equal subset sum | 416 | 0.466 | Medium | 7,410 |
https://leetcode.com/problems/partition-equal-subset-sum/discuss/2339554/python3-easy-fast-concise-solution | class Solution:
def canPartition(self, nums: List[int]) -> bool:
if sum(nums) % 2:
return False
dp = set()
dp.add(0)
target = sum(nums) // 2
for i in range(len(nums) - 1, -1, -1):
nextDp = set()
for t... | partition-equal-subset-sum | python3 easy fast concise solution | soumyadexter7 | 0 | 74 | partition equal subset sum | 416 | 0.466 | Medium | 7,411 |
https://leetcode.com/problems/partition-equal-subset-sum/discuss/2279474/Python-DP-Solution | class Solution:
def canPartition(self, nums: List[int]) -> bool:
dp = {}
def r(index, target):
if index == 0:
return target == nums[0]
if target == 0:
return True
if (index, target) in dp:
return dp[(index, target)]
... | partition-equal-subset-sum | Python DP Solution | DietCoke777 | 0 | 39 | partition equal subset sum | 416 | 0.466 | Medium | 7,412 |
https://leetcode.com/problems/partition-equal-subset-sum/discuss/2152308/Python(DP%2BCache-Solution) | class Solution:
def canPartition(self, nums: List[int]) -> bool:
target = sum(nums)//2
if sum(nums)%2==1:
return False
def dfs(target,start):
if target==0:
return True
if target<0:
return False
if target in dp:
... | partition-equal-subset-sum | Python(DP+Cache Solution) | sethaman1112 | 0 | 50 | partition equal subset sum | 416 | 0.466 | Medium | 7,413 |
https://leetcode.com/problems/partition-equal-subset-sum/discuss/2100378/python-or-dp-or-example | class Solution:
def canPartition(self, nums: List[int]) -> bool:
total = sum(nums)
if total & 1:
return False
target = total >> 1
# print([i for i in range(target + 1)])
# print()
dp = [[0 for _ in range(target + 1)] for _ in range(len(nums) + 1)]
... | partition-equal-subset-sum | python | dp | example | yzhao156 | 0 | 30 | partition equal subset sum | 416 | 0.466 | Medium | 7,414 |
https://leetcode.com/problems/partition-equal-subset-sum/discuss/2086646/Python-Solution | class Solution:
def canPartition(self, nums: List[int]) -> bool:
s=sum(nums)
if s%2==1:
return False
else:
s=s//2
n=len(nums)
dp =([[False for i in range(s+1)] for i in range(n + 1)])
for i in range(n+1):
for j in ra... | partition-equal-subset-sum | Python Solution | a_dityamishra | 0 | 71 | partition equal subset sum | 416 | 0.466 | Medium | 7,415 |
https://leetcode.com/problems/partition-equal-subset-sum/discuss/2076192/Python3-Very-Simple-Top-Down-Solution | class Solution:
from functools import cache
def canPartition(self, nums: List[int]) -> bool:
n, sum_ = len(nums), sum(nums)
if n == 1 or sum_ % 2 != 0:
return False
half_sum = sum_ / 2
@cache
def recurse(start: int, curr_sum: int) -> bool:
if curr... | partition-equal-subset-sum | Python3 Very Simple Top-Down Solution | martynas-subonis | 0 | 68 | partition equal subset sum | 416 | 0.466 | Medium | 7,416 |
https://leetcode.com/problems/partition-equal-subset-sum/discuss/2068021/Using-dp-learned-from-NeetCode | class Solution:
def canPartition(self, nums: List[int]) -> bool:
res = sum(nums)
if res % 2:
return False
res /= 2
t1 = set([0])
for n in nums:
if res in t1:
return True
t2 = set(t + n for t in t1)... | partition-equal-subset-sum | Using dp learned from NeetCode | andrewnerdimo | 0 | 68 | partition equal subset sum | 416 | 0.466 | Medium | 7,417 |
https://leetcode.com/problems/partition-equal-subset-sum/discuss/2019151/Python3-Solution-or-Dynamic-Programming | class Solution:
def canPartition(self, nums: List[int]) -> bool:
if sum(nums) & 1:
return False
target = sum(nums) >> 1
dp = [True] + [False] * target
for i in nums:
for j in range(target , i - 1, -1):
dp[j] |= dp[j-i]
return dp[tar... | partition-equal-subset-sum | Python3 Solution | Dynamic Programming | satyam2001 | 0 | 103 | partition equal subset sum | 416 | 0.466 | Medium | 7,418 |
https://leetcode.com/problems/partition-equal-subset-sum/discuss/2000757/Simple-easy-to-understand-and-a-clean-code-in-python | class Solution:
def canPartition(self, nums: List[int]) -> bool:
if sum(nums)%2!=0:
return False
target=sum(nums)//2
@lru_cache(None)
def rec(i,tsum):
if tsum==target:
return True
if i==len(nums):
return Fal... | partition-equal-subset-sum | Simple, easy to understand and a clean code in python | pbhuvaneshwar | 0 | 107 | partition equal subset sum | 416 | 0.466 | Medium | 7,419 |
https://leetcode.com/problems/partition-equal-subset-sum/discuss/1700120/Python-or-Recursion | class Solution:
def canPartition(self, nums: List[int]) -> bool:
sm=sum(nums)
if sm%2==1:
return False
tomake=sm//2
nums.sort()
@lru_cache(None)
def dfs(tomake,start):
if tomake==0:
return True
for i in range(start,l... | partition-equal-subset-sum | Python | Recursion | heckt27 | 0 | 55 | partition equal subset sum | 416 | 0.466 | Medium | 7,420 |
https://leetcode.com/problems/partition-equal-subset-sum/discuss/1625345/Python3-DP-solution | class Solution:
def canPartition(self, nums: List[int]) -> bool:
_sum = sum(nums)
if _sum % 2 != 0:
return False
target = _sum // 2
dp = [[False for _ in range(target + 1)] for _ in range(len(nums) + 1)]
dp[0][0] = True
... | partition-equal-subset-sum | [Python3] DP solution | maosipov11 | 0 | 37 | partition equal subset sum | 416 | 0.466 | Medium | 7,421 |
https://leetcode.com/problems/partition-equal-subset-sum/discuss/1624455/Python3-or-Solution-or-Works-or-Alts-tried-it-and-it-worked-or | class Solution:
def canPartition(self, nums: List[int]) -> bool:
r = sum(nums)
if r%2 != 0:
return False
f = {}
return self.canSum(nums, r/2, f)
def canSum(self, nums, ew, f):
if ew in f: return f[ew]
if ew < 0: return False
if ew == 0: return True
... | partition-equal-subset-sum | 📌 Python3 | Solution | Works | Alts tried it and it worked | | underplaceomg | 0 | 80 | partition equal subset sum | 416 | 0.466 | Medium | 7,422 |
https://leetcode.com/problems/partition-equal-subset-sum/discuss/1624341/Python-Solution | class Solution:
def canPartition(self, nums: List[int]) -> bool:
nums.sort(key=lambda x: -x)
e = len(nums)
r = sum(nums)
if r % 2 == 1:
return False
re = r // 2
if nums[0] > re:
return False
def dp(total, mask):
if total > re:... | partition-equal-subset-sum | Python Solution | underplaceomg | 0 | 74 | partition equal subset sum | 416 | 0.466 | Medium | 7,423 |
https://leetcode.com/problems/partition-equal-subset-sum/discuss/1502793/Python-DP-Memoization-Simple-Solution | class Solution:
def canPartition(self, nums: List[int]) -> bool:
tot = sum(nums)
N = len(nums)
if tot % 2 == 1: return False
memo = {}
def backtrack(cumsum=0, pos=0):
if pos >= N:
return False
... | partition-equal-subset-sum | Python DP Memoization Simple Solution | vharigovind9 | 0 | 217 | partition equal subset sum | 416 | 0.466 | Medium | 7,424 |
https://leetcode.com/problems/partition-equal-subset-sum/discuss/1454567/PyPy3-Solution-using-recursion-%2B-memoization-w-comments | class Solution:
def canPartition(self, nums: List[int]) -> bool:
"""
# NOTE: Time limit execeeds for this function.
# Solution using recursion
def subsetSum(a: List[int], target: int) -> bool:
def recursiveSubsetSum(n, w):
if n==0 and w=... | partition-equal-subset-sum | [Py/Py3] Solution using recursion + memoization w/ comments | ssshukla26 | 0 | 121 | partition equal subset sum | 416 | 0.466 | Medium | 7,425 |
https://leetcode.com/problems/partition-equal-subset-sum/discuss/1190653/Python-Recursive-and-Tabulation-DP-Solution | class Solution:
def canPartition(self, nums: List[int]) -> bool:
n = len(nums)
sumArr = sum(nums)
if sumArr%2 != 0:
return False
#return self.partition(nums,n,sumArr//2)
return self.dpApproach(nums,n,sumArr//2)
def recursive(self,nums,n,... | partition-equal-subset-sum | Python - Recursive and Tabulation DP Solution | tgoel219 | 0 | 98 | partition equal subset sum | 416 | 0.466 | Medium | 7,426 |
https://leetcode.com/problems/partition-equal-subset-sum/discuss/951162/Python3-Beats-90-in-Time-and-70-in-Space | class Solution:
def canPartition(self, nums: List[int]) -> bool:
summation = sum(nums)
if summation % 2:
# if odd sum, return False
return False
nums = sorted(nums)
def selection(ls, target):
"""
ls is sorted.
This function is to select elements from the list whose sum is the target.
It ... | partition-equal-subset-sum | [Python3] Beats 90% in Time and 70% in Space | leonine9 | 0 | 59 | partition equal subset sum | 416 | 0.466 | Medium | 7,427 |
https://leetcode.com/problems/partition-equal-subset-sum/discuss/950616/python-dp-solution | class Solution:
def canPartition(self, nums: List[int]) -> bool:
if sum(nums)%2 != 0:
return False
target = (sum(nums)//2)
dp = [0] * (target*2)
dp[0] = 1
for item in nums:
if item > sum(nums)//2:
return False
if item == sum... | partition-equal-subset-sum | python dp solution | yingziqing123 | 0 | 295 | partition equal subset sum | 416 | 0.466 | Medium | 7,428 |
https://leetcode.com/problems/partition-equal-subset-sum/discuss/487165/Python3-three-different-solutions-with-time-limit-test-case | class Solution:
def canPartition(self, nums: List[int]) -> bool:
s = sum(nums)
if s%2!=0: return False
ht = {0}
for num in nums:
ht.update({sum + num for sum in ht})
return s//2 in ht
def canPartitionDP(self, nums: List[int]) -> bool:
... | partition-equal-subset-sum | Python3 three different solutions with time limit test case | jb07 | 0 | 225 | partition equal subset sum | 416 | 0.466 | Medium | 7,429 |
https://leetcode.com/problems/partition-equal-subset-sum/discuss/1564627/Python-2-way-of-solution-with-same-time-complexity-but-different-space-complexity-using-DP | class Solution:
def canPartition(self, nums: List[int]) -> bool:
total = sum(nums)
limit = total//2
if total%2==1: return False
dp = [[0 for j in range(limit+1)] for i in range(len(nums))]
for i in range(len(nums)):
for j in range(1, limit+1):
dp[i... | partition-equal-subset-sum | Python - 2 way of solution with same time complexity, but different space complexity using DP | abrarjahin | -1 | 205 | partition equal subset sum | 416 | 0.466 | Medium | 7,430 |
https://leetcode.com/problems/partition-equal-subset-sum/discuss/1564627/Python-2-way-of-solution-with-same-time-complexity-but-different-space-complexity-using-DP | class Solution:
def canPartition(self, nums: List[int]) -> bool:
total = sum(nums)
if total % 2 != 0: return False
target = total // 2
dp = [False]*(target + 1)
dp[0] = True
for num in nums:
for i in range(target, num-1, -1):
dp[i] = dp[i]... | partition-equal-subset-sum | Python - 2 way of solution with same time complexity, but different space complexity using DP | abrarjahin | -1 | 205 | partition equal subset sum | 416 | 0.466 | Medium | 7,431 |
https://leetcode.com/problems/pacific-atlantic-water-flow/discuss/2507252/PYTHON-oror-EXPLAINED-oror | class Solution:
def pacificAtlantic(self, ht: List[List[int]]) -> List[List[int]]:
def pac(i,j):
if rp[i][j]:
return True
k=False
h=ht[i][j]
ht[i][j]=100001
if ht[i-1][j]<=h:
k=k or pac(i-1,j)
... | pacific-atlantic-water-flow | 🥇 PYTHON || EXPLAINED || ; ] | karan_8082 | 16 | 1,900 | pacific atlantic water flow | 417 | 0.541 | Medium | 7,432 |
https://leetcode.com/problems/pacific-atlantic-water-flow/discuss/882548/Python-3-or-BFS-Set-Intersection-or-Explanation | class Solution:
def pacificAtlantic(self, matrix: List[List[int]]) -> List[List[int]]:
if not matrix: return []
m, n = len(matrix), len(matrix[0])
pacific = [(0, i) for i in range(n)] + [(i, 0) for i in range(1, m)]
atlantic = [(m-1, i) for i in range(n)] + [(i, n-1) for i in range(m... | pacific-atlantic-water-flow | Python 3 | BFS, Set Intersection | Explanation | idontknoooo | 12 | 1,600 | pacific atlantic water flow | 417 | 0.541 | Medium | 7,433 |
https://leetcode.com/problems/pacific-atlantic-water-flow/discuss/2508533/Python-Elegant-and-Short-or-DFS-or-99.21-faster | class Solution:
"""
Time: O(n*m)
Memory: O(n*m)
"""
MOVES = [(-1, 0), (0, -1), (1, 0), (0, 1)]
def pacificAtlantic(self, heights: List[List[int]]) -> Set[Tuple[int, int]]:
def dfs(i: int, j: int, visited: set):
visited.add((i, j))
for di, dj in self.MOVES:
x, y = i + di, j + dj
if 0 <= x < n a... | pacific-atlantic-water-flow | Python Elegant & Short | DFS | 99.21% faster | Kyrylo-Ktl | 10 | 690 | pacific atlantic water flow | 417 | 0.541 | Medium | 7,434 |
https://leetcode.com/problems/pacific-atlantic-water-flow/discuss/954799/DFS-Python3-easy-to-understand-with-comments. | class Solution:
def pacificAtlantic(self, matrix: List[List[int]]) -> List[List[int]]:
if not matrix or not matrix[0]:
return []
# list which will have both the coordinates
pacific = set()
atlantic = set()
# get the number of rows and columns
m,n ... | pacific-atlantic-water-flow | DFS Python3, easy to understand with comments. | rotikapdamakan | 10 | 554 | pacific atlantic water flow | 417 | 0.541 | Medium | 7,435 |
https://leetcode.com/problems/pacific-atlantic-water-flow/discuss/1581227/Clean-python-code-O(mn)-DFS | class Solution:
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
m, n = len(heights), len(heights[0])
v_pac = set()
v_atl = set()
def dfs(v_set, row, col, curr_height):
if row < 0 or row >= m or \
col < 0 or col >= n or \
... | pacific-atlantic-water-flow | Clean python code O(mn) - DFS | Arvindn | 6 | 911 | pacific atlantic water flow | 417 | 0.541 | Medium | 7,436 |
https://leetcode.com/problems/pacific-atlantic-water-flow/discuss/2321368/Python-DFS-Beats-99.13-with-full-working-explanation | class Solution:
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]: # Time: O(mn) and Space: O(mn)
rows, cols = len(heights), len(heights[0])
pac, atl = set(), set()
def dfs(r, c, visit, prevHeight): # current location, set of already visited tiles, the value of t... | pacific-atlantic-water-flow | Python [DFS / Beats 99.13%] with full working explanation | DanishKhanbx | 5 | 343 | pacific atlantic water flow | 417 | 0.541 | Medium | 7,437 |
https://leetcode.com/problems/pacific-atlantic-water-flow/discuss/2694838/Python-or-BFS | class Solution:
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
ROWS, COLS = len(heights), len(heights[0])
pacificQueue, atlanticQueue = deque(), deque()
for col in range(COLS):
pacificQueue.append((0, col, heights[0][col]))
a... | pacific-atlantic-water-flow | Python | BFS | seangohck | 3 | 149 | pacific atlantic water flow | 417 | 0.541 | Medium | 7,438 |
https://leetcode.com/problems/pacific-atlantic-water-flow/discuss/2694838/Python-or-BFS | class Solution:
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
ROWS, COLS = len(heights), len(heights[0])
def dfs(r, c, prev_height):
if r < 0 or c < 0 or r == ROWS or c == COLS or heights[r][c] < prev_height or (r, c) in reachable:
retur... | pacific-atlantic-water-flow | Python | BFS | seangohck | 3 | 149 | pacific atlantic water flow | 417 | 0.541 | Medium | 7,439 |
https://leetcode.com/problems/pacific-atlantic-water-flow/discuss/1203469/Python-BFS-Time-and-Space-O(M*N) | class Solution:
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
if not heights or not heights[0]:
return []
row, col = len(heights), len(heights[0])
pacific_queue = deque([(r, c) for r in range(row) for c in range(col) if (r == 0) or (c == 0)])
... | pacific-atlantic-water-flow | Python BFS - Time & Space O(M*N) | ChidinmaKO | 2 | 476 | pacific atlantic water flow | 417 | 0.541 | Medium | 7,440 |
https://leetcode.com/problems/pacific-atlantic-water-flow/discuss/2509465/python-solution-using-BFS | class Solution:
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
res2=set()
n,m=len(heights),len(heights[0])
def check(pair):
vis=set()
pacific=False
atlantic=False
qu=deque([pair])
moves=[(1,0),(0,1),(-1,0),(... | pacific-atlantic-water-flow | python solution using BFS | benon | 1 | 75 | pacific atlantic water flow | 417 | 0.541 | Medium | 7,441 |
https://leetcode.com/problems/pacific-atlantic-water-flow/discuss/2161314/simple-dfs-beats-98 | class Solution:
def pacificAtlantic(self, board: List[List[int]]) -> List[List[int]]:
def reachable(stack, visited):
while(stack):
node = stack.pop()
if node not in visited:
visited[node] = True
x, y = node
... | pacific-atlantic-water-flow | simple dfs beats 98% | gabhinav001 | 1 | 85 | pacific atlantic water flow | 417 | 0.541 | Medium | 7,442 |
https://leetcode.com/problems/pacific-atlantic-water-flow/discuss/2117317/python-3-oror-simple-dfs | class Solution:
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
m, n = len(heights), len(heights[0])
pac, atl = set(), set()
def dfs(i, j, visited, prevHeight):
if (i, j) in visited or heights[i][j] < prevHeight:
return
... | pacific-atlantic-water-flow | python 3 || simple dfs | dereky4 | 1 | 202 | pacific atlantic water flow | 417 | 0.541 | Medium | 7,443 |
https://leetcode.com/problems/pacific-atlantic-water-flow/discuss/1829555/Python-clean-dfs-solution | class Solution:
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
pac, atl = set(), set()
rows, cols = len(heights), len(heights[0])
def dfs(r, c, visited, prevHeight):
if r < 0 or r >= rows or c < 0 or c >= cols or (r,c) in visited or heights[r][c]... | pacific-atlantic-water-flow | Python clean dfs solution | johnnychang | 1 | 158 | pacific atlantic water flow | 417 | 0.541 | Medium | 7,444 |
https://leetcode.com/problems/pacific-atlantic-water-flow/discuss/1503405/Well-Coded-oror-Easy-to-Understand-oror-For-Beginner-(DP) | class Solution:
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
m,n = len(heights),len(heights[0])
pacific = [[-1 for _ in range(n)] for _ in range(m)]
atlantic= [[-1 for _ in range(n)] for _ in range(m)]
def isValid(i,j):
if 0<=i<m and 0<=j<n:
retur... | pacific-atlantic-water-flow | 📌📌 Well-Coded || Easy-to-Understand || For-Beginner (DP) 🐍 | abhi9Rai | 1 | 191 | pacific atlantic water flow | 417 | 0.541 | Medium | 7,445 |
https://leetcode.com/problems/pacific-atlantic-water-flow/discuss/838783/Python3-flood-fill | class Solution:
def pacificAtlantic(self, matrix: List[List[int]]) -> List[List[int]]:
if not matrix: return [] # edge case
m, n = len(matrix), len(matrix[0])
ans = [[0]*n for _ in matrix]
def dfs(i, j, v):
"""Flood fill matrix with given value."""
... | pacific-atlantic-water-flow | [Python3] flood fill | ye15 | 1 | 224 | pacific atlantic water flow | 417 | 0.541 | Medium | 7,446 |
https://leetcode.com/problems/pacific-atlantic-water-flow/discuss/2837489/Python-3-BFS-fast-and-very-simple-solution | class Solution:
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
def find_ocean_cells(arr):
visited, queue = set(), deque()
queue.extend(arr)
directions = [[-1, 0], [0, -1], [1, 0], [0, 1]]
while queue:
... | pacific-atlantic-water-flow | Python 3 - BFS - fast and very simple solution | noob_in_prog | 0 | 4 | pacific atlantic water flow | 417 | 0.541 | Medium | 7,447 |
https://leetcode.com/problems/pacific-atlantic-water-flow/discuss/2797686/Basic-DFS | class Solution:
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
#Pacific contains all the coordinates of Pacific ocean
#Atlantic contains all coordinates of Atlantic Ocean
pacific_ocean = set()
atlantic_ocean = set()
ans = []
#if pacific = fir... | pacific-atlantic-water-flow | Basic DFS | fellowshiptech | 0 | 9 | pacific atlantic water flow | 417 | 0.541 | Medium | 7,448 |
https://leetcode.com/problems/pacific-atlantic-water-flow/discuss/2738711/DFS-searching-backwards | class Solution:
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
ROWS, COLS = len(heights), len(heights[0])
pac, atl = set(), set()
def dfs(r, c, visited, prev_height):
# base case
if ((r, c) in visited or
r < 0 or c < 0 or r ==... | pacific-atlantic-water-flow | DFS searching backwards | meechos | 0 | 7 | pacific atlantic water flow | 417 | 0.541 | Medium | 7,449 |
https://leetcode.com/problems/pacific-atlantic-water-flow/discuss/2713300/Python-or-A-different-approach-from-most-or-Memory-efficient | class Solution:
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
if not heights or not heights[0]:
return []
nrows = len(heights)
ncols = len(heights[0])
visited = set()
def dfs(r,c,ocean):
nonlocal visited
... | pacific-atlantic-water-flow | Python | A different approach from most | Memory efficient | asangariu | 0 | 11 | pacific atlantic water flow | 417 | 0.541 | Medium | 7,450 |
https://leetcode.com/problems/pacific-atlantic-water-flow/discuss/2673685/Easy-to-Understand | class Solution:
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
m = len(heights)
n = len(heights[0])
def getEdges(i, j):
possibleEdges = [(i-1,j),(i, j+1), (i+1,j), (i, j-1)]
edges = []
for e in possibleEdges:
x = e... | pacific-atlantic-water-flow | Easy to Understand | lalit84 | 0 | 6 | pacific atlantic water flow | 417 | 0.541 | Medium | 7,451 |
https://leetcode.com/problems/pacific-atlantic-water-flow/discuss/2509857/Python-Solution-or-DFS | class Solution:
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
n=len(heights)
m=len(heights[0])
if n==1 and m==1 and heights[n-1][m-1]>=1:
return [[0,0]]
ans=[]
rVector=[-1, 0, 1, 0]
cVector=[0, ... | pacific-atlantic-water-flow | Python Solution | DFS | Siddharth_singh | 0 | 28 | pacific atlantic water flow | 417 | 0.541 | Medium | 7,452 |
https://leetcode.com/problems/pacific-atlantic-water-flow/discuss/2508805/288-ms-faster-than-94.48-of-Python3 | class Solution:
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
pac, atl = set(), set()
rows, cols = len(heights), len(heights[0])
def dfs(r, c, visited, prevHeight):
if (r, c) in visited or r < 0 or r == rows or c < 0 or c == cols or prevHeight > heights[r][c]:
return
visite... | pacific-atlantic-water-flow | 288 ms, faster than 94.48% of Python3 | sagarhasan273 | 0 | 28 | pacific atlantic water flow | 417 | 0.541 | Medium | 7,453 |
https://leetcode.com/problems/pacific-atlantic-water-flow/discuss/2508740/Python-DFS-from-Water-Verbose-but-readable-solution | class Solution:
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
# we could start from every outside cell (every ocean) and keep track
# of cells that are reachable from this cell
# once we did that for both oceans, we can intersect these sets and
# ha... | pacific-atlantic-water-flow | [Python] - DFS from Water - Verbose but readable solution | Lucew | 0 | 17 | pacific atlantic water flow | 417 | 0.541 | Medium | 7,454 |
https://leetcode.com/problems/pacific-atlantic-water-flow/discuss/2508351/Easy-to-follow-python3-solution-with-complexity-analysis | class Solution:
# O(n) time,
# O(n) space,
# Approach: BFS, reverse thinking, hashset, matrix
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
n = len(heights)
m = len(heights[0])
def bfs(qu: Deque) -> Set[int]:
vstd = set()
... | pacific-atlantic-water-flow | Easy to follow python3 solution with complexity analysis | destifo | 0 | 7 | pacific atlantic water flow | 417 | 0.541 | Medium | 7,455 |
https://leetcode.com/problems/pacific-atlantic-water-flow/discuss/2508088/GolangPython-O(N*M)-time-or-O(N*M)-space | class Solution:
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
pacific = set()
for i in range(len(heights[0])):
dfs(len(heights)-1,i,-1,pacific,heights)
for i in range(len(heights)):
dfs(i,len(heights[0])-1,-1,pacific,heights)
... | pacific-atlantic-water-flow | Golang/Python O(N*M) time | O(N*M) space | vtalantsev | 0 | 42 | pacific atlantic water flow | 417 | 0.541 | Medium | 7,456 |
https://leetcode.com/problems/pacific-atlantic-water-flow/discuss/2507781/Python-DFS | class Solution:
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
m = len(heights)
n = len(heights[0])
pacific_reachable = set()
atlantic_reachable = set()
def dfs(x, y, reachable):
reachable.add((x, y))
dirs = [(1, 0)... | pacific-atlantic-water-flow | Python DFS | rylim | 0 | 20 | pacific atlantic water flow | 417 | 0.541 | Medium | 7,457 |
https://leetcode.com/problems/pacific-atlantic-water-flow/discuss/2507372/Python-DFS-from-borders | class Solution:
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
def dfs(r, c, seen):
seen.add((r, c))
h = heights[r][c]
for nr, nc in [(r-1, c), (r+1, c), (r, c-1), (r, c+1)]:
if (0 <= nr < m and 0 <= nc < n and
... | pacific-atlantic-water-flow | Python, DFS from borders | blue_sky5 | 0 | 52 | pacific atlantic water flow | 417 | 0.541 | Medium | 7,458 |
https://leetcode.com/problems/pacific-atlantic-water-flow/discuss/2450777/Simple-DFS-solution | class Solution:
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
rows , cols = len(heights), len(heights[0])
pac , atl = set(), set()
def dfs(r, c , visit, preHeight):
if (r,c) in visit or r<0 or c< 0 or r>= rows or c>= cols or heights[r][c] < preH... | pacific-atlantic-water-flow | Simple DFS solution | Abhi_009 | 0 | 101 | pacific atlantic water flow | 417 | 0.541 | Medium | 7,459 |
https://leetcode.com/problems/pacific-atlantic-water-flow/discuss/2242774/Python3-Efficient-Solution-oror-Iterative-DFS-and-BFS-Hashset-oror-TC-SC%3A-O(m-x-n)-oror-Clearly-Explained | class Solution:
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
num_row = len(heights)
num_col = len(heights[0])
pac = set()
atl = set()
def dfs(row, col, sea, height):
"""DFS traversal to mark which "land" is efficient for the wat... | pacific-atlantic-water-flow | Python3 Efficient Solution || Iterative DFS & BFS, Hashset || TC, SC: O(m x n) || Clearly Explained | minheapolis | 0 | 78 | pacific atlantic water flow | 417 | 0.541 | Medium | 7,460 |
https://leetcode.com/problems/pacific-atlantic-water-flow/discuss/1919467/Python-oror-Solution | class Solution:
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
def calculate(x,i,j):
if 0<=i<len(heights) and 0<=j<len(heights[0]) and x[i][j]==0:
x[i][j] = 1
for r,c in [(-1,0),(0,-1),(1,0),(0,1)]:
... | pacific-atlantic-water-flow | Python || Solution | Brillianttyagi | 0 | 98 | pacific atlantic water flow | 417 | 0.541 | Medium | 7,461 |
https://leetcode.com/problems/pacific-atlantic-water-flow/discuss/1850976/Python3-save-space-with-bit-operation | class Solution:
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
m = len(heights)
n = len(heights[0])
#result is a 2D grid that keeps track of both whether the cell can reach Atlantic
# and whether the cell can reach Pacific. The first bit is used t... | pacific-atlantic-water-flow | Python3 save space with bit operation | Yihang-- | 0 | 30 | pacific atlantic water flow | 417 | 0.541 | Medium | 7,462 |
https://leetcode.com/problems/pacific-atlantic-water-flow/discuss/1773672/Python-BFS-using-bitmask | class Solution:
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
def bfs(q, ocean):
while q:
r, c = q.popleft()
for dr, dc in [(-1, 0), (1, 0), (0, 1), (0, -1)]:
nr, nc = r + dr, c + dc
... | pacific-atlantic-water-flow | Python BFS using bitmask | blue_sky5 | 0 | 49 | pacific atlantic water flow | 417 | 0.541 | Medium | 7,463 |
https://leetcode.com/problems/pacific-atlantic-water-flow/discuss/1755572/Python-DFS | class Solution:
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
def dfs(r, c, h, o):
if r < 0 or c < 0 or r == m or c == n:
return
if values[r][c] & o:
return
if heights[r][c] < h:
... | pacific-atlantic-water-flow | Python, DFS | blue_sky5 | 0 | 233 | pacific atlantic water flow | 417 | 0.541 | Medium | 7,464 |
https://leetcode.com/problems/pacific-atlantic-water-flow/discuss/1532121/Py3Py-Solution-DFS-for-both-sides-w-comments | class Solution:
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
# Init
rows = len(heights)
cols = len(heights[0])
pacific = set()
atlantic = set()
# Helper functions
def inLimits(node):
return (0 <= node[0] ... | pacific-atlantic-water-flow | [Py3/Py] Solution DFS for both sides w/ comments | ssshukla26 | 0 | 110 | pacific atlantic water flow | 417 | 0.541 | Medium | 7,465 |
https://leetcode.com/problems/pacific-atlantic-water-flow/discuss/1466050/WEEB-DOES-PYTHON-BFS | class Solution:
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
row, col = len(heights), len(heights[0])
pacific = deque([(0,i) for i in range(col)] + [(i,0) for i in range(row)])
atlantic = deque([(row-1,i) for i in range(col)] + [(i,col-1) for i in range(row)])
return self.bfs(heigh... | pacific-atlantic-water-flow | WEEB DOES PYTHON BFS | Skywalker5423 | 0 | 102 | pacific atlantic water flow | 417 | 0.541 | Medium | 7,466 |
https://leetcode.com/problems/pacific-atlantic-water-flow/discuss/1449929/Python-Clean-DFS-and-BFS | class Solution:
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
def dfs(r, c, vis):
queue = deque([(r, c, vis)])
while queue:
r, c, vis = queue.pop()
for nr, nc in [(r-1, c), (r, c-1), (r, c+1), (r+1, c)]:
... | pacific-atlantic-water-flow | [Python] Clean DFS & BFS | soma28 | 0 | 220 | pacific atlantic water flow | 417 | 0.541 | Medium | 7,467 |
https://leetcode.com/problems/pacific-atlantic-water-flow/discuss/1449929/Python-Clean-DFS-and-BFS | class Solution:
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
def bfs(r, c, vis):
queue = deque([(r, c, vis)])
while queue:
r, c, vis = queue.popleft()
for nr, nc in [(r-1, c), (r, c-1), (r, c+1), (r+1, c)]:
... | pacific-atlantic-water-flow | [Python] Clean DFS & BFS | soma28 | 0 | 220 | pacific atlantic water flow | 417 | 0.541 | Medium | 7,468 |
https://leetcode.com/problems/pacific-atlantic-water-flow/discuss/1126721/python3-DFS | class Solution:
def pacificAtlantic(self, matrix: List[List[int]]) -> Set[Tuple[int, int]]:
if not matrix:
return set()
m, n = len(matrix), len(matrix[0])
def dfs(r: int, c: int, res: MutableSet[Tuple[int, int]]) -> None:
res.add((r, c))
for nr, nc in {(r... | pacific-atlantic-water-flow | python3 DFS | mcolen | 0 | 131 | pacific atlantic water flow | 417 | 0.541 | Medium | 7,469 |
https://leetcode.com/problems/pacific-atlantic-water-flow/discuss/1807361/Python-3-DFS-From-Edge-to-Rest-of-Land | class Solution:
def flow(self, q: List[List[int]], canFlowFrom: List[List[bool]], heights: List[List[int]]):
# directions that we can water can flow from
directions = [[0, 1], [1, 0], [0, -1], [-1, 0]]
while len(q) != 0:
inds = q.pop(0)
for direction in directions:
... | pacific-atlantic-water-flow | Python 3 DFS From Edge to Rest of Land | 9D76 | -1 | 69 | pacific atlantic water flow | 417 | 0.541 | Medium | 7,470 |
https://leetcode.com/problems/pacific-atlantic-water-flow/discuss/1807361/Python-3-DFS-From-Edge-to-Rest-of-Land | class Solution:
def flow(self, q: List[List[int]], canFlowFrom: List[List[bool]], heights: List[List[int]]):
# directions that we can water can flow from
directions = [[0, 1], [1, 0], [0, -1], [-1, 0]]
while len(q) != 0:
inds = q.pop(0)
for direction in directions:
... | pacific-atlantic-water-flow | Python 3 DFS From Edge to Rest of Land | 9D76 | -1 | 69 | pacific atlantic water flow | 417 | 0.541 | Medium | 7,471 |
https://leetcode.com/problems/battleships-in-a-board/discuss/1523048/Simple-Easy-to-Understand-Python-O(1)-extra-memory | class Solution:
def countBattleships(self, board: List[List[str]]) -> int:
count = 0
for r in range(len(board)):
for c in range(len(board[0])):
if board[r][c] == 'X':
var = 1
if (r > 0 and board[r-1][c] == 'X') or (c > 0 and board[r... | battleships-in-a-board | Simple Easy to Understand Python O(1) extra memory | bshien | 9 | 638 | battleships in a board | 419 | 0.747 | Medium | 7,472 |
https://leetcode.com/problems/battleships-in-a-board/discuss/2523176/Python-or-O(NM)-one-liner-Solution-without-modifying-board | class Solution:
def countBattleships(self, board: List[List[str]]) -> int:
count = 0
for i, row in enumerate(board):
for j, cell in enumerate(row):
if cell == "X":
if (i == 0 or board[i - 1][j] == ".") and\
(j == 0 or bo... | battleships-in-a-board | Python | O(NM) one-liner Solution without modifying board | ahmadheshamzaki | 8 | 471 | battleships in a board | 419 | 0.747 | Medium | 7,473 |
https://leetcode.com/problems/battleships-in-a-board/discuss/2523176/Python-or-O(NM)-one-liner-Solution-without-modifying-board | class Solution:
def countBattleships(self, board: List[List[str]]) -> int:
return sum(1 for i, row in enumerate(board) for j, cell in enumerate(row) if cell == "X" and (i == 0 or board[i - 1][j] == ".") and (j == 0 or board[i][j - 1] == ".")) | battleships-in-a-board | Python | O(NM) one-liner Solution without modifying board | ahmadheshamzaki | 8 | 471 | battleships in a board | 419 | 0.747 | Medium | 7,474 |
https://leetcode.com/problems/battleships-in-a-board/discuss/461076/Python-3-(beats-~100)-(one-pass)-(-O(1)-space-)-(60-ms) | class Solution:
def countBattleships(self, B: List[List[str]]) -> int:
if not B[0]: return 0
M, N, t = len(B), len(B[0]), int(B[0][0] == 'X')
for i in range(1,M): t += (B[i-1][0],B[i][0]) == ('.','X')
for j in range(1,N): t += (B[0][j-1],B[0][j]) == ('.','X')
for i,j in itert... | battleships-in-a-board | Python 3 (beats ~100%) (one pass) ( O(1) space ) (60 ms) | junaidmansuri | 6 | 1,300 | battleships in a board | 419 | 0.747 | Medium | 7,475 |
https://leetcode.com/problems/battleships-in-a-board/discuss/838599/Python3-dfs | class Solution:
def countBattleships(self, board: List[List[str]]) -> int:
m, n = len(board), len(board[0])
def dfs(i, j):
"""Traverse the grids connected to (i, j)."""
board[i][j] = "." # mark as visited
for ii, jj in (i-1, j), (i, j-1), (i, j+1), (i+1... | battleships-in-a-board | [Python3] dfs | ye15 | 2 | 210 | battleships in a board | 419 | 0.747 | Medium | 7,476 |
https://leetcode.com/problems/battleships-in-a-board/discuss/838599/Python3-dfs | class Solution:
def countBattleships(self, board: List[List[str]]) -> int:
m, n = len(board), len(board[0])
ans = 0
for i in range(m):
for j in range(n):
if board[i][j] == 'X' and (i == 0 or board[i-1][j] == '.') and (j == 0 or board[i][j-1] == '.'):
... | battleships-in-a-board | [Python3] dfs | ye15 | 2 | 210 | battleships in a board | 419 | 0.747 | Medium | 7,477 |
https://leetcode.com/problems/battleships-in-a-board/discuss/1906684/Clean-intuitive-Python-O(1)-space-solution-explained | class Solution:
def countBattleships(self, board: List[List[str]]) -> int:
nrows = len(board)
ncols = len(board[0])
count = 0
for row, col in product(range(nrows), range(ncols)):
if board[row][col] == 'X':
if (row == nrows-1 or board[row+1][col] =... | battleships-in-a-board | Clean, intuitive Python, O(1) space, solution explained | boris17 | 1 | 114 | battleships in a board | 419 | 0.747 | Medium | 7,478 |
https://leetcode.com/problems/battleships-in-a-board/discuss/755605/Simple-Python-Recursive-DFS-beats-86-and-Iterative-BFS-with-Comments! | class Solution:
def countBattleships(self, board: List[List[str]]) -> int:
if not board: return 0
rows = len(board)
cols = len(board[0])
def helper(row, col):
# Make sure the point we're searching is valid if not return
if row >= rows or row < 0 or... | battleships-in-a-board | Simple Python Recursive DFS beats 86% and Iterative BFS with Comments! | Pythagoras_the_3rd | 1 | 239 | battleships in a board | 419 | 0.747 | Medium | 7,479 |
https://leetcode.com/problems/battleships-in-a-board/discuss/755605/Simple-Python-Recursive-DFS-beats-86-and-Iterative-BFS-with-Comments! | class Solution:
def countBattleships(self, board: List[List[str]]) -> int:
if not board: return 0
rows = len(board)
cols = len(board[0])
directions = [(1, 0), (-1, 0), (0, 1), (0, -1)]
res = 0
# Iterate through our board looking for ships 'X'.
... | battleships-in-a-board | Simple Python Recursive DFS beats 86% and Iterative BFS with Comments! | Pythagoras_the_3rd | 1 | 239 | battleships in a board | 419 | 0.747 | Medium | 7,480 |
https://leetcode.com/problems/battleships-in-a-board/discuss/2358712/You-don't-need-to-move-in-Four-Directions.-Just-Two-Directional-Movements. | class Solution:
def countBattleships(self, board: List[List[str]]) -> int:
def isBound(row,col):
return 0<=row<len(board) and 0<=col<len(board[0])
DIR=[(1,0),(0,1)]
@lru_cache(None)
def dfs(row,col):
board[row][col]="."
for neighbor in DIR... | battleships-in-a-board | You don't need to move in Four Directions. Just Two Directional Movements. | Sefinehtesfa34 | 0 | 41 | battleships in a board | 419 | 0.747 | Medium | 7,481 |
https://leetcode.com/problems/battleships-in-a-board/discuss/1925260/Python3-DFS-solution-(with-comments) | class Solution:
def countBattleships(self, board: List[List[str]]) -> int:
pairs = []
#puts coords of X's in a list
for i,j in enumerate(board):
if 'X' in j:
for x, y in enumerate(j):
if y == 'X':
pairs.append((... | battleships-in-a-board | Python3 DFS solution (with comments) | ssonzeu | 0 | 73 | battleships in a board | 419 | 0.747 | Medium | 7,482 |
https://leetcode.com/problems/battleships-in-a-board/discuss/1912334/Python-Solution-(DFS) | class Solution:
def countBattleships(self, board: List[List[str]]) -> int:
rowlen = len(board)
collen = len(board[0])
def dfs(row,col,d):
board[row][col] = "#"
if (d == "all" or d == "U") and row-1 >= 0 and row-1 < rowlen and col >= 0 and col < collen and bo... | battleships-in-a-board | Python Solution (DFS) | DietCoke777 | 0 | 70 | battleships in a board | 419 | 0.747 | Medium | 7,483 |
https://leetcode.com/problems/battleships-in-a-board/discuss/1762461/Python-easy-to-understand-or-DFS | class Solution:
def dfs(self, board, row, col):
if row < 0 or col < 0 or row == len(board) or col == len(board[0]) or board[row][col] != 'X':
return
board[row][col] = 'O'
self.dfs(board, row+1, col)
self.dfs(board, row, col+1)
def countBattleships(self, board: Li... | battleships-in-a-board | Python easy to understand | DFS | sanial2001 | 0 | 150 | battleships in a board | 419 | 0.747 | Medium | 7,484 |
https://leetcode.com/problems/battleships-in-a-board/discuss/1344826/WEEB-DOES-PYTHON | class Solution:
def countBattleships(self, board: List[List[str]]) -> int:
row, col = len(board), len(board[0])
queue = deque([])
count = 0
for x in range(row):
for y in range(col):
if board[x][y] == "X":
queue.append((x,y))
self.bfs(queue, row, col, board)
count += 1
return count
d... | battleships-in-a-board | WEEB DOES PYTHON | Skywalker5423 | 0 | 95 | battleships in a board | 419 | 0.747 | Medium | 7,485 |
https://leetcode.com/problems/battleships-in-a-board/discuss/640111/Intuitive-approach-by-looking-for-the-head-of-battleships | class Solution:
def countBattleships(self, board: List[List[str]]) -> int:
# 0) Initialization
count_of_bs = 0
''' count of battle ship'''
def is_head(r, c):
# Check if the given position is the head of battleships
if (r == 0 or board[... | battleships-in-a-board | Intuitive approach by looking for the head of battleships | puremonkey2001 | 0 | 111 | battleships in a board | 419 | 0.747 | Medium | 7,486 |
https://leetcode.com/problems/battleships-in-a-board/discuss/340116/Python-solution-using-dictionary | class Solution:
def countBattleships(self, board: List[List[str]]) -> int:
count=0
d={}
for i in range(len(board)):
for j in range(len(board[i])):
if board[i][j]=='X':d[(i,j)]=1
l=list(d.keys())
while d:
k=l[0]
del[d[k]]
... | battleships-in-a-board | Python solution using dictionary | ketan35 | 0 | 222 | battleships in a board | 419 | 0.747 | Medium | 7,487 |
https://leetcode.com/problems/battleships-in-a-board/discuss/1022691/Python3%3A-DFS-(Same-solution-can-be-used-to-solve-Number-of-Islands) | class Solution:
def countBattleships(self, board: List[List[str]]) -> int:
if not board:
return 0
counter = 0
for i in range(len(board)):
for j in range(len(board[0])):
if board[i][j] == "X":
self.dfs(board, i, j)
... | battleships-in-a-board | Python3: DFS (Same solution can be used to solve Number of Islands) | MakeTeaNotWar | -1 | 171 | battleships in a board | 419 | 0.747 | Medium | 7,488 |
https://leetcode.com/problems/strong-password-checker/discuss/2345991/Runtime%3A-23-ms-or-Memory-Usage%3A-13.9-MB-or-python3 | class Solution:
def strongPasswordChecker(self, password: str) -> int:
#vimla_kushwaha
s = password
missing_type = 3
if any('a' <= c <= 'z' for c in s): missing_type -= 1
if any('A' <= c <= 'Z' for c in s): missing_type -= 1
if any(c.isdigit() for c in s): missing_typ... | strong-password-checker | Runtime: 23 ms | Memory Usage: 13.9 MB | python3 | vimla_kushwaha | 2 | 309 | strong password checker | 420 | 0.143 | Hard | 7,489 |
https://leetcode.com/problems/strong-password-checker/discuss/2545499/Python3-greedy | class Solution:
def strongPasswordChecker(self, password: str) -> int:
digit = lower = upper = 1
for ch in password:
if ch.isdigit(): digit = 0
elif ch.islower(): lower = 0
elif ch.isupper(): upper = 0
missing = digit + lower + upper
reps = one... | strong-password-checker | [Python3] greedy | ye15 | 0 | 164 | strong password checker | 420 | 0.143 | Hard | 7,490 |
https://leetcode.com/problems/strong-password-checker/discuss/1066447/Python%3A-20ms-98.58-faster | class Solution:
def strongPasswordChecker(self, password: str) -> int:
lengthOfPassword=len(password)
#IF PASSWORD IS LESS THAN 2 CHARACTER
if lengthOfPassword<3:
return 6-lengthOfPassword
isLower=False
isUpper=False
isDigit=False
repeat... | strong-password-checker | Python: 20ms - 98.58 % faster | kshitijb | 0 | 745 | strong password checker | 420 | 0.143 | Hard | 7,491 |
https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/discuss/404504/Python-O(N)-Trie-Solution-wcomments-and-explanations | class Solution:
def findMaximumXOR(self, nums: List[int]) -> int:
# need to know the largest binary representation
# bin prepends '0b', ignore
L = len(bin(max(nums))) - 2
# preprocess step - left-pad zeros to ensure each number has L bits
# (x >> i) & 1 produces the bit at position i for number... | maximum-xor-of-two-numbers-in-an-array | Python O(N) Trie Solution w/comments and explanations | crippled_baby | 2 | 904 | maximum xor of two numbers in an array | 421 | 0.545 | Medium | 7,492 |
https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/discuss/850399/Python-(75-speed-98-memory)-No-Trie-Simple-Explained-O(n) | class Solution:
def findMaximumXOR(self, nums: List[int]) -> int:
def helper(group0=[], group1=[], div_bit=0):
if len(group0) + len(group1) <= 1: return 0
if len(group0) == 1 and len(group1) == 1: return group0[0] ^ group1[0]
if div_bit == -1 and group0 and group1: return... | maximum-xor-of-two-numbers-in-an-array | Python (75% speed, 98% memory) - No Trie - Simple, Explained - O(n) | perenially_curious | 1 | 128 | maximum xor of two numbers in an array | 421 | 0.545 | Medium | 7,493 |
https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/discuss/2164227/Simple-Logic-(Python) | class Solution:
def findMaximumXOR(self, nums: List[int]) -> int:
res = [0]
for i,j in combinations(nums, 2):
res.append(i^j)
return max(res) | maximum-xor-of-two-numbers-in-an-array | Simple Logic (Python) | writemeom | 0 | 129 | maximum xor of two numbers in an array | 421 | 0.545 | Medium | 7,494 |
https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/discuss/1723707/10-lines-of-code-with-explanation-by-comments-for-every-line-of-code | class Solution:
def findMaximumXOR(self, nums: List[int]) -> int:
mask, maximum = 0, 0
for i in range(31, -1, -1):
mask = mask | 1 << i
# finding elements having 1's between their msb to ith bit from the start
# eg: when i = 3 then mask is 111111111111111111111111... | maximum-xor-of-two-numbers-in-an-array | 10 lines of code with explanation by comments for every line of code | yashmishra0207 | 0 | 219 | maximum xor of two numbers in an array | 421 | 0.545 | Medium | 7,495 |
https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/discuss/1722795/Python-Simple-and-Clear-Python-Solution-Using-Bit-Masking-!! | class Solution:
def findMaximumXOR(self, nums: List[int]) -> int:
maxvalue = 0
mask = 0;
newset = set()
for i in range(30, -1, -1):
mask = mask | (1 << i)
newMaxvalue = maxvalue | (1 << i)
for i in range(len(nums)):
newset.add(nums[i] & mask)
for prefix in newset:
if (newMaxvalu... | maximum-xor-of-two-numbers-in-an-array | [ Python ] ✔✔ Simple and Clear Python Solution Using Bit Masking !! 🔥🔥✌ | ASHOK_KUMAR_MEGHVANSHI | 0 | 330 | maximum xor of two numbers in an array | 421 | 0.545 | Medium | 7,496 |
https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/discuss/839154/Python3-via-trie-O(N) | class Solution:
def findMaximumXOR(self, nums: List[int]) -> int:
ans = 0
trie = {}
for x in nums:
s = bin(x)[2:].zfill(32)
node = oppo = trie
for c in map(int, s):
node = node.setdefault(c, {}) # add to trie
oppo = oppo.... | maximum-xor-of-two-numbers-in-an-array | [Python3] via trie O(N) | ye15 | 0 | 257 | maximum xor of two numbers in an array | 421 | 0.545 | Medium | 7,497 |
https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/discuss/839154/Python3-via-trie-O(N) | class Solution:
def findMaximumXOR(self, nums: List[int]) -> int:
ans = 0
for i in range(32)[::-1]:
ans <<= 1
prefix = {num >> i for num in nums}
ans += any(ans^1^p in prefix for p in prefix)
return ans | maximum-xor-of-two-numbers-in-an-array | [Python3] via trie O(N) | ye15 | 0 | 257 | maximum xor of two numbers in an array | 421 | 0.545 | Medium | 7,498 |
https://leetcode.com/problems/reconstruct-original-digits-from-english/discuss/1556493/Python3-One-pass-solution | class Solution:
def originalDigits(self, s: str) -> str:
c = collections.Counter(s)
digit_count = [0] * 10
digit_count[0] = c['z']
digit_count[2] = c['w']
digit_count[4] = c['u']
digit_count[6] = c['x']
digit_count[8] = c['g']
digit_c... | reconstruct-original-digits-from-english | [Python3] One pass solution | maosipov11 | 2 | 361 | reconstruct original digits from english | 423 | 0.513 | Medium | 7,499 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.