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/best-sightseeing-pair/discuss/2429468/python-oror-DP-oror-constant-space-oror-constant-time-oror-fast-oror
class Solution: def maxScoreSightseeingPair(self, values: List[int]) -> int: i = 0 score = 0 for j in range(1, len(values)): score = max(score, values[i] + values[j] + (i - j)) if values[j] >= values[i]: i = j elif values[i] - values[j] < j...
best-sightseeing-pair
python || DP || constant space || constant time || fast ||
Yared_betsega
0
48
best sightseeing pair
1,014
0.595
Medium
16,600
https://leetcode.com/problems/best-sightseeing-pair/discuss/2204185/Python3-or-Clear-Explanation-with-Illustration-or-Faster-than-93.63-or-Less-than-87.31
class Solution: def maxScoreSightseeingPair(self, values: List[int]) -> int: Max = res = 0 for i in range(1, len(values)): Max = max(Max-1, values[i-1]-1) res = max(res, values[i]+Max) return res
best-sightseeing-pair
✅[Python3] | Clear Explanation with Illustration | Faster than 93.63% | Less than 87.31%
chanchishen
0
35
best sightseeing pair
1,014
0.595
Medium
16,601
https://leetcode.com/problems/best-sightseeing-pair/discuss/2185730/python-3-or-very-simple-solution-or-O(n)O(1)
class Solution: def maxScoreSightseeingPair(self, values: List[int]) -> int: score = i = 0 for j in range(1, len(values)): score = max(score, values[i] + values[j] + i - j) if values[j] + j - i > values[i]: i = j return score
best-sightseeing-pair
python 3 | very simple solution | O(n)/O(1)
dereky4
0
80
best sightseeing pair
1,014
0.595
Medium
16,602
https://leetcode.com/problems/best-sightseeing-pair/discuss/2060075/Python-or-DP
class Solution: def maxScoreSightseeingPair(self, va: List[int]) -> int: ma = float("-inf") ans = float("-inf") for i in range(len(va)): if i!=0: ans = max(ans,va[i]-i+ma) ma = max(ma,va[i]+i) return ans
best-sightseeing-pair
Python | DP
Shivamk09
0
50
best sightseeing pair
1,014
0.595
Medium
16,603
https://leetcode.com/problems/best-sightseeing-pair/discuss/2007189/ororPYTHON-SOLoror-FASTER-THAN-99-oror-EASY-oror-LINEAR-TIME-oror-EXPLAINED-oror-INTUTIVE-oror
class Solution: def maxScoreSightseeingPair(self, values: List[int]) -> int: ans = 0 n = len(values) highest = values[0] for i in range(1,n): sub = values[i] - i if sub + highest > ans : ans = sub + highest if values[i] + i > highest: highest = val...
best-sightseeing-pair
||PYTHON SOL|| FASTER THAN 99% || EASY || LINEAR TIME || EXPLAINED || INTUTIVE ||
reaper_27
0
35
best sightseeing pair
1,014
0.595
Medium
16,604
https://leetcode.com/problems/best-sightseeing-pair/discuss/1898262/Python3%3A-DP-Solution-%3A-Easy-to-Understand
class Solution: def maxScoreSightseeingPair(self, values: List[int]) -> int: """ If you look closely on the requirement: it asks for chosing a previous index(i) from current index(j) by which the summation is maximum. So, for a current index(j), our task is to figure...
best-sightseeing-pair
Python3: DP Solution : Easy to Understand
showing_up_each_day
0
71
best sightseeing pair
1,014
0.595
Medium
16,605
https://leetcode.com/problems/best-sightseeing-pair/discuss/1849875/Python-One-Pass-or-Clean-Code
class Solution: def maxScoreSightseeingPair(self, values: List[int]) -> int: currScore = maxScore = 0 for i in range(len(values)-2,-1,-1): value1 = values[i] + values[i+1] -1 value2 = currScore - values[i+1] + values[i] -1 currSc...
best-sightseeing-pair
Python One Pass | Clean Code
deepanksinghal
0
46
best sightseeing pair
1,014
0.595
Medium
16,606
https://leetcode.com/problems/best-sightseeing-pair/discuss/1793193/Super-Clear-And-Easy-Understanding-Python3-Solution
class Solution: def maxScoreSightseeingPair(self, values: List[int]) -> int: ans=0 curMax=values[0]-1 for i in range(1,len(values)): ans=max(ans,values[i]+curMax) if values[i]>=curMax: curMax=values[i]-1 else: curMax-=1 ...
best-sightseeing-pair
♠️ Super Clear And Easy Understanding Python3 Solution
edwardchor
0
45
best sightseeing pair
1,014
0.595
Medium
16,607
https://leetcode.com/problems/best-sightseeing-pair/discuss/1784258/Python-easy-to-read-and-understand-or-DP
class Solution: def maxScoreSightseeingPair(self, values: List[int]) -> int: n = len(values) t1, t2 = [values[0]], [values[0]] for i in range(1, n): t1.append(max(values[i]+i, t1[i-1])) t2.append(values[i]-i) t1, t2 = t1[:-1], t2[1:] #print(t1, t2) ...
best-sightseeing-pair
Python easy to read and understand | DP
sanial2001
0
56
best sightseeing pair
1,014
0.595
Medium
16,608
https://leetcode.com/problems/best-sightseeing-pair/discuss/1692717/Keep-calculating-best-fit-sightseeing-and-maximum-score
class Solution: def maxScoreSightseeingPair(self, values: List[int]) -> int: valuable_point_pair = (values[0], 0) maxiumum_score = 0 def get_score(p, value): return valuable_point_pair[0] + value - abs(p-valuable_point_pair[1]) for p, value in enumerate(...
best-sightseeing-pair
Keep calculating best fit sightseeing and maximum score
puremonkey2001
0
43
best sightseeing pair
1,014
0.595
Medium
16,609
https://leetcode.com/problems/best-sightseeing-pair/discuss/1689682/One-Pass-Python-O(n)
class Solution: def maxScoreSightseeingPair(self, values: List[int]) -> int: best = 0 high, spot = 0, -1 for i, x in enumerate(values): high -= 1 # old spot loses value due to increase in travel distance score = high + x best = max(score, best...
best-sightseeing-pair
One Pass Python [O(n)]
briancoj
0
49
best sightseeing pair
1,014
0.595
Medium
16,610
https://leetcode.com/problems/best-sightseeing-pair/discuss/1493819/Simple-Python-Solution-w-explanation-o(1)-space-o(n)-runtime
class Solution(object): def maxScoreSightseeingPair(self, nums): starter = nums[0]+0 ender = 0 bestPair = 0 for i in range(1,len(nums)): ender = starter +nums[i]-i starter = max(starter,nums[i]+i) bestPair = max(bestPair,ender) return bestP...
best-sightseeing-pair
Simple Python Solution w/ explanation, o(1) space, o(n) runtime
CesarDN
0
79
best sightseeing pair
1,014
0.595
Medium
16,611
https://leetcode.com/problems/best-sightseeing-pair/discuss/1480982/Python3-solution-with-comment
class Solution: def maxScoreSightseeingPair(self, values: List[int]) -> int: # Keeps ith sightseeing spot max value so far current_max_i = values[0] # The maximum score of a pair of sightseeing splots to return answer = 0 for i in range(1, len(values)): # Image v...
best-sightseeing-pair
Python3 solution with comment
yukikitayama
0
52
best sightseeing pair
1,014
0.595
Medium
16,612
https://leetcode.com/problems/best-sightseeing-pair/discuss/1006939/Python3-linear-scan
class Solution: def maxScoreSightseeingPair(self, A: List[int]) -> int: ans = val = 0 for i, x in enumerate(A): ans = max(ans, x - i + val) val = max(val, x + i) return ans
best-sightseeing-pair
[Python3] linear scan
ye15
0
89
best sightseeing pair
1,014
0.595
Medium
16,613
https://leetcode.com/problems/best-sightseeing-pair/discuss/398015/Python-3
class Solution: def maxScoreSightseeingPair(self, A): a, b = A[0], 0 for i in range(1, len(A)): b, a = max(a + A[i] - i, b), max(A[i] + i, a) return b
best-sightseeing-pair
Python 3
slight_edge
0
274
best sightseeing pair
1,014
0.595
Medium
16,614
https://leetcode.com/problems/best-sightseeing-pair/discuss/1598504/Python3-One-pass-O(1)-space
class Solution: def maxScoreSightseeingPair(self, values: List[int]) -> int: prev, res = values[0], 0 for i in range(1, len(values)): res = max(res, prev + values[i] - i) prev = max(prev, values[i] + i) return res
best-sightseeing-pair
[Python3] One pass, O(1) space
maosipov11
-1
117
best sightseeing pair
1,014
0.595
Medium
16,615
https://leetcode.com/problems/smallest-integer-divisible-by-k/discuss/1655649/Python3-Less-Math-More-Intuition-or-2-Accepted-Solutions-or-Intuitive
class Solution: def smallestRepunitDivByK(self, k: int) -> int: if not k % 2 or not k % 5: return -1 n = length = 1 while True: if not n % k: return length length += 1 n = 10*n + 1
smallest-integer-divisible-by-k
[Python3] ✔️ Less Math, More Intuition ✔️ | 2 Accepted Solutions | Intuitive
PatrickOweijane
26
1,900
smallest integer divisible by k
1,015
0.47
Medium
16,616
https://leetcode.com/problems/smallest-integer-divisible-by-k/discuss/1655649/Python3-Less-Math-More-Intuition-or-2-Accepted-Solutions-or-Intuitive
class Solution: def smallestRepunitDivByK(self, k: int) -> int: if not k % 2 or not k % 5: return -1 r = length = 1 while True: r = r % k if not r: return length length += 1 r = 10*r + 1
smallest-integer-divisible-by-k
[Python3] ✔️ Less Math, More Intuition ✔️ | 2 Accepted Solutions | Intuitive
PatrickOweijane
26
1,900
smallest integer divisible by k
1,015
0.47
Medium
16,617
https://leetcode.com/problems/smallest-integer-divisible-by-k/discuss/1655706/Using-while-loop-and-hashmap-in-Python
class Solution: def smallestRepunitDivByK(self, k: int) -> int: #edge case if k % 2 == 0 or k % 5 == 0: return -1 #keep track of the remainder remain, length = 0, 0 found_so_far = set() while remain not in found_so_far: found_so_far.add(remain) remain =...
smallest-integer-divisible-by-k
Using while loop and hashmap in Python
kryuki
2
118
smallest integer divisible by k
1,015
0.47
Medium
16,618
https://leetcode.com/problems/smallest-integer-divisible-by-k/discuss/948652/Python-Simple-Solutiom
class Solution: def smallestRepunitDivByK(self, K): if K % 2 == 0 or K % 5 == 0: return -1 r = 0 for N in range(1, K + 1): r = (r * 10 + 1) % K if r==0: return N
smallest-integer-divisible-by-k
Python Simple Solutiom
lokeshsenthilkumar
1
175
smallest integer divisible by k
1,015
0.47
Medium
16,619
https://leetcode.com/problems/smallest-integer-divisible-by-k/discuss/2007261/oror-PYTHON-SOL-oror-REMAINDER-FIND-oror-HASHMAP-oror-EASY-oror-EXPLAINED
class Solution: def smallestRepunitDivByK(self, k: int) -> int: if k % 2 == 0: return -1 n = 1 leng = 1 mapp = {} while True: rem = n % k if rem == 0: return leng if rem in mapp : return -1 mapp[rem] = True n = n*10 ...
smallest-integer-divisible-by-k
|| PYTHON SOL || REMAINDER FIND || HASHMAP || EASY || EXPLAINED
reaper_27
0
73
smallest integer divisible by k
1,015
0.47
Medium
16,620
https://leetcode.com/problems/smallest-integer-divisible-by-k/discuss/1657500/Python3-Performant-and-streamlined-code(payload-4-lines!)-if-you-have-minimum-lines-obsession-%3A)
class Solution: def smallestRepunitDivByK(self, k: int) -> int: if math.gcd(k,10) != 1: return -1 # intuitative after some observation: any number divisable by 2 or 5 will never be a divisor of preunit. reminder = 0 for length in range(1, k+1): if (reminder := (1+reminder*10) % k...
smallest-integer-divisible-by-k
✅ [Python3] Performant and streamlined code(payload 4 lines!) if you have minimum lines obsession :)
win-9527
0
21
smallest integer divisible by k
1,015
0.47
Medium
16,621
https://leetcode.com/problems/smallest-integer-divisible-by-k/discuss/352334/Solution-in-Python-3-(beats-~98)-(With-Explanation)
class Solution: def smallestRepunitDivByK(self, K: int) -> int: if K % 2 == 0 or K % 5 == 0: return -1 i = n = 1 while n % K != 0: n, i = (10*n + 1) % K, i + 1 return i - Junaid Mansuri (LeetCode ID)@hotmail.com
smallest-integer-divisible-by-k
Solution in Python 3 (beats ~98%) (With Explanation)
junaidmansuri
0
375
smallest integer divisible by k
1,015
0.47
Medium
16,622
https://leetcode.com/problems/binary-string-with-substrings-representing-1-to-n/discuss/1106296/Python3-2-approaches
class Solution: def queryString(self, S: str, N: int) -> bool: for x in range(N, 0, -1): if bin(x)[2:] not in S: return False return True
binary-string-with-substrings-representing-1-to-n
[Python3] 2 approaches
ye15
6
387
binary string with substrings representing 1 to n
1,016
0.575
Medium
16,623
https://leetcode.com/problems/binary-string-with-substrings-representing-1-to-n/discuss/1106296/Python3-2-approaches
class Solution: def queryString(self, S: str, N: int) -> bool: ans = set() for i in range(len(S)): for ii in range(i, i + N.bit_length()): x = int(S[i:ii+1], 2) if 1 <= x <= N: ans.add(x) return len(ans) == N
binary-string-with-substrings-representing-1-to-n
[Python3] 2 approaches
ye15
6
387
binary string with substrings representing 1 to n
1,016
0.575
Medium
16,624
https://leetcode.com/problems/binary-string-with-substrings-representing-1-to-n/discuss/2841590/2-LINES-ororor-PYTHON-EASY-SOLUTIONoror-USING-STRING
class Solution: def queryString(self, s: str, n: int) -> bool: for i in range(1,n+1): if bin(i)[2:] not in s:return 0 return 1
binary-string-with-substrings-representing-1-to-n
2 LINES ||| PYTHON EASY SOLUTION|| USING STRING
thezealott
1
3
binary string with substrings representing 1 to n
1,016
0.575
Medium
16,625
https://leetcode.com/problems/binary-string-with-substrings-representing-1-to-n/discuss/1798033/Python3-solution-or-Using-python-bin()-function-or-88-lesser-memory
class Solution: def queryString(self, s: str, n: int) -> bool: for i in range(1,n+1): if (bin(i)[2:]) not in s: return False return True
binary-string-with-substrings-representing-1-to-n
✔Python3 solution | Using python bin() function | 88% lesser memory
Coding_Tan3
1
112
binary string with substrings representing 1 to n
1,016
0.575
Medium
16,626
https://leetcode.com/problems/binary-string-with-substrings-representing-1-to-n/discuss/2007280/ororPYTHON-SOL-oror-SIMPLE-oror-EXPLAINED-oror-STRINGS-oror
class Solution: def queryString(self, s: str, n: int) -> bool: leng_s = len(s) for i in range(1,n+1): binary = str(bin(i)[2:]) leng_b = len(binary) flag = False for j in range(leng_s - leng_b + 1): if s[j:j + leng_b] == binary: ...
binary-string-with-substrings-representing-1-to-n
||PYTHON SOL || SIMPLE || EXPLAINED || STRINGS ||
reaper_27
0
95
binary string with substrings representing 1 to n
1,016
0.575
Medium
16,627
https://leetcode.com/problems/binary-string-with-substrings-representing-1-to-n/discuss/1408126/24ms-or-96-fasteror-Simple-python3-solution.
class Solution: def queryString(self, s: str, n: int) -> bool: while(n): a=bin(n) if(a.replace('0b','') not in s): return 0 n-=1 return 1 Improved version class Solution: def queryString(self, s: str, n: int) -> bool: while(n): if(bin(n)[2:] not in s): return 0 n-=1 re...
binary-string-with-substrings-representing-1-to-n
24ms | 96% faster| Simple python3 solution.
kavikidadumbe
0
111
binary string with substrings representing 1 to n
1,016
0.575
Medium
16,628
https://leetcode.com/problems/binary-string-with-substrings-representing-1-to-n/discuss/597247/Super-easy-python-solution
class Solution: def queryString(self, S: str, N: int) -> bool: for i in range(1,N+1): x=str(bin(i).replace("0b", "")) if S.find(x)==-1: return False return True
binary-string-with-substrings-representing-1-to-n
Super easy python solution
Ayu-99
0
86
binary string with substrings representing 1 to n
1,016
0.575
Medium
16,629
https://leetcode.com/problems/convert-to-base-2/discuss/2007392/PYTHON-SOL-oror-EASY-oror-BINARY-CONVERSION-oror-WELL-EXPLAINED-oror
class Solution: def baseNeg2(self, n: int) -> str: ans = "" while n != 0: if n%-2 != 0 : ans = '1' + ans n = (n-1)//-2 else: ans = '0' + ans n = n//-2 return ans if ans !="" else '0'
convert-to-base-2
PYTHON SOL || EASY || BINARY CONVERSION || WELL EXPLAINED ||
reaper_27
1
172
convert to base 2
1,017
0.61
Medium
16,630
https://leetcode.com/problems/convert-to-base-2/discuss/2482057/Python-siolution
class Solution: def baseNeg2(self, n: int) -> str: result = "" while n != 0: if n%2 != 0 : result = '1' + result n = (n-1)//-2 else: result = '0' + result n = n//-2 return result if result != "" else '0'
convert-to-base-2
Python siolution
Yauhenish
0
57
convert to base 2
1,017
0.61
Medium
16,631
https://leetcode.com/problems/convert-to-base-2/discuss/1015354/Python3-similar-to-base-2
class Solution: def baseNeg2(self, N: int) -> str: ans = [] while N: ans.append(N &amp; 1) N = (1-N) >> 1 return "".join(map(str, ans[::-1] or [0]))
convert-to-base-2
[Python3] similar to base-2
ye15
0
159
convert to base 2
1,017
0.61
Medium
16,632
https://leetcode.com/problems/convert-to-base-2/discuss/1015354/Python3-similar-to-base-2
class Solution: def baseNeg2(self, N: int) -> str: ans = [] while N: ans.append(N &amp; 1) N = -(N >> 1) return "".join(map(str, ans[::-1] or [0]))
convert-to-base-2
[Python3] similar to base-2
ye15
0
159
convert to base 2
1,017
0.61
Medium
16,633
https://leetcode.com/problems/binary-prefix-divisible-by-5/discuss/356289/Solution-in-Python-3-(beats-~98)-(three-lines)-(-O(1)-space-)
class Solution: def prefixesDivBy5(self, A: List[int]) -> List[bool]: n = 0 for i in range(len(A)): A[i], n = (2*n + A[i]) % 5 == 0, (2*n + A[i]) % 5 return A - Junaid Mansuri (LeetCode ID)@hotmail.com
binary-prefix-divisible-by-5
Solution in Python 3 (beats ~98%) (three lines) ( O(1) space )
junaidmansuri
4
349
binary prefix divisible by 5
1,018
0.473
Easy
16,634
https://leetcode.com/problems/binary-prefix-divisible-by-5/discuss/791074/Python-Simple-solution
class Solution: def prefixesDivBy5(self, A: List[int]) -> List[bool]: s='';l=[] for i in A: s+=str(i) l.append(int(s,2)%5==0) return l
binary-prefix-divisible-by-5
Python Simple solution
lokeshsenthilkumar
3
275
binary prefix divisible by 5
1,018
0.473
Easy
16,635
https://leetcode.com/problems/binary-prefix-divisible-by-5/discuss/1231086/Python3-simple-solution
class Solution: def prefixesDivBy5(self, nums: List[int]) -> List[bool]: res = [] n = 0 for i in nums: n *= 2 if i == 1: n += 1 res.append(n % 5 == 0) return res
binary-prefix-divisible-by-5
Python3 simple solution
EklavyaJoshi
2
59
binary prefix divisible by 5
1,018
0.473
Easy
16,636
https://leetcode.com/problems/binary-prefix-divisible-by-5/discuss/1060101/Time-O(n)-Space-O(1)-Python3-solution
class Solution: def prefixesDivBy5(self, A: List[int]) -> List[bool]: # time O(n) # space O(1) output = [] last_bit = 0 for i in range(len(A)): new_bit = last_bit*2 + A[i] output.append(new_bit % 5 == 0) last_bit = new_bit return ou...
binary-prefix-divisible-by-5
Time O(n) Space O(1) Python3 solution
mhviraf
1
110
binary prefix divisible by 5
1,018
0.473
Easy
16,637
https://leetcode.com/problems/binary-prefix-divisible-by-5/discuss/2524509/python
class Solution: def prefixesDivBy5(self, nums: List[int]) -> List[bool]: s = '' res = [] for i in nums: s += str(i) decimal = int(s , 2) if decimal % 5 == 0: res.append(True) else: res.append(False) retur...
binary-prefix-divisible-by-5
python
akashp2001
0
25
binary prefix divisible by 5
1,018
0.473
Easy
16,638
https://leetcode.com/problems/binary-prefix-divisible-by-5/discuss/2021039/Python-Clean-and-Simple!-Bitwise
class Solution: def prefixesDivBy5(self, nums): total, result = 0, [] for num in nums: total <<= 1 total += num result.append(total % 5 == 0) return result
binary-prefix-divisible-by-5
Python - Clean and Simple! Bitwise
domthedeveloper
0
64
binary prefix divisible by 5
1,018
0.473
Easy
16,639
https://leetcode.com/problems/binary-prefix-divisible-by-5/discuss/1568866/Python3-dollarolution
class Solution: def prefixesDivBy5(self, nums: List[int]) -> List[bool]: x, l = 0, [] for i in nums: n = x * 2 + i l.append(n%5 == 0) x = n return l
binary-prefix-divisible-by-5
Python3 $olution
AakRay
0
64
binary prefix divisible by 5
1,018
0.473
Easy
16,640
https://leetcode.com/problems/binary-prefix-divisible-by-5/discuss/1248954/Python-or-O(n)
class Solution: def prefixesDivBy5(self, nums: List[int]) -> List[bool]: len_nums=len(nums) result=[False]*len_nums prev = nums[0] result[0] = True if prev%5==0 else False for i in range(1, len_nums): prev = 2*prev+nums[i] # previous = 2*(2^0num[0] + 2^1*num[1]) + nums[i] (0 or 1) result[i] = True ...
binary-prefix-divisible-by-5
Python | O(n)
rksharma19896
0
53
binary prefix divisible by 5
1,018
0.473
Easy
16,641
https://leetcode.com/problems/binary-prefix-divisible-by-5/discuss/1106199/python-direct-approach
class Solution: def prefixesDivBy5(self, A: List[int]) -> List[bool]: lis = [] st = "" for i in range(len(A)): st = st+str(A[i]) lis.append(int(st,2)%5 == 0) return lis
binary-prefix-divisible-by-5
python direct approach
abhisek_
0
69
binary prefix divisible by 5
1,018
0.473
Easy
16,642
https://leetcode.com/problems/binary-prefix-divisible-by-5/discuss/1085391/Deterministic-Finite-Automaton-Python3-(-96-time-80-memory-)
class Solution: def prefixesDivBy5(self, A: List[int]) -> List[bool]: state = 0 answer = [] for a in A: if a == 0: state = ( 2*state ) % 5 else: state = ( 2*state+1 ) % 5 answer.append(state==0) return answe...
binary-prefix-divisible-by-5
Deterministic Finite Automaton Python3 ( 96% time 80% memory )
rafic
0
47
binary prefix divisible by 5
1,018
0.473
Easy
16,643
https://leetcode.com/problems/next-greater-node-in-linked-list/discuss/283607/Clean-Python-Code
class Solution: def nextLargerNodes(self, head: ListNode) -> List[int]: result = [] stack = [] for i, current in enumerate(self.value_iterator(head)): result.append(0) while stack and stack[-1][0] < current: _, index = stack.pop() resul...
next-greater-node-in-linked-list
Clean Python Code
aquafie
3
817
next greater node in linked list
1,019
0.599
Medium
16,644
https://leetcode.com/problems/next-greater-node-in-linked-list/discuss/303575/Python-Solution
class Solution: def nextLargerNodes(self, head: ListNode) -> List[int]: res, stack, idx = [], [], 0 while head: while stack and stack[-1][0] < head.val: _, i = stack.pop() res[i] = head.val res.append(0) stack.append((h...
next-greater-node-in-linked-list
Python Solution
tahir3
2
761
next greater node in linked list
1,019
0.599
Medium
16,645
https://leetcode.com/problems/next-greater-node-in-linked-list/discuss/1554340/Python3-Two-version-of-solutions-with-using-stack
class Solution: def nextLargerNodes(self, head: ListNode) -> List[int]: res = [] stack = [] idx = 0 while head: res.append(0) while stack and stack[-1][0] < head.val: _, index = stack.pop() res[index] = hea...
next-greater-node-in-linked-list
[Python3] Two version of solutions with using stack
maosipov11
1
81
next greater node in linked list
1,019
0.599
Medium
16,646
https://leetcode.com/problems/next-greater-node-in-linked-list/discuss/1554340/Python3-Two-version-of-solutions-with-using-stack
class Solution: def nextLargerNodes(self, head: ListNode) -> List[int]: lst = [] stack = [] res = [] while head: lst.append(head.val) head = head.next for i in range(len(lst) - 1, -1, -1): max_prev = 0 ...
next-greater-node-in-linked-list
[Python3] Two version of solutions with using stack
maosipov11
1
81
next greater node in linked list
1,019
0.599
Medium
16,647
https://leetcode.com/problems/next-greater-node-in-linked-list/discuss/1320814/Python3-solution-using-stack
class Solution: def nextLargerNodes(self, head: ListNode) -> List[int]: values = [] temp = head while temp: values.append(temp.val) temp = temp.next ans = [0]*len(values) stack = [] for i,j in enumerate(values): if not stack or stac...
next-greater-node-in-linked-list
Python3 solution using stack
EklavyaJoshi
1
104
next greater node in linked list
1,019
0.599
Medium
16,648
https://leetcode.com/problems/next-greater-node-in-linked-list/discuss/2782042/Easy-python-solution-using-stack
class Solution: def nextLargerNodes(self, head: ListNode) -> List[int]: arr = [] while head: arr.append(head.val) head = head.next l=[] stk=[] n=len(arr) for i in range(n-1,-1,-1): if(len(stk)<=0): l.append(0) ...
next-greater-node-in-linked-list
Easy python solution using stack
liontech_123
0
2
next greater node in linked list
1,019
0.599
Medium
16,649
https://leetcode.com/problems/next-greater-node-in-linked-list/discuss/2781493/Next-Greater-Node-In-Linked-List-(Python)
class Solution: def nextLargerNodes(self, head: Optional[ListNode]) -> List[int]: nodeValue=[] current = head while current : nodeValue.append(current.val) current = current.next output = [0] * len(nodeValue) stack = [] for index , value in enu...
next-greater-node-in-linked-list
Next Greater Node In Linked List (Python)
abdullah956
0
1
next greater node in linked list
1,019
0.599
Medium
16,650
https://leetcode.com/problems/next-greater-node-in-linked-list/discuss/2577113/Python-92-or-using-deque-as-a-stack
class Solution: def nextLargerNodes(self, head: Optional[ListNode]) -> List[int]: #reverse it r = None curr = head while curr: tmp = curr.next curr.next = r r = curr curr = tmp curr = r ''' #use double ende...
next-greater-node-in-linked-list
Python 92% | using deque as a stack
pandish
0
16
next greater node in linked list
1,019
0.599
Medium
16,651
https://leetcode.com/problems/next-greater-node-in-linked-list/discuss/2333834/Python-monotonic-stack
class Solution: def nextLargerNodes(self, head: Optional[ListNode]) -> List[int]: result = [] stack = [] idx = 0 node = head while node: while stack and node.val > stack[-1][0]: _, i = stack.pop() result[i] = node.val ...
next-greater-node-in-linked-list
Python, monotonic stack
blue_sky5
0
14
next greater node in linked list
1,019
0.599
Medium
16,652
https://leetcode.com/problems/next-greater-node-in-linked-list/discuss/1975543/python-simple-easy-small-(Time-On-space-On)
class Solution: def nextLargerNodes(self, head: Optional[ListNode]) -> List[int]: l = [head] w = head.next while w != None : while len(l) != 0 and l[-1].val < w.val : l[-1].val = w.val l.pop() l.append(w) w = w.next while len(l) != 0 : l[-1].val = 0 ...
next-greater-node-in-linked-list
python - simple, easy, small (Time On, space On)
ZX007java
0
53
next greater node in linked list
1,019
0.599
Medium
16,653
https://leetcode.com/problems/next-greater-node-in-linked-list/discuss/1764531/Python-oror-Recursion-oror-Stack
class Solution: def nextLargerNodes(self, head: Optional[ListNode]) -> List[int]: def recur(head): if head.next is None: return [0], [head.val] ans, stack = recur(head.next) while stack and stack[-1] <= head.val: stack.pop(-1) i...
next-greater-node-in-linked-list
Python || Recursion || Stack
kalyan_yadav
0
74
next greater node in linked list
1,019
0.599
Medium
16,654
https://leetcode.com/problems/next-greater-node-in-linked-list/discuss/1624165/WEEB-DOES-PYTHON-(BEATS-98.48)
class Solution: def nextLargerNodes(self, head: Optional[ListNode]) -> List[int]: arr = [] pointer = head while pointer: arr.append(pointer.val) pointer = pointer.next result = [0] * len(arr) stack = [] # stores index for i in range(len(arr)): # implement decreasing stack while stack and arr...
next-greater-node-in-linked-list
WEEB DOES PYTHON (BEATS 98.48%)
Skywalker5423
0
127
next greater node in linked list
1,019
0.599
Medium
16,655
https://leetcode.com/problems/next-greater-node-in-linked-list/discuss/1452133/In-O(N)-easy-to-undestand-python3
class Solution: def nextLargerNodes(self, head: Optional[ListNode]) -> List[int]: if not head: return head arr = [] while head: arr.append(head.val) head = head.next n = len(arr) stack = [] ...
next-greater-node-in-linked-list
In O(N) - easy to undestand - python3
Shubham_Muramkar
0
112
next greater node in linked list
1,019
0.599
Medium
16,656
https://leetcode.com/problems/next-greater-node-in-linked-list/discuss/1387313/Python3-Stacks-89-Faster-with-less-Memory
class Solution: def nextLargerNodes(self, head: ListNode) -> List[int]: stack = [] while head: stack.append(head.val) head = head.next tmp = [] i = len(stack) - 1 ans = [0] * len(stack) while stack: curr = stack.p...
next-greater-node-in-linked-list
[Python3] Stacks 89%, Faster with less Memory
whitehatbuds
0
163
next greater node in linked list
1,019
0.599
Medium
16,657
https://leetcode.com/problems/next-greater-node-in-linked-list/discuss/1387313/Python3-Stacks-89-Faster-with-less-Memory
class Solution: def nextLargerNodes(self, head: ListNode) -> List[int]: stack = [] while head: stack.append(head.val) head = head.next tmp = [] i = len(stack) - 1 ans = [0] * len(stack) for i in range(i, -1, -1): while tm...
next-greater-node-in-linked-list
[Python3] Stacks 89%, Faster with less Memory
whitehatbuds
0
163
next greater node in linked list
1,019
0.599
Medium
16,658
https://leetcode.com/problems/next-greater-node-in-linked-list/discuss/1387299/Python3-Recursive-Dynamic-Programming-Slow-but-Accepted-One-Pass.
class Solution: def nextLargerNodes(self, head: ListNode) -> List[int]: if head is None: return [] def recurse(node, mem, ans): if node.next is None: ans.append(0) mem[node.val] = 0 return ...
next-greater-node-in-linked-list
[Python3] Recursive Dynamic Programming, Slow but Accepted, One Pass.
whitehatbuds
0
59
next greater node in linked list
1,019
0.599
Medium
16,659
https://leetcode.com/problems/next-greater-node-in-linked-list/discuss/1291620/python3-O(N)-using-stack
class Solution: def nextLargerNodes(self, head: ListNode) -> List[int]: if head==None: return [] if head.next==None: return [0] L=[] while head: L.append(head.val) head=head.next stack=[L[-1]] t=[None for _ in range(len(...
next-greater-node-in-linked-list
python3 O(N) using stack
ketan_raut
0
104
next greater node in linked list
1,019
0.599
Medium
16,660
https://leetcode.com/problems/next-greater-node-in-linked-list/discuss/1074486/python-3-simple-solution-using-stack
class Solution: def nextLargerNodes(self, head: ListNode) -> List[int]: if not head: return [] stack=[0] li=[] prev=head head=head.next prev.next=None while(head): node=head head=head.next node.next=prev ...
next-greater-node-in-linked-list
python 3 simple solution using stack
AchalGupta
0
419
next greater node in linked list
1,019
0.599
Medium
16,661
https://leetcode.com/problems/next-greater-node-in-linked-list/discuss/1007891/Python3-forward-and-backward-approaches
class Solution: def nextLargerNodes(self, head: ListNode) -> List[int]: ans, stack = [], [] while head: while stack and stack[-1][1] < head.val: ans[stack.pop()[0]] = head.val stack.append((len(ans), head.val)) ans.append(0) head = head.next ...
next-greater-node-in-linked-list
[Python3] forward & backward approaches
ye15
0
46
next greater node in linked list
1,019
0.599
Medium
16,662
https://leetcode.com/problems/next-greater-node-in-linked-list/discuss/1007891/Python3-forward-and-backward-approaches
class Solution: def nextLargerNodes(self, head: ListNode) -> List[int]: prev, node = None, head while node: node.next, node, prev = prev, node.next, node node = prev ans, stack = [], [] while node: while stack and stack[-1] <= node.val: stack.pop() ...
next-greater-node-in-linked-list
[Python3] forward & backward approaches
ye15
0
46
next greater node in linked list
1,019
0.599
Medium
16,663
https://leetcode.com/problems/number-of-enclaves/discuss/1040282/Python-BFS-and-DFS-by-yours-truly
class Solution: def numEnclaves(self, A: List[List[int]]) -> int: row, col = len(A), len(A[0]) if not A or not A[0]: return 0 boundary1 = deque([(i,0) for i in range(row) if A[i][0]==1]) + deque([(i,col-1) for i in range(row) if A[i][col-1]==1]) boundary2 = deque([(0,i) for i in range(...
number-of-enclaves
Python BFS and DFS by yours truly
Skywalker5423
14
1,100
number of enclaves
1,020
0.65
Medium
16,664
https://leetcode.com/problems/number-of-enclaves/discuss/1040282/Python-BFS-and-DFS-by-yours-truly
class Solution: def numEnclaves(self, A: List[List[int]]) -> int: row, col = len(A), len(A[0]) if not A or not A[0]: return 0 def dfs(x,y,A): if 0<=x<row and 0<=y<col and A[x][y] ==1: A[x][y] = "T" dfs(x+1,y,A) dfs(x-1,y,A) dfs(x,y+1,...
number-of-enclaves
Python BFS and DFS by yours truly
Skywalker5423
14
1,100
number of enclaves
1,020
0.65
Medium
16,665
https://leetcode.com/problems/number-of-enclaves/discuss/2519350/Python-Elegant-and-Short-or-In-place-or-DFS
class Solution: """ Time: O(n^2) Memory: O(n^2) """ WATER = 0 LAND = 1 def numEnclaves(self, grid: List[List[int]]) -> int: n, m = len(grid), len(grid[0]) for i in range(n): self.sink_island(i, 0, grid) self.sink_island(i, m - 1, grid) for j in range(m): self.sink_island(0, j, grid) self....
number-of-enclaves
Python Elegant & Short | In-place | DFS
Kyrylo-Ktl
2
85
number of enclaves
1,020
0.65
Medium
16,666
https://leetcode.com/problems/number-of-enclaves/discuss/2274986/Python3-clean-DFS-solution
class Solution: def numEnclaves(self, grid: List[List[int]]) -> int: m,n = len(grid), len(grid[0]) visited = set() result = [0] def dfs(i,j, isBoundary): if i < 0 or i >= m or j < 0 or j>= n or grid[i][j]!=1 or (i,j) in visited: return ...
number-of-enclaves
📌 Python3 clean DFS solution
Dark_wolf_jss
1
19
number of enclaves
1,020
0.65
Medium
16,667
https://leetcode.com/problems/number-of-enclaves/discuss/1853713/Python-DFS-Solution
class Solution: def numEnclaves(self, grid: List[List[int]]) -> int: if not grid: return 0 rows, cols = len(grid), len(grid[0]) def dfs(r, c, value): if r < 0 or r >= rows or c < 0 or c >= cols or grid[r][c] == 0: return grid[r][c]...
number-of-enclaves
[Python] DFS Solution
tejeshreddy111
1
91
number of enclaves
1,020
0.65
Medium
16,668
https://leetcode.com/problems/number-of-enclaves/discuss/2822959/DFS-approach-with-similar-question
class Solution: def dfs_util(self,grid: List[List[int]],i:int,j:int,r:int,c:int) -> int: # if any land cell touches boundary return -1 if i<0 or j<0 or i>=r or j>=c: return -1 if grid[i][j]==0: return 0 grid[i][j]=0 left = self.dfs_util(grid,i,j-1,r,c)...
number-of-enclaves
DFS approach with similar question
Sakshamji
0
4
number of enclaves
1,020
0.65
Medium
16,669
https://leetcode.com/problems/number-of-enclaves/discuss/2772377/Python-or-Easy-Solution-or-Matrix
class Solution(object): def numEnclaves(self, mat): """ :type grid: List[List[int]] :rtype: int """ def solve(r, c): if r not in range(len(mat)): return float("inf") if c not in range(len(mat[0])): return float("inf") ...
number-of-enclaves
Python | Easy Solution | Matrix
atharva77
0
3
number of enclaves
1,020
0.65
Medium
16,670
https://leetcode.com/problems/number-of-enclaves/discuss/2690795/DFS-solution-in-python
class Solution: def numEnclaves(self, grid: List[List[int]]) -> int: #this bfs function will make all the boundary 1's and the 1's connected to them as 0 def dfs(i,j): if i<0 or i>=m or j<0 or j>=n or grid[i][j]==0: return 0 grid[i][j]=0 dfs(i+1,j)...
number-of-enclaves
DFS solution in python
shashank_2000
0
5
number of enclaves
1,020
0.65
Medium
16,671
https://leetcode.com/problems/number-of-enclaves/discuss/2642340/Python-BFS
class Solution: def numEnclaves(self, grid: List[List[int]]) -> int: R, C = len(grid), len(grid[0]) ans = 0 q = collections.deque([]) for r in range(R): if r == 0 or r == R-1: for c in range(C): if grid[r][c] == 1: ...
number-of-enclaves
Python BFS
stanleyyuen_pang
0
2
number of enclaves
1,020
0.65
Medium
16,672
https://leetcode.com/problems/number-of-enclaves/discuss/2589109/Python-really-easy-to-understand-approach.......
class Solution: def numEnclaves(self, grid: List[List[int]]) -> int: visited=set() for i in range(0,len(grid)): for j in range(0,len(grid[0])): if i==0: if grid[i][j]==1: visited.add((i,j)) elif j==0: ...
number-of-enclaves
Python really easy to understand approach.......
guneet100
0
18
number of enclaves
1,020
0.65
Medium
16,673
https://leetcode.com/problems/number-of-enclaves/discuss/2407906/Optimal-python3-solution
class Solution: def numEnclaves(self, grid: List[List[int]]) -> int: n = len(grid) m = len(grid[0]) def getNeighbours(root): x, y = root neighbours = [] if x > 0 and grid[x-1][y] == 1: neighbours.append((x-1, y)) ...
number-of-enclaves
Optimal python3 solution
destifo
0
5
number of enclaves
1,020
0.65
Medium
16,674
https://leetcode.com/problems/number-of-enclaves/discuss/2356427/Python-3-or-O(rows*cols)-runtime-solution(Straightforward-BFS-%2B-Queue)
class Solution: #Time-Complexity: O(rows*cols + rows*cols), since for loop must run through each and every cell grid! Our bfs #helper in worst case has to run while loop for each entry if our grid is all land cells (rows*cols)! #-> O(rows*cols) #Space: O(rows*cols + rows*cols),worst case each and every ...
number-of-enclaves
Python 3 | O(rows*cols) runtime solution(Straightforward BFS + Queue)
JOON1234
0
10
number of enclaves
1,020
0.65
Medium
16,675
https://leetcode.com/problems/number-of-enclaves/discuss/2285869/Python3-DFS.-Remove-border-islands-and-count-%221%22s
class Solution: def numEnclaves(self, grid: List[List[int]]) -> int: C = len(grid[0]) R = len(grid) if C <= 1 or R <= 1: return 0 def dfs(i,j): if (i>=0 and j>=0 and i<R and j<C and grid[i][j] == 1): #if 0<i<R-1 and 0<j<C-1: ...
number-of-enclaves
Python3 DFS. Remove border islands and count "1"s
devmich
0
12
number of enclaves
1,020
0.65
Medium
16,676
https://leetcode.com/problems/number-of-enclaves/discuss/2186157/python-3-or-simple-dfs-or-O(mn)O(1)
class Solution: def numEnclaves(self, grid: List[List[int]]) -> int: m, n = len(grid), len(grid[0]) def dfs(i, j): if grid[i][j] == 0: return grid[i][j] = 0 if i: dfs(i - 1, j) if i != m - 1: dfs(i + 1, j) if j: df...
number-of-enclaves
python 3 | simple dfs | O(mn)/O(1)
dereky4
0
35
number of enclaves
1,020
0.65
Medium
16,677
https://leetcode.com/problems/number-of-enclaves/discuss/2166254/Simple-DFS-Solution
class Solution: def isSafe(self,i,j,grid): n = len(grid) m = len(grid[0]) if 0 <= i < n and 0 <= j < m: return True else: return False def dfs(self,i,j,grid): if not self.isSafe(i,j,grid) or grid[i][j] != 1: return ...
number-of-enclaves
Simple DFS Solution
Vaibhav7860
0
56
number of enclaves
1,020
0.65
Medium
16,678
https://leetcode.com/problems/number-of-enclaves/discuss/2107023/Python3-or-DFS
class Solution: def numEnclaves(self, grid: List[List[int]]) -> int: def dfs(grid, i, j): if i<0 or j<0 or i >= len(grid) or j >= len(grid[0]): return if grid[i][j] == 0: return grid[i][j] = 0...
number-of-enclaves
Python3 | DFS
iamirulofficial
0
18
number of enclaves
1,020
0.65
Medium
16,679
https://leetcode.com/problems/number-of-enclaves/discuss/2070141/Python3-fill-from-the-edges-and-then-count-ones
class Solution: def numEnclaves(self, grid: List[List[int]]) -> int: # 0 = sea, 1 = land # move = adjacent cells 4-directionally # can move walk off boundary # we want number of land cells in grid # which you cannot move off grid # aka just fill in from edges and coun...
number-of-enclaves
Python3 fill from the edges and then count ones
normalpersontryingtopayrent
0
20
number of enclaves
1,020
0.65
Medium
16,680
https://leetcode.com/problems/number-of-enclaves/discuss/2007519/PYTHON-SOL-oror-EASY-TO-READ-oror-BFS-SOL-oror-SIMPLE-oror-EXPLAINED-oror
class Solution: def numEnclaves(self, grid: List[List[int]]) -> int: rows = len(grid) cols = len(grid[0]) ones = 0 queue = [] vis =[[False for i in range(cols)] for j in range(rows)] for i in range(rows): for j in range(cols): if grid[i][j]...
number-of-enclaves
PYTHON SOL || EASY TO READ || BFS SOL || SIMPLE || EXPLAINED ||
reaper_27
0
25
number of enclaves
1,020
0.65
Medium
16,681
https://leetcode.com/problems/number-of-enclaves/discuss/2006908/Python-easy-to-read-and-understand-or-DFS
class Solution: def dfs(self, grid, row, col): if row < 0 or col < 0 or row == len(grid) or col == len(grid[0]) or grid[row][col] != 1: return 0 grid[row][col] = 2 t = self.dfs(grid, row-1, col) l = self.dfs(grid, row, col-1) d = self.dfs(grid, row+1, col) ...
number-of-enclaves
Python easy to read and understand | DFS
sanial2001
0
27
number of enclaves
1,020
0.65
Medium
16,682
https://leetcode.com/problems/number-of-enclaves/discuss/1960865/faster-than-98.63-of-Python3-online-submissions-for-Number-of-Enclaves.
class Solution: def numEnclaves(self, grid: List[List[int]]) -> int: m,n=len(grid),len(grid[0]) def Util(r,c): grid[r][c]=0 for i,j in [(r-1,c),(r+1,c),(r,c-1),(r,c+1)]: if i<0 or i>=m or j<0 or j>=n: continue if grid[i][...
number-of-enclaves
faster than 98.63% of Python3 online submissions for Number of Enclaves.
Neerajbirajdar
0
30
number of enclaves
1,020
0.65
Medium
16,683
https://leetcode.com/problems/number-of-enclaves/discuss/1891956/python-easy-to-understand-bfs-solution
class Solution: def numEnclaves(self, grid: List[List[int]]) -> int: enclaves = 0 rows = len(grid) cols = len(grid[0]) visited = [[False for _ in range(cols)] for _ in range(rows)] for i in range(rows): for j in range(cols): if not visited[i][j] an...
number-of-enclaves
python easy to understand bfs solution
karthik2265
0
14
number of enclaves
1,020
0.65
Medium
16,684
https://leetcode.com/problems/number-of-enclaves/discuss/1853496/Dye-mainland-and-remain-enclaves-or-DFS-or-clear-and-with-explanation
class Solution: def numEnclaves(self, grid): """ consider cells out side of grid as 'Mainland', like surrounding by 1s so any 1 in the border is connect to Mainland, so as that island since it's not a part of enclave, let's dye it dfs as water and then use another dfs we can ...
number-of-enclaves
Dye mainland and remain enclaves | DFS | clear and with explanation
steve-jokes
0
25
number of enclaves
1,020
0.65
Medium
16,685
https://leetcode.com/problems/number-of-enclaves/discuss/1808963/Python-Recursive-DFS
class Solution: def numEnclaves(self, grid: List[List[int]]) -> int: rows, cols = len(grid), len(grid[0]) Position = namedtuple('Position', ['row', 'col']) def withinBounds(cell): return 0 <= cell.row < rows and 0 <= cell.col < cols def dfs(cell...
number-of-enclaves
Python Recursive DFS
Rush_P
0
52
number of enclaves
1,020
0.65
Medium
16,686
https://leetcode.com/problems/number-of-enclaves/discuss/1665558/Python-DFS-Readable-with-Comments
class Solution: def numEnclaves(self, grid: List[List[int]]) -> int: #we will start dfs from the 1's on the boundary and will keep looking for 1's if we can visit them #the final result is total number of 1's - visited 1's M = len(grid) N = len(grid[0]) visit = set() count = 0 def dfs(i,j): i...
number-of-enclaves
Python DFS Readable with Comments
Jazzyb1999
0
56
number of enclaves
1,020
0.65
Medium
16,687
https://leetcode.com/problems/number-of-enclaves/discuss/1659172/Python-simple-bfs-solution-(O(mn)-time-O(mn)-space)
class Solution: from collections import deque def numEnclaves(self, grid: List[List[int]]) -> int: m, n = len(grid), len(grid[0]) def valid(x, y): return x>=0 and x<=m-1 and y>=0 and y<=n-1 def bfs(p, q): queue = deque([(p, q)]) while...
number-of-enclaves
Python simple bfs solution (O(mn) time, O(mn) space)
byuns9334
0
30
number of enclaves
1,020
0.65
Medium
16,688
https://leetcode.com/problems/number-of-enclaves/discuss/1062883/Easy-Python-Solution-or-DFS-or-Python
class Solution: def numEnclaves(self, A: List[List[int]]) -> int: # [[0,0,0,0], # [1,0,1,0], # [0,1,1,0], # [0,0,0,0]] def dfs(i,j,A): if i < 0 or i > len(A) - 1 or j < 0 or j > len(A[0]) - 1 or A[i][j] != 1: return if A[i][j] == 1...
number-of-enclaves
Easy Python Solution | DFS | Python
Ayush87
0
129
number of enclaves
1,020
0.65
Medium
16,689
https://leetcode.com/problems/number-of-enclaves/discuss/1015528/Python-faster-than-93-DFS
class Solution: def numEnclaves(self, A: List[List[int]]) -> int: ''' 1) We need number of land squares, so a result variable count. 2) Return 0, if the size of A is 0. 3) If not, find a path to land from borders, extract borders and run dfs. 4) In dfs, mark each visited cell...
number-of-enclaves
Python, faster than 93%, DFS
Narasimhag
0
62
number of enclaves
1,020
0.65
Medium
16,690
https://leetcode.com/problems/number-of-enclaves/discuss/1007898/Python3-flood-fill-via-dfs
class Solution: def numEnclaves(self, A: List[List[int]]) -> int: m, n = len(A), len(A[0]) # dimensions stack = [] for i in range(m): if A[i][0]: stack.append((i, 0)) if A[i][n-1]: stack.append((i, n-1)) for j in range(n): if A[0...
number-of-enclaves
[Python3] flood fill via dfs
ye15
0
39
number of enclaves
1,020
0.65
Medium
16,691
https://leetcode.com/problems/number-of-enclaves/discuss/650787/Python3-flood-fill-%2B-sum-Number-of-Enclaves
class Solution: def numEnclaves(self, A: List[List[int]]) -> int: m = len(A) n = len(A[0]) def floodFill(i: int, j:int) -> None: nonlocal m, n if not 0 <= i < m or not 0 <= j < n or not A[i][j]: return A[i][j] = 0 for x...
number-of-enclaves
Python3 flood fill + sum - Number of Enclaves
r0bertz
0
100
number of enclaves
1,020
0.65
Medium
16,692
https://leetcode.com/problems/number-of-enclaves/discuss/479657/520ms-python3-using-stack
class Solution: def numEnclaves(self, A: List[List[int]]) -> int: m = len(A) n = len(A[0]) stack = [] for i in range(m): if A[i][0]==1: A[i][0]=2 if 1<=i<=m-2 and A[i][1]==1 and n>=2: A[i][1]=2 stack....
number-of-enclaves
520ms python3, using stack
felicia1994
0
53
number of enclaves
1,020
0.65
Medium
16,693
https://leetcode.com/problems/number-of-enclaves/discuss/471336/Python3-98.40-(496-ms)100.00-(13.9-MB)-O(n)-time-O(1)-space-recursion
class Solution: def delete_valid_squares(self, A, row, column, max_row, max_column): if (A[row][column]): A[row][column] = 0 if (column < max_column): self.delete_valid_squares(A, row, column + 1, max_row, max_column) ...
number-of-enclaves
Python3 98.40% (496 ms)/100.00% (13.9 MB) -- O(n) time / O(1) space -- recursion
numiek_p
0
73
number of enclaves
1,020
0.65
Medium
16,694
https://leetcode.com/problems/number-of-enclaves/discuss/300833/Python%3A-Using-generators-to-make-the-code-easier-to-read-(beats-95)
class Solution: # Generates all coordinates on boundaries def boundary_coordinates(self, grid): rows = len(grid) cols = len(grid[0]) for row_index in range(rows): yield (row_index, 0) yield (row_index, cols - 1) for col_index in range(1, cols - 1): ...
number-of-enclaves
Python: Using generators to make the code easier to read (beats 95%)
Hai_dee
0
119
number of enclaves
1,020
0.65
Medium
16,695
https://leetcode.com/problems/remove-outermost-parentheses/discuss/1162269/Python-Simplest-Solution
class Solution: def removeOuterParentheses(self, S: str) -> str: stack=[] counter=0 for i in S: if i=='(': counter=counter+1 if counter==1: pass else: stack.append(i) else...
remove-outermost-parentheses
Python Simplest Solution
aishwaryanathanii
5
164
remove outermost parentheses
1,021
0.802
Easy
16,696
https://leetcode.com/problems/remove-outermost-parentheses/discuss/942888/Python-Simple-Solution
class Solution: def removeOuterParentheses(self, S: str) -> str: ans=[];o=0 for i in S: if i=='(' and o>0: ans.append(i) if i==')' and o>1: ans.append(')') o+=1 if i=='(' else -1 return ''.join(ans)
remove-outermost-parentheses
Python Simple Solution
lokeshsenthilkumar
2
534
remove outermost parentheses
1,021
0.802
Easy
16,697
https://leetcode.com/problems/remove-outermost-parentheses/discuss/2819789/Python-oror-96.77-Faster-oror-Without-Stack-oror-O(n)-Solution
class Solution: def removeOuterParentheses(self, s: str) -> str: c,j,n=0,0,len(s) ans=[] for i in range(n): if s[i]=='(': c+=1 #If there is opening paranthesis we increment the counter variable else: c-=1 #If there is closing paranthesi...
remove-outermost-parentheses
Python || 96.77% Faster || Without Stack || O(n) Solution
DareDevil_007
1
95
remove outermost parentheses
1,021
0.802
Easy
16,698
https://leetcode.com/problems/remove-outermost-parentheses/discuss/2819732/Python-Easy-Solution-Using-Stack-in-O(n)-Complexity
class Solution: def removeOuterParentheses(self, s: str) -> str: a,n=[],len(s) i=j=0 t='' while i<n: print(s[i],"s[i]") if a and a[-1]=='(' and s[i]==')': a.pop() elif len(a)==0 and s[i]=='(' and i>0: t+=s[j+1:i-1] ...
remove-outermost-parentheses
Python Easy Solution Using Stack in O(n) Complexity
DareDevil_007
1
61
remove outermost parentheses
1,021
0.802
Easy
16,699