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/beautiful-arrangement/discuss/872354/Python3-backtracking | class Solution:
def countArrangement(self, N: int) -> int:
def fn(k):
"""Return number of beautiful arrangements."""
if k == 1: return 1 # boundary condition
ans = 0
for kk in range(k):
if nums[kk] % k == 0 or k % nums[kk] == 0:
... | beautiful-arrangement | [Python3] backtracking | ye15 | 2 | 225 | beautiful arrangement | 526 | 0.646 | Medium | 9,300 |
https://leetcode.com/problems/beautiful-arrangement/discuss/2688753/Easy-to-understand-python-oror-Backtracking-Approach | class Solution:
def countArrangement(self, n: int) -> int:
res=[0]
def dfs(i,arr):
if i==n+1:
res[0]+=1
return
m=len(arr)
for j in range(m):
e=arr.pop(0)
if (not i%e) or (not e%i):
... | beautiful-arrangement | Easy to understand python || Backtracking Approach | code_is_in_my_veins | 0 | 7 | beautiful arrangement | 526 | 0.646 | Medium | 9,301 |
https://leetcode.com/problems/beautiful-arrangement/discuss/2634029/Python3-Easy-to-Understand-Approach-oror-Optimized-Brute-Force-Approach | class Solution:
def countArrangement(self, n: int) -> int:
def permute(i):
if i == len(t):
self.ans+=1
for j in range(i,n):
t[i],t[j] = t[j],t[i]
if t[i] % (i+1) == 0 or (i+1) % t[i] == 0:
permute(i+1)
... | beautiful-arrangement | Python3 Easy to Understand Approach || Optimized Brute Force Approach | hoo__mann | 0 | 15 | beautiful arrangement | 526 | 0.646 | Medium | 9,302 |
https://leetcode.com/problems/beautiful-arrangement/discuss/2492282/Python-(Simple-Solution-and-Beginner-Friendly) | class Solution:
def countArrangement(self, n: int) -> int:
res = []
self.backtrack(n, [], res)
return len(res)
def backtrack(self, n, curr, res):
if len(curr) == n:
res.append(curr[:])
for i in range(1, n+1):
leng = len(curr)+1
if i in ... | beautiful-arrangement | Python (Simple Solution and Beginner-Friendly) | vishvavariya | 0 | 102 | beautiful arrangement | 526 | 0.646 | Medium | 9,303 |
https://leetcode.com/problems/beautiful-arrangement/discuss/2478569/Python3-recursion-with-dictionary-lookup | class Solution:
def countArrangement(self, n: int) -> int:
# Work out which digits can go in which positions and store in options
options = {}
for i in range(1,n+1):
options[i] = []
for j in range(1,n+1):
if i % j == 0 or j % i == 0:
... | beautiful-arrangement | Python3 recursion with dictionary lookup | tawaca | 0 | 48 | beautiful arrangement | 526 | 0.646 | Medium | 9,304 |
https://leetcode.com/problems/beautiful-arrangement/discuss/2460961/Python3-or-Backtracking-Approach-That-Builds-Up-to-Beautiful-Arrangement | class Solution:
def countArrangement(self, n: int) -> int:
#Backtracking approach: Start with empty array and build up to beautiful permutation!
#We will consider all elements in range from [1, n] that are not previously used that
#could potentially be added to next empty position!... | beautiful-arrangement | Python3 | Backtracking Approach That Builds Up to Beautiful Arrangement | JOON1234 | 0 | 47 | beautiful arrangement | 526 | 0.646 | Medium | 9,305 |
https://leetcode.com/problems/beautiful-arrangement/discuss/2460887/Python3-or-Backtracking-%2B-Recursion | class Solution:
#Time-Complexity: Definitely faster than O(n!) since you don't recurse along every path poss
#ible!
#Space-Complexity: O(n), since in worst case, the recursion will utilize stack frame, which
#will be at most stack depth of n! Also, arr array takes up n integers always!
def countArra... | beautiful-arrangement | Python3 | Backtracking + Recursion | JOON1234 | 0 | 44 | beautiful arrangement | 526 | 0.646 | Medium | 9,306 |
https://leetcode.com/problems/beautiful-arrangement/discuss/1998513/python-easy-understandable-dfs-solution-(backtracking) | class Solution:
def countArrangement(self, n: int) -> int:
res = 0
def cond(m, n):
return m % n == 0 or n % m == 0
stack = [([], set())] # path, visited
while stack:
path, visited = stack.pop()
idx = len(path)
... | beautiful-arrangement | python easy-understandable dfs solution (backtracking) | byuns9334 | 0 | 154 | beautiful arrangement | 526 | 0.646 | Medium | 9,307 |
https://leetcode.com/problems/beautiful-arrangement/discuss/1629288/bitmask-based-dynamic-programming | class Solution:
def countArrangement(self, n: int) -> int:
@lru_cache(maxsize = None)
def helper(ind, mask):
if ind > n:
return 1
total_count = 0
for i in range(len(mask)):
if mask[i] == 0:
... | beautiful-arrangement | bitmask-based dynamic programming | throwawayleetcoder19843 | 0 | 86 | beautiful arrangement | 526 | 0.646 | Medium | 9,308 |
https://leetcode.com/problems/beautiful-arrangement/discuss/1537533/WEEB-DOES-PYTHON-BFS | class Solution:
def countArrangement(self, n: int) -> int:
nums = [i for i in range(1, n+1)]
queue = deque([(nums, 0)])
return self.bfs(queue, len(nums))
def bfs(self, queue, target):
result = 0
while queue:
curNums, idx = queue.popleft()
if idx == target:
result += 1
for i in range(len(cur... | beautiful-arrangement | WEEB DOES PYTHON BFS | Skywalker5423 | 0 | 138 | beautiful arrangement | 526 | 0.646 | Medium | 9,309 |
https://leetcode.com/problems/beautiful-arrangement/discuss/1510482/python-backtracking-very-easy-to-understand | class Solution:
def countArrangement(self, n: int) -> int:
graph = defaultdict(set)
for i in range(1, n):
for j in range(i+1, n+1):
graph[i].add(j)
graph[j].add(i)
def dfs(m):
res = 0
... | beautiful-arrangement | python backtracking, very easy to understand | byuns9334 | 0 | 303 | beautiful arrangement | 526 | 0.646 | Medium | 9,310 |
https://leetcode.com/problems/beautiful-arrangement/discuss/1476086/Python3-Solution-with-using-backtracking | class Solution:
def __init__(self):
self.count = 0
def backtracking(self, nums, combs):
if len(nums) == 0:
self.count += 1
return
for idx in range(len(nums)):
if nums[idx] % (len(combs) + 1) == 0 or (len(combs) + 1) % nums[idx] == 0:
... | beautiful-arrangement | [Python3] Solution with using backtracking | maosipov11 | 0 | 161 | beautiful arrangement | 526 | 0.646 | Medium | 9,311 |
https://leetcode.com/problems/beautiful-arrangement/discuss/1426604/Python-Clean-Backtracking-template | class Solution:
def countArrangement(self, n: int) -> int:
def backtrack(arrangement = []):
nonlocal number
if len(arrangement) == n:
number += 1
return
for index in range(1, n+1):
if visited[index]: continue
... | beautiful-arrangement | [Python] Clean Backtracking template | soma28 | 0 | 489 | beautiful arrangement | 526 | 0.646 | Medium | 9,312 |
https://leetcode.com/problems/beautiful-arrangement/discuss/1391167/Python-or-DFS-or-Backtracking | class Solution:
def countArrangement(self, n: int) -> int:
arr=[i+1 for i in range(n)]
self.ans=0
def DFS(arr,ind):
if not arr:
self.ans+=1
for i in range(len(arr)):
if arr[i]%ind==0 or ind%arr[i]==0:
DFS(arr[:i]+arr... | beautiful-arrangement | Python | DFS | Backtracking | heckt27 | 0 | 125 | beautiful arrangement | 526 | 0.646 | Medium | 9,313 |
https://leetcode.com/problems/beautiful-arrangement/discuss/977408/Intuitive-approach-by-recursive-search | class Solution:
def countArrangement(self, N: int) -> int:
n_list = [i+1 for i in range(N)]
a = 0
def search_ba(rest_ns, curr_ns):
if not rest_ns:
# Found beautiful arrangement
nonlocal a
a +=1
return
... | beautiful-arrangement | Intuitive approach by recursive search | puremonkey2001 | 0 | 44 | beautiful arrangement | 526 | 0.646 | Medium | 9,314 |
https://leetcode.com/problems/random-pick-with-weight/discuss/1535699/Better-than-96.5 | class Solution:
def __init__(self, w: List[int]):
self.li = []
ma = sum(w)
for i, weight in enumerate(w):
ratio = ceil(weight / ma * 100)
self.li += ([i] * ratio)
def pickIndex(self) -> int:
return random.choice(self.li) | random-pick-with-weight | Better than 96.5% | josephp27 | 8 | 815 | random pick with weight | 528 | 0.461 | Medium | 9,315 |
https://leetcode.com/problems/random-pick-with-weight/discuss/1115275/99.8-faster-solution-in-Python-with-explanation | class Solution:
def __init__(self, w: List[int]):
self.w = w
self.c = [0] * len(w)
s = sum(w)
self.c[0] = w[0]/s
for i in range(1, len(w)):
self.c[i] = self.c[i-1] + w[i]/s
def pickIndex(self) -> int:
pr = random.random()
return bisect.bisect... | random-pick-with-weight | 99.8% faster solution in Python with explanation | akhileshravi | 6 | 888 | random pick with weight | 528 | 0.461 | Medium | 9,316 |
https://leetcode.com/problems/random-pick-with-weight/discuss/2667808/Python-Roblox-VO-Preparation%3A-Prefix-Sum-%2B-Binary-Search-Dichotomy-Solution | class Solution:
def __init__(self, w: List[int]):
self.total = sum(w)
pre_sum = [0 for i in range(len(w))]
for i in range(len(w)):
if i == 0:
pre_sum[i] = w[i]
else:
pre_sum[i] = w[i] + pre_sum[i - 1]
# print(pre_sum)
... | random-pick-with-weight | [Python] Roblox VO Preparation: Prefix Sum + Binary Search / Dichotomy Solution | bbshark | 2 | 205 | random pick with weight | 528 | 0.461 | Medium | 9,317 |
https://leetcode.com/problems/random-pick-with-weight/discuss/1733448/Python3-BinSearch-%2B-Prefix-Sum-%2B-Question-on-random-num-generation | class Solution:
def __init__(self, w: List[int]):
self.prefix = [w[0]]
for weight in w[1:]:
pre = weight + self.prefix[-1]
self.prefix.append(pre)
start = 0
self.buckets = []
for pre in self.prefix:
self.buckets.append([start,pre]... | random-pick-with-weight | [Python3] BinSearch + Prefix Sum + Question on random num generation | mahib | 1 | 236 | random pick with weight | 528 | 0.461 | Medium | 9,318 |
https://leetcode.com/problems/random-pick-with-weight/discuss/1272454/Easy-%2B-Clean-Python-Beats-97 | class Solution:
def __init__(self, w: List[int]):
self.t = sum(w)
self.probs = [w[0]/self.t]
for i in w[1:]:
self.probs.append(self.probs[-1] + (i / self.t))
def pickIndex(self) -> int:
rnd = random.random()
idx = bisect.bisect(self.probs, rnd)
retur... | random-pick-with-weight | Easy + Clean Python Beats 97% | Pythagoras_the_3rd | 1 | 361 | random pick with weight | 528 | 0.461 | Medium | 9,319 |
https://leetcode.com/problems/random-pick-with-weight/discuss/672005/Python3-randint-and-bisect | class Solution:
def __init__(self, w: List[int]):
self.w = w
for i in range(1, len(self.w)): self.w[i] += self.w[i-1]
def pickIndex(self) -> int:
return bisect_left(self.w, randint(1, self.w[-1])) | random-pick-with-weight | [Python3] randint & bisect | ye15 | 1 | 193 | random pick with weight | 528 | 0.461 | Medium | 9,320 |
https://leetcode.com/problems/random-pick-with-weight/discuss/672005/Python3-randint-and-bisect | class Solution:
def __init__(self, w: List[int]):
self.prefix = [0]
for x in w: self.prefix.append(self.prefix[-1] + x)
def pickIndex(self) -> int:
r = randint(1, self.prefix[-1])
def fn(arr, x):
"""Return the position of x in arr."""
lo, hi = 0... | random-pick-with-weight | [Python3] randint & bisect | ye15 | 1 | 193 | random pick with weight | 528 | 0.461 | Medium | 9,321 |
https://leetcode.com/problems/random-pick-with-weight/discuss/481529/Python3-two-different-solutions | class Solution:
def __init__(self, w: List[int]):
self.weight=list(itertools.accumulate(w))
def pickIndex(self) -> int:
return bisect.bisect_left(self.weight,random.randint(1,self.weight[-1]))
class Solution1:
def __init__(self, w: List[int]):
self.weight=w
... | random-pick-with-weight | Python3 two different solutions | jb07 | 1 | 312 | random pick with weight | 528 | 0.461 | Medium | 9,322 |
https://leetcode.com/problems/random-pick-with-weight/discuss/2744657/Python3-Prefix-Sums-with-Binary-Search | class Solution:
def __init__(self, w: List[int]):
self.cum_sum = 0
self.cum_sums = []
for i, weight in enumerate(w):
self.cum_sum += weight
self.cum_sums.append(self.cum_sum)
def pickIndex(self) -> int:
target = self.cum_sum * random.uniform(0, ... | random-pick-with-weight | Python3 Prefix Sums with Binary Search | jonathanbrophy47 | 0 | 9 | random pick with weight | 528 | 0.461 | Medium | 9,323 |
https://leetcode.com/problems/random-pick-with-weight/discuss/2268243/Python-binary-search-with-bisect_left | class Solution:
def __init__(self, w: List[int]):
s = 0
self.weight = []
for weight in w:
s += weight
self.weight.append(s)
def pickIndex(self) -> int:
return bisect_left(self.weight, random.randint(1, self.weight[... | random-pick-with-weight | Python, binary search with bisect_left | blue_sky5 | 0 | 38 | random pick with weight | 528 | 0.461 | Medium | 9,324 |
https://leetcode.com/problems/random-pick-with-weight/discuss/2264585/Python-solution-with-Prefix-Sum-and-Binary-Search | class Solution:
preSum = []
def __init__(self, w: List[int]):
self.preSum = [0] * (len(w) + 1)
for i in range(1, len(w)+1):
# preSum[i] = sum(w[0,i-1])
self.preSum[i] = self.preSum[i-1] + w[i-1]
def pickIndex(self) -> int:
n = len(self.preSum)
... | random-pick-with-weight | Python solution with Prefix Sum and Binary Search | leqinancy | 0 | 66 | random pick with weight | 528 | 0.461 | Medium | 9,325 |
https://leetcode.com/problems/random-pick-with-weight/discuss/1897930/Python-Clean-and-Simple!-One-Liner-with-Explanation!-Accumulating-weights-%2B-Binary-Search | class Solution:
def __init__(self, w):
runningSum, accWeights = 0, []
for x in w:
runningSum += x
accWeights.append(runningSum)
self.acc = accWeights
self.finalSum = runningSum
def pickIndex(self):
num = random.randint(1, self.fi... | random-pick-with-weight | Python - Clean and Simple! One Liner with Explanation! Accumulating weights + Binary Search | domthedeveloper | 0 | 247 | random pick with weight | 528 | 0.461 | Medium | 9,326 |
https://leetcode.com/problems/random-pick-with-weight/discuss/1897930/Python-Clean-and-Simple!-One-Liner-with-Explanation!-Accumulating-weights-%2B-Binary-Search | class Solution:
def __init__(self, w):
self.acc = list(accumulate(w))
self.sum = self.acc[-1]
def pickIndex(self):
num = random.randint(1, self.sum)
idx = bisect.bisect_left(self.acc, num)
return idx | random-pick-with-weight | Python - Clean and Simple! One Liner with Explanation! Accumulating weights + Binary Search | domthedeveloper | 0 | 247 | random pick with weight | 528 | 0.461 | Medium | 9,327 |
https://leetcode.com/problems/random-pick-with-weight/discuss/1897930/Python-Clean-and-Simple!-One-Liner-with-Explanation!-Accumulating-weights-%2B-Binary-Search | class Solution:
def __init__(self, w): self.acc = list(accumulate(w))
def pickIndex(self): return bisect_left(self.acc,randint(1,self.acc[-1])) | random-pick-with-weight | Python - Clean and Simple! One Liner with Explanation! Accumulating weights + Binary Search | domthedeveloper | 0 | 247 | random pick with weight | 528 | 0.461 | Medium | 9,328 |
https://leetcode.com/problems/random-pick-with-weight/discuss/1202237/Python-cumulative-weights-%2B-binary-search-(visual-example) | class Solution:
def __init__(self, w: List[int]):
for i in range(1, len(w)):
w[i] += w[i-1]
self.presum = w
def pickIndex(self) -> int:
r = random.randint(1, self.presum[-1])
lo, hi = 0, len(self.presum)-1
while lo < hi:
mid = (lo + hi) // 2
if self.presum[mid] < r:
lo = mid + 1
else:
h... | random-pick-with-weight | Python cumulative weights + binary search (visual example) | coco987 | 0 | 133 | random pick with weight | 528 | 0.461 | Medium | 9,329 |
https://leetcode.com/problems/random-pick-with-weight/discuss/872371/Python3-binary-search-in-weight-space | class Solution:
def __init__(self, w: List[int]):
self.prefix = [0]
for x in w: self.prefix.append(self.prefix[-1] + x)
def pickIndex(self) -> int:
r = randint(1, self.prefix[-1])
return bisect_left(self.prefix, r)-1 | random-pick-with-weight | [Python3] binary search in weight space | ye15 | 0 | 102 | random pick with weight | 528 | 0.461 | Medium | 9,330 |
https://leetcode.com/problems/minesweeper/discuss/875335/Python-3-or-Ad-hoc-DFS-or-Explanation | class Solution:
def updateBoard(self, board: List[List[str]], click: List[int]) -> List[List[str]]:
m, n = len(board), len(board[0])
def dfs(x, y):
if board[x][y] == 'M': board[x][y] = 'X'
elif board[x][y] == 'E':
cnt, nei = 0, []
for i, j in m... | minesweeper | Python 3 | Ad-hoc DFS | Explanation | idontknoooo | 5 | 743 | minesweeper | 529 | 0.655 | Medium | 9,331 |
https://leetcode.com/problems/minesweeper/discuss/1335534/Python3-recursive-solution | class Solution:
def updateBoard(self, board: List[List[str]], click: List[int]) -> List[List[str]]:
x,y = click[0],click[1]
options = []
if board[x][y] == "M":
board[x][y] = "X"
else:
options = [(0,1),(0,-1),(1,0),(1,-1),(1,1),(-1,-1),(-1,0),(-1,1)]
... | minesweeper | Python3 recursive solution | EklavyaJoshi | 4 | 212 | minesweeper | 529 | 0.655 | Medium | 9,332 |
https://leetcode.com/problems/minesweeper/discuss/1980661/Python-Clean-BFS-Solution-with-explanation | class Solution:
def traverse(self, board, row, col):
n, m = len(board), len(board[0])
for dr, dc in [[-1, -1], [-1, 0], [-1, 1], [0, -1], [0, 1], [1, -1], [1, 0], [1, 1]]:
newRow = row + dr
newCol = col + dc
if newRow >= 0 and newRow < n ... | minesweeper | [Python] Clean BFS Solution with explanation | vladimir_polyakov | 1 | 238 | minesweeper | 529 | 0.655 | Medium | 9,333 |
https://leetcode.com/problems/minesweeper/discuss/1676372/Python-dfs | class Solution:
def updateBoard(self, board: List[List[str]], click: List[int]) -> List[List[str]]:
neighbors = [(-1, 0), (0, -1), (1, 0), (0, 1),
(-1, -1), (1, -1), (1, 1), (-1, 1)]
def add_mine(mines, r, c):
if r < 0 or r >= m or c < 0 or c >= n or board... | minesweeper | Python, dfs | blue_sky5 | 1 | 152 | minesweeper | 529 | 0.655 | Medium | 9,334 |
https://leetcode.com/problems/minesweeper/discuss/748656/Python-DFS-Clear-Solution | class Solution:
# Time: O(mn)
# Space: O(mn)
def updateBoard(self, board: List[List[str]], click: List[int]) -> List[List[str]]:
i, j = click[0], click[1]
if board[i][j] == "M":
board[i][j] = "X"
return board
self.dfs(board, i, j)
return board
... | minesweeper | Python DFS Clear Solution | whissely | 1 | 174 | minesweeper | 529 | 0.655 | Medium | 9,335 |
https://leetcode.com/problems/minesweeper/discuss/732584/Python-Simple-short-DFS-solution | class Solution:
def updateBoard(self, board: List[List[str]], click: List[int]) -> List[List[str]]:
# DFS
(row, col), directions = click, ((-1, 0), (1, 0), (0, 1), (0, -1), (-1, 1), (-1, -1), (1, 1), (1, -1))
if 0 <= row < len(board) and 0 <= col < len(board[0]):
if boar... | minesweeper | Python - Simple short DFS solution | nbismoi | 1 | 120 | minesweeper | 529 | 0.655 | Medium | 9,336 |
https://leetcode.com/problems/minesweeper/discuss/2814389/python-super-easy-to-understand-dfs | class Solution:
def updateBoard(self, board: List[List[str]], click: List[int]) -> List[List[str]]:
def adj_mine_count(i,j):
mine_count = 0
for x in range(-1, 2):
for y in range(-1, 2):
if x != 0 or y != 0:
if i+y ... | minesweeper | python super easy to understand dfs | harrychen1995 | 0 | 4 | minesweeper | 529 | 0.655 | Medium | 9,337 |
https://leetcode.com/problems/minesweeper/discuss/2730522/Typical-DFS-solution-in-Python | class Solution:
def updateBoard(self, board: List[List[str]], click: List[int]) -> List[List[str]]:
#edge cases
if board[click[0]][click[1]] == 'M':
board[click[0]][click[1]] = 'X'
return board
if board[click[0]][click[1]] == 'B' or board[click[0]][click[1]] in ['1','... | minesweeper | Typical DFS solution in Python | Arana | 0 | 11 | minesweeper | 529 | 0.655 | Medium | 9,338 |
https://leetcode.com/problems/minesweeper/discuss/2718511/Python3-Quick-and-commented-BFS | class Solution:
def updateBoard(self, board: List[List[str]], click: List[int]) -> List[List[str]]:
# check whether we hit a mine
if board[click[0]][click[1]] == 'M':
board[click[0]][click[1]] = 'X'
return board
m = len(board)
n = len(board[0])
... | minesweeper | [Python3] - Quick and commented BFS | Lucew | 0 | 13 | minesweeper | 529 | 0.655 | Medium | 9,339 |
https://leetcode.com/problems/minesweeper/discuss/2410455/python3-only-5steps-strait-forward-soln-98-beats | class Solution:
def updateBoard(self, board: List[List[str]], click: List[int]) -> List[List[str]]:
def dfs(clk_r, clk_c):
#step1 init NO of mines
M = 0
#step2 validating click
if clk_r < 0 or clk_r >= len(board) or clk_c < 0 or clk_c >= len(board[0]) or board[clk_r][... | minesweeper | python3 only 5steps strait forward soln 98% beats | benon | 0 | 101 | minesweeper | 529 | 0.655 | Medium | 9,340 |
https://leetcode.com/problems/minesweeper/discuss/2360912/Python3-or-Solved-using-BFS-%2B-Queue-%2B-HashSet(Modified-Board-in-place) | class Solution:
#Time-Complexity: O(16rows*cols), in worst case our bfs algorithm needs to process, and each iteration of while loop in worst case will run for loop
#2 times, with each going through each of eight directions for total of 16!
#-> O(rows*cols)
#Space-Complexity: O(2rows*cols)->O(ro... | minesweeper | Python3 | Solved using BFS + Queue + HashSet(Modified Board in-place) | JOON1234 | 0 | 86 | minesweeper | 529 | 0.655 | Medium | 9,341 |
https://leetcode.com/problems/minesweeper/discuss/2061699/MIne-sweeeeeper | class Solution:
def updateBoard(self, board: List[List[str]], click: List[int]) -> List[List[str]]:
m = len(board[0])
n = len(board)
seen = set()
if board[click[0]][click[1]] == 'M':
board[click[0]][click[1]] = 'X'
return board
steps = [(0, 1), (1, 0),... | minesweeper | MIne sweeeeeper | Omyx | 0 | 84 | minesweeper | 529 | 0.655 | Medium | 9,342 |
https://leetcode.com/problems/minesweeper/discuss/1893214/Python-solution-using-BFS-Queue | class Solution:
def updateBoard(self, board: List[List[str]], click: List[int]) -> List[List[str]]:
if board[click[0]][click[1]] == 'M':
board[click[0]][click[1]] = 'X'
return board
directions = [[0,1],[1,0],[0,-1],[-1,0],[1,1],[-1,-1],[1,-1],[-1,1]]
q = deque()
... | minesweeper | Python solution using BFS Queue | remy1991 | 0 | 86 | minesweeper | 529 | 0.655 | Medium | 9,343 |
https://leetcode.com/problems/minesweeper/discuss/1803031/python-solution | class Solution:
def updateBoard(self, board: List[List[str]], click: List[int]) -> List[List[str]]:
m = len(board[0])
n = len(board)
seen = set()
if board[click[0]][click[1]] == 'M':
board[click[0]][click[1]] = 'X'
return board
steps = [(0, 1), (1, 0),... | minesweeper | python solution | Omyx | 0 | 190 | minesweeper | 529 | 0.655 | Medium | 9,344 |
https://leetcode.com/problems/minesweeper/discuss/1196047/Stack-easy-to-understand-python-solution-DFS | class Solution:
def updateBoard(self, board: List[List[str]], click: List[int]) -> List[List[str]]:
if board[click[0]][click[1]] == 'M':
board[click[0]][click[1]] = 'X'
return board
stack = [click]
while stack:
x, y = stack.pop()
... | minesweeper | Stack, easy to understand, python solution, DFS | PhilipPhil | 0 | 132 | minesweeper | 529 | 0.655 | Medium | 9,345 |
https://leetcode.com/problems/minesweeper/discuss/872756/Python3-dfs | class Solution:
def updateBoard(self, board: List[List[str]], click: List[int]) -> List[List[str]]:
m, n = len(board), len(board[0]) # dimensions
i, j = click
if board[i][j] == "M": board[i][j] = "X"
elif board[i][j] == "E":
stack = [(i, j)]
while stack:
... | minesweeper | [Python3] dfs | ye15 | 0 | 140 | minesweeper | 529 | 0.655 | Medium | 9,346 |
https://leetcode.com/problems/minesweeper/discuss/467515/Python-BFS-solution-with-explanation. | class Solution:
def updateBoard(self, board: List[List[str]], click: List[int]) -> List[List[str]]:
x, y = click[0], click[1]
steps = [
(1,1),(1,0),(1,-1),(0,1),(0,-1),(-1,1),(-1,0),(-1,-1)
]
if board[x][y] == 'M':
board[x][y] = 'X'
return board
... | minesweeper | Python BFS solution with explanation. | skvrd | 0 | 263 | minesweeper | 529 | 0.655 | Medium | 9,347 |
https://leetcode.com/problems/minesweeper/discuss/258663/Heavily-commented-Python-solution-that's-faster-than-80-Py3-submissions | class Solution:
def updateBoard(self, board: List[List[str]], click: List[int]) -> List[List[str]]:
i, j = click[0], click[1]
# If a mine ('M') is revealed, then the game is over - change it to 'X'.
if board[i][j] == "M":
board[i][j] = "X"
# Else, proceed to *rev... | minesweeper | Heavily commented Python solution that's faster than 80% Py3 submissions | jovianlin | 0 | 116 | minesweeper | 529 | 0.655 | Medium | 9,348 |
https://leetcode.com/problems/minimum-absolute-difference-in-bst/discuss/1414654/Faster-than-99.61-of-Python3-with-logical-explanation | class Solution:
def getMinimumDifference(self, root: Optional[TreeNode]) -> int:
d = float('inf')
s = []
if root == None:
return
d = self.traverse(root,d,s)
return d
def traverse(self,root,d,s):
if root.left != None:
d = self.traverse(root... | minimum-absolute-difference-in-bst | Faster than 99.61% of Python3 with logical explanation | iron_man_365 | 4 | 786 | minimum absolute difference in bst | 530 | 0.568 | Easy | 9,349 |
https://leetcode.com/problems/minimum-absolute-difference-in-bst/discuss/1170986/WEEB-DOES-PYTHON-BFS(BEATS-96.55) | class Solution:
def getMinimumDifference(self, root: TreeNode) -> int:
queue, result, diff = deque([root]), [], float("inf")
while queue:
curNode = queue.popleft()
result.append(curNode.val)
if curNode.left:
queue.append(curNode.left)
if curNode.right:
queue.append(curNode.right)
result.... | minimum-absolute-difference-in-bst | WEEB DOES PYTHON BFS(BEATS 96.55%) | Skywalker5423 | 2 | 200 | minimum absolute difference in bst | 530 | 0.568 | Easy | 9,350 |
https://leetcode.com/problems/minimum-absolute-difference-in-bst/discuss/1178860/THE-MOST-EASIEST-SOLUTION-IN-PYTHON-Runtime%3A-48-ms-faster-than-96.52 | class Solution:
def __init__(self):
self.min = 10000
self.l = []
def getMinimumDifference(self, root: TreeNode) -> int:
self.solve(root)
self.l.sort()
for i in range(len(self.l)-1):
if self.l[i+1] - self.l[i] < self.min:
self.min = self.l[i+1] - self.l[i]
return self.min
def solve(self, root: Tre... | minimum-absolute-difference-in-bst | THE MOST EASIEST SOLUTION IN PYTHON , Runtime: 48 ms, faster than 96.52% | user4953S | 1 | 137 | minimum absolute difference in bst | 530 | 0.568 | Easy | 9,351 |
https://leetcode.com/problems/minimum-absolute-difference-in-bst/discuss/777232/Easy-Python-Recursive-Beats-87 | class Solution:
def getMinimumDifference(self, root: TreeNode) -> int:
res = []
def helper(node, vals, minn):
# If we find a min diff of 1 we can return (given properties of BST + abs diff)
# we know this is the lowest result we can find.
if minn == 1:
... | minimum-absolute-difference-in-bst | Easy Python Recursive Beats 87% | Pythagoras_the_3rd | 1 | 257 | minimum absolute difference in bst | 530 | 0.568 | Easy | 9,352 |
https://leetcode.com/problems/minimum-absolute-difference-in-bst/discuss/2547634/Inorder-Tree-Traversal | class Solution:
def getMinimumDifference(self, root: Optional[TreeNode]) -> int:
a = []
def traverse(node: Optional[TreeNode]) -> None:
if node is None:
return
traverse(node.left)
a.append(node.val)
traverse(node.right... | minimum-absolute-difference-in-bst | Inorder Tree Traversal | mansoorafzal | 0 | 24 | minimum absolute difference in bst | 530 | 0.568 | Easy | 9,353 |
https://leetcode.com/problems/minimum-absolute-difference-in-bst/discuss/2482132/Python-2-approaches | class Solution:
def getMinimumDifference(self, root: Optional[TreeNode]) -> int:
# difference could be found from right-root and root-left
stack = []
def dfs(node):
if not node:
return
dfs(node.left)
stack.append(node.val)
... | minimum-absolute-difference-in-bst | Python , 2 approaches | Abhi_009 | 0 | 46 | minimum absolute difference in bst | 530 | 0.568 | Easy | 9,354 |
https://leetcode.com/problems/minimum-absolute-difference-in-bst/discuss/2035688/Easy-understanding-Solution | class Solution:
def getMinimumDifference(self, root: Optional[TreeNode]) -> int:
k=[]
def treeTraverse(root):
if root :
treeTraverse(root.left)
k.append(root.val)
treeTraverse(root.right)
treeTraverse(root)
return min(b - a ... | minimum-absolute-difference-in-bst | Easy understanding Solution | Toshnav_Khatke | 0 | 107 | minimum absolute difference in bst | 530 | 0.568 | Easy | 9,355 |
https://leetcode.com/problems/minimum-absolute-difference-in-bst/discuss/1961295/Clean-Python-Using-a-Generator | class Solution:
def getMinimumDifference(self, root: Optional[TreeNode]) -> int:
def walk(root):
if not root:
return
yield from walk(root.left)
yield root.val
yield from walk(root.right)
res = float('inf')... | minimum-absolute-difference-in-bst | Clean Python Using a Generator | r_vaghefi | 0 | 30 | minimum absolute difference in bst | 530 | 0.568 | Easy | 9,356 |
https://leetcode.com/problems/minimum-absolute-difference-in-bst/discuss/1957665/Python-DFS-(In-Order-Traversal)-Solution-Faster-Than-93.53-Explained-Via-Comments | class Solution:
def getMinimumDifference(self, root: Optional[TreeNode]) -> int:
# list with two element
# the first for the previous element
# the second for the min value
pre_mn = [-float("inf"), float("inf")]
def dfs(tree):
if not... | minimum-absolute-difference-in-bst | Python DFS (In-Order Traversal) Solution, Faster Than 93.53%, Explained Via Comments | Hejita | 0 | 85 | minimum absolute difference in bst | 530 | 0.568 | Easy | 9,357 |
https://leetcode.com/problems/minimum-absolute-difference-in-bst/discuss/1780179/python3-solution | class Solution:
def getMinimumDifference(self, root: Optional[TreeNode]) -> int:
def dfs(root):
if not root:
return
dfs(root.left)
if self.prev!=-1:
self.res=min(self.res,root.val-self.prev)
... | minimum-absolute-difference-in-bst | python3 solution | Karna61814 | 0 | 38 | minimum absolute difference in bst | 530 | 0.568 | Easy | 9,358 |
https://leetcode.com/problems/minimum-absolute-difference-in-bst/discuss/1511754/Python3-simple-solution-using-recursion | class Solution:
def getMinimumDifference(self, root: TreeNode) -> int:
def make(root):
if not root:
return
if root.left:
make(root.left)
self.ans.append(root.val)
if root.right:
make(root.right)
self.ans ... | minimum-absolute-difference-in-bst | Python3 simple solution using recursion | EklavyaJoshi | 0 | 83 | minimum absolute difference in bst | 530 | 0.568 | Easy | 9,359 |
https://leetcode.com/problems/minimum-absolute-difference-in-bst/discuss/1344360/python3-easy-solution | class Solution:
def getMinimumDifference(self, root: TreeNode) -> int:
def inorder(node,lst):
if node is None:
return lst
else:
lst.append(node.val)
lst=inorder(node.left,lst)
lst=inorder(node.right,lst)
retu... | minimum-absolute-difference-in-bst | python3 easy solution | minato_namikaze | 0 | 50 | minimum absolute difference in bst | 530 | 0.568 | Easy | 9,360 |
https://leetcode.com/problems/minimum-absolute-difference-in-bst/discuss/546618/Python-simple-solution-44-ms-faster-than-86.07 | class Solution(object):
def getMinimumDifference(self, root):
"""
:type root: TreeNode
:rtype: int
"""
def get_values(values, root):
if root is not None:
values.append(root.val)
values = get_values(values, root.left)
... | minimum-absolute-difference-in-bst | Python simple solution 44 ms, faster than 86.07% | hemina | 0 | 325 | minimum absolute difference in bst | 530 | 0.568 | Easy | 9,361 |
https://leetcode.com/problems/k-diff-pairs-in-an-array/discuss/1757434/Python-O(n)-Solution-or-98-Faster-or-Easy-Solution-or-K-diff-Pairs-in-an-Array | class Solution:
def findPairs(self, nums: List[int], k: int) -> int:
cnt=0
c=Counter(nums)
if k==0:
for key,v in c.items():
if v>1:
cnt+=1
else:
for key,v in c.items():
if key+k in c:
... | k-diff-pairs-in-an-array | ✔️ Python O(n) Solution | 98% Faster | Easy Solution | K-diff Pairs in an Array | pniraj657 | 34 | 2,200 | k diff pairs in an array | 532 | 0.408 | Medium | 9,362 |
https://leetcode.com/problems/k-diff-pairs-in-an-array/discuss/1758373/Python-3-(60ms)-or-O(n)-Counter-Hashmap-Solution-or-Easy-to-Understand | class Solution:
def findPairs(self, nums: List[int], k: int) -> int:
count = Counter(nums)
if k > 0:
return sum([i + k in count for i in count])
else:
return sum([count[i] > 1 for i in count]) | k-diff-pairs-in-an-array | Python 3 (60ms) | O(n) Counter Hashmap Solution | Easy to Understand | MrShobhit | 3 | 203 | k diff pairs in an array | 532 | 0.408 | Medium | 9,363 |
https://leetcode.com/problems/k-diff-pairs-in-an-array/discuss/1758729/Python-%2B-Binary-Search | class Solution:
def findPairs(self, nums: List[int], k: int) -> int:
def search(l,h,k):
while l <= h:
mid = l+(h-l)//2
if nums[mid] == k:
return True
if nums[mid] > k:
h = mi... | k-diff-pairs-in-an-array | Python + Binary Search | shankha117 | 1 | 119 | k diff pairs in an array | 532 | 0.408 | Medium | 9,364 |
https://leetcode.com/problems/k-diff-pairs-in-an-array/discuss/1757954/Python-O(n)-Solution-or-98-Faster-or-Easy-Solution-or-K-diff-Pairs-in-an-Array | class Solution:
def findPairs(self, nums: List[int], k: int) -> int:
d = {}
for i in nums:
if d.get(i):
d[i]+=1
else:
d[i] = 1
ans = 0
for i in d:
if d.get(i+k) and (k != 0 or d[i] > 1):
ans+=1
... | k-diff-pairs-in-an-array | Python O(n) Solution | 98% Faster | Easy Solution | K-diff Pairs in an Array | khanter | 1 | 59 | k diff pairs in an array | 532 | 0.408 | Medium | 9,365 |
https://leetcode.com/problems/k-diff-pairs-in-an-array/discuss/1757871/Python-Solutions | class Solution:
def findPairs(self, nums: List[int], k: int) -> int:
return self.findPairsHashMap(nums, k)
def findPairsHashMap(self, nums: List[int], k: int) -> int:
c = collections.Counter(nums)
res = 0
for i in c:
if (k > 0 and (i + k) in c) or (k == 0 an... | k-diff-pairs-in-an-array | Python Solutions | Priyanshu_Arya | 1 | 18 | k diff pairs in an array | 532 | 0.408 | Medium | 9,366 |
https://leetcode.com/problems/k-diff-pairs-in-an-array/discuss/704514/Python-iterate-through-sorted-unique-list | class Solution:
def findPairs(self, nums: List[int], k: int) -> int:
#edge case
if not nums : return 0
# sum how many elements are in list more than once
if k == 0 :
c = Counter(nums)
return sum([1 for n in c if c[n] > 1 ])
# if k > 0, then create a unique sort... | k-diff-pairs-in-an-array | Python - iterate through sorted unique list | burento | 1 | 303 | k diff pairs in an array | 532 | 0.408 | Medium | 9,367 |
https://leetcode.com/problems/k-diff-pairs-in-an-array/discuss/704514/Python-iterate-through-sorted-unique-list | class Solution:
def findPairs(self, nums: List[int], k: int) -> int:
if not nums : return 0
if k == 0 :
c = Counter(nums)
return sum([1 for n in c if c[n] > 1 ])
nums = sorted(list(set(nums)))
a, b, pair = 0, 1, 0
while b < len(nums) :
d... | k-diff-pairs-in-an-array | Python - iterate through sorted unique list | burento | 1 | 303 | k diff pairs in an array | 532 | 0.408 | Medium | 9,368 |
https://leetcode.com/problems/k-diff-pairs-in-an-array/discuss/2781387/Python3-or-Sorting-%2B-Binary-Search-or-Explanation | class Solution:
def findPairs(self, nums: List[int], k: int) -> int:
nums.sort()
ans = 0
n = len(nums)
vis = set()
for i in range(n):
target = nums[i] - k
index = bisect.bisect_left(nums,target)
if index!=i and nums[index] == target:
... | k-diff-pairs-in-an-array | [Python3] | Sorting + Binary Search | Explanation | swapnilsingh421 | 0 | 7 | k diff pairs in an array | 532 | 0.408 | Medium | 9,369 |
https://leetcode.com/problems/k-diff-pairs-in-an-array/discuss/2708878/Python-or-sort-or-simple-and-clear | class Solution:
def findPairs(self, nums: List[int], k: int) -> int:
res = set()
i = 0
j = 1
nums.sort()
while j < len(nums):
if nums[j]-nums[i] == k:
res.add((nums[i], nums[j]))
i += 1
j = i+1
elif nums[... | k-diff-pairs-in-an-array | Python | sort | simple and clear | jainsiddharth99 | 0 | 21 | k diff pairs in an array | 532 | 0.408 | Medium | 9,370 |
https://leetcode.com/problems/k-diff-pairs-in-an-array/discuss/2429368/K-diff-pair-in-an-array-oror-Python3-oror-hashmap | class Solution:
def findPairs(self, nums: List[int], k: int) -> int:
ans = 0
counter = Counter(nums)
for x in counter:
if k > 0 and x + k in counter:
ans += 1
elif k ==0 and counter[x] > 1:
ans += 1
return ans | k-diff-pairs-in-an-array | K-diff pair in an array || Python3 || hashmap | vanshika_2507 | 0 | 12 | k diff pairs in an array | 532 | 0.408 | Medium | 9,371 |
https://leetcode.com/problems/k-diff-pairs-in-an-array/discuss/1958460/Python-easy-to-read-and-understand-or-hashmap | class Solution:
def findPairs(self, nums: List[int], k: int) -> int:
d = collections.defaultdict(int)
ans = 0
for num in nums:
d[num] = d.get(num, 0) + 1
#print(d.items())
if k == 0:
for key in d:
if d[key] > 1:
... | k-diff-pairs-in-an-array | Python easy to read and understand | hashmap | sanial2001 | 0 | 92 | k diff pairs in an array | 532 | 0.408 | Medium | 9,372 |
https://leetcode.com/problems/k-diff-pairs-in-an-array/discuss/1955403/Clean-Solution-using-HashMap-with-O(n)-TC | class Solution:
def findPairs(self, nums: List[int], k: int) -> int:
hash_list = Counter(nums)
pairs = 0
#Special Case because now we have to consider the duplicate element as well.
if(k == 0):
for i in hash_list.values():
if i > 1:
... | k-diff-pairs-in-an-array | Clean Solution using HashMap with O(n) TC | akshit-goyal | 0 | 28 | k diff pairs in an array | 532 | 0.408 | Medium | 9,373 |
https://leetcode.com/problems/k-diff-pairs-in-an-array/discuss/1830082/Python-Easy-Dictionary-Solution | class Solution:
def findPairs(self, nums: List[int], k: int) -> int:
nums = sorted(nums)
numdict = dict()
res = set()
for n in nums :
if n in numdict :
res.add((n,numdict[n]))
numdict[k+n]=n
return len... | k-diff-pairs-in-an-array | [Python] Easy Dictionary Solution | crazypuppy | 0 | 130 | k diff pairs in an array | 532 | 0.408 | Medium | 9,374 |
https://leetcode.com/problems/k-diff-pairs-in-an-array/discuss/1758594/Python3-Solution-with-using-hashmap | class Solution:
def findPairs(self, nums: List[int], k: int) -> int:
c = collections.Counter(nums)
res = 0
for elem in c:
if k > 0 and elem + k in c:
res += 1
elif k == 0 and c[elem] >= 2:
res += 1
retu... | k-diff-pairs-in-an-array | [Python3] Solution with using hashmap | maosipov11 | 0 | 11 | k diff pairs in an array | 532 | 0.408 | Medium | 9,375 |
https://leetcode.com/problems/k-diff-pairs-in-an-array/discuss/1757663/Python-oror-Easy-to-understand-solution-oror-Simple-Hashing | class Solution:
def findPairs(self, nums: List[int], k: int) -> int:
c = 0
freq = Counter(nums)
if k == 0:
for _ in freq.values():
if _ > 1:
c += 1
else:
for i in freq.keys():
if i + k in freq:
... | k-diff-pairs-in-an-array | Python || Easy to understand solution || Simple Hashing | cherrysri1997 | 0 | 14 | k diff pairs in an array | 532 | 0.408 | Medium | 9,376 |
https://leetcode.com/problems/k-diff-pairs-in-an-array/discuss/1757169/Python-3-EASY-Intuitive-Solution | class Solution:
def findPairs(self, nums: List[int], k: int) -> int:
nums.sort()
res = i = 0
j = i+1
memo = []
while i < len(nums):
if j < len(nums) and nums[j]-nums[i] == k and (nums[j], nums[i]) not in memo:
res += 1
memo.append((... | k-diff-pairs-in-an-array | ✅ [Python 3] EASY Intuitive Solution | JawadNoor | 0 | 18 | k diff pairs in an array | 532 | 0.408 | Medium | 9,377 |
https://leetcode.com/problems/k-diff-pairs-in-an-array/discuss/1757085/Python3-Two-Sum-Variation-w-Simple-Explanation | class Solution:
def findPairs(self, nums: List[int], k: int) -> int:
used, res = {}, 0
for n in nums:
if k == 0:
if n in used:
res += not used[n]
used[n] = n in used
elif n not in used:
used[n], below, above ... | k-diff-pairs-in-an-array | [Python3] Two Sum Variation w/ Simple Explanation | PatrickOweijane | 0 | 98 | k diff pairs in an array | 532 | 0.408 | Medium | 9,378 |
https://leetcode.com/problems/k-diff-pairs-in-an-array/discuss/1757001/Python-Simple-Python-Solution-Using-Dictionary | class Solution:
def findPairs(self, nums: List[int], k: int) -> int:
ans = 0
d={}
for i in nums:
if i not in d:
d[i]=1
else:
d[i]=d[i]+1
if k>0:
for j in d:
if j+k in d.keys():
ans=ans+1
else:
for j in d:
if d[j]>1:
ans=ans+1
return ans | k-diff-pairs-in-an-array | [ Python ] ✔✔ Simple Python Solution Using Dictionary | ASHOK_KUMAR_MEGHVANSHI | 0 | 43 | k diff pairs in an array | 532 | 0.408 | Medium | 9,379 |
https://leetcode.com/problems/k-diff-pairs-in-an-array/discuss/1756852/Python-or-Super-Easy-or-O(NlogN)-Solution | class Solution:
def findPairs(self, nums: List[int], k: int) -> int:
nums.sort()
n = len(nums)
s = set()
result = set()
for i in range(n):
if nums[i] in s:
result.add(tuple([nums[i]-k, nums[i]]))
s.add(nums[i] + k)... | k-diff-pairs-in-an-array | Python | Super Easy | O(NlogN) Solution | Mikey98 | 0 | 60 | k diff pairs in an array | 532 | 0.408 | Medium | 9,380 |
https://leetcode.com/problems/k-diff-pairs-in-an-array/discuss/1663383/Python-O(n)-time-O(1)-space-solution-using-hashmap | class Solution:
def findPairs(self, nums: List[int], k: int) -> int:
res = set()
n = len(nums)
count1, count2 = defaultdict(set), defaultdict(set)
# nums[j] = nums[i] + k or nums[j] = nums[i] - k
for i in range(n):
count1[nums[i] + k].add(i)
count2[num... | k-diff-pairs-in-an-array | Python O(n) time, O(1) space solution using hashmap | byuns9334 | 0 | 209 | k diff pairs in an array | 532 | 0.408 | Medium | 9,381 |
https://leetcode.com/problems/k-diff-pairs-in-an-array/discuss/877168/python3-beats-98-using-dictionary | class Solution:
def findPairs(self, nums: List[int], k: int) -> int:
nums_n_to_ind = defaultdict(set)
for i,n in enumerate(nums):
nums_n_to_ind[n].add(i)
res = set()
for i,n in enumerate(nums):
if n + k in nums_n_to_ind and any([j != i for j in... | k-diff-pairs-in-an-array | python3, beats 98%, using dictionary | w7089 | 0 | 24 | k diff pairs in an array | 532 | 0.408 | Medium | 9,382 |
https://leetcode.com/problems/k-diff-pairs-in-an-array/discuss/872781/Python3-1-pass-via-two-sets-(100) | class Solution:
def findPairs(self, nums: List[int], k: int) -> int:
ans, seen = set(), set()
for x in nums:
if x - k in seen: ans.add(x)
if x + k in seen: ans.add(x+k)
seen.add(x)
return len(ans) | k-diff-pairs-in-an-array | [Python3] 1 pass via two sets (100%) | ye15 | 0 | 43 | k diff pairs in an array | 532 | 0.408 | Medium | 9,383 |
https://leetcode.com/problems/complex-number-multiplication/discuss/1187864/Python3-simple-solution | class Solution:
def complexNumberMultiply(self, num1: str, num2: str) -> str:
a1,b1 = num1.split('+')
a1 = int(a1)
b1 = int(b1[:-1])
a2,b2 = num2.split('+')
a2 = int(a2)
b2 = int(b2[:-1])
return str(a1*a2 + b1*b2*(-1)) + '+' + str(a1*b2 + a2*b1) + 'i' | complex-number-multiplication | Python3 simple solution | EklavyaJoshi | 7 | 113 | complex number multiplication | 537 | 0.714 | Medium | 9,384 |
https://leetcode.com/problems/complex-number-multiplication/discuss/453974/Python-3-(two-lines)-(beats-~100) | class Solution:
def complexNumberMultiply(self, a: str, b: str) -> str:
[A1,B1,A2,B2] = map(int,(a+'+'+b).replace('i','').split('+'))
return str(A1*A2-B1*B2)+'+'+str(A1*B2+A2*B1)+'i'
- Junaid Mansuri
- Chicago, IL | complex-number-multiplication | Python 3 (two lines) (beats ~100%) | junaidmansuri | 2 | 273 | complex number multiplication | 537 | 0.714 | Medium | 9,385 |
https://leetcode.com/problems/complex-number-multiplication/discuss/2831284/Python.-3-liner | class Solution:
def complexNumberMultiply(self, num1: str, num2: str) -> str:
ax, ay = map(int, num1.rstrip('i').split('+'))
bx, by = map(int, num2.rstrip('i').split('+'))
return '{x}+{y}i'.format(x = ax*bx - ay*by, y = ax*by + bx*ay) | complex-number-multiplication | [Python]. 3-liner | nonchalant-enthusiast | 0 | 1 | complex number multiplication | 537 | 0.714 | Medium | 9,386 |
https://leetcode.com/problems/complex-number-multiplication/discuss/2660656/Simple-Python-or-Gaussian-integers | class Solution:
def complexNumberMultiply(self, num1: str, num2: str) -> str:
def complex(s):
r, im = s.split('+')
return (int(r), int(im[:-1]))
def to_string(a, b):
return f"{a}+{b}i"
# The product of the complex numbers z1 = a + bi and z2 = c + di
... | complex-number-multiplication | Simple Python | Gaussian integers | on_danse_encore_on_rit_encore | 0 | 1 | complex number multiplication | 537 | 0.714 | Medium | 9,387 |
https://leetcode.com/problems/complex-number-multiplication/discuss/2282429/easy-python-solution | class Solution:
def complexNumberMultiply(self, num1: str, num2: str) -> str:
num1_real, num1_comp = num1.split('+')
num2_real, num2_comp = num2.split('+')
# print(num1_real, num1_comp)
# print(num2_real, num2_comp)
if num1_comp[0] == '-' :
num1_comp_num... | complex-number-multiplication | easy python solution | sghorai | 0 | 34 | complex number multiplication | 537 | 0.714 | Medium | 9,388 |
https://leetcode.com/problems/complex-number-multiplication/discuss/2098477/Easy-Python-Solution | class Solution:
def complexNumberMultiply(self, num1: str, num2: str) -> str:
val1 = num1.split('+')
val2 = num2.split('+')
val1[1] = val1[1].split('i')
val2[1] = val2[1].split('i')
real = int(val1[0])*int(val2[0]) - int(val1[1][0])*int(val2[1][0])
i... | complex-number-multiplication | Easy Python Solution | kn_vardhan | 0 | 23 | complex number multiplication | 537 | 0.714 | Medium | 9,389 |
https://leetcode.com/problems/complex-number-multiplication/discuss/1874805/python-3-oror-short-and-easy-to-understand | class Solution:
def complexNumberMultiply(self, num1: str, num2: str) -> str:
r1, i1 = map(int, num1.strip('i').split('+'))
r2, i2 = map(int, num2.strip('i').split('+'))
real = r1*r2 - i1*i2
imaginary = r1*i2 + i1*r2
return f'{real}+{imaginary}i' | complex-number-multiplication | python 3 || short and easy to understand | dereky4 | 0 | 90 | complex number multiplication | 537 | 0.714 | Medium | 9,390 |
https://leetcode.com/problems/complex-number-multiplication/discuss/1756898/Python-Easy-Soln-with-explanation | class Solution:
def complexNumberMultiply(self, num1: str, num2: str) -> str:
#converts into integer values
def cnum(n):
x=int(n[0])
y=int(n[1].replace("i",""))
return [x,y]
n1=cnum(num1.split('+'))
n2=cnum(num2.split('+'))
... | complex-number-multiplication | Python Easy Soln with explanation | AjayKadiri | 0 | 29 | complex number multiplication | 537 | 0.714 | Medium | 9,391 |
https://leetcode.com/problems/complex-number-multiplication/discuss/1719157/Python3-accepted-solution | class Solution:
def complexNumberMultiply(self, num1: str, num2: str) -> str:
a = num1[:num1.index("+")]
b = num1[num1.index("+")+1:num1.index("i")]
c = num2[:num2.index("+")]
d = num2[num2.index("+")+1:num2.index("i")]
return (str(int(a)*int(c) + int(b)*int(d)*(-1))+"+"+ str... | complex-number-multiplication | Python3 accepted solution | sreeleetcode19 | 0 | 28 | complex number multiplication | 537 | 0.714 | Medium | 9,392 |
https://leetcode.com/problems/complex-number-multiplication/discuss/1719157/Python3-accepted-solution | class Solution:
def complexNumberMultiply(self, num1: str, num2: str) -> str:
return (str(int(num1[:num1.index("+")])*int(num2[:num2.index("+")]) + int(num1[num1.index("+")+1:num1.index("i")])*int(num2[num2.index("+")+1:num2.index("i")])*(-1))+"+"+ str(int(num1[:num1.index("+")])*int(num2[num2.index("+")+1:... | complex-number-multiplication | Python3 accepted solution | sreeleetcode19 | 0 | 28 | complex number multiplication | 537 | 0.714 | Medium | 9,393 |
https://leetcode.com/problems/complex-number-multiplication/discuss/1423948/python3-ez-solution | class Solution:
def complexNumberMultiply(self, num1: str, num2: str) -> str:
target1 = num1.split('+')
target2 = num2.split('+')
real = int(target1[0]) * int(target2[0]) - int(target1[1][:-1]) * int(target2[1][:-1])
imag = int(target1[1][:-1]) * int(target2[0]) + int(target1[0]) * i... | complex-number-multiplication | python3 ez solution | yingziqing123 | 0 | 26 | complex number multiplication | 537 | 0.714 | Medium | 9,394 |
https://leetcode.com/problems/complex-number-multiplication/discuss/1423536/Python-straightforward-solution | class Solution:
def complexNumberMultiply(self, num1: str, num2: str) -> str:
real1, imag1 = int(num1.split('+')[0]), num1.split('+')[1]
real2, imag2 = int(num2.split('+')[0]), num2.split('+')[1]
# Turn imaginary strings into integers to perform multiplication
imag1, imag2 =... | complex-number-multiplication | Python straightforward solution | SmittyWerbenjagermanjensen | 0 | 37 | complex number multiplication | 537 | 0.714 | Medium | 9,395 |
https://leetcode.com/problems/complex-number-multiplication/discuss/1422903/Python3-Easy-and-Clean-Solution-Using-f-strings | class Solution:
def complexNumberMultiply(self, num1: str, num2: str) -> str:
x1, x1i = map(int, num1[:-1].split('+'))
x2, x2i = map(int, num2[:-1].split('+'))
return f"{x1*x2 - x1i*x2i}+{x1*x2i + x2*x1i}i" | complex-number-multiplication | [Python3] Easy & Clean Solution Using f-strings | chaudhary1337 | 0 | 32 | complex number multiplication | 537 | 0.714 | Medium | 9,396 |
https://leetcode.com/problems/complex-number-multiplication/discuss/1422784/Python-Solution | class Solution:
def complexNumberMultiply(self, num1: str, num2: str) -> str:
r1, im1 = num1.split("+")
r2, im2 = num2.split("+")
r1, im1 = int(r1), int(im1[:-1])
r2, im2 = int(r2), int(im2[:-1])
r3, im3 = 0, 0
r3 = r1 * r2 + im1 * im2 * -1
im3 = r1 * im2 + im... | complex-number-multiplication | Python Solution | mariandanaila01 | 0 | 52 | complex number multiplication | 537 | 0.714 | Medium | 9,397 |
https://leetcode.com/problems/complex-number-multiplication/discuss/1422777/Python-3-easy-solution | class Solution:
def complexNumberMultiply(self, num1: str, num2: str) -> str:
def process_arr(num):
set_ = num.split("+")
num1 = int(set_[0])
num2 = int(set_[1][:-1])
return num1, num2
num1_r, num1_i = process_arr(num1)
num2_r, num2_i = process_arr(num2)
real = num1_r * num2_r
r_i = num1_r ... | complex-number-multiplication | Python 3 easy solution | Andy_Feng97 | 0 | 20 | complex number multiplication | 537 | 0.714 | Medium | 9,398 |
https://leetcode.com/problems/complex-number-multiplication/discuss/1257803/python-oror-with-comments-oror-easy-understanding | class Solution:
def complexNumberMultiply(self, num1: str, num2: str) -> str:
a=int(num1[:num1.index('+')])
b= int(num1[num1.index('+')+1:num1.index('i')])
c=int(num2[:num2.index('+')])
d= int(num2[num2.index('+')+1:num2.index('i')])
... | complex-number-multiplication | python || with comments || easy understanding | chikushen99 | 0 | 36 | complex number multiplication | 537 | 0.714 | Medium | 9,399 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.