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/reduce-array-size-to-the-half/discuss/2480303/Brute-Force-and-Optimized-Brute-Force......-Faster-than-100 | class Solution:
def minSetSize(self, arr: List[int]) -> int:
c=Counter(arr)
d=sorted(c.values(),reverse=True)
n=len(arr)
f=len(arr)//2
count=0
while n>f:
n-=d[count]
count+=1
return count | reduce-array-size-to-the-half | Brute Force and Optimized Brute Force...... Faster than 100% | guneet100 | 0 | 27 | reduce array size to the half | 1,338 | 0.697 | Medium | 20,100 |
https://leetcode.com/problems/reduce-array-size-to-the-half/discuss/2466461/Python-simple-solution | class Solution:
def minSetSize(self, arr: List[int]) -> int:
di = {}
count = 0
su = 0
length = len(arr)
for i in range(length):
if arr[i] not in di:
di[arr[i]] = 1
else:
di[arr[i]] += 1
li = list(di.values())
... | reduce-array-size-to-the-half | Python simple solution | AchalGupta | 0 | 25 | reduce array size to the half | 1,338 | 0.697 | Medium | 20,101 |
https://leetcode.com/problems/reduce-array-size-to-the-half/discuss/2463663/Python-Solution-EXPLAINED-EASILY | class Solution:
def minSetSize(self, arr: List[int]) -> int:
if len(set(arr))==1:
return 1
elif len(set(arr))==len(arr): #if all nubers are different in arr
return len(arr)//2
v=[]
a=list(set(arr)) # getting different nubers of arr
for i in a:
... | reduce-array-size-to-the-half | Python Solution - EXPLAINED EASILY | T1n1_B0x1 | 0 | 23 | reduce array size to the half | 1,338 | 0.697 | Medium | 20,102 |
https://leetcode.com/problems/reduce-array-size-to-the-half/discuss/2448160/Python-Solution-or-Easy-and-Straightforward-Logic-or-90-Faster-or-Counter-most-common-Based | class Solution:
def minSetSize(self, arr: List[int]) -> int:
size = len(arr) # original length
toSize = size // 2 # target length
counter = 0
counts = Counter(arr).most_common() # to get frequency sorted list
for item in counts:
size -= item[1] # compute ... | reduce-array-size-to-the-half | Python Solution | Easy and Straightforward Logic | 90% Faster | Counter - most common Based | Gautam_ProMax | 0 | 3 | reduce array size to the half | 1,338 | 0.697 | Medium | 20,103 |
https://leetcode.com/problems/reduce-array-size-to-the-half/discuss/2446595/Python3-Easy-and-Quick-Solution | class Solution:
def minSetSize(self, arr: List[int]) -> int:
# Use the Counter func to find counts of each element
cnt = collections.Counter(arr)
# Put the counts in List and sort it descendingly
c = []
for e in cnt.values(): c.append(e)
c.sort(reverse = True)
# Next to ... | reduce-array-size-to-the-half | Python3 Easy and Quick Solution | wwwhhh1988 | 0 | 5 | reduce array size to the half | 1,338 | 0.697 | Medium | 20,104 |
https://leetcode.com/problems/reduce-array-size-to-the-half/discuss/2446511/GolangPython-O(N(log(N))-time-or-O(N)-space | class Solution:
def minSetSize(self, arr: List[int]) -> int:
counter = Counter(arr)
freq = [value for value in counter.values()]
freq.sort(reverse = True)
target_size = len(arr)//2
ans=0
while target_size > 0:
target_size-=freq[ans]
ans+=1
... | reduce-array-size-to-the-half | Golang/Python O(N(log(N)) time | O(N) space | vtalantsev | 0 | 8 | reduce array size to the half | 1,338 | 0.697 | Medium | 20,105 |
https://leetcode.com/problems/reduce-array-size-to-the-half/discuss/2446483/RUNTIME%3A-Faster-than-75-MEMORY%3A-Less-then-99-oror-Clean-and-Simple | class Solution:
def minSetSize(self, arr: List[int]) -> int:
if len(set(arr))==1:
return 1
elif len(set(arr))==len(arr):
return len(arr)//2
else:
from collections import Counter
lst=[item for items, c in Counter(arr).most_common() for item in [items] * c]
sum=0
c=0
i=0
p=lst[i]
while s... | reduce-array-size-to-the-half | RUNTIME: Faster than 75% ; MEMORY: Less then 99% || Clean and Simple | keertika27 | 0 | 4 | reduce array size to the half | 1,338 | 0.697 | Medium | 20,106 |
https://leetcode.com/problems/reduce-array-size-to-the-half/discuss/2445333/Python-oror-Easy-to-Understand-Solution-oror-Using-Collection.Counter() | class Solution:
def minSetSize(self, arr: List[int]) -> int:
halfLength = len(arr)//2
eleminateNum = 0
frequency = 0
for num, count in collections.Counter(arr).most_common():
if frequency + count >= halfLength:
return eleminateNum + 1
... | reduce-array-size-to-the-half | Python ✅ || 🌟 Easy to Understand Solution 🌟 || Using Collection.Counter() 💯 | zunairashafaq | 0 | 8 | reduce array size to the half | 1,338 | 0.697 | Medium | 20,107 |
https://leetcode.com/problems/reduce-array-size-to-the-half/discuss/2444924/Python3-Simple-Solution | class Solution:
def minSetSize(self, arr: List[int]) -> int:
count = {}
for x in range(len(arr)) :
if arr[x] not in count : count[arr[x]] = 0
count[arr[x]] += 1
counter = 0
add = 0
for temp in sorted(count.values(), reverse = True) :
... | reduce-array-size-to-the-half | Python3 Simple Solution | jasoriasaksham01 | 0 | 22 | reduce array size to the half | 1,338 | 0.697 | Medium | 20,108 |
https://leetcode.com/problems/reduce-array-size-to-the-half/discuss/2444414/Python-oror-easy-unstanding-oror-counter | class Solution:
def minSetSize(self, arr: List[int]) -> int:
x = Counter(arr)
y = x.most_common()
_len = counter = 0
for i in range(len(y)):
_len += y[i][1]
counter += 1
if _len >= len(arr) // 2:
return counter | reduce-array-size-to-the-half | Python || easy unstanding || counter | noobj097 | 0 | 2 | reduce array size to the half | 1,338 | 0.697 | Medium | 20,109 |
https://leetcode.com/problems/reduce-array-size-to-the-half/discuss/2444325/Python-greedy-approach-O(n-log-n) | class Solution:
def minSetSize(self, arr: List[int]) -> int:
target = len(arr) // 2
counts = Counter(arr)
sizes = sorted(counts.values())
res = 0
removed = 0
while removed < target:
to_remove = sizes.pop()
res += 1
removed ... | reduce-array-size-to-the-half | Python greedy approach O(n log n) | moselhy | 0 | 5 | reduce array size to the half | 1,338 | 0.697 | Medium | 20,110 |
https://leetcode.com/problems/reduce-array-size-to-the-half/discuss/2444264/Python-oror-simple-solution-oror-sorting-and-heap | class Solution:
def minSetSize(self, arr: List[int]) -> int:
num_counts = []
for num, count in Counter(arr).items():
num_counts.append((num, count))
num_counts.sort(key=lambda x: x[1], reverse=True)
t_count = 0
for i in range(len(num_counts)):
(num, c... | reduce-array-size-to-the-half | Python || simple solution || sorting and heap | wilspi | 0 | 9 | reduce array size to the half | 1,338 | 0.697 | Medium | 20,111 |
https://leetcode.com/problems/reduce-array-size-to-the-half/discuss/2444264/Python-oror-simple-solution-oror-sorting-and-heap | class Solution:
def minSetSize(self, arr: List[int]) -> int:
num_counts = []
for num, count in Counter(arr).items():
num_counts.append((-count, num))
heapq.heapify(num_counts)
t_count, n = 0, 0
while len(num_counts):
(count, num) = heapq.heappop(num_c... | reduce-array-size-to-the-half | Python || simple solution || sorting and heap | wilspi | 0 | 9 | reduce array size to the half | 1,338 | 0.697 | Medium | 20,112 |
https://leetcode.com/problems/reduce-array-size-to-the-half/discuss/2444112/python3-easy-using-counter | class Solution:
def minSetSize(self, arr: List[int]) -> int:
halfLenght = len(arr) // 2
counter = Counter(arr).most_common()
if len(counter) == 1:
return 1
for i, (_, amount) in enumerate(counter):
if halfLenght > 0:
halfLenght -= amount
... | reduce-array-size-to-the-half | python3 easy using counter | 1ncu804u | 0 | 4 | reduce array size to the half | 1,338 | 0.697 | Medium | 20,113 |
https://leetcode.com/problems/reduce-array-size-to-the-half/discuss/2443783/Using-Sorted-Hashmap-or-Python | class Solution:
def minSetSize(self, arr: List[int]) -> int:
frequency = {}
for i in arr:
if i not in frequency:
frequency[i] = 1
else:
frequency[i] += 1
frequency =sorted(frequency.items(), key=lambda x: x[1], reverse=... | reduce-array-size-to-the-half | Using Sorted Hashmap | Python | Abhi_-_- | 0 | 3 | reduce array size to the half | 1,338 | 0.697 | Medium | 20,114 |
https://leetcode.com/problems/reduce-array-size-to-the-half/discuss/2443572/python-WITH-EXPLANATION | class Solution:
def minSetSize(self, arr: List[int]) -> int:
#converts iterable object into a dictionary
hash_set = Counter(arr)
#two ways to sort the dictionary in reverse
#hash_set = dict(sorted(hash_set.items(), key=operator.itemgetter(1),reverse=True))
#... | reduce-array-size-to-the-half | python WITH EXPLANATION | akashp2001 | 0 | 12 | reduce array size to the half | 1,338 | 0.697 | Medium | 20,115 |
https://leetcode.com/problems/reduce-array-size-to-the-half/discuss/2442827/Reduce-Array-Size-to-The-Half | class Solution:
def minSetSize(self, arr: List[int]) -> int:
cnt = Counter(arr)
frequencies = list(cnt.values())
frequencies.sort()
ans, removed, half = 0, 0, len(arr) // 2
while removed < half:
ans += 1
removed += frequencies.pop()
re... | reduce-array-size-to-the-half | Reduce Array Size to The Half | klu_2100031497 | 0 | 7 | reduce array size to the half | 1,338 | 0.697 | Medium | 20,116 |
https://leetcode.com/problems/reduce-array-size-to-the-half/discuss/2442542/Python-Counter-Easy-understand | class Solution:
def minSetSize(self, arr: List[int]) -> int:
n = len(arr) // 2
count = Counter(arr)
res = 0
for i, c in sorted(count.items(), key = lambda k: -k[1]):
n -= c
res += 1
if n <= 0:
return res | reduce-array-size-to-the-half | Python Counter Easy understand | Kennyyhhu | 0 | 3 | reduce array size to the half | 1,338 | 0.697 | Medium | 20,117 |
https://leetcode.com/problems/reduce-array-size-to-the-half/discuss/2442394/Python-easy-solution-for-beginners-using-Counter | class Solution:
def minSetSize(self, arr: List[int]) -> int:
freq_dict = Counter(arr)
freq = sorted([x for x in freq_dict.values()], reverse=True)
i = 0
res = 0
original_size = len(arr)
while original_size > len(arr) // 2:
original_size -= freq[i]
... | reduce-array-size-to-the-half | Python easy solution for beginners using Counter | alishak1999 | 0 | 7 | reduce array size to the half | 1,338 | 0.697 | Medium | 20,118 |
https://leetcode.com/problems/reduce-array-size-to-the-half/discuss/2442384/Python-Easy-Solution-using-Counter | class Solution:
def minSetSize(self, arr: List[int]) -> int:
c= Counter(arr)
f= list(c.values())
f.sort()
res, rem, half= 0,0, len(arr)//2
while (rem<half):
res=res+1
rem+= f.pop()
return res | reduce-array-size-to-the-half | Python Easy Solution using Counter | trickycat10 | 0 | 11 | reduce array size to the half | 1,338 | 0.697 | Medium | 20,119 |
https://leetcode.com/problems/reduce-array-size-to-the-half/discuss/2442274/Python-easy-solution | class Solution:
def minSetSize(self, arr: List[int]) -> int:
occurrence = {}
n = 0
i = 1
for num in arr:
if num not in occurrence:
occurrence[num] = 1
else:
occurrence[num] += 1
occurrence = dict(sorted(occurrence.items... | reduce-array-size-to-the-half | Python easy solution | dean_kuo | 0 | 7 | reduce array size to the half | 1,338 | 0.697 | Medium | 20,120 |
https://leetcode.com/problems/reduce-array-size-to-the-half/discuss/2442252/CPP-JAVA-PYTHON3-oror-Simple-Solution-oror-Greedy-Approach | class Solution:
def minSetSize(self, arr: List[int]) -> int:
c=Counter(arr)
a=list(c.values())
a.sort()
p=sum(a)
p2=p//2
c=0
a=a[::-1]
i=0
while p2>0:
p2-=a[i]
i+=1
c+=1
return c | reduce-array-size-to-the-half | [CPP, JAVA, PYTHON3] || Simple Solution || Greedy Approach | WhiteBeardPirate | 0 | 4 | reduce array size to the half | 1,338 | 0.697 | Medium | 20,121 |
https://leetcode.com/problems/reduce-array-size-to-the-half/discuss/2442187/python3-using-heap | class Solution:
def minSetSize(self, arr: List[int]) -> int:
threshold = len(arr) // 2
counter = collections.Counter(arr)
heap = []
for k,v in counter.items():
heapq.heappush(heap,-v)
count, ans = 0,0
while count < threshold:
count -= heapq.he... | reduce-array-size-to-the-half | python3, using heap | pjy953 | 0 | 4 | reduce array size to the half | 1,338 | 0.697 | Medium | 20,122 |
https://leetcode.com/problems/reduce-array-size-to-the-half/discuss/2442077/Python3-Counter-and-Sort-O(n*logn)-time-O(n)-space | class Solution:
def minSetSize(self, arr: List[int]) -> int:
"""
Logic: Counter
Time: O(n*logn)
Space: O(n)
"""
counter = collections.Counter(arr)
counter = dict(sorted(counter.items(), key=lambda x: x[1], reverse=True))
removed = 0
... | reduce-array-size-to-the-half | [Python3] Counter & Sort - O(n*logn) time, O(n) space | hanelios | 0 | 3 | reduce array size to the half | 1,338 | 0.697 | Medium | 20,123 |
https://leetcode.com/problems/reduce-array-size-to-the-half/discuss/2442075/Python-3-Most-Common | class Solution:
def minSetSize(self, arr: list[int]) -> int:
n, deleted, ans = len(arr), 0, 0
for _, count in Counter(arr).most_common(n):
deleted, ans = deleted + count, ans + 1
if deleted >= n // 2:
return ans
return -1 | reduce-array-size-to-the-half | Python 3 Most Common | cerocha | 0 | 8 | reduce array size to the half | 1,338 | 0.697 | Medium | 20,124 |
https://leetcode.com/problems/reduce-array-size-to-the-half/discuss/2441964/Python-or-HashMap-or-Easy-to-Understand | class Solution:
def minSetSize(self, arr: List[int]) -> int:
target = (len(arr) + 1) // 2
count = Counter(arr)
occurance = list(count.values())
occurance.sort(reverse = True)
result = 0
curSum = 0
if len(occurance) == 1:
return 1
... | reduce-array-size-to-the-half | Python | HashMap | Easy to Understand | Mikey98 | 0 | 15 | reduce array size to the half | 1,338 | 0.697 | Medium | 20,125 |
https://leetcode.com/problems/reduce-array-size-to-the-half/discuss/2441911/Python-Easy-to-understand-heap-solution | class Solution:
def minSetSize(self, arr: List[int]) -> int:
# count number of instances of each number in arr
counts = list(Counter(arr).items())
# create max heap
heap = [(count*(-1),c) for c,count in counts]
heapq.heapify(heap)
# remove largest items from heap until we've remo... | reduce-array-size-to-the-half | [Python] Easy to understand heap solution | fomiee | 0 | 9 | reduce array size to the half | 1,338 | 0.697 | Medium | 20,126 |
https://leetcode.com/problems/reduce-array-size-to-the-half/discuss/2441871/Python-easy-understanding-solution. | class Solution:
def minSetSize(self, arr: List[int]) -> int:
c = list(Counter(arr).values()) # We don't care the exact number, we only care about how many times a number appear
c.sort(reverse=True) # Dealing with the most frequent number first.
target = ceil(len(arr)/2) ... | reduce-array-size-to-the-half | Python easy-understanding solution. | byroncharly3 | 0 | 12 | reduce array size to the half | 1,338 | 0.697 | Medium | 20,127 |
https://leetcode.com/problems/reduce-array-size-to-the-half/discuss/2441716/Python-Solution-Fast-than-95-TC-O(n-log-n) | class Solution:
def minSetSize(self, arr: List[int]) -> int:
size = len(arr)
target = math.floor(size/2)
count = {}
for n in arr:
if n in count: count[n] +=1
else: count[n] = 1
h = []
for v in count.values():
heapq.heappus... | reduce-array-size-to-the-half | Python Solution Fast than 95% TC O(n log n) | EnergyBoy | 0 | 18 | reduce array size to the half | 1,338 | 0.697 | Medium | 20,128 |
https://leetcode.com/problems/reduce-array-size-to-the-half/discuss/2441702/Python-heap.-O(n-%2B-k-log-n) | class Solution:
def minSetSize(self, arr: List[int]) -> int:
h = [-c for c in Counter(arr).values()]
heapq.heapify(h)
size = len(arr)
half = (size + 1) // 2
result = 0
while size > half:
size += heapq.heappop(h)
result += 1
... | reduce-array-size-to-the-half | Python, heap. O(n + k log n) | blue_sky5 | 0 | 9 | reduce array size to the half | 1,338 | 0.697 | Medium | 20,129 |
https://leetcode.com/problems/maximum-product-of-splitted-binary-tree/discuss/496700/Python3-post-order-dfs | class Solution:
def maxProduct(self, root: Optional[TreeNode]) -> int:
vals = []
def fn(node):
"""Return sum of sub-tree."""
if not node: return 0
ans = node.val + fn(node.left) + fn(node.right)
vals.append(ans)
return ans
... | maximum-product-of-splitted-binary-tree | [Python3] post-order dfs | ye15 | 39 | 1,500 | maximum product of splitted binary tree | 1,339 | 0.434 | Medium | 20,130 |
https://leetcode.com/problems/maximum-product-of-splitted-binary-tree/discuss/496700/Python3-post-order-dfs | class Solution:
def maxProduct(self, root: Optional[TreeNode]) -> int:
stack, vals = [], []
node, prev = root, None
mp = defaultdict(int)
while node or stack:
if node:
stack.append(node)
node = node.left
else:
... | maximum-product-of-splitted-binary-tree | [Python3] post-order dfs | ye15 | 39 | 1,500 | maximum product of splitted binary tree | 1,339 | 0.434 | Medium | 20,131 |
https://leetcode.com/problems/maximum-product-of-splitted-binary-tree/discuss/1413108/Python-Explained-Solution-with-Diagrams-oror-Beats-99 | class Solution:
def maxProduct(self, root: Optional[TreeNode]) -> int:
sums = []
def dfs(node):
if node is None:
return 0
subtree_sum = dfs(node.left) + dfs(node.right) + node.val
sums.append(subtree_sum)
return subtree_sum
... | maximum-product-of-splitted-binary-tree | [Python] Explained Solution with Diagrams || Beats 99% | sevdariklejdi | 13 | 480 | maximum product of splitted binary tree | 1,339 | 0.434 | Medium | 20,132 |
https://leetcode.com/problems/maximum-product-of-splitted-binary-tree/discuss/1413919/97-faster-oror-Clean-and-Simple-oror-Easy-to-understand-oror-Well-Explained | class Solution:
def maxProduct(self, root: Optional[TreeNode]) -> int:
def dfs(root):
if root is None:
return 0
ans = dfs(root.left)+dfs(root.right)+root.val
res.append(ans)
return ans
res=[]
dfs(root)
total,m = max(res),float('-inf')
for s ... | maximum-product-of-splitted-binary-tree | 🐍 97% faster || Clean & Simple || Easy to understand || Well Explained 📌📌 | abhi9Rai | 2 | 110 | maximum product of splitted binary tree | 1,339 | 0.434 | Medium | 20,133 |
https://leetcode.com/problems/maximum-product-of-splitted-binary-tree/discuss/1903169/Python-3-or-Clean-and-concise | class Solution:
def maxProduct(self, root: Optional[TreeNode]) -> int:
def solve(root):
if root.left:
x=solve(root.left)
else:
x=0
if root.right:
y=solve(root.right)
else:
y=0
root.val... | maximum-product-of-splitted-binary-tree | Python 3 | Clean and concise | RickSanchez101 | 1 | 249 | maximum product of splitted binary tree | 1,339 | 0.434 | Medium | 20,134 |
https://leetcode.com/problems/maximum-product-of-splitted-binary-tree/discuss/1413994/Python3-recursive-post-order-dfs-solution | class Solution:
def sumedTree(self, node):
if node == None:
return 0
left_sum = self.sumedTree(node.left)
right_sum = self.sumedTree(node.right)
node.val += left_sum + right_sum
return node.val
def cutEdge(self, nod... | maximum-product-of-splitted-binary-tree | [Python3] recursive post-order dfs solution | maosipov11 | 1 | 37 | maximum product of splitted binary tree | 1,339 | 0.434 | Medium | 20,135 |
https://leetcode.com/problems/maximum-product-of-splitted-binary-tree/discuss/1413576/Python-Solution | class Solution:
def maxProduct(self, root: Optional[TreeNode]) -> int:
'''
1. build a sum tree
2. check for max product
'''
mod = pow(10,9)+7
def recur(node):
if node==None:
return 0
left,right = recur(node.left),recur(node.righ... | maximum-product-of-splitted-binary-tree | [Python] Solution | SaSha59 | 1 | 94 | maximum product of splitted binary tree | 1,339 | 0.434 | Medium | 20,136 |
https://leetcode.com/problems/maximum-product-of-splitted-binary-tree/discuss/1415850/Python-3-iterative-and-simple-Memory-Usage%3A-34.6-MB-less-than-100.00 | class Solution:
def maxProduct(self, root: Optional[TreeNode]) -> int:
sums = []
stack = [(root, False)]
hm = {}
while stack:
node, vizited = stack.pop()
if vizited:
s = node.val + hm.pop(node.left, 0) + hm.pop(node.right, 0)
... | maximum-product-of-splitted-binary-tree | Python 3, iterative and simple, Memory Usage: 34.6 MB, less than 100.00% | MihailP | 0 | 84 | maximum product of splitted binary tree | 1,339 | 0.434 | Medium | 20,137 |
https://leetcode.com/problems/maximum-product-of-splitted-binary-tree/discuss/1414838/Easy-to-understand-Python3-(beating-95%2B-in-both-speed-and-memory) | class Solution:
def maxProduct(self, root: Optional[TreeNode]) -> int:
self.pre_order = []
total = self.subtree_sum(root)
ans = 0
for partial in self.pre_order:
candid = (total - partial) * partial
if candid > ans:
ans = candid
... | maximum-product-of-splitted-binary-tree | Easy to understand Python3 (beating 95+ % in both speed and memory) | smohsensh | 0 | 34 | maximum product of splitted binary tree | 1,339 | 0.434 | Medium | 20,138 |
https://leetcode.com/problems/maximum-product-of-splitted-binary-tree/discuss/1414826/JavascriptPython-Solution | class Solution:
def maxProduct(self, root: Optional[TreeNode]) -> int:
self.total = 0
def totalSumOfNodes(root):
if (root == None): return
self.total += root.val
totalSumOfNodes(root.left)
totalSumOfNodes(root.right)
totalSumOfNodes(r... | maximum-product-of-splitted-binary-tree | Javascript/Python Solution | Kurosakicoder | 0 | 45 | maximum product of splitted binary tree | 1,339 | 0.434 | Medium | 20,139 |
https://leetcode.com/problems/maximum-product-of-splitted-binary-tree/discuss/1414597/Python3-Simple-Readable-Recursive-Solution | class Solution:
def maxProduct(self, root: Optional[TreeNode]) -> int:
self.max = treeSum = 0
def sum(root):
if (not root):
return 0
subtreeSum = root.val + sum(root.left) + sum(root.right)
product = (treeSum - subtreeSum) * subtreeSum
... | maximum-product-of-splitted-binary-tree | Python3 - Simple Readable Recursive Solution ✅ | Bruception | 0 | 42 | maximum product of splitted binary tree | 1,339 | 0.434 | Medium | 20,140 |
https://leetcode.com/problems/maximum-product-of-splitted-binary-tree/discuss/1414540/Python-with-explanation-18-lines-Runtime%3A-268-ms-(100) | class Solution:
def maxProduct(self, root: Optional[TreeNode]) -> int:
subtree_totals = []
def calc_nodes(node):
if node == None:
return 0
subtree_totals.append(node.val + calc_nodes(node.left) + calc_nodes(node.right))
return subtree_totals[-1]
calc_nodes(root)
target = subtree_totals[-1] / 2
... | maximum-product-of-splitted-binary-tree | [Python] with explanation / 18 lines / Runtime: 268 ms (100%) | tan14142 | 0 | 22 | maximum product of splitted binary tree | 1,339 | 0.434 | Medium | 20,141 |
https://leetcode.com/problems/maximum-product-of-splitted-binary-tree/discuss/1414366/Python-Solution-Fast | class Solution:
def AllSums(self, node):
leftSum = rightSum = 0
if node.left:
leftSum = self.AllSums(node.left)
if node.right:
rightSum = self.AllSums(node.right)
treeSum = node.val + leftSum + rightSum
self.nodeSums... | maximum-product-of-splitted-binary-tree | Python Solution Fast | peatear-anthony | 0 | 39 | maximum product of splitted binary tree | 1,339 | 0.434 | Medium | 20,142 |
https://leetcode.com/problems/maximum-product-of-splitted-binary-tree/discuss/1413086/Python3-Thinking-process-of-my-solution | class Solution:
def add_child(self, root):
if(not root): return 0
root.val += self.add_child(root.left)+self.add_child(root.right)
self.sum_list.append(root.val)
return root.val
def maxProduct(self, root: Optional[TreeNode]) -> int:
self.sum_list = [] # store all of the ... | maximum-product-of-splitted-binary-tree | [Python3] Thinking process of my solution | potpotpotpotpotpotpot | 0 | 27 | maximum product of splitted binary tree | 1,339 | 0.434 | Medium | 20,143 |
https://leetcode.com/problems/maximum-product-of-splitted-binary-tree/discuss/834769/Python3-BFS%2BDFS-(Recursive) | class Solution:
def maxProduct(self, root: TreeNode) -> int:
total_sum=0
def bfs(node):
nonlocal total_sum
if node:
total_sum+=node.val
bfs(node.left)
bfs(node.right)
bfs(root) # Find the total_sum of ... | maximum-product-of-splitted-binary-tree | Python3 BFS+DFS (Recursive) | harshitCode13 | 0 | 127 | maximum product of splitted binary tree | 1,339 | 0.434 | Medium | 20,144 |
https://leetcode.com/problems/maximum-product-of-splitted-binary-tree/discuss/496601/Python-3-(DFS)-(beats-100)-(nine-lines) | class Solution:
def maxProduct(self, R: TreeNode) -> int:
SN, M = [], 10**9 + 7
def SumTree(R):
if R == None: return 0
SN.append(R.val + SumTree(R.left) + SumTree(R.right))
return SN[-1]
SumTree(R)
S, _ = SN[-1], SN.sort()
for i,s in enumer... | maximum-product-of-splitted-binary-tree | Python 3 (DFS) (beats 100%) (nine lines) | junaidmansuri | 0 | 258 | maximum product of splitted binary tree | 1,339 | 0.434 | Medium | 20,145 |
https://leetcode.com/problems/maximum-product-of-splitted-binary-tree/discuss/496536/Python-3-Find-subtree-with-sum-closest-to-half-of-root-tree-sum. | class Solution:
def maxProduct(self, root: TreeNode) -> int:
subtreeSums = set()
def getSum(node):
if not node:
return 0
elif not node.left and not node.right:
subtreeSums.add(node.val)
return node.val
else:... | maximum-product-of-splitted-binary-tree | [Python 3] Find subtree with sum closest to half of root tree sum. | vilchinsky | 0 | 167 | maximum product of splitted binary tree | 1,339 | 0.434 | Medium | 20,146 |
https://leetcode.com/problems/jump-game-v/discuss/1670065/Well-Coded-and-Easy-Explanation-oror-Use-of-Memoization | class Solution:
def maxJumps(self, arr: List[int], d: int) -> int:
dp = defaultdict(int)
def dfs(i):
if i in dp: return dp[i]
m_path = 0
for j in range(i+1,i+d+1):
if j>=len(arr) or arr[j]>=arr[i]: break
m_path = max(m_path,dfs(j))
for j in range(i-1,i-d-1,-1):
if j<0 or arr[j]>=arr[i]: b... | jump-game-v | 📌📌 Well-Coded and Easy Explanation || Use of Memoization 🐍 | abhi9Rai | 5 | 173 | jump game v | 1,340 | 0.625 | Hard | 20,147 |
https://leetcode.com/problems/jump-game-v/discuss/2443846/Easy-Explained-Python-Soln.-time%3A-O(n*d)-space%3AO(n) | class Solution:
def maxJumps(self, nums: List[int], d: int) -> int:
N = len(nums)
seen = set() # seen for lookup, to memoize
dp = [1]*N # stores the values of jump we can make from Ith index in DP. # minimum being 1 jump (i.e its self)
d... | jump-game-v | Easy, Explained Python Soln. time: O(n*d) space:O(n) | notxkaran | 1 | 54 | jump game v | 1,340 | 0.625 | Hard | 20,148 |
https://leetcode.com/problems/jump-game-v/discuss/2703901/Python-DP | class Solution:
def maxJumps(self, arr: List[int], d: int) -> int:
g = defaultdict(list)
for i in range(len(arr)):
for j in range(i + 1, min(len(arr), i + d + 1)):
if arr[j] >= arr[i]:
break
g[i].append(j)
... | jump-game-v | Python DP | JSTM2022 | 0 | 4 | jump game v | 1,340 | 0.625 | Hard | 20,149 |
https://leetcode.com/problems/jump-game-v/discuss/2358863/Python3-or-DP-or-Top-Down-Approach | class Solution:
def maxJumps(self, arr: List[int], d: int) -> int:
n=len(arr)
ans=-float('inf')
dp=[-1 for i in range(n)]
def dfs(ind):
if dp[ind]!=-1:
return dp[ind]
v1,v2=0,0
for left in range(ind-1,max(-1,ind-d-1),-1):
... | jump-game-v | [Python3] | DP | Top-Down Approach | swapnilsingh421 | 0 | 16 | jump game v | 1,340 | 0.625 | Hard | 20,150 |
https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/discuss/2738381/Python-Elegant-and-Short-or-O(1)-or-Recursive-Iterative-Bit-Manipulation | class Solution:
"""
Time: O(log(n))
Memory: O(log(n))
"""
def numberOfSteps(self, num: int) -> int:
if num == 0:
return 0
return 1 + self.numberOfSteps(num - 1 if num & 1 else num >> 1) | number-of-steps-to-reduce-a-number-to-zero | Python Elegant & Short | O(1) | Recursive / Iterative / Bit Manipulation | Kyrylo-Ktl | 28 | 1,300 | number of steps to reduce a number to zero | 1,342 | 0.854 | Easy | 20,151 |
https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/discuss/2738381/Python-Elegant-and-Short-or-O(1)-or-Recursive-Iterative-Bit-Manipulation | class Solution:
"""
Time: O(log(n))
Memory: O(1)
"""
def numberOfSteps(self, num: int) -> int:
steps = 0
while num != 0:
steps += 1
if num & 1:
num -= 1
else:
num >>= 1
return steps | number-of-steps-to-reduce-a-number-to-zero | Python Elegant & Short | O(1) | Recursive / Iterative / Bit Manipulation | Kyrylo-Ktl | 28 | 1,300 | number of steps to reduce a number to zero | 1,342 | 0.854 | Easy | 20,152 |
https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/discuss/2738381/Python-Elegant-and-Short-or-O(1)-or-Recursive-Iterative-Bit-Manipulation | class Solution:
"""
Time: O(1)
Memory: O(1)
"""
def numberOfSteps(self, num: int) -> int:
if num == 0:
return 0
return num.bit_length() - 1 + num.bit_count() | number-of-steps-to-reduce-a-number-to-zero | Python Elegant & Short | O(1) | Recursive / Iterative / Bit Manipulation | Kyrylo-Ktl | 28 | 1,300 | number of steps to reduce a number to zero | 1,342 | 0.854 | Easy | 20,153 |
https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/discuss/2272942/PYTHON-3-FAST-or-SIMPLE-or-EASY-TO-UNDERSTAND | class Solution:
def numberOfSteps(self, num: int) -> int:
c = 0
while num != 0:
if num % 2 == 0:
num /= 2
else:
num -= 1
c += 1
return c | number-of-steps-to-reduce-a-number-to-zero | [PYTHON 3] FAST | SIMPLE | EASY TO UNDERSTAND | omkarxpatel | 10 | 498 | number of steps to reduce a number to zero | 1,342 | 0.854 | Easy | 20,154 |
https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/discuss/505915/2-python-log(n)-by-simulationmath-analysis.-w-Explanation | class Solution:
table = dict()
def numberOfSteps (self, num: int) -> int:
step = 0
while num != 0 :
step += 1
if num & 1 == 1:
# odd number, subtract by 1
num -= 1
else:
# ... | number-of-steps-to-reduce-a-number-to-zero | 2 python log(n) by simulation//math analysis. [w/ Explanation ] | brianchiang_tw | 7 | 656 | number of steps to reduce a number to zero | 1,342 | 0.854 | Easy | 20,155 |
https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/discuss/505915/2-python-log(n)-by-simulationmath-analysis.-w-Explanation | class Solution:
def numberOfSteps (self, num: int) -> int:
if not num:
return 0
else:
bit_string = bin(num)[2:]
return len(bit_string) + bit_string.count('1') - 1 | number-of-steps-to-reduce-a-number-to-zero | 2 python log(n) by simulation//math analysis. [w/ Explanation ] | brianchiang_tw | 7 | 656 | number of steps to reduce a number to zero | 1,342 | 0.854 | Easy | 20,156 |
https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/discuss/2323739/Python-Simple-faster-solution-oror-Bitwise-Operators | class Solution:
# Time Complexity O(log n)
# Space Complexity O(1)
def numberOfSteps(self, num: int) -> int:
steps = 0
while num != 0:
if num & 1 == 0:
num >>= 1
else:
num -= 1
steps += 1
return step... | number-of-steps-to-reduce-a-number-to-zero | [Python] Simple faster solution || Bitwise Operators | Buntynara | 4 | 104 | number of steps to reduce a number to zero | 1,342 | 0.854 | Easy | 20,157 |
https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/discuss/2117432/faster-than-97.84-of-Python3 | class Solution:
def numberOfSteps(self, num: int) -> int:
return bin(num).count('1') *2 + bin(num).count('0') - 2 | number-of-steps-to-reduce-a-number-to-zero | faster than 97.84% of Python3 | writemeom | 4 | 223 | number of steps to reduce a number to zero | 1,342 | 0.854 | Easy | 20,158 |
https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/discuss/939796/Easy-Python-Solution | class Solution:
def numberOfSteps (self, num: int) -> int:
return bin(num).count('1')*2 + bin(num).count('0') - 2 | number-of-steps-to-reduce-a-number-to-zero | Easy Python Solution | lokeshsenthilkumar | 3 | 433 | number of steps to reduce a number to zero | 1,342 | 0.854 | Easy | 20,159 |
https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/discuss/505127/Python-3-(three-lines)-(24-ms) | class Solution:
def numberOfSteps (self, n: int) -> int:
c = 0
while n != 0: n, c = n - 1 if n % 2 else n//2, c + 1
return c
- Junaid Mansuri
- Chicago, IL | number-of-steps-to-reduce-a-number-to-zero | Python 3 (three lines) (24 ms) | junaidmansuri | 3 | 2,000 | number of steps to reduce a number to zero | 1,342 | 0.854 | Easy | 20,160 |
https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/discuss/2453268/Python-3-88-Faster-solution | class Solution:
def numberOfSteps(self, num: int) -> int:
steps = 0
while num:
num = num // 2 if num % 2 == 0 else num - 1
steps = steps + 1
return steps | number-of-steps-to-reduce-a-number-to-zero | Python 3 - 88% Faster solution | hassamboi | 1 | 36 | number of steps to reduce a number to zero | 1,342 | 0.854 | Easy | 20,161 |
https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/discuss/2175650/Easy-Python-3.10-one-liner | class Solution:
def numberOfSteps(self, num: int) -> int:
return max((num.bit_length() - 1) + num.bit_count(), 0) # max() used for edge case where num = 0 | number-of-steps-to-reduce-a-number-to-zero | Easy Python 3.10 one-liner | RandomChemist | 1 | 49 | number of steps to reduce a number to zero | 1,342 | 0.854 | Easy | 20,162 |
https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/discuss/2175650/Easy-Python-3.10-one-liner | class Solution:
def numberOfSteps(self, num: int) -> int:
return max((len(bin(num).lstrip('-0b')) - 1) + bin(num).count("1"), 0) # max() used for edge case where num = 0 | number-of-steps-to-reduce-a-number-to-zero | Easy Python 3.10 one-liner | RandomChemist | 1 | 49 | number of steps to reduce a number to zero | 1,342 | 0.854 | Easy | 20,163 |
https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/discuss/2078833/Simple-python-solution | class Solution:
def numberOfSteps(self, num: int) -> int:
sum=0
while num>0:
if num%2==0:
num=num/2
sum+=1
else:
num=num-1
sum+=1
return sum | number-of-steps-to-reduce-a-number-to-zero | Simple python solution | HadaEn | 1 | 13 | number of steps to reduce a number to zero | 1,342 | 0.854 | Easy | 20,164 |
https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/discuss/1948905/Python-Iterative-and-Recursive-%2B-Bonus%3A-One-Liner! | class Solution:
def numberOfSteps(self, num):
steps = 0
while num:
match num % 2:
case 0: num //= 2
case 1: num -= 1
steps += 1
return steps | number-of-steps-to-reduce-a-number-to-zero | Python - Iterative and Recursive + Bonus: One-Liner! | domthedeveloper | 1 | 167 | number of steps to reduce a number to zero | 1,342 | 0.854 | Easy | 20,165 |
https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/discuss/1948905/Python-Iterative-and-Recursive-%2B-Bonus%3A-One-Liner! | class Solution:
def numberOfSteps(self, num, steps=0):
if not num:
return steps
else:
match num % 2:
case 0: return self.numberOfSteps(num//2, steps+1)
case 1: return self.numberOfSteps(num-1, steps+1) | number-of-steps-to-reduce-a-number-to-zero | Python - Iterative and Recursive + Bonus: One-Liner! | domthedeveloper | 1 | 167 | number of steps to reduce a number to zero | 1,342 | 0.854 | Easy | 20,166 |
https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/discuss/1948905/Python-Iterative-and-Recursive-%2B-Bonus%3A-One-Liner! | class Solution:
def numberOfSteps(self, n, s=0):
return s if not n else self.numberOfSteps(n-1,s+1) if n%2 else self.numberOfSteps(n//2,s+1) | number-of-steps-to-reduce-a-number-to-zero | Python - Iterative and Recursive + Bonus: One-Liner! | domthedeveloper | 1 | 167 | number of steps to reduce a number to zero | 1,342 | 0.854 | Easy | 20,167 |
https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/discuss/1794982/Python3-or-brute-force-or | class Solution:
def numberOfSteps(self, n: int) -> int:
ct=0
while n:
if n%2==0:
n=n/2
ct+=1
else:
n-=1
ct+=1
return ct | number-of-steps-to-reduce-a-number-to-zero | Python3 | brute force | | Anilchouhan181 | 1 | 57 | number of steps to reduce a number to zero | 1,342 | 0.854 | Easy | 20,168 |
https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/discuss/1760259/1342-ez-python-solution | class Solution(object):
def numberOfSteps(self, num):
steps = 0
while num != 0:
if not num%2:
num /= 2
else:
num -= 1
steps += 1
return steps | number-of-steps-to-reduce-a-number-to-zero | 1342 - ez python solution | ankit61d | 1 | 50 | number of steps to reduce a number to zero | 1,342 | 0.854 | Easy | 20,169 |
https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/discuss/1555563/Python-Easy-Solution-or-Faster-than-96 | class Solution:
def numberOfSteps(self, num: int) -> int:
if num == 0:
return 0
c = 0
while num > 0:
if num & 1 == 0:
num //= 2
else:
num -= 1
c += 1
return c | number-of-steps-to-reduce-a-number-to-zero | Python Easy Solution | Faster than 96% | leet_satyam | 1 | 117 | number of steps to reduce a number to zero | 1,342 | 0.854 | Easy | 20,170 |
https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/discuss/616885/Easy-Python-24ms-Explained | class Solution:
def numberOfSteps (self, num: int) -> int:
numi =0
while num!=0:
if num%2==0:
num=num//2
else:
num = num-1
numi +=1
return numi | number-of-steps-to-reduce-a-number-to-zero | Easy Python [24ms] Explained | code_zero | 1 | 217 | number of steps to reduce a number to zero | 1,342 | 0.854 | Easy | 20,171 |
https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/discuss/2845041/python3-logical-and-easier-way-understandable-solution-of-1342 | class Solution:
def numberOfSteps(self, num: int) -> int:
b = True #used for while loop if true loop run if false loop stop
i=0 #the step counter it will count the steps
while b:
# while loop started
if(num == 0):
b = False # if the value is equal to ... | number-of-steps-to-reduce-a-number-to-zero | python3 logical and easier way, understandable solution of 1342 | shakilofficial0 | 0 | 2 | number of steps to reduce a number to zero | 1,342 | 0.854 | Easy | 20,172 |
https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/discuss/2832209/Easy-Python | class Solution:
def numberOfSteps(self, num: int) -> int:
steps = 0
while num != 0:
if num % 2 == 0:
num /= 2
steps += 1
else:
num -= 1
steps += 1
return steps | number-of-steps-to-reduce-a-number-to-zero | Easy Python | corylynn | 0 | 1 | number of steps to reduce a number to zero | 1,342 | 0.854 | Easy | 20,173 |
https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/discuss/2830513/Me | class Solution:
def numberOfSteps(self, num: int) -> int:
result = 0
while num !=0 :
if num%2==0:
result+=1
num /= 2
else:
result +=1
num -=1
return result | number-of-steps-to-reduce-a-number-to-zero | Me | Hristo2076 | 0 | 1 | number of steps to reduce a number to zero | 1,342 | 0.854 | Easy | 20,174 |
https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/discuss/2830426/Steps-to-zero-solution | class Solution:
def numberOfSteps(self, num: int) -> int:
s = 0
while num != 0:
s = s + 1
if num % 2 == 0:
num = num / 2
else:
num = num - 1
return s | number-of-steps-to-reduce-a-number-to-zero | Steps to zero - solution | charlvdmerwe06 | 0 | 1 | number of steps to reduce a number to zero | 1,342 | 0.854 | Easy | 20,175 |
https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/discuss/2827975/Bit-Counting-The-Best | class Solution:
def numberOfSteps(self, num: int) -> int:
return (num.bit_count() + num.bit_length() or 1) - 1 | number-of-steps-to-reduce-a-number-to-zero | Bit Counting, The Best | Triquetra | 0 | 1 | number of steps to reduce a number to zero | 1,342 | 0.854 | Easy | 20,176 |
https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/discuss/2818335/The-simplest | class Solution:
def numberOfSteps(self, num: int) -> int:
counter = 0
while num != 0:
if num % 2 == 0:
num = num / 2
counter += 1
else:
num -= 1
counter += 1
return counter | number-of-steps-to-reduce-a-number-to-zero | The simplest | pkozhem | 0 | 1 | number of steps to reduce a number to zero | 1,342 | 0.854 | Easy | 20,177 |
https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/discuss/2812670/python-easy-solution | class Solution:
def numberOfSteps(self, num: int) -> int:
c=0
while(num!=0):
if(num%2==0):
c=c+1
num=num//2
else:
num=num-1
c=c+1
return c | number-of-steps-to-reduce-a-number-to-zero | python easy solution | Manjeet_Malik | 0 | 2 | number of steps to reduce a number to zero | 1,342 | 0.854 | Easy | 20,178 |
https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/discuss/2807774/Python-solution-for-Number-of-Steps-to-Reduce-a-Number-to-Zero | class Solution:
def numberOfSteps(self, num: int) -> int:
steps = 0
while num > 0:
if not (num & 1):
num >>= 1
else:
num -= 1
steps += 1
return steps | number-of-steps-to-reduce-a-number-to-zero | Python solution for Number of Steps to Reduce a Number to Zero | Bassel_Alf | 0 | 1 | number of steps to reduce a number to zero | 1,342 | 0.854 | Easy | 20,179 |
https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/discuss/2787297/python3-T-O(logn)-S-O(1) | class Solution:
def numberOfSteps(self, num: int) -> int:
stepsTillNow=0
while num!=0:
if num%2==0:
num/=2
stepsTillNow+=1
else:
num-=1
stepsTillNow+=1
return stepsTillNow | number-of-steps-to-reduce-a-number-to-zero | python3 - T-O(logn) , S-O(1) | fancyuserid | 0 | 1 | number of steps to reduce a number to zero | 1,342 | 0.854 | Easy | 20,180 |
https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/discuss/2779436/Python-Simple-Solution | class Solution:
def numberOfSteps(self, num: int) -> int:
steps = 0
while num != 0:
if num%2 == 0:
num = num/2
else:
num = num-1
steps += 1
return steps | number-of-steps-to-reduce-a-number-to-zero | Python Simple Solution | sdsahil12 | 0 | 1 | number of steps to reduce a number to zero | 1,342 | 0.854 | Easy | 20,181 |
https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/discuss/2776522/Pythonic-Crisp-Solution! | class Solution:
def numberOfSteps(self, num: int) -> int:
count = 0
while num > 0:
if num % 2 == 0:
num = num // 2
count += 1
elif num % 2 == 1:
num = num - 1
count += 1
return count | number-of-steps-to-reduce-a-number-to-zero | Pythonic Crisp Solution! | arifaisal123 | 0 | 1 | number of steps to reduce a number to zero | 1,342 | 0.854 | Easy | 20,182 |
https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/discuss/2770158/Easy-python-solution-using-while-loop | class Solution:
def numberOfSteps(self, num: int) -> int:
count=0
while(num!=0):
if num%2 ==0:
num=num/2
else:
num-=1
count+=1
return count | number-of-steps-to-reduce-a-number-to-zero | Easy python solution using while loop | user7798V | 0 | 1 | number of steps to reduce a number to zero | 1,342 | 0.854 | Easy | 20,183 |
https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/discuss/2750485/python-solution-using-while-loop | class Solution:
def numberOfSteps(self, num: int) -> int:
count = 0
while num != 0:
if num % 2 == 0:
num = num // 2
else:
num -= 1
count += 1
return count | number-of-steps-to-reduce-a-number-to-zero | python solution using while loop | samanehghafouri | 0 | 4 | number of steps to reduce a number to zero | 1,342 | 0.854 | Easy | 20,184 |
https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/discuss/2737838/Simple-while-approach-python | class Solution:
def numberOfSteps(self, num: int) -> int:
count = 0
while num != 0:
if num%2 == 0:
num=num//2
else:
num = num-1
count += 1
return count | number-of-steps-to-reduce-a-number-to-zero | Simple while approach - python | DavidCastillo | 0 | 1 | number of steps to reduce a number to zero | 1,342 | 0.854 | Easy | 20,185 |
https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/discuss/2733165/Python3%3A-another-solution-with-binary | class Solution:
def numberOfSteps(self, num: int) -> int:
steps = 0
b = bin(num)
while b != '0b0':
# '0b0' is 0 in decimal.
if b[-1] == '0':
# In that case the number is even.
b = b[:-1]
#Removing the last character .
else:
b... | number-of-steps-to-reduce-a-number-to-zero | Python3: another solution with binary | hafid-hub | 0 | 2 | number of steps to reduce a number to zero | 1,342 | 0.854 | Easy | 20,186 |
https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/discuss/2732495/Python3-solution | class Solution:
def numberOfSteps(self, num: int) -> int:
step = 0
while num != 0:
if num % 2 == 0:
num = num // 2
else:
num -= 1
step += 1
return step | number-of-steps-to-reduce-a-number-to-zero | Python3 solution | hafid-hub | 0 | 3 | number of steps to reduce a number to zero | 1,342 | 0.854 | Easy | 20,187 |
https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/discuss/2717374/Python-One-Line-Solution | class Solution:
def numberOfSteps(self, num: int) -> int:
return 1 + self.numberOfSteps(num // 2 if not num % 2 else num - 1) if num else 0 | number-of-steps-to-reduce-a-number-to-zero | Python One-Line Solution | vobogorod | 0 | 5 | number of steps to reduce a number to zero | 1,342 | 0.854 | Easy | 20,188 |
https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/discuss/2716127/Recursive-Python3-simple-to-understand-and-beats-greater50-of-solutions.-TC%3A-O(logn)-SC%3A-O(1) | class Solution:
def numberOfSteps(self, num: int) -> int:
if num == 0: ## base condition
return 0
if num % 2: ## odd condition
num -= 1
else: ## even condition
num //= 2
return 1 + self.numberOfSteps(num) | number-of-steps-to-reduce-a-number-to-zero | Recursive Python3 simple to understand and beats >50% of solutions. TC: O(logn), SC: O(1) | mwalle | 0 | 5 | number of steps to reduce a number to zero | 1,342 | 0.854 | Easy | 20,189 |
https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/discuss/2716039/Number-of-Steps-to-get-0 | class Solution:
def numberOfSteps(self, num: int) -> int:
numSteps = 0
while num > 0:
if num % 2 == 0:
num /= 2
numSteps += 1
else:
num % 2 != 0
num -= 1
numSteps +=1
return numSteps | number-of-steps-to-reduce-a-number-to-zero | Number of Steps to get 0 | EverydayScriptkiddie | 0 | 2 | number of steps to reduce a number to zero | 1,342 | 0.854 | Easy | 20,190 |
https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/discuss/2710519/Python-classic-loop-while-create-a-counter | class Solution:
def numberOfSteps(self, num: int) -> int:
count = 0
while num != 0:
if num % 2 == 0:
num = num/2
count += 1
else:
num = num - 1
count += 1
return count | number-of-steps-to-reduce-a-number-to-zero | Python #classic loop #while #create a counter | moodkeeper | 0 | 2 | number of steps to reduce a number to zero | 1,342 | 0.854 | Easy | 20,191 |
https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/discuss/2657035/Noob-Python-Solution | class Solution:
def numberOfSteps(self, num: int) -> int:
# c=0
count=0
while(num>0):
if(num%2==0):
num=num/2
count+=1
else:
# c=n-1
num=num-1
count+=1
return count | number-of-steps-to-reduce-a-number-to-zero | Noob Python Solution | Abhisheksoni5975 | 0 | 3 | number of steps to reduce a number to zero | 1,342 | 0.854 | Easy | 20,192 |
https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/discuss/2605180/Python | class Solution:
def numberOfSteps(self, num: int) -> int:
steps = 0
while(num!=0):
if num%2==0:
num//=2
steps+=1
else:
num-=1
steps+=1
return steps | number-of-steps-to-reduce-a-number-to-zero | Python | amansaini1030 | 0 | 86 | number of steps to reduce a number to zero | 1,342 | 0.854 | Easy | 20,193 |
https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/discuss/2597969/Easy-python-solution-faster-than-98.11-of-users | class Solution:
def numberOfSteps(self, num: int) -> int:
count=0
while True:
if num==0:
break
else:
if num%2==0:
num=num/2
count=count+1
elif not num%2==0:
num=num-1
... | number-of-steps-to-reduce-a-number-to-zero | Easy python solution faster than 98.11% of users | mustafapanjiwala | 0 | 93 | number of steps to reduce a number to zero | 1,342 | 0.854 | Easy | 20,194 |
https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/discuss/2521180/Python-88.9-Faster.-Simple-Solution | class Solution:
def numberOfSteps(self, num: int) -> int:
#hold the output value
ovalue = 0
#perform the steps
while num != 0:
if (num %2) == 0:
num = num/2
else:
num = num-1
#count steps
ovalue... | number-of-steps-to-reduce-a-number-to-zero | Python 88.9% Faster. Simple Solution | ovidaure | 0 | 97 | number of steps to reduce a number to zero | 1,342 | 0.854 | Easy | 20,195 |
https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/discuss/2500466/Simple-Recursion-using-Python | class Solution:
def numberOfSteps(self, num: int) -> int:
if num==0:
return 0
elif(num%2 ==0 ):
return 1+ self.numberOfSteps(num//2)
return 1 + self.numberOfSteps(num-1) | number-of-steps-to-reduce-a-number-to-zero | Simple Recursion using Python | ShubhayanS | 0 | 46 | number of steps to reduce a number to zero | 1,342 | 0.854 | Easy | 20,196 |
https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/discuss/2481091/Python3-or-98-faster | class Solution:
def numberOfSteps(self, num: int) -> int:
counter = 0
while num > 0:
num = (num / 2) if (num % 2 == 0) else (num - 1)
counter += 1
return counter | number-of-steps-to-reduce-a-number-to-zero | Python3 | 98% faster | pamellabezerra | 0 | 74 | number of steps to reduce a number to zero | 1,342 | 0.854 | Easy | 20,197 |
https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/discuss/2471722/Python-very-simple | class Solution:
def numberOfSteps(self, num: int) -> int:
res = 0
while num != 0:
if num % 2 == 0:
num //= 2
res += 1
else:
num -= 1
res += 1
return res | number-of-steps-to-reduce-a-number-to-zero | Python very simple | aruj900 | 0 | 44 | number of steps to reduce a number to zero | 1,342 | 0.854 | Easy | 20,198 |
https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/discuss/2455180/Easy-Python-Solution | class Solution:
def numberOfSteps(self, num: int) -> int:
stepCount = 0
while num != 0 :
if num % 2 == 0:
num //= 2
else:
num -= 1
stepCount += 1
return stepCount | number-of-steps-to-reduce-a-number-to-zero | Easy Python Solution | nishabhukan | 0 | 26 | number of steps to reduce a number to zero | 1,342 | 0.854 | Easy | 20,199 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.