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/array-with-elements-not-equal-to-average-of-neighbors/discuss/2280763/O(nlogn)-Solution-or-Python
class Solution: def rearrangeArray(self, nums: List[int]) -> List[int]: nums.sort() if len(nums)==3: nums[1],nums[0] = nums[0],nums[1] return nums for i in range(1,len(nums)-1): if nums[i]-nums[i-1] == nums[i+1]-nums[i]: if i!=...
array-with-elements-not-equal-to-average-of-neighbors
O(nlogn) Solution | Python
user7457RV
0
27
array with elements not equal to average of neighbors
1,968
0.496
Medium
27,600
https://leetcode.com/problems/array-with-elements-not-equal-to-average-of-neighbors/discuss/1750374/Easy-solution-using-JavaScript-%2B-Python3
class Solution: def rearrangeArray(self, nums: List[int]) -> List[int]: nums.sort() res,l,r = [],0,len(nums) - 1 while len(res) != len(nums): res.append(nums[l]) l += 1 if l <= r : res.append(nums[r]) r -= 1 ...
array-with-elements-not-equal-to-average-of-neighbors
Easy solution using - JavaScript + Python3
shakilbabu
0
36
array with elements not equal to average of neighbors
1,968
0.496
Medium
27,601
https://leetcode.com/problems/array-with-elements-not-equal-to-average-of-neighbors/discuss/1403943/Python3-wiggle
class Solution: def rearrangeArray(self, nums: List[int]) -> List[int]: for i in range(1, len(nums)-1): if nums[i-1] < nums[i] < nums[i+1] or nums[i-1] > nums[i] > nums[i+1]: nums[i], nums[i+1] = nums[i+1], nums[i] return nums
array-with-elements-not-equal-to-average-of-neighbors
[Python3] wiggle
ye15
0
55
array with elements not equal to average of neighbors
1,968
0.496
Medium
27,602
https://leetcode.com/problems/minimum-non-zero-product-of-the-array-elements/discuss/1403953/Python3-2-line
class Solution: def minNonZeroProduct(self, p: int) -> int: x = (1 << p) - 1 return pow(x-1, (x-1)//2, 1_000_000_007) * x % 1_000_000_007
minimum-non-zero-product-of-the-array-elements
[Python3] 2-line
ye15
6
569
minimum non zero product of the array elements
1,969
0.338
Medium
27,603
https://leetcode.com/problems/minimum-non-zero-product-of-the-array-elements/discuss/1404240/Well-Explained-oror-Clean-and-Concise-oror-98-faster
class Solution: def minNonZeroProduct(self, p: int) -> int: MOD = 10**9+7 h = 2**p - 2 n = 2**(p-1)-1 return ((pow(h,n,MOD))*(h+1))%MOD
minimum-non-zero-product-of-the-array-elements
🐍 Well-Explained || Clean & Concise || 98% faster 📌📌
abhi9Rai
0
101
minimum non zero product of the array elements
1,969
0.338
Medium
27,604
https://leetcode.com/problems/minimum-non-zero-product-of-the-array-elements/discuss/1403906/Python-Math-Solution-with-Detailed-Explanation
class Solution: def minNonZeroProduct(self, p: int) -> int: MOD = 10 ** 9 + 7 res = pow(pow(2, p, MOD) - 2, pow(2, (p - 1)) - 1, MOD) * (pow(2, p, MOD) - 1) return int(res) % MOD
minimum-non-zero-product-of-the-array-elements
[Python] Math Solution with Detailed Explanation
fishballLin
0
144
minimum non zero product of the array elements
1,969
0.338
Medium
27,605
https://leetcode.com/problems/last-day-where-you-can-still-cross/discuss/2489142/Python3-or-Binary-Search-%2B-BFS
class Solution(object): def latestDayToCross(self, row, col, cells): l,h=0,len(cells)-1 ans=-1 while l<=h: m=(l+h)>>1 if self.isPath(cells,m,row,col): l=m+1 ans=m+1 else: h=m-1 return ans def isPa...
last-day-where-you-can-still-cross
[Python3] | Binary Search + BFS
swapnilsingh421
0
36
last day where you can still cross
1,970
0.495
Hard
27,606
https://leetcode.com/problems/last-day-where-you-can-still-cross/discuss/1403924/Python-Binary-Search-and-DFS-detailed-explanation
class Solution: def latestDayToCross(self, row: int, col: int, cells: List[List[int]]) -> int: _board = [[0 for _ in range(col)] for _ in range(row)] board = None def search(days): def dfs(x, y): res = False for _x, _y in ((x + 1, y), (x -...
last-day-where-you-can-still-cross
[Python] Binary Search & DFS - detailed explanation
fishballLin
0
79
last day where you can still cross
1,970
0.495
Hard
27,607
https://leetcode.com/problems/find-if-path-exists-in-graph/discuss/1406782/Python-Easy-to-Understand-or-Beginners
class Solution(object): def validPath(self, n, edges, start, end): """ :type n: int :type edges: List[List[int]] :type start: int :type end: int :rtype: bool """ visited = [False]*n d = {} #store the undirected edges for both vertices ...
find-if-path-exists-in-graph
Python - Easy to Understand | Beginners
Sibu0811
29
6,800
find if path exists in graph
1,971
0.504
Easy
27,608
https://leetcode.com/problems/find-if-path-exists-in-graph/discuss/1511658/Simple-Solution-DFS
class Solution: def validPath(self, n: int, edges: List[List[int]], start: int, end: int) -> bool: graph = self.buildGraph(edges) return self.hasPath(graph, start, end, set()) # function to convert list of edges to adjacency list graph def buildGraph(self, edges): graph = {} ...
find-if-path-exists-in-graph
Simple Solution [DFS]
nandanabhishek
13
2,500
find if path exists in graph
1,971
0.504
Easy
27,609
https://leetcode.com/problems/find-if-path-exists-in-graph/discuss/1407531/Python3-dfs-of-UnionFind
class Solution: def validPath(self, n: int, edges: List[List[int]], start: int, end: int) -> bool: graph = {} for u, v in edges: graph.setdefault(u, []).append(v) graph.setdefault(v, []).append(u) seen = {start} stack = [start] while stack: ...
find-if-path-exists-in-graph
[Python3] dfs of UnionFind
ye15
5
286
find if path exists in graph
1,971
0.504
Easy
27,610
https://leetcode.com/problems/find-if-path-exists-in-graph/discuss/1473226/Python-DFS-and-UnionFind-and-BFS-with-explanation
class Solution: def validPath(self, n: int, edges: List[List[int]], start: int, end: int) -> bool: if start == end: return True graph = {} for vertex in range(n): graph[vertex] = [] for e1, e2 in edges: graph[e1].append(e2) ...
find-if-path-exists-in-graph
Python DFS & UnionFind & BFS with explanation
SleeplessChallenger
4
675
find if path exists in graph
1,971
0.504
Easy
27,611
https://leetcode.com/problems/find-if-path-exists-in-graph/discuss/1473226/Python-DFS-and-UnionFind-and-BFS-with-explanation
class Solution: def validPath(self, n: int, edges: List[List[int]], start: int, end: int) -> bool: queue = [start] graph = {} for vertex in range(n): graph[vertex] = [] for v1, v2 in edges: graph[v1].append(v2) graph[v2].append(v1) ...
find-if-path-exists-in-graph
Python DFS & UnionFind & BFS with explanation
SleeplessChallenger
4
675
find if path exists in graph
1,971
0.504
Easy
27,612
https://leetcode.com/problems/find-if-path-exists-in-graph/discuss/1450522/Python-Union-Find
class Solution: def validPath(self, n: int, edges: List[List[int]], start: int, end: int) -> bool: def find(n): while n != parent[n]: n = parent[n] return n parent = list(range(n)) for n1, n2 in edges: p1 = find(n1) p2 =...
find-if-path-exists-in-graph
Python, Union-Find
blue_sky5
3
219
find if path exists in graph
1,971
0.504
Easy
27,613
https://leetcode.com/problems/find-if-path-exists-in-graph/discuss/2459419/My-noob-slow-solution-or-Python-or-Easy-DFS-or-Graph-or-Dict
class Solution: def validPath(self, n: int, edges: List[List[int]], source: int, destination: int) -> bool: graph = {} for item in edges: if item[0] in graph: graph[item[0]].append(item[1]) else: graph[item[0]] = [item[1]] if item[1...
find-if-path-exists-in-graph
My noob slow solution | Python | Easy DFS | Graph | Dict
prameshbajra
1
93
find if path exists in graph
1,971
0.504
Easy
27,614
https://leetcode.com/problems/find-if-path-exists-in-graph/discuss/2366550/Python3-or-BFS-%2B-Queue-%2B-Set
class Solution: def validPath(self, n: int, edges: List[List[int]], source: int, destination: int) -> bool: #first, iterate through edges array and construct a adjacency list representation for #the bi-directional undirected unweightd graph! #Then, start bfs from the source node see if any ...
find-if-path-exists-in-graph
Python3 | BFS + Queue + Set
JOON1234
1
91
find if path exists in graph
1,971
0.504
Easy
27,615
https://leetcode.com/problems/find-if-path-exists-in-graph/discuss/2038519/Python-3-greater-BFS-with-explanation
class Solution: def validPath(self, n: int, edges: List[List[int]], source: int, destination: int) -> bool: if source == destination: return True adjMap = self.buildAdjMap(edges) if source not in adjMap or destination not in adjMap: return False ...
find-if-path-exists-in-graph
Python 3 -> BFS with explanation
mybuddy29
1
111
find if path exists in graph
1,971
0.504
Easy
27,616
https://leetcode.com/problems/find-if-path-exists-in-graph/discuss/1977964/Simple-BFS-Python-Solution-oror-80-Faster-oror-Memory-less-than-75
class Solution: def validPath(self, n: int, edges: List[List[int]], source: int, destination: int) -> bool: visited=[False]*n ; G=defaultdict(list) ; Q=deque([source]) for e in edges: G[e[0]].append(e[1]) G[e[1]].append(e[0]) while Q: cur=Q.popleft() ...
find-if-path-exists-in-graph
Simple BFS Python Solution || 80% Faster || Memory less than 75%
Taha-C
1
131
find if path exists in graph
1,971
0.504
Easy
27,617
https://leetcode.com/problems/find-if-path-exists-in-graph/discuss/1962478/Python3-Runtime%3A-2248ms-71.16-memory%3A-313.2mb-7.76
class Solution: def validPath(self, n: int, edges: List[List[int]], source: int, destination: int) -> bool: graph = self.makeGraph(edges) return self.depthFirstSearch(graph, source, destination, set()) def makeGraph(self, edges): graph = dict() for edge in ...
find-if-path-exists-in-graph
Python3 Runtime: 2248ms 71.16% memory: 313.2mb 7.76%
arshergon
1
77
find if path exists in graph
1,971
0.504
Easy
27,618
https://leetcode.com/problems/find-if-path-exists-in-graph/discuss/2805066/unit_set
class Solution: def validPath(self, n: int, edges: List[List[int]], source: int, destination: int) -> bool: if source == destination: return True left_edges = [] set_source = set() ## add all the vertex directly connected with source for edge in edges: ...
find-if-path-exists-in-graph
unit_set
Codeminer
0
1
find if path exists in graph
1,971
0.504
Easy
27,619
https://leetcode.com/problems/find-if-path-exists-in-graph/discuss/2515142/Simple-python-solution-using-BFS-or-using-queue
class Solution: def validPath(self, n: int, edges: List[List[int]], source: int, destination: int) -> bool: if source == destination: return True adjl = collections.defaultdict(list) for u, v in edges: adjl[u].append(v) adjl[v].append(u) print(adjl) visit = set() q = deque([source]) while q: ...
find-if-path-exists-in-graph
Simple python solution using BFS | using queue
nikhitamore
0
71
find if path exists in graph
1,971
0.504
Easy
27,620
https://leetcode.com/problems/find-if-path-exists-in-graph/discuss/2492617/Disjoint-Set-Union-Find-Solution-with-Python
class Solution: def validPath(self, n: int, edges: List[List[int]], source: int, destination: int) -> bool: root = [i for i in range(n)] rank = [1] * n def find(x): if x == root[x]: return x root[x] = find(root[x]) return ...
find-if-path-exists-in-graph
Disjoint Set Union Find Solution with Python
PaulAlek
0
41
find if path exists in graph
1,971
0.504
Easy
27,621
https://leetcode.com/problems/find-if-path-exists-in-graph/discuss/2447739/Python-BFS-and-DFS-(recursion).
class Solution: def validPath(self, n, edges, source, destination): if n == 1: return True graph = {} for i in range(len(edges)): graph[edges[i][0]] = [] graph[edges[i][1]] = [] for i in range(len(edges)): graph[edges[i][0]...
find-if-path-exists-in-graph
Python BFS and DFS (recursion).
OsamaRakanAlMraikhat
0
73
find if path exists in graph
1,971
0.504
Easy
27,622
https://leetcode.com/problems/find-if-path-exists-in-graph/discuss/2447739/Python-BFS-and-DFS-(recursion).
class Solution: def validPath(self, n, edges, source, destination): graph = {} for edge in edges: graph[edge[0]] = [] graph[edge[1]] = [] for edge in edges: graph[edge[0]].append(edge[1]) graph[edge[1]].append(edge[0]) ...
find-if-path-exists-in-graph
Python BFS and DFS (recursion).
OsamaRakanAlMraikhat
0
73
find if path exists in graph
1,971
0.504
Easy
27,623
https://leetcode.com/problems/find-if-path-exists-in-graph/discuss/2238377/Python3-Solution-oror-Hashmap-DFS-and-BFS-oror-TC%3A-O(n)-SC%3A-O(n-%2B-h)-oror-Clearly-Explained!
class Solution: def validPath(self, n: int, edges: List[List[int]], source: int, destination: int) -> bool: # use the hashmap to convert from edges-connection list to adjacency map G = {} for node1, node2 in edges: # map node1 to node2 if node1 not in G: ...
find-if-path-exists-in-graph
Python3 Solution || Hashmap, DFS & BFS || TC: O(n), SC: O(n + h) || Clearly Explained!
minheapolis
0
89
find if path exists in graph
1,971
0.504
Easy
27,624
https://leetcode.com/problems/find-if-path-exists-in-graph/discuss/2238377/Python3-Solution-oror-Hashmap-DFS-and-BFS-oror-TC%3A-O(n)-SC%3A-O(n-%2B-h)-oror-Clearly-Explained!
class Solution: def validPath(self, n: int, edges: List[List[int]], source: int, destination: int) -> bool: # use the hashmap to convert from edges-connection list to adjacency map G = {} for node1, node2 in edges: # map node1 to node2 if node1 not in G: ...
find-if-path-exists-in-graph
Python3 Solution || Hashmap, DFS & BFS || TC: O(n), SC: O(n + h) || Clearly Explained!
minheapolis
0
89
find if path exists in graph
1,971
0.504
Easy
27,625
https://leetcode.com/problems/find-if-path-exists-in-graph/discuss/1965006/Python-DFS-%2B-BFS
class Solution: def validPath(self, n: int, edges: List[List[int]], source: int, destination: int) -> bool: def build_graph(edges): graph = {} for edge in edges: source, destination = edge if source not in graph: ...
find-if-path-exists-in-graph
Python, DFS + BFS
Hejita
0
250
find if path exists in graph
1,971
0.504
Easy
27,626
https://leetcode.com/problems/find-if-path-exists-in-graph/discuss/1877967/Python-dollarolution
class Solution: def validPath(self, n: int, edges: List[List[int]], source: int, destination: int) -> bool: if n < 3 or source == destination: return True tree = [source] visited = [source] while tree != []: for i in edges: if i[0] == tree[0]: ...
find-if-path-exists-in-graph
Python $olution
AakRay
0
110
find if path exists in graph
1,971
0.504
Easy
27,627
https://leetcode.com/problems/find-if-path-exists-in-graph/discuss/1814495/Python-or-Depth-First-Search-or-Iterative-or-Runtime-97
class Solution: def validPath(self, n: int, edges: List[List[int]], source: int, destination: int) -> bool: if source == destination: return True # create adjacency adjacency = collections.defaultdict(list) for x,y in edges: adjacency[x].append(y) ...
find-if-path-exists-in-graph
[Python] | Depth First Search | Iterative | Runtime 97%
haydarevren
0
159
find if path exists in graph
1,971
0.504
Easy
27,628
https://leetcode.com/problems/find-if-path-exists-in-graph/discuss/1569850/Python-Easy-BFS-solution-one-function-iterative
class Solution: def create_graph(self,n,edges): graph = {} for i in range(n): graph[i] = [] for i in edges: graph[i[0]].append(i[1]) graph[i[1]].append(i[0]) return graph def validPath(self, n: int, edges: List[List[int]], start: ...
find-if-path-exists-in-graph
Python Easy BFS solution one function iterative
Brillianttyagi
0
211
find if path exists in graph
1,971
0.504
Easy
27,629
https://leetcode.com/problems/find-if-path-exists-in-graph/discuss/1547516/Python3-solution
class Solution: def gen_graph(self, edges): graph = defaultdict(list) for u, v in edges: graph[u].append(v) graph[v].append(u) return graph def bfs(self, graph, start, end): q = deque([start]) visited = set([start]) while q: ...
find-if-path-exists-in-graph
Python3 solution
dalechoi
0
221
find if path exists in graph
1,971
0.504
Easy
27,630
https://leetcode.com/problems/find-if-path-exists-in-graph/discuss/1543833/Python-BFS
class Solution: def validPath(self, n: int, edges: List[List[int]], start: int, end: int) -> bool: if start == end: return True g = defaultdict(list) for u, v in edges: g[u].append(v) g[v].append(u) seen = set() ...
find-if-path-exists-in-graph
Python BFS
Jay1984
0
152
find if path exists in graph
1,971
0.504
Easy
27,631
https://leetcode.com/problems/find-if-path-exists-in-graph/discuss/1418947/Python3-using-BFS-method
class Solution: def validPath(self, n: int, edges: List[List[int]], start: int, end: int) -> bool: graph = defaultdict(list) for v1, v2 in edges: graph[v1].append(v2) graph[v2].append(v1) queue = deque() # FIFO visited = set() queue.append(sta...
find-if-path-exists-in-graph
[Python3] using BFS method
zhangzuxin007
0
110
find if path exists in graph
1,971
0.504
Easy
27,632
https://leetcode.com/problems/find-if-path-exists-in-graph/discuss/1409941/Python-3-BFS-solution
class Solution: def validPath(self, n: int, edges: List[List[int]], start: int, end: int) -> bool: if start == end: return True if len(edges) == 0: return False adj = [set() for _ in range(n)] seen = set() for u, v in edges: adj[u].add(v) adj[...
find-if-path-exists-in-graph
Python 3 - BFS solution
NamanGarg20
0
134
find if path exists in graph
1,971
0.504
Easy
27,633
https://leetcode.com/problems/minimum-time-to-type-word-using-special-typewriter/discuss/1417585/Python3-greedy
class Solution: def minTimeToType(self, word: str) -> int: ans = len(word) prev = "a" for ch in word: val = (ord(ch) - ord(prev)) % 26 ans += min(val, 26 - val) prev = ch return ans
minimum-time-to-type-word-using-special-typewriter
[Python3] greedy
ye15
48
2,200
minimum time to type word using special typewriter
1,974
0.714
Easy
27,634
https://leetcode.com/problems/minimum-time-to-type-word-using-special-typewriter/discuss/1928295/Python3-simple-solution
class Solution: def minTimeToType(self, word: str) -> int: count = 0 ini = 'a' for i in word: x = abs(ord(i) - ord(ini)) count += min(x, 26-x) + 1 ini = i return count
minimum-time-to-type-word-using-special-typewriter
Python3 simple solution
EklavyaJoshi
2
76
minimum time to type word using special typewriter
1,974
0.714
Easy
27,635
https://leetcode.com/problems/minimum-time-to-type-word-using-special-typewriter/discuss/1441408/Python-O(N)
class Solution: def minTimeToType(self, word: str) -> int: count = 0 prev = 0 for idx in map(lambda c: ord(c) - ord('a'), word): distance = abs(idx-prev) count += 1 + min(distance, 26-distance) prev = idx return count
minimum-time-to-type-word-using-special-typewriter
Python, O(N)
blue_sky5
2
68
minimum time to type word using special typewriter
1,974
0.714
Easy
27,636
https://leetcode.com/problems/minimum-time-to-type-word-using-special-typewriter/discuss/1859511/Python3-99.24-or-Circle-Array-or-Easy-Implementation
class Solution: def minTimeToType(self, word: str) -> int: d = {chr(i):(i-97) for i in range(97, 123)} cur = 'a' ans = 0 for w in word: offset = min(abs(d[w] - d[cur]), 26 - abs(d[w] - d[cur])) cur = w ans += offset + 1 return ...
minimum-time-to-type-word-using-special-typewriter
Python3 99.24% | Circle Array | Easy Implementation
doneowth
1
32
minimum time to type word using special typewriter
1,974
0.714
Easy
27,637
https://leetcode.com/problems/minimum-time-to-type-word-using-special-typewriter/discuss/2840245/Python-1-line%3A-Optimal-and-Clean-with-explanation-2-ways%3A-O(n)-time-and-O(1)-space
class Solution: # 1 liner # 3 parts. 1. starting from 'a' to word[0] # 2. move from each word[i] to word[i+1] # 3. add len(word) for len(word) key-presses def minTimeToType(self, word: str) -> int: return min(abs(ord('a') - ord(word[0])), 26 - abs(ord('a') - ord(word[0]))) + sum(min(abs(ord...
minimum-time-to-type-word-using-special-typewriter
Python 1 line: Optimal and Clean with explanation - 2 ways: O(n) time and O(1) space
topswe
0
2
minimum time to type word using special typewriter
1,974
0.714
Easy
27,638
https://leetcode.com/problems/minimum-time-to-type-word-using-special-typewriter/discuss/2672951/Python-One-Liner-(Faster-Than-99.24)
class Solution: def minTimeToType(self, word: str) -> int: return sum([min(abs(ord(word[i]) - ord(word[i+1])), 26 - abs(ord(word[i]) - ord(word[i+1]))) for i in range(0, len(word)-1)]) + len(word) + min(abs(ord('a') - ord(word[0])), 26 - abs(ord('a') - ord(word[0])))
minimum-time-to-type-word-using-special-typewriter
Python One Liner (Faster Than 99.24%)
ashishkulkarnii
0
27
minimum time to type word using special typewriter
1,974
0.714
Easy
27,639
https://leetcode.com/problems/minimum-time-to-type-word-using-special-typewriter/discuss/1878033/Python-dollarolution
class Solution: def minTimeToType(self, word: str) -> int: current = 0 count = 0 for i in word: y = ord(i) - 97 x = abs(y - current) if x > 13: count += 26 - x + 1 else: count += x + 1 current = y ...
minimum-time-to-type-word-using-special-typewriter
Python $olution
AakRay
0
36
minimum time to type word using special typewriter
1,974
0.714
Easy
27,640
https://leetcode.com/problems/minimum-time-to-type-word-using-special-typewriter/discuss/1846948/Simple-Python3-Solution-oror-Faster-100-oror-Easy-to-understand-Python-Code
class Solution: def minTimeToType(self, word: str) -> int: x=0 t=0 c=0 c=ord(word[0])-ord('a'); if c>13: c=26-c t=c+1 for i in range(1,len(word)): x=ord(word[i])- ord(word[i-1]) if x<0...
minimum-time-to-type-word-using-special-typewriter
Simple Python3 Solution || Faster 100% || Easy to understand Python Code
RatnaPriya
0
47
minimum time to type word using special typewriter
1,974
0.714
Easy
27,641
https://leetcode.com/problems/minimum-time-to-type-word-using-special-typewriter/discuss/1826893/easy-python-code
class Solution: def minTimeToType(self, word: str) -> int: count = 0 l = "abcdefghijklmnopqrstuvwxyz" p = "a" for i in word: if abs(l.index(i)-l.index(p)) <= 13: dif = abs((l.index(i)-l.index(p))) count += dif+1 p = i ...
minimum-time-to-type-word-using-special-typewriter
easy python code
dakash682
0
52
minimum time to type word using special typewriter
1,974
0.714
Easy
27,642
https://leetcode.com/problems/minimum-time-to-type-word-using-special-typewriter/discuss/1798000/Python-or-Easy-or-Beats-99.85
class Solution: def minTimeToType(self, word: str) -> int: ss = 0 curr = "a" diff = abs(ord(curr) - ord(word[0])) if diff >13: diff = 26-diff ss += diff%26 for i in range(1,len(word)): diff = abs(ord(word[i]) - ord((word[i-1]))) ...
minimum-time-to-type-word-using-special-typewriter
Python | Easy | Beats 99.85%
veerbhansari
0
45
minimum time to type word using special typewriter
1,974
0.714
Easy
27,643
https://leetcode.com/problems/minimum-time-to-type-word-using-special-typewriter/discuss/1708966/Python3-accepted-solution
class Solution: def minTimeToType(self, word: str) -> int: if(len(word)==1): return len(word) + min(ord(word[0]) - ord('a'), 26 - (ord(word[0]) - ord('a'))) move = 0 for i in range(len(word)): if(i==0): move += min(ord(word[0]) - ord('a'), 26 - (ord(wo...
minimum-time-to-type-word-using-special-typewriter
Python3 accepted solution
sreeleetcode19
0
58
minimum time to type word using special typewriter
1,974
0.714
Easy
27,644
https://leetcode.com/problems/minimum-time-to-type-word-using-special-typewriter/discuss/1554408/Find-shortest-rotations-97-speed
class Solution: def minTimeToType(self, word: str) -> int: pos = rotations = 0 for c in word: new_pos = 97 - ord(c) a, b = (pos, new_pos) if pos <= new_pos else (new_pos, pos) rotations += min(b - a, a + 26 - b) pos = new_pos return rotations +...
minimum-time-to-type-word-using-special-typewriter
Find shortest rotations, 97% speed
EvgenySH
0
80
minimum time to type word using special typewriter
1,974
0.714
Easy
27,645
https://leetcode.com/problems/minimum-time-to-type-word-using-special-typewriter/discuss/1425359/Python3
class Solution: def minTimeToType(self, word: str) -> int: count = 0 curr = "a" for i in word: t = abs(ord(i)-ord(curr)) if t <= 12: count += 1 + t curr = i else: count += 1 + 26 - t curr = i return count
minimum-time-to-type-word-using-special-typewriter
[Python3]
StellerAmbition
0
48
minimum time to type word using special typewriter
1,974
0.714
Easy
27,646
https://leetcode.com/problems/maximum-matrix-sum/discuss/1417592/Python3-greedy
class Solution: def maxMatrixSum(self, matrix: List[List[int]]) -> int: ans = mult = 0 val = inf for i in range(len(matrix)): for j in range(len(matrix)): ans += abs(matrix[i][j]) val = min(val, abs(matrix[i][j])) if matrix[i][j] ...
maximum-matrix-sum
[Python3] greedy
ye15
12
581
maximum matrix sum
1,975
0.457
Medium
27,647
https://leetcode.com/problems/maximum-matrix-sum/discuss/1417589/Greedy-oror-Clean-and-Concise-oror-Well-Explained-oror-Easy-Approach
class Solution: def maxMatrixSum(self, matrix: List[List[int]]) -> int: s,c,z,m=0,0,0,float('inf') for i in range(len(matrix)): for j in range(len(matrix[0])): if matrix[i][j]<0: c+=1 if matrix[i][j]==0: z=1 s+=abs(matrix[i][j]) ...
maximum-matrix-sum
🐍 Greedy || Clean & Concise || Well-Explained || Easy Approach 📌📌
abhi9Rai
1
101
maximum matrix sum
1,975
0.457
Medium
27,648
https://leetcode.com/problems/maximum-matrix-sum/discuss/2834782/Python%3A-Optimal-and-Clean-with-explanation-%3A-O(n2)-time-%3A-O(1)-space
class Solution: # observe : if there are an even number of negatives, we can get rid of them all! just bubble the negatives towards each other... and pairwise become positive # if there's an odd number, we greedily pairwise the largest negatives in abs value. The remaining negative, we can actually transfer int...
maximum-matrix-sum
Python: Optimal and Clean with explanation -: O(n^2) time : O(1) space
topswe
0
1
maximum matrix sum
1,975
0.457
Medium
27,649
https://leetcode.com/problems/maximum-matrix-sum/discuss/2105547/python-3-oror-greedy-solution-oror-O(n2)O(1)
class Solution: def maxMatrixSum(self, matrix: List[List[int]]) -> int: odd = False # is there an odd number of negative values? s, m = 0, math.inf # sum, min for row in matrix: for num in row: if num < 0: odd = not odd n...
maximum-matrix-sum
python 3 || greedy solution || O(n^2)/O(1)
dereky4
0
34
maximum matrix sum
1,975
0.457
Medium
27,650
https://leetcode.com/problems/maximum-matrix-sum/discuss/1495981/Python-Solution-Maximum-Matrix-Sum
class Solution: def maxMatrixSum(self, matrix: List[List[int]]) -> int: ''' given : n*n matrix goal : maximize summation of all elements in matrix 1. The max possible sum can be sum of all positive elements in matrix i.e when all are positive. 2. Now suppose if there...
maximum-matrix-sum
[Python] Solution - Maximum Matrix Sum
SaSha59
0
109
maximum matrix sum
1,975
0.457
Medium
27,651
https://leetcode.com/problems/maximum-matrix-sum/discuss/1500836/Python-O(n2)-time-O(1)-space-solution
class Solution(object): def maxMatrixSum(self, matrix): """ :type matrix: List[List[int]] :rtype: int """ n = len(matrix) s = 0 s_abs = 0 neg = 0 maxneg = float('-inf') minabs = float('inf') for i in range(n): for...
maximum-matrix-sum
Python O(n^2) time, O(1) space solution
byuns9334
-1
126
maximum matrix sum
1,975
0.457
Medium
27,652
https://leetcode.com/problems/number-of-ways-to-arrive-at-destination/discuss/1417598/Python3-dfs-%2B-dp
class Solution: def countPaths(self, n: int, roads: List[List[int]]) -> int: graph = {} for u, v, time in roads: graph.setdefault(u, {})[v] = time graph.setdefault(v, {})[u] = time dist = [inf]*n dist[-1] = 0 stack = [(n-1, 0)] wh...
number-of-ways-to-arrive-at-destination
[Python3] dfs + dp
ye15
5
846
number of ways to arrive at destination
1,976
0.323
Medium
27,653
https://leetcode.com/problems/number-of-ways-to-arrive-at-destination/discuss/1417598/Python3-dfs-%2B-dp
class Solution: def countPaths(self, n: int, roads: List[List[int]]) -> int: graph = {} for u, v, time in roads: graph.setdefault(u, []).append((v, time)) graph.setdefault(v, []).append((u, time)) dist = [inf] * n dist[0] = 0 ways = [0] * n ...
number-of-ways-to-arrive-at-destination
[Python3] dfs + dp
ye15
5
846
number of ways to arrive at destination
1,976
0.323
Medium
27,654
https://leetcode.com/problems/number-of-ways-to-arrive-at-destination/discuss/1417975/Concise-solution-with-pq-implementation-of-Dijkstra.
class Solution: def countPaths(self, n: int, roads: List[List[int]]) -> int: neighbours = defaultdict(set) graph = [[0]*n for _ in range(n)] for u,v,d in roads: neighbours[u].add(v) neighbours[v].add(u) graph[u][v] = d graph[v...
number-of-ways-to-arrive-at-destination
Concise solution with pq implementation of Dijkstra.
worker-bee
1
157
number of ways to arrive at destination
1,976
0.323
Medium
27,655
https://leetcode.com/problems/number-of-ways-to-arrive-at-destination/discuss/2710145/Simplest-solution-with-intuition
class Solution: def countPaths(self, n: int, roads: List[List[int]]) -> int: time = [float("inf")]*n time[0] = 0 ways = [0]*n ways[0] = 1 minHeap = [] heappush(minHeap,(0,0)) graph = defaultdict(list) for road in roads: src,dst,t...
number-of-ways-to-arrive-at-destination
Simplest solution with intuition
shriyansnaik
0
8
number of ways to arrive at destination
1,976
0.323
Medium
27,656
https://leetcode.com/problems/number-of-ways-to-arrive-at-destination/discuss/2497651/Python3-solution%3A-faster-than-most-submissions-oror-Clean-code
class Solution: def countPaths(self, n: int, roads: List[List[int]]) -> int: graph = defaultdict(dict) for u, v, w in roads: graph[u][v] = graph[v][u] = w dist = {i:float(inf) for i in range(n)} ways = {i:0 for i in range(n)} dist[0], ways[0] = 0, 1 heap =...
number-of-ways-to-arrive-at-destination
✔️ Python3 solution: faster than most submissions || Clean code
explusar
0
56
number of ways to arrive at destination
1,976
0.323
Medium
27,657
https://leetcode.com/problems/number-of-ways-to-arrive-at-destination/discuss/2338273/Python-3or-Need-Help-Figuring-Why-My-DFS-Does-Not-Count-All-Valid-Paths
class Solution: def countPaths(self, n: int, roads: List[List[int]]) -> int: #basically, have a helper dfs function that dfs until we hit n-1th node! #If the total time <= cur_min_time, we inccrement our answer! #otherwise, return to parent and explore other unexplored paths from parent ...
number-of-ways-to-arrive-at-destination
Python 3| Need Help Figuring Why My DFS Does Not Count All Valid Paths
JOON1234
0
38
number of ways to arrive at destination
1,976
0.323
Medium
27,658
https://leetcode.com/problems/number-of-ways-to-arrive-at-destination/discuss/1417989/Python-3-Topological-sort-%2B-Dijkstra-(304ms)
class Solution: def countPaths(self, n: int, roads: List[List[int]]) -> int: g = defaultdict(set) deg = [0] * n for u, v, t in roads: if u > v: u, v = v, u g[u].add((v, t)) deg[v] += 1 vis = defaultdict(lambda: float('inf'), {0: 0}...
number-of-ways-to-arrive-at-destination
[Python 3] Topological sort + Dijkstra (304ms)
chestnut890123
0
307
number of ways to arrive at destination
1,976
0.323
Medium
27,659
https://leetcode.com/problems/number-of-ways-to-separate-numbers/discuss/1424057/Python3-dp
class Solution: def numberOfCombinations(self, num: str) -> int: n = len(num) lcs = [[0]*(n+1) for _ in range(n)] for i in reversed(range(n)): for j in reversed(range(i+1, n)): if num[i] == num[j]: lcs[i][j] = 1 + lcs[i+1][j+1] def cmp(i, j, d):...
number-of-ways-to-separate-numbers
[Python3] dp
ye15
3
367
number of ways to separate numbers
1,977
0.209
Hard
27,660
https://leetcode.com/problems/find-greatest-common-divisor-of-array/discuss/2580234/2-lines-PythonJavascript-(no-built-in-gcd-function)
class Solution: def findGCD(self, nums: List[int]) -> int: gcd = lambda a, b: a if b == 0 else gcd(b, a % b) return gcd(max(nums), min(nums))
find-greatest-common-divisor-of-array
2 lines Python/Javascript (no built in gcd function)
SmittyWerbenjagermanjensen
1
125
find greatest common divisor of array
1,979
0.767
Easy
27,661
https://leetcode.com/problems/find-greatest-common-divisor-of-array/discuss/2808307/O(n)-54ms-python
class Solution: #O(n) traverse #O(1) space def getMinMax(self, nums: List[int]) -> List[int]: min, max = nums[0], nums[0] for num in nums: if max < num: max = num if min > num: min = num return [max,min] #Euclidian gcd #constraint: a>b #O(h) where...
find-greatest-common-divisor-of-array
O(n) 54ms python
lucasscodes
0
8
find greatest common divisor of array
1,979
0.767
Easy
27,662
https://leetcode.com/problems/find-greatest-common-divisor-of-array/discuss/2801474/Just-2-line-code-in-Python
class Solution: def findGCD(self, nums: List[int]) -> int: minV, maxV = min(nums), max(nums) return max ([k for k in range(1, min (minV, maxV)+1) if minV % k == 0 and maxV % k == 0])
find-greatest-common-divisor-of-array
Just 2 line code in Python
DNST
0
3
find greatest common divisor of array
1,979
0.767
Easy
27,663
https://leetcode.com/problems/find-greatest-common-divisor-of-array/discuss/2800127/Python-Approach-for-The-Question
class Solution: def findGCD(self, nums: List[int]) -> int: import math return math.gcd(max(nums),min(nums))
find-greatest-common-divisor-of-array
Python Approach for The Question
TanmayNewatia
0
4
find greatest common divisor of array
1,979
0.767
Easy
27,664
https://leetcode.com/problems/find-greatest-common-divisor-of-array/discuss/2768984/Python-99.21-Fastest-Solution-Explained-(With-Submission-Link)
class Solution: def findGCD(self, nums: List[int]) -> int: l,h = min(nums),max(nums) #we create these temps instead of calling min &amp; max in iteration to increase speed for t in range(l,0,-1): #divisor will always be smaller than low if l % t == 0 and h % t == 0: return t re...
find-greatest-common-divisor-of-array
[Python] - 99.21% Fastest Solution Explained (With Submission Link)
keioon
0
8
find greatest common divisor of array
1,979
0.767
Easy
27,665
https://leetcode.com/problems/find-greatest-common-divisor-of-array/discuss/2716003/one-line-oror-Python
class Solution: def findGCD(self, nums: List[int]) -> int: nums.sort() return gcd(nums[0],nums[-1])
find-greatest-common-divisor-of-array
one line || Python
MaryLuz
0
1
find greatest common divisor of array
1,979
0.767
Easy
27,666
https://leetcode.com/problems/find-greatest-common-divisor-of-array/discuss/2679934/Python3-Solution-using-Modular-Approach
class Solution: def findGCD(self, nums: List[int]) -> int: return self.solve(min(nums), max(nums)) def solve(self, a, b): if b == 0: return a return self.solve(b, a%b)
find-greatest-common-divisor-of-array
Python3 Solution using Modular Approach
anurag_patil007
0
15
find greatest common divisor of array
1,979
0.767
Easy
27,667
https://leetcode.com/problems/find-greatest-common-divisor-of-array/discuss/2534634/iterative
class Solution: def findGCD(self, nums: List[int]) -> int: # obtain min &amp; max of array # store a GCD var to update everytime we find one # iterate from [1, min] to find GCD of min &amp; max # if it is, set GCD var to it (iterate ascending) # return GCD # Time O(N)...
find-greatest-common-divisor-of-array
iterative
andrewnerdimo
0
13
find greatest common divisor of array
1,979
0.767
Easy
27,668
https://leetcode.com/problems/find-greatest-common-divisor-of-array/discuss/2426765/Find-gcd-easy-and-fast-python-solution-using-math.gcd()
class Solution: def findGCD(self, nums: List[int]) -> int: sorted_nums = sorted(nums) smallest_num = min(sorted_nums) largest_num = max(sorted_nums) return math.gcd(smallest_num, largest_num)
find-greatest-common-divisor-of-array
Find gcd easy and fast python solution using math.gcd()
samanehghafouri
0
14
find greatest common divisor of array
1,979
0.767
Easy
27,669
https://leetcode.com/problems/find-greatest-common-divisor-of-array/discuss/2105252/Euclidean-Algorithm-or-GCD-or-Python3
class Solution: def findGCD(self, nums: List[int]) -> int: #finding smallest number and largest number smallestNum = min( nums ) largestNum = max( nums ) #Creating a recursive function for finding the GCD of these two numbers def GCD( big, small )...
find-greatest-common-divisor-of-array
Euclidean Algorithm | GCD | Python3
athrvb
0
56
find greatest common divisor of array
1,979
0.767
Easy
27,670
https://leetcode.com/problems/find-greatest-common-divisor-of-array/discuss/2060218/Python-or-Very-Simple-Solution-or-TC%3A-O(N)-SC%3A-O(1)
class Solution: # ////////// Using Euclidean Algorithm to compute gcd////////// def gcd(self,n1,n2): while n2: n1, n2 = n2, n1 % n2 return n1 def findGCD(self, nums: List[int]) -> int: nums.sort() maxNum = nums[-1] minNum = nums[0] ...
find-greatest-common-divisor-of-array
Python | Very Simple Solution | TC: O(N) SC: O(1)
__Asrar
0
74
find greatest common divisor of array
1,979
0.767
Easy
27,671
https://leetcode.com/problems/find-greatest-common-divisor-of-array/discuss/2044713/Python-simple-solution
class Solution: def findGCD(self, nums: List[int]) -> int: ans = [] for i in range(1,max(nums)+1): if min(nums)%i == 0 and max(nums)%i == 0: ans.append(i) return max(ans)
find-greatest-common-divisor-of-array
Python simple solution
StikS32
0
82
find greatest common divisor of array
1,979
0.767
Easy
27,672
https://leetcode.com/problems/find-greatest-common-divisor-of-array/discuss/1878061/Python-dollarolution
class Solution: def findGCD(self, nums: List[int]) -> int: x, y = min(nums), max(nums) c = 1 if y%x == 0: return x for i in range(2,x//2 + 1): if x%i == 0 and y%i == 0: c = i return c
find-greatest-common-divisor-of-array
Python $olution
AakRay
0
79
find greatest common divisor of array
1,979
0.767
Easy
27,673
https://leetcode.com/problems/find-greatest-common-divisor-of-array/discuss/1874844/Python3-Recursion
class Solution: def findGCD(self, nums: List[int]) -> int: gcd = lambda x, y: x if y == 0 else gcd(y, x%y) return gcd(min(nums),max(nums))
find-greatest-common-divisor-of-array
✔Python3 Recursion
khRay13
0
43
find greatest common divisor of array
1,979
0.767
Easy
27,674
https://leetcode.com/problems/find-greatest-common-divisor-of-array/discuss/1860649/Python-one-line-solution
class Solution: def findGCD(self, nums: List[int]) -> int: return math.gcd(min(nums), max(nums))
find-greatest-common-divisor-of-array
Python one line solution
alishak1999
0
61
find greatest common divisor of array
1,979
0.767
Easy
27,675
https://leetcode.com/problems/find-greatest-common-divisor-of-array/discuss/1826628/Faster-than-99.21-Memory-less-than-44.39
class Solution: def findGCD(self, nums: List[int]) -> int: mi = min(nums) ma = max(nums) return math.gcd(mi, ma)
find-greatest-common-divisor-of-array
Faster than 99.21% // Memory less than 44.39%
yelatoks
0
75
find greatest common divisor of array
1,979
0.767
Easy
27,676
https://leetcode.com/problems/find-greatest-common-divisor-of-array/discuss/1694862/Python-Two-Solutions
class Solution: def findGCD(self, nums: List[int]) -> int: # return self.ekub(max(nums), min(nums)) return self.ekub2(nums) def ekub(self, a, b) -> int: if a == b: return a if a == 1: return 1 if b == 1: return 1 c = b ...
find-greatest-common-divisor-of-array
Python Two Solutions
CleverUzbek
0
82
find greatest common divisor of array
1,979
0.767
Easy
27,677
https://leetcode.com/problems/find-greatest-common-divisor-of-array/discuss/1682332/Step-by-Step-Solution
class Solution: def findGCD(self, nums: List[int]) -> int: def gcd(a,b): if b%a == 0: return a gcd = 1 for i in range(1, a+1): if a % i == 0 and b % i == 0: gcd = i return gcd nums....
find-greatest-common-divisor-of-array
Step by Step Solution
snagsbybalin
0
95
find greatest common divisor of array
1,979
0.767
Easy
27,678
https://leetcode.com/problems/find-greatest-common-divisor-of-array/discuss/1600511/python
class Solution: def findGCD(self, nums: List[int]) -> int: def gcda(a,b): if b == 0: return a return gcda(b,a%b) mn = 1001 mx = 0 for i in nums: if i<mn: mn= i if i>mx: mx=i ...
find-greatest-common-divisor-of-array
python
dsalgobros
0
88
find greatest common divisor of array
1,979
0.767
Easy
27,679
https://leetcode.com/problems/find-greatest-common-divisor-of-array/discuss/1583710/python3-readable-easy
class Solution: def findGCD(self, nums: List[int]) -> int: result = 1 mn = min(nums) mx = max(nums) for i in range(1,mx+1): if mn%i == 0 and mx%i ==0: result = i return result
find-greatest-common-divisor-of-array
python3 readable easy
RionelKuster
0
62
find greatest common divisor of array
1,979
0.767
Easy
27,680
https://leetcode.com/problems/find-greatest-common-divisor-of-array/discuss/1558076/simple-and-91.29-faster
class Solution: def findGCD(self, nums: List[int]) -> int: a=max(nums) b=min(nums) q=a//b r=a%b while(r!=0): a=b b=r q=a//b r=a%b a=b*q+r return b
find-greatest-common-divisor-of-array
simple and 91.29% faster
srinath1reddy
0
55
find greatest common divisor of array
1,979
0.767
Easy
27,681
https://leetcode.com/problems/find-greatest-common-divisor-of-array/discuss/1457943/Faster-than-91-oror-Python-3-oror-using-custom-GCD-function-oror-Euclid's-Lemma
class Solution: def findGCD(self, nums: List[int]) -> int: def gcd(q,div): rem = q%div ans = div while(rem!=0): q=div div = rem rem = q%div ans = div return ans ...
find-greatest-common-divisor-of-array
Faster than 91% || Python 3 || using custom GCD function || Euclid's Lemma
ana_2kacer
0
207
find greatest common divisor of array
1,979
0.767
Easy
27,682
https://leetcode.com/problems/find-greatest-common-divisor-of-array/discuss/1442477/Python-Euclidean-algorithm
class Solution: def findGCD(self, nums: List[int]) -> int: a, b = min(nums), max(nums) while a: a, b = b % a, a return b
find-greatest-common-divisor-of-array
Python, Euclidean algorithm
blue_sky5
0
82
find greatest common divisor of array
1,979
0.767
Easy
27,683
https://leetcode.com/problems/find-greatest-common-divisor-of-array/discuss/1430337/python-Recursive-solution
class Solution: def findGCD(self, nums: List[int]) -> int: def parser(max_num, min_num): residue = max_num % min_num if not residue: return min_num return parser(min_num, residue) max_num = nums[0] min_num = nums[0] for num in nums:...
find-greatest-common-divisor-of-array
python Recursive solution
mike_d0_ob
0
64
find greatest common divisor of array
1,979
0.767
Easy
27,684
https://leetcode.com/problems/find-greatest-common-divisor-of-array/discuss/1424079/Python3-1-line
class Solution: def findGCD(self, nums: List[int]) -> int: return gcd(min(nums), max(nums))
find-greatest-common-divisor-of-array
[Python3] 1-line
ye15
0
47
find greatest common divisor of array
1,979
0.767
Easy
27,685
https://leetcode.com/problems/find-greatest-common-divisor-of-array/discuss/1418697/python-simple-way-to-find-GCD
class Solution: def findGCD(self, nums: List[int]) -> int: inNum = sorted(nums) a = inNum[0] b = inNum[len(nums)-1] while b != 0: (a,b) = (b,a%b) return a
find-greatest-common-divisor-of-array
python - simple way to find GCD
JoeyThirawat
0
74
find greatest common divisor of array
1,979
0.767
Easy
27,686
https://leetcode.com/problems/find-unique-binary-string/discuss/1657090/One-line-python-Solution
class Solution: def findDifferentBinaryString(self, nums: List[str]) -> str: return list(set(list((map(lambda x:"".join(list(map(str,x))),list(itertools.product([0,1],repeat=len(nums)))))))-set(nums))[0]
find-unique-binary-string
One line python Solution
amannarayansingh10
2
159
find unique binary string
1,980
0.643
Medium
27,687
https://leetcode.com/problems/find-unique-binary-string/discuss/1424091/Python3-greedy
class Solution: def findDifferentBinaryString(self, nums: List[str]) -> str: ans = [] for i, x in enumerate(nums): if x[i] == "1": ans.append("0") else: ans.append("1") return "".join(ans)
find-unique-binary-string
[Python3] greedy
ye15
2
66
find unique binary string
1,980
0.643
Medium
27,688
https://leetcode.com/problems/find-unique-binary-string/discuss/1421750/Simple-python-recursive-approach-100-faster
class Solution: def findDifferentBinaryString(self, nums: List[str]) -> str: nums=set(nums) def recur(string,depth): if depth == 0: return string if string not in nums else "" for value in "01": ans=recur(string+value,depth-...
find-unique-binary-string
Simple python recursive approach 100% faster
hasham
2
224
find unique binary string
1,980
0.643
Medium
27,689
https://leetcode.com/problems/find-unique-binary-string/discuss/2823559/Python-solution-O(n)
class Solution: def findDifferentBinaryString(self, nums: List[str]) -> str: ans = [] for i in range(len(nums)): digit = nums[i][i] if digit == '1': ans.append('0') else: ans.append('1') return "".join(ans)
find-unique-binary-string
Python solution - O(n)
rxhxlx
0
4
find unique binary string
1,980
0.643
Medium
27,690
https://leetcode.com/problems/find-unique-binary-string/discuss/2801183/Python-3-Solution
class Solution: def findDifferentBinaryString(self, nums: List[str]) -> str: self.ans=[False,''] self.backtracking('',nums) return self.ans[1] def backtracking(self,substring,nums): if self.ans[0] or len(substring)>len(nums[0]):return for i in nums: if i.start...
find-unique-binary-string
Python 3 Solution
Burak1069711851
0
2
find unique binary string
1,980
0.643
Medium
27,691
https://leetcode.com/problems/find-unique-binary-string/discuss/2689706/Python-O(N)-Space-and-Time-oror-Easy-to-Understand-no-Recursion
class Solution: def findDifferentBinaryString(self, nums: List[str]) -> str: present=set() # O(N)=O(16) for n in nums: present.add(int(n,base=2)) # although we search through 2^N elements, since nums is of size N, and each element of nums in of size N, # our wor...
find-unique-binary-string
Python O(N) Space and Time || Easy to Understand, no Recursion
d4mahu
0
5
find unique binary string
1,980
0.643
Medium
27,692
https://leetcode.com/problems/find-unique-binary-string/discuss/2520136/Python3-solution-or-Simple-solution
class Solution: def findDifferentBinaryString(self, nums: List[str]) -> str: n = len(nums) nums = set(nums) for i in range(2**n, 2**(n+1)): if bin(i)[3:] not in nums: return bin(i)[3:]
find-unique-binary-string
Python3 solution | Simple solution
FlorinnC1
0
34
find unique binary string
1,980
0.643
Medium
27,693
https://leetcode.com/problems/find-unique-binary-string/discuss/2461110/Python3-or-Solved-Using-Recursion-%2B-Backtracking-%2B-Boolean-Flag
class Solution: #Time-Complexity: O(n + 2^n), O(n) time to initialize start string, but in worst case, the #answer leaf node would be located at rightmost leaf node at last level, which would #imply O(2^n) -> O(2^n) #Space-Complexity: The recursive solution utilizes call stack which holds at most #...
find-unique-binary-string
Python3 | Solved Using Recursion + Backtracking + Boolean Flag
JOON1234
0
28
find unique binary string
1,980
0.643
Medium
27,694
https://leetcode.com/problems/find-unique-binary-string/discuss/2128185/Python-Intuitive-Solution-or-O(n)-Time-O(1)-Space
class Solution: def findDifferentBinaryString(self, nums: List[str]) -> str: n: int = len(nums) nums_as_set: Set[int] = set(nums) for i in range(2**n+1): bin_string = f"{i:0{n}b}" if not bin_string in nums_as_set: return bin_string
find-unique-binary-string
Python Intuitive Solution | O(n) Time, O(1) Space
cschan1828
0
62
find unique binary string
1,980
0.643
Medium
27,695
https://leetcode.com/problems/find-unique-binary-string/discuss/1971970/Python-easy-to-read-and-understand-or-recursion
class Solution: def solve(self, nums, res, cnt): if cnt >= self.n: if cnt == self.n and res not in self.visit: self.ans.append(res) return for val in nums: self.solve(nums, res + val, cnt + 1) def findDifferentBinaryString(self, nums: List[str...
find-unique-binary-string
Python easy to read and understand | recursion
sanial2001
0
91
find unique binary string
1,980
0.643
Medium
27,696
https://leetcode.com/problems/find-unique-binary-string/discuss/1971970/Python-easy-to-read-and-understand-or-recursion
class Solution: def solve(self, nums, res, cnt, n): if cnt >= n: if cnt == n and res not in self.visit: return res return 0 for val in nums: temp = self.solve(nums, res+val, cnt+1, n) if temp: return temp els...
find-unique-binary-string
Python easy to read and understand | recursion
sanial2001
0
91
find unique binary string
1,980
0.643
Medium
27,697
https://leetcode.com/problems/find-unique-binary-string/discuss/1966571/Simple-5-line-solution
class Solution: def findDifferentBinaryString(self, nums: List[str]) -> str: for a in range(2**len(nums)): b = str(bin(a)).replace('0b', "") b = ('0'*(len(nums)-len(b))) + b if b not in nums: return b
find-unique-binary-string
Simple 5 line solution
Tonmoy-saha18
0
42
find unique binary string
1,980
0.643
Medium
27,698
https://leetcode.com/problems/find-unique-binary-string/discuss/1953588/Python-simple-solution-or-Easy-to-understand
class Solution(object): def findDifferentBinaryString(self, nums): """ :type nums: List[str] :rtype: str """ d = {} for i in nums: if i not in d: d[i] = 1 a = len(nums[0]) b = 2**a + 1 for i in range(b): ...
find-unique-binary-string
Python simple solution | Easy to understand
AkashHooda
0
44
find unique binary string
1,980
0.643
Medium
27,699