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/find-three-consecutive-integers-that-sum-to-a-given-number/discuss/2221822/Python-simple-solution
class Solution: def sumOfThree(self, n: int) -> List[int]: if n//3 == n/3: return [n//3-1,n//3,n//3+1] else: return []
find-three-consecutive-integers-that-sum-to-a-given-number
Python simple solution
StikS32
0
10
find three consecutive integers that sum to a given number
2,177
0.637
Medium
30,200
https://leetcode.com/problems/find-three-consecutive-integers-that-sum-to-a-given-number/discuss/2009445/python-3-oror-simple-O(1)-solution
class Solution: def sumOfThree(self, num: int) -> List[int]: if num % 3: return [] middle = num // 3 return [middle - 1, middle, middle + 1]
find-three-consecutive-integers-that-sum-to-a-given-number
python 3 || simple O(1) solution
dereky4
0
26
find three consecutive integers that sum to a given number
2,177
0.637
Medium
30,201
https://leetcode.com/problems/find-three-consecutive-integers-that-sum-to-a-given-number/discuss/1806196/Python-easy-to-read-and-understand
class Solution: def sumOfThree(self, num: int) -> List[int]: ans = [] if num % 3 == 0: ans = [num//3-1, num//3, num//3+1] return ans
find-three-consecutive-integers-that-sum-to-a-given-number
Python easy to read and understand
sanial2001
0
37
find three consecutive integers that sum to a given number
2,177
0.637
Medium
30,202
https://leetcode.com/problems/find-three-consecutive-integers-that-sum-to-a-given-number/discuss/1799983/Python3-accepted-one-liner-solution
class Solution: def sumOfThree(self, num: int) -> List[int]: return [num//3 -1,num//3,num//3 +1] if(num%3==0) else []
find-three-consecutive-integers-that-sum-to-a-given-number
Python3 accepted one-liner solution
sreeleetcode19
0
17
find three consecutive integers that sum to a given number
2,177
0.637
Medium
30,203
https://leetcode.com/problems/find-three-consecutive-integers-that-sum-to-a-given-number/discuss/1798731/Why-is-this-even-medium-level
class Solution: def sumOfThree(self, num: int) -> List[int]: if num % 3 != 0: return [] k = num//3 return [k-1, k, k+1]
find-three-consecutive-integers-that-sum-to-a-given-number
Why is this even medium level?
byuns9334
0
26
find three consecutive integers that sum to a given number
2,177
0.637
Medium
30,204
https://leetcode.com/problems/find-three-consecutive-integers-that-sum-to-a-given-number/discuss/1785045/python3-number-theory-concept
class Solution: def sumOfThree(self, num: int) -> List[int]: if num%3!=0: return [] return [num//3-1,num//3,num//3+1]
find-three-consecutive-integers-that-sum-to-a-given-number
python3 number theory concept
Karna61814
0
8
find three consecutive integers that sum to a given number
2,177
0.637
Medium
30,205
https://leetcode.com/problems/find-three-consecutive-integers-that-sum-to-a-given-number/discuss/1783860/Python-math
class Solution: def sumOfThree(self, num: int) -> List[int]: return [num //3 - 1, num // 3, num // 3 + 1] if num % 3 == 0 else []
find-three-consecutive-integers-that-sum-to-a-given-number
Python, math
blue_sky5
0
14
find three consecutive integers that sum to a given number
2,177
0.637
Medium
30,206
https://leetcode.com/problems/find-three-consecutive-integers-that-sum-to-a-given-number/discuss/1783291/Python%3A-Very-Simple-Math-formula-SC%3A-O(1)-TC%3A-O(1)
class Solution: def sumOfThree(self, num: int) -> List[int]: rightHandSide = num - 3 if rightHandSide % 3 != 0: return [] firstNumber = rightHandSide // 3 return [firstNumber, firstNumber + 1, firstNumber + 2]
find-three-consecutive-integers-that-sum-to-a-given-number
Python: Very Simple Math formula SC: O(1) TC: O(1)
tyrocoder
0
33
find three consecutive integers that sum to a given number
2,177
0.637
Medium
30,207
https://leetcode.com/problems/maximum-split-of-positive-even-integers/discuss/1783966/Simple-Python-Solution-with-Explanation-oror-O(sqrt(n))-Time-Complexity-oror-O(1)-Space-Complexity
class Solution: def maximumEvenSplit(self, finalSum: int) -> List[int]: l=set() if finalSum%2!=0: return l else: s=0 i=2 # even pointer 2, 4, 6, 8, 10, 12........... while(s<finalSum): s+=i #sum l.add(i) # append the i in list i+=2 if s==finalSum: #if sum s is equal to finalSum then no modidfication required return l else: l.discard(s-finalSum) #Deleting the element which makes s greater than finalSum return l
maximum-split-of-positive-even-integers
Simple Python Solution with Explanation || O(sqrt(n)) Time Complexity || O(1) Space Complexity
HimanshuGupta_p1
51
2,000
maximum split of positive even integers
2,178
0.591
Medium
30,208
https://leetcode.com/problems/maximum-split-of-positive-even-integers/discuss/1783248/Python-Solution-by-Splitting-and-Merging
class Solution: def maximumEvenSplit(self, finalSum: int) -> List[int]: arr = [] if finalSum % 2 == 0: # If finalSum is odd then we cannot ever divide it with the given conditions a, i = finalSum // 2, 1 # a is the number of 2's and i is the number of 2's that we will use to form a even number in the current iteration while i <= a: # Till we have sufficient number of 2's available arr.append(2*i) # Join the i number of 2's to form a even number a -= i # Number of 2's remaining reduces by i i += 1 # Number of 2's required in next itertation increases by 1 s = sum(arr) arr[-1] += finalSum - s # This is done if their were still some 2's remaining that could not form a number due to insufficient count, then we add the remaining 2's into the last number. return arr
maximum-split-of-positive-even-integers
Python Solution by Splitting and Merging
anCoderr
10
1,000
maximum split of positive even integers
2,178
0.591
Medium
30,209
https://leetcode.com/problems/maximum-split-of-positive-even-integers/discuss/1783481/Python-O(finalSum0.5)-Easy-Solution
class Solution: def maximumEvenSplit(self, finalSum: int) -> List[int]: if(finalSum%2 != 0): return [] finalSum = finalSum//2 result = [] total = 0 remove = None for i in range(1, finalSum+1): result.append(i) total += i if(total == finalSum): break elif(total > finalSum): remove = total-finalSum break output = [] for num in result: if(remove==None): output.append(2*num) else: if(num!=remove): output.append(2*num) return output
maximum-split-of-positive-even-integers
Python - O(finalSum^0.5) Easy Solution
RoshanNaik
7
282
maximum split of positive even integers
2,178
0.591
Medium
30,210
https://leetcode.com/problems/maximum-split-of-positive-even-integers/discuss/1783483/Well-Explained-oror-For-Beginners-oror-O(n)
class Solution: def maximumEvenSplit(self, f: int) -> List[int]: if f%2: return [] res, i = [], 2 while i<=f: res.append(i) f -= i i += 2 res[-1]+=f return res
maximum-split-of-positive-even-integers
πŸ“ŒπŸ“Œ Well-Explained || For Beginners || O(n) 🐍
abhi9Rai
2
80
maximum split of positive even integers
2,178
0.591
Medium
30,211
https://leetcode.com/problems/maximum-split-of-positive-even-integers/discuss/1934288/python-3-oror-simple-greedy-solution
class Solution: def maximumEvenSplit(self, finalSum: int) -> List[int]: if finalSum % 2: return [] res = [] n = 2 while finalSum >= 2*n + 2: res.append(n) finalSum -= n n += 2 res.append(finalSum) return res
maximum-split-of-positive-even-integers
python 3 || simple greedy solution
dereky4
1
313
maximum split of positive even integers
2,178
0.591
Medium
30,212
https://leetcode.com/problems/maximum-split-of-positive-even-integers/discuss/1814930/Python-3-Math-and-Summations-O(MaxSplit)-or-Beats-95
class Solution: def maximumEvenSplit(self, finalSum: int) -> List[int]: if finalSum%2 != 0: return [] total = 0 i=2 answer = [] while total+i <= finalSum: total += i answer.append(i) i += 2 answer[-1] += finalSum-total return answer
maximum-split-of-positive-even-integers
[Python 3] Math and Summations O(MaxSplit) | Beats 95%
hari19041
1
122
maximum split of positive even integers
2,178
0.591
Medium
30,213
https://leetcode.com/problems/maximum-split-of-positive-even-integers/discuss/1783428/Python3-simulation
class Solution: def maximumEvenSplit(self, finalSum: int) -> List[int]: if finalSum % 2: return [] ans = [] x = 2 while finalSum >= x: ans.append(x) finalSum -= x x += 2 ans[-1] += finalSum return ans
maximum-split-of-positive-even-integers
[Python3] simulation
ye15
1
60
maximum split of positive even integers
2,178
0.591
Medium
30,214
https://leetcode.com/problems/maximum-split-of-positive-even-integers/discuss/2542035/Python-simple-solution
class Solution: ''' The idea is summing all even numbers starting from 2. It is guaranteed to be larger than finalSum when we reach 2i and i^2+i just larger than finalSum. The difference between the sum and finalSum must also be an even number and is contained in the sum. So we skip it. ''' def maximumEvenSplit(self, finalSum: int) -> List[int]: if finalSum%2: return [] i = math.floor(sqrt(finalSum)) #To save some time while i**2+i<finalSum: i+=1 skip = i**2+i-finalSum return [2*j for j in range(1,i+1) if 2*j != skip]
maximum-split-of-positive-even-integers
Python simple solution
li87o
0
80
maximum split of positive even integers
2,178
0.591
Medium
30,215
https://leetcode.com/problems/maximum-split-of-positive-even-integers/discuss/2366897/Easy-Python-solution-following-hints-provided-in-the-question
class Solution: def maximumEvenSplit(self, finalSum: int) -> List[int]: if finalSum % 2 == 1: # odd number return [] temp = 0 last_number = 2 # first even number res = list() while temp < finalSum: temp += last_number res.append(last_number) last_number += 2 if temp == finalSum: return res else: # remove the final element from list and subtract it from the running sum temp -= res.pop() diff = finalSum - temp res[-1] += diff return res
maximum-split-of-positive-even-integers
Easy Python solution following hints provided in the question
lau125
0
60
maximum split of positive even integers
2,178
0.591
Medium
30,216
https://leetcode.com/problems/maximum-split-of-positive-even-integers/discuss/2039079/5-lines-simple-solution-beat-100-(with-explanation)
class Solution: def maximumEvenSplit(self, finalSum: int) -> List[int]: # (2 + 2 * n) * n / 2 <= finalSum # math.floor(math.sqrt(finalSum)) # if n * (n + 1) > finalSum then n-=1 if finalSum % 2 != 0: return [] n = int(math.floor(math.sqrt(finalSum))) if n * (n + 1) > finalSum: n -= 1 ret = [2 * i for i in range(1, n + 1)] ret[-1] += finalSum - n * (n + 1) return ret
maximum-split-of-positive-even-integers
5 lines simple solution beat 100% (with explanation)
BichengWang
0
159
maximum split of positive even integers
2,178
0.591
Medium
30,217
https://leetcode.com/problems/maximum-split-of-positive-even-integers/discuss/2014843/Python3-Binary-Search-Solution
class Solution: def maximumEvenSplit(self, finalSum: int) -> List[int]: if finalSum % 2 != 0: return [] else: #binary search left, right = 1, finalSum//2 while left < right: middle = ceil((left + right)/2) diff_needed = (2 + (middle-1)*2)*(middle-1)//2 base = (finalSum - diff_needed)//middle if base % 2 == 1: base -= 1 if base >= 2: left = middle else: right = middle-1 res = [2 for _ in range(left)] current = 2 left = finalSum - sum(res) for t in range(len(res)-2): res[t+1] += current left -= current current += 2 res[-1] += left return res
maximum-split-of-positive-even-integers
Python3 Binary Search Solution
xxHRxx
0
116
maximum split of positive even integers
2,178
0.591
Medium
30,218
https://leetcode.com/problems/maximum-split-of-positive-even-integers/discuss/1960822/5-Lines-Python-Solution-oror-75-Faster
class Solution: def maximumEvenSplit(self, num: int) -> List[int]: if num%2: return [] ans=set() ; i=1 ; s=0 while s<num: ans.add(2*i) ; s+=2*i ; i+=1 ans.discard(s-num) return ans
maximum-split-of-positive-even-integers
5-Lines Python Solution || 75% Faster
Taha-C
0
224
maximum split of positive even integers
2,178
0.591
Medium
30,219
https://leetcode.com/problems/maximum-split-of-positive-even-integers/discuss/1846473/Greedy-solution
class Solution: def maximumEvenSplit(self, finalSum: int) -> List[int]: # we have to split the sum as a maximum number of unique positive integers if finalSum%2!=0: return [] result=[] count=0 final=0 while final<=finalSum: result.append(count+2) count+=2 final+=count if (finalSum-final-(count+2))<=count+2: if finalSum-final>result[-1]: result.append(finalSum-final) break if sum(result)!=finalSum: result=[finalSum] return result
maximum-split-of-positive-even-integers
Greedy solution
g0urav
0
91
maximum split of positive even integers
2,178
0.591
Medium
30,220
https://leetcode.com/problems/maximum-split-of-positive-even-integers/discuss/1785048/python3-solution
class Solution: def maximumEvenSplit(self, finalSum: int) -> List[int]: if finalSum%2!=0: return [] even=2 res=[] while finalSum>=even: finalSum-=even res.append(even) even+=2 res[-1]+=finalSum return res
maximum-split-of-positive-even-integers
python3 solution
Karna61814
0
15
maximum split of positive even integers
2,178
0.591
Medium
30,221
https://leetcode.com/problems/maximum-split-of-positive-even-integers/discuss/1784492/Python-3-or-Math-or-Explanation
class Solution: def maximumEvenSplit(self, finalSum: int) -> List[int]: if finalSum % 2: return [] value = int(sqrt(finalSum)) ans = [2 * i for i in range(1, value)] diff = finalSum - sum(ans) if ans and diff <= ans[-1]: return ans[:-1] + [diff + ans[-1]] else: return ans + [diff]
maximum-split-of-positive-even-integers
Python 3 | Math | Explanation
idontknoooo
0
93
maximum split of positive even integers
2,178
0.591
Medium
30,222
https://leetcode.com/problems/maximum-split-of-positive-even-integers/discuss/1784445/Python-3-or-Newbie
class Solution: def maximumEvenSplit(self, finalSum: int) -> List[int]: if finalSum%2!=0: return [] res=[] i=1 rest = finalSum while rest >0: add = 2*i if rest<add: temp = res.pop() res.append(temp+add+(rest-add)) else: res.append(add) rest = rest - add i+=1 return res
maximum-split-of-positive-even-integers
Python 3 | Newbie
hugsypenguin
0
19
maximum split of positive even integers
2,178
0.591
Medium
30,223
https://leetcode.com/problems/maximum-split-of-positive-even-integers/discuss/1783853/Python-O(n)-Solution
class Solution: def maximumEvenSplit(self, finalSum: int) -> List[int]: if finalSum % 2 == 1: return [] first = 2 sum_ = 0 result = [] while sum_ < finalSum: sum_ += first result.append(first) first += 2 if sum_ > finalSum: e1 = result.pop() e2 = result.pop() result.append(finalSum - (sum_ - (e1 + e2))) return result
maximum-split-of-positive-even-integers
[Python] O(n) Solution
tejeshreddy111
0
15
maximum split of positive even integers
2,178
0.591
Medium
30,224
https://leetcode.com/problems/maximum-split-of-positive-even-integers/discuss/1783773/Python3-oror-Explanation-With-Examples
class Solution: def maximumEvenSplit(self, f: int) -> List[int]: if f%2!=0: # if odd then return return [] count= (-1 + sqrt(1 + 4 * f)) // 2 # find x ans=[] i=2 while len(ans)!=count: ans.append(i) # add even integers in list i=i+2 if sum(ans)==f: # check if sum is euqal to finalSum return ans ans[-1]=ans[-1]+(f-sum(ans)) # else add difference in the last element return ans
maximum-split-of-positive-even-integers
Python3 || Explanation With Examples
rushi_javiya
0
21
maximum split of positive even integers
2,178
0.591
Medium
30,225
https://leetcode.com/problems/count-good-triplets-in-an-array/discuss/1783361/Python-3-SortedList-solution-O(NlogN)
class Solution: def goodTriplets(self, nums1: List[int], nums2: List[int]) -> int: n = len(nums1) hashmap2 = {} for i in range(n): hashmap2[nums2[i]] = i indices = [] for num in nums1: indices.append(hashmap2[num]) from sortedcontainers import SortedList left, right = SortedList(), SortedList() leftCount, rightCount = [], [] for i in range(n): leftCount.append(left.bisect_left(indices[i])) left.add(indices[i]) for i in range(n - 1, -1, -1): rightCount.append(len(right) - right.bisect_right(indices[i])) right.add(indices[i]) count = 0 for i in range(n): count += leftCount[i] * rightCount[n - 1 - i] return count
count-good-triplets-in-an-array
Python 3 SortedList solution, O(NlogN)
xil899
13
554
count good triplets in an array
2,179
0.371
Hard
30,226
https://leetcode.com/problems/count-good-triplets-in-an-array/discuss/1784262/Python-Simple-O(NlogN)-solution-using-bisect
class Solution: def goodTriplets(self, nums1: List[int], nums2: List[int]) -> int: n = len(nums1) res = 0 m2 = [0] * n q = [] # Build index map of nums2 for i in range(n): m2[nums2[i]] = i for p1 in range(n): p2 = m2[nums1[p1]] # Position of nums1[p1] in nums2 idx = bisect.bisect(q, p2) # Position smaller than this one so far q.insert(idx, p2) before = idx after = n-1 - p1 - p2 + before # Based on number of unique values before and after are the same res += before * after return res
count-good-triplets-in-an-array
[Python] Simple O(NlogN) solution using bisect
slbteam08
2
94
count good triplets in an array
2,179
0.371
Hard
30,227
https://leetcode.com/problems/count-good-triplets-in-an-array/discuss/1783658/Share-My-Code
class Solution: def goodTriplets(self, nums1: List[int], nums2: List[int]) -> int: N = len(nums1) BIT = [0] * (N+1) nums = [0] * N num1 = [0] * N num2 = [0] * N for i, n in enumerate(nums1): num1[n]=i for i, n in enumerate(nums2): num2[n]=i for i, n in enumerate(num1): nums[n] = num2[i] def lowbit(i): return i&amp;(-i) def add(i): i+=1 while i <= N: BIT[i]+=1 i+=lowbit(i) def query(i): i += 1 re = 0 while i: re += BIT[i] i -= lowbit(i) return re smaller = [] bigger = [] for i, n in enumerate(nums): smaller.append(query(n)) bigger.append(N-1-i-n+smaller[-1]) add(n) return sum(i*j for i, j in zip(smaller, bigger)) ```
count-good-triplets-in-an-array
Share My Code
Jeff871025
0
54
count good triplets in an array
2,179
0.371
Hard
30,228
https://leetcode.com/problems/count-integers-with-even-digit-sum/discuss/1785049/lessPython3greater-O(1)-Discrete-Formula-100-faster-1-LINE
class Solution: def countEven(self, num: int) -> int: return num // 2 if sum([int(k) for k in str(num)]) % 2 == 0 else (num - 1) // 2
count-integers-with-even-digit-sum
<Python3> O(1) - Discrete Formula - 100% faster - 1 LINE
drknzz
9
845
count integers with even digit sum
2,180
0.645
Easy
30,229
https://leetcode.com/problems/count-integers-with-even-digit-sum/discuss/1785599/Python-Faster-than-100-(Easy-solution)
class Solution: def countEven(self, num: int) -> int: count = 0 for i in range(num+1): sum_of_digits = sum(int(digit) for digit in str(i)) if sum_of_digits % 2 == 0 and sum_of_digits != 0: count += 1 return count
count-integers-with-even-digit-sum
[Python] Faster than 100% (Easy solution)
yashitanamdeo
3
121
count integers with even digit sum
2,180
0.645
Easy
30,230
https://leetcode.com/problems/count-integers-with-even-digit-sum/discuss/1784841/Python-3-(30ms)-or-Simple-Maths-Formula-or-Even-Odd-Solution
class Solution: def countEven(self, num: int) -> int: if num%2!=0: return (num//2) s=0 t=num while t: s=s+(t%10) t=t//10 if s%2==0: return num//2 else: return (num//2)-1
count-integers-with-even-digit-sum
Python 3 (30ms) | Simple Maths Formula | Even Odd Solution
MrShobhit
3
217
count integers with even digit sum
2,180
0.645
Easy
30,231
https://leetcode.com/problems/count-integers-with-even-digit-sum/discuss/1784774/Python3-brute-force
class Solution: def countEven(self, num: int) -> int: ans = 0 for x in range(1, num+1): sm = sum(map(int, str(x))) if not sm&amp;1: ans += 1 return ans
count-integers-with-even-digit-sum
[Python3] brute-force
ye15
3
211
count integers with even digit sum
2,180
0.645
Easy
30,232
https://leetcode.com/problems/count-integers-with-even-digit-sum/discuss/1877275/Python-easy-solution-full-explanation-oror-99.23-success-rate
class Solution: def countEven(self, num: int) -> int: if sum(map(int,str(num))) % 2 == 0: return num//2 return (num-1)//2
count-integers-with-even-digit-sum
βœ…Python easy solution full explanation || 99.23% success rate
Dev_Kesarwani
2
158
count integers with even digit sum
2,180
0.645
Easy
30,233
https://leetcode.com/problems/count-integers-with-even-digit-sum/discuss/1793315/Python-Solution
class Solution: def countEven(self, num: int) -> int: t = num s = 0 while num > 0: s = s + num%10 num = num//10 return t//2 if s%2 == 0 else (t-1)//2
count-integers-with-even-digit-sum
Python Solution
ayush_kushwaha
1
55
count integers with even digit sum
2,180
0.645
Easy
30,234
https://leetcode.com/problems/count-integers-with-even-digit-sum/discuss/2837383/Python-solution
class Solution: def countEven(self, num: int) -> int: nums = [sum(list(map(int,list(str(i))))) for i in range(1,num+1) if (i < 10 and i % 2 == 0) or i >= 10] nums = [i for i in nums if i % 2 == 0] return len(nums)
count-integers-with-even-digit-sum
Python solution
lornedot
0
1
count integers with even digit sum
2,180
0.645
Easy
30,235
https://leetcode.com/problems/count-integers-with-even-digit-sum/discuss/2836626/Simple-Python-sol
class Solution: def digitSum(self,n): s = 0 while n: s += n%10 n = n//10 return s def countEven(self, num: int) -> int: ans = 0 for i in range(2,num+1): if self.digitSum(i) % 2 == 0: ans += 1 return ans
count-integers-with-even-digit-sum
Simple Python sol
aruj900
0
1
count integers with even digit sum
2,180
0.645
Easy
30,236
https://leetcode.com/problems/count-integers-with-even-digit-sum/discuss/2778177/python-solution
class Solution: def countEven(self, num: int) -> int: count=0 su=0 for i in range(2,num+1): su=0 s=list(str(i)) for i in s: su=su+int(i) if su%2==0: count+=1 return count
count-integers-with-even-digit-sum
python solution
sindhu_300
0
3
count integers with even digit sum
2,180
0.645
Easy
30,237
https://leetcode.com/problems/count-integers-with-even-digit-sum/discuss/2695120/Python-1-liner-with-explanation
class Solution: def countEven(self, num: int) -> int: return int(num/2) if sum(list(map(int, str(num)))) % 2 == 0 else int((num-1)/2)
count-integers-with-even-digit-sum
Python 1 liner with explanation
code_snow
0
7
count integers with even digit sum
2,180
0.645
Easy
30,238
https://leetcode.com/problems/count-integers-with-even-digit-sum/discuss/2645231/Python-Simple-and-Easy-Solution
class Solution: def countEven(self, n: int) -> int: c = 0 for i in range(2,n+1): i = str(i) ; l = [int(j) for j in i] if sum(l) % 2 == 0: c += 1 return c
count-integers-with-even-digit-sum
Python Simple and Easy Solution
SouravSingh49
0
19
count integers with even digit sum
2,180
0.645
Easy
30,239
https://leetcode.com/problems/count-integers-with-even-digit-sum/discuss/2645181/Python-easy-solution-78ms-only
class Solution: def countEven(self, num: int) -> int: l=[] for i in range(1,num+1): l.append(list(str(i))) s=0 c=0 for i in l: for j in i: s+=int(j) if s%2==0: c+=1 s=0 return c
count-integers-with-even-digit-sum
Python easy solution 78ms only
abhint1
0
3
count integers with even digit sum
2,180
0.645
Easy
30,240
https://leetcode.com/problems/count-integers-with-even-digit-sum/discuss/2616026/Python-Beats-98-without-checking-all-numbers-below
class Solution: def countEven(self, num: int) -> int: digits = [int(x) for x in list(str(num))] if sum(digits) % 2 == 0: return num//2 else: return (num-1)//2
count-integers-with-even-digit-sum
[Python] Beats 98%, without checking all numbers below
aaron2k12
0
20
count integers with even digit sum
2,180
0.645
Easy
30,241
https://leetcode.com/problems/count-integers-with-even-digit-sum/discuss/2547704/Python-Simple-Python-Solution-or-Faster-than-89.99
class Solution: def countEven(self, num: int) -> int: def DigitSum(number): total = 0 while number > 0: remainder = number % 10 number = number // 10 total = total + remainder return total result = 0 for current_num in range(1,num + 1): if DigitSum(current_num) % 2 == 0: result = result + 1 return result
count-integers-with-even-digit-sum
[ Python ] βœ…βœ… Simple Python Solution | Faster than 89.99% πŸ₯³βœŒπŸ‘
ASHOK_KUMAR_MEGHVANSHI
0
34
count integers with even digit sum
2,180
0.645
Easy
30,242
https://leetcode.com/problems/count-integers-with-even-digit-sum/discuss/2536621/Easy-approach-or-Python3
class Solution: def countEven(self, n: int) -> int: c=0 for i in range(1,n+1): evens=0 odds=0 '''even number even no's. &amp; even number of odd no's. gievs resultant= even no. similarly, odd even no.&amp; odd odd no.=odd odd even no. &amp; even odd no.=even even even no.&amp; odd odd no.=odd''' for j in str(i): if int(j)%2==0: evens+=1 else: odds+=1 if (evens%2==0 and odds%2==0) or (evens%2!=0 and odds%2==0): c=c+1 return c
count-integers-with-even-digit-sum
Easy approach | Python3
keertika27
0
31
count integers with even digit sum
2,180
0.645
Easy
30,243
https://leetcode.com/problems/count-integers-with-even-digit-sum/discuss/2509748/Python-or-Easy-to-read-solution
class Solution: def countEven(self, num: int) -> int: counter = 0 for i in range(1, num+1): res = list(str(i)) res = list(map(int, res)) res = sum(res) if res % 2 == 0: if res <= num: counter += 1 else: break return counter
count-integers-with-even-digit-sum
Python | Easy to read solution
Wartem
0
236
count integers with even digit sum
2,180
0.645
Easy
30,244
https://leetcode.com/problems/count-integers-with-even-digit-sum/discuss/2466539/Python-for-beginners-naive-approach-optimized-approach
class Solution: def countEven(self, num: int) -> int: #Optimized Approach #Runtime: 57ms return num//2 if sum([int(i) for i in str(num)])%2==0 else (num-1)//2 """ #Naive Approach #Runtime: 81ms count=0 lis=[str(i) for i in range(1,num+1)] for i in lis: check=0 for j in i: check+=int(j) if(check%2==0): count+=1 return count """
count-integers-with-even-digit-sum
Python for beginners naive approach, optimized approach
mehtay037
0
32
count integers with even digit sum
2,180
0.645
Easy
30,245
https://leetcode.com/problems/count-integers-with-even-digit-sum/discuss/2407525/Python-Soln
class Solution: def countEven(self, num: int) -> int: cnt = 0 for i in range(1, num+1): if self.sumOfDigits(i)%2==0 and self.sumOfDigits(i) <= num: # print(i) cnt += 1 return cnt def sumOfDigits(self, n): digitSum = 0 while n: digitSum += n%10 n = n //10 print(digitSum) return digitSum
count-integers-with-even-digit-sum
Python Soln
logeshsrinivasans
0
40
count integers with even digit sum
2,180
0.645
Easy
30,246
https://leetcode.com/problems/count-integers-with-even-digit-sum/discuss/2380621/Python3-straightforward
class Solution: def countEven(self, num: int) -> int: c=0 for i in range(1,num+1): if sum(map(int,list(str(i))))%2==0: c+=1 return c
count-integers-with-even-digit-sum
[Python3] straightforward
sunakshi132
0
35
count integers with even digit sum
2,180
0.645
Easy
30,247
https://leetcode.com/problems/count-integers-with-even-digit-sum/discuss/2157385/Count-Integers-With-Even-Digit-Sum-for-Python
class Solution: def countEven(self, num: int) -> int: count = 0 for i in range(1, num + 1): oddI = i product = 0 while i >= 1: product += (i % 10) i //= 10 if oddI < 10 and product % 2 == 0: count += 1 if product % 2 == 0 and oddI >= 10: count += 1 return count
count-integers-with-even-digit-sum
Count Integers With Even Digit Sum for Python
YangJenHao
0
26
count integers with even digit sum
2,180
0.645
Easy
30,248
https://leetcode.com/problems/count-integers-with-even-digit-sum/discuss/2106162/basic-solution-with-helper-function
class Solution: def countEven(self, num: int) -> int: def sum_digits(n): return sum([int(v) for v in str(n)]) for n in range(1, num + 1): if not sum_digits(n) % 2: res += 1 return res
count-integers-with-even-digit-sum
basic solution with helper function
andrewnerdimo
0
36
count integers with even digit sum
2,180
0.645
Easy
30,249
https://leetcode.com/problems/count-integers-with-even-digit-sum/discuss/2095645/Python-Solution-with-a-simple-approach
class Solution: def countEven(self, num: int) -> int: count = 0 for i in range(1, num+1): # Range: [1, 1000] check = self.sum_digits(i) # Call this fuction to get the sum of digits if check%2 == 0: count += 1 return count def sum_digits(self, n): # Eg: n = 19 sum_ = 0 while n > 0: digit = n % 10 # digit = 9 : Step - 1 sum_ += digit # sum_ = 9 : Step - 2 n = n//10 # n = 1, Again Step 1 and 2 as 1 > 0 then the sum_ will be 10 return sum_
count-integers-with-even-digit-sum
Python Solution with a simple approach
yash921
0
67
count integers with even digit sum
2,180
0.645
Easy
30,250
https://leetcode.com/problems/count-integers-with-even-digit-sum/discuss/2036696/Python-Clean-and-Simple!
class Solution: def countEven(self, num): return sum(self.isEven(self.calcDigitSum(n)) for n in range(1,num+1)) def calcDigitSum(self, num): return sum(int(d) for d in str(num)) def isEven(self, num): return num % 2 == 0
count-integers-with-even-digit-sum
Python - Clean and Simple!
domthedeveloper
0
92
count integers with even digit sum
2,180
0.645
Easy
30,251
https://leetcode.com/problems/count-integers-with-even-digit-sum/discuss/2036696/Python-Clean-and-Simple!
class Solution: def countEven(self, num): return num//2 if self.isEven(self.calcDigitSum(num)) else (num-1)//2 def calcDigitSum(self, num): return sum(int(d) for d in str(num)) def isEven(self, num): return num % 2 == 0
count-integers-with-even-digit-sum
Python - Clean and Simple!
domthedeveloper
0
92
count integers with even digit sum
2,180
0.645
Easy
30,252
https://leetcode.com/problems/count-integers-with-even-digit-sum/discuss/2034306/Python-oneliner
class Solution: def countEven(self, num: int) -> int: return len([x for x in range(1,num+1) if sum(map(int,list(str(x))))%2 == 0])
count-integers-with-even-digit-sum
Python oneliner
StikS32
0
71
count integers with even digit sum
2,180
0.645
Easy
30,253
https://leetcode.com/problems/count-integers-with-even-digit-sum/discuss/1950200/Easy
class Solution: def countEven(self, num: int) -> int: c = 0 for i in range(1, num+1): if sum([int(d) for d in str(i)]) % 2 == 0: c += 1 return c
count-integers-with-even-digit-sum
Easy
Dario_Adamovic
0
34
count integers with even digit sum
2,180
0.645
Easy
30,254
https://leetcode.com/problems/count-integers-with-even-digit-sum/discuss/1944128/Python-dollarolution-(98-Faster)
class Solution: def countEven(self, num: int) -> int: count = 0 for i in range(2,num+1): k = i s = 0 while k != 0: s += k%10 k //= 10 if s % 2 == 0: count += 1 return count
count-integers-with-even-digit-sum
Python $olution (98% Faster)
AakRay
0
85
count integers with even digit sum
2,180
0.645
Easy
30,255
https://leetcode.com/problems/count-integers-with-even-digit-sum/discuss/1926825/Python-Simple-Math-Solution-O(Length(Num))
class Solution: def countEven(self, num: int) -> int: s, c = str(num), 0 for i in s: c += int(i) return num // 2 if c % 2 == 0 else (num - 1) // 2
count-integers-with-even-digit-sum
Python Simple Math Solution O(Length(Num))
Hejita
0
56
count integers with even digit sum
2,180
0.645
Easy
30,256
https://leetcode.com/problems/count-integers-with-even-digit-sum/discuss/1854962/Python-light-solution-space-less-than-95
class Solution: def countEven(self, num: int) -> int: count = 0 for i in range(1, num+1): temp = list(str(i)) digits = [int(x) for x in temp] if sum(digits) % 2 == 0: count += 1 return count
count-integers-with-even-digit-sum
Python light solution, space less than 95%
alishak1999
0
45
count integers with even digit sum
2,180
0.645
Easy
30,257
https://leetcode.com/problems/count-integers-with-even-digit-sum/discuss/1840824/python-3-oror-O(log-n)
class Solution: def countEven(self, num: int) -> int: digitSum = 0 n = num while n: digitSum += n % 10 n //= 10 return (num - 1) // 2 if digitSum % 2 else num // 2
count-integers-with-even-digit-sum
python 3 || O(log n)
dereky4
0
70
count integers with even digit sum
2,180
0.645
Easy
30,258
https://leetcode.com/problems/count-integers-with-even-digit-sum/discuss/1799915/Python3-accepted-solution
class Solution: def countEven(self, num: int) -> int: count=0 for i in range(1,num+1): if(sum([int(j) for j in str(i)])%2==0): count+=1 return count
count-integers-with-even-digit-sum
Python3 accepted solution
sreeleetcode19
0
53
count integers with even digit sum
2,180
0.645
Easy
30,259
https://leetcode.com/problems/count-integers-with-even-digit-sum/discuss/1796430/Python-Brute-force-using-str-less-greater-int
class Solution: def countEven(self, num: int) -> int: count = 0 for n in range(1, num + 1): if n < 10: if n % 2 == 0: count += 1 else: ns = str(n) x = 0 for s in ns: x += int(s) if x % 2 == 0: count += 1 return count
count-integers-with-even-digit-sum
[Python] Brute force using str <-> int
casshsu
0
33
count integers with even digit sum
2,180
0.645
Easy
30,260
https://leetcode.com/problems/count-integers-with-even-digit-sum/discuss/1794191/Python-Iterative-Solutions
class Solution: def countEven(self, num: int) -> int: def getDidgitSum(num): digitSum = 0 while(num > 0): digitSum += num % 10 num //= 10 return digitSum total = 0 for i in range(1, num+1): digitSum = getDidgitSum(i) if digitSum % 2 == 0: total += 1 return total
count-integers-with-even-digit-sum
Python Iterative Solutions
rushirg
0
47
count integers with even digit sum
2,180
0.645
Easy
30,261
https://leetcode.com/problems/count-integers-with-even-digit-sum/discuss/1794191/Python-Iterative-Solutions
class Solution: def countEven(self, num: int) -> int: def getDidgitSum(num): digitSum = 0 for digit in str(num): digitSum += int(digit) return digitSum total = 0 for i in range(1, num+1): digitSum = getDidgitSum(i) if digitSum % 2 == 0: total += 1 return total
count-integers-with-even-digit-sum
Python Iterative Solutions
rushirg
0
47
count integers with even digit sum
2,180
0.645
Easy
30,262
https://leetcode.com/problems/count-integers-with-even-digit-sum/discuss/1793947/1-Line-Python-Solution-oror-97-Faster-(33ms)-oror-Memory-less-than-60
class Solution: def countEven(self, num: int) -> int: return num//2 if sum([int(d) for d in str(num)])%2==0 else (num-1)//2
count-integers-with-even-digit-sum
1-Line Python Solution || 97% Faster (33ms) || Memory less than 60%
Taha-C
0
59
count integers with even digit sum
2,180
0.645
Easy
30,263
https://leetcode.com/problems/count-integers-with-even-digit-sum/discuss/1791391/Memorization-trick-39-ms-96-speed
class Solution: memo = {1: 0} keys = [1] def countEven(self, num: int) -> int: if num not in Solution.memo: idx = bisect_right(Solution.keys, num) Solution.memo[num] = (Solution.memo[Solution.keys[idx - 1]] + sum(not sum(int(d) for d in str(n)) % 2 for n in range(Solution.keys[idx - 1] + 1, num + 1))) Solution.keys.insert(idx, num) return Solution.memo[num]
count-integers-with-even-digit-sum
Memorization trick, 39 ms 96% speed
EvgenySH
0
45
count integers with even digit sum
2,180
0.645
Easy
30,264
https://leetcode.com/problems/count-integers-with-even-digit-sum/discuss/1791006/Intutive-easy-Python-solution
class Solution: def countEven(self, num: int) -> int: def digitSum(num): sum_of_digits = 0 while num: num, remainder = divmod(num, 10) #num = num/10 &amp; remainder = num%10 sum_of_digits += remainder return sum_of_digits return num//2 if digitSum(num)%2 == 0 else (num-1)//2
count-integers-with-even-digit-sum
Intutive easy Python solution
MaverickEyedea
0
28
count integers with even digit sum
2,180
0.645
Easy
30,265
https://leetcode.com/problems/count-integers-with-even-digit-sum/discuss/1787580/Short-fast-python-solution
class Solution: def countEven(self, num: int) -> int: def match(v): return sum(map(int, str(v))) % 2 == 0 return sum(map(match, range(1, num+1)))
count-integers-with-even-digit-sum
Short, fast python solution
emwalker
0
27
count integers with even digit sum
2,180
0.645
Easy
30,266
https://leetcode.com/problems/count-integers-with-even-digit-sum/discuss/1785031/python3-solution-brute-force
class Solution: def countEven(self, num: int) -> int: count=0 for i in range(1,num+1): s=str(i) mylist=[int(val) for val in s] if sum(mylist)%2==0: count+=1 return count
count-integers-with-even-digit-sum
python3 solution brute force
Karna61814
0
17
count integers with even digit sum
2,180
0.645
Easy
30,267
https://leetcode.com/problems/count-integers-with-even-digit-sum/discuss/1784965/Python-brute-force
class Solution: def countEven(self, num: int) -> int: return sum(sum(map(int, str(n))) % 2 == 0 for n in range(2, num + 1))
count-integers-with-even-digit-sum
Python, brute force
blue_sky5
0
28
count integers with even digit sum
2,180
0.645
Easy
30,268
https://leetcode.com/problems/count-integers-with-even-digit-sum/discuss/1784833/Python-Solution
class Solution: def countEven(self, num: int) -> int: count = 0 for i in range(2,num+1): a = list(str(i)) sum1 = 0 for item in a: sum1 += int(item) if (sum1%2==0): count+=1 return count
count-integers-with-even-digit-sum
Python Solution
anjaligupta0621
0
31
count integers with even digit sum
2,180
0.645
Easy
30,269
https://leetcode.com/problems/count-integers-with-even-digit-sum/discuss/1785205/Python3-%2B-Js-solution
class Solution: def countEven(self, num: int) -> int: count = 0 for i in range(1, num + 1): summetion = 0 for digit in str(i): summetion += int(digit) if summetion % 2 == 0 : count += 1 return count
count-integers-with-even-digit-sum
Python3 + Js solution
shakilbabu
-1
37
count integers with even digit sum
2,180
0.645
Easy
30,270
https://leetcode.com/problems/merge-nodes-in-between-zeros/discuss/1784873/Python-3-or-Dummy-Node-O(N)-Time-Solution
class Solution: def mergeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]: d=ListNode(0) t=0 r=ListNode(0,d) while head: if head.val!=0: t+=head.val else: print(t) if t!=0: d.next=ListNode(t) d=d.next t=0 head=head.next return r.next.next
merge-nodes-in-between-zeros
Python 3 | Dummy Node O(N) Time Solution
MrShobhit
6
576
merge nodes in between zeros
2,181
0.867
Medium
30,271
https://leetcode.com/problems/merge-nodes-in-between-zeros/discuss/1784780/Python3-simulation
class Solution: def mergeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]: dummy = node = ListNode() chunk = head while chunk: chunk = chunk.next sm = 0 while chunk and chunk.val: sm += chunk.val chunk = chunk.next if sm: node.next = node = ListNode(sm) return dummy.next
merge-nodes-in-between-zeros
[Python3] simulation
ye15
6
417
merge nodes in between zeros
2,181
0.867
Medium
30,272
https://leetcode.com/problems/merge-nodes-in-between-zeros/discuss/1784818/Simple-Python-Solution-in-O(n)-Time
class Solution: def mergeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]: current = head ans = ListNode() dummy = ans while current is not None and current.next is not None: if current.val == 0: count = 0 current = current.next while current.val != 0 and current is not None: count += current.val current = current.next dummy.next = ListNode(count) dummy = dummy.next return ans.next
merge-nodes-in-between-zeros
Simple Python Solution in O(n) Time
anCoderr
4
391
merge nodes in between zeros
2,181
0.867
Medium
30,273
https://leetcode.com/problems/merge-nodes-in-between-zeros/discuss/2787661/Easy-Python-solution-in-O(N)-TC
class Solution: def mergeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]: temp=head sm=0 temp=temp.next curr=head while temp: if temp.val==0: curr=curr.next curr.val=sm sm=0 else: sm+=temp.val temp=temp.next curr.next=None return head.next
merge-nodes-in-between-zeros
Easy Python solution in O(N) TC
shubham_1307
2
95
merge nodes in between zeros
2,181
0.867
Medium
30,274
https://leetcode.com/problems/merge-nodes-in-between-zeros/discuss/1904688/python3-oror-easy-solution-oror-O(n)O(1)
class Solution: def mergeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]: res, cur = head, head.next while cur.next: if cur.val: res.val += cur.val else: res.next = res = cur cur = cur.next res.next = None return head
merge-nodes-in-between-zeros
python3 || easy solution || O(n)/O(1)
dereky4
1
104
merge nodes in between zeros
2,181
0.867
Medium
30,275
https://leetcode.com/problems/merge-nodes-in-between-zeros/discuss/1787614/Python-two-pointers
class Solution: def mergeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]: ans = prev = ListNode(-1) ans.next = curr = head while curr.next: if curr.val == 0: prev.next = curr prev = curr else: prev.val += curr.val prev.next = curr.next curr = curr.next prev.next = None return ans.next
merge-nodes-in-between-zeros
Python, two pointers
emwalker
1
43
merge nodes in between zeros
2,181
0.867
Medium
30,276
https://leetcode.com/problems/merge-nodes-in-between-zeros/discuss/2820924/Python-or-Beats-99.58-or-O(N)-Time-Complexity-or-O(1)-Space-Complexity
class Solution: def mergeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]: sumi=0 dummy = curr = head curr = curr.next while curr: if curr.val!=0: sumi+=curr.val else: dummy=dummy.next dummy.val=sumi sumi=0 curr=curr.next dummy.next=None return head.next
merge-nodes-in-between-zeros
Python | Beats 99.58% | O(N) Time Complexity | O(1) Space Complexity
liontech_123
0
2
merge nodes in between zeros
2,181
0.867
Medium
30,277
https://leetcode.com/problems/merge-nodes-in-between-zeros/discuss/2692527/Python3-Simple-Solution
class Solution: def mergeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]: arr = [] s = -1 while head is not None: if head.val == 0: if s != -1: arr.append(s) s = 0 s += head.val head = head.next H = ListNode(arr[0]) prev = H for num in arr[1:]: tmpNode = ListNode(num) prev.next = tmpNode prev = tmpNode return H
merge-nodes-in-between-zeros
Python3 Simple Solution
mediocre-coder
0
2
merge nodes in between zeros
2,181
0.867
Medium
30,278
https://leetcode.com/problems/merge-nodes-in-between-zeros/discuss/2526510/Python-Simple-one-pass-Iterative-solution
class Solution: def mergeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]: p1 = p2 = head while p2 and p2.next: p2 = p2.next p1.val += p2.val if p2.val == 0: if p2.next: p1.next = p2 p1 = p2 else: p1.next = None return head
merge-nodes-in-between-zeros
[Python] Simple one-pass Iterative solution
kavansoni
0
36
merge nodes in between zeros
2,181
0.867
Medium
30,279
https://leetcode.com/problems/merge-nodes-in-between-zeros/discuss/2237455/Python3-SImple-solution
class Solution: def mergeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]: prev = head curr = head.next sumNode = 0 while curr: if curr.val == 0: # We have reached a 0 node # Create a new node with sum of all nodes between zeros new_node = ListNode(sumNode) prev.next = new_node # Skip the 0 node new_node.next = curr.next # this is needed to link the new sum node (new_node) to prev node (old new_node) prev = new_node sumNode = 0 else: sumNode += curr.val curr = curr.next return head.next
merge-nodes-in-between-zeros
[Python3] SImple solution
Gp05
0
7
merge nodes in between zeros
2,181
0.867
Medium
30,280
https://leetcode.com/problems/merge-nodes-in-between-zeros/discuss/1962701/Merge-Nodes-in-Between-Zeros-O(n)
class Solution: def mergeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]: newList = ListNode(0) pointer = newList while head and head.next: if head.val == 0: head = head.next nodesum = 0 while head.val > 0: nodesum += head.val head = head.next pointer.next = ListNode(nodesum) pointer =pointer.next return newList.next
merge-nodes-in-between-zeros
Merge Nodes in Between Zeros O(n)
Edithturn
0
35
merge nodes in between zeros
2,181
0.867
Medium
30,281
https://leetcode.com/problems/merge-nodes-in-between-zeros/discuss/1901147/Python3-simple-solution
class Solution: def mergeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]: temp = head x1 = False x2 = False c = 0 head1 = ListNode() temp1 = head1 while temp: if temp.val == 0: if x1: if x2: temp1.next = ListNode(c) temp1 = temp1.next else: temp1.val = c x2 = True c = 0 else: x1 = True else: c += temp.val temp = temp.next return head1
merge-nodes-in-between-zeros
Python3 simple solution
EklavyaJoshi
0
15
merge nodes in between zeros
2,181
0.867
Medium
30,282
https://leetcode.com/problems/merge-nodes-in-between-zeros/discuss/1896237/Python-Iterative-2-Approaches-oror-Time-O(N)-Space-O(1)
class Solution: def mergeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]: """ time: O(n) space: O(1) """ if not head: return head dummy_node = second_head = ListNode(-1) curr, prev = head.next, head while curr: sum_ = 0 while (prev.val == 0) and (curr.val != 0): sum_ += curr.val curr = curr.next new_node = ListNode(sum_) dummy_node.next = new_node dummy_node = dummy_node.next prev, curr = curr, curr.next return second_head.next
merge-nodes-in-between-zeros
Python Iterative - 2 Approaches || Time - O(N), Space - O(1)
ChidinmaKO
0
35
merge nodes in between zeros
2,181
0.867
Medium
30,283
https://leetcode.com/problems/merge-nodes-in-between-zeros/discuss/1896237/Python-Iterative-2-Approaches-oror-Time-O(N)-Space-O(1)
class Solution: def mergeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]: """ time: O(n) space: O(1) """ if not head: return head curr, prev, sum_ = head.next, head, 0 while curr: if curr.val == 0: prev.next = ListNode(sum_) prev = prev.next sum_ = 0 else: sum_ += curr.val curr = curr.next return head.next
merge-nodes-in-between-zeros
Python Iterative - 2 Approaches || Time - O(N), Space - O(1)
ChidinmaKO
0
35
merge nodes in between zeros
2,181
0.867
Medium
30,284
https://leetcode.com/problems/merge-nodes-in-between-zeros/discuss/1895286/Python3-Solution-with-using-two-pointers
class Solution: def mergeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]: dummy = ListNode() cur = dummy prev_sum = 0 while head: if head.val == 0: cur.val = prev_sum # skip last zero element if head.next: cur.next = ListNode() cur = cur.next prev_sum = 0 else: prev_sum += head.val head = head.next return dummy.next
merge-nodes-in-between-zeros
[Python3] Solution with using two pointers
maosipov11
0
21
merge nodes in between zeros
2,181
0.867
Medium
30,285
https://leetcode.com/problems/merge-nodes-in-between-zeros/discuss/1877950/Easy-to-understand-python-submission-100-working.
class Solution: def mergeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]: ans=ListNode() temp=ans s=0 head=head.next while head: if head.val!=0: s+=head.val else: temp.val=s s=0 if head.next!=None: temp.next=ListNode() temp=temp.next head=head.next return ans
merge-nodes-in-between-zeros
Easy to understand python submission 100% working.
tkdhimanshusingh
0
32
merge nodes in between zeros
2,181
0.867
Medium
30,286
https://leetcode.com/problems/merge-nodes-in-between-zeros/discuss/1799554/Python-Easy-creating-new-chain
class Solution: def mergeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]: root=None curr=None while head : if head.val==0 : head=head.next continue x=0 while head and head.val : x+=head.val head=head.next if not x : continue if root is None : root=ListNode(x) curr=root else : curr.next=ListNode(x) curr=curr.next return root
merge-nodes-in-between-zeros
Python Easy creating new chain
P3rf3ct0
0
76
merge nodes in between zeros
2,181
0.867
Medium
30,287
https://leetcode.com/problems/merge-nodes-in-between-zeros/discuss/1791087/Relink-in-place-one-pass.-85-speed
class Solution: def mergeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]: node = head while node.next: if node.next.val != 0: node.val += node.next.val node.next = node.next.next elif node.next.next: node = node.next else: node.next = None return head
merge-nodes-in-between-zeros
Relink in place, one pass. 85% speed
EvgenySH
0
35
merge nodes in between zeros
2,181
0.867
Medium
30,288
https://leetcode.com/problems/merge-nodes-in-between-zeros/discuss/1785303/Simple-solution-O(n)-(js%2Bpython3)
class Solution: def mergeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]: cur = head.next dummy = ListNode(-1) newHead = dummy summetion = 0 while cur : if cur.val == 0 : newHead.next = ListNode(summetion) newHead = newHead.next summetion = 0 else : summetion += cur.val cur = cur.next return dummy.next
merge-nodes-in-between-zeros
Simple solution - O(n) (js+python3)
shakilbabu
0
36
merge nodes in between zeros
2,181
0.867
Medium
30,289
https://leetcode.com/problems/merge-nodes-in-between-zeros/discuss/1785274/Python3-O(N)-Recursive-and-Iterative-Solutions
class Solution: def getMerged(self, head): if not head.next: return None, 0 nextList, _sum = self.getMerged(head.next) if head.val: return nextList, _sum + head.val else: head.next = nextList head.val = _sum return head, 0 def mergeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]: return self.getMerged(head)[0]
merge-nodes-in-between-zeros
[Python3] O(N) - Recursive & Iterative Solutions
dwschrute
0
31
merge nodes in between zeros
2,181
0.867
Medium
30,290
https://leetcode.com/problems/merge-nodes-in-between-zeros/discuss/1785274/Python3-O(N)-Recursive-and-Iterative-Solutions
class Solution: def mergeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]: slow, fast = head, head.next accum = 0 while fast: if not fast.val: slow.val = accum if fast.next: slow = slow.next else: slow.next = None accum = 0 else: accum += fast.val fast = fast.next return head
merge-nodes-in-between-zeros
[Python3] O(N) - Recursive & Iterative Solutions
dwschrute
0
31
merge nodes in between zeros
2,181
0.867
Medium
30,291
https://leetcode.com/problems/merge-nodes-in-between-zeros/discuss/1785081/Python3-simple-intuitive-solution-explained
class Solution: def mergeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]: nodes = [] while head: nodes.append(head.val) head = head.next result = ListNode() temp = [] cumsum = 0 for i in nodes[1:]: if i != 0: cumsum += i else: temp.append(cumsum) cumsum = 0 head = result for i in temp: head.next = ListNode(i) head = head.next return result.next
merge-nodes-in-between-zeros
Python3 simple intuitive solution explained
s_mugisha
0
23
merge nodes in between zeros
2,181
0.867
Medium
30,292
https://leetcode.com/problems/merge-nodes-in-between-zeros/discuss/1785029/python3-solution-easy-and-clean
class Solution: def mergeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]: temp=head.next s=0 newHead=dummy=ListNode(-1) while temp: if temp.val==0: newHead.next=ListNode(s) newHead=newHead.next s=0 else: s+=temp.val temp=temp.next return dummy.next
merge-nodes-in-between-zeros
python3 solution easy and clean
Karna61814
0
22
merge nodes in between zeros
2,181
0.867
Medium
30,293
https://leetcode.com/problems/construct-string-with-repeat-limit/discuss/1784789/Python3-priority-queue
class Solution: def repeatLimitedString(self, s: str, repeatLimit: int) -> str: pq = [(-ord(k), v) for k, v in Counter(s).items()] heapify(pq) ans = [] while pq: k, v = heappop(pq) if ans and ans[-1] == k: if not pq: break kk, vv = heappop(pq) ans.append(kk) if vv-1: heappush(pq, (kk, vv-1)) heappush(pq, (k, v)) else: m = min(v, repeatLimit) ans.extend([k]*m) if v-m: heappush(pq, (k, v-m)) return "".join(chr(-x) for x in ans)
construct-string-with-repeat-limit
[Python3] priority queue
ye15
23
1,100
construct string with repeat limit
2,182
0.519
Medium
30,294
https://leetcode.com/problems/construct-string-with-repeat-limit/discuss/1784794/Greedy-Python-Solution-or-100-Runtime
class Solution: def repeatLimitedString(self, s: str, repeatLimit: int) -> str: table = Counter(s) char_set = ['0', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] sorted_table = [] for i in range(26,-1,-1): if char_set[i] in table: sorted_table.append((char_set[i],table[char_set[i]])) result = "" n = len(sorted_table) for i in range(n): char, curr_freq = sorted_table[i] # The lexicographically larger character and its frequency index_to_take_from = i + 1 # We take from this index the next lexicographically larger character(TEMP) if the repeatLimit for A is exceeded while curr_freq > repeatLimit: # Limit exceeded result += char*repeatLimit # Add repeatLimit number of character A's curr_freq -= repeatLimit # Decrease frequency of character A # Now we search for the next lexicographically superior character that can be used once while index_to_take_from < n: # Till we run out of characters ch_avail, freq_avail = sorted_table[index_to_take_from] if freq_avail == 0: # If its freq is 0 that means that it was previously used. This is done as we are not removing the character from table if its freq becomes 0. index_to_take_from += 1 # Check for next lexicographically superior character else: result += ch_avail # If found then add that character sorted_table[index_to_take_from] = (ch_avail,freq_avail-1) # Update the new characters frequency break else: break # We cannot find any lexicographically superior character else: result += char*curr_freq # If the freq is in limits then just add them return result
construct-string-with-repeat-limit
Greedy Python Solution | 100% Runtime
anCoderr
4
425
construct string with repeat limit
2,182
0.519
Medium
30,295
https://leetcode.com/problems/construct-string-with-repeat-limit/discuss/1973741/python-3-oror-O(n)O(1)-oror-without-priority-queue
class Solution: def repeatLimitedString(self, s: str, repeatLimit: int) -> str: count = collections.Counter(s) chrs = list(map(list, sorted(count.items(), reverse=True))) res = [] first, second = 0, 1 n = len(chrs) while second < n: if chrs[first][1] <= repeatLimit: res.append(chrs[first][0] * chrs[first][1]) first += 1 while chrs[first][1] == 0: first += 1 if first >= second: second = first + 1 else: res.append(chrs[first][0] * repeatLimit + chrs[second][0]) chrs[first][1] -= repeatLimit chrs[second][1] -= 1 if chrs[second][1] == 0: second += 1 res.append(chrs[first][0] * min(repeatLimit, chrs[first][1])) return ''.join(res)
construct-string-with-repeat-limit
python 3 || O(n)/O(1) || without priority queue
dereky4
1
52
construct string with repeat limit
2,182
0.519
Medium
30,296
https://leetcode.com/problems/construct-string-with-repeat-limit/discuss/1953156/8-Lines-Python-Solution-oror-80-Faster-oror-Memory-less-than-80
class Solution: def repeatLimitedString(self, s: str, l: int) -> str: C=[list(x) for x in sorted(Counter(s).items(),reverse=True)] ; ans='' if len(C)==1: return C[0][0]*l while len(C)>1: ans+=C[0][0]*min(C[0][1],l) C[0][1]-=min(C[0][1],l) if C[0][1]>0: ans+=C[1][0] ; C[1][1]-=1 C=[c for c in C if c[1]>0] return ans+C[0][0]*min(C[0][1],l)
construct-string-with-repeat-limit
8-Lines Python Solution || 80% Faster || Memory less than 80%
Taha-C
0
44
construct string with repeat limit
2,182
0.519
Medium
30,297
https://leetcode.com/problems/construct-string-with-repeat-limit/discuss/1787058/Python-O(n)-Time-Solution
class Solution: def repeatLimitedString(self, s: str, repeatLimit: int) -> str: hmap = OrderedDict(Counter(s)) hmap = dict(sorted(hmap.items(), key = lambda x: x[0], reverse=True)) result = "" while len(hmap) > 0: keys = list(hmap.keys()) first = keys[0] if hmap[first] > repeatLimit: result += first * repeatLimit hmap[first] -= repeatLimit if len(hmap) == 1: return result result += keys[1] hmap[keys[1]] -= 1 if hmap[keys[1]] == 0: del hmap[keys[1]] else: result += first * hmap[first] del hmap[first] return result
construct-string-with-repeat-limit
[Python] O(n) Time Solution
tejeshreddy111
0
16
construct string with repeat limit
2,182
0.519
Medium
30,298
https://leetcode.com/problems/construct-string-with-repeat-limit/discuss/1786630/Python-3-Heap-and-wait-character
class Solution: def repeatLimitedString(self, s: str, r: int) -> str: cnt = Counter(s) q = [-(ord(x) - ord('a')) for x in cnt] heapq.heapify(q) wait = '' ans = '' while q: cur = chr(ord('a') - heappop(q)) if wait: ans += cur cnt[cur] -= 1 if cnt[cur]: heappush(q, -(ord(cur) - ord('a'))) heappush(q, -(ord(wait) - ord('a'))) wait = "" else: if cnt[cur] <= r: ans += cur * cnt[cur] else: ans += cur * r cnt[cur] -= r wait = cur return ans
construct-string-with-repeat-limit
[Python 3] Heap and wait character
chestnut890123
0
28
construct string with repeat limit
2,182
0.519
Medium
30,299