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/di-string-match/discuss/1121091/Python-two-phases-solution
class Solution: def diStringMatch(self, S: str) -> List[int]: N = len(S) result = [0] * (N + 1) # start from 0 # and track the current minimal and maximal number. mi = mx = 0 n = 0 for i in range(N): if S[i] == 'I': ...
di-string-match
Python two phases solution
pochy
0
25
di string match
942
0.768
Easy
15,300
https://leetcode.com/problems/di-string-match/discuss/1032257/Python3-simple-and-efficient-solution
class Solution: def diStringMatch(self, S: str) -> List[int]: x = [] a = 0 b = len(S) for i in range(b): if S[i] == 'I': x.append(a) a += 1 else: x.append(b) b -= 1 x.append(b) ret...
di-string-match
Python3 simple and efficient solution
EklavyaJoshi
0
45
di string match
942
0.768
Easy
15,301
https://leetcode.com/problems/di-string-match/discuss/1232339/python-easy-solution-or
class Solution: def diStringMatch(self, s: str) -> List[int]: a=0 b=len(s) ans=[] for j in s: if j=='I': ans.append(a) a+=1 else: ans.append(b) b-=1 ...
di-string-match
python easy solution |
chikushen99
-1
56
di string match
942
0.768
Easy
15,302
https://leetcode.com/problems/find-the-shortest-superstring/discuss/1231147/Python3-travelling-sales-person-(TSP)
class Solution: def shortestSuperstring(self, words: List[str]) -> str: n = len(words) graph = [[0]*n for _ in range(n)] # graph as adjacency matrix for i in range(n): for j in range(n): if i != j: for k in range(len(words[j])): ...
find-the-shortest-superstring
[Python3] travelling sales person (TSP)
ye15
1
441
find the shortest superstring
943
0.448
Hard
15,303
https://leetcode.com/problems/delete-columns-to-make-sorted/discuss/427225/Python3-6-line-96ms-beats-99-easy-to-understand
class Solution: def minDeletionSize(self, A: List[str]) -> int: zipped=list(map(list,zip(*A))) count=0 for item in zipped: if item!=sorted(item): count+=1 return count
delete-columns-to-make-sorted
Python3 6 line 96ms beats 99%, easy to understand
wangzi100
3
244
delete columns to make sorted
944
0.696
Easy
15,304
https://leetcode.com/problems/delete-columns-to-make-sorted/discuss/953723/Python-most-simple-O(NM)-time-O(1)-space
class Solution: def minDeletionSize(self, A: List[str]) -> int: res = 0 for pos in range(len(A[0])): for word in range(len(A)-1): if A[word][pos] > A[word+1][pos]: res += 1 break return res
delete-columns-to-make-sorted
Python most simple O(NM) time O(1) space
IKM98
2
223
delete columns to make sorted
944
0.696
Easy
15,305
https://leetcode.com/problems/delete-columns-to-make-sorted/discuss/382127/Solution-in-Python-3-(beats-~100)-(one-line)
class Solution: def minDeletionSize(self, A: List[str]) -> int: return sum(list(i) != sorted(i) for i in zip(*A)) - Junaid Mansuri (LeetCode ID)@hotmail.com
delete-columns-to-make-sorted
Solution in Python 3 (beats ~100%) (one line)
junaidmansuri
2
509
delete columns to make sorted
944
0.696
Easy
15,306
https://leetcode.com/problems/delete-columns-to-make-sorted/discuss/1309418/CLEANEST-CODE-no-extra-space-just-check-each-column-manually-whether-it-is-unsorted-%3A)
class Solution: def minDeletionSize(self, strs: List[str]) -> int: C = len(strs[0]) res = 0 for col in range(C): unsorted_flag = False for si in range(1, len(strs)): if strs[si][col] < strs[si-1][col]: unso...
delete-columns-to-make-sorted
CLEANEST CODE no extra space, just check each column manually whether it is unsorted :)
yozaam
1
91
delete columns to make sorted
944
0.696
Easy
15,307
https://leetcode.com/problems/delete-columns-to-make-sorted/discuss/2828234/Solution-without-sorting-oror-99.5-Faster
class Solution: def minDeletionSize(self, strs: List[str]) -> int: length = len(strs[0]) res= ''.join(strs) count =0 for idx in range(length): index=idx while(index+length<len(res)): if(ord(res[index])>ord(res[index+length])): ...
delete-columns-to-make-sorted
Solution without sorting || 99.5 % Faster
hasan2599
0
2
delete columns to make sorted
944
0.696
Easy
15,308
https://leetcode.com/problems/delete-columns-to-make-sorted/discuss/2804920/Simple-Python-Solution
class Solution: def minDeletionSize(self, strs: List[str]) -> int: n = len(strs[0]) l =[] j =0 #return n while j < n: a =[] for i in strs: a.append(i[j]) l.append(a) j+=1 c =0 for i in l: ...
delete-columns-to-make-sorted
Simple Python Solution
Shagun_Mittal
0
2
delete columns to make sorted
944
0.696
Easy
15,309
https://leetcode.com/problems/delete-columns-to-make-sorted/discuss/2668287/Easy-approach
class Solution: def minDeletionSize(self, strs: List[str]) -> int: m, n = len(strs),len(strs[0]) ret = 0 for j in range(n): for i in range(m-1): if strs[i][j]> strs[i+1][j]: ret+=1 break return ret
delete-columns-to-make-sorted
Easy approach
prakhar_28581
0
3
delete columns to make sorted
944
0.696
Easy
15,310
https://leetcode.com/problems/delete-columns-to-make-sorted/discuss/2572849/Easy-Python-Solution
class Solution: def minDeletionSize(self, strs: List[str]) -> int: width = len(strs[0]) height = len(strs) unsorted = 0 for col in range(width): for row in range(1, height): if strs[row-1][col] > strs[row][col]: unsorted += 1 ...
delete-columns-to-make-sorted
Easy Python Solution
complete_noob
0
25
delete columns to make sorted
944
0.696
Easy
15,311
https://leetcode.com/problems/delete-columns-to-make-sorted/discuss/2367150/Readable-python-solution-hashmap
class Solution: def minDeletionSize(self, strs: List[str]) -> int: cols={} l=len(strs) l_s = len(strs[0]) delete = set() for i in range(l): for col in range(l_s): if col in cols: if cols[col]>strs[i][col]: ...
delete-columns-to-make-sorted
Readable python solution- hashmap
sunakshi132
0
27
delete columns to make sorted
944
0.696
Easy
15,312
https://leetcode.com/problems/delete-columns-to-make-sorted/discuss/1975052/Easiest-and-Simplest-Python-3-Solution-or-100-Faster-or-Beginner-Friendly-or-List-Operation
class Solution: def minDeletionSize(self, strs: List[str]) -> int: k=0 ss="" temp=[] ct=0 while k!=len(strs[0]): ss="" for i in range(len(strs)): ss=ss+strs[i][k] temp.append(ss) k+=1 for i in temp: ...
delete-columns-to-make-sorted
Easiest & Simplest Python 3 Solution | 100% Faster | Beginner-Friendly | List Operation
RatnaPriya
0
89
delete columns to make sorted
944
0.696
Easy
15,313
https://leetcode.com/problems/delete-columns-to-make-sorted/discuss/1952767/Python-beats-100-and-EASY
class Solution: def minDeletionSize(self, strs: List[str]) -> int: ans = 0 for i in zip(*strs): if list(i) != sorted(i): ans += 1 return ans
delete-columns-to-make-sorted
Python beats 100% & EASY
psplove80048
0
62
delete columns to make sorted
944
0.696
Easy
15,314
https://leetcode.com/problems/delete-columns-to-make-sorted/discuss/1952677/easy-python-code
class Solution: def minDeletionSize(self, strs: List[str]) -> int: count = 0 for i in range(len(strs[0])): a = [] for j in range(len(strs)): a.append(strs[j][i]) if len(a)>1: if a[-1]<a[-2]: count += ...
delete-columns-to-make-sorted
easy python code
dakash682
0
46
delete columns to make sorted
944
0.696
Easy
15,315
https://leetcode.com/problems/delete-columns-to-make-sorted/discuss/1905313/Python-beginner-friendly-solution-using-2-loops-and-sorting
class Solution: def minDeletionSize(self, strs: List[str]) -> int: count = 0 for i in range(len(strs[0])): temp = "" for j in range(len(strs)): temp += strs[j][i] if ''.join(sorted(temp)) != temp: count += 1 return count
delete-columns-to-make-sorted
Python beginner friendly solution using 2 loops and sorting
alishak1999
0
36
delete columns to make sorted
944
0.696
Easy
15,316
https://leetcode.com/problems/delete-columns-to-make-sorted/discuss/1772086/python-3-easy-solution
class Solution: def minDeletionSize(self, strs: List[str]) -> int: n = len(strs) res = 0 for col in zip(*strs): for i in range(n - 1): if col[i] > col[i+1]: res += 1 break return res
delete-columns-to-make-sorted
python 3, easy solution
dereky4
0
104
delete columns to make sorted
944
0.696
Easy
15,317
https://leetcode.com/problems/delete-columns-to-make-sorted/discuss/1450068/Zip-2-times-73-speed
class Solution: def minDeletionSize(self, strs: List[str]) -> int: count = 0 for col in zip(*strs): for a, b in zip(col, col[1:]): if a > b: count += 1 break return count
delete-columns-to-make-sorted
Zip 2 times, 73% speed
EvgenySH
0
90
delete columns to make sorted
944
0.696
Easy
15,318
https://leetcode.com/problems/delete-columns-to-make-sorted/discuss/1173829/Python-pythonic
class Solution: def minDeletionSize(self, strs: List[str]) -> int: result = 0 for i in range(len(strs[0])): temp = [x[i] for x in strs] result += 0 if temp == sorted(temp) else 1 return result
delete-columns-to-make-sorted
[Python] pythonic
cruim
0
59
delete columns to make sorted
944
0.696
Easy
15,319
https://leetcode.com/problems/delete-columns-to-make-sorted/discuss/1040023/Python3-easy-solution-using-%22zip%22-and-%22set%22
class Solution: def minDeletionSize(self, A: List[str]) -> int: d = set() A = list(zip(*A)) for i in range(len(A)): if not sorted(A[i]) == list(A[i]): d.add(i) return len(d)
delete-columns-to-make-sorted
Python3 easy solution using "zip" and "set"
EklavyaJoshi
0
66
delete columns to make sorted
944
0.696
Easy
15,320
https://leetcode.com/problems/delete-columns-to-make-sorted/discuss/606424/Intuitive-solution-and-easy-to-understand
class Solution: def minDeletionSize(self, A: List[str]) -> int: d = 0 for t in zip(*A): # Confirm if all chars are follow increasing order c = t[0] for nc in t[1:]: if nc < c: # Found decreasing and break d +...
delete-columns-to-make-sorted
Intuitive solution and easy to understand
puremonkey2001
0
59
delete columns to make sorted
944
0.696
Easy
15,321
https://leetcode.com/problems/delete-columns-to-make-sorted/discuss/587921/Simple-Python-Solution
class Solution: def minDeletionSize(self, A: List[str]) -> int: deletionIndices=[] x=[] length=len(A[0]) counter=-1 for i in range(length): check=[] acheck=[] for j in range(len(A)): check.append(A[j][i]) ...
delete-columns-to-make-sorted
Simple Python Solution
Ayu-99
0
86
delete columns to make sorted
944
0.696
Easy
15,322
https://leetcode.com/problems/minimum-increment-to-make-array-unique/discuss/1897470/PYTHON-SOL-oror-WELL-EXPLAINED-oror-SORTING-ororGREEDYoror-APPROACH-EXPLAINED-oror-SIMPLE-oror-O(n*log(n))oror
class Solution: def minIncrementForUnique(self, nums: List[int]) -> int: nums.sort() n = len(nums) ans = 0 for i in range(1,n): if nums[i] <= nums[i-1]: # this is the case for making item unique diff = nums[i-1] + 1 - nums[i] ...
minimum-increment-to-make-array-unique
PYTHON SOL || WELL EXPLAINED || SORTING ||GREEDY|| APPROACH EXPLAINED || SIMPLE || O(n*log(n))||
reaper_27
8
271
minimum increment to make array unique
945
0.504
Medium
15,323
https://leetcode.com/problems/minimum-increment-to-make-array-unique/discuss/1156808/O(n)-Python3-%3A-Easy-to-understand
class Solution: def minIncrementForUnique(self, A: List[int]) -> int: A.sort() count = 0 for i in range(1, len(A)): cur = A[i] prev = A[i-1] if(prev >= cur ): A[i] = prev + 1 count += prev - cur + 1 return count
minimum-increment-to-make-array-unique
O(n) Python3 : Easy to understand
abhijeetmallick29
1
197
minimum increment to make array unique
945
0.504
Medium
15,324
https://leetcode.com/problems/minimum-increment-to-make-array-unique/discuss/2336640/Using-Bool-Array
class Solution: def minIncrementForUnique(self, nums: List[int]) -> int: map_array = [False]*(10**6); for i in range(len(nums)): map_array[nums[i]] = True; nums.sort(); moves_count = 0; prev_max = 0; for i in range(...
minimum-increment-to-make-array-unique
Using Bool Array
DUMMY_BUG
0
22
minimum increment to make array unique
945
0.504
Medium
15,325
https://leetcode.com/problems/minimum-increment-to-make-array-unique/discuss/2097620/Python3-or-Simple-and-short-solution-or-O(nlogn)-or-O(1)-space
class Solution: def minIncrementForUnique(self, nums: List[int]) -> int: nums.sort() count = 0 for i in range(1, len(nums)): if nums[i]<=nums[i-1]: count+= (nums[i-1]+1 - nums[i]) nums[i] = nums[i-1]+1 return count
minimum-increment-to-make-array-unique
Python3 | Simple and short solution | O(nlogn) | O(1) space
HemantRana
0
39
minimum increment to make array unique
945
0.504
Medium
15,326
https://leetcode.com/problems/minimum-increment-to-make-array-unique/discuss/2068756/Python3-easy-sort-and-then-iterate
class Solution: def minIncrementForUnique(self, nums: List[int]) -> int: nums.sort() n = len(nums) moves = 0 prev = nums[0] for i in range(1, n): if nums[i] <= prev: moves = moves + prev +1 - nums[i] nums[i] = prev +1 ...
minimum-increment-to-make-array-unique
Python3 easy sort and then iterate
emerald19
0
66
minimum increment to make array unique
945
0.504
Medium
15,327
https://leetcode.com/problems/minimum-increment-to-make-array-unique/discuss/1687157/Python-or-Stack
class Solution: def minIncrementForUnique(self, nums: List[int]) -> int: nums.sort() new=nums[0]+1 stk=set({nums[0]}) ans=0 for n in nums[1:]: if n not in stk: stk.add(n) new=n+1 else: ans+=new-n ...
minimum-increment-to-make-array-unique
Python | Stack
heckt27
0
38
minimum increment to make array unique
945
0.504
Medium
15,328
https://leetcode.com/problems/minimum-increment-to-make-array-unique/discuss/1510656/Python3-Solution-with-using-sorting
class Solution: def minIncrementForUnique(self, nums: List[int]) -> int: nums.sort() cnt = 0 for idx in range(1, len(nums)): prev = nums[idx - 1] if prev >= nums[idx]: cnt += prev - nums[idx] + 1 nums[idx] = prev + 1 ...
minimum-increment-to-make-array-unique
[Python3] Solution with using sorting
maosipov11
0
110
minimum increment to make array unique
945
0.504
Medium
15,329
https://leetcode.com/problems/minimum-increment-to-make-array-unique/discuss/959042/Python3-greedy-O(NlogN)
class Solution: def minIncrementForUnique(self, A: List[int]) -> int: ans = cap = 0 for x in sorted(A): ans += max(0, cap - x) cap = max(cap, x) + 1 return ans
minimum-increment-to-make-array-unique
[Python3] greedy O(NlogN)
ye15
0
87
minimum increment to make array unique
945
0.504
Medium
15,330
https://leetcode.com/problems/minimum-increment-to-make-array-unique/discuss/405471/Python-3-(beats-~99)-(three-lines)
class Solution: def minIncrementForUnique(self, A: List[int]) -> int: m, c, _ = -math.inf, 0, A.sort() for a in A: if a <= m: c += 1 + m - a m += 1 else: m = a return c
minimum-increment-to-make-array-unique
Python 3 (beats ~99%) (three lines)
junaidmansuri
-2
536
minimum increment to make array unique
945
0.504
Medium
15,331
https://leetcode.com/problems/minimum-increment-to-make-array-unique/discuss/405471/Python-3-(beats-~99)-(three-lines)
class Solution: def minIncrementForUnique(self, A: List[int]) -> int: m, c, _ = -math.inf, 0, A.sort() for a in A: (c, m) = (c + (1 + m - a), m + 1) if a <= m else (c, a) return c - Junaid Mansuri
minimum-increment-to-make-array-unique
Python 3 (beats ~99%) (three lines)
junaidmansuri
-2
536
minimum increment to make array unique
945
0.504
Medium
15,332
https://leetcode.com/problems/validate-stack-sequences/discuss/1106110/Easy-python-solution-or-86-memory-86-time
class Solution: def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool: stack = [] for i in pushed: stack.append(i) while stack and popped and stack[-1] == popped[0]: stack.pop() popped.pop(0) return not stack
validate-stack-sequences
Easy python solution | 86% memory 86% time
vanigupta20024
6
259
validate stack sequences
946
0.676
Medium
15,333
https://leetcode.com/problems/validate-stack-sequences/discuss/1854875/python3-stack-solution-with-try-and-except
class Solution: def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool: s = [] ipush = 0 ipop = 0 try: while ipush < len(pushed) or ipop < len(popped): if len(s) == 0 or (len(s) != 0 and s[-1] != popped[ipop]): ...
validate-stack-sequences
python3 stack solution with try and except
DheerajGadwala
2
17
validate stack sequences
946
0.676
Medium
15,334
https://leetcode.com/problems/validate-stack-sequences/discuss/1853257/Readable-Python-solution
class Solution: def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool: stack = [] for num in pushed: stack.append(num) while len(stack) > 0 and stack[-1] == popped[0]: stack.pop() popped.pop(0) ...
validate-stack-sequences
Readable Python solution
zebra-f
2
33
validate stack sequences
946
0.676
Medium
15,335
https://leetcode.com/problems/validate-stack-sequences/discuss/1853628/python3or-fastest-solution
class Solution: def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool: stack=[] index=0 n=len(pushed) for i in pushed: stack.append(i) while len(stack)>0 and index<n and popped[index]==stack[-1]: stack.pop(...
validate-stack-sequences
python3| fastest solution
Anilchouhan181
1
56
validate stack sequences
946
0.676
Medium
15,336
https://leetcode.com/problems/validate-stack-sequences/discuss/1220344/Python-Solution
class Solution: def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool: stack, pushed, popped = [], collections.deque(pushed), collections.deque(popped) while pushed or popped: try: while not stack or stack[-1] != ...
validate-stack-sequences
Python Solution
dev-josh
1
64
validate stack sequences
946
0.676
Medium
15,337
https://leetcode.com/problems/validate-stack-sequences/discuss/2845458/python-easy-to-understand
class Solution: def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool: tmp, i = [],0 for num in pushed: tmp.append(num) #if tmp is not empty and the last item of tmp is equal to popped[i], #then it means the item of popped[i] can be popped for thi...
validate-stack-sequences
python easy to understand
xiaolaotou
0
1
validate stack sequences
946
0.676
Medium
15,338
https://leetcode.com/problems/validate-stack-sequences/discuss/2831764/Python-Solution-Stack-Approach-oror-Easy-Understanding
class Solution: def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool: #this is what happening in examples -> if stack[-1]==popped[0] then do pop in stack and popped till they are same stack=[] j=0 for i in pushed: stack.append(i) ...
validate-stack-sequences
Python Solution -Stack Approach || Easy- Understanding
T1n1_B0x1
0
1
validate stack sequences
946
0.676
Medium
15,339
https://leetcode.com/problems/validate-stack-sequences/discuss/2830413/Python.-Stack-solution-with-explanation.
class Solution: def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool: pushed = pushed[::-1] stack = [] for i in popped: if stack and stack[-1] == i: stack.pop() else: while pushed: cur = pus...
validate-stack-sequences
Python. Stack solution with explanation.
ebarykin
0
9
validate stack sequences
946
0.676
Medium
15,340
https://leetcode.com/problems/validate-stack-sequences/discuss/2241045/O(1)-space
class Solution: def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool: i=0 j=0 for e in pushed: pushed[i]=e while i>=0 and popped[j]==pushed[i]: j+=1 i-=1 i+=1 return i==0
validate-stack-sequences
O(1) space
HaoChenNus
0
9
validate stack sequences
946
0.676
Medium
15,341
https://leetcode.com/problems/validate-stack-sequences/discuss/2043454/Python-simulation
class Solution: def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool: stack = [] i = j = 0 while i < len(pushed) or j < len(popped): if stack and j < len(popped) and stack[-1] == popped[j]: j += 1 stack.pop() ...
validate-stack-sequences
Python, simulation
blue_sky5
0
26
validate stack sequences
946
0.676
Medium
15,342
https://leetcode.com/problems/validate-stack-sequences/discuss/1903682/Python-easy-to-read-and-understand-or-stack
class Solution: def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool: stack = [] while popped: if stack and popped[0] == stack[-1]: popped.pop(0) stack.pop() elif pushed: stack.append(pushed.po...
validate-stack-sequences
Python easy to read and understand | stack
sanial2001
0
50
validate stack sequences
946
0.676
Medium
15,343
https://leetcode.com/problems/validate-stack-sequences/discuss/1897500/PYTHON-SOL-oror-EXPLAINED-WELL-oror-STACK-oror-LINEAR-TIME-AND-SPACE-oror
class Solution: def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool: stack = [] idx = 0 for i in pushed: stack.append(i) while stack and popped[idx] == stack[-1]: stack.pop() idx += 1 return stack == [...
validate-stack-sequences
PYTHON SOL || EXPLAINED WELL || STACK || LINEAR TIME AND SPACE ||
reaper_27
0
21
validate stack sequences
946
0.676
Medium
15,344
https://leetcode.com/problems/validate-stack-sequences/discuss/1856281/Tricked-Solution
class Solution: def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool: rel=["k"] popped.append("z") popped[:]=popped[::-1] #by this, we can use as a stack for i in pushed: rel.append(i) while rel[-1]==popped[-1]: #k a...
validate-stack-sequences
Tricked Solution
AjayKadiri
0
6
validate stack sequences
946
0.676
Medium
15,345
https://leetcode.com/problems/validate-stack-sequences/discuss/1856214/Python3-95.52-or-Stack-Simulation-(Greedy)-or-Easy-Implementaion-and-Commented
class Solution: def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool: # simulation stack st = [] # simulation queue q = deque(popped) # greedy cancel (pop st and q) the sequences when you can for ele in pushed: st.append...
validate-stack-sequences
Python3 95.52% | Stack Simulation (Greedy) | Easy Implementaion & Commented
doneowth
0
10
validate stack sequences
946
0.676
Medium
15,346
https://leetcode.com/problems/validate-stack-sequences/discuss/1856185/Python-or-64-ms-or-Stack
class Solution: def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool: pushed_size = len(pushed) popped_index = 0 pushed_index = 0 stack = [] for i in range(pushed_size * 2): if not stack: stack.append(pushed[pushe...
validate-stack-sequences
Python | 64 ms | Stack
chinjiec
0
6
validate stack sequences
946
0.676
Medium
15,347
https://leetcode.com/problems/validate-stack-sequences/discuss/1855648/Simple-Python3-Solution
class Solution: def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool: pop_idx, stack = 0, [] for push_val in pushed: stack.append(push_val) while len(stack) and stack[-1] == popped[pop_idx]: stack.pop() pop_id...
validate-stack-sequences
Simple Python3 Solution
user6774u
0
12
validate stack sequences
946
0.676
Medium
15,348
https://leetcode.com/problems/validate-stack-sequences/discuss/1854800/PYTHON-STACK-solution-step-by-step-(76ms)
class Solution: def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool: LENGTH = len ( pushed ); #Putting in a sentinel at the end to prevent index error with pointers pushed.append( - 1 ); popped.append( - 1 ); #Cre...
validate-stack-sequences
PYTHON STACK solution step-by-step (76ms)
greg_savage
0
10
validate stack sequences
946
0.676
Medium
15,349
https://leetcode.com/problems/validate-stack-sequences/discuss/1854527/Python-stack-solution
class Solution: def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool: ans = [] i = 0 for j in pushed: ans.append(j) # print(i,j,ans,popped) while i<len(popped) and len(ans)!=0 and popped[i]==ans[-1]: ...
validate-stack-sequences
Python stack solution
Brillianttyagi
0
7
validate stack sequences
946
0.676
Medium
15,350
https://leetcode.com/problems/validate-stack-sequences/discuss/1854384/Easy-to-Understand-Python3-solution
class Solution: def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool: new_arr = [] while pushed or popped: if popped and new_arr: if popped[0] == new_arr[-1]: new_arr.pop(-1) popped.pop(0) ...
validate-stack-sequences
Easy to Understand Python3 solution
shubham3
0
4
validate stack sequences
946
0.676
Medium
15,351
https://leetcode.com/problems/validate-stack-sequences/discuss/1854347/Python-or-Easy-solution-with-commented-explanation
class Solution: def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool: stack=[] #As the question says that pushed and popped have same length, then pushed will get empty first after all operations while len(pushed)!=0: stack.append(pushed.pop(0)) #Start push...
validate-stack-sequences
Python | Easy solution with commented explanation
RickSanchez101
0
12
validate stack sequences
946
0.676
Medium
15,352
https://leetcode.com/problems/validate-stack-sequences/discuss/1854203/Python3-Simple-Stack-solution
class Solution: def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool: st = [] popped.reverse() for i in range(len(pushed)): if pushed[i] == popped[-1]: popped.pop() while st and st[-1] == popped[-1]: st...
validate-stack-sequences
[Python3] Simple Stack solution
nandhakiran366
0
7
validate stack sequences
946
0.676
Medium
15,353
https://leetcode.com/problems/validate-stack-sequences/discuss/1854145/Python3-Solution-with-using-stack
class Solution: def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool: it = 0 stack = [] for elem in pushed: stack.append(elem) while stack and it < len(popped) and stack[-1] == popped[it]: stack.pop() ...
validate-stack-sequences
[Python3] Solution with using stack
maosipov11
0
9
validate stack sequences
946
0.676
Medium
15,354
https://leetcode.com/problems/validate-stack-sequences/discuss/1854046/Python-Very-Easy-O(N)
class Solution: def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool: stack, b = [], 0 for i in popped: if stack and stack[-1] == i: stack.pop() continue while b < len(pushed) and pushed[b] != i: stack...
validate-stack-sequences
✅ Python Very Easy O(N)
dhananjay79
0
14
validate stack sequences
946
0.676
Medium
15,355
https://leetcode.com/problems/validate-stack-sequences/discuss/1853960/Python-Easy-to-understand-O(1)-Space-and-Faster-Than-95
class Solution: def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool: stack=list() i=0 j=0 n=len(pushed) m=len(popped) while(i<n): stack.append(pushed[i]) i+=1 ...
validate-stack-sequences
Python Easy to understand O(1) Space and Faster Than 95%
bhawesh_bhashkar
0
9
validate stack sequences
946
0.676
Medium
15,356
https://leetcode.com/problems/validate-stack-sequences/discuss/1853885/Self-Understandable-Python-(2-methods)-%3A
class Solution: def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool: stack=[] i=0 for p in pushed: stack.append(p) while stack and stack[-1]==popped[i]: stack.pop() i+=1 if stack: ...
validate-stack-sequences
Self Understandable Python (2 methods) :
goxy_coder
0
34
validate stack sequences
946
0.676
Medium
15,357
https://leetcode.com/problems/validate-stack-sequences/discuss/1853885/Self-Understandable-Python-(2-methods)-%3A
class Solution: def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool: if pushed==popped or pushed==popped[::-1]: return True i,j=0,0 stack=[] m=0 while i<=len(pushed) and j<len(popped): if m==i: # item can be p...
validate-stack-sequences
Self Understandable Python (2 methods) :
goxy_coder
0
34
validate stack sequences
946
0.676
Medium
15,358
https://leetcode.com/problems/validate-stack-sequences/discuss/1853761/Python-3-or-Easy-Solution-or-O(N)
class Solution: def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool: pop_idx, stack = 0, [] for push_val in pushed: stack.append(push_val) while len(stack) and stack[-1] == popped[pop_idx]: stack.pop() pop_id...
validate-stack-sequences
Python 3 | Easy Solution | O(N)
sathwickreddy
0
20
validate stack sequences
946
0.676
Medium
15,359
https://leetcode.com/problems/validate-stack-sequences/discuss/1853672/Python-or-TC-O(N)SC-O(N)-or-EASY-Code-or-Faster-than-97-or
class Solution(object): def validateStackSequences(self, pushed, popped): """ :type pushed: List[int] :type popped: List[int] :rtype: bool """ popIdx = 0 stack = [] for push in pushed: stack.append(push) while ...
validate-stack-sequences
Python | TC-O(N)/SC-O(N) | EASY Code | Faster than 97% |
Patil_Pratik
0
9
validate stack sequences
946
0.676
Medium
15,360
https://leetcode.com/problems/validate-stack-sequences/discuss/1853330/Python-Simple-Python-Solution-Using-Stack-and-Iterative-Approach
class Solution: def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool: stack = [] i , j = 0 , 0 while i<len(pushed): stack.append(pushed[i]) while stack and j<len(popped) and stack[-1]==popped[j]: stack.pop() j=j+1 i=i+1 if j == len(popped): return True ...
validate-stack-sequences
[ Python ] ✔✔ Simple Python Solution Using Stack and Iterative Approach 🔥✌
ASHOK_KUMAR_MEGHVANSHI
0
28
validate stack sequences
946
0.676
Medium
15,361
https://leetcode.com/problems/validate-stack-sequences/discuss/1853191/Python-Easy-understand-illustrate-with-comment
class Solution: def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool: val = [pushed.pop(0)] # val = list() is more clean but more one time loop in while while pushed or popped: # two list have value if val and val[-1] == popped[0] : # if last ...
validate-stack-sequences
[Python] Easy-understand illustrate with comment
crazypuppy
0
14
validate stack sequences
946
0.676
Medium
15,362
https://leetcode.com/problems/validate-stack-sequences/discuss/1853168/Python-Solution
class Solution: def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool: my_res = [] for i in pushed: my_res.append(i) while my_res and my_res[-1] == popped[0]: popped = popped[1:] my_res.pop() return (my_res) == ...
validate-stack-sequences
✔Python Solution
Coding_Tan3
0
10
validate stack sequences
946
0.676
Medium
15,363
https://leetcode.com/problems/validate-stack-sequences/discuss/1853161/Elixir-and-Python3-Simple-Pattern-Matching-Solution
class Solution: def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool: u_h, *u_t = pushed return self.test(u_t, popped, [u_h]) def test(self, pushed, popped, test): l_u, l_o, l_t = len(pushed), len(popped), len(test) if l_u == 0 and l_o == 0 and l_t == 0: ...
validate-stack-sequences
Elixir and Python3, Simple Pattern-Matching Solution
bpfaust
0
14
validate stack sequences
946
0.676
Medium
15,364
https://leetcode.com/problems/validate-stack-sequences/discuss/1853115/Easy-Intuitive-Solution-in-Python
class Solution: def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool: s1 = [] i = 0 for p in pushed: s1.append(p) while s1 and s1[-1] == popped[i]: s1.pop() i += 1 if not s1: return True else: return False
validate-stack-sequences
Easy Intuitive Solution in Python
Jazzyb1999
0
16
validate stack sequences
946
0.676
Medium
15,365
https://leetcode.com/problems/validate-stack-sequences/discuss/1853112/Python-Stack-or-Beats-98
class Solution: def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool: stack = [] pop_index = 0 for i in pushed: stack.append(i) while stack and stack[-1] == popped[pop_index]: stack.pop() pop_index += ...
validate-stack-sequences
[Python] Stack | Beats 98%
tejeshreddy111
0
28
validate stack sequences
946
0.676
Medium
15,366
https://leetcode.com/problems/validate-stack-sequences/discuss/1365420/Not-the-Fastest-but-easy-understand-with-explanation-(python3)
class Solution: def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool: ans = [] # If "popped" is not empty, we keep worknig on it. while len(popped) != 0: # If ans is empty, we should append the first element from the "pushed". # Also, we must...
validate-stack-sequences
Not the Fastest, but easy understand with explanation (python3)
An_222
0
69
validate stack sequences
946
0.676
Medium
15,367
https://leetcode.com/problems/validate-stack-sequences/discuss/1326038/Python3-solution-single-pass-using-stack
class Solution: def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool: x = -1 l = [] for i in popped: if i not in l: a = pushed.index(i) l += pushed[x+1:a] x = a else: if l[-1] ==...
validate-stack-sequences
Python3 solution single pass using stack
EklavyaJoshi
0
44
validate stack sequences
946
0.676
Medium
15,368
https://leetcode.com/problems/validate-stack-sequences/discuss/1083998/python-on-solution-using-stack
class Solution: def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool: j = 0 stack,output = [],[] for i in range(len(pushed)): if pushed[i] != popped[j]: stack.append(pushed[i]) elif pushed[i] == popped[j]: j +=...
validate-stack-sequences
python on solution using stack
yingziqing123
0
33
validate stack sequences
946
0.676
Medium
15,369
https://leetcode.com/problems/validate-stack-sequences/discuss/1083594/Python-or-Fast-and-Easy-or-Beats-99
class Solution: def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool: l = deque() for i in pushed: l.append(i) while l and popped and l[-1] == popped[0]: popped.pop(0) l.pop() return not popped
validate-stack-sequences
Python | Fast & Easy | Beats 99%
SlavaHerasymov
0
45
validate stack sequences
946
0.676
Medium
15,370
https://leetcode.com/problems/validate-stack-sequences/discuss/1055505/Easy-python-solution-O(N)-runtime
class Solution: def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool: stack = [] for i in pushed: while stack and stack[-1] == popped[0]: stack.pop() popped.pop(0) stack.append(i) while stack and stack[-1] == p...
validate-stack-sequences
Easy python solution - O(N) runtime
Ayush87
0
40
validate stack sequences
946
0.676
Medium
15,371
https://leetcode.com/problems/validate-stack-sequences/discuss/1049309/Python-stack-solution
class Solution: def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool: stack=[] curr=0 for i in range(len(pushed)): stack.append(pushed[i]) while stack and stack[-1]==popped[curr]: stack.pop() curr+=1 return not stack
validate-stack-sequences
Python stack solution
samarthnehe
0
31
validate stack sequences
946
0.676
Medium
15,372
https://leetcode.com/problems/validate-stack-sequences/discuss/966373/Python3-stack-O(N)
class Solution: def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool: i = 0 stack = [] for x in pushed: stack.append(x) while stack and stack[-1] == popped[i]: stack.pop() i += 1 return not stack
validate-stack-sequences
[Python3] stack O(N)
ye15
0
40
validate stack sequences
946
0.676
Medium
15,373
https://leetcode.com/problems/most-stones-removed-with-same-row-or-column/discuss/1689443/For-Beginners-oror-Count-Number-of-Connected-Graphs-O(N)-oror-94-Faster
class Solution: def removeStones(self, stones: List[List[int]]) -> int: def remove_point(a,b): # Function to remove connected points from the ongoing graph. points.discard((a,b)) for y in x_dic[a]: if (a,y) in points: remove_point(a,y) for x in y_dic[b]: ...
most-stones-removed-with-same-row-or-column
📌📌 For Beginners || Count Number of Connected Graphs O(N) || 94% Faster 🐍
abhi9Rai
22
1,500
most stones removed with same row or column
947
0.588
Medium
15,374
https://leetcode.com/problems/most-stones-removed-with-same-row-or-column/discuss/1900212/PYTHON-SOL-oror-WELL-EXPLAINED-oror-FASTER-THAN-97-oror-APPROACH-EXPLAINED-oror-DFS-oror
class Solution: def removeStones(self, stones: List[List[int]]) -> int: n = len(stones) visited = [False]*n rows = defaultdict(list) cols = defaultdict(list) ans = 0 for i,point in enumerate(stones): rows[point[0]].append(i) cols[point[1]].appe...
most-stones-removed-with-same-row-or-column
PYTHON SOL || WELL EXPLAINED || FASTER THAN 97% || APPROACH EXPLAINED || DFS ||
reaper_27
4
472
most stones removed with same row or column
947
0.588
Medium
15,375
https://leetcode.com/problems/most-stones-removed-with-same-row-or-column/discuss/2488227/python-3-or-clean-union-find
class Solution: def removeStones(self, stones: List[List[int]]) -> int: n = len(stones) rank = [1] * n parent = [i for i in range(n)] def union(i, j): i, j = find(i), find(j) if i == j: return 0 if rank[i] < rank[j]: ...
most-stones-removed-with-same-row-or-column
python 3 | clean union find
dereky4
2
404
most stones removed with same row or column
947
0.588
Medium
15,376
https://leetcode.com/problems/most-stones-removed-with-same-row-or-column/discuss/1659025/BFS-%2B-hashmap
class Solution: def removeStones(self, stones: List[List[int]]) -> int: # create a completed graph, use graphX to store the node with same x axis and use graphY to store the node with the same y axis # Use BFS # Use visited to store the passed node # for one conneted graph count the...
most-stones-removed-with-same-row-or-column
BFS + hashmap
JackYeh17
2
298
most stones removed with same row or column
947
0.588
Medium
15,377
https://leetcode.com/problems/most-stones-removed-with-same-row-or-column/discuss/2814392/Faster-than-98.5-less-memory-than-86.5
class Solution: def removeStones(self, stones: List[List[int]]) -> int: h=defaultdict(list) v=defaultdict(list) for i, s in enumerate(stones): h[s[0]].append(i) v[s[1]].append(i) l=len(stones) g=[[] for i in range(l)] vlist=[0]*l for i,...
most-stones-removed-with-same-row-or-column
Faster than 98.5%, less memory than 86.5%
mbeceanu
1
94
most stones removed with same row or column
947
0.588
Medium
15,378
https://leetcode.com/problems/most-stones-removed-with-same-row-or-column/discuss/2839263/dictionary-solution
class Solution: def removeStones(self, stones: List[List[int]]) -> int: xdict = {} ydict = {} for x, y in stones: if x not in xdict: xdict[x] = [y] else: xdict[x].append(y) if y not in ydict: ydict[y] = [x]...
most-stones-removed-with-same-row-or-column
dictionary solution
TrickyUnicorn
0
2
most stones removed with same row or column
947
0.588
Medium
15,379
https://leetcode.com/problems/most-stones-removed-with-same-row-or-column/discuss/2817348/Basic-Union-Find-method-to-solve-the-problem
class Solution: def removeStones(self, stones: List[List[int]]) -> int: p={} def find(m): if m!=p[m]: p[m]=find(p[m]) return p[m] def union(x,y): p.setdefault(x,x) p.setdefault(y,y) r1=find(x) r2=find(...
most-stones-removed-with-same-row-or-column
Basic Union Find method to solve the problem
Jai_Trivedi
0
9
most stones removed with same row or column
947
0.588
Medium
15,380
https://leetcode.com/problems/most-stones-removed-with-same-row-or-column/discuss/2816234/Python3-Solution-with-using-dfs
class Solution: def dfs(self, i, stones, visited): visited.add(i) for j in range(len(stones)): if (stones[i][0] == stones[j][0] or stones[i][1] == stones[j][1]) and j not in visited: self.dfs(j, stones, visited) def removeStones(self, stones: List[List[int]]) -> i...
most-stones-removed-with-same-row-or-column
[Python3] Solution with using dfs
maosipov11
0
11
most stones removed with same row or column
947
0.588
Medium
15,381
https://leetcode.com/problems/most-stones-removed-with-same-row-or-column/discuss/2815389/Python3-pictorial-explanation-with-thought-process-%2B-code
class Solution: def removeStones(self, stones: List[List[int]]) -> int: n = len(stones) freq = defaultdict(int) visited = set() def connect(i,src,label): visited.add(i) freq[label]+=1 for j in range(n): if j not in visited: ...
most-stones-removed-with-same-row-or-column
Python3 pictorial explanation with thought process + code
user9611y
0
4
most stones removed with same row or column
947
0.588
Medium
15,382
https://leetcode.com/problems/most-stones-removed-with-same-row-or-column/discuss/2815331/Python-or-Union-find-or-Approach-explained
class Solution: def removeStones(self, stones: List[List[int]]) -> int: root = [i for i in range(len(stones))] rank = [1] * len(stones) rows = collections.defaultdict(list) cols = collections.defaultdict(list) def unify_stones(): for i in range(len(stones)): ...
most-stones-removed-with-same-row-or-column
Python | Union find | Approach explained
xyp7x
0
4
most stones removed with same row or column
947
0.588
Medium
15,383
https://leetcode.com/problems/most-stones-removed-with-same-row-or-column/discuss/2815290/Python-Short-DFS-solution-with-graph-%2B-tuple
class Solution: def removeStones(self, stones: List[List[int]]) -> int: graph = collections.defaultdict(list) for i in range(len(stones)): for j in range(i + 1, len(stones)): (x1, y1), (x2, y2) = stones[i], stones[j] if x1 == x2 or y1 == y2: ...
most-stones-removed-with-same-row-or-column
[Python] Short DFS solution with graph + tuple
Nezuko-NoBamboo
0
8
most stones removed with same row or column
947
0.588
Medium
15,384
https://leetcode.com/problems/most-stones-removed-with-same-row-or-column/discuss/2815177/Python-DFS-solution-(connected-graph)
class Solution: def removeStones(self, stones: List[List[int]]) -> int: n = len(stones) adj = defaultdict(set) for i in range(n): r1, c1 = stones[i] for j in range(i+1, n): r2, c2 = stones[j] if r1 == r2 or c1 == c2: ...
most-stones-removed-with-same-row-or-column
Python DFS solution (connected graph)
zananpech9
0
9
most stones removed with same row or column
947
0.588
Medium
15,385
https://leetcode.com/problems/most-stones-removed-with-same-row-or-column/discuss/2815113/Python3-Easy-oror-BFS-oror-O(n)-oror-Apply-BFS-on-each-components-(explained)
class Solution: def removeStones(self, stones: List[List[int]]) -> int: row_dict = defaultdict(list) col_dict = defaultdict(list) for a,b in stones: row_dict[a].append(b) col_dict[b].append(a) visited = set() ans = 0 que = de...
most-stones-removed-with-same-row-or-column
🤩 Python3 Easy || BFS || O(n)🔥 || Apply BFS on each components (explained)
Laxman_Singh_Saini
0
10
most stones removed with same row or column
947
0.588
Medium
15,386
https://leetcode.com/problems/most-stones-removed-with-same-row-or-column/discuss/2814912/Union-Approach-Python3
class Solution: def removeStones(self, stones: List[List[int]]) -> int: #Use Unified method, for best practice parents = {} def find(x): if x != parents[x]: parents[x] = find(parents[x]) return parents[x] def union(x,y): pare...
most-stones-removed-with-same-row-or-column
Union Approach Python3
TheSeer507
0
11
most stones removed with same row or column
947
0.588
Medium
15,387
https://leetcode.com/problems/most-stones-removed-with-same-row-or-column/discuss/2814838/Python-(Faster-than-97)-or-Union-find
class Solution: def removeStones(self, stones: List[List[int]]) -> int: par = {} for r, c in stones: r = -(r + 1) c = c + 1 par[r] = r par[c] = c count = len(par) rank = {} def find(n): if n != par[n]: ...
most-stones-removed-with-same-row-or-column
Python (Faster than 97%) | Union find
KevinJM17
0
15
most stones removed with same row or column
947
0.588
Medium
15,388
https://leetcode.com/problems/most-stones-removed-with-same-row-or-column/discuss/2814692/Python-Simple-Python-Solution-Using-DFS
class Solution: def removeStones(self, stones: List[List[int]]) -> int: def NextStone(r,c): next_neighbour = [] index = 0 while index < len(stones): row , col = stones[index] if row == r or col == c: next_neighbour.append(stones.pop(index)) else: index = index + 1 for stone i...
most-stones-removed-with-same-row-or-column
[ Python ] ✅✅ Simple Python Solution Using DFS🥳✌👍
ASHOK_KUMAR_MEGHVANSHI
0
34
most stones removed with same row or column
947
0.588
Medium
15,389
https://leetcode.com/problems/most-stones-removed-with-same-row-or-column/discuss/2814563/Python-solution-with-explanation.
class Solution: def removeStones(self, stones: List[List[int]]) -> int: visited = set() def dfs(cell): if cell in visited: return 0 visited.add(cell) # If stone passes through above code, then it's a part of a new graph # and it's basi...
most-stones-removed-with-same-row-or-column
Python solution with explanation.
raghupalash
0
23
most stones removed with same row or column
947
0.588
Medium
15,390
https://leetcode.com/problems/most-stones-removed-with-same-row-or-column/discuss/2814488/Python-BFS-similar-to-rotting-oranges-problem
class Solution: def removeStones(self, A): A = [tuple(x) for x in A] # hashable 2D positions visited = {x: False for x in A} ans = 0 for x in A: if visited[x]: continue q = {x} while q: y = q.pop() # item in same row/colu...
most-stones-removed-with-same-row-or-column
Python BFS, similar to rotting oranges problem
kevv96
0
16
most stones removed with same row or column
947
0.588
Medium
15,391
https://leetcode.com/problems/most-stones-removed-with-same-row-or-column/discuss/2814477/Python3-solution
class Solution: def removeStones(self, stones: List[List[int]]) -> int: rows, cols = defaultdict(list), defaultdict(list) # [1] all stones within the same row for s, (r, c) in enumerate(stones): # and within the same column rows[r].append(s) ...
most-stones-removed-with-same-row-or-column
Python3 solution
avs-abhishek123
0
15
most stones removed with same row or column
947
0.588
Medium
15,392
https://leetcode.com/problems/most-stones-removed-with-same-row-or-column/discuss/2814132/Simple-DFS-beats-93
class Solution: def removeStones(self, stones: List[List[int]]) -> int: xs = defaultdict(list) ys = defaultdict(list) for x, y in stones: xs[x].append((x,y)) ys[y].append((x,y)) stones = set((x, y) for x, y in stones) l = len(stones) c = 0 ...
most-stones-removed-with-same-row-or-column
Simple DFS beats 93%
Mencibi
0
21
most stones removed with same row or column
947
0.588
Medium
15,393
https://leetcode.com/problems/most-stones-removed-with-same-row-or-column/discuss/2813592/Python3-Commented-DFS-Solution
class Solution: def removeStones(self, stones: List[List[int]]) -> int: # make the trivial case if len(stones) < 2: return 0 # save the indices of the stones rows = collections.defaultdict(list) cols = collections.defaultdict(list) for x,y in sto...
most-stones-removed-with-same-row-or-column
[Python3] - Commented DFS Solution
Lucew
0
19
most stones removed with same row or column
947
0.588
Medium
15,394
https://leetcode.com/problems/most-stones-removed-with-same-row-or-column/discuss/2813282/Python-or-DFS
class Solution: def removeStones(self, stones: List[List[int]]) -> int: visited = {} i = 0 for s in stones: visited[s[0], s[1]] = False for stone in stones: if not visited[stone[0], stone[1]]: self.dfs(stone, visited, stones) ...
most-stones-removed-with-same-row-or-column
Python | DFS
rahul_mishra_
0
22
most stones removed with same row or column
947
0.588
Medium
15,395
https://leetcode.com/problems/most-stones-removed-with-same-row-or-column/discuss/2812984/python3-Union-find-solution-for-reference
class Solution: def removeStones(self, stones: List[List[int]]) -> int: S = len(stones) parents = defaultdict(lambda: -1) b = [] ans = 0 def find(x): if parents[x] != -1: parents[x] = find(parents[x]) return parents[x] ...
most-stones-removed-with-same-row-or-column
[python3] Union find solution for reference
vadhri_venkat
0
16
most stones removed with same row or column
947
0.588
Medium
15,396
https://leetcode.com/problems/most-stones-removed-with-same-row-or-column/discuss/2812977/Most-stones-removed-with-same-row-or-column-position-solution-in-Python
class Solution: def removeStones(self, stones: List[List[int]]) -> int: def dfs(idx): seen.add(idx) for nidx in adj[idx]: if nidx not in seen: dfs(nidx) adj = defaultdict(set) for i in range(len(stones)): x1, y1 = stones...
most-stones-removed-with-same-row-or-column
Most stones removed with same row or column position solution in Python
prashantghi8
0
26
most stones removed with same row or column
947
0.588
Medium
15,397
https://leetcode.com/problems/most-stones-removed-with-same-row-or-column/discuss/2812960/Python3-Treat-like-connected-graph
class Solution: def removeStones(self, stones: List[List[int]]) -> int: d1, d2 = defaultdict(lambda: set()), {} for [R, C] in stones: r, c = R + 1, C + 1 k = (r << 14) + c d1[-r].add(k) d1[c].add(k) d2[k] = [-r, c] rc = len(stones)...
most-stones-removed-with-same-row-or-column
Python3 - Treat like connected graph
godshiva
0
9
most stones removed with same row or column
947
0.588
Medium
15,398
https://leetcode.com/problems/most-stones-removed-with-same-row-or-column/discuss/2812856/Python3-oror-Hashmap-DFS
class Solution: def removeStones(self, stones: List[List[int]]) -> int: self.vis = set() dtr, dtc = defaultdict(lambda: []), defaultdict(lambda: []) for stone in stones: dtr[stone[0]].append(stone[1]) dtc[stone[1]].append(stone[0]) self.res = 0 ...
most-stones-removed-with-same-row-or-column
Python3 || Hashmap DFS
rupamkarmakarcr7
0
15
most stones removed with same row or column
947
0.588
Medium
15,399