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/count-unhappy-friends/discuss/2345920/Success-Details-Runtime%3A-394-ms-faster-than-99.40-of-Python3-online-submissions-oror-vimla_kushwaha | class Solution:
def unhappyFriends(self, n: int, preferences: List[List[int]], pairs: List[List[int]]) -> int:
dd = {}
for i,x in pairs:
dd[i] = preferences[i][:preferences[i].index(x)]
dd[x] = preferences[x][:preferences[x].index(i)]
ans = 0
... | count-unhappy-friends | Success Details Runtime: 394 ms, faster than 99.40% of Python3 online submissions || vimla_kushwaha | vimla_kushwaha | 2 | 136 | count unhappy friends | 1,583 | 0.603 | Medium | 23,300 |
https://leetcode.com/problems/count-unhappy-friends/discuss/844034/Python-3-or-Brutal-Force-Hash-Table-O(n2)-or-Explanations | class Solution:
def unhappyFriends(self, n: int, preferences: List[List[int]], pairs: List[List[int]]) -> int:
idx_table = collections.defaultdict(lambda: collections.defaultdict(int))
for i in range(n):
for idx, person in enumerate(preferences[i]): idx_table[i][person] = idx
... | count-unhappy-friends | Python 3 | Brutal Force, Hash Table, O(n^2) | Explanations | idontknoooo | 2 | 510 | count unhappy friends | 1,583 | 0.603 | Medium | 23,301 |
https://leetcode.com/problems/count-unhappy-friends/discuss/1325617/13-line-simple-clean-python | class Solution:
def unhappyFriends(self, n: int, preferences: List[List[int]], pairs: List[List[int]]) -> int:
likemore = {}
for a,b in pairs:
likemore[a] = set(preferences[a][:preferences[a].index(b)])
likemore[b] = set(preferences[b][:preferences[b].index(a)])
unhap... | count-unhappy-friends | 13 line simple clean python | artalukd | 1 | 188 | count unhappy friends | 1,583 | 0.603 | Medium | 23,302 |
https://leetcode.com/problems/count-unhappy-friends/discuss/2845736/Python-O(n3)-follow-the-rules-word-by-word | class Solution:
def unhappyFriends(self, n: int, preferences: List[List[int]], pairs: List[List[int]]) -> int:
# iterate through the pairs
# add pairs to d: d[x] = y, d[y] = x
# check each pair x, y in preferences to see if
# for friends in preferences[x][:indexy]:
# check... | count-unhappy-friends | Python O(n^3) follow the rules word by word | monkecoder | 0 | 1 | count unhappy friends | 1,583 | 0.603 | Medium | 23,303 |
https://leetcode.com/problems/count-unhappy-friends/discuss/1307222/Python3-10-lines-and-extended-solution-for-readability-or-HashtableDict | class Solution:
def unhappyFriends(self, n: int, preferences: List[List[int]], pairs: List[List[int]]) -> int:
total = 0
pair_map = {p[0]:p[1] for p in pairs} | {p[1]:p[0] for p in pairs}
for pair in pair_map.keys():
for i in range(preferences[pair].index(pair_map[pair]) + 1):
... | count-unhappy-friends | [Python3] 10 lines and extended solution for readability | Hashtable/Dict | random_user_4389 | 0 | 101 | count unhappy friends | 1,583 | 0.603 | Medium | 23,304 |
https://leetcode.com/problems/count-unhappy-friends/discuss/1307222/Python3-10-lines-and-extended-solution-for-readability-or-HashtableDict | class Solution:
def unhappyFriends(self, n: int, preferences: List[List[int]], pairs: List[List[int]]) -> int:
total = 0
pair_map = {p[0]:p[1] for p in pairs} | {p[1]:p[0] for p in pairs}
for pair in pair_map.keys():
preferences_for_pair = preferences[pair]
check_matc... | count-unhappy-friends | [Python3] 10 lines and extended solution for readability | Hashtable/Dict | random_user_4389 | 0 | 101 | count unhappy friends | 1,583 | 0.603 | Medium | 23,305 |
https://leetcode.com/problems/count-unhappy-friends/discuss/847802/O(n2)-solution-by-rearranging-data | class Solution:
def unhappyFriends(self, n: int, preferences: List[List[int]], pairs: List[List[int]]) -> int:
indices = [[0] * n for i in range(n)]
for i, p in enumerate(preferences):
for j, f in enumerate(p):
indices[i][f] = j
pairedWith = [0] *... | count-unhappy-friends | O(n^2) solution by rearranging data | snibbets | 0 | 81 | count unhappy friends | 1,583 | 0.603 | Medium | 23,306 |
https://leetcode.com/problems/count-unhappy-friends/discuss/844026/Python-Brute-Force-using-index-of-item | class Solution:
def unhappyFriends(self, n: int, preferences: List[List[int]], pairs: List[List[int]]) -> int:
prefDict = {i:preferences[i] for i in range(n)}
res = set()
for i, (x1,y1) in enumerate(pairs):
for j, (x2,y2) in enumerate(pairs):
if i != j:
... | count-unhappy-friends | Python Brute Force using index of item | akrishna06 | 0 | 52 | count unhappy friends | 1,583 | 0.603 | Medium | 23,307 |
https://leetcode.com/problems/min-cost-to-connect-all-points/discuss/843995/Python-3-or-Min-Spanning-Tree-or-Prim's-Algorithm | class Solution:
def minCostConnectPoints(self, points: List[List[int]]) -> int:
manhattan = lambda p1, p2: abs(p1[0]-p2[0]) + abs(p1[1]-p2[1])
n, c = len(points), collections.defaultdict(list)
for i in range(n):
for j in range(i+1, n):
d = manhattan(points[i], poi... | min-cost-to-connect-all-points | Python 3 | Min Spanning Tree | Prim's Algorithm | idontknoooo | 80 | 13,100 | min cost to connect all points | 1,584 | 0.641 | Medium | 23,308 |
https://leetcode.com/problems/min-cost-to-connect-all-points/discuss/1982821/Python-Simple-and-Concise-MST-with-Explanation | class Solution:
def minCostConnectPoints(self, points: List[List[int]]) -> int:
d, res = {(x, y): float('inf') if i else 0 for i, (x, y) in enumerate(points)}, 0
while d:
x, y = min(d, key=d.get) # obtain the current minimum edge
res += d.pop((x, y)) # and remove the co... | min-cost-to-connect-all-points | [Python] Simple and Concise MST with Explanation | zayne-siew | 60 | 4,100 | min cost to connect all points | 1,584 | 0.641 | Medium | 23,309 |
https://leetcode.com/problems/min-cost-to-connect-all-points/discuss/1487547/Python-Kruskal's-with-Union-Find-and-explanation | class Solution:
def minCostConnectPoints(self, points: List[List[int]]) -> int:
if len(points) <= 1:
return 0
graph = self.create_graph(points)
graph.sort(key=lambda x: x[2])
n = len(points)
uf = UnionFind(n)
result = 0
... | min-cost-to-connect-all-points | Python Kruskal's with Union Find and explanation | SleeplessChallenger | 12 | 455 | min cost to connect all points | 1,584 | 0.641 | Medium | 23,310 |
https://leetcode.com/problems/min-cost-to-connect-all-points/discuss/1987578/Python-Prim-Algorithm-oror-Easy-and-Simple-Solution | class Solution:
def minCostConnectPoints(self, points: List[List[int]]) -> int:
N = len(points)
adj = {i:[] for i in range(N)}
for i in range(N):
x1, y1 = points[i]
for j in range(i+1, N):
x2, y2 = points[j]
dis = abs(... | min-cost-to-connect-all-points | Python - Prim Algorithm || Easy and Simple Solution | dayaniravi123 | 1 | 27 | min cost to connect all points | 1,584 | 0.641 | Medium | 23,311 |
https://leetcode.com/problems/min-cost-to-connect-all-points/discuss/2799958/Python3-Submission-using-Union-Find-(Beats-~96-Time-and-~89-Space) | class Solution:
def to_tuple(self, points):
return tuple(tuple(point) for point in points)
def getDistance(self, x, y):
return abs(y[0]-x[0])+abs(y[1]-x[1])
def computeEdges(self, pointsTuple, length):
edges = []
for point1 in range(length-1):
for point2... | min-cost-to-connect-all-points | Python3 Submission using Union-Find (Beats ~96% Time & ~89% Space) | frizzid07 | 0 | 3 | min cost to connect all points | 1,584 | 0.641 | Medium | 23,312 |
https://leetcode.com/problems/min-cost-to-connect-all-points/discuss/2529316/Python-3-or-Kruskal's-algo-or-union-find-or-easy-to-understand | class Solution:
def minCostConnectPoints(self, points: List[List[int]]) -> int:
if len(points) == 1: return 0
# calculates manhattan distance
def dist(u, v):
x1, y1 = points[u]
x2, y2 = points[v]
return abs(x1 - x2) + abs(y1 - y2)
def fin... | min-cost-to-connect-all-points | Python 3 | Kruskal's algo | union find | easy to understand | user7726Y | 0 | 28 | min cost to connect all points | 1,584 | 0.641 | Medium | 23,313 |
https://leetcode.com/problems/min-cost-to-connect-all-points/discuss/2336838/Python-very-simple-beats-75-Prims-with-comments | class Solution:
def minCostConnectPoints(self, points: List[List[int]]) -> int:
cost = 0
heap = []
#set to store past points to prevent cycle
visited = set([0])
#i == the index of current point
i = 0
while len(visited) < len... | min-cost-to-connect-all-points | Python very simple beats 75% Prims with comments | mateobv07 | 0 | 75 | min cost to connect all points | 1,584 | 0.641 | Medium | 23,314 |
https://leetcode.com/problems/min-cost-to-connect-all-points/discuss/2035031/Python3-Prim's-algorithm | class Solution:
def minCostConnectPoints(self, points: List[List[int]]) -> int:
visited = set()
def getManhattanDist(x1, y1):
res = []
for x2, y2 in points:
if (x2, y2) not in visited:
res.append([abs(x1 - x2) + abs(y1 - y2), (x2, y2)])
... | min-cost-to-connect-all-points | Python3 Prim's algorithm | user6397p | 0 | 43 | min cost to connect all points | 1,584 | 0.641 | Medium | 23,315 |
https://leetcode.com/problems/min-cost-to-connect-all-points/discuss/2033061/Python3-solution-using-Prim's | class Solution:
def minCostConnectPoints(self, points: List[List[int]]) -> int:
"""Given an array points representing integer coordinates
of some points on a 2D-plane, where points[i] = [xi, yi],
return the minimum cost to make all points connected. All
points are connected if there ... | min-cost-to-connect-all-points | Python3 solution using Prim's | plasticuproject | 0 | 33 | min cost to connect all points | 1,584 | 0.641 | Medium | 23,316 |
https://leetcode.com/problems/min-cost-to-connect-all-points/discuss/1984461/Python3-Solution-with-using-prims-algorithm | class Solution:
def find_dist(self, pair1, pair2):
return abs(pair1[0] - pair2[0]) + abs(pair1[1] - pair2[1])
def minCostConnectPoints(self, points: List[List[int]]) -> int:
total_points = len(points)
d = collections.defaultdict(list)
for i in range(total_points):
... | min-cost-to-connect-all-points | [Python3] Solution with using prims algorithm | maosipov11 | 0 | 17 | min cost to connect all points | 1,584 | 0.641 | Medium | 23,317 |
https://leetcode.com/problems/min-cost-to-connect-all-points/discuss/1984109/Python-Solution-using-Prims-algorithm | class Solution:
def minCostConnectPoints(self, points: List[List[int]]) -> int:
# Create adjacency graph
n = len(points)
adj = {i: [] for i in range(n)}
for i in range(n):
x1, y1 = points[i]
for j in range(i + 1, n):
x2, y2 = points[j]
... | min-cost-to-connect-all-points | Python Solution using Prims algorithm | pradeep288 | 0 | 15 | min cost to connect all points | 1,584 | 0.641 | Medium | 23,318 |
https://leetcode.com/problems/min-cost-to-connect-all-points/discuss/1982735/Python-solution-faster-than-77 | class Solution:
def minCostConnectPoints(self, points: List[List[int]]) -> int:
n = len(points)
mst_cost = 0
edges_used = 0
in_mst = [False] * n
min_dist = [math.inf] * n
min_dist[0] = 0
while edges_used < n:
curr_min_edge = math.inf
cu... | min-cost-to-connect-all-points | Python solution faster than 77 % | bad_karma25 | 0 | 43 | min cost to connect all points | 1,584 | 0.641 | Medium | 23,319 |
https://leetcode.com/problems/min-cost-to-connect-all-points/discuss/1843171/Python-Two-solutions%3A-Kruskal's-and-Prim's-Algorithm-or-Complexity-analysis | class Solution:
def minCostConnectPoints(self, points: List[List[int]]) -> int:
import heapq
def distance(i,j):
return abs(points[i][0] - points[j][0]) + abs(points[i][1] - points[j][1])
visited = [0]
notvisited = set(range(1,len(points)))
ret ... | min-cost-to-connect-all-points | [Python] Two solutions: Kruskal's and Prim's Algorithm | Complexity analysis | haydarevren | 0 | 82 | min cost to connect all points | 1,584 | 0.641 | Medium | 23,320 |
https://leetcode.com/problems/min-cost-to-connect-all-points/discuss/1835990/Self-Understandable-Python-(Primes-%2B-Kruskal's)-%3A | class Solution:
def minCostConnectPoints(self, points: List[List[int]]) -> int:
if not points:
return 0
adlist=[]
size=len(points)
visited=[False]*size
x1,y1=points[0]
for i in range(1,size):
x2,y2=points[i]
cost=abs(x1-x2)+abs... | min-cost-to-connect-all-points | Self Understandable Python (Primes + Kruskal's) : | goxy_coder | 0 | 90 | min cost to connect all points | 1,584 | 0.641 | Medium | 23,321 |
https://leetcode.com/problems/min-cost-to-connect-all-points/discuss/1835990/Self-Understandable-Python-(Primes-%2B-Kruskal's)-%3A | class Solution:
def minCostConnectPoints(self, points: List[List[int]]) -> int:
if not points:
return 0
size=len(points)
adlist=[]
uf=UnionFind(size)
for i in range(size):
x1,y1=points[i]
for j in range(i+1,size):
... | min-cost-to-connect-all-points | Self Understandable Python (Primes + Kruskal's) : | goxy_coder | 0 | 90 | min cost to connect all points | 1,584 | 0.641 | Medium | 23,322 |
https://leetcode.com/problems/min-cost-to-connect-all-points/discuss/1791389/Python-solution-Minimum-spanning-tree-%2B-Prim's-algorithm-(with-union-find) | class Solution:
def initialize(self, n):
# all nodes are root node
self.parent = [i for i in range(n)]
def findParent(self, i):
parent = i
while parent != self.parent[parent]:
parent = self.parent[parent]
# compression
self.parent[i]... | min-cost-to-connect-all-points | Python solution, Minimum spanning tree + Prim's algorithm (with union find) | luke-mao | 0 | 69 | min cost to connect all points | 1,584 | 0.641 | Medium | 23,323 |
https://leetcode.com/problems/min-cost-to-connect-all-points/discuss/1488747/Python-Prim's-algo-with-self-made-Heap-and-explanation | class Solution:
def minCostConnectPoints(self, points: List[List[int]]) -> int:
graph = {}
self.create_graph(points, graph)
print(graph)
visited = {0}
min_heap = MinHeap(graph[0])
result = 0
while len(visited) < len(points):
cost, vertex = min_he... | min-cost-to-connect-all-points | Python Prim's algo with self-made Heap and explanation | SleeplessChallenger | 0 | 174 | min cost to connect all points | 1,584 | 0.641 | Medium | 23,324 |
https://leetcode.com/problems/check-if-string-is-transformable-with-substring-sort-operations/discuss/844119/Python3-8-line-deque | class Solution:
def isTransformable(self, s: str, t: str) -> bool:
if sorted(s) != sorted(t): return False # edge case
pos = [deque() for _ in range(10)]
for i, ss in enumerate(s): pos[int(ss)].append(i)
for tt in t:
i = pos[int(tt)].popleft()
... | check-if-string-is-transformable-with-substring-sort-operations | [Python3] 8-line deque | ye15 | 2 | 198 | check if string is transformable with substring sort operations | 1,585 | 0.484 | Hard | 23,325 |
https://leetcode.com/problems/sum-of-all-odd-length-subarrays/discuss/943380/Python-Simple-Solution | class Solution:
def sumOddLengthSubarrays(self, arr: List[int]) -> int:
s=0
for i in range(len(arr)):
for j in range(i,len(arr),2):
s+=sum(arr[i:j+1])
return s | sum-of-all-odd-length-subarrays | Python Simple Solution | lokeshsenthilkumar | 53 | 4,100 | sum of all odd length subarrays | 1,588 | 0.835 | Easy | 23,326 |
https://leetcode.com/problems/sum-of-all-odd-length-subarrays/discuss/1222855/44ms-Python-(with-comments-and-detailed-walkthrough) | class Solution(object):
def sumOddLengthSubarrays(self, arr):
"""
:type arr: List[int]
:rtype: int
"""
#We declare our output variable with the initial value as the sum of all elements as this is the 1st iteration of the sum of all individual elements.
result = sum(arr)
#... | sum-of-all-odd-length-subarrays | 44ms, Python (with comments and detailed walkthrough) | Akshar-code | 31 | 1,500 | sum of all odd length subarrays | 1,588 | 0.835 | Easy | 23,327 |
https://leetcode.com/problems/sum-of-all-odd-length-subarrays/discuss/2117346/Python3-easy-solution | class Solution:
def sumOddLengthSubarrays(self, arr: List[int]) -> int:
subs = []
for n in range(1,len(arr)+1,2):
for i in range(len(arr)+1-n):
subs.append(arr[i:i+n])
return(sum(sum(s) for s in subs)) | sum-of-all-odd-length-subarrays | Python3 easy solution | yashkumarjha | 4 | 246 | sum of all odd length subarrays | 1,588 | 0.835 | Easy | 23,328 |
https://leetcode.com/problems/sum-of-all-odd-length-subarrays/discuss/1982810/Python-Easy-math-Solution-or-O(n)-Beats-95-submits | class Solution:
def sumOddLengthSubarrays(self, arr: List[int]) -> int:
length = len(arr)
ans = 0
for i in range(length) :
ans += ((i+1)*(length-i)+1)//2 * arr[i]
return ans; | sum-of-all-odd-length-subarrays | [ Python ] Easy math Solution | O(n) Beats 95% submits | crazypuppy | 4 | 301 | sum of all odd length subarrays | 1,588 | 0.835 | Easy | 23,329 |
https://leetcode.com/problems/sum-of-all-odd-length-subarrays/discuss/2063747/Python-TC%3A-O(n)-and-SC%3A-O(1) | class Solution:
def sumOddLengthSubarrays(self, arr: List[int]) -> int:
# We check how many times each element in arr occurs in odd length subarray and finally multiply the value with its freq and add it to our ans
# TC: O(n) and SC: O(n)
# Total no. of subarrays including arr[i] is = No. of... | sum-of-all-odd-length-subarrays | Python TC: O(n) and SC: O(1) | dsalgobros | 3 | 204 | sum of all odd length subarrays | 1,588 | 0.835 | Easy | 23,330 |
https://leetcode.com/problems/sum-of-all-odd-length-subarrays/discuss/1473127/Two-loops-94-speed | class Solution:
def sumOddLengthSubarrays(self, arr: List[int]) -> int:
len_arr, ans = len(arr), 0
for start in range(len_arr):
sub_sum = 0
for i in range(start, len_arr):
sub_sum += arr[i]
if not (i - start) % 2:
ans += sub... | sum-of-all-odd-length-subarrays | Two loops, 94% speed | EvgenySH | 3 | 550 | sum of all odd length subarrays | 1,588 | 0.835 | Easy | 23,331 |
https://leetcode.com/problems/sum-of-all-odd-length-subarrays/discuss/2501878/Python-99-faster-with-1-Line-or-O(n)-or-Explained | class Solution:
def sumOddLengthSubarrays(self, arr: List[int]) -> int:
result = 0
length = len(arr)
for idx, val in enumerate(arr):
"""
Start = how many sub arrays will be start with the value
End = how many sub arrays will end with the value
Total = how many times the value will be there in all su... | sum-of-all-odd-length-subarrays | ✅ Python 99% faster with 1 Line | O(n) | Explained | sezanhaque | 2 | 159 | sum of all odd length subarrays | 1,588 | 0.835 | Easy | 23,332 |
https://leetcode.com/problems/sum-of-all-odd-length-subarrays/discuss/2312922/Python-91.2-fasters-or-Python-Simplest-Solution-With-Explanation-or-Beg-to-adv-or-Prefix-sum | class Solution:
def sumOddLengthSubarrays(self, arr: List[int]) -> int:
if len(arr) < 3: # if we are having only 2 elements then we cant perform anything with them, rather just provide there sum.
return sum(arr)
prefix_sum = [0] * (len(arr) + 1) # this will create a list of multi... | sum-of-all-odd-length-subarrays | Python 91.2% fasters | Python Simplest Solution With Explanation | Beg to adv | Prefix sum | rlakshay14 | 2 | 307 | sum of all odd length subarrays | 1,588 | 0.835 | Easy | 23,333 |
https://leetcode.com/problems/sum-of-all-odd-length-subarrays/discuss/2057978/Python-List-Comprehension | class Solution:
def sumOddLengthSubarrays(self, arr: List[int]) -> int:
return sum(
sum(arr[i:i + odd_arr])
for odd_arr in range(1, len(arr) + 1, 2)
for i, n in enumerate(arr)
if i + odd_arr <= len(arr)
) | sum-of-all-odd-length-subarrays | Python List Comprehension | kedeman | 2 | 84 | sum of all odd length subarrays | 1,588 | 0.835 | Easy | 23,334 |
https://leetcode.com/problems/sum-of-all-odd-length-subarrays/discuss/2020988/Python-One-Liner!-Clean-and-Simple! | class Solution:
def sumOddLengthSubarrays(self, arr):
subs = [arr[i:j] for i in range(len(arr)) for j in range(i+1, len(arr)+1)]
return sum(sum(sub) for sub in subs if len(sub)%2) | sum-of-all-odd-length-subarrays | Python - One-Liner! Clean and Simple! | domthedeveloper | 2 | 278 | sum of all odd length subarrays | 1,588 | 0.835 | Easy | 23,335 |
https://leetcode.com/problems/sum-of-all-odd-length-subarrays/discuss/2020988/Python-One-Liner!-Clean-and-Simple! | class Solution:
def sumOddLengthSubarrays(self, arr):
ans = 0
for i in range(len(arr)):
for j in range(i+1,len(arr)+1):
if (j-i)%2: ans += sum(arr[i:j])
return ans | sum-of-all-odd-length-subarrays | Python - One-Liner! Clean and Simple! | domthedeveloper | 2 | 278 | sum of all odd length subarrays | 1,588 | 0.835 | Easy | 23,336 |
https://leetcode.com/problems/sum-of-all-odd-length-subarrays/discuss/2020988/Python-One-Liner!-Clean-and-Simple! | class Solution:
def sumOddLengthSubarrays(self, arr):
return sum(sum(arr[i:j]) for i in range(len(arr)) for j in range(i+1, len(arr)+1) if (j-i)%2) | sum-of-all-odd-length-subarrays | Python - One-Liner! Clean and Simple! | domthedeveloper | 2 | 278 | sum of all odd length subarrays | 1,588 | 0.835 | Easy | 23,337 |
https://leetcode.com/problems/sum-of-all-odd-length-subarrays/discuss/1609646/Python-3-oror-Prefix-Sum | class Solution:
def sumOddLengthSubarrays(self, arr: List[int]) -> int:
prefix_sum = [0] * len(arr)
prefix_sum[0] = arr[0]
for i in range(1,len(arr)):
prefix_sum[i] = prefix_sum[i-1]+arr[i]
#construct odd length sub arrays:
ans = 0
for i in range(... | sum-of-all-odd-length-subarrays | Python 3 || Prefix Sum | s_m_d_29 | 2 | 383 | sum of all odd length subarrays | 1,588 | 0.835 | Easy | 23,338 |
https://leetcode.com/problems/sum-of-all-odd-length-subarrays/discuss/1078897/easy-and-simple-using-python | class Solution:
def sumOddLengthSubarrays(self, arr: List[int]) -> int:
res,n=[],0
for i in range(1,len(arr)+1,2):
if i==1:
res.append(sum(arr))
else:
while n+i<=len(arr):
res.append(sum(arr[n:n+i]))
n +=... | sum-of-all-odd-length-subarrays | easy and simple using python | yashwanthreddz | 2 | 187 | sum of all odd length subarrays | 1,588 | 0.835 | Easy | 23,339 |
https://leetcode.com/problems/sum-of-all-odd-length-subarrays/discuss/858491/Python3-an-alternative-O(N)-approach | class Solution:
def sumOddLengthSubarrays(self, arr: List[int]) -> int:
ans = 0
for i, x in enumerate(arr):
k = (i//2+1)*((len(arr)-i+1)//2) # odd-odd
k += (i+1)//2*((len(arr)-i)//2) # even-even
ans += k*x
return ans | sum-of-all-odd-length-subarrays | [Python3] an alternative O(N) approach | ye15 | 2 | 144 | sum of all odd length subarrays | 1,588 | 0.835 | Easy | 23,340 |
https://leetcode.com/problems/sum-of-all-odd-length-subarrays/discuss/858491/Python3-an-alternative-O(N)-approach | class Solution:
def sumOddLengthSubarrays(self, arr: List[int]) -> int:
return sum(((i+1)*(len(arr)-i) + 1)//2 * x for i, x in enumerate(arr)) | sum-of-all-odd-length-subarrays | [Python3] an alternative O(N) approach | ye15 | 2 | 144 | sum of all odd length subarrays | 1,588 | 0.835 | Easy | 23,341 |
https://leetcode.com/problems/sum-of-all-odd-length-subarrays/discuss/2352237/Sum-of-All-Odd-Length-Subarrays-Python-or-Easy-code-or-O(n2)-time-complexity | class Solution:
def sumOddLengthSubarrays(self, arr: List[int]) -> int:
result = sum(arr) #as each subarray results in 1 element
length = len(arr)+1
for l in range(3,length , 2): #odd length
for i in range(length-l):
result += sum(arr[i:l+i]) #sum of subaarays of... | sum-of-all-odd-length-subarrays | Sum of All Odd Length Subarrays- Python | Easy code | O(n^2)-time complexity | phanee16 | 1 | 130 | sum of all odd length subarrays | 1,588 | 0.835 | Easy | 23,342 |
https://leetcode.com/problems/sum-of-all-odd-length-subarrays/discuss/2134686/For-Loop-Solution | class Solution:
def sumOddLengthSubarrays(self, arr: List[int]) -> int:
ls = []
s = 0
for i in range(len(arr)):
for j in range(i,len(arr)):
if (j-i+1)%2!=0:
ls.append(arr[i:j+1])
for i in range(len(ls)):
for j in range(len(l... | sum-of-all-odd-length-subarrays | For Loop Solution | jayeshvarma | 1 | 90 | sum of all odd length subarrays | 1,588 | 0.835 | Easy | 23,343 |
https://leetcode.com/problems/sum-of-all-odd-length-subarrays/discuss/2129998/Memory-Usage%3A-13.9-MB-less-than-63.39-of-Python3 | class Solution:
def sumOddLengthSubarrays(self, arr: List[int]) -> int:
s = 0
for i in range(len(arr)):
for j in range(i, len(arr), 2):
s += sum(arr[i:j+1])
return s | sum-of-all-odd-length-subarrays | Memory Usage: 13.9 MB, less than 63.39% of Python3 | writemeom | 1 | 92 | sum of all odd length subarrays | 1,588 | 0.835 | Easy | 23,344 |
https://leetcode.com/problems/sum-of-all-odd-length-subarrays/discuss/2088524/Simple-Python-Solution | class Solution:
def sumOddLengthSubarrays(self, arr: List[int]) -> int:
subs = []
for n in range(1, len(arr)+1, 2):
for i in range(len(arr)+1-n):
subs.append(arr[i:i+n])
return sum(sum(s) for s in subs) | sum-of-all-odd-length-subarrays | Simple Python Solution | pe-mn | 1 | 48 | sum of all odd length subarrays | 1,588 | 0.835 | Easy | 23,345 |
https://leetcode.com/problems/sum-of-all-odd-length-subarrays/discuss/1533252/Python3-Prefix-Sum-and-List-slicing-solution | class Solution:
def sumOddLengthSubarrays(self, arr: List[int]) -> int:
res = 0
for i in range(len(arr)):
j = i
while j < len(arr):
res += sum(arr[i:j+1])
j += 2
return res | sum-of-all-odd-length-subarrays | [Python3] Prefix Sum and List slicing solution | deleted_user | 1 | 117 | sum of all odd length subarrays | 1,588 | 0.835 | Easy | 23,346 |
https://leetcode.com/problems/sum-of-all-odd-length-subarrays/discuss/1016805/Python-Prefix-Sum-Technique-(Much-Faster-than-brute-force) | class Solution:
def sumOddLengthSubarrays(self, arr: List[int]) -> int:
result = 0
prefix = [0] + list(itertools.accumulate(arr))
for i in range(1, len(prefix)):
for j in range(i):
if (i+j) % 2 == 0: continue
result += prefi... | sum-of-all-odd-length-subarrays | Python, Prefix Sum Technique (Much Faster than brute force) | dev-josh | 1 | 139 | sum of all odd length subarrays | 1,588 | 0.835 | Easy | 23,347 |
https://leetcode.com/problems/sum-of-all-odd-length-subarrays/discuss/2846579/For-beginner-and-easy-approach | class Solution:
def sumOddLengthSubarrays(self, arr: List[int]) -> int:
sum1 = 0
n=len(arr)
for i in range (n):
s=0
for j in range(i,n):
s += arr[j]
if (j-i)%2==0: # checking even because indexes starts from 0 as its even
... | sum-of-all-odd-length-subarrays | For beginner and easy approach | Soumodip | 0 | 2 | sum of all odd length subarrays | 1,588 | 0.835 | Easy | 23,348 |
https://leetcode.com/problems/sum-of-all-odd-length-subarrays/discuss/2824147/Python-simple-solution | class Solution:
def sumOddLengthSubarrays(self, arr: List[int]) -> int:
total=0
l=len(arr)
for i in range(l):
total += (((i+1)*(l-i) +1)//2)*arr[i]
return total | sum-of-all-odd-length-subarrays | Python simple solution | Just_03 | 0 | 3 | sum of all odd length subarrays | 1,588 | 0.835 | Easy | 23,349 |
https://leetcode.com/problems/sum-of-all-odd-length-subarrays/discuss/2803049/Simple-7-lines-brute-force-solution. | class Solution:
def sumOddLengthSubarrays(self, arr: List[int]) -> int:
"""
Solution #1. Brute force.
Iterate over each element in array.
Iterate over all possible sizes of the odd arrays.
Sum up current element with sums of all possible odd-lenght sub arrays.
Runtim... | sum-of-all-odd-length-subarrays | Simple 7-lines brute-force solution. | denisOgr | 0 | 4 | sum of all odd length subarrays | 1,588 | 0.835 | Easy | 23,350 |
https://leetcode.com/problems/sum-of-all-odd-length-subarrays/discuss/2800899/Sum-of-All-Odd-Length-Subarrays-or-Python | class Solution:
def sumOddLengthSubarrays(self, arr: List[int]) -> int:
total, n = 0, len(arr)
for i in range(n):
times = ((i+1)*(n-i)+1)//2
total += times * arr[i]
return total | sum-of-all-odd-length-subarrays | Sum of All Odd Length Subarrays | Python | jashii96 | 0 | 4 | sum of all odd length subarrays | 1,588 | 0.835 | Easy | 23,351 |
https://leetcode.com/problems/sum-of-all-odd-length-subarrays/discuss/2793317/Easy-python-please-help-with-time-complexity | class Solution:
def sumOddLengthSubarrays(self, arr: List[int]) -> int:
_sum = sum(arr)
i=3
while i<=len(arr):
for j in range((len(arr)-i)+1):
_chunk = arr[j:j+i]
_sum += sum(_chunk)
i+=2
return _sum | sum-of-all-odd-length-subarrays | Easy python - please help with time complexity? | ATHBuys | 0 | 3 | sum of all odd length subarrays | 1,588 | 0.835 | Easy | 23,352 |
https://leetcode.com/problems/sum-of-all-odd-length-subarrays/discuss/2787826/Sliding-Window-beginner-friendly-easy-to-DEBUG | class Solution:
def sumOddLengthSubarrays(self, arr: List[int]) -> int:
k = 1
total = 0
while k <= len(arr):
for i in range(len(arr)-k+1):
total += sum(arr[i:i+k])
k += 2
return total | sum-of-all-odd-length-subarrays | Sliding Window, beginner friendly, easy to DEBUG | karanvirsagar98 | 0 | 4 | sum of all odd length subarrays | 1,588 | 0.835 | Easy | 23,353 |
https://leetcode.com/problems/sum-of-all-odd-length-subarrays/discuss/2773963/Python-or-O(n)-Maths-Solution-(57-ms) | class Solution:
def sumOddLengthSubarrays(self, arr: List[int]) -> int:
n = len(arr)
if n == 1:
return arr[0]
m = n // 2
ans = 0
if n & 1:
# That is, n is odd.
k = m + 1
j = k
if n % 4 == 1:
# n... | sum-of-all-odd-length-subarrays | Python | O(n) Maths Solution (57 ms) | on_danse_encore_on_rit_encore | 0 | 3 | sum of all odd length subarrays | 1,588 | 0.835 | Easy | 23,354 |
https://leetcode.com/problems/sum-of-all-odd-length-subarrays/discuss/2713473/Python-easy-solution | class Solution:
def sumOddLengthSubarrays(self, arr: List[int]) -> int:
ans = 0 # sum of all odd length subarrays
j = 1
for i in range(len(arr)):
s = 0 # sum of all odd length subarrays containing/starting with current element arr[i]
# print('loop no.',i) -> breakpoin... | sum-of-all-odd-length-subarrays | Python easy solution | anandanshul001 | 0 | 9 | sum of all odd length subarrays | 1,588 | 0.835 | Easy | 23,355 |
https://leetcode.com/problems/sum-of-all-odd-length-subarrays/discuss/2697777/Python-3-solution | class Solution:
def sumOddLengthSubarrays(self, arr: List[int]) -> int:
len_arr = len(arr)
check_for = [i for i in range(len_arr+1) if i&1 and i>1]
res = sum(arr) # case of 1
for r in check_for:
i = 0
while i + r <= len_arr:
res += sum(arr... | sum-of-all-odd-length-subarrays | Python 3 solution | sipi09 | 0 | 6 | sum of all odd length subarrays | 1,588 | 0.835 | Easy | 23,356 |
https://leetcode.com/problems/sum-of-all-odd-length-subarrays/discuss/2686444/PYTHON3 | class Solution:
def sumOddLengthSubarrays(self, arr: List[int]) -> int:
Sum=0
for i in range(1,len(arr)+1,2):
for j in range(len(arr)-i+1):
Sum += sum(arr[j:j+i])
return Sum | sum-of-all-odd-length-subarrays | PYTHON3 | Gurugubelli_Anil | 0 | 2 | sum of all odd length subarrays | 1,588 | 0.835 | Easy | 23,357 |
https://leetcode.com/problems/sum-of-all-odd-length-subarrays/discuss/2685336/Python-easy-solution.-No-fancy-stuff-but-it-does-the-job-quite-well | class Solution:
def sumOddLengthSubarrays(self, arr: List[int]) -> int:
a=0
for i in range(len(arr)):
for j in range(i,len(arr),2):
a+=sum(arr[i:j+1])
return a | sum-of-all-odd-length-subarrays | Python easy solution. No fancy stuff but it does the job quite well | Navaneeth7 | 0 | 5 | sum of all odd length subarrays | 1,588 | 0.835 | Easy | 23,358 |
https://leetcode.com/problems/sum-of-all-odd-length-subarrays/discuss/2623551/Python3-or-Solved-Using-Prefix-Sum-and-Considering-Increasing-Length-of-Odd-Subarrays | class Solution:
#Let n = len(arr)!
#Time-Complexity: O(n + n + n + ((n - 3 // 2) + 1)) -> O(n)
#Space-Complexity: O(n), due to prefix sum array!
def sumOddLengthSubarrays(self, arr: List[int]) -> int:
#Approach: Consider subarrays of increasing odd sizes: 1, 3, 5, etc.
#However, for each... | sum-of-all-odd-length-subarrays | Python3 | Solved Using Prefix Sum and Considering Increasing Length of Odd-Subarrays | JOON1234 | 0 | 25 | sum of all odd length subarrays | 1,588 | 0.835 | Easy | 23,359 |
https://leetcode.com/problems/sum-of-all-odd-length-subarrays/discuss/2616974/Faster-and-clean-PYTHON-solution | class Solution:
def sumOddLengthSubarrays(self, arr: List[int]) -> int:
pref_arr = [0]*len(arr)
pref_arr[0]=arr[0]
for i in range(1,len(arr)):
pref_arr[i]=pref_arr[i-1]+arr[i]
sum=0
for i in range(1,len(arr)+1,2):
j=0
while i+j<=len(arr):
if j==0:
... | sum-of-all-odd-length-subarrays | Faster and clean PYTHON solution | Abdulahad_Abduqahhorov | 0 | 45 | sum of all odd length subarrays | 1,588 | 0.835 | Easy | 23,360 |
https://leetcode.com/problems/sum-of-all-odd-length-subarrays/discuss/2589892/Python3-or-Easy-to-Understand-or-Hashing | class Solution:
def sumOddLengthSubarrays(self, ar: List[int]) -> int:
n = len(ar)
max_len = n if n%2 else n-1
dict_ = {}
dict_[(ar[0],0)] = ar[0]
for idx in range(1,n):
dict_[(ar[idx],idx)] = dict_.get((ar[idx-1],idx-1)) + ar[idx]
sum = 0
for odd... | sum-of-all-odd-length-subarrays | Python3 | Easy to Understand | Hashing | anurag899 | 0 | 65 | sum of all odd length subarrays | 1,588 | 0.835 | Easy | 23,361 |
https://leetcode.com/problems/sum-of-all-odd-length-subarrays/discuss/2577121/simple-python-solution.-easy-understand | class Solution:
def sumOddLengthSubarrays(self, arr: List[int]) -> int:
result = 0
array_len = len(arr)
for i in arr:
result += i
for x in range(array_len):
for y in range(x+1, array_len):
diff = y - x
... | sum-of-all-odd-length-subarrays | simple python solution. easy understand | maschwartz5006 | 0 | 53 | sum of all odd length subarrays | 1,588 | 0.835 | Easy | 23,362 |
https://leetcode.com/problems/sum-of-all-odd-length-subarrays/discuss/2448566/Python3-Solution | class Solution:
def sumOddLengthSubarrays(self, arr: List[int]) -> int:
list1=[]
for i in range(len(arr)+1):
for j in range(i):
if (len(arr[j:i])%2)!=0:
list1.append(arr[j:i])
return sum(map(sum,list1)) | sum-of-all-odd-length-subarrays | Python3 Solution | Coder0212 | 0 | 48 | sum of all odd length subarrays | 1,588 | 0.835 | Easy | 23,363 |
https://leetcode.com/problems/sum-of-all-odd-length-subarrays/discuss/2317660/Sum-of-all-odd-length-subarrays | ```class Solution:
def sumOddLengthSubarrays(self, arr: List[int]) -> int:
sume = sum(arr)
k = 2
i = 0
j = 2
while True:
if j == len(arr):
i = 0
j = k + 2
k = k + 2
if j >= len(arr):
break... | sum-of-all-odd-length-subarrays | Sum of all odd length subarrays | Jonny69 | 0 | 137 | sum of all odd length subarrays | 1,588 | 0.835 | Easy | 23,364 |
https://leetcode.com/problems/sum-of-all-odd-length-subarrays/discuss/2289326/38ms-easy-fast-simplest-logic | class Solution:
def sumOddLengthSubarrays(self, arr: List[int]) -> int:
output = sum(arr)
length = len(arr)+1
for l in range(3,length , 2):
for i in range(length-l):
output += sum(arr[i:l+i])
return output | sum-of-all-odd-length-subarrays | 38ms easy fast simplest logic | ashish_p34 | 0 | 81 | sum of all odd length subarrays | 1,588 | 0.835 | Easy | 23,365 |
https://leetcode.com/problems/sum-of-all-odd-length-subarrays/discuss/2185740/Python-Simple-Python-Solution-By-Generating-All-Subarray | class Solution:
def sumOddLengthSubarrays(self, arr: List[int]) -> int:
result = 0
for i in range(len(arr)):
for j in range(i,len(arr)):
if (j+1-i) % 2 != 0:
result = result + sum(arr[i:j+1])
return result | sum-of-all-odd-length-subarrays | [ Python ] ✅✅ Simple Python Solution By Generating All Subarray 🥳✌👍 | ASHOK_KUMAR_MEGHVANSHI | 0 | 148 | sum of all odd length subarrays | 1,588 | 0.835 | Easy | 23,366 |
https://leetcode.com/problems/sum-of-all-odd-length-subarrays/discuss/2159987/Piece-Of-Cake-Python | class Solution:
def sumOddLengthSubarrays(self, arr: List[int]) -> int:
size = len(arr)
max_step = size if size%2 else size-1
step = 1
ans = list()
while step<=max_step:
for i in range(size):
if i+step>=size+1:
continue
... | sum-of-all-odd-length-subarrays | Piece Of Cake 🍰 [Python] | 2bumaher | 0 | 137 | sum of all odd length subarrays | 1,588 | 0.835 | Easy | 23,367 |
https://leetcode.com/problems/sum-of-all-odd-length-subarrays/discuss/2063482/Python-easy-to-read-and-understand | class Solution:
def sumOddLengthSubarrays(self, arr: List[int]) -> int:
ans = 0
n = len(arr)
for i in range(n):
starts, ends = n-i, i+1
temp = starts*ends
if temp%2 == 0:
ans += temp//2*arr[i]
else:
ans += (temp/... | sum-of-all-odd-length-subarrays | Python easy to read and understand | sanial2001 | 0 | 148 | sum of all odd length subarrays | 1,588 | 0.835 | Easy | 23,368 |
https://leetcode.com/problems/sum-of-all-odd-length-subarrays/discuss/2019895/Python3-DP-O(N) | class Solution:
def sumOddLengthSubarrays(self, arr: List[int]) -> int:
if len(arr) == 0:
return 0
if len(arr) == 1:
return 1
endCnt = [1, 1]
for i in range(2, len(arr)):
endCnt.append(endCnt[i-2]+1)
preprocess = [arr[0], arr[1]]
... | sum-of-all-odd-length-subarrays | Python3 DP O(N) | Eminemozart | 0 | 63 | sum of all odd length subarrays | 1,588 | 0.835 | Easy | 23,369 |
https://leetcode.com/problems/sum-of-all-odd-length-subarrays/discuss/1884998/Python-Solution | class Solution:
def sumOddLengthSubarrays(self, arr: List[int]) -> int:
result = 0
arr_len = len(arr)
for i in range(0, arr_len, 2):
for j in range(arr_len - i):
result += sum(arr[j:i + j + 1])
return result | sum-of-all-odd-length-subarrays | Python Solution | hgalytoby | 0 | 194 | sum of all odd length subarrays | 1,588 | 0.835 | Easy | 23,370 |
https://leetcode.com/problems/sum-of-all-odd-length-subarrays/discuss/1878890/Python-easy-solution | class Solution:
def sumOddLengthSubarrays(self, arr):
summe = 0
length = len(arr)
for i in range(length+1):
if i%2 != 0:
for ind in range(length):
if len(arr[ind:(i+ind)]) == i:
summe += sum(arr[ind:(i+ind)])
ret... | sum-of-all-odd-length-subarrays | Python easy solution | poliliu | 0 | 144 | sum of all odd length subarrays | 1,588 | 0.835 | Easy | 23,371 |
https://leetcode.com/problems/sum-of-all-odd-length-subarrays/discuss/1878584/Python-(Simple-Approach-and-Beginner-Friendly) | class Solution:
def sumOddLengthSubarrays(self, arr: List[int]) -> int:
output = []
count = 0
for i in range(0, len(arr)+1):
for j in range(0, len(arr)+1):
if arr[i:j] != []:
output.append(arr[i:j])
for i in output:
if len(i... | sum-of-all-odd-length-subarrays | Python (Simple Approach and Beginner-Friendly) | vishvavariya | 0 | 140 | sum of all odd length subarrays | 1,588 | 0.835 | Easy | 23,372 |
https://leetcode.com/problems/sum-of-all-odd-length-subarrays/discuss/1869667/Python-3-solution | class Solution:
def sumOddLengthSubarrays(self, arr: List[int]) -> int:
final_sum = 0
for i in range(len(arr)):
for j in range(len(arr) + 1):
sliced = arr[:j]
if len(sliced) % 2 == 1:
final_sum += sum(sliced)
if j == len(arr):
arr.pop(0)
return final_sum | sum-of-all-odd-length-subarrays | Python 3 solution | zuzannakilar | 0 | 105 | sum of all odd length subarrays | 1,588 | 0.835 | Easy | 23,373 |
https://leetcode.com/problems/sum-of-all-odd-length-subarrays/discuss/1828013/4-Lines-Python-Solution-oror-75-Faster-oror-Memory-less-than-80 | class Solution:
def sumOddLengthSubarrays(self, arr: List[int]) -> int:
ans=0
for i in range(len(arr)):
for j in range(i,len(arr),2): ans+=sum(arr[i:j+1])
return ans | sum-of-all-odd-length-subarrays | 4-Lines Python Solution || 75% Faster || Memory less than 80% | Taha-C | 0 | 107 | sum of all odd length subarrays | 1,588 | 0.835 | Easy | 23,374 |
https://leetcode.com/problems/sum-of-all-odd-length-subarrays/discuss/1779472/Python3-Solution-or-99.04-lesser-memory | class Solution:
def sumOddLengthSubarrays(self, arr: List[int]) -> int:
total = 0
for i in range(len(arr)):
for j in range(i+1, len(arr)+1):
if len(arr[i:j])%2 == 1:
total += sum(arr[i:j])
return total | sum-of-all-odd-length-subarrays | ✔Python3 Solution | 99.04% lesser memory | Coding_Tan3 | 0 | 153 | sum of all odd length subarrays | 1,588 | 0.835 | Easy | 23,375 |
https://leetcode.com/problems/sum-of-all-odd-length-subarrays/discuss/1558145/WEEB-DOES-PYTHON | class Solution:
def sumOddLengthSubarrays(self, arr: List[int]) -> int:
queue = deque([])
for i in range(len(arr)):
queue.append((i, arr[i]))
return self.bfs(queue, arr)
def bfs(self, queue, nums):
total = 0
while queue:
idx, curSum = queue.popleft()
total += curSum
if idx+2 < len(nums):
... | sum-of-all-odd-length-subarrays | WEEB DOES PYTHON | Skywalker5423 | 0 | 231 | sum of all odd length subarrays | 1,588 | 0.835 | Easy | 23,376 |
https://leetcode.com/problems/maximum-sum-obtained-of-any-permutation/discuss/858448/Python3-mark-and-sweep | class Solution:
def maxSumRangeQuery(self, nums: List[int], requests: List[List[int]]) -> int:
chg = [0]*len(nums) # change
for i, j in requests:
chg[i] += 1
if j+1 < len(nums): chg[j+1] -= 1
for i in range(1, len(nums)): chg[i] += chg[i-1] # cumulated change
... | maximum-sum-obtained-of-any-permutation | [Python3] mark & sweep | ye15 | 0 | 39 | maximum sum obtained of any permutation | 1,589 | 0.37 | Medium | 23,377 |
https://leetcode.com/problems/make-sum-divisible-by-p/discuss/1760163/WEEB-DOES-PYTHONC%2B%2B | class Solution:
def minSubarray(self, nums: List[int], p: int) -> int:
dp = defaultdict(int)
dp[0] = -1
target = sum(nums) % p
curSum = 0
result = len(nums)
if sum(nums) % p == 0: return 0
for i in range(len(nums)):
curSum += nums[i]
curMod = curSum % p
... | make-sum-divisible-by-p | WEEB DOES PYTHON/C++ | Skywalker5423 | 2 | 153 | make sum divisible by p | 1,590 | 0.28 | Medium | 23,378 |
https://leetcode.com/problems/make-sum-divisible-by-p/discuss/1444032/python-O(n)-time-O(1)-space | class Solution:
def minSubarray(self, nums: List[int], p: int) -> int:
s = sum(nums)
if s % p == 0: return 0
moddict = {}
minv = float('inf')
t = 0
moddict[0] = 0
cnt = 1
for num in nums:
t = (t + num) % p
if (t-s) % p... | make-sum-divisible-by-p | python O(n) time, O(1) space | byuns9334 | 0 | 244 | make sum divisible by p | 1,590 | 0.28 | Medium | 23,379 |
https://leetcode.com/problems/make-sum-divisible-by-p/discuss/858516/Python3-two-approaches | class Solution:
def minSubarray(self, nums: List[int], p: int) -> int:
suffix = [0]
for x in reversed(nums): suffix.append((suffix[-1] + x)%p)
suffix.reverse()
if suffix[0] % p == 0: return 0 # edge case
ans = inf
prefix = 0
seen = {0: -1}
... | make-sum-divisible-by-p | [Python3] two approaches | ye15 | 0 | 117 | make sum divisible by p | 1,590 | 0.28 | Medium | 23,380 |
https://leetcode.com/problems/make-sum-divisible-by-p/discuss/858516/Python3-two-approaches | class Solution:
def minSubarray(self, nums: List[int], p: int) -> int:
target = sum(nums) % p # targetted remainder
ans = inf
seen = {(prefix := 0): -1}
for i, x in enumerate(nums):
seen[(prefix := (prefix+x)%p)] = i # update seen before check
if (prefix-ta... | make-sum-divisible-by-p | [Python3] two approaches | ye15 | 0 | 117 | make sum divisible by p | 1,590 | 0.28 | Medium | 23,381 |
https://leetcode.com/problems/strange-printer-ii/discuss/911370/Same-as-CourseSchedule-Topological-sort. | class Solution:
def isPrintable(self, targetGrid: List[List[int]]) -> bool:
visited = [0] * 61
graph = collections.defaultdict(set)
m, n = len(targetGrid), len(targetGrid[0])
for c in range(1, 61):
l,r,t,b = n,-1,m,-1
#to specify the covered range of color c
... | strange-printer-ii | Same as CourseSchedule, Topological sort. | Sakata_Gintoki | 17 | 810 | strange printer ii | 1,591 | 0.584 | Hard | 23,382 |
https://leetcode.com/problems/strange-printer-ii/discuss/1516602/toposort-concept-or-python-code | class Solution(object):
def isPrintable(self, targetGrid):
"""
:type targetGrid: List[List[int]]
:rtype: bool
"""
#doing reverse engineering like toposort concept find that reactangle which is completely independent,and remove and process futher
m,n=len(targetGrid),len(target... | strange-printer-ii | toposort concept | python code | 13as1827000713 | 2 | 163 | strange printer ii | 1,591 | 0.584 | Hard | 23,383 |
https://leetcode.com/problems/rearrange-spaces-between-words/discuss/1834393/Python-simple-and-elegant | class Solution(object):
def reorderSpaces(self, text):
word_list = text.split()
words, spaces = len(word_list), text.count(" ")
if words > 1:
q, r = spaces//(words-1), spaces%(words-1)
return (" " * q).join(word_list) + " " * r
else:
retur... | rearrange-spaces-between-words | Python - simple and elegant | domthedeveloper | 3 | 176 | rearrange spaces between words | 1,592 | 0.437 | Easy | 23,384 |
https://leetcode.com/problems/rearrange-spaces-between-words/discuss/1834393/Python-simple-and-elegant | class Solution(object):
def reorderSpaces(self, text):
word_list = text.split()
words, spaces = len(word_list), text.count(" ")
if words > 1:
q, r = spaces//(words-1), spaces%(words-1)
else:
q, r = 0, spaces
return (" " * q).join(word... | rearrange-spaces-between-words | Python - simple and elegant | domthedeveloper | 3 | 176 | rearrange spaces between words | 1,592 | 0.437 | Easy | 23,385 |
https://leetcode.com/problems/rearrange-spaces-between-words/discuss/1834393/Python-simple-and-elegant | class Solution(object):
def reorderSpaces(self, text):
word_list = text.split()
words, spaces = len(word_list), text.count(" ")
if words > 1:
q, r = divmod(spaces, (words - 1))
else:
q, r = 0, spaces
return (" " * q).join(word_list) +... | rearrange-spaces-between-words | Python - simple and elegant | domthedeveloper | 3 | 176 | rearrange spaces between words | 1,592 | 0.437 | Easy | 23,386 |
https://leetcode.com/problems/rearrange-spaces-between-words/discuss/1834393/Python-simple-and-elegant | class Solution(object):
def reorderSpaces(self, text):
w, s = len(text.split()), text.count(" ")
q, r = (divmod(s, (w - 1))) if w > 1 else (0, s)
return (" " * q).join(text.split()) + " " * r | rearrange-spaces-between-words | Python - simple and elegant | domthedeveloper | 3 | 176 | rearrange spaces between words | 1,592 | 0.437 | Easy | 23,387 |
https://leetcode.com/problems/rearrange-spaces-between-words/discuss/1834393/Python-simple-and-elegant | class Solution(object):
def reorderSpaces(self, text):
return "".join(text.split()) + " " * text.count(" ") if len(text.split()) <= 1 else (" " * (text.count(" ")//(len(text.split())-1))).join(text.split()) + " " * (text.count(" ") % (len(text.split())-1)) | rearrange-spaces-between-words | Python - simple and elegant | domthedeveloper | 3 | 176 | rearrange spaces between words | 1,592 | 0.437 | Easy | 23,388 |
https://leetcode.com/problems/rearrange-spaces-between-words/discuss/858790/Python3-Intuitive-Simple | class Solution:
def reorderSpaces(self, text: str) -> str:
space = 0
# Count number of spaces
for char in list(text):
if char ==' ':
space += 1
words = text.split()
num_words = len(words)
# Count number of spaces after each... | rearrange-spaces-between-words | [Python3] Intuitive Simple | nachiketsd | 3 | 121 | rearrange spaces between words | 1,592 | 0.437 | Easy | 23,389 |
https://leetcode.com/problems/rearrange-spaces-between-words/discuss/858193/Python3-concise-4-line | class Solution:
def reorderSpaces(self, text: str) -> str:
ns = text.count(" ") # count of spaces
nw = len(text := text.split()) # count of words
if nw > 1: nw, ns = divmod(ns, nw-1) # nw - between word spaces / ns - trailing spaces
return (" "*nw).join(text) + " "*ns | rearrange-spaces-between-words | [Python3] concise 4-line | ye15 | 2 | 133 | rearrange spaces between words | 1,592 | 0.437 | Easy | 23,390 |
https://leetcode.com/problems/rearrange-spaces-between-words/discuss/2761138/Simple-Python-Solution | class Solution:
def reorderSpaces(self, text: str) -> str:
spaces = 0
for i in text:
if i == " ":
spaces+=1
text1 = text.split(" ")
temp = []
words = 0
for i in text1:
if i == "":
continue
else:
... | rearrange-spaces-between-words | Simple Python Solution | aniketbhamani | 1 | 29 | rearrange spaces between words | 1,592 | 0.437 | Easy | 23,391 |
https://leetcode.com/problems/rearrange-spaces-between-words/discuss/2356961/Easy-understandable-Python-code | class Solution:
def reorderSpaces(self, text: str) -> str:
spaces = 0
for i in text:
if i==" ":
spaces+=1
words = text.split()
num_words = len(words)
if num_words==1:
return words[0]+(" "*spaces)
space = spaces//(num_words-1)
... | rearrange-spaces-between-words | Easy understandable Python code | sunakshi132 | 1 | 54 | rearrange spaces between words | 1,592 | 0.437 | Easy | 23,392 |
https://leetcode.com/problems/rearrange-spaces-between-words/discuss/1442638/Count-spaces-and-split-into-words-84-speed | class Solution:
def reorderSpaces(self, text: str) -> str:
spaces = text.count(" ")
words = [w for w in text.strip(" ").split(" ") if w]
len_words = len(words)
if len_words > 1:
equal_spaces, remainder = divmod(spaces, len_words - 1)
separator = " " * equal_sp... | rearrange-spaces-between-words | Count spaces and split into words, 84% speed | EvgenySH | 1 | 174 | rearrange spaces between words | 1,592 | 0.437 | Easy | 23,393 |
https://leetcode.com/problems/rearrange-spaces-between-words/discuss/975520/Python-A-one-liner* | class Solution:
def reorderSpaces(self, text: str) -> str:
words = text.split()
cw = len(words)
cs = text.count(' ')
return f'{" "*(cs // (cw-1))}'.join(words) + " "*(cs % (cw-1)) if cw > 1 else ''.join(words) + " "*cs | rearrange-spaces-between-words | Python - A one-liner* | limtis | 1 | 106 | rearrange spaces between words | 1,592 | 0.437 | Easy | 23,394 |
https://leetcode.com/problems/rearrange-spaces-between-words/discuss/2194620/Python-solution | class Solution:
def reorderSpaces(self, text: str) -> str:
spc = text.count(' ')
if spc == 0: return text
words = [x for x in text.split() if x]
word_count = len(words)
if word_count == 1: return words[0]+ ' ' * spc
sp_betw = spc//(word_count-1)
last_space = s... | rearrange-spaces-between-words | Python solution | StikS32 | 0 | 34 | rearrange spaces between words | 1,592 | 0.437 | Easy | 23,395 |
https://leetcode.com/problems/rearrange-spaces-between-words/discuss/2156171/python-simple-solution | class Solution:
def reorderSpaces(self, text: str) -> str:
# evaluate the number of spaces
num_spaces = sum(i == ' ' for i in text)
# remove spaces and convert string to list of words
text = text.split()
# initialize variable to store - space btw words & extra space to b... | rearrange-spaces-between-words | ✅ python simple solution | Nk0311 | 0 | 35 | rearrange spaces between words | 1,592 | 0.437 | Easy | 23,396 |
https://leetcode.com/problems/rearrange-spaces-between-words/discuss/1789946/4-Lines-Python-Solution-oror-85-Faster-(31ms)-oror-Memory-Less-than-99 | class Solution:
def reorderSpaces(self, text: str) -> str:
words, spaces = text.split(), len([x for x in text if x==' '])
if ' ' not in text: return text
if len(words) == 1: return words[0]+' '*spaces
return (' '*(spaces//(len(words)-1))).join(words) + ' '*(spaces%(len(words)-1)) | rearrange-spaces-between-words | 4-Lines Python Solution || 85% Faster (31ms) || Memory Less than 99% | Taha-C | 0 | 68 | rearrange spaces between words | 1,592 | 0.437 | Easy | 23,397 |
https://leetcode.com/problems/rearrange-spaces-between-words/discuss/1759355/Python-dollarolution-(faster-than-98-and-better-memory-use-than-95) | class Solution:
def reorderSpaces(self, text: str) -> str:
d = {' ': 0}
for i in text:
if i == ' ':
d[i] += 1
text = text.split()
if len(text) == 1:
return text[0]+(' '*d[' '])
l, x = d[' '] // (len(text) - 1), d[' '] % (len(text) - 1)
... | rearrange-spaces-between-words | Python $olution (faster than 98% and better memory use than 95%) | AakRay | 0 | 93 | rearrange spaces between words | 1,592 | 0.437 | Easy | 23,398 |
https://leetcode.com/problems/rearrange-spaces-between-words/discuss/1755512/Python3-or-Faster-than-98.39 | class Solution:
def reorderSpaces(self, text: str) -> str:
count_space = text.count(" ")
words = text.split()
if count_space == 0 or words is None:
return text
words_len = len(words)
if words_len == 1:
return text.lstrip().rstrip() + " " * count_spac... | rearrange-spaces-between-words | Python3 | Faster than 98.39% | khalidhassan3011 | 0 | 45 | rearrange spaces between words | 1,592 | 0.437 | Easy | 23,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.