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/path-with-maximum-probability/discuss/2767986/Dijksrta-maxHeap-Python-Soln | class Solution:
def maxProbability(self, n: int, edges: List[List[int]], succProb: List[float], start: int, end: int) -> float:
#create adjList
adjList = {i:[] for i in range(n)}
for directions, prob in zip(edges, succProb):
adjList[directions[0]].append([directions[1], -1*prob])
adjList[directions[1]].append([directions[0], -1*prob])
#create minHeap
maxHeap = []
maxHeap.append([-1, start])
heapq.heapify(maxHeap)
#dijkstra
finalProb = [0] * n
while maxHeap:
prob, node = heapq.heappop(maxHeap)
for it in adjList[node]:
edgeProb = -1 * it[1]
edgeNode = it[0]
if prob*edgeProb < finalProb[edgeNode]:
finalProb[edgeNode] = prob*edgeProb
heapq.heappush(maxHeap, [prob*edgeProb, edgeNode])
return -1*finalProb[end] | path-with-maximum-probability | Dijksrta maxHeap Python Soln | logeshsrinivasans | 0 | 19 | path with maximum probability | 1,514 | 0.484 | Medium | 22,500 |
https://leetcode.com/problems/path-with-maximum-probability/discuss/1812162/python-Dijkstra's-algorithm-solution | class Solution:
def maxProbability(self, n: int, edges: List[List[int]], succProb: List[float], start: int, end: int) -> float:
edge_prob=[]
for i in range(n):
edge_prob.append([])
for i in range(len(edges)):
edge_prob[edges[i][0]].append([edges[i][1],succProb[i]])
edge_prob[edges[i][1]].append([edges[i][0],succProb[i]])
prob=[0]*n
pq=[]
pq.append((-1,start))
while pq:
current=heapq.heappop(pq)
if prob[current[1]]==0:
prob[current[1]]=-current[0]
for neighbor in edge_prob[current[1]]:
heapq.heappush(pq,(-neighbor[1]*prob[current[1]],neighbor[0]))
if current[1]==end:
break
return prob[end] | path-with-maximum-probability | python Dijkstra's algorithm solution | k3232908 | 0 | 72 | path with maximum probability | 1,514 | 0.484 | Medium | 22,501 |
https://leetcode.com/problems/best-position-for-a-service-centre/discuss/731717/Python3-geometric-median | class Solution:
def getMinDistSum(self, positions: List[List[int]]) -> float:
#euclidean distance
fn = lambda x, y: sum(sqrt((x-xx)**2 + (y-yy)**2) for xx, yy in positions)
#centroid as starting point
x = sum(x for x, _ in positions)/len(positions)
y = sum(y for _, y in positions)/len(positions)
ans = fn(x, y)
chg = 100 #change since 0 <= positions[i][0], positions[i][1] <= 100
while chg > 1e-6: #accuracy within 1e-5
zoom = True
for dx, dy in (-1, 0), (0, -1), (0, 1), (1, 0):
xx = x + chg * dx
yy = y + chg * dy
dd = fn(xx, yy)
if dd < ans:
ans = dd
x, y = xx, yy
zoom = False
break
if zoom: chg /= 2
return ans | best-position-for-a-service-centre | [Python3] geometric median | ye15 | 62 | 3,700 | best position for a service centre | 1,515 | 0.377 | Hard | 22,502 |
https://leetcode.com/problems/water-bottles/discuss/743152/Python3-5-line-iterative | class Solution:
def numWaterBottles(self, numBottles: int, numExchange: int) -> int:
ans = r = 0
while numBottles:
ans += numBottles
numBottles, r = divmod(numBottles + r, numExchange)
return ans | water-bottles | [Python3] 5-line iterative | ye15 | 9 | 573 | water bottles | 1,518 | 0.602 | Easy | 22,503 |
https://leetcode.com/problems/water-bottles/discuss/1173171/Python3-Simple-Solution | class Solution:
def numWaterBottles(self, numBottles: int, numExchange: int) -> int:
drank , left = [numBottles] * 2
while left >= numExchange:
left -= numExchange - 1
drank += 1
return drank | water-bottles | [Python3] Simple Solution | VoidCupboard | 5 | 212 | water bottles | 1,518 | 0.602 | Easy | 22,504 |
https://leetcode.com/problems/water-bottles/discuss/1158383/Python3-Simple-And-99-Faster-Solution | class Solution:
def numWaterBottles(self, numBottles: int, numExchange: int) -> int:
bottles , empty = numBottles , numBottles
while empty >= numExchange:
empty -= numExchange - 1
bottles += 1
return bottles | water-bottles | [Python3] Simple And 99% Faster Solution | Lolopola | 2 | 101 | water bottles | 1,518 | 0.602 | Easy | 22,505 |
https://leetcode.com/problems/water-bottles/discuss/1093216/Python3-simple-solution | class Solution:
def numWaterBottles(self, a: int, b: int) -> int:
empty = 0
count = 0
while a > 0:
count += a
empty += a
a = empty // b
empty %= b
return count | water-bottles | Python3 simple solution | EklavyaJoshi | 1 | 53 | water bottles | 1,518 | 0.602 | Easy | 22,506 |
https://leetcode.com/problems/water-bottles/discuss/819908/Python-faster-than-99-percent! | class Solution:
def numWaterBottles(self, numBottles: int, numExchange: int) -> int:
empty=numBottles
fill=numBottles
while True:
if empty//numExchange>0:
fill+=empty//numExchange
empty=empty%numExchange+empty//numExchange
else:
return fill | water-bottles | Python faster than 99 percent! | man_it | 1 | 114 | water bottles | 1,518 | 0.602 | Easy | 22,507 |
https://leetcode.com/problems/water-bottles/discuss/2808370/O(log-n)-32ms-python | class Solution:
def numWaterBottles(self, numBottles: int, numExchange: int) -> int:
drunken = 0
empty = 0
while numBottles > 0:
drunken += numBottles
empty += numBottles
n = floor(empty/numExchange)
numBottles = n
empty -= n*numExchange
return drunken | water-bottles | O(log n) 32ms python | lucasscodes | 0 | 3 | water bottles | 1,518 | 0.602 | Easy | 22,508 |
https://leetcode.com/problems/water-bottles/discuss/2442165/Python-for-beginners-(with-Explanation) | class Solution:
def numWaterBottles(self, numBottles: int, numExchange: int) -> int:
# Runtime: 40ms
ans=numBottles #Initial bottles he/she will drink
while numBottles>=numExchange: #If numBottles<numExchange exit the while loop
remainder=numBottles%numExchange #remaining bottles which is not change
numBottles//=numExchange #The bottles which are changed
ans+=numBottles #The bottles which are changed added to the answer
numBottles+=remainder #Remaining bottles==The bottles which is not change+The bottles which are changed
return ans #Return The answer | water-bottles | Python for beginners (with Explanation) | mehtay037 | 0 | 28 | water bottles | 1,518 | 0.602 | Easy | 22,509 |
https://leetcode.com/problems/water-bottles/discuss/2317540/90-fast-Solution-of-Water-bottles | ```class Solution:
def numWaterBottles(self, numBottles: int, numExchange: int) -> int:
drunk = numBottles
while True:
if numBottles < numExchange:
break
div = int(numBottles//numExchange)
drunk += div
remaining = numBottles % numExchange
numBottles = remaining + div
return (drunk) | water-bottles | 90% fast Solution of Water bottles | Jonny69 | 0 | 36 | water bottles | 1,518 | 0.602 | Easy | 22,510 |
https://leetcode.com/problems/water-bottles/discuss/2240764/5-liner-Python-O(log-n)-solution........Easy-to-understand-and-one-liner-soln-as-well. | class Solution:
def numWaterBottles(self, numBottles: int, numExchange: int) -> int:
return numBottles+(numBottles-1)//(numExchange-1) | water-bottles | 5 liner Python O(log n) solution........Easy to understand and one liner soln as well. | guneet100 | 0 | 40 | water bottles | 1,518 | 0.602 | Easy | 22,511 |
https://leetcode.com/problems/water-bottles/discuss/1842550/1-Line-Python-Solution-oror-60-Faster-oror-Memory-less-than-80 | class Solution:
def numWaterBottles(self, N: int, E: int) -> int:
ans=N
while N>=E: ans+=N//E ; N-=(N//E)*(E-1)
return ans | water-bottles | 1-Line Python Solution || 60% Faster || Memory less than 80% | Taha-C | 0 | 68 | water bottles | 1,518 | 0.602 | Easy | 22,512 |
https://leetcode.com/problems/water-bottles/discuss/1842550/1-Line-Python-Solution-oror-60-Faster-oror-Memory-less-than-80 | class Solution:
def numWaterBottles(self, N: int, E: int) -> int:
return N + N//(E-1) - (1 if not N%(E-1) else 0) | water-bottles | 1-Line Python Solution || 60% Faster || Memory less than 80% | Taha-C | 0 | 68 | water bottles | 1,518 | 0.602 | Easy | 22,513 |
https://leetcode.com/problems/water-bottles/discuss/1842550/1-Line-Python-Solution-oror-60-Faster-oror-Memory-less-than-80 | class Solution:
def numWaterBottles(self, N: int, E: int) -> int:
return (N*E-1)//(E-1) | water-bottles | 1-Line Python Solution || 60% Faster || Memory less than 80% | Taha-C | 0 | 68 | water bottles | 1,518 | 0.602 | Easy | 22,514 |
https://leetcode.com/problems/water-bottles/discuss/1756535/Python-dollarolution | class Solution:
def numWaterBottles(self, numBottles: int, numExchange: int) -> int:
count = numBottles
while numBottles > numExchange-1:
left = numBottles % numExchange
numBottles //= numExchange
count += numBottles
numBottles += left
return count | water-bottles | Python $olution | AakRay | 0 | 59 | water bottles | 1,518 | 0.602 | Easy | 22,515 |
https://leetcode.com/problems/water-bottles/discuss/1594969/Python-3-iterative-solution | class Solution:
def numWaterBottles(self, numBottles: int, numExchange: int) -> int:
empty = res = 0
while numBottles:
res += numBottles
empty += numBottles
numBottles, empty = divmod(empty, numExchange)
return res | water-bottles | Python 3 iterative solution | dereky4 | 0 | 116 | water bottles | 1,518 | 0.602 | Easy | 22,516 |
https://leetcode.com/problems/water-bottles/discuss/1397528/Straightforward-Python-Solution | class Solution:
def numWaterBottles(self, numBottles: int, numExchange: int) -> int:
out, empty = 0, 0
while numBottles > 0:
out += 1
numBottles -= 1
empty += 1
if empty == numExchange:
empty = 0
numBottles += 1
return out | water-bottles | Straightforward Python Solution | remy1991 | 0 | 43 | water bottles | 1,518 | 0.602 | Easy | 22,517 |
https://leetcode.com/problems/water-bottles/discuss/1149189/Python3 | class Solution:
def numWaterBottles(self, numBottles: int, numExchange: int) -> int:
count, empty = 0, 0
while numBottles > 0 :
count = count + numBottles
empty += numBottles
numBottles = empty//numExchange
empty = empty%numExchange
return(count) | water-bottles | Python3 | levi07 | 0 | 57 | water bottles | 1,518 | 0.602 | Easy | 22,518 |
https://leetcode.com/problems/water-bottles/discuss/1142276/Python-Easy-to-Read-Solution | class Solution:
def numWaterBottles(self, numBottles: int, numExchange: int) -> int:
Sum = numBottles
while (numBottles>=numExchange):
numBottles, spares = divmod(numBottles,numExchange)
Sum = Sum + numBottles
numBottles= numBottles + spares
return Sum | water-bottles | Python Easy to Read Solution | JamesTaylor108 | 0 | 69 | water bottles | 1,518 | 0.602 | Easy | 22,519 |
https://leetcode.com/problems/water-bottles/discuss/1048173/Ultra-Simple-CppPython3Java-Solution-or-Suggestions-for-optimization-are-welcomed-or | class Solution:
def numWaterBottles(self, numBottles: int, numExchange: int) -> int:
ans=numBottles
while int(numBottles/numExchange)>0:
ans=ans+int(numBottles/numExchange)
numBottles=int(numBottles/numExchange)+int(numBottles%numExchange)
return ans; | water-bottles | Ultra Simple Cpp/Python3/Java Solution | Suggestions for optimization are welcomed | | angiras_rohit | 0 | 55 | water bottles | 1,518 | 0.602 | Easy | 22,520 |
https://leetcode.com/problems/water-bottles/discuss/1016649/Easy-and-Clear-Solution-Python-3 | class Solution:
def numWaterBottles(self, nb: int, nx: int) -> int:
res=0
empty=nb
r=0
while empty>=nx:
drink=empty//nx
res+=drink
r=empty%nx
empty=drink+r
return res+nb | water-bottles | Easy & Clear Solution Python 3 | moazmar | 0 | 100 | water bottles | 1,518 | 0.602 | Easy | 22,521 |
https://leetcode.com/problems/water-bottles/discuss/987821/Fast-Python-solution | class Solution:
def numWaterBottles(self, numBottles: int, numExchange: int) -> int:
had = numBottles
emptybottles = numBottles
while emptybottles>=numExchange:
had += emptybottles // numExchange
emptybottles = emptybottles - emptybottles // numExchange * numExchange + emptybottles // numExchange
return had | water-bottles | Fast Python solution | LarryTao | 0 | 44 | water bottles | 1,518 | 0.602 | Easy | 22,522 |
https://leetcode.com/problems/water-bottles/discuss/900242/python-While-loop-and-Easy-to-understand | class Solution:
def numWaterBottles(self, numBottles: int, numExchange: int) -> int:
res = numBottles
i = numBottles
while i >= numExchange:
res += i // numExchange
i = i // numExchange + i % numExchange
return res | water-bottles | [python] While loop & Easy to understand | 221Baker | 0 | 41 | water bottles | 1,518 | 0.602 | Easy | 22,523 |
https://leetcode.com/problems/water-bottles/discuss/792657/Explanation-water-bottle-faster-running | class Solution:
def numWaterBottles(self, f: int, e: int) -> int:
eb=f
su=0
while eb>=e:
b=eb//e
print (b)
su=su+b
eb=b+(eb%e)
return f+su | water-bottles | Explanation water bottle faster running | paul_dream | 0 | 37 | water bottles | 1,518 | 0.602 | Easy | 22,524 |
https://leetcode.com/problems/water-bottles/discuss/744108/python3-Solution-using-floor-division-100-Faster-100-Memory | class Solution:
def numWaterBottles(self, numBottles: int, numExchange: int) -> int:
drink = numBottles # Initial drank bottles
empty = numBottles
trade = numBottles # Initial traded bottles
while empty//numExchange > 0:
trade = (empty//numExchange) * numExchange
drink += empty//numExchange
empty = (empty - trade) + trade//numExchange
return drink | water-bottles | python3 Solution using floor division, 100% Faster, 100% Memory | zharfanf | 0 | 45 | water bottles | 1,518 | 0.602 | Easy | 22,525 |
https://leetcode.com/problems/water-bottles/discuss/743237/Easy-Python-Recursive-with-Comments | class Solution:
def numWaterBottles(self, numBottles: int, numExchange: int) -> int:
def helper(full, empty, drank):
# Base case, make sure we can continue (we have enough to cash in)
if full + empty < numExchange:
return drank+full
# If we have any full bottles we'll make them empty
if full:
empty += full
# Exchange empties for fulls
new = empty // numExchange
# Don't forget there might be remainders!
remaining_empt = empty - (new*numExchange)
return helper(new, remaining_empt, drank + full)
return helper(numBottles, 0, 0) | water-bottles | Easy Python Recursive with Comments | Pythagoras_the_3rd | 0 | 58 | water bottles | 1,518 | 0.602 | Easy | 22,526 |
https://leetcode.com/problems/water-bottles/discuss/743144/PythonPython3-Water-Bottles | class Solution:
def numWaterBottles(self, numBottles: int, numExchange: int) -> int:
total = numBottles
p = numBottles
while p >= numExchange:
p_int = p // numExchange
p_rem = p % numExchange
p = p_int + p_rem
total += p_int
return total | water-bottles | [Python/Python3] Water Bottles | newborncoder | 0 | 144 | water bottles | 1,518 | 0.602 | Easy | 22,527 |
https://leetcode.com/problems/number-of-nodes-in-the-sub-tree-with-the-same-label/discuss/1441578/Python-3-or-DFS-Graph-Counter-or-Explanation | class Solution:
def countSubTrees(self, n: int, edges: List[List[int]], labels: str) -> List[int]:
ans = [0] * n
tree = collections.defaultdict(list)
for a, b in edges: # build tree
tree[a].append(b)
tree[b].append(a)
def dfs(node): # dfs
nonlocal visited, ans, tree
c = collections.Counter(labels[node])
for nei in tree[node]:
if nei in visited: continue # avoid revisit
visited.add(nei)
c += dfs(nei) # add counter (essentially adding a 26 elements dictionary)
ans[node] = c.get(labels[node]) # assign count of label to this node
return c
visited = set([0])
dfs(0)
return ans | number-of-nodes-in-the-sub-tree-with-the-same-label | Python 3 | DFS, Graph, Counter | Explanation | idontknoooo | 7 | 366 | number of nodes in the sub tree with the same label | 1,519 | 0.41 | Medium | 22,528 |
https://leetcode.com/problems/number-of-nodes-in-the-sub-tree-with-the-same-label/discuss/743208/Python3-spectrum-of-each-node | class Solution:
def countSubTrees(self, n: int, edges: List[List[int]], labels: str) -> List[int]:
#tree as adjacency list
tree = dict()
for u, v in edges:
tree.setdefault(u, []).append(v)
tree.setdefault(v, []).append(u)
def fn(k):
"""Return frequency table of tree rooted at given node"""
seen.add(k) #mark as visited
freq = [0]*26
freq[ord(labels[k])-97] = 1
for kk in tree[k]:
if kk not in seen: freq = [x+y for x, y in zip(freq, fn(kk))]
ans[k] = freq[ord(labels[k])-97] #populate table
return freq
ans = [0]*n
seen = set()
fn(0)
return ans | number-of-nodes-in-the-sub-tree-with-the-same-label | [Python3] spectrum of each node | ye15 | 1 | 88 | number of nodes in the sub tree with the same label | 1,519 | 0.41 | Medium | 22,529 |
https://leetcode.com/problems/number-of-nodes-in-the-sub-tree-with-the-same-label/discuss/2166117/Python-DFS-using-Counter | class Solution:
def countSubTrees(self, n: int, edges: List[List[int]], labels: str) -> List[int]:
def dfs(node, parent):
counter = Counter()
for child in adj[node]:
if child != parent:
counter += dfs(child, node)
counter[labels[node]] += 1
result[node] = counter[labels[node]]
return counter
adj = defaultdict(list)
for a, b in edges:
adj[a].append(b)
adj[b].append(a)
result = [0] * n
dfs(0, None)
return result | number-of-nodes-in-the-sub-tree-with-the-same-label | Python, DFS using Counter | blue_sky5 | 0 | 24 | number of nodes in the sub tree with the same label | 1,519 | 0.41 | Medium | 22,530 |
https://leetcode.com/problems/number-of-nodes-in-the-sub-tree-with-the-same-label/discuss/743206/Python3-DFS-With-Comments | class Solution:
def countSubTrees(self, n: int, edges: List[List[int]], labels: str) -> List[int]:
graph = self.build_graph(edges)
res = [0] * n
visited = set()
def add(seen1, seen2):
seen = [0] * 26
for i in range(26):
seen[i] = seen1[i] + seen2[i]
return seen
def index(char):
return ord(char) - ord('a')
def dfs(node):
# Maintain a visited since the edge is represented in both directions in the graph
visited.add(node)
seen = [0] * 26
for neigh in graph.get(node, []):
if not neigh in visited:
seen = add(seen, dfs(neigh))
seen[index(labels[node])] += 1
res[node] = seen[index(labels[node])]
return seen
dfs(0)
return res
def build_graph(self, edges):
graph = {}
for edge in edges:
# Include edges in both directions since ordering of edges is not guaranteed
# e.g. case [[0, 2], [0, 3], [1, 2]]
# In above example we should traverse from 2 to 1.
graph.setdefault(edge[0], []).append(edge[1])
graph.setdefault(edge[1], []).append(edge[0])
return graph | number-of-nodes-in-the-sub-tree-with-the-same-label | Python3 DFS With Comments | jzrand | 0 | 65 | number of nodes in the sub tree with the same label | 1,519 | 0.41 | Medium | 22,531 |
https://leetcode.com/problems/maximum-number-of-non-overlapping-substrings/discuss/1208758/Python3-greedy | class Solution:
def maxNumOfSubstrings(self, s: str) -> List[str]:
locs = {}
for i, x in enumerate(s):
locs.setdefault(x, []).append(i)
def fn(lo, hi):
"""Return expanded range covering all chars in s[lo:hi+1]."""
for xx in locs:
k0 = bisect_left(locs[xx], lo)
k1 = bisect_left(locs[xx], hi)
if k0 < k1 and (locs[xx][0] < lo or hi < locs[xx][-1]):
lo = min(lo, locs[xx][0])
hi = max(hi, locs[xx][-1])
lo, hi = fn(lo, hi)
return lo, hi
group = set()
for x in locs:
group.add(fn(locs[x][0], locs[x][-1]))
ans = [] # ISMP (interval scheduling maximization problem)
prev = -1
for lo, hi in sorted(group, key=lambda x: x[1]):
if prev < lo:
ans.append(s[lo:hi+1])
prev = hi
return ans | maximum-number-of-non-overlapping-substrings | [Python3] greedy | ye15 | 1 | 281 | maximum number of non overlapping substrings | 1,520 | 0.381 | Hard | 22,532 |
https://leetcode.com/problems/maximum-number-of-non-overlapping-substrings/discuss/744042/Python3-use-union-find-to-remove-redundant-characters-readable-but-slow | class Solution:
def maxNumOfSubstrings(self, s: str) -> List[str]:
# record last index(+1) and interval relations
last, interval = {}, {}
for i, c in enumerate(s):
last[c] = i + 1
if c not in interval:
interval[c] = set(''.join(s.split(c)[1:-1]))
# union-find
parent = {c: c for c in s}
def find(x):
if parent[x] != x:
parent[x] = find(parent[x])
return parent[x]
def union(x, y):
parent[find(x)] = find(y)
return
for c in interval:
for n in interval[c]:
if c in interval[n]:
union(c, n)
# cut and remove redundant character
splits, max_splits = [(s, 0)], []
while splits:
# cut
splits_cut = []
for string, start in splits:
left = right = 0
for i, c in enumerate(string):
right = max(right, last[c] - start)
if i == right - 1:
splits_cut.append((string[left:right], start + left))
left = right
# remove redundant
splits_rem = []
for string, start in splits_cut:
len_splits_rem = len(splits_rem)
redundant = find(string[0])
left = 0
while left < len(string):
if find(string[left]) == redundant:
left += 1
else:
right = left
while right < len(string) and find(string[right]) != redundant:
right += 1
splits_rem.append((string[left:right], start + left))
left = right
if len_splits_rem == len(splits_rem):
max_splits.append(string)
splits = splits_rem
return max_splits | maximum-number-of-non-overlapping-substrings | [Python3] use union-find to remove redundant characters, readable but slow | dashidhy | 0 | 109 | maximum number of non overlapping substrings | 1,520 | 0.381 | Hard | 22,533 |
https://leetcode.com/problems/find-a-value-of-a-mysterious-function-closest-to-target/discuss/746723/Python3-bitwise-and | class Solution:
def closestToTarget(self, arr: List[int], target: int) -> int:
ans, seen = inf, set()
for x in arr:
seen = {ss & x for ss in seen} | {x}
ans = min(ans, min(abs(ss - target) for ss in seen))
return ans | find-a-value-of-a-mysterious-function-closest-to-target | [Python3] bitwise and | ye15 | 8 | 306 | find a value of a mysterious function closest to target | 1,521 | 0.436 | Hard | 22,534 |
https://leetcode.com/problems/find-a-value-of-a-mysterious-function-closest-to-target/discuss/746723/Python3-bitwise-and | class Solution:
def closestToTarget(self, arr: List[int], target: int) -> int:
ans, seen = inf, set()
for x in arr:
tmp = set() #new set
seen.add(0xffffffff)
for ss in seen:
ss &= x
ans = min(ans, abs(ss - target))
if ss > target: tmp.add(ss) #fine tuning
seen = tmp
return ans | find-a-value-of-a-mysterious-function-closest-to-target | [Python3] bitwise and | ye15 | 8 | 306 | find a value of a mysterious function closest to target | 1,521 | 0.436 | Hard | 22,535 |
https://leetcode.com/problems/find-a-value-of-a-mysterious-function-closest-to-target/discuss/746723/Python3-bitwise-and | class Solution:
def rangeBitwiseAnd(self, m: int, n: int) -> int:
while n > m:
n &= n-1 #unset last set bit
return n | find-a-value-of-a-mysterious-function-closest-to-target | [Python3] bitwise and | ye15 | 8 | 306 | find a value of a mysterious function closest to target | 1,521 | 0.436 | Hard | 22,536 |
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/discuss/1813332/Python-3-or-Math-or-Intuitive | class Solution:
def countOdds(self, low: int, high: int) -> int:
if low % 2 == 0:
return (high-low+1)//2
return (high-low)//2 + 1 | count-odd-numbers-in-an-interval-range | Python 3 | Math | Intuitive | ndus | 60 | 4,500 | count odd numbers in an interval range | 1,523 | 0.462 | Easy | 22,537 |
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/discuss/1804243/Python3-or-2-solutions | class Solution:
def countOdds(self, low: int, high: int) -> int:
val=(high-low+1)//2
return val+1 if(low&1 and high&1) else val | count-odd-numbers-in-an-interval-range | Python3 | 2 solutions | Anilchouhan181 | 4 | 401 | count odd numbers in an interval range | 1,523 | 0.462 | Easy | 22,538 |
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/discuss/1804243/Python3-or-2-solutions | class Solution:
def countOdds(self, low: int, high: int) -> int:
if low%2==0 and high%2==0:
return (high-low)//2
if low%2==1 or high%2==1:
return (high-low)//2+1 | count-odd-numbers-in-an-interval-range | Python3 | 2 solutions | Anilchouhan181 | 4 | 401 | count odd numbers in an interval range | 1,523 | 0.462 | Easy | 22,539 |
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/discuss/2099110/Python-solution | class Solution:
def countOdds(self, low: int, high: int) -> int:
if(low%2==0 and high%2==0):
return (high-low)//2
else:
return (high-low)//2 + 1 | count-odd-numbers-in-an-interval-range | Python solution | yashkumarjha | 3 | 312 | count odd numbers in an interval range | 1,523 | 0.462 | Easy | 22,540 |
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/discuss/1917728/Python-Explanation-or-One-Liner-or-Math-Approach-or-Clean-and-Concise! | class Solution:
def countOdds(self, low, high):
odds = 0
for i in range(low,high+1):
odds += i % 2
return odds | count-odd-numbers-in-an-interval-range | Python - Explanation | One-Liner | Math Approach | Clean and Concise! | domthedeveloper | 3 | 385 | count odd numbers in an interval range | 1,523 | 0.462 | Easy | 22,541 |
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/discuss/1917728/Python-Explanation-or-One-Liner-or-Math-Approach-or-Clean-and-Concise! | class Solution:
def countOdds(self, low: int, high: int) -> int:
return (high-low)//2 + high%2 + low%2 - (high%2 and low%2) | count-odd-numbers-in-an-interval-range | Python - Explanation | One-Liner | Math Approach | Clean and Concise! | domthedeveloper | 3 | 385 | count odd numbers in an interval range | 1,523 | 0.462 | Easy | 22,542 |
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/discuss/1917728/Python-Explanation-or-One-Liner-or-Math-Approach-or-Clean-and-Concise! | class Solution:
def countOdds(self, low, high):
return (high-low)//2 + (high%2 or low%2) | count-odd-numbers-in-an-interval-range | Python - Explanation | One-Liner | Math Approach | Clean and Concise! | domthedeveloper | 3 | 385 | count odd numbers in an interval range | 1,523 | 0.462 | Easy | 22,543 |
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/discuss/1851676/Python3-beginners-solution | class Solution:
def countOdds(self, low: int, high: int) -> int:
if low%2==0 and high%2==0:
return (high-low)//2
else:
return (high-low)//2+1 | count-odd-numbers-in-an-interval-range | Python3 beginners solution | alishak1999 | 3 | 304 | count odd numbers in an interval range | 1,523 | 0.462 | Easy | 22,544 |
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/discuss/2476392/Beats-99-or-Python-Solution-or-Math-Based-Approach-or-Basic | class Solution:
def countOdds(self, low: int, high: int) -> int:
#if both are even integers
if low%2 == 0 and high%2== 0:
return ((high-low-1)//2 + 1)
#if one of them is odd
elif low%2==0 and high%2!=0 or low%2!=0 and high%2==0:
return ((high-low)//2) + 1
#if both are odd
else:
return ((high-low-1)//2) + 2 | count-odd-numbers-in-an-interval-range | Beats 99% | Python Solution | Math Based Approach | Basic | haminearyan | 2 | 87 | count odd numbers in an interval range | 1,523 | 0.462 | Easy | 22,545 |
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/discuss/1140436/Python3-simple-one-liner-solution-beats-90-users | class Solution:
def countOdds(self, low: int, high: int) -> int:
return (high-low+1)//2 if low%2 == 0 else (high-low+2)//2 | count-odd-numbers-in-an-interval-range | Python3 simple one-liner solution beats 90% users | EklavyaJoshi | 2 | 58 | count odd numbers in an interval range | 1,523 | 0.462 | Easy | 22,546 |
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/discuss/2316421/time-Limit | class Solution:
def countOdds(self, low: int, high: int) -> int:
return (high - low + low % 2 + high % 2) // 2
count=0
if(low < high):
for i in range(low, high+1):
if(low%2!=0):
count=count+1
low=low+1
else:
low=low+1
return count
elif(low==high):
if(low%2!=0):
return 1
else:
return 0 | count-odd-numbers-in-an-interval-range | time Limit | vimla_kushwaha | 1 | 30 | count odd numbers in an interval range | 1,523 | 0.462 | Easy | 22,547 |
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/discuss/2178132/Python-or-Math-logic | class Solution:
def countOdds(self, low: int, high: int) -> int:
if low % 2 == 0 and high % 2 == 0:
return (high - low) // 2
else:
return (high - low) // 2 + 1 | count-odd-numbers-in-an-interval-range | Python | Math logic | YangJenHao | 1 | 160 | count odd numbers in an interval range | 1,523 | 0.462 | Easy | 22,548 |
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/discuss/1815525/Python-all-approaches-or-Simple-Solution | class Solution:
def countOdds(self, low: int, high: int) -> int:
oddList=[]
for x in range(low,high+1):
if x%2!=0:
oddList.append(x)
return len(oddList) | count-odd-numbers-in-an-interval-range | Python all approaches | Simple Solution | vampirepapi | 1 | 197 | count odd numbers in an interval range | 1,523 | 0.462 | Easy | 22,549 |
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/discuss/1815525/Python-all-approaches-or-Simple-Solution | class Solution:
def countOdds(self, low: int, high: int) -> int:
oddCount=0
for x in range(low,high+1):
if x%2!=0:
oddCount+=1
return oddCount | count-odd-numbers-in-an-interval-range | Python all approaches | Simple Solution | vampirepapi | 1 | 197 | count odd numbers in an interval range | 1,523 | 0.462 | Easy | 22,550 |
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/discuss/1815525/Python-all-approaches-or-Simple-Solution | class Solution:
def countOdds(self, low: int, high: int) -> int:
diff = high-low
# if any on the number high/low is odd
if high %2 != 0 or low %2 != 0:
return (diff // 2) +1
else:
return diff//2 | count-odd-numbers-in-an-interval-range | Python all approaches | Simple Solution | vampirepapi | 1 | 197 | count odd numbers in an interval range | 1,523 | 0.462 | Easy | 22,551 |
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/discuss/1800092/Python3-One-liner | class Solution:
def countOdds(self, low: int, high: int) -> int:
return (high-low)//2 + 1 if (low % 2 or high % 2) else (high - low)//2 | count-odd-numbers-in-an-interval-range | [Python3] One-liner | __PiYush__ | 1 | 58 | count odd numbers in an interval range | 1,523 | 0.462 | Easy | 22,552 |
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/discuss/1539649/python-soln-without-forwhile-loops | class Solution:
def countOdds(self, low: int, high: int):
if low%2!=0 and high%2!=0: return int(((high-low)/2)+1)
elif low%2==0 and high%2==0: return int(((high-low)/2))
else: return int(((high-low)+1)/2) | count-odd-numbers-in-an-interval-range | python soln without for/while loops | anandanshul001 | 1 | 187 | count odd numbers in an interval range | 1,523 | 0.462 | Easy | 22,553 |
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/discuss/859945/Python3-summarizing-a-few-1-liners | class Solution:
def countOdds(self, low: int, high: int) -> int:
return (high-low)//2 + (low%2 or high%2) | count-odd-numbers-in-an-interval-range | [Python3] summarizing a few 1-liners | ye15 | 1 | 59 | count odd numbers in an interval range | 1,523 | 0.462 | Easy | 22,554 |
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/discuss/859945/Python3-summarizing-a-few-1-liners | class Solution:
def countOdds(self, low: int, high: int) -> int:
return (high+1)//2 - low//2 | count-odd-numbers-in-an-interval-range | [Python3] summarizing a few 1-liners | ye15 | 1 | 59 | count odd numbers in an interval range | 1,523 | 0.462 | Easy | 22,555 |
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/discuss/859945/Python3-summarizing-a-few-1-liners | class Solution:
def countOdds(self, low: int, high: int) -> int:
return (high - low)//2 + ((high | low) & 1) | count-odd-numbers-in-an-interval-range | [Python3] summarizing a few 1-liners | ye15 | 1 | 59 | count odd numbers in an interval range | 1,523 | 0.462 | Easy | 22,556 |
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/discuss/2833124/Solution-for-task-about-number-of-odds-in-interval | class Solution:
def countOdds(self, low: int, high: int) -> int:
if low %2 ==0:
low+=1
if high % 2 ==0:
high-=1
return int((high - low)/2 +1) | count-odd-numbers-in-an-interval-range | Solution for task about number of odds in interval | kristinapoberezhna8 | 0 | 1 | count odd numbers in an interval range | 1,523 | 0.462 | Easy | 22,557 |
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/discuss/2803468/Python-Solution | class Solution:
def countOdds(self, low: int, high: int) -> int:
return (high-low)//2+max(low%2,high%2) | count-odd-numbers-in-an-interval-range | Python Solution | manishkumarsahu724 | 0 | 6 | count odd numbers in an interval range | 1,523 | 0.462 | Easy | 22,558 |
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/discuss/2785637/Python-one-liner | class Solution:
def countOdds(self, low: int, high: int) -> int:
return math.ceil(high/2)-math.floor(low/2) | count-odd-numbers-in-an-interval-range | Python one liner | rama_krishna044 | 0 | 7 | count odd numbers in an interval range | 1,523 | 0.462 | Easy | 22,559 |
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/discuss/2782189/Python3-oror-Easy-solution | class Solution:
def countOdds(self, low: int, high: int) -> int:
if low % 2 == 0:
low += 1
if high % 2 == 0:
high -= 1
return int((high-low)/2+1) | count-odd-numbers-in-an-interval-range | ✅ Python3 || Easy solution | PabloVE2001 | 0 | 13 | count odd numbers in an interval range | 1,523 | 0.462 | Easy | 22,560 |
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/discuss/2781581/Fast-Algorithm!-Pythonic-Solution!-Beats-99.38-in-time-complexity. | class Solution:
def countOdds(self, low: int, high: int) -> int:
# Checks if both the extreme ends of the range are odd
if low % 2 == 1 and high % 2 == 1:
return ((((high - low) + 1) // 2) + 1)
# Checks if both the extreme ends of the range are even
elif low % 2 == 0 and high % 2 == 0:
return (((high - low) + 1) // 2)
# The extreme ends of the range can either be odd or even
else:
return (((high - low) + 1) // 2) | count-odd-numbers-in-an-interval-range | Fast Algorithm! Pythonic Solution! Beats 99.38% in time complexity. | arifaisal123 | 0 | 2 | count odd numbers in an interval range | 1,523 | 0.462 | Easy | 22,561 |
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/discuss/2780337/Count-Odd-Numbers-in-an-Interval-Range-or-Python-orVery-Easy-Solution | class Solution:
def countOdds(self, low: int, high: int) -> int:
# Basic Brute Force Approach
#O(N)
'''
count = 0
for i in range(low,high+1):
if i % 2 != 0:
count += 1
return count
'''
# OPtimise Solution O(1)
if low % 2 == 0:
return (high - low+1)//2
return ((high - low)// 2) +1 | count-odd-numbers-in-an-interval-range | Count Odd Numbers in an Interval Range | Python |Very Easy Solution | jashii96 | 0 | 2 | count odd numbers in an interval range | 1,523 | 0.462 | Easy | 22,562 |
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/discuss/2771049/Python | class Solution:
def countOdds(self, low: int, high: int) -> int:
nums = high-low
if low%2 == 0 and high%2 ==0:
return nums//2
else:
return nums//2+1 | count-odd-numbers-in-an-interval-range | Python | haniyeka | 0 | 4 | count odd numbers in an interval range | 1,523 | 0.462 | Easy | 22,563 |
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/discuss/2770695/Python-Simple-Solution-O(1) | class Solution:
def countOdds(self, low: int, high: int) -> int:
#First step
res = (high - low) // 2
#Second step
if high%2 or low%2:
res = res + 1
return res | count-odd-numbers-in-an-interval-range | Python Simple Solution O(1) | silvestrofrisullo | 0 | 1 | count odd numbers in an interval range | 1,523 | 0.462 | Easy | 22,564 |
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/discuss/2749926/odd-numbers | class Solution:
def countOdds(self, low: int, high: int) -> int:
length = len(range(low,high+1))
count = length // 2
if high % 2 > 0 and low % 2 > 0:
count += 1
return count | count-odd-numbers-in-an-interval-range | odd numbers | nicklucianocorona | 0 | 3 | count odd numbers in an interval range | 1,523 | 0.462 | Easy | 22,565 |
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/discuss/2749055/Easy-and-Fast-Python-Solution | class Solution:
def countOdds(self, low: int, high: int) -> int:
low = low+1 if low%2==0 else low
high = high-1 if high%2==0 else high
return (high-low)//2 + 1 | count-odd-numbers-in-an-interval-range | Easy and Fast Python Solution | vivekrajyaguru | 0 | 3 | count odd numbers in an interval range | 1,523 | 0.462 | Easy | 22,566 |
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/discuss/2749049/Python3-Solution | class Solution:
def countOdds(self, low: int, high: int) -> int:
low = low+1 if low%2==0 else low
high = high-1 if high%2==0 else high
return (high-low)//2 + 1 | count-odd-numbers-in-an-interval-range | Python3 Solution | vivekrajyaguru | 0 | 2 | count odd numbers in an interval range | 1,523 | 0.462 | Easy | 22,567 |
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/discuss/2724401/Simple-Math | class Solution:
def countOdds(self, low: int, high: int) -> int:
if high%2 != 0 and low%2 != 0:
return (high-low+1)//2+1
return (high-low+1)//2 | count-odd-numbers-in-an-interval-range | Simple Math | wakadoodle | 0 | 8 | count odd numbers in an interval range | 1,523 | 0.462 | Easy | 22,568 |
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/discuss/2709527/Py3-Step-by-step-beginner-explanations-for-all-different-types-of-solution-COMPILATION | class Solution:
def countOdds(self, low: int, high: int) -> int:
odds = 0
for i in range(low,high+1):
if i % 2 ==1:
odds += 1
return odds | count-odd-numbers-in-an-interval-range | ⭐[Py3] Step by step beginner explanations for all different types of solution COMPILATION | bromalone | 0 | 5 | count odd numbers in an interval range | 1,523 | 0.462 | Easy | 22,569 |
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/discuss/2709527/Py3-Step-by-step-beginner-explanations-for-all-different-types-of-solution-COMPILATION | class Solution:
def countOdds(self, low: int, high: int) -> int:
range_len = high-low+1
range_len_is_even = range_len % 2 == 0
is_odd_odd = low % 2 == 1 and high % 2 == 1
if range_len_is_even:
return range_len//2
else:
if is_odd_odd:
return range_len//2 + 1
else:
return range_len//2 | count-odd-numbers-in-an-interval-range | ⭐[Py3] Step by step beginner explanations for all different types of solution COMPILATION | bromalone | 0 | 5 | count odd numbers in an interval range | 1,523 | 0.462 | Easy | 22,570 |
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/discuss/2709527/Py3-Step-by-step-beginner-explanations-for-all-different-types-of-solution-COMPILATION | class Solution:
def countOdds(self, low: int, high: int) -> int:
if high % 2 == 1:
high += 1
return len(range(low,high,2)) | count-odd-numbers-in-an-interval-range | ⭐[Py3] Step by step beginner explanations for all different types of solution COMPILATION | bromalone | 0 | 5 | count odd numbers in an interval range | 1,523 | 0.462 | Easy | 22,571 |
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/discuss/2709527/Py3-Step-by-step-beginner-explanations-for-all-different-types-of-solution-COMPILATION | class Solution:
def countOdds(self, low: int, high: int) -> int:
if high % 2 == 1:
high += 1
return (high-low+1)//2 | count-odd-numbers-in-an-interval-range | ⭐[Py3] Step by step beginner explanations for all different types of solution COMPILATION | bromalone | 0 | 5 | count odd numbers in an interval range | 1,523 | 0.462 | Easy | 22,572 |
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/discuss/2709527/Py3-Step-by-step-beginner-explanations-for-all-different-types-of-solution-COMPILATION | class Solution:
def countOdds(self, low: int, high: int) -> int:
return (high + 1) // 2 - low // 2 | count-odd-numbers-in-an-interval-range | ⭐[Py3] Step by step beginner explanations for all different types of solution COMPILATION | bromalone | 0 | 5 | count odd numbers in an interval range | 1,523 | 0.462 | Easy | 22,573 |
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/discuss/2676210/Simple-solution | class Solution:
def countOdds(self, low: int, high: int) -> int:
if low % 2 == 0:
low += 1
if high % 2 == 0:
high -= 1
return (high-low)//2+1 | count-odd-numbers-in-an-interval-range | Simple solution | MockingJay37 | 0 | 61 | count odd numbers in an interval range | 1,523 | 0.462 | Easy | 22,574 |
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/discuss/2657310/Python3-Solution | class Solution:
def countOdds(self, low: int, high: int) -> int:
if low % 2 == 0:
low += 1
if high % 2 == 0:
high -= 1
return (high - low) // 2 + 1 | count-odd-numbers-in-an-interval-range | Python3 Solution | AnzheYuan1217 | 0 | 93 | count odd numbers in an interval range | 1,523 | 0.462 | Easy | 22,575 |
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/discuss/2598262/Python-Math-Solution | class Solution:
def countOdds(self, low: int, high: int) -> int:
if low % 2 == 1:
low -= 1
if high % 2 == 1:
high += 1
return int((high - low) / 2) | count-odd-numbers-in-an-interval-range | Python Math Solution | mansoorafzal | 0 | 72 | count odd numbers in an interval range | 1,523 | 0.462 | Easy | 22,576 |
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/discuss/2581264/Simple-and-concise-solution-in-Python-(33ms) | class Solution:
def countOdds(self, low: int, high: int) -> int:
rang = (high-low)
if rang&1: # if odd
return (rang+1)//2
else:
return rang//2 + (low&1) | count-odd-numbers-in-an-interval-range | Simple and concise solution in Python (33ms) | alexion1 | 0 | 59 | count odd numbers in an interval range | 1,523 | 0.462 | Easy | 22,577 |
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/discuss/2462391/Python3-count-Odd-number | class Solution:
def countOdds(self, low: int, high: int) -> int:
count = 0
for i in list(range(low,high+1)):
if i % 2 == 1:
count += 1
return count | count-odd-numbers-in-an-interval-range | Python3 - count Odd number | prakashpandit | 0 | 131 | count odd numbers in an interval range | 1,523 | 0.462 | Easy | 22,578 |
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/discuss/2442232/Python-for-beginners-3-solutions.. | class Solution:
def countOdds(self, low: int, high: int) -> int:
#More Optimized Solution: Logical odd numbers between [1,high] is (high+1)/2 [1,Low-1] is low/2
#Runtime: 48ms
return (high+1)//2 - low//2
#Optimized Solution: A.P.
#Runtime: 51ms
if(low%2!=0): low=low
else: low=low+1
if(high%2!=0): high=high
else: high=high-1
common_difference=2 #Applying Arithmetic Progression formula Tn=a+(n-1)d
n=((high-low)//common_difference)+1
return n
#Simple Iterative Approach
#Runtime: Time Limit Exceed
count=0
for i in range(low,high+1):
if(i%2!=0):
count+=1
return count | count-odd-numbers-in-an-interval-range | Python for beginners 3 solutions.. | mehtay037 | 0 | 100 | count odd numbers in an interval range | 1,523 | 0.462 | Easy | 22,579 |
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/discuss/2426323/python-one-line-equation-90%2B90%2B | class Solution:
def countOdds(self, low: int, high: int) -> int:
return (high-low+low%2)//2+high%2 | count-odd-numbers-in-an-interval-range | python one-line equation, 90%+/90%+ | atmosphere77777 | 0 | 114 | count odd numbers in an interval range | 1,523 | 0.462 | Easy | 22,580 |
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/discuss/2318133/Python3-Using-Range-Runtime%3A-36-ms-faster-than-80.63 | class Solution:
def countOdds(self, low: int, high: int) -> int:
if low%2 ==1: count = range(low, high+1 ,2)
else: count = range(low+1, high+1 ,2)
return len(count) | count-odd-numbers-in-an-interval-range | Python3 Using Range Runtime: 36 ms, faster than 80.63% | amit0693 | 0 | 79 | count odd numbers in an interval range | 1,523 | 0.462 | Easy | 22,581 |
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/discuss/2313628/Count-Odd-numbers-in-an-Interval-Range | ```class Solution:
def countOdds(self, low: int, high: int) -> int:
if low % 2 == 0:
low += 1
if high % 2 == 0:
high -= 1
odds = int((high - low - 1)//2)
odds += 2
return odds | count-odd-numbers-in-an-interval-range | Count Odd numbers in an Interval Range | Jonny69 | 0 | 31 | count odd numbers in an interval range | 1,523 | 0.462 | Easy | 22,582 |
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/discuss/2272831/Python3-One-line-solution.-O(1). | class Solution:
def countOdds(self, low: int, high: int) -> int:
return (high - low) // 2 + (low & 1 | high & 1) | count-odd-numbers-in-an-interval-range | [Python3] One line solution. O(1). | geka32 | 0 | 142 | count odd numbers in an interval range | 1,523 | 0.462 | Easy | 22,583 |
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/discuss/2165629/Python-Simple-Python-Solution-Using-Math | class Solution:
def countOdds(self, low: int, high: int) -> int:
if low % 2 == 0 and high % 2 == 0:
result = high - low
return result//2
else:
result = high - low
return (result//2)+1 | count-odd-numbers-in-an-interval-range | [ Python ] ✅✅ Simple Python Solution Using Math 🥳✌👍 | ASHOK_KUMAR_MEGHVANSHI | 0 | 257 | count odd numbers in an interval range | 1,523 | 0.462 | Easy | 22,584 |
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/discuss/2144774/Python3-or-O(1)-or-Math-Logic | class Solution:
def countOdds(self, low: int, high: int) -> int:
# both odd
if low%2==1 and high%2==1:
return (high-low)//2 + 1
# both even
elif low%2==0 and high%2==0:
return (high-low)//2
# one odd, one even
else:
return (high-low+1)//2 | count-odd-numbers-in-an-interval-range | Python3 | O(1) | Math Logic | theKshah | 0 | 94 | count odd numbers in an interval range | 1,523 | 0.462 | Easy | 22,585 |
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/discuss/2129824/AP-Concept-or-Python3 | class Solution:
def countOdds(self, low: int, high: int) -> int:
# Applying AP concept
if low % 2 == 0:
low += 1
return (high - low)//2 + 1 | count-odd-numbers-in-an-interval-range | AP Concept | Python3 | May-i-Code | 0 | 149 | count odd numbers in an interval range | 1,523 | 0.462 | Easy | 22,586 |
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/discuss/2093708/Easy-Python-Code | class Solution:
def countOdds(self, low: int, high: int) -> int:
if low%2!=0 or high%2!=0:
return (high-low)//2 +1
elif low%2==0 and high%2==0:
return (high-low)//2 | count-odd-numbers-in-an-interval-range | Easy Python Code | aditigarg_28 | 0 | 198 | count odd numbers in an interval range | 1,523 | 0.462 | Easy | 22,587 |
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/discuss/1982822/Python-O(1) | class Solution:
def countOdds(self, low: int, high: int) -> int:
if high % 2 != 0 or low %2 != 0:
ans = 1
else:
ans = 0
return (high - low)//2 + ans | count-odd-numbers-in-an-interval-range | Python - O(1) | tauilabdelilah97 | 0 | 164 | count odd numbers in an interval range | 1,523 | 0.462 | Easy | 22,588 |
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/discuss/1868206/Simple-python3-solution | class Solution:
def countOdds(self, low: int, high: int) -> int:
dif = high-low
if(low == high):
return 0 if (low%2==0) else 1
if(high%2 == 0):
return -(dif//-2) if (low%2==0) else (dif//2)+1
else:
return (dif//2)+1 if (low%2==0 and low != 0) else (dif//2)+1 | count-odd-numbers-in-an-interval-range | Simple python3 solution | shchamb | 0 | 205 | count odd numbers in an interval range | 1,523 | 0.462 | Easy | 22,589 |
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/discuss/1842610/1-Line-Python-Solution-oror-96-Faster-oror-Memory-less-than-98 | class Solution:
def countOdds(self, l: int, h: int) -> int:
return (h-l)//2+1 if l%2 or h%2 else (h-l)//2 | count-odd-numbers-in-an-interval-range | 1-Line Python Solution || 96% Faster || Memory less than 98% | Taha-C | 0 | 189 | count odd numbers in an interval range | 1,523 | 0.462 | Easy | 22,590 |
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/discuss/1842610/1-Line-Python-Solution-oror-96-Faster-oror-Memory-less-than-98 | class Solution:
def countOdds(self, l: int, h: int) -> int:
return (h+1)//2 - l//2 | count-odd-numbers-in-an-interval-range | 1-Line Python Solution || 96% Faster || Memory less than 98% | Taha-C | 0 | 189 | count odd numbers in an interval range | 1,523 | 0.462 | Easy | 22,591 |
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/discuss/1832012/Help-finding-the-mistake-in-this... | class Solution:
def countOdds(self, low: int, high: int) -> int:
count = 0
for n in range(low, high + 1):
if n % 2 != 0:
count += 1
return count | count-odd-numbers-in-an-interval-range | Help finding the mistake in this... | layrcb | 0 | 53 | count odd numbers in an interval range | 1,523 | 0.462 | Easy | 22,592 |
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/discuss/1756547/Python-dollarolution | class Solution:
def countOdds(self, low: int, high: int) -> int:
if low % 2 == 0:
low += 1
return (((high - low)//2) + 1) | count-odd-numbers-in-an-interval-range | Python $olution | AakRay | 0 | 155 | count odd numbers in an interval range | 1,523 | 0.462 | Easy | 22,593 |
https://leetcode.com/problems/number-of-sub-arrays-with-odd-sum/discuss/2061760/Python-oror-8-line-math-using-Prefix-Sum | class Solution:
def numOfSubarrays(self, arr: List[int]) -> int:
cumSum = odd = even = 0
for num in arr:
cumSum += num
if cumSum % 2:
odd += 1
else:
even += 1
return odd * (even + 1) % (pow(10, 9) + 7) | number-of-sub-arrays-with-odd-sum | Python || 8-line math using Prefix Sum | gulugulugulugulu | 1 | 138 | number of sub arrays with odd sum | 1,524 | 0.436 | Medium | 22,594 |
https://leetcode.com/problems/number-of-sub-arrays-with-odd-sum/discuss/2782867/5-lines-python | class Solution:
def numOfSubarrays(self, arr: List[int]) -> int:
cur = total = 0
for i in range(len(arr)):
if arr[i] & 1: cur = i+1 - cur
total += cur
return total % 1000000007 | number-of-sub-arrays-with-odd-sum | 5 lines python | bent101 | 0 | 3 | number of sub arrays with odd sum | 1,524 | 0.436 | Medium | 22,595 |
https://leetcode.com/problems/number-of-sub-arrays-with-odd-sum/discuss/2782282/DP-with-Greedy-O(n)-python-easy-to-understand | class Solution:
MOD = pow(10, 9) + 7
def numOfSubarrays(self, arr: List[int]) -> int:
N = len(arr)
dp = [0] * N
if arr[0] % 2:
dp[0] = 1
else:
dp[0] = 0
for i in range(1, N):
if arr[i] % 2:
dp[i] = (i + 1 - dp[i - 1]) % self.MOD
else:
dp[i] = dp[i - 1]
return sum(dp) % self.MOD | number-of-sub-arrays-with-odd-sum | DP with Greedy / O(n) / python easy to understand | Lara_Craft | 0 | 5 | number of sub arrays with odd sum | 1,524 | 0.436 | Medium | 22,596 |
https://leetcode.com/problems/number-of-sub-arrays-with-odd-sum/discuss/1895496/Python-easy-to-read-and-understand-or-prefix-sum | class Solution:
def numOfSubarrays(self, arr: List[int]) -> int:
sums, even, odd = 0, 0, 0
ans = 0
for val in arr:
sums += val
ans += 1 if sums % 2 != 0 else 0
if sums % 2 == 0:
even += 1
ans += odd
else:
odd += 1
ans += even
return ans % (10 ** 9 + 7) | number-of-sub-arrays-with-odd-sum | Python easy to read and understand | prefix sum | sanial2001 | 0 | 173 | number of sub arrays with odd sum | 1,524 | 0.436 | Medium | 22,597 |
https://leetcode.com/problems/number-of-sub-arrays-with-odd-sum/discuss/1730738/WEEB-DOES-PYTHON-DP | class Solution:
def numOfSubarrays(self, arr: List[int]) -> int:
if arr[0] % 2 == 0:
dpOdd = [0] * len(arr)
dpEven = [1] + [0] * (len(arr)-1)
result = 0
else:
dpOdd = [1] + [0] * (len(arr)-1)
dpEven = [0] * len(arr)
result = 1
for i in range(1,len(arr)):
if arr[i] % 2 == 1:
dpOdd[i] += 1 + dpEven[i-1]
dpEven[i] += dpOdd[i-1]
else:
dpOdd[i] += dpOdd[i-1]
dpEven[i] += 1 + dpEven[i-1]
result += dpOdd[i]
return result % (10**9 + 7) | number-of-sub-arrays-with-odd-sum | WEEB DOES PYTHON DP | Skywalker5423 | 0 | 105 | number of sub arrays with odd sum | 1,524 | 0.436 | Medium | 22,598 |
https://leetcode.com/problems/number-of-sub-arrays-with-odd-sum/discuss/1105149/Python3-prefix-sum-and-freq-table | class Solution:
def numOfSubarrays(self, arr: List[int]) -> int:
freq = [1, 0]
ans = prefix = 0
for x in arr:
prefix += x
ans += freq[1 ^ prefix&1]
freq[prefix&1] += 1
return ans % 1_000_000_007 | number-of-sub-arrays-with-odd-sum | [Python3] prefix sum & freq table | ye15 | 0 | 126 | number of sub arrays with odd sum | 1,524 | 0.436 | Medium | 22,599 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.