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/reverse-substrings-between-each-pair-of-parentheses/discuss/2290806/PYTHON-SOL-or-RECURSION-AND-STACK-SOL-or-DETAILED-EXPLANATION-WITH-PICTRUE-or | class Solution:
def reverseParentheses(self, s: str) -> str:
def solve(string):
n = len(string)
word = ""
i = 0
while i <n:
if string[i] == '(':
new = ""
count = 0
while True:
... | reverse-substrings-between-each-pair-of-parentheses | PYTHON SOL | RECURSION AND STACK SOL | DETAILED EXPLANATION WITH PICTRUE | | reaper_27 | 4 | 123 | reverse substrings between each pair of parentheses | 1,190 | 0.658 | Medium | 18,100 |
https://leetcode.com/problems/reverse-substrings-between-each-pair-of-parentheses/discuss/2290806/PYTHON-SOL-or-RECURSION-AND-STACK-SOL-or-DETAILED-EXPLANATION-WITH-PICTRUE-or | class Solution:
def reverseParentheses(self, s: str) -> str:
stack = []
for i in s:
if i == ')':
tmp = ""
while stack[-1] != '(':
tmp += stack.pop()
stack.pop()
for j in tmp: stack.append(j)
e... | reverse-substrings-between-each-pair-of-parentheses | PYTHON SOL | RECURSION AND STACK SOL | DETAILED EXPLANATION WITH PICTRUE | | reaper_27 | 4 | 123 | reverse substrings between each pair of parentheses | 1,190 | 0.658 | Medium | 18,101 |
https://leetcode.com/problems/reverse-substrings-between-each-pair-of-parentheses/discuss/382379/Python3-regex-find | class Solution:
def reverseParentheses(self, s: str) -> str:
while '(' in s:
posopen=s.rfind('(')
posclose=s.find(')',posopen+1)
s=s[:posopen]+s[posopen+1:posclose][::-1]+s[posclose+1:]
return s | reverse-substrings-between-each-pair-of-parentheses | Python3 regex find | aj_to_rescue | 3 | 120 | reverse substrings between each pair of parentheses | 1,190 | 0.658 | Medium | 18,102 |
https://leetcode.com/problems/reverse-substrings-between-each-pair-of-parentheses/discuss/1079296/Python-FASTER-THAN-98-SUBMISSION-Using-Stack-and-Array | class Solution:
def reverseParentheses(self, s: str) -> str:
stk,arr=[],[]
for i in s:
if i!=')':
stk.append(i)
else:
popele=stk.pop()
while popele!='(':
arr.append(popele)
popele=stk.pop(... | reverse-substrings-between-each-pair-of-parentheses | [Python] FASTER THAN 98% SUBMISSION, Using Stack and Array | jayrathore070 | 1 | 181 | reverse substrings between each pair of parentheses | 1,190 | 0.658 | Medium | 18,103 |
https://leetcode.com/problems/reverse-substrings-between-each-pair-of-parentheses/discuss/2695464/Python-O(N)-O(N) | class Solution:
def reverseParentheses(self, s: str) -> str:
stack = []
for char in s:
if char != ")":
stack.append(char)
continue
current = []
while stack[-1] != "(":
current.append(stack.pop()... | reverse-substrings-between-each-pair-of-parentheses | Python - O(N), O(N) | Teecha13 | 0 | 9 | reverse substrings between each pair of parentheses | 1,190 | 0.658 | Medium | 18,104 |
https://leetcode.com/problems/reverse-substrings-between-each-pair-of-parentheses/discuss/2587019/python-stack-solution-with-explanation | class Solution:
def reverseParentheses(self, s: str) -> str:
stack = deque()
for i in s:
if i == ')':
c = ""
while stack[-1] != '(':
c += stack.pop()
stack.pop()
for j in c:
... | reverse-substrings-between-each-pair-of-parentheses | python stack solution with explanation | pandish | 0 | 30 | reverse substrings between each pair of parentheses | 1,190 | 0.658 | Medium | 18,105 |
https://leetcode.com/problems/reverse-substrings-between-each-pair-of-parentheses/discuss/2206705/97-faster-solution-or-python-or-easy-implementation-using-stack | class Solution:
def reverseParentheses(self, s: str) -> str:
stack=[]
left=0
right=len(s)
while left<right:
if s[left]!=")":
stack.append(s[left])
left+=1
else:
st=""
while stack and stack[-1]!="(... | reverse-substrings-between-each-pair-of-parentheses | 97% faster solution | python | easy implementation using stack | glimloop | 0 | 95 | reverse substrings between each pair of parentheses | 1,190 | 0.658 | Medium | 18,106 |
https://leetcode.com/problems/reverse-substrings-between-each-pair-of-parentheses/discuss/2178205/Easy-to-understand-oror-python | class Solution:
def reverseParentheses(self, s: str) -> str:
stack = []
for i in range(0,len(s)):
if(s[i] == "("):
stack.append(s[i])
elif(s[i] >= "a" and s[i] <= "z"):
stack.append(s[i])
else:
s1 = ""
... | reverse-substrings-between-each-pair-of-parentheses | Easy to understand || python | MB16biwas | 0 | 55 | reverse substrings between each pair of parentheses | 1,190 | 0.658 | Medium | 18,107 |
https://leetcode.com/problems/reverse-substrings-between-each-pair-of-parentheses/discuss/1912870/Python-easy-to-read-and-understand-or-stack | class Solution:
def reverseParentheses(self, s: str) -> str:
stack = []
for i in range(len(s)):
if s[i] == ")":
temp = ""
while stack and stack[-1] != "(":
temp += stack.pop()
stack.pop()
for ch in temp:
... | reverse-substrings-between-each-pair-of-parentheses | Python easy to read and understand | stack | sanial2001 | 0 | 98 | reverse substrings between each pair of parentheses | 1,190 | 0.658 | Medium | 18,108 |
https://leetcode.com/problems/reverse-substrings-between-each-pair-of-parentheses/discuss/1867932/Python-or-Recursion | class Solution:
def reverseParentheses(self, s: str) -> str:
def dfs(i):
curr=''
while i<len(s):
if s[i]=='(':
ans,i=dfs(i+1)
curr+=ans
elif s[i]==')':
return curr[::-1],i
else... | reverse-substrings-between-each-pair-of-parentheses | Python | Recursion | heckt27 | 0 | 53 | reverse substrings between each pair of parentheses | 1,190 | 0.658 | Medium | 18,109 |
https://leetcode.com/problems/reverse-substrings-between-each-pair-of-parentheses/discuss/1576385/Python3-Solution-with-using-stack | class Solution:
def reverseParentheses(self, s: str) -> str:
stack = [[]]
for c in s:
if c == '(':
stack.append([])
elif c == ')':
last_chain = stack.pop()
stack[-1] += last_chain[::-1]
else:
... | reverse-substrings-between-each-pair-of-parentheses | [Python3] Solution with using stack | maosipov11 | 0 | 72 | reverse substrings between each pair of parentheses | 1,190 | 0.658 | Medium | 18,110 |
https://leetcode.com/problems/reverse-substrings-between-each-pair-of-parentheses/discuss/1387821/Python-Stack | class Solution:
def reverseParentheses(self, s: str) -> str:
result, stack = "", []
for c in s:
if c == "(":
stack += [""]
elif c == ")":
temp = "".join(stack.pop())[::-1]
if stack:
stack[-1] += temp
... | reverse-substrings-between-each-pair-of-parentheses | [Python] Stack | dev-josh | 0 | 127 | reverse substrings between each pair of parentheses | 1,190 | 0.658 | Medium | 18,111 |
https://leetcode.com/problems/reverse-substrings-between-each-pair-of-parentheses/discuss/1363086/Python3-solution-using-stack | class Solution:
def reverseParentheses(self, s: str) -> str:
st = []
for i in s:
if i == ')':
l = []
while st[-1] != '(':
l.append(st.pop())
st.pop()
st.extend(l)
else:
st.appe... | reverse-substrings-between-each-pair-of-parentheses | Python3 solution using stack | EklavyaJoshi | 0 | 51 | reverse substrings between each pair of parentheses | 1,190 | 0.658 | Medium | 18,112 |
https://leetcode.com/problems/reverse-substrings-between-each-pair-of-parentheses/discuss/1325923/Python3-Stack-approach | class Solution:
def reverseParentheses(self, s: str) -> str:
stack = []
ln = len(s)
for i in range(ln):
if s[i] != ')': stack.append(s[i])
else:
reverse = []
while stack and stack[-1] != '(':
reverse.append(stack.pop... | reverse-substrings-between-each-pair-of-parentheses | [Python3] Stack approach | sann2011 | 0 | 39 | reverse substrings between each pair of parentheses | 1,190 | 0.658 | Medium | 18,113 |
https://leetcode.com/problems/reverse-substrings-between-each-pair-of-parentheses/discuss/1267913/python-beats-95-or | class Solution:
def reverseParentheses(self, s: str) -> str:
s=list(s)
u=[]
while s:
a=s.pop(0)
if a!=')':
u.append(a)
else:
p=[]
while True:
... | reverse-substrings-between-each-pair-of-parentheses | python beats 95% | | chikushen99 | 0 | 103 | reverse substrings between each pair of parentheses | 1,190 | 0.658 | Medium | 18,114 |
https://leetcode.com/problems/reverse-substrings-between-each-pair-of-parentheses/discuss/1232204/Python-Simple-Straightforward-Easy-Understand-Stack-Solution | class Solution:
def reverseParentheses(self, s: str) -> str:
stack = []
for ele in s:
if ele.isalpha() or ele == '(':
stack.append(ele)
else:
temp = []
while stack[-1] and stack[-1] != '(':
... | reverse-substrings-between-each-pair-of-parentheses | [Python] Simple, Straightforward, Easy Understand, Stack Solution | Ruifeng_Wang | 0 | 129 | reverse substrings between each pair of parentheses | 1,190 | 0.658 | Medium | 18,115 |
https://leetcode.com/problems/reverse-substrings-between-each-pair-of-parentheses/discuss/1155426/Python3-stack | class Solution:
def reverseParentheses(self, s: str) -> str:
stack = [""]
for c in s:
if c == "(": stack.append("")
elif c == ")":
val = stack.pop()[::-1]
stack[-1] += val
else: stack[-1] += c
return stack.pop() | reverse-substrings-between-each-pair-of-parentheses | [Python3] stack | ye15 | 0 | 67 | reverse substrings between each pair of parentheses | 1,190 | 0.658 | Medium | 18,116 |
https://leetcode.com/problems/reverse-substrings-between-each-pair-of-parentheses/discuss/1155426/Python3-stack | class Solution:
def reverseParentheses(self, s: str) -> str:
stack = []
mp = {}
for i, c in enumerate(s):
if c == "(": stack.append(i)
elif c == ")":
k = stack.pop()
mp[i], mp[k] = k, i
ans = []
i, ii = 0, 1
... | reverse-substrings-between-each-pair-of-parentheses | [Python3] stack | ye15 | 0 | 67 | reverse substrings between each pair of parentheses | 1,190 | 0.658 | Medium | 18,117 |
https://leetcode.com/problems/reverse-substrings-between-each-pair-of-parentheses/discuss/414098/Python-100-memory-with-Comments%3A-Easy-Understand | class Solution:
def reverseParentheses(self, s: str) -> str:
#Take an empty stack for iteration
stack = []
for i in range(0, len(s)):
# If the current charector is anything other than closing bracket append it
if s[i] != ')':
stack.append(s[i])
else:
... | reverse-substrings-between-each-pair-of-parentheses | Python 100% memory with Comments: Easy Understand | kottapallicharan | 0 | 178 | reverse substrings between each pair of parentheses | 1,190 | 0.658 | Medium | 18,118 |
https://leetcode.com/problems/reverse-substrings-between-each-pair-of-parentheses/discuss/382563/Two-Solutions-in-Python-3-(beats-100.0-)-(three-lines) | class Solution:
def reverseParentheses(self, s: str) -> str:
L = [i for i,j in enumerate(s) if j == '(']
while L: s = (lambda x,y: s[0:x]+s[x+1:y][::-1]+s[y+1:])(L[-1],s.index(')',L.pop()+1))
return s | reverse-substrings-between-each-pair-of-parentheses | Two Solutions in Python 3 (beats 100.0 %) (three lines) | junaidmansuri | -1 | 207 | reverse substrings between each pair of parentheses | 1,190 | 0.658 | Medium | 18,119 |
https://leetcode.com/problems/reverse-substrings-between-each-pair-of-parentheses/discuss/382563/Two-Solutions-in-Python-3-(beats-100.0-)-(three-lines) | class Solution:
def reverseParentheses(self, s: str) -> str:
j, s, S = 0, list(s)+[''], []
while s[j]:
if s[j] == '(': S.append(j)
elif s[j] == ')':
i = S.pop()
s, j = s[0:i]+s[i+1:j][::-1]+s[j+1:], i-1
j += 1
return "".join(s)
- Junaid Mansuri
(LeetCode ID)@hotm... | reverse-substrings-between-each-pair-of-parentheses | Two Solutions in Python 3 (beats 100.0 %) (three lines) | junaidmansuri | -1 | 207 | reverse substrings between each pair of parentheses | 1,190 | 0.658 | Medium | 18,120 |
https://leetcode.com/problems/reverse-substrings-between-each-pair-of-parentheses/discuss/1145106/Python-Stack-Solution-or-O(n)-time-O(n)-space | class Solution:
def reverseParentheses(self, s: str) -> str:
stack = []
i = 0
while i < len(s):
while i < len(s) and s[i] != ")":
stack.append(s[i])
i += 1
temp = ""
if i < len(s) and s[i] == ")":
while stack... | reverse-substrings-between-each-pair-of-parentheses | Python Stack Solution | O(n) time O(n) space | vanigupta20024 | -3 | 108 | reverse substrings between each pair of parentheses | 1,190 | 0.658 | Medium | 18,121 |
https://leetcode.com/problems/k-concatenation-maximum-sum/discuss/2201976/Python-easy-to-read-and-understand-or-kadane | class Solution:
def kadane(self, nums):
for i in range(1, len(nums)):
if nums[i-1] > 0:
nums[i] += nums[i-1]
return max(max(nums), 0)
def kConcatenationMaxSum(self, arr: List[int], k: int) -> int:
sums = sum(arr)
mod = 10**9 + 7
if k == 1:... | k-concatenation-maximum-sum | Python easy to read and understand | kadane | sanial2001 | 2 | 147 | k concatenation maximum sum | 1,191 | 0.239 | Medium | 18,122 |
https://leetcode.com/problems/k-concatenation-maximum-sum/discuss/1155407/Python3-Kadane's-algo | class Solution:
def kConcatenationMaxSum(self, arr: List[int], k: int) -> int:
rsm = val = 0
sm = sum(arr)
if k > 1: arr *= 2
for x in arr:
val = max(0, val + x)
rsm = max(rsm, val)
return max(rsm, rsm + max(0, k-2)*sm) % 1_000_000_007 | k-concatenation-maximum-sum | [Python3] Kadane's algo | ye15 | 1 | 157 | k concatenation maximum sum | 1,191 | 0.239 | Medium | 18,123 |
https://leetcode.com/problems/k-concatenation-maximum-sum/discuss/2810564/Python-(Simple-Dynamic-Programming) | class Solution:
def kadane(self,ans):
max_sum_so_far, max_ending_here = ans[0], ans[0]
for i in ans[1:]:
max_ending_here = max(max_ending_here+i,i)
max_sum_so_far = max(max_sum_so_far,max_ending_here)
return max_sum_so_far
def kConcatenationMaxSum(self, arr, k)... | k-concatenation-maximum-sum | Python (Simple Dynamic Programming) | rnotappl | 0 | 1 | k concatenation maximum sum | 1,191 | 0.239 | Medium | 18,124 |
https://leetcode.com/problems/k-concatenation-maximum-sum/discuss/2295009/PYTHON-or-KADANE-ALGO-or-DETAILED-EXPLANATION-WITH-PICTURE-or-INTUITIVE-or | class Solution:
def kConcatenationMaxSum(self, arr: List[int], k: int) -> int:
n = len(arr)
if n == 0: return 0
summ,gmax,cmax,gmin,cmin = arr[0],arr[0],arr[0],arr[0],arr[0]
for i in range(1,n):
summ += arr[i]
cmax = max(arr[i],cmax+arr[i])
gmax = ... | k-concatenation-maximum-sum | PYTHON | KADANE ALGO | DETAILED EXPLANATION WITH PICTURE | INTUITIVE | | reaper_27 | 0 | 108 | k concatenation maximum sum | 1,191 | 0.239 | Medium | 18,125 |
https://leetcode.com/problems/k-concatenation-maximum-sum/discuss/2100728/python-3-oror-Kadane's-Algorithim-oror-O(n)O(1) | class Solution:
def kConcatenationMaxSum(self, nums: List[int], k: int) -> int:
def maxSum(k):
res = cur = 0
for _ in range(k):
for num in nums:
cur = max(cur + num, num)
res = max(res, cur)
return res
... | k-concatenation-maximum-sum | python 3 || Kadane's Algorithim || O(n)/O(1) | dereky4 | 0 | 146 | k concatenation maximum sum | 1,191 | 0.239 | Medium | 18,126 |
https://leetcode.com/problems/k-concatenation-maximum-sum/discuss/1223296/Python-wo-any-algo(Pure-Case-based-easy-Soln) | class Solution:
def kConcatenationMaxSum(self, a: List[int], k: int) -> int:
b = []
if k>1:
for i in a:
b.append(i)
b.extend(a)
def maxSubArray(nums):
meh=0
msf=min(nums)
temp = []
for i in nums:
... | k-concatenation-maximum-sum | Python w/o any algo(Pure Case based easy Soln) | iamkshitij77 | 0 | 141 | k concatenation maximum sum | 1,191 | 0.239 | Medium | 18,127 |
https://leetcode.com/problems/k-concatenation-maximum-sum/discuss/382382/python3-Extension-to-kadane's-algo | class Solution:
def kConcatenationMaxSum(self, arr: List[int], k: int) -> int:
def kadane(a):
n = len(a)
max_so_far = a[0]
max_ending_here = a[0]
for i in range(1, n):
max_ending_here = max(a[i],max_ending_here + a[i])
max_so_fa... | k-concatenation-maximum-sum | python3 Extension to kadane's algo | aj_to_rescue | 0 | 84 | k concatenation maximum sum | 1,191 | 0.239 | Medium | 18,128 |
https://leetcode.com/problems/k-concatenation-maximum-sum/discuss/382490/Solution-in-Python-3-(beats-100.0)-(ten-lines) | class Solution:
def kConcatenationMaxSum(self, a: List[int], k: int) -> int:
L, M, j, m = len(a), 10**9 + 7, 0, 0
if min(a) >= 0: return sum(a)*k % M
if max(a) <= 0: return 0
while j < 2*L:
n, i = 0, j
for j in range(i,2*L):
n, j = n + a[j%L], j + 1
if n < 0: break
... | k-concatenation-maximum-sum | Solution in Python 3 (beats 100.0%) (ten lines) | junaidmansuri | -1 | 250 | k concatenation maximum sum | 1,191 | 0.239 | Medium | 18,129 |
https://leetcode.com/problems/critical-connections-in-a-network/discuss/382440/Python-DFS-Tree-Solution-(O(V%2BE)-complexity) | class Solution:
def criticalConnections(self, n: int, connections: List[List[int]]) -> List[List[int]]:
dic = collections.defaultdict(list)
for c in connections:
u, v = c
dic[u].append(v)
dic[v].append(u)
timer = 0
... | critical-connections-in-a-network | Python DFS-Tree Solution (O(V+E) complexity) | ywen1995 | 16 | 5,500 | critical connections in a network | 1,192 | 0.545 | Hard | 18,130 |
https://leetcode.com/problems/critical-connections-in-a-network/discuss/990791/Tarjan-Algo-Critical-Connections-Python-Solution-explained | class Solution:
def criticalConnections(self, n: int, connections: List[List[int]]) -> List[List[int]]:
'''
disc = discovery time of each node in the graph
_____________________
|The concept of 'LOW'|
low values means to which scc this node belongs to and that if this node is... | critical-connections-in-a-network | Tarjan Algo Critical Connections Python Solution explained | SaSha59 | 6 | 838 | critical connections in a network | 1,192 | 0.545 | Hard | 18,131 |
https://leetcode.com/problems/critical-connections-in-a-network/discuss/1339096/Elegant-Python-DFS-or-98.66-82.23 | class Solution:
def criticalConnections(self, n: int, connections: List[List[int]]) -> List[List[int]]:
graph = defaultdict(list)
for node1, node2 in connections:
graph[node1].append(node2), graph[node2].append(node1)
arrival_time = [None]*n
critical_con... | critical-connections-in-a-network | Elegant Python DFS | 98.66%, 82.23% | soma28 | 5 | 656 | critical connections in a network | 1,192 | 0.545 | Hard | 18,132 |
https://leetcode.com/problems/critical-connections-in-a-network/discuss/1155457/Python3-Tarjan's-algo | class Solution:
def criticalConnections(self, n: int, connections: List[List[int]]) -> List[List[int]]:
graph = {} # graph as adjacency list
for u, v in connections:
graph.setdefault(u, []).append(v)
graph.setdefault(v, []).append(u)
def dfs(x, p, step):
... | critical-connections-in-a-network | [Python3] Tarjan's algo | ye15 | 4 | 356 | critical connections in a network | 1,192 | 0.545 | Hard | 18,133 |
https://leetcode.com/problems/critical-connections-in-a-network/discuss/865386/DFS-Tarjans-Algorithm-Python3-Clean-and-Intuitive | class Solution:
def criticalConnections(self, n: int, connections: List[List[int]]) -> List[List[int]]:
if not connections:
return []
def create_graph(edges):
graph = defaultdict(list)
for u, v in edges:
graph[u].append(v)
... | critical-connections-in-a-network | DFS Tarjans Algorithm Python3 Clean & Intuitive | nyc_coder | 4 | 817 | critical connections in a network | 1,192 | 0.545 | Hard | 18,134 |
https://leetcode.com/problems/critical-connections-in-a-network/discuss/1810111/Python-DFS-brute-force-solution | class Solution:
def criticalConnections(self, n: int, connections: List[List[int]]) -> List[List[int]]:
critConns = []
def buildGraph(exclude, conns):
graph = defaultdict(list)
for conn in conns:
if conn == exclude:
continue
... | critical-connections-in-a-network | Python DFS brute force solution | FluffBlankie | 2 | 198 | critical connections in a network | 1,192 | 0.545 | Hard | 18,135 |
https://leetcode.com/problems/critical-connections-in-a-network/discuss/1651832/DFS-with-only-one-main-condition-or-O(V%2BE)-time-complexity | class Solution:
def criticalConnections(self, n: int, connections: List[List[int]]) -> List[List[int]]:
graph=collections.defaultdict(list)
for i,j in connections:
graph[i].append(j)
graph[j].append(i)
low=[0]*n
visited=set()
... | critical-connections-in-a-network | DFS with only one main condition | O(V+E) time complexity | DaRk_hEaRt | 2 | 242 | critical connections in a network | 1,192 | 0.545 | Hard | 18,136 |
https://leetcode.com/problems/critical-connections-in-a-network/discuss/2052958/Python3-Solution-with-using-dfs | class Solution:
def build_graph(self, connections):
g = collections.defaultdict(list)
for src, dst in connections:
g[src].append(dst)
g[dst].append(src)
return g
def traversal(self, g, cur_v, prev_v, cur_rank, lowest_rank, visited, res):
... | critical-connections-in-a-network | [Python3] Solution with using dfs | maosipov11 | 1 | 40 | critical connections in a network | 1,192 | 0.545 | Hard | 18,137 |
https://leetcode.com/problems/critical-connections-in-a-network/discuss/2435830/Python-DFS-bridge-finder | class Solution(object):
def criticalConnections(self, n, nodes):
graph = defaultdict(list)
for u,v in nodes:
graph[u].append(v)
graph[v].append(u)
intime = {}
lowtime = {}
visited = set()
timer = 1
ans = []
def dfs(graph, node,... | critical-connections-in-a-network | Python DFS bridge finder | Abhi_009 | 0 | 119 | critical connections in a network | 1,192 | 0.545 | Hard | 18,138 |
https://leetcode.com/problems/critical-connections-in-a-network/discuss/972519/Need-help-DFS | class Solution:
def criticalConnections(self, n: int, connections: List[List[int]]) -> List[List[int]]:
connect = collections.defaultdict(set)
for c in connections:
connect[c[0]].add(c[1])
connect[c[1]].add(c[0])
step = [-1] * n
res = []
... | critical-connections-in-a-network | Need help DFS | HaixuSong | 0 | 173 | critical connections in a network | 1,192 | 0.545 | Hard | 18,139 |
https://leetcode.com/problems/critical-connections-in-a-network/discuss/415497/Can-anyone-explain-this-solution-to-me | class Solution:
def criticalConnections(self, n: int, connections: List[List[int]]) -> List[List[int]]:
graph = [[] for _ in range(n)]
group = [None] * n
for n1, n2 in connections:
graph[n1].append(n2)
graph[n2].append(n1)
def dfs(node, parent):
... | critical-connections-in-a-network | Can anyone explain this solution to me? | SkookumChoocher | 0 | 410 | critical connections in a network | 1,192 | 0.545 | Hard | 18,140 |
https://leetcode.com/problems/minimum-absolute-difference/discuss/569795/Easy-to-Understand-or-Faster-or-Simple-or-Python-Solution | class Solution:
def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:
arr.sort()
m = float('inf')
out = []
for i in range(1, len(arr)):
prev = arr[i - 1]
curr = abs(prev - arr[i])
if curr < m:
out = [[prev, arr[i]]]
... | minimum-absolute-difference | Easy to Understand | Faster | Simple | Python Solution | Mrmagician | 16 | 1,300 | minimum absolute difference | 1,200 | 0.697 | Easy | 18,141 |
https://leetcode.com/problems/minimum-absolute-difference/discuss/1636910/Python3-SINGLE-PASS-(~)~-Explained | class Solution:
def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:
arr.sort()
mini, res = abs(arr[1] - arr[0]), [arr[0:2]]
for i in range(2, len(arr)):
diff = abs(arr[i] - arr[i - 1])
if diff > mini:
conti... | minimum-absolute-difference | ✔️ [Python3] SINGLE PASS (~˘▾˘)~, Explained | artod | 5 | 411 | minimum absolute difference | 1,200 | 0.697 | Easy | 18,142 |
https://leetcode.com/problems/minimum-absolute-difference/discuss/1636754/Beats-100-(292ms)-sort-%2B-single-scan-in-Python | class Solution:
def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:
arr.sort()
res = []
min_diff_so_far = float('inf')
for val0, val1 in zip(arr, arr[1:]):
diff = val1 - val0
if diff < min_diff_so_far:
min_diff_so_far = diff
... | minimum-absolute-difference | Beats 100% (292ms), sort + single scan in Python | kryuki | 4 | 355 | minimum absolute difference | 1,200 | 0.697 | Easy | 18,143 |
https://leetcode.com/problems/minimum-absolute-difference/discuss/1264206/Python3-Brute-Force-Solution-with-defaultdict | class Solution:
def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:
arr.sort()
diffs = defaultdict(list)
for i in range(len(arr) - 1):
diff = abs(arr[i] - arr[i + 1])
diffs[diff].append([arr[i] , arr[i + 1]])
... | minimum-absolute-difference | [Python3] Brute Force Solution with defaultdict | VoidCupboard | 3 | 87 | minimum absolute difference | 1,200 | 0.697 | Easy | 18,144 |
https://leetcode.com/problems/minimum-absolute-difference/discuss/1636776/Python3-O(nlogn)-Time-(beats-98)-or-Simple-%2B-Clean-%2B-Explanation-or-No-hashmap-or-1-Traversal-Only | class Solution:
def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:
minDif = float('+inf')
res = []
arr.sort()
for i in range(1, len(arr)):
d = arr[i] - arr[i - 1]
if d == minDif:
res.append([arr[i - 1], arr[i]])
elif... | minimum-absolute-difference | [Python3] O(nlogn) Time (beats 98%) | Simple + Clean + Explanation | No hashmap | 1 Traversal Only | PatrickOweijane | 2 | 197 | minimum absolute difference | 1,200 | 0.697 | Easy | 18,145 |
https://leetcode.com/problems/minimum-absolute-difference/discuss/1149275/Python3-1-pass | class Solution:
def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:
arr.sort()
ans = []
diff = inf
for i in range(1, len(arr)):
if arr[i] - arr[i-1] <= diff:
if arr[i] - arr[i-1] < diff:
ans = []
d... | minimum-absolute-difference | [Python3] 1-pass | ye15 | 2 | 119 | minimum absolute difference | 1,200 | 0.697 | Easy | 18,146 |
https://leetcode.com/problems/minimum-absolute-difference/discuss/1362155/Easy-Fast-Python-Solution-(faster-than-94) | class Solution:
def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:
temp = sorted(arr)
min_ = 9999999
ret = []
for i in range(len(temp)-1):
diff = abs(temp[i+1] - temp[i])
if diff < min_:
ret = []
min_ = diff
... | minimum-absolute-difference | Easy, Fast, Python Solution (faster than 94%) | the_sky_high | 1 | 249 | minimum absolute difference | 1,200 | 0.697 | Easy | 18,147 |
https://leetcode.com/problems/minimum-absolute-difference/discuss/1204035/Python3-Easy-Solution | class Solution:
def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:
x = sorted(arr)
s = []
count = 0
for i in range(len(x)-1):
s.append((abs(x[i]-x[i+1])))
minv = min(s)
s = []
for i in range(len(x)-1):
if minv == abs(x[i... | minimum-absolute-difference | Python3 Easy Solution | Sanyamx1x | 1 | 155 | minimum absolute difference | 1,200 | 0.697 | Easy | 18,148 |
https://leetcode.com/problems/minimum-absolute-difference/discuss/1047926/Python3-simple-solution-using-two-different-approaches | class Solution:
def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:
arr.sort()
min_diff = 100
for i in range(len(arr)-1):
if arr[i+1] - arr[i] < min_diff:
min_diff = arr[i+1] - arr[i]
l = []
for i in range(len(arr)-1):
if... | minimum-absolute-difference | Python3 simple solution using two different approaches | EklavyaJoshi | 1 | 91 | minimum absolute difference | 1,200 | 0.697 | Easy | 18,149 |
https://leetcode.com/problems/minimum-absolute-difference/discuss/1047926/Python3-simple-solution-using-two-different-approaches | class Solution:
def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:
arr.sort()
min_diff = 100
d = {}
for i in range(len(arr)-1):
if min_diff > (arr[i+1] - arr[i]):
min_diff = (arr[i+1] - arr[i])
if arr[i+1]- arr[i] in d:
... | minimum-absolute-difference | Python3 simple solution using two different approaches | EklavyaJoshi | 1 | 91 | minimum absolute difference | 1,200 | 0.697 | Easy | 18,150 |
https://leetcode.com/problems/minimum-absolute-difference/discuss/387763/Two-Solutions-in-Python-3-(beats-100)-(three-lines) | class Solution:
def minimumAbsDifference(self, A: List[int]) -> List[List[int]]:
L, D, m, _ = len(A), [], float('inf'), A.sort()
for i in range(L-1):
d = A[i+1] - A[i]
if d == m: D.append([A[i],A[i+1]])
elif d < m: D, m = [[A[i],A[i+1]]], d
return D | minimum-absolute-difference | Two Solutions in Python 3 (beats 100%) (three lines) | junaidmansuri | 1 | 565 | minimum absolute difference | 1,200 | 0.697 | Easy | 18,151 |
https://leetcode.com/problems/minimum-absolute-difference/discuss/387763/Two-Solutions-in-Python-3-(beats-100)-(three-lines) | class Solution:
def minimumAbsDifference(self, A: List[int]) -> List[List[int]]:
L, D, _ = len(A), collections.defaultdict(list), A.sort()
for i in range(L-1): D[A[i+1] - A[i]].append([A[i],A[i+1]])
return D[min(D.keys())]
- Junaid Mansuri
(LeetCode ID)@hotmail.com | minimum-absolute-difference | Two Solutions in Python 3 (beats 100%) (three lines) | junaidmansuri | 1 | 565 | minimum absolute difference | 1,200 | 0.697 | Easy | 18,152 |
https://leetcode.com/problems/minimum-absolute-difference/discuss/2820144/Simple-dictonary-solution | class Solution:
def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:
arr.sort()
dic = defaultdict(list)
for i in range(1,len(arr)):
diff = abs(arr[i-1]-arr[i])
dic[diff].append([arr[i-1],arr[i]])
min_diff = min(dic)
return dic[min_diff] | minimum-absolute-difference | Simple dictonary solution | aruj900 | 0 | 4 | minimum absolute difference | 1,200 | 0.697 | Easy | 18,153 |
https://leetcode.com/problems/minimum-absolute-difference/discuss/2807242/Python-3-lines-or-91-or-Short-easy-with-sort-%2B-zip | class Solution:
def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:
arr.sort()
min_diff = min(abs(prev - curr) for prev, curr in zip(arr[:-1], arr[1:]))
return [[prev, curr] for prev, curr in zip(arr[:-1], arr[1:]) if abs(curr - prev) == min_diff] | minimum-absolute-difference | [Python] 3 lines | 91% | Short, easy with sort + zip | Nezuko-NoBamboo | 0 | 5 | minimum absolute difference | 1,200 | 0.697 | Easy | 18,154 |
https://leetcode.com/problems/minimum-absolute-difference/discuss/2781449/Minimum-Absolute-Difference | class Solution:
def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:
d={}
arr.sort()
for i in range(len(arr)-1):
d[arr[i],arr[i+1]]=arr[i+1]-arr[i]
min_difference=min(d.values())
result=[]
for i in d.keys():
if d[i]==min_d... | minimum-absolute-difference | Minimum Absolute Difference | Harsh_Gautam45 | 0 | 9 | minimum absolute difference | 1,200 | 0.697 | Easy | 18,155 |
https://leetcode.com/problems/minimum-absolute-difference/discuss/2740264/Python-HashMap-Solution-with-sorting | class Solution:
def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:
d = {}
arr.sort()
for i in range(len(arr)-1):
if arr[i+1] - arr[i] not in d:
d[arr[i+1] - arr[i]] = [[arr[i], arr[i+1]]]
else:
d[arr[i+1] - arr[... | minimum-absolute-difference | Python HashMap Solution with sorting | theReal007 | 0 | 11 | minimum absolute difference | 1,200 | 0.697 | Easy | 18,156 |
https://leetcode.com/problems/minimum-absolute-difference/discuss/2712832/faster-than-96-of-python | class Solution:
def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:
arr.sort()
a = abs(arr[0] - arr[1])
for i in range(1, len(arr)):
if abs(arr[i] - arr[i - 1]) < a:
a = abs(arr[i] - arr[i - 1])
lst = []
for i in range(1, len(arr)):
... | minimum-absolute-difference | faster than 96% of python | dastankg | 0 | 14 | minimum absolute difference | 1,200 | 0.697 | Easy | 18,157 |
https://leetcode.com/problems/minimum-absolute-difference/discuss/2712826/faster-than-96-of-python | class Solution:
def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:
arr.sort()
a = abs(arr[0] - arr[1])
for i in range(1, len(arr)):
if abs(arr[i] - arr[i - 1]) < a:
a = abs(arr[i] - arr[i - 1])
lst = []
for i in range(1, len(arr)):
... | minimum-absolute-difference | faster than 96% of python | dastankg | 0 | 7 | minimum absolute difference | 1,200 | 0.697 | Easy | 18,158 |
https://leetcode.com/problems/minimum-absolute-difference/discuss/2685042/Python3-One-pass-easy-solution | class Solution:
def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:
# sort the list
arr.sort()
print(arr)
# find the minimum difference
current_min = 10**14
result = []
for idx, num in enumerate(arr[:-1]):
# check whether we are a... | minimum-absolute-difference | [Python3] - One pass, easy solution | Lucew | 0 | 9 | minimum absolute difference | 1,200 | 0.697 | Easy | 18,159 |
https://leetcode.com/problems/minimum-absolute-difference/discuss/2669511/Python%2BNumpy | class Solution:
def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:
import numpy as np
arr.sort()
arr=np.array(arr)
diff=np.diff(arr)
Min=np.min(diff)
Ind=np.where(diff==Min)[0]
return [[arr[Ind[i]],arr[Ind[i]+1]] for i in range(len(Ind))] | minimum-absolute-difference | Python+Numpy | Leox2022 | 0 | 3 | minimum absolute difference | 1,200 | 0.697 | Easy | 18,160 |
https://leetcode.com/problems/minimum-absolute-difference/discuss/2669084/USING-BRUTE-FORCE-oror-PYTHON | class Solution:
def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:
output=[]
arr.sort()
minn=[]
for i in range(len(arr)-1):
minn.append(arr[i+1]-arr[i])
min_arr=min(minn)
for j in range(len(arr)-1):
if (arr[j+1]-arr[j])==min_a... | minimum-absolute-difference | USING BRUTE FORCE || PYTHON | pratiyushray2152 | 0 | 3 | minimum absolute difference | 1,200 | 0.697 | Easy | 18,161 |
https://leetcode.com/problems/minimum-absolute-difference/discuss/2573119/Python-solution-using-sort-abs()-min-and-dictionary!O(nlogn) | class Solution:
def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:
result = []
dict_diff_nums = {}
arr.sort()
for i in range(len(arr) - 1):
diff = abs(arr[i] - arr[i+1])
dict_diff_nums[arr[i], arr[i+1]] = diff
minimum_diff = min(dict_d... | minimum-absolute-difference | Python solution using sort, abs(), min, and dictionary!O(nlogn) | samanehghafouri | 0 | 53 | minimum absolute difference | 1,200 | 0.697 | Easy | 18,162 |
https://leetcode.com/problems/minimum-absolute-difference/discuss/2463189/PYTHON-3-Simple-Solution-(Feedbacks-appreciated) | class Solution:
def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:
# I created a sorted copy of the array to not modify arr in-place. In case this needs to be implemented in further code.
ref_arr = sorted(arr)
min_diff = ref_arr[-1] - ref_arr[0]
# Initialize... | minimum-absolute-difference | [PYTHON 3] Simple Solution (Feedbacks appreciated) | Eli47 | 0 | 104 | minimum absolute difference | 1,200 | 0.697 | Easy | 18,163 |
https://leetcode.com/problems/minimum-absolute-difference/discuss/2299216/Easy-Python-solution-for-beginners....-o(n-log-n) | class Solution:
def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:
arr.sort()
diff=float(inf)
for i in range(0,len(arr)-1):
if arr[i+1]-arr[i]<diff:
diff=arr[i+1]-arr[i]
lst=[]
for i in range(0,len(arr)-1):
if arr[i+1]-a... | minimum-absolute-difference | Easy Python solution for beginners.... o(n log n) | guneet100 | 0 | 116 | minimum absolute difference | 1,200 | 0.697 | Easy | 18,164 |
https://leetcode.com/problems/minimum-absolute-difference/discuss/2095919/PYTHON-or-Super-Simple-Python-Solution | class Solution:
def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:
arr = sorted(arr)
pairMap = {}
min_diff = abs(arr[0] - arr[1])
for i in ran... | minimum-absolute-difference | PYTHON | Super Simple Python Solution | shreeruparel | 0 | 154 | minimum absolute difference | 1,200 | 0.697 | Easy | 18,165 |
https://leetcode.com/problems/minimum-absolute-difference/discuss/1986021/Python-Simple-and-Straight-Forward | class Solution:
def minimumAbsDifference(self, arr):
minAbsDiff, res = inf, []
for a,b in pairwise(sorted(arr)):
diff = b-a
if diff < minAbsDiff:
res.clear()
minAbsDiff = diff
if diff == minAbs... | minimum-absolute-difference | Python - Simple and Straight Forward | domthedeveloper | 0 | 131 | minimum absolute difference | 1,200 | 0.697 | Easy | 18,166 |
https://leetcode.com/problems/minimum-absolute-difference/discuss/1959163/easy-python-code | class Solution:
def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:
mini = None
pairs = []
arr.sort()
for i in range(len(arr)-1):
if mini == None:
mini = abs(arr[i]-arr[i+1])
pairs.append([arr[i],arr[i+1]])
elif m... | minimum-absolute-difference | easy python code | dakash682 | 0 | 101 | minimum absolute difference | 1,200 | 0.697 | Easy | 18,167 |
https://leetcode.com/problems/minimum-absolute-difference/discuss/1918895/Python-solution-faster-than-88 | class Solution:
def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:
arr.sort()
min_diff = min([arr[x+1] - arr[x] for x in range(len(arr)-1)])
res = []
for i in range(len(arr)-1):
if arr[i+1] - arr[i] == min_diff:
res.append([arr[i], arr[i+1]... | minimum-absolute-difference | Python solution faster than 88% | alishak1999 | 0 | 128 | minimum absolute difference | 1,200 | 0.697 | Easy | 18,168 |
https://leetcode.com/problems/minimum-absolute-difference/discuss/1794699/4-Lines-Python-Solution-oror-75-Faster-oror-Memory-less-than-70 | class Solution:
def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:
arr, mn = sorted(arr), 1000000
for i in range(len(arr)-1):
if arr[i+1]-arr[i] < mn: mn = arr[i+1]-arr[i]
return [[arr[i],arr[i+1]] for i in range(len(arr)-1) if arr[i+1]-arr[i]==mn] | minimum-absolute-difference | 4-Lines Python Solution || 75% Faster || Memory less than 70% | Taha-C | 0 | 138 | minimum absolute difference | 1,200 | 0.697 | Easy | 18,169 |
https://leetcode.com/problems/minimum-absolute-difference/discuss/1725880/Easy-solution-using-python3 | class Solution:
def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:
arr.sort()
li = []
for i in range(len(arr) - 1) :
dif = abs(arr[i+1] - arr[i])
li.append(dif)
minDiff = min(li)
ans = []
for i in range(len(arr) - 1... | minimum-absolute-difference | Easy solution using python3 | shakilbabu | 0 | 141 | minimum absolute difference | 1,200 | 0.697 | Easy | 18,170 |
https://leetcode.com/problems/minimum-absolute-difference/discuss/1637761/python-sorting-solution-or-constant-space | class Solution:
def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:
arr.sort()
minDiff = float('inf')
res = []
for i in range(1, len(arr)):
prev = arr[i - 1]
curr_diff = abs(prev - arr[i])
if curr_diff < minDiff:
res ... | minimum-absolute-difference | python sorting solution | constant space | abkc1221 | 0 | 39 | minimum absolute difference | 1,200 | 0.697 | Easy | 18,171 |
https://leetcode.com/problems/minimum-absolute-difference/discuss/1637240/python3-or-sort()-or-time-O(n-log-n)-or-space-O(n) | class Solution:
def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:
if len(arr)==1:
return []
arr.sort() # sort the array
n=len(arr)
min_diff=10**9
# calculate the min diff by iterrating through array
for i in range(1,n):
min_dif... | minimum-absolute-difference | python3 | sort() | time- O(n log n) | space -O(n) | Rohit_Patil | 0 | 31 | minimum absolute difference | 1,200 | 0.697 | Easy | 18,172 |
https://leetcode.com/problems/minimum-absolute-difference/discuss/1637216/Python3-Using-zip-saves-your-life-linear-one-pass | class Solution:
def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:
arr.sort()
mindiff = arr[-1]-arr[0]
res = []
for x,y in zip(arr[:-1],arr[1:]):
if y-x < mindiff:
res = []
mindiff = y-x
if y-x == mindiff:
... | minimum-absolute-difference | [Python3] Using zip saves your life - linear one pass | Rainyforest | 0 | 23 | minimum absolute difference | 1,200 | 0.697 | Easy | 18,173 |
https://leetcode.com/problems/minimum-absolute-difference/discuss/1637149/Using-zip()-and-sort()-to-solve-it-in-python3 | class Solution:
def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:
arr.sort()
arr2 = [abs(x-y) for x, y in zip(arr, arr[1:])]
m = min(arr2)
return [[arr[i], arr[i+1]] for i in range(len(arr2)) if arr2[i]==m] | minimum-absolute-difference | Using zip() and sort() to solve it in python3 | etu7912a482 | 0 | 20 | minimum absolute difference | 1,200 | 0.697 | Easy | 18,174 |
https://leetcode.com/problems/minimum-absolute-difference/discuss/1636935/Two-Liner-Solution-or-Iterative-Solution | class Solution:
def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:
arr.sort()
return[[arr[i],arr[i+1]] for i,j in enumerate([arr[i+1]-arr[i] for i in range(len(arr)-1)]) if j == min([arr[i+1]-arr[i] for i in range(len(arr)-1)])] | minimum-absolute-difference | Two Liner Solution | Iterative Solution | radar21 | 0 | 37 | minimum absolute difference | 1,200 | 0.697 | Easy | 18,175 |
https://leetcode.com/problems/minimum-absolute-difference/discuss/1636935/Two-Liner-Solution-or-Iterative-Solution | class Solution:
def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:
n = len(arr)
arr.sort()
diff = [arr[i+1]-arr[i] for i in range(n-1)]
final = min(diff)
return[[arr[i],arr[i+1]] for i,j in enumerate(diff) if j == final] | minimum-absolute-difference | Two Liner Solution | Iterative Solution | radar21 | 0 | 37 | minimum absolute difference | 1,200 | 0.697 | Easy | 18,176 |
https://leetcode.com/problems/minimum-absolute-difference/discuss/1636895/WEEB-DOES-PYTHON | class Solution:
def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:
arr.sort()
result = []
curMin = float("inf")
for i in range(len(arr)-1):
minDiff = abs(arr[i+1] - arr[i])
if minDiff < curMin:
result.clear()
curMin = minDiff
result.append([arr[i],arr[i+1]])
elif minDiff... | minimum-absolute-difference | WEEB DOES PYTHON | Skywalker5423 | 0 | 51 | minimum absolute difference | 1,200 | 0.697 | Easy | 18,177 |
https://leetcode.com/problems/minimum-absolute-difference/discuss/1569788/Python3-Solution-with-using-sorting | class Solution:
def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:
arr.sort()
res = []
_min = float('inf')
for i in range(len(arr) - 1):
_abs = abs(arr[i + 1] - arr[i])
if _abs < _min:
_min = _abs
res = ... | minimum-absolute-difference | [Python3] Solution with using sorting | maosipov11 | 0 | 193 | minimum absolute difference | 1,200 | 0.697 | Easy | 18,178 |
https://leetcode.com/problems/minimum-absolute-difference/discuss/1552961/python-easy-solution-with-stack | class Solution:
def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:
arr.sort()
ans = []
for i in range(1, len(arr)):
temp = abs(arr[i] - arr[i-1])
if ans == []:
ans.append([arr[i-1],arr[i]])
... | minimum-absolute-difference | python easy solution with stack | equus3144 | 0 | 75 | minimum absolute difference | 1,200 | 0.697 | Easy | 18,179 |
https://leetcode.com/problems/minimum-absolute-difference/discuss/1154315/Python-faster-than-99-O(nlogn) | class Solution:
def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:
arr.sort() # O(nlogn)
history, min_diff = {}, float("inf")
for i in range(len(arr)-1): # O(n)
if min_diff >= abs(arr[i] - arr[i+1]):
min_diff = abs(arr[i] - arr[i+1])
... | minimum-absolute-difference | Python faster than 99%, O(nlogn) | anylee2142 | 0 | 109 | minimum absolute difference | 1,200 | 0.697 | Easy | 18,180 |
https://leetcode.com/problems/minimum-absolute-difference/discuss/881143/Python-simple-solution-with-sorting-%2B-1-%22for%22-loop | class Solution:
def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:
res, d = [], float('Inf')
arr.sort()
for i, k in zip(arr[:-1], arr[1:]):
d_curr = k - i
if d_curr <= d:
if d_curr != d:
res, d = [], d_curr
... | minimum-absolute-difference | Python simple solution with sorting + 1 "for" loop | stom1407 | 0 | 92 | minimum absolute difference | 1,200 | 0.697 | Easy | 18,181 |
https://leetcode.com/problems/minimum-absolute-difference/discuss/416772/Python3-5-lines-clear-code-beat-100-in-O(n) | class Solution:
def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:
arr.sort()
temp = arr[1] - arr[0]
for i in range(len(arr) - 1):
temp = min(temp ,arr[i+1] - arr[i] )
res = [[arr[i] , arr[i+1]] for i in range(len(arr)-1) if arr[i+1] - ar... | minimum-absolute-difference | Python3 5 lines clear code beat 100% in O(n) | macqueen | -2 | 185 | minimum absolute difference | 1,200 | 0.697 | Easy | 18,182 |
https://leetcode.com/problems/ugly-number-iii/discuss/723589/Python3-inconsistent-definition-of-%22ugly-numbers%22 | class Solution:
def nthUglyNumber(self, n: int, a: int, b: int, c: int) -> int:
# inclusion-exclusion principle
ab = a*b//gcd(a, b)
bc = b*c//gcd(b, c)
ca = c*a//gcd(c, a)
abc = ab*c//gcd(ab, c)
lo, hi = 1, n*min(a, b, c)
while lo < hi:
m... | ugly-number-iii | [Python3] inconsistent definition of "ugly numbers" | ye15 | 33 | 1,000 | ugly number iii | 1,201 | 0.285 | Medium | 18,183 |
https://leetcode.com/problems/ugly-number-iii/discuss/2299901/PYTHON-or-BRUTE-FORCE-TO-OPTIMZIED-SOL-or-FULL-DETAILED-EXPLANATIONor | class Solution:
def nthUglyNumber(self, n: int, a: int, b: int, c: int) -> int:
times = [1,1,1]
smallest = inf
while n != 0:
smallest = min ( times[0]*a,times[1]*b,times[2]*c)
if times[0]*a == smallest: times[0] += 1
if times[1]*b == smallest: times[1] += ... | ugly-number-iii | PYTHON | BRUTE FORCE TO OPTIMZIED SOL | FULL DETAILED EXPLANATION| | reaper_27 | 3 | 132 | ugly number iii | 1,201 | 0.285 | Medium | 18,184 |
https://leetcode.com/problems/ugly-number-iii/discuss/2299901/PYTHON-or-BRUTE-FORCE-TO-OPTIMZIED-SOL-or-FULL-DETAILED-EXPLANATIONor | class Solution:
def nthUglyNumber(self, n: int, a: int, b: int, c: int) -> int:
a,b,c = sorted((a,b,c))
ans = inf
def hcf(a,b):
if a %b == 0: return b
return hcf(b , a % b)
p,q,r= hcf(a,b),hcf(b,c),hcf(a,c)
s = hcf(r,b)
x1 = (a*b) // p
... | ugly-number-iii | PYTHON | BRUTE FORCE TO OPTIMZIED SOL | FULL DETAILED EXPLANATION| | reaper_27 | 3 | 132 | ugly number iii | 1,201 | 0.285 | Medium | 18,185 |
https://leetcode.com/problems/ugly-number-iii/discuss/1457965/Simple-Binary-Search-Python3 | class Solution:
def nthUglyNumber(self, n: int, a: int, b: int, c: int) -> int:
ab = a*b//math.gcd(a,b)
ac = a*c//math.gcd(a,c)
bc = b*c//math.gcd(b,c)
abc = a*bc//math.gcd(a,bc)
def enough(m):
tot = m//a + m//b + m//c - m//ab - m//bc -m//ac +m//abc
... | ugly-number-iii | Simple Binary Search [Python3] | rudr | 0 | 285 | ugly number iii | 1,201 | 0.285 | Medium | 18,186 |
https://leetcode.com/problems/ugly-number-iii/discuss/387749/Solution-in-Python-3-(beats-100.00-)-(Binary-Search)-(ten-lines) | class Solution:
def nthUglyNumber(self, n: int, a: int, b: int, c: int) -> int:
[a,b,c] = sorted([a,b,c])
if a == 1: return n
def lcm(x,y): return x*y//math.gcd(x,y)
AB, BC, AC, ABC, r, s = lcm(a,b), lcm(b,c), lcm(a,c), lcm(lcm(a,b),c), n*a//3, n*a+1
def unc(x): return x//a +... | ugly-number-iii | Solution in Python 3 (beats 100.00 %) (Binary Search) (ten lines) | junaidmansuri | 0 | 480 | ugly number iii | 1,201 | 0.285 | Medium | 18,187 |
https://leetcode.com/problems/smallest-string-with-swaps/discuss/1985185/Python3-UNION-FIND-()**-Explained | class Solution:
def union(self, a, b):
self.parent[self.find(a)] = self.find(b)
def find(self, a):
if self.parent[a] != a:
self.parent[a] = self.find(self.parent[a])
return self.parent[a]
def smallestStringWithSwaps(self, s: str, pairs: List[List[int]]) -> st... | smallest-string-with-swaps | ✔️ [Python3] UNION-FIND (❁´▽`❁)*✲゚*, Explained | artod | 55 | 2,700 | smallest string with swaps | 1,202 | 0.576 | Medium | 18,188 |
https://leetcode.com/problems/smallest-string-with-swaps/discuss/1985417/Easy-Python-Solution-oror-Union-Find-Explanation-oror-faster-than-94.67 | class Solution:
def smallestStringWithSwaps(self, s: str, pairs: List[List[int]]) -> str:
# Start of Union Find Data Structure
p = list(range(len(s))) # parent
# each element in the pairs == node
# used to store each node's parent based on its index
# eg. pairs = [[0,3],[1,2... | smallest-string-with-swaps | ✅✅✅Easy [Python] Solution || Union Find Explanation || faster than 94.67% | ziqinyeow | 4 | 456 | smallest string with swaps | 1,202 | 0.576 | Medium | 18,189 |
https://leetcode.com/problems/smallest-string-with-swaps/discuss/1501837/Clean-and-Efficient-Python3-Solution-Using-the-UnionFind-Paradigm | class Solution:
def smallestStringWithSwaps(self, s: str, pairs: List[List[int]]) -> str:
num_nodes = len(s)
self.uf = UnionFind(num_nodes)
for i,j in pairs:
self.uf.union(i,j)
indexes_by_root = {}
chars_by_root = {}
for i in range(num_nodes):
... | smallest-string-with-swaps | Clean and Efficient Python3 Solution Using the UnionFind Paradigm | sayan007 | 4 | 349 | smallest string with swaps | 1,202 | 0.576 | Medium | 18,190 |
https://leetcode.com/problems/smallest-string-with-swaps/discuss/1985347/Pythonoror98.27-DFS | class Solution:
def smallestStringWithSwaps(self, s: str, pairs: List[List[int]]) -> str:
def dfs(idx, arr):
arr.append(idx)
visited[idx] = True
for nei in graph[idx]:
if not visited[nei]:
visited[nei] = True
dfs(nei... | smallest-string-with-swaps | Python||98.27% DFS | gulugulugulugulu | 2 | 129 | smallest string with swaps | 1,202 | 0.576 | Medium | 18,191 |
https://leetcode.com/problems/smallest-string-with-swaps/discuss/2308250/PYTHON-or-DFS-or-GRAPH-BASED-SOL-or-EXPLAINED-or-EASY-or | class Solution:
def smallestStringWithSwaps(self, s: str, pairs: List[List[int]]) -> str:
n = len(s)
visited = [False]*n
canGo = defaultdict(list)
mainList = []
for i,j in pairs:
canGo[i].append(j)
canGo[j].append(i)
def dfs(n... | smallest-string-with-swaps | PYTHON | DFS | GRAPH BASED SOL | EXPLAINED | EASY | | reaper_27 | 1 | 66 | smallest string with swaps | 1,202 | 0.576 | Medium | 18,192 |
https://leetcode.com/problems/smallest-string-with-swaps/discuss/1692768/Python-solution-or-92-faster | class Solution:
def smallestStringWithSwaps(self, s: str, pairs: List[List[int]]) -> str:
n = len(s)
parent = list(range(n))
rank = [1]*n
def find(node):
if parent[node]!=node:
parent[node] = find(parent[node])
return parent[node]
def u... | smallest-string-with-swaps | Python solution | 92% faster | 1579901970cg | 1 | 244 | smallest string with swaps | 1,202 | 0.576 | Medium | 18,193 |
https://leetcode.com/problems/smallest-string-with-swaps/discuss/1692768/Python-solution-or-92-faster | class Solution:
def smallestStringWithSwaps(self, s: str, pairs: List[List[int]]) -> str:
n = len(s)
l = [[] for _ in range(n)]
parent = list(range(n))
for n1,n2 in pairs:
l[n1].append(n2)
l[n2].append(n1)
d = defaultdict(list)
visited = set()
... | smallest-string-with-swaps | Python solution | 92% faster | 1579901970cg | 1 | 244 | smallest string with swaps | 1,202 | 0.576 | Medium | 18,194 |
https://leetcode.com/problems/smallest-string-with-swaps/discuss/1653105/92-faster-oror-Thought-Process-oror-For-Beginners-oror-Easy-and-Concise | class Solution:
def smallestStringWithSwaps(self, s: str, pairs: List[List[int]]) -> str:
graph = defaultdict(list)
for a,b in pairs:
graph[a].append(b)
graph[b].append(a)
col = []
self.seen = set()
def grp_making(point):
nonlocal local
if point in self.seen:
return
local.append(point)
... | smallest-string-with-swaps | 📌📌 92% faster || Thought Process || For Beginners || Easy & Concise 🐍 | abhi9Rai | 1 | 245 | smallest string with swaps | 1,202 | 0.576 | Medium | 18,195 |
https://leetcode.com/problems/smallest-string-with-swaps/discuss/1469915/Python-UnionFind-with-step-by-step-explanation | class Solution:
def smallestStringWithSwaps(self, s: str, pairs: List[List[int]]) -> str:
'''
Take s = "dcab", pairs = [[0,3],[1,2]] as example.
1. At first we create UnionFind structure.
2. Next step is filling it with pairs. Here let's
look more precisely: self... | smallest-string-with-swaps | Python UnionFind with step-by-step explanation | SleeplessChallenger | 1 | 294 | smallest string with swaps | 1,202 | 0.576 | Medium | 18,196 |
https://leetcode.com/problems/smallest-string-with-swaps/discuss/2833578/python-union-find | class Solution:
def smallestStringWithSwaps(self, s: str, pairs: List[List[int]]) -> str:
u = [i for i in range(len(s))]
def find_root(n):
p = u[n]
if p == n:
return p
else:
r = find_root(p)
u[n] = r
... | smallest-string-with-swaps | python union find | xsdnmg | 0 | 2 | smallest string with swaps | 1,202 | 0.576 | Medium | 18,197 |
https://leetcode.com/problems/smallest-string-with-swaps/discuss/2072337/Python-fast-and-simple-(98-Runtime-98-Memory) | class Solution:
def smallestStringWithSwaps(self, s: str, pairs: List[List[int]]) -> str:
v = defaultdict(list)
for a, b in pairs:
v[a].append(b)
v[b].append(a)
s = list(s)
idxs, chrs = [], []
for i in range(len(s)):
... | smallest-string-with-swaps | Python, fast and simple (98% Runtime, 98% Memory) | MihailP | 0 | 98 | smallest string with swaps | 1,202 | 0.576 | Medium | 18,198 |
https://leetcode.com/problems/smallest-string-with-swaps/discuss/1986752/Python-Union-find-solution | class Solution:
def find_parent(self, parent, x) -> int:
if parent[x] != x:
parent[x] = self.find_parent(parent, parent[x])
return parent[x]
def union_parent(self, parent, a, b):
a = self.find_parent(parent, a)
b = self.find_parent(parent, b)
if a < b:
... | smallest-string-with-swaps | Python Union find solution | LeetMus | 0 | 37 | smallest string with swaps | 1,202 | 0.576 | Medium | 18,199 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.