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/rotting-oranges/discuss/2771844/Chinese-Explanation-%2B-Python
class Solution: def orangesRotting(self, grid: List[List[int]]) -> int: # bfs # 1. 定位rotten orange的坐标,并且save in visited and queue # 2. start bfs m,n = len(grid), len(grid[0]) queue = collections.deque() # locate rotten orange for i in range(m): ...
rotting-oranges
Chinese Explanation + Python
Michael_Songru
0
2
rotting oranges
994
0.525
Medium
16,200
https://leetcode.com/problems/rotting-oranges/discuss/2749731/Efficient-Python-BFS-with-comments
class Solution: ''' BFS approach ''' def orangesRotting(self, grid: List[List[int]]) -> int: queue = deque() minutes, fresh = 0, 0 # Populate the initial queue with coordinates of 2's # Count 1's as fresh for i, row in enumerate(grid): for j, v...
rotting-oranges
Efficient Python BFS with comments
decsery
0
12
rotting oranges
994
0.525
Medium
16,201
https://leetcode.com/problems/rotting-oranges/discuss/2735774/Python-Easy-Solution-Time%3A-O(n)-Space%3A-O(n)
class Solution: def orangesRotting(self, grid: List[List[int]]) -> int: # record rotten and fresh oranges rottens = [] freshs = [] for r in range(len(grid)): for c in range(len(grid[0])): if grid[r][c] == 1: freshs.append((r,c)) ...
rotting-oranges
Python Easy Solution Time: O(n) Space: O(n)
chienhsiang-hung
0
9
rotting oranges
994
0.525
Medium
16,202
https://leetcode.com/problems/rotting-oranges/discuss/2707117/Simple-python-oror-beats-99-memory
class Solution: def orangesRotting(self, grid: List[List[int]]) -> int: mins=0 # store result dirs = [(0,1),(1,0),(-1,0), (0,-1)] # movement directions rotten_set = set() total_r = 0 # total rotten in any minute total_o = 0 # total oranges m = len(grid) n = ...
rotting-oranges
Simple python || beats 99% memory
user1090g
0
6
rotting oranges
994
0.525
Medium
16,203
https://leetcode.com/problems/rotting-oranges/discuss/2687990/Simple-DFS-like-Solution-with-explanation
class Solution: def orangesRotting(self, grid: List[List[int]]) -> int: def Rotting(i, j): # function to make oranges rotten if i >= 0 and i < len(grid) and j >= 0 and j < len(grid[0]) and grid[i][j] == 1: grid[i][j] = 2 count = 0 prev = copy.deepcopy(grid) # us...
rotting-oranges
Simple DFS-like Solution with explanation
AustinHuang823
0
30
rotting oranges
994
0.525
Medium
16,204
https://leetcode.com/problems/rotting-oranges/discuss/2673232/python-!
class Solution: def orangesRotting(self, grid: List[List[int]]) -> int: q = deque() m,n = len(grid), len(grid[0]) oranges = set() for i in range(m): for j in range(n): if grid[i][j]==2: q.append((i,j,0)) ...
rotting-oranges
python !
sanjeevpathak
0
6
rotting oranges
994
0.525
Medium
16,205
https://leetcode.com/problems/rotting-oranges/discuss/2666239/python-BFS-with-readable-explanation
class Solution: def orangesRotting(self, grid: List[List[int]]) -> int: rows = len(grid) cols = len(grid[0]) minutes = 0 directions = [(1, 0), (-1, 0), (0, 1), (0, -1)] rotten = set() fresh_count = 0 infected = set() # go thru each cell to find the po...
rotting-oranges
python BFS with readable explanation
deezeey
0
15
rotting oranges
994
0.525
Medium
16,206
https://leetcode.com/problems/rotting-oranges/discuss/2622386/python3-oror-easy-oror-bfs-solution
class Solution: def orangesRotting(self, grid: List[List[int]]) -> int: visited=[[0]*len(grid[0]) for i in range(len(grid))] rowSize=len(grid) colSize=len(grid[0]) q=collections.deque() for r in range(rowSize): ...
rotting-oranges
python3 || easy || bfs solution
_soninirav
0
11
rotting oranges
994
0.525
Medium
16,207
https://leetcode.com/problems/rotting-oranges/discuss/2610530/Simple-Python-solution-(faster-than-90)-with-detailed-explanation.-easy-to-understand.
class Solution: def orangesRotting(self, grid: List[List[int]]) -> int: M = len(grid) # num rows N = len(grid[0]) # num cols q = [] # queue visited = [ [0]*N for _ in range(M)] #2D list n_fresh = [0] # global variable (pointer) def...
rotting-oranges
Simple Python solution (faster than 90%) with detailed explanation. easy to understand.
alexion1
0
37
rotting oranges
994
0.525
Medium
16,208
https://leetcode.com/problems/rotting-oranges/discuss/2600535/BREADTH-FIRST-SEARCH-APPROACH
class Solution: def orangesRotting(self, grid: List[List[int]]) -> int: """ Each cell can have one of 3 values 0 - empty cell 1 - fresh orange 2 - rotten orange output - time to turn all fresh oranges to rotten if possible else return -1 ...
rotting-oranges
BREADTH FIRST SEARCH APPROACH
leomensah
0
42
rotting oranges
994
0.525
Medium
16,209
https://leetcode.com/problems/rotting-oranges/discuss/2541032/Python3-Solution-or-BFS
class Solution: def orangesRotting(self, grid): n, m = len(grid), len(grid[0]) q, ans = collections.deque(), -1 count = sum(row.count(1) for row in grid) for i in range(n): for j in range(m): if grid[i][j] == 2: q.append((i, j)) ...
rotting-oranges
✔ Python3 Solution | BFS
satyam2001
0
44
rotting oranges
994
0.525
Medium
16,210
https://leetcode.com/problems/rotting-oranges/discuss/2537000/Python-oror-BFS-oror-Queue-oror-97-fast
class Solution: def orangesRotting(self, grid: List[List[int]]) -> int: q = [] m = len(grid) n = len(grid[0]) seen = set() def makerot(i,j,q): if (i,j) in seen: return seen.add((i,j)) if i+1 < m and (i+1,j) not in seen: ...
rotting-oranges
Python || BFS || Queue || 97% fast
1md3nd
0
70
rotting oranges
994
0.525
Medium
16,211
https://leetcode.com/problems/rotting-oranges/discuss/2470825/Clean-Fast-Python3-or-BFS
class Solution: def orangesRotting(self, grid: List[List[int]]) -> int: # for each fresh orange, bfs to nearest rotten one. Take max of these distances rows, cols = len(grid), len(grid[0]) dirs = [(0, -1), (-1, 0), (0, 1), (1, 0)] def bfs(start_row, start_col): n...
rotting-oranges
Clean, Fast Python3 | BFS
ryangrayson
0
47
rotting oranges
994
0.525
Medium
16,212
https://leetcode.com/problems/rotting-oranges/discuss/2417509/Python3-9792-Simple-Solution-w-Explanation
class Solution: def orangesRotting(self, grid: List[List[int]]) -> int: m = len(grid) n = len(grid[0]) changing = True infected = 1 # Loop through mxn changing grid entries until no entries are changed on a loop while changing: infected += 1 ...
rotting-oranges
[Python3] 97%/92% Simple Solution w Explanation
connorthecrowe
0
56
rotting oranges
994
0.525
Medium
16,213
https://leetcode.com/problems/rotting-oranges/discuss/2337831/Python3-knapsack-solution-with-mega-comprehension
class Solution: def orangesRotting(self, grid: List[List[int]]) -> int: rotten = frozenset((x, y) for y, row in enumerate(grid) for x, cell in enumerate(row) if cell == 2) length = len(grid) width = len(grid[0]) minutes = 0 while True: neighbors = (cell for x, y i...
rotting-oranges
Python3 knapsack solution with mega comprehension
SkookumChoocher
0
38
rotting oranges
994
0.525
Medium
16,214
https://leetcode.com/problems/rotting-oranges/discuss/2255567/Python-Readable-and-easy-to-understand-solution-with-explanation-using-only-a-queue
class Solution: EMPTY = 0 FRESH = 1 ROTTEN = 2 ADJACENT_DIRECTIONS = [(-1, 0), (+1, 0), (0, -1), (0, +1)] def orangesRotting(self, grid: List[List[int]]) -> int: self.grid = grid self.rows, self.columns = len(grid), len(grid[0]) rotten_coordinates_queue = self._get_rott...
rotting-oranges
[Python] Readable and easy to understand solution with explanation, using only a queue
julenn
0
67
rotting oranges
994
0.525
Medium
16,215
https://leetcode.com/problems/rotting-oranges/discuss/2254223/Python-Basic-BFS-91-Less-Memory
class Solution: def orangesRotting(self, grid: List[List[int]]) -> int: def searchFreshAndRotten(grid): freshes = 0 rots = [] for row in range(len(grid)): for col in range(len(grid[row])): if grid[row][col] == 1: ...
rotting-oranges
Python Basic BFS 91% Less Memory
codeee5141
0
54
rotting oranges
994
0.525
Medium
16,216
https://leetcode.com/problems/rotting-oranges/discuss/2116219/BFS-Solution-Python-(Time-87-Space-99)
class Solution: def orangesRotting(self, grid: List[List[int]]) -> int: # Breadth-first search m, n = len(grid), len(grid[0]) minute = 0 qRotten, freshCount = [], 0 for i in range(m): for j in range(n): # We store rotten ...
rotting-oranges
BFS Solution - Python (Time 87%, Space 99%)
tylerpruitt
0
92
rotting oranges
994
0.525
Medium
16,217
https://leetcode.com/problems/rotting-oranges/discuss/1988876/ororPYTHON-SOL-oror-VERY-EASY-oror-WELL-EXPLAINED-oror-JUST-AS-QUESTION-DEMANDS-oror-BFS-oror
class Solution: def orangesRotting(self, grid: List[List[int]]) -> int: # 0 = means empty # 1 = fresh orange # 2 = rotten orange # every minute any every rotten orange infects its adjacent fresh orange # min no of minutes to make all rotten else -1 ...
rotting-oranges
||PYTHON SOL || VERY EASY || WELL EXPLAINED || JUST AS QUESTION DEMANDS || BFS ||
reaper_27
0
77
rotting oranges
994
0.525
Medium
16,218
https://leetcode.com/problems/minimum-number-of-k-consecutive-bit-flips/discuss/2122927/Python-O(N)-S(N)-Queue-solution
class Solution: def minKBitFlips(self, nums: List[int], k: int) -> int: ans = 0 q = [] for i in range(len(nums)): if len(q) % 2 == 0: if nums[i] == 0: if i+k-1 <= len(nums)-1: ans += 1 q.append(i+...
minimum-number-of-k-consecutive-bit-flips
Python O(N) S(N) Queue solution
DietCoke777
0
64
minimum number of k consecutive bit flips
995
0.512
Hard
16,219
https://leetcode.com/problems/minimum-number-of-k-consecutive-bit-flips/discuss/1266069/Python3-greedy
class Solution: def minKBitFlips(self, nums: List[int], k: int) -> int: ans = flip = 0 queue = deque() for i, x in enumerate(nums): if queue and i == queue[0]: flip ^= 1 queue.popleft() if x == flip: if len(nums) - i ...
minimum-number-of-k-consecutive-bit-flips
[Python3] greedy
ye15
0
186
minimum number of k consecutive bit flips
995
0.512
Hard
16,220
https://leetcode.com/problems/number-of-squareful-arrays/discuss/1375586/python-simple-backtracking.-20ms
class Solution(object): def numSquarefulPerms(self, nums): """ :type nums: List[int] :rtype: int """ def dfs(temp,num,count = 0): if len(num)==0: return count+1 for i in xrange(len(num)): if (i>0 and num[i]==num[i-1]) or...
number-of-squareful-arrays
python simple backtracking. 20ms
leah123
1
305
number of squareful arrays
996
0.492
Hard
16,221
https://leetcode.com/problems/number-of-squareful-arrays/discuss/1314226/Python3-TSP
class Solution: def numSquarefulPerms(self, nums: List[int]) -> int: @cache def fn(v, mask): """Return squareful arrays given prev value and mask.""" if not mask: return 1 ans = 0 seen = set() for i, x in enumerate(nums): ...
number-of-squareful-arrays
[Python3] TSP
ye15
1
152
number of squareful arrays
996
0.492
Hard
16,222
https://leetcode.com/problems/number-of-squareful-arrays/discuss/2674701/Python
class Solution: def numSquarefulPerms(self, nums: List[int]) -> int: n = len(nums) nums.sort() def dfs(prev,rem): if not rem: return 1 ans = 0 for i,a in enumerate(rem): if (i > 0 and a == rem[i-1]) or (prev != -1 and mat...
number-of-squareful-arrays
Python
Akhil_krish_na
0
4
number of squareful arrays
996
0.492
Hard
16,223
https://leetcode.com/problems/number-of-squareful-arrays/discuss/2447950/Python-3-Backtrack
class Solution: def numSquarefulPerms(self, nums: List[int]) -> int: def is_perfect(v): k=int(math.sqrt(v)) return k*k==v nums.sort() def perm(A,prev): if len(A)==0: self.res+=1 return for j in range(len(A)): if j>0 and A[j]==A[j-1]:continue if prev is None: perm(A[:j]+A[j+1:],A...
number-of-squareful-arrays
[Python 3] Backtrack
gabhay
0
40
number of squareful arrays
996
0.492
Hard
16,224
https://leetcode.com/problems/number-of-squareful-arrays/discuss/1991455/orPYTHON-SOL-or-BACKTRACKING-%2B-HASHMAP-or-SIMPLE-SOLUTION-or-WELL-EXPLAINED-or
class Solution: def isSquare(self,num): return int(num**0.5)**2 == num def makePermutation(self,used,vis,prev,n): if used == n: #we reached the end self.ans += 1 return tmp = {} for i in range(n): if vis[i] == False and self.nums[i...
number-of-squareful-arrays
|PYTHON SOL | BACKTRACKING + HASHMAP | SIMPLE SOLUTION | WELL EXPLAINED |
reaper_27
0
96
number of squareful arrays
996
0.492
Hard
16,225
https://leetcode.com/problems/number-of-squareful-arrays/discuss/1691089/Python-optimal-backtrackingdfs-solution-(clean-code)
class Solution: def numSquarefulPerms(self, nums: List[int]) -> int: @lru_cache def square(m): left, right = 1, m while left < right: mid = (left + right) // 2 if mid**2 >= m: right = mid else: ...
number-of-squareful-arrays
Python optimal backtracking/dfs solution (clean code)
byuns9334
0
125
number of squareful arrays
996
0.492
Hard
16,226
https://leetcode.com/problems/number-of-squareful-arrays/discuss/831137/Similar-as-problem-47-and-use-DFS
class Solution: def numSquarefulPerms(self, A: List[int]) -> int: res = [] visited = [0] * len(A) A.sort() def helper(nums,out, res): if len(out) == len(nums): res.append(out[:]) return else: for i in range(len(A)): ...
number-of-squareful-arrays
Similar as problem 47 and use DFS
jppooo888
0
116
number of squareful arrays
996
0.492
Hard
16,227
https://leetcode.com/problems/find-the-town-judge/discuss/1663344/C%2B%2BJavaPython3Javascript-Everything-you-need-to-know-from-start-to-end-.
class Solution: def findJudge(self, N: int, trust: List[List[int]]) -> int: Trusted = [0] * (N+1) for (a, b) in trust: Trusted[a] -= 1 Trusted[b] += 1 for i in range(1, len(Trusted)): if Trusted[i] == N-1: return i retu...
find-the-town-judge
[C++/Java/Python3/Javascript] Everything you need to know from start to end .
Cosmic_Phantom
144
9,500
find the town judge
997
0.493
Easy
16,228
https://leetcode.com/problems/find-the-town-judge/discuss/1663192/Python3-EASY-TO-UNDERSTAND-CODE-Explained
class Solution: def findJudge(self, n: int, trust: List[List[int]]) -> int: trust_to, trusted = defaultdict(int), defaultdict(int) for a, b in trust: trust_to[a] += 1 trusted[b] += 1 for i in range(1, n+1): if trust_to[i] == 0 and trusted[i] == n...
find-the-town-judge
✔️ [Python3] EASY TO UNDERSTAND CODE, Explained
artod
7
864
find the town judge
997
0.493
Easy
16,229
https://leetcode.com/problems/find-the-town-judge/discuss/1621150/O(n)-solution-in-Python
class Solution: def findJudge(self, n: int, trust: List[List[int]]) -> int: dg = [0] * (n + 1) for a, b in trust: dg[a] -= 1 # out dg[b] += 1 # in return next((i for i in range(1, n + 1) if dg[i] == n - 1), -1)
find-the-town-judge
O(n) solution in Python
mousun224
3
266
find the town judge
997
0.493
Easy
16,230
https://leetcode.com/problems/find-the-town-judge/discuss/404001/Python-Logical-solution.-No-hashing-required.
class Solution(object): def findJudge(self, N, trust): if trust==[] and N==1: return 1 x1 = [x[1] for x in trust] x0 = [x[0] for x in trust] for i in range(1, N+1): if i in x1: if x1.count(i)==(N-1): if i not in x0: return i return -1
find-the-town-judge
Python Logical solution. No hashing required.
saffi
3
877
find the town judge
997
0.493
Easy
16,231
https://leetcode.com/problems/find-the-town-judge/discuss/1664882/Python-easy-and-clean-solution-with-full-explanation
class Solution: def findJudge(self, n: int, trust: List[List[int]]) -> int: no_of_trust = [0] * (n+1) #because in trust numbers starts from 1 to N for a,b in trust: no_of_trust[a] -= 1 # a trusts b so a will become less no_of_trust[b] += 1 # a trusts b so b will become more t...
find-the-town-judge
Python easy and clean solution with full explanation
yashitanamdeo
2
201
find the town judge
997
0.493
Easy
16,232
https://leetcode.com/problems/find-the-town-judge/discuss/382225/Two-Short-Solutions-in-Python-3
class Solution: def findJudge(self, n: int, t: List[List[int]]) -> int: N = set(range(1,n+1)) for i in t: N.discard(i[0]) if len(N) == 0: return -1 a = list(N)[0] return a if sum(i[1] == a for i in t) == n-1 else -1
find-the-town-judge
Two Short Solutions in Python 3
junaidmansuri
2
479
find the town judge
997
0.493
Easy
16,233
https://leetcode.com/problems/find-the-town-judge/discuss/382225/Two-Short-Solutions-in-Python-3
class Solution: def findJudge(self, n: int, t: List[List[int]]) -> int: N = set(range(1,n+1)) for i in t: N.discard(i[0]) return (lambda x: x if sum(i[1] == x for i in t) == n-1 and len(N) == 1 else -1)(list(N)[0] if len(N) != 0 else -1) - Junaid Mansuri (LeetCode ID)@hotmail.com
find-the-town-judge
Two Short Solutions in Python 3
junaidmansuri
2
479
find the town judge
997
0.493
Easy
16,234
https://leetcode.com/problems/find-the-town-judge/discuss/242937/Python3-List-O(N)-space-O(N)-time
class Solution: def findJudge(self, N: int, trust: List[List[int]]) -> int: a = [0] * (N + 1) for l in trust: a[l[1]] += 1 a[l[0]] -= 1 for i in range(1, len(a)): if a[i] == N - 1: return i return -1
find-the-town-judge
Python3 List O(N) space, O(N) time
jimmyyentran
2
319
find the town judge
997
0.493
Easy
16,235
https://leetcode.com/problems/find-the-town-judge/discuss/2515433/Efficient-Python-Solution-or-Memory-less-than-91.50
class Solution: def findJudge(self, n: int, trust: List[List[int]]) -> int: if not trust and n == 1: return 1 degree = [0 for i in range(0,n+1)] for u, v in trust: degree[u] -= 1 #indegree = -1 for that node degree[v] += 1 #outdegree = +1 for that node for i in degree: if i == (n - 1): return...
find-the-town-judge
Efficient Python Solution | Memory less than 91.50%
nikhitamore
1
73
find the town judge
997
0.493
Easy
16,236
https://leetcode.com/problems/find-the-town-judge/discuss/1810855/Python-3-hashmap-solution
class Solution: def findJudge(self, n: int, trust: List[List[int]]) -> int: trustCount = collections.Counter() trustedCount = collections.Counter() for a, b in trust: trustCount[a] += 1 trustedCount[b] += 1 for i in range(1, n + 1): ...
find-the-town-judge
Python 3, hashmap solution
dereky4
1
195
find the town judge
997
0.493
Easy
16,237
https://leetcode.com/problems/find-the-town-judge/discuss/1664607/Python3-Explanation-and-Intuition-of-complete-solution.
class Solution: def findJudge(self, n: int, trust: List[List[int]]) -> int: trusted_by = [0] * n for a, b in trust: [a - 1] -= 1 trusted_by[b - 1] += 1 for i in range(n): if trusted_by[i] == n - 1: return i + 1 return -1
find-the-town-judge
[Python3] Explanation and Intuition of complete solution.
Crimsoncad3
1
66
find the town judge
997
0.493
Easy
16,238
https://leetcode.com/problems/find-the-town-judge/discuss/1663944/Python3-2-liner-and-one-liner
class Solution: def findJudge(self, n: int, trust: List[List[int]]) -> int: counts= collections.Counter([edge for p1,p2 in trust for edge in ((p1,0),(0,p2))]) return next(itertools.chain((p for p in range(1,n+1) if counts[(0,p)]-counts[(p,0)] == n-1),[-1]))
find-the-town-judge
Python3 2-liner and one-liner
pknoe3lh
1
80
find the town judge
997
0.493
Easy
16,239
https://leetcode.com/problems/find-the-town-judge/discuss/1663944/Python3-2-liner-and-one-liner
class Solution: def findJudge(self, n: int, trust: List[List[int]]) -> int: return (lambda counts: next(itertools.chain((p for p in range(1,n+1) if counts[(0,p)]-counts[(p,0)] == n-1),[-1])))(collections.Counter([edge for p1,p2 in trust for edge in ((p1,0),(0,p2))]))
find-the-town-judge
Python3 2-liner and one-liner
pknoe3lh
1
80
find the town judge
997
0.493
Easy
16,240
https://leetcode.com/problems/find-the-town-judge/discuss/1663227/Python3-O(V-%2B-E)-or-Clean-%2B-Simple-Solution-or-In-Degree-and-Out-Degree
class Solution: def findJudge(self, n: int, trust: List[List[int]]) -> int: inDegree = [0] * n outDegree = [0] * n for node, neighb in trust: inDegree[neighb - 1] += 1 outDegree[node - 1] += 1 for i, (inD, outD) in enumerate(zip(inDegree, outDegree)):...
find-the-town-judge
✅ [Python3] O(V + E) | Clean + Simple Solution | In-Degree & Out-Degree
PatrickOweijane
1
207
find the town judge
997
0.493
Easy
16,241
https://leetcode.com/problems/find-the-town-judge/discuss/1258626/Python3-solution-faster-100
class Solution: def findJudge(self, n: int, trust: List[List[int]]) -> int: first = [] second = [] if n==1 and len(trust)==0: return 1 for i in trust: first.append(i[0]) second.append(i[1]) x = list((set(second)-set(first))) if len(...
find-the-town-judge
Python3 solution faster 100%
Sanyamx1x
1
253
find the town judge
997
0.493
Easy
16,242
https://leetcode.com/problems/find-the-town-judge/discuss/1219967/Python3-simple-solution-using-two-lists
class Solution: def findJudge(self, n: int, trust: List[List[int]]) -> int: if n == 1: return 1 l1 = list(range(1,n+1)) l2 = [] for i in trust: if i[0] in l1: l1.remove(i[0]) if i[1] in l1: l2.append(i[1]) fo...
find-the-town-judge
Python3 simple solution using two lists
EklavyaJoshi
1
97
find the town judge
997
0.493
Easy
16,243
https://leetcode.com/problems/find-the-town-judge/discuss/891198/Python3-simple-using-iteration-and-degree
class Solution: def findJudge(self, N: int, trust: List[List[int]]) -> int: inDegree = [0]*N outDegree = [0]*N for a,b in trust: outDegree[a-1] += 1 inDegree[b-1] += 1 for i in range(N): if outDegree[i] == 0 and inDegree[i] == N-1...
find-the-town-judge
Python3 - simple using iteration and degree
gargprat
1
112
find the town judge
997
0.493
Easy
16,244
https://leetcode.com/problems/find-the-town-judge/discuss/760354/Python-3Find-the-Town-Judge.
class Solution: def findJudge(self, N: int, trust: List[List[int]]) -> int: if N==1: return 1 # Since it is a Directed Graph # if -> income degree +=1 # if -> outgoing degree -=1 degree = [0]*(N+1) for i,j in trust: ...
find-the-town-judge
[Python 3]Find the Town Judge.
tilak_
1
186
find the town judge
997
0.493
Easy
16,245
https://leetcode.com/problems/find-the-town-judge/discuss/624957/Python3-indeg-and-outdeg
class Solution: def findJudge(self, n: int, trust: List[List[int]]) -> int: degree = [0]*n for u, v in trust: degree[v-1] += 1 degree[u-1] -= 1 return next((i+1 for i, x in enumerate(degree) if x == n-1), -1)
find-the-town-judge
[Python3] indeg & outdeg
ye15
1
42
find the town judge
997
0.493
Easy
16,246
https://leetcode.com/problems/find-the-town-judge/discuss/2757946/Python3-81-faster-with-explanation
class Solution: def findJudge(self, n: int, trust: List[List[int]]) -> int: trustMap = {} for i in range(1, n + 1): trustMap[i] = [0, 0] for tPath in trust: trustMap[tPath[0]][1] += 1 trustMap[tPath[1]][0] += 1 for person in t...
find-the-town-judge
Python3, 81% faster with explanation
cvelazquez322
0
9
find the town judge
997
0.493
Easy
16,247
https://leetcode.com/problems/find-the-town-judge/discuss/2733453/Simple-Python-Solution
class Solution: def findJudge(self, n: int, trust: List[List[int]]) -> int: tracker = [0] * n for a, b in trust: tracker[a-1] -= 1 tracker[b-1] += 1 for i in range(0, n): if tracker[i] == n - 1: return i + 1 return -1
find-the-town-judge
Simple Python Solution
ekomboy012
0
3
find the town judge
997
0.493
Easy
16,248
https://leetcode.com/problems/find-the-town-judge/discuss/2708890/Graph-or-Python-or-O(n)
class Solution: def findJudge(self, n: int, trust: List[List[int]]) -> int: t = defaultdict(int) for i in range(n): t[i] = 0 for a, b in trust: t[a - 1] -= 1 t[b - 1] += 1 for k, _ in t.items(): if t[k...
find-the-town-judge
Graph | Python | O(n)
Kiyomi_
0
12
find the town judge
997
0.493
Easy
16,249
https://leetcode.com/problems/find-the-town-judge/discuss/1959649/Python-3-or-faster-than-99.91
class Solution: def findJudge(self, n: int, trust: List[List[int]]) -> int: if not trust and n == 1: return 1 elif not trust: return -1 judgeCnt = Counter([ y for x, y in trust]).most_common()[0] if judgeCnt[1] != n - 1 or judgeCnt[0] in [ x ...
find-the-town-judge
Python 3 | faster than 99.91%
anels
0
194
find the town judge
997
0.493
Easy
16,250
https://leetcode.com/problems/find-the-town-judge/discuss/1806350/3-Lines-Python-Solution-oror-slow-oror-Memory-less-than-60
class Solution: def findJudge(self, n: int, trust: List[List[int]]) -> int: for i in range(1,n+1): if sorted([trus[0] for trus in trust if trus[1]==i])==[x for x in range(1,n+1) if x!=i] and i not in [trus[0] for trus in trust]: return i return -1
find-the-town-judge
3-Lines Python Solution || slow || Memory less than 60%
Taha-C
0
107
find the town judge
997
0.493
Easy
16,251
https://leetcode.com/problems/find-the-town-judge/discuss/1664970/Using-sets-and-intersection-python-O(n)-solution
class Solution: def findJudge(self, n: int, trust: List[List[int]]) -> int: if trust == []: return 1 if n == 1 else -1 memory = {} for t in trust: memory.setdefault(t[0], set()).add(t[1]) trusted = set.intersection(*memory.val...
find-the-town-judge
Using sets & intersection python O(n) solution
Sima24
0
50
find the town judge
997
0.493
Easy
16,252
https://leetcode.com/problems/find-the-town-judge/discuss/1664611/Python-3-Easy-Solution
class Solution: def findJudge(self, n: int, trust: List[List[int]]) -> int: if n==1: return 1 ct={} t={} for i in trust: if i[1] in ct: ct[i[1]]+=1 else: ct[i[1]]=1 for i in trust: t[i[0]]=i[1] ...
find-the-town-judge
Python 3 Easy Solution
aryanagrawal2310
0
74
find the town judge
997
0.493
Easy
16,253
https://leetcode.com/problems/find-the-town-judge/discuss/1664288/Unique-Approach-Python3-Solution-O(1)-Memory
class Solution: def findJudge(self, n: int, trust: List[List[int]]) -> int: trust = list(sorted(trust, key=lambda x: x[1])) # Sort list based on the trustee if n == 1: # Only one person condition return 1 if not trust: return -1 ...
find-the-town-judge
[Unique Approach] Python3 Solution O(1) Memory
Sparkles4
0
73
find the town judge
997
0.493
Easy
16,254
https://leetcode.com/problems/find-the-town-judge/discuss/1663815/Python3-hashmap-solution-or-easy-understanding
class Solution: def findJudge(self, n: int, trust: List[List[int]]) -> int: if n == 1: return 1 if not trust: return -1 potential_judge = dict() normal_people = [] for i in trust: if i[1] in potential_judge.keys(): potential_judge[i[1...
find-the-town-judge
Python3 hashmap solution | easy-understanding
Janetcxy
0
52
find the town judge
997
0.493
Easy
16,255
https://leetcode.com/problems/find-the-town-judge/discuss/1663483/Python-Solution-SImple-to-understand
class Solution: def findJudge(self, n: int, trust: List[List[int]]) -> int: if n == 1 and len(trust) == 0: return 1 if len(trust) == 0: return -1 persons = [] for i in range(n+1): persons.append({"id": i, "trusted_by": 0, "trusts": 0}) ...
find-the-town-judge
Python Solution SImple to understand
pradeep288
0
156
find the town judge
997
0.493
Easy
16,256
https://leetcode.com/problems/find-the-town-judge/discuss/1663177/python3-Simple-O(n)-Solution
class Solution: def findJudge(self, n: int, trust: List[List[int]]) -> int: votes = [0] * n # track the current most popular candidate c = 0 for a, b in trust: # the judge trusts noone, so anyone that votes cannot possibly be in the running votes[a-1] = -inf ...
find-the-town-judge
python3 Simple O(n) Solution
zldobbs
0
51
find the town judge
997
0.493
Easy
16,257
https://leetcode.com/problems/find-the-town-judge/discuss/1342456/Easy-Python-Solution
class Solution: def findJudge(self, n: int, trust: List[List[int]]) -> int: if not trust and n!=1: return -1 s=set() j=0 for i in (trust): s.add(i[0]) for i in range(1,n+1): if(i not in s): j=i break ...
find-the-town-judge
Easy Python Solution
Sneh17029
0
316
find the town judge
997
0.493
Easy
16,258
https://leetcode.com/problems/find-the-town-judge/discuss/1092248/Python-Solution-using-a-Dictionary
class Solution: def findJudge(self, N: int, trust: List[List[int]]) -> int: """ Uses a hash table to store the valid candidates and who trusted this candidate. Time complexity: O(N). Space complexity: O(N) """ # A dictionary maps a candidate to who trusted t...
find-the-town-judge
Python Solution using a Dictionary
QizhangJia
0
263
find the town judge
997
0.493
Easy
16,259
https://leetcode.com/problems/find-the-town-judge/discuss/1080127/a-Python-solution-based-on-%22277.-Find-the-Celebrity%22
class Solution: def findJudge(self, N: int, trust: List[List[int]]) -> int: # main ideas: # 1. if a trusts b -> a is not a judge # 2. if a doesn't trust b -> b is not a judge candidate = 1 for i in range(2, N+1): # 1~N if [candidate, i] in trust: ...
find-the-town-judge
a Python solution based on "277. Find the Celebrity"
kylu
0
146
find the town judge
997
0.493
Easy
16,260
https://leetcode.com/problems/find-the-town-judge/discuss/991887/Python-easy-to-understand
class Solution: def findJudge(self, N: int, trust: List[List[int]]) -> int: if N==1: return 1 d = defaultdict(list) for n in trust: d[n[0]].append(1) d[n[1]].append(2) for k,v in d.items(): cond = [True if a==2 els...
find-the-town-judge
Python easy to understand
vimoxshah
0
207
find the town judge
997
0.493
Easy
16,261
https://leetcode.com/problems/find-the-town-judge/discuss/959284/Python3-O(n)-with-explanation
class Solution: def findJudge(self, N: int, trust: List[List[int]]) -> int: seen = {t[0] for t in trust} j = None for i in range(1, N+1): if i in seen: continue if j: return -1 j = i if not j: return -1 ...
find-the-town-judge
[Python3] O(n) with explanation
gdm
0
201
find the town judge
997
0.493
Easy
16,262
https://leetcode.com/problems/find-the-town-judge/discuss/624177/Python3-Beautiful-and-detailed-solution-with-O(N)-Time-and-O(N)-Extra-Space
class Solution: def findJudge(self, N: int, trust: List[List[int]]) -> int: all_people = set(range(1, N + 1)) people_who_trust = set([x[0] for x in trust]) people_who_dont_trust = all_people - people_who_trust if len(people_who_dont_trust) != 1: return - 1 judge =...
find-the-town-judge
[Python3] Beautiful & detailed solution with O(N) Time & O(N) Extra Space
timetoai
0
46
find the town judge
997
0.493
Easy
16,263
https://leetcode.com/problems/find-the-town-judge/discuss/243955/Celebrity-question-detailed-explanation
class Solution: def findJudge(self, N: int, trust: List[List[int]]) -> int: a_trust_b = set() for a, b in trust: a_trust_b.add((a, b)) if N == 1: return 1 # find candidate judge as b # each round elimnate 1 candidate # after a...
find-the-town-judge
Celebrity question detailed explanation
leonmak
0
158
find the town judge
997
0.493
Easy
16,264
https://leetcode.com/problems/find-the-town-judge/discuss/1520554/Python3-Two-kind-of-solutions
class Solution: def findJudge(self, n: int, trust: List[List[int]]) -> int: if n == 1: return 1 d = {} trusted = set() for t in trust: if t[1] not in d: d[t[1]] = [] d[t[1]].append(t[0]) trusted.add(t[0...
find-the-town-judge
[Python3] Two kind of solutions
maosipov11
-1
111
find the town judge
997
0.493
Easy
16,265
https://leetcode.com/problems/maximum-binary-tree-ii/discuss/2709985/Python-short-python-solution
class Solution: def insertIntoMaxTree(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]: if not root: return TreeNode(val) if val > root.val: return TreeNode(val, root) root.right = self.insertIntoMaxTree(root.right, val) return root
maximum-binary-tree-ii
[Python] short python solution
scrptgeek
0
6
maximum binary tree ii
998
0.665
Medium
16,266
https://leetcode.com/problems/maximum-binary-tree-ii/discuss/2533846/Python-Commented-and-simple-DFS-solution
class Solution: def insertIntoMaxTree(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]: # that can be solved using DFS, as it is quite easy to # keep track of the parent node there # take care of the edge case that there is no root if not root: ...
maximum-binary-tree-ii
[Python] - Commented and simple DFS solution
Lucew
0
18
maximum binary tree ii
998
0.665
Medium
16,267
https://leetcode.com/problems/maximum-binary-tree-ii/discuss/2533846/Python-Commented-and-simple-DFS-solution
class Solution: def insertIntoMaxTree(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]: if not root: return TreeNode(val=val) if root.val < val: return TreeNode(val=val, left=root) dfs(root.right, root, val) return root def dfs(node, pa...
maximum-binary-tree-ii
[Python] - Commented and simple DFS solution
Lucew
0
18
maximum binary tree ii
998
0.665
Medium
16,268
https://leetcode.com/problems/maximum-binary-tree-ii/discuss/985315/Python3-move-down-the-tree-O(logN)
class Solution: def insertIntoMaxTree(self, root: TreeNode, val: int) -> TreeNode: prev, node = None, root while node and val < node.val: prev, node = node, node.right temp = TreeNode(val, left=node) if prev: prev.right = temp else: root = temp return root
maximum-binary-tree-ii
[Python3] move down the tree O(logN)
ye15
0
76
maximum binary tree ii
998
0.665
Medium
16,269
https://leetcode.com/problems/available-captures-for-rook/discuss/356593/Solution-in-Python-3-(beats-~97)-(three-lines)
class Solution: def numRookCaptures(self, b: List[List[str]]) -> int: I, J = divmod(sum(b,[]).index('R'),8) C = "".join([i for i in [b[I]+['B']+[b[i][J] for i in range(8)]][0] if i != '.']) return C.count('Rp') + C.count('pR') - Junaid Mansuri (LeetCode ID)@hotmail.com
available-captures-for-rook
Solution in Python 3 (beats ~97%) (three lines)
junaidmansuri
8
839
available captures for rook
999
0.679
Easy
16,270
https://leetcode.com/problems/available-captures-for-rook/discuss/1601678/Python-3-easy-to-understand-faster-than-96
class Solution: def numRookCaptures(self, board: List[List[str]]) -> int: n = 8 for i in range(n): # find rook location for j in range(n): if board[i][j] == 'R': x, y = i, j break res = 0 for i in range(x-1...
available-captures-for-rook
Python 3 easy to understand, faster than 96%
dereky4
3
159
available captures for rook
999
0.679
Easy
16,271
https://leetcode.com/problems/available-captures-for-rook/discuss/1112858/Easiest-Recursive-solution-or-97.6-time-98.2-space
class Solution: def numRookCaptures(self, board: List[List[str]]) -> int: def find(rx, ry, direction, count): if rx == 8 or ry == 8 or rx == -1 or ry == -1: return count if board[rx][ry] == "B": return 0 if board[rx][ry] == "p": return count + 1 if di...
available-captures-for-rook
Easiest Recursive solution | 97.6% time, 98.2% space
vanigupta20024
3
243
available captures for rook
999
0.679
Easy
16,272
https://leetcode.com/problems/available-captures-for-rook/discuss/500938/Python3%3A-not-pretty-but-straight-forward
class Solution: def numRookCaptures(self, board: List[List[str]]) -> int: for i in range(len(board)): for j in range(len(board[0])): if board[i][j] == 'R': count = 0; l, r = j - 1, j + 1 while l >= 0: ...
available-captures-for-rook
Python3: not pretty, but straight forward
andnik
1
157
available captures for rook
999
0.679
Easy
16,273
https://leetcode.com/problems/available-captures-for-rook/discuss/379640/Simon's-Note-Python3
class Solution: def numRookCaptures(self, board: List[List[str]]) -> int: res=0 n_row=len(board) n_col=len(board[0]) dirs=[[0,1],[0,-1],[-1,0],[1,0]] for i in range(n_row): for j in range(n_col): if board[i][j]=="R": for dir in ...
available-captures-for-rook
[🎈Simon's Note🎈] Python3
SunTX
1
101
available captures for rook
999
0.679
Easy
16,274
https://leetcode.com/problems/available-captures-for-rook/discuss/2843995/Python3-Solution-using-DFS
class Solution: def numRookCaptures(self, board: List[List[str]]) -> int: def dfs(r, c, i, j): ans = 0 while 0 <= r < 8 and 0 <= c < 8: if board[r][c] == 'p': ans += 1 break if board[r][c] == 'B': ...
available-captures-for-rook
[Python3] Solution using DFS
BLOCKS
0
3
available captures for rook
999
0.679
Easy
16,275
https://leetcode.com/problems/available-captures-for-rook/discuss/2716302/Python-Easy-to-follow-with-comments
class Solution: def numRookCaptures(self, board: List[List[str]]) -> int: def find_pawn(board_slice): for square in board_slice: if square == 'B': return 0 if square == 'p': return 1 return 0 output = ...
available-captures-for-rook
Python - Easy to follow with comments
ptegan
0
10
available captures for rook
999
0.679
Easy
16,276
https://leetcode.com/problems/available-captures-for-rook/discuss/2711816/Python-!-Simple-Solution
class Solution: def numRookCaptures(self, board: List[List[str]]) -> int: bod = board[::] rows = [[i for i in ro if i != "."] for ro in bod] cols = [[i for i in list(co) if i != "."]for co in list(zip(*bod))] count = 0 rows = rows + cols for row in ...
available-captures-for-rook
Python ! Simple Solution
w7Pratham
0
11
available captures for rook
999
0.679
Easy
16,277
https://leetcode.com/problems/available-captures-for-rook/discuss/2681421/Python-Solution-Fast-and-Easy
class Solution: def numRookCaptures(self, board: List[List[str]]) -> int: ans = 0 boardT = list(zip(*board)) for i in range(8): if "R" in board[i]: j = board[i].index("R") rookIndex = (i, j) if "p" in board[i][:j]: ...
available-captures-for-rook
Python Solution - Fast and Easy
scifigurmeet
0
2
available captures for rook
999
0.679
Easy
16,278
https://leetcode.com/problems/available-captures-for-rook/discuss/1991449/easy-soln
class Solution: def numRookCaptures(self, board: List[List[str]]) -> int: rookcoord=[] i=j=0 while i < len(board): j=0 while j < len(board[0]): if board[i][j] == "R": #print("Rook found", i,j) r = i ...
available-captures-for-rook
easy soln
golden-eagle
0
18
available captures for rook
999
0.679
Easy
16,279
https://leetcode.com/problems/available-captures-for-rook/discuss/1980496/python3-easy-solution-for-rook
class Solution: def numRookCaptures(self, board: List[List[str]]) -> int: counter=0 number=0 number2=0 for i in range(len(board)): temp=board[i] for j in range(len(temp)): if temp[j] == 'R': index_rook = j ...
available-captures-for-rook
python3 easy solution for rook
vishwahiren16
0
44
available captures for rook
999
0.679
Easy
16,280
https://leetcode.com/problems/available-captures-for-rook/discuss/1650582/Simple-Python-Solution
class Solution: def numRookCaptures(self, board: List[List[str]]) -> int: x=y=0 for i in range(len(board)): flag=0 for j in range(len(board[0])): if board[i][j]=='R': x,y=i,j # print(x, y) count=0 for i in range(x, -...
available-captures-for-rook
Simple Python Solution
Siddharth_singh
0
83
available captures for rook
999
0.679
Easy
16,281
https://leetcode.com/problems/available-captures-for-rook/discuss/1398741/Python3-Simulation-Faster-Than-95.27-Memory-Less-Than-63.43
class Solution: def numRookCaptures(self, board: List[List[str]]) -> int: for i in range(8): for j in range(8): if board[i][j] == 'R': x, y = i, j break cap = 0 flag1, flag2, flag3, flag4 = False, False, False, False...
available-captures-for-rook
Python3 Simulation Faster Than 95.27%, Memory Less Than 63.43%
Hejita
0
48
available captures for rook
999
0.679
Easy
16,282
https://leetcode.com/problems/available-captures-for-rook/discuss/1268386/Simple-Python-Solution-Recursive-Approach-(DFS-like)
class Solution: def numRookCaptures(self, board: List[List[str]]) -> int: # to store result res = [0] # recursive function to find number of available captures def find(i,j,dirn): # checking if the position is still out of bound or is 'B' meaning it ...
available-captures-for-rook
Simple Python Solution - Recursive Approach (DFS-like)
nagashekar
0
77
available captures for rook
999
0.679
Easy
16,283
https://leetcode.com/problems/available-captures-for-rook/discuss/1060944/Python3-simple-solution
class Solution: def numRookCaptures(self, board: List[List[str]]) -> int: def check(board, row, col): res = 0 a = [[1,0],[-1,0],[0,1],[0,-1]] for n in a: x,y = row,col i = n[0] j = n[1] while 0<=x<=7 and 0<=y...
available-captures-for-rook
Python3 simple solution
EklavyaJoshi
0
78
available captures for rook
999
0.679
Easy
16,284
https://leetcode.com/problems/available-captures-for-rook/discuss/1004621/Python-Faster-than-99.49-20-ms-Search-and-Capture
class Solution: def numRookCaptures(self, board: List[List[str]]) -> int: def total_captures(i,j): res = 0 # here we are searching for a pawn in top, # bottom, left, and right directions # If we find a pawn first, we can capture it # If we find a bishop, then we can't ca...
available-captures-for-rook
Python Faster than 99.49% 20 ms - Search and Capture
prashantsengar
0
181
available captures for rook
999
0.679
Easy
16,285
https://leetcode.com/problems/available-captures-for-rook/discuss/777981/Intuitive-approach-by-searching-four-directions
class Solution: def numRookCaptures(self, board: List[List[str]]) -> int: R, C = len(board), len(board[0]) # 1) Search for rock rock_r = rock_c = 0 for i in range(R): for j in range(C): if board[i][j] == 'R': rock_r, rock_c = i, j ...
available-captures-for-rook
Intuitive approach by searching four directions
puremonkey2001
0
52
available captures for rook
999
0.679
Easy
16,286
https://leetcode.com/problems/available-captures-for-rook/discuss/512642/Python3-94.24-extremely-easy-to-write-using-too-many-'break'-though......
class Solution: def numRookCaptures(self, board: List[List[str]]) -> int: # find R for j in range(8): for i in range(8): if board[j][i] == 'R': count = 0 # find if there is any p vertically above R for n in range(j,-1,-1): ...
available-captures-for-rook
Python3 94.24% - extremely easy to write using too many 'break' though......
Bannbuuu
0
111
available captures for rook
999
0.679
Easy
16,287
https://leetcode.com/problems/available-captures-for-rook/discuss/248407/Faster-Than-100-Python-Solution
class Solution: def numRookCaptures(self, board: List[List[str]]) -> int: row,column=self.findRook(board) if(row is None and column is None): return 0 count=0 #Above Rook for i in range(row,0,-1): if(board[i][column]=='B'): bre...
available-captures-for-rook
Faster Than 100% Python Solution
bismeet
0
117
available captures for rook
999
0.679
Easy
16,288
https://leetcode.com/problems/available-captures-for-rook/discuss/244982/Easy-to-read-and-understand-Python-16ms
class Solution(object): def numRookCaptures(self, board): """ :type board: List[List[str]] :rtype: int """ found, i, j = self.findWhiteRook(board) if not found: print ('Rook not found.') return False p = 0 p = self.blackPondCoun...
available-captures-for-rook
Easy to read and understand Python 16ms
jujbates
0
113
available captures for rook
999
0.679
Easy
16,289
https://leetcode.com/problems/available-captures-for-rook/discuss/251469/Python-3-100-faster-100-memory
class Solution: def numRookCaptures(self, board: List[List[str]]) -> int: # At first find position of rook and save in 'iR' and 'jR' variables for i in range(len(board)): for j in range(len(board[i])): if board[i][j] == 'R': iR = i ...
available-captures-for-rook
Python 3, 100% faster, 100% memory
astepano
-1
174
available captures for rook
999
0.679
Easy
16,290
https://leetcode.com/problems/minimum-cost-to-merge-stones/discuss/1465680/Python3-dp
class Solution: def mergeStones(self, stones: List[int], k: int) -> int: if (len(stones)-1) % (k-1): return -1 # impossible prefix = [0] for x in stones: prefix.append(prefix[-1] + x) @cache def fn(lo, hi): """Return min cost of merging stones[l...
minimum-cost-to-merge-stones
[Python3] dp
ye15
1
654
minimum cost to merge stones
1,000
0.423
Hard
16,291
https://leetcode.com/problems/minimum-cost-to-merge-stones/discuss/781323/python-Top-Down-solution
class Solution: def minCost(self, n: int, cuts: List[int]) -> int: _cuts = [0] + sorted(cuts) + [n] N = len(_cuts) @lru_cache(None) def helper(lp,rp): nonlocal _cuts if rp-lp==1: return 0 return _cuts[rp]-_cuts[lp] + min([h...
minimum-cost-to-merge-stones
python Top Down solution
e-yi
1
564
minimum cost to merge stones
1,000
0.423
Hard
16,292
https://leetcode.com/problems/minimum-cost-to-merge-stones/discuss/2633516/Top-down-dynamic-programming-in-concise-Python
class Solution: @cache def dp(self, l, r, piles) -> int: if r - l < piles: return inf if r - l == piles: return 0 if piles == 1: return self.dp(l, r, self.k) + self.prefix_sum[r] - self.prefix_sum[l] return min(self.dp(l, m, i) + self.dp(m, r, ...
minimum-cost-to-merge-stones
Top-down dynamic programming in concise Python
metaphysicalist
0
36
minimum cost to merge stones
1,000
0.423
Hard
16,293
https://leetcode.com/problems/grid-illumination/discuss/1233528/Python-or-HashMap-or-O(L%2BQ)-or-928ms
class Solution: def gridIllumination(self, n: int, lamps: List[List[int]], queries: List[List[int]]) -> List[int]: lamps = {(r, c) for r, c in lamps} row, col, left, right = dict(), dict(), dict(), dict() for r, c in lamps: row[r] = row.get(r, 0) + 1 col[c] =...
grid-illumination
Python | HashMap | O(L+Q) | 928ms
PuneethaPai
2
103
grid illumination
1,001
0.362
Hard
16,294
https://leetcode.com/problems/grid-illumination/discuss/2181052/python-3-or-simple-4-hash-map-solution
class Solution: def gridIllumination(self, n: int, lamps: List[List[int]], queries: List[List[int]]) -> List[int]: rows = collections.Counter() cols = collections.Counter() diags1 = collections.Counter() diags2 = collections.Counter() lamps = {tuple(lamp) for lamp in lamps} ...
grid-illumination
python 3 | simple 4 hash map solution
dereky4
1
142
grid illumination
1,001
0.362
Hard
16,295
https://leetcode.com/problems/grid-illumination/discuss/2115766/Python3-solution-or-Hashmap-or-Explained
class Solution: def gridIllumination(self, n: int, lamps: List[List[int]], queries: List[List[int]]) -> List[int]: def check(i, j, dRow, dCol, dDiagS, dDiagP): if (i in dRow and dRow[i] > 0) or (j in dCol and dCol[j] > 0) or ( i + j in dDiagS and dDiagS[i + j] > 0) or ( ...
grid-illumination
Python3 solution | Hashmap | Explained
FlorinnC1
1
51
grid illumination
1,001
0.362
Hard
16,296
https://leetcode.com/problems/grid-illumination/discuss/1998042/PYTHON-SOL-oror-WELL-EXPLAINED-oror-HASHMAP-BASED-oror-SIMPLE-oror-EFFICIENT-oror
class Solution: def checkIsOn(self,row,col): return 1 if (self.rows[row] > 0 or self.cols[col] > 0 \ or self.digonal1[row-col] > 0 or self.digonal2[row+col] > 0) else 0 def TurnOff(self,row,col): adj = ((row,col),(row+1,col),(row-1,col),(row,col-1),(row,col+1),\ (row+1...
grid-illumination
PYTHON SOL || WELL EXPLAINED || HASHMAP BASED || SIMPLE || EFFICIENT ||
reaper_27
1
51
grid illumination
1,001
0.362
Hard
16,297
https://leetcode.com/problems/grid-illumination/discuss/1638153/python-solution-with-tables-tracking-rows-cols-and-diags-lit
class Solution: from collections import defaultdict from itertools import product def gridIllumination(self, n: int, lamps: List[List[int]], queries: List[List[int]]) -> List[int]: rows = defaultdict(int) cols = defaultdict(int) downright = defaultdict(int) downleft = default...
grid-illumination
python solution with tables tracking rows, cols, and diags lit
PsyKosh
1
95
grid illumination
1,001
0.362
Hard
16,298
https://leetcode.com/problems/grid-illumination/discuss/1521789/Python3-freq-table
class Solution: def gridIllumination(self, n: int, lamps: List[List[int]], queries: List[List[int]]) -> List[int]: lamps = {(i, j) for i, j in lamps} rows = defaultdict(int) cols = defaultdict(int) anti = defaultdict(int) diag = defaultdict(int) for i, j in lamps: ...
grid-illumination
[Python3] freq table
ye15
0
92
grid illumination
1,001
0.362
Hard
16,299