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/keep-multiplying-found-values-by-two/discuss/2522123/Simple-Python-solution-beats-82 | class Solution:
def findFinalValue(self, nums: List[int], original: int) -> int:
while original in nums:
index = nums.index(original)
nums.pop(index)
original *= 2
return original | keep-multiplying-found-values-by-two | Simple Python solution beats 82% | aruj900 | 0 | 21 | keep multiplying found values by two | 2,154 | 0.731 | Easy | 29,900 |
https://leetcode.com/problems/keep-multiplying-found-values-by-two/discuss/2390179/Python3-or-Easy-Solution-Using-a-Set | class Solution:
#let n = max(nums)
#T.C = O(lg(n/original))
#S.C = O(n)
def findFinalValue(self, nums: List[int], original: int) -> int:
#Easy approach: Typecast nums array into a set since we are only
#interested if original integer is in nums array!
numbers = set(nums)
while original in numbers:
original *= 2
return original | keep-multiplying-found-values-by-two | Python3 | Easy Solution Using a Set | JOON1234 | 0 | 12 | keep multiplying found values by two | 2,154 | 0.731 | Easy | 29,901 |
https://leetcode.com/problems/keep-multiplying-found-values-by-two/discuss/2195500/Easy-Python-Solution-oror-One-Liner-oror-Faster-than-93.80 | class Solution(object):
def findFinalValue(self, nums, original):
"""
:type nums: List[int]
:type original: int
:rtype: int
"""
while(original in nums):
original = original*2
return original | keep-multiplying-found-values-by-two | Easy Python Solution || One Liner || Faster than 93.80% | aayushcode07 | 0 | 23 | keep multiplying found values by two | 2,154 | 0.731 | Easy | 29,902 |
https://leetcode.com/problems/keep-multiplying-found-values-by-two/discuss/2130710/Python-simple-solution | class Solution:
def findFinalValue(self, nums: List[int], original: int) -> int:
while original in nums:
original *= 2
return original | keep-multiplying-found-values-by-two | Python simple solution | StikS32 | 0 | 32 | keep multiplying found values by two | 2,154 | 0.731 | Easy | 29,903 |
https://leetcode.com/problems/keep-multiplying-found-values-by-two/discuss/2123573/easy-Python-Solution | class Solution:
def findFinalValue(self, nums: List[int], original: int) -> int:
a = []
for i in nums:
if i%original == 0:
a.append(i)
n = len(a)
a.sort()
if original not in a:
return original
while n >=0:
if original*2 not in a:
return original*2
original = original*2
n -= 1 | keep-multiplying-found-values-by-two | easy Python Solution | prernaarora221 | 0 | 22 | keep multiplying found values by two | 2,154 | 0.731 | Easy | 29,904 |
https://leetcode.com/problems/keep-multiplying-found-values-by-two/discuss/2029547/python-easy-hash-solution | class Solution:
def findFinalValue(self, nums: List[int], original: int) -> int:
numbers = set()
for n in nums : numbers.add(n)
while original in numbers : original *= 2
return original; | keep-multiplying-found-values-by-two | python - easy hash solution | ZX007java | 0 | 62 | keep multiplying found values by two | 2,154 | 0.731 | Easy | 29,905 |
https://leetcode.com/problems/keep-multiplying-found-values-by-two/discuss/2015236/python3-Simple-Solution-by-sorting | class Solution:
def findFinalValue(self, nums: List[int], original: int) -> int:
nums.sort()
for n in nums:
if n == original:
original *= 2
return original | keep-multiplying-found-values-by-two | [python3] Simple Solution by sorting | terrencetang | 0 | 24 | keep multiplying found values by two | 2,154 | 0.731 | Easy | 29,906 |
https://leetcode.com/problems/keep-multiplying-found-values-by-two/discuss/1991289/Python-Easiest-Solution-With-Explanation-or-Sorting-or-Beg-to-adv-or | class Solution:
def findFinalValue(self, nums: List[int], original: int) -> int:
nums.sort() # sorting the provided list.
"""
But what`s the need of sorting it?
See we could write a solution with out sorting the list also, thoug it wont work for all the situations.
Lets take a example:-
We have this list[5,6,3,1,12], original = 3
- nums[0] & nums[1] != original
- now comes to nums[2] which is equal to original.
- now original will be multiplied by 2, as given in the problem statement.
- 3*2 = 6, now if we wont find any 6 further in the list as we dont have any 6.
- But if I sort the list in the first place will have the greater value(which will always be the case as we are doing multiplication) further in the list.
"""
for i in range(len(nums)): # traversing through the list.
if nums[i] == original: # checking if the current elem is equal to original.
original = 2 * original # if it is then multiply it by 2.
return original # return the final value of original. | keep-multiplying-found-values-by-two | Python Easiest Solution With Explanation | Sorting | Beg to adv | | rlakshay14 | 0 | 41 | keep multiplying found values by two | 2,154 | 0.731 | Easy | 29,907 |
https://leetcode.com/problems/keep-multiplying-found-values-by-two/discuss/1934715/easy-python-code | class Solution:
def findFinalValue(self, nums: List[int], original: int) -> int:
while(True):
if original in nums:
original = original*2
else:
return original | keep-multiplying-found-values-by-two | easy python code | dakash682 | 0 | 37 | keep multiplying found values by two | 2,154 | 0.731 | Easy | 29,908 |
https://leetcode.com/problems/keep-multiplying-found-values-by-two/discuss/1927169/Python-Set-or-Iterative-%2B-Recursive-or-Clean-and-Simple! | class Solution:
def findFinalValue(self, nums, original):
s = set(nums)
while original in s:
original *= 2
return original | keep-multiplying-found-values-by-two | Python - Set | Iterative + Recursive | Clean and Simple! | domthedeveloper | 0 | 30 | keep multiplying found values by two | 2,154 | 0.731 | Easy | 29,909 |
https://leetcode.com/problems/keep-multiplying-found-values-by-two/discuss/1927169/Python-Set-or-Iterative-%2B-Recursive-or-Clean-and-Simple! | class Solution:
def findFinalValue(self, nums, original):
def helper(nums, original):
return original if original not in nums else helper(nums, original*2)
return helper(set(nums), original) | keep-multiplying-found-values-by-two | Python - Set | Iterative + Recursive | Clean and Simple! | domthedeveloper | 0 | 30 | keep multiplying found values by two | 2,154 | 0.731 | Easy | 29,910 |
https://leetcode.com/problems/keep-multiplying-found-values-by-two/discuss/1910622/Python3-Easy-Solution-Using-Python-Dictionary | class Solution:
def findFinalValue(self, nums: List[int], original: int) -> int:
m = {}
for num in nums:
m[num] = True
while True:
indicator = m.get(original, -1)
if indicator != -1:
original *= 2
else:
return original | keep-multiplying-found-values-by-two | Python3, Easy Solution Using Python Dictionary | Hejita | 0 | 33 | keep multiplying found values by two | 2,154 | 0.731 | Easy | 29,911 |
https://leetcode.com/problems/keep-multiplying-found-values-by-two/discuss/1878646/Python-(Simple-Approach-and-Beginner-Approach) | class Solution:
def findFinalValue(self, nums: List[int], original: int) -> int:
for _ in range(len(nums)):
if original in nums:
a = (original*2)
original = a
return original | keep-multiplying-found-values-by-two | Python (Simple Approach and Beginner-Approach) | vishvavariya | 0 | 26 | keep multiplying found values by two | 2,154 | 0.731 | Easy | 29,912 |
https://leetcode.com/problems/keep-multiplying-found-values-by-two/discuss/1868654/Python-or-Keep-Checking-if-in-Set-of-Nums | class Solution:
def findFinalValue(self, nums: List[int], original: int) -> int:
nums = set(nums)
while True:
if original in nums:
original *= 2
else:
break
return original | keep-multiplying-found-values-by-two | Python | Keep Checking if in Set of Nums | jgroszew | 0 | 27 | keep multiplying found values by two | 2,154 | 0.731 | Easy | 29,913 |
https://leetcode.com/problems/keep-multiplying-found-values-by-two/discuss/1855058/Python-faster-than-97 | class Solution:
def findFinalValue(self, nums: List[int], original: int) -> int:
while True:
if original in nums:
original *= 2
else:
return original | keep-multiplying-found-values-by-two | Python faster than 97% | alishak1999 | 0 | 32 | keep multiplying found values by two | 2,154 | 0.731 | Easy | 29,914 |
https://leetcode.com/problems/keep-multiplying-found-values-by-two/discuss/1826454/python-3-or-hash-set-or-O(n)-or-O(n) | class Solution:
def findFinalValue(self, nums: List[int], original: int) -> int:
numsSet = set(nums)
while original in numsSet:
original *= 2
return original | keep-multiplying-found-values-by-two | python 3 | hash set | O(n) | O(n) | dereky4 | 0 | 40 | keep multiplying found values by two | 2,154 | 0.731 | Easy | 29,915 |
https://leetcode.com/problems/keep-multiplying-found-values-by-two/discuss/1800781/Python3-accepted-solution | class Solution:
def findFinalValue(self, nums: List[int], original: int) -> int:
while(original in nums):
original *= 2
return original | keep-multiplying-found-values-by-two | Python3 accepted solution | sreeleetcode19 | 0 | 27 | keep multiplying found values by two | 2,154 | 0.731 | Easy | 29,916 |
https://leetcode.com/problems/keep-multiplying-found-values-by-two/discuss/1794733/4-Lines-Python-Solution-oror-75-Faster-oror-Memory-less-than-70 | class Solution:
def findFinalValue(self, nums: List[int], original: int) -> int:
while True:
if original in nums: original *= 2
else: break
return original | keep-multiplying-found-values-by-two | 4-Lines Python Solution || 75% Faster || Memory less than 70% | Taha-C | 0 | 36 | keep multiplying found values by two | 2,154 | 0.731 | Easy | 29,917 |
https://leetcode.com/problems/keep-multiplying-found-values-by-two/discuss/1785323/Python-solutionor-faster-than-99.98-of-Python3-online-submissions | class Solution:
def findFinalValue(self, nums: List[int], original: int) -> int:
while True:
if original in nums:
original=original*2
else:
break
return original | keep-multiplying-found-values-by-two | Python solution| faster than 99.98% of Python3 online submissions | Shivam_20 | 0 | 33 | keep multiplying found values by two | 2,154 | 0.731 | Easy | 29,918 |
https://leetcode.com/problems/keep-multiplying-found-values-by-two/discuss/1760493/Python3-simple-solution | class Solution:
def findFinalValue(self, nums: List[int], original: int) -> int:
while True:
if original in nums:
original *= 2
else:
return original | keep-multiplying-found-values-by-two | Python3 simple solution | EklavyaJoshi | 0 | 37 | keep multiplying found values by two | 2,154 | 0.731 | Easy | 29,919 |
https://leetcode.com/problems/keep-multiplying-found-values-by-two/discuss/1755004/Easy-Python-Solution | class Solution:
def findFinalValue(self, nums: List[int], original: int) -> int:
nums=sorted(nums)
for i in nums:
if original==i:
original=2*original
return original | keep-multiplying-found-values-by-two | Easy Python Solution | sangam92 | 0 | 43 | keep multiplying found values by two | 2,154 | 0.731 | Easy | 29,920 |
https://leetcode.com/problems/keep-multiplying-found-values-by-two/discuss/1752597/Python3-simple-solution | class Solution:
def findFinalValue(self, nums: List[int], original: int) -> int:
while original in nums:
original *= 2
return original | keep-multiplying-found-values-by-two | Python3 simple solution | noobj097 | 0 | 24 | keep multiplying found values by two | 2,154 | 0.731 | Easy | 29,921 |
https://leetcode.com/problems/keep-multiplying-found-values-by-two/discuss/1743924/Python-faster-than-93.14-submissions | class Solution:
def findFinalValue(self, nums: List[int], original: int) -> int:
nums.sort()
i, n = 0, len(nums)
while i < n:
while i < n and nums[i] != original:
i += 1
original *= 2
return original >> 1 | keep-multiplying-found-values-by-two | Python faster than 93.14% submissions | HuynhHoangPhuc | 0 | 39 | keep multiplying found values by two | 2,154 | 0.731 | Easy | 29,922 |
https://leetcode.com/problems/keep-multiplying-found-values-by-two/discuss/1741135/Python3-Faster-than-93-submissions | class Solution:
def findFinalValue(self, nums, original):
nums_set = set(nums)
while original in nums_set:
original = original*2
return original | keep-multiplying-found-values-by-two | [Python3] Faster than 93% submissions | GauravKK08 | 0 | 27 | keep multiplying found values by two | 2,154 | 0.731 | Easy | 29,923 |
https://leetcode.com/problems/keep-multiplying-found-values-by-two/discuss/1739818/Python-simple-2-line-solution | class Solution:
def findFinalValue(self, nums: List[int], original: int) -> int:
while(original in set(nums)): original=2*original
return original | keep-multiplying-found-values-by-two | Python simple 2 line solution | Rajashekar_Booreddy | 0 | 72 | keep multiplying found values by two | 2,154 | 0.731 | Easy | 29,924 |
https://leetcode.com/problems/keep-multiplying-found-values-by-two/discuss/1739795/Easy-Python-with-hashing | class Solution:
def findFinalValue(self, nums: List[int], original: int) -> int:
nums = set(nums)
while original in nums:
original *= 2
return original | keep-multiplying-found-values-by-two | Easy Python with hashing | lukasz944 | 0 | 26 | keep multiplying found values by two | 2,154 | 0.731 | Easy | 29,925 |
https://leetcode.com/problems/keep-multiplying-found-values-by-two/discuss/1732881/Python3-loop | class Solution:
def findFinalValue(self, nums: List[int], original: int) -> int:
seen = set(nums)
while original in seen: original *= 2
return original | keep-multiplying-found-values-by-two | [Python3] loop | ye15 | 0 | 15 | keep multiplying found values by two | 2,154 | 0.731 | Easy | 29,926 |
https://leetcode.com/problems/keep-multiplying-found-values-by-two/discuss/1731628/Python-set-and-sorting | class Solution:
def findFinalValue(self, nums: List[int], original: int) -> int:
while original in set(nums):
original *= 2
return original
class Solution:
def findFinalValue(self, nums: List[int], original: int) -> int:
for n in sorted(nums):
if original == n:
original *= 2
return original | keep-multiplying-found-values-by-two | Python, set and sorting | blue_sky5 | 0 | 20 | keep multiplying found values by two | 2,154 | 0.731 | Easy | 29,927 |
https://leetcode.com/problems/keep-multiplying-found-values-by-two/discuss/1730525/Easy-python-solution | class Solution:
def findFinalValue(self, nums: List[int], original: int) -> int:
arr = nums.sort()
for item in nums :
if item == original :
original = original * 2
return original | keep-multiplying-found-values-by-two | Easy python solution | shakilbabu | 0 | 13 | keep multiplying found values by two | 2,154 | 0.731 | Easy | 29,928 |
https://leetcode.com/problems/keep-multiplying-found-values-by-two/discuss/1730242/Python3-Simple-Solution-oror-Easy-way | class Solution:
def findFinalValue(self, nums: List[int], original: int) -> int:
if original not in nums:
return original
else:
for x in nums:
original *= 2
if original not in nums:
return original | keep-multiplying-found-values-by-two | [Python3] Simple Solution || Easy way | Cheems_Coder | 0 | 14 | keep multiplying found values by two | 2,154 | 0.731 | Easy | 29,929 |
https://leetcode.com/problems/keep-multiplying-found-values-by-two/discuss/1730236/Python3-easy-to-understaand | class Solution(object):
def findFinalValue(self, nums, original):
while original in nums:
original *= 2
return original | keep-multiplying-found-values-by-two | Python3 easy to understaand | castcoder | 0 | 22 | keep multiplying found values by two | 2,154 | 0.731 | Easy | 29,930 |
https://leetcode.com/problems/keep-multiplying-found-values-by-two/discuss/1730233/Python-3-EASY-Intuitive-Solution | class Solution:
def findFinalValue(self, nums: List[int], original: int) -> int:
m = max(nums)
while original <= m:
if original in nums:
original *= 2
else:
break
return original | keep-multiplying-found-values-by-two | ✅ [Python 3] EASY Intuitive Solution | JawadNoor | 0 | 22 | keep multiplying found values by two | 2,154 | 0.731 | Easy | 29,931 |
https://leetcode.com/problems/keep-multiplying-found-values-by-two/discuss/1730222/Python-oror-3-line-oror-easy | class Solution:
def findFinalValue(self, nums: List[int], x: int) -> int:
if nums.count(x)==0: # check if number is in list
return x # if not return number
return self.findFinalValue(nums,2*x) # else check for 2*number | keep-multiplying-found-values-by-two | Python || 3 line || easy | rushi_javiya | 0 | 34 | keep multiplying found values by two | 2,154 | 0.731 | Easy | 29,932 |
https://leetcode.com/problems/keep-multiplying-found-values-by-two/discuss/1736681/python-easy-solution | class Solution:
def findFinalValue(self, nums: List[int], x: int) -> int:
nums = set(nums)
while True:
if x in nums:
x *= 2
else:
break
return x | keep-multiplying-found-values-by-two | python easy solution | byuns9334 | -1 | 27 | keep multiplying found values by two | 2,154 | 0.731 | Easy | 29,933 |
https://leetcode.com/problems/all-divisions-with-the-highest-score-of-a-binary-array/discuss/1732887/Python3-scan | class Solution:
def maxScoreIndices(self, nums: List[int]) -> List[int]:
ans = [0]
cand = most = nums.count(1)
for i, x in enumerate(nums):
if x == 0: cand += 1
elif x == 1: cand -= 1
if cand > most: ans, most = [i+1], cand
elif cand == most: ans.append(i+1)
return ans | all-divisions-with-the-highest-score-of-a-binary-array | [Python3] scan | ye15 | 2 | 39 | all divisions with the highest score of a binary array | 2,155 | 0.634 | Medium | 29,934 |
https://leetcode.com/problems/all-divisions-with-the-highest-score-of-a-binary-array/discuss/1732300/Python-3-Solutions | class Solution:
def maxScoreIndices(self, nums: List[int]) -> List[int]:
n=len(nums)
zero=[0]*(n+1)
one=[0]*(n+1)
for i in range(n):
zero[i+1]=zero[i]+(nums[i]==0)
for i in range(n-1,-1,-1):
one[i]=one[i+1]+(nums[i]==1)
total = [0]*(n+1)
m=0
res=[]
for i in range(n+1):
total[i]=zero[i]+one[i]
if total[i]>m:
res=[]
m=total[i]
if total[i]==m:
res+=[i]
return res | all-divisions-with-the-highest-score-of-a-binary-array | Python 3 Solutions | vermaayush680 | 2 | 108 | all divisions with the highest score of a binary array | 2,155 | 0.634 | Medium | 29,935 |
https://leetcode.com/problems/all-divisions-with-the-highest-score-of-a-binary-array/discuss/1732300/Python-3-Solutions | class Solution:
def maxScoreIndices(self, nums: List[int]) -> List[int]:
n=len(nums)
res=[]
pref=[0]*(n+1)
for i in range(n):
pref[i+1]=pref[i]+nums[i]
zero,total,one=0,0,0
m=-1
for i in range(n+1):
one=pref[n]-pref[i]
zero=i-pref[i]
total=zero+one
if total>m:
m=total
res=[]
if total==m:
res+=[i]
return res | all-divisions-with-the-highest-score-of-a-binary-array | Python 3 Solutions | vermaayush680 | 2 | 108 | all divisions with the highest score of a binary array | 2,155 | 0.634 | Medium | 29,936 |
https://leetcode.com/problems/all-divisions-with-the-highest-score-of-a-binary-array/discuss/1732300/Python-3-Solutions | class Solution:
def maxScoreIndices(self, nums: List[int]) -> List[int]:
n=len(nums)
res=[0]
onecount=0
for i in range(n):
onecount+=(nums[i]==1)
m=onecount
for i in range(n):
onecount+=(nums[i]==0)-(nums[i]==1)
if onecount>=m:
if onecount!=m:
m=onecount
res=[]
res+=[i+1]
return res | all-divisions-with-the-highest-score-of-a-binary-array | Python 3 Solutions | vermaayush680 | 2 | 108 | all divisions with the highest score of a binary array | 2,155 | 0.634 | Medium | 29,937 |
https://leetcode.com/problems/all-divisions-with-the-highest-score-of-a-binary-array/discuss/2224510/Python-or-O(n)-step-by-step-solution | class Solution:
def maxScoreIndices(self, nums: List[int]) -> List[int]:
score = max_score = sum(nums)
highest_scores = [0]
for i, v in enumerate(nums, 1):
score += 1 if v == 0 else -1
if score > max_score:
highest_scores = [i]
max_score = score
elif score == max_score:
highest_scores.append(i)
return highest_scores | all-divisions-with-the-highest-score-of-a-binary-array | Python | O(n) step by step solution | ahmadheshamzaki | 1 | 47 | all divisions with the highest score of a binary array | 2,155 | 0.634 | Medium | 29,938 |
https://leetcode.com/problems/all-divisions-with-the-highest-score-of-a-binary-array/discuss/2224510/Python-or-O(n)-step-by-step-solution | class Solution:
def maxScoreIndices(self, nums: List[int]) -> List[int]:
dev_scores = [0] * (len(nums) + 1)
dev_scores[0] = max_score = sum(nums)
for i, v in enumerate(nums, 1):
dev_scores[i] = dev_scores[i - 1] + (1 if v == 0 else -1)
max_score = max(max_score, dev_scores[i])
return [i for i, score in enumerate(dev_scores) if score == max_score] | all-divisions-with-the-highest-score-of-a-binary-array | Python | O(n) step by step solution | ahmadheshamzaki | 1 | 47 | all divisions with the highest score of a binary array | 2,155 | 0.634 | Medium | 29,939 |
https://leetcode.com/problems/all-divisions-with-the-highest-score-of-a-binary-array/discuss/1731602/Python-Iterative-Approch | class Solution:
def maxScoreIndices(self, nums: List[int]) -> List[int]:
N = len(nums)
values = [0]*(N+1)
count = 0
for i in range(N):
values[i]=count
if nums[i] == 0:count+=1
values[-1] = count
count = 0
for i in range(N-1,-1,-1):
count+=nums[i]
values[i] += count
max_value = max(values)
return [x for x in range(N+1) if values[x] == max_value] | all-divisions-with-the-highest-score-of-a-binary-array | Python Iterative Approch | H1ccup | 1 | 53 | all divisions with the highest score of a binary array | 2,155 | 0.634 | Medium | 29,940 |
https://leetcode.com/problems/all-divisions-with-the-highest-score-of-a-binary-array/discuss/1730528/Python3-solution | class Solution:
def maxScoreIndices(self, nums: List[int]) -> List[int]:
leftSum = 0
rightSum = sum(nums)
Sum = [leftSum+rightSum]
for i in range(len(nums)):
if nums[i]==0:
leftSum +=1
if nums[i]==1:
rightSum -= 1
Sum.append(leftSum+rightSum)
maxValue = max(Sum)
return( [i for i, v in enumerate(Sum) if v==maxValue]) | all-divisions-with-the-highest-score-of-a-binary-array | Python3 solution | shakilbabu | 1 | 12 | all divisions with the highest score of a binary array | 2,155 | 0.634 | Medium | 29,941 |
https://leetcode.com/problems/all-divisions-with-the-highest-score-of-a-binary-array/discuss/2774458/Python3-Commented-and-Concise-Sliding-Window | class Solution:
def maxScoreIndices(self, nums: List[int]) -> List[int]:
# make two variables for a sliding window
left_zeros = 0
right_ones = nums.count(1)
# make a list to save the indices
indices = [0]
# make a variable to save the max amount of score
max_score = left_zeros + right_ones
# go through the array
for idx in range(len(nums)):
# check if prev was a one
if nums[idx] == 1:
right_ones -= 1
else:
left_zeros += 1
# update the indices
score = right_ones + left_zeros
if score > max_score:
indices = [idx+1]
max_score = score
elif score == max_score:
indices.append(idx+1)
return indices | all-divisions-with-the-highest-score-of-a-binary-array | [Python3] - Commented and Concise Sliding Window | Lucew | 0 | 2 | all divisions with the highest score of a binary array | 2,155 | 0.634 | Medium | 29,942 |
https://leetcode.com/problems/all-divisions-with-the-highest-score-of-a-binary-array/discuss/2722444/Python-or-One-Pass-Solution-with-Explanation-or-Runtime-99-Memory-Usage-95 | class Solution:
def maxScoreIndices(self, nums: List[int]) -> List[int]:
m = 0 # max number
n = 0 # current number
arr = [0] # indices which all have the max number
for i, b in enumerate(nums, 1):
n -= b or -1 # +1 at 0, -1 at 0
if n > m:
arr = [i] # reset indices list
m = n # update max number
elif n == m:
arr.append(i) # add index to list
return arr | all-divisions-with-the-highest-score-of-a-binary-array | Python | One-Pass Solution with Explanation | Runtime 99%, Memory Usage 95% | SakretteAmi | 0 | 4 | all divisions with the highest score of a binary array | 2,155 | 0.634 | Medium | 29,943 |
https://leetcode.com/problems/all-divisions-with-the-highest-score-of-a-binary-array/discuss/2722444/Python-or-One-Pass-Solution-with-Explanation-or-Runtime-99-Memory-Usage-95 | class Solution:
def maxScoreIndices(self, nums: List[int]) -> List[int]:
m = 0 # max number
n = 0 # current number
arr = [0] + [-1]*(len(nums)//2) # indices list with initial length
k = 1 # answer length
for i, b in enumerate(nums, 1):
n -= b or -1 # +1 at 0, -1 at 0
if n > m:
arr[0] = i # reset list
k = 1 # reset answer length
m = n # update maximum
elif n == m:
arr[k] = i # add index to list
k += 1 # increase answer length
return arr[:k] | all-divisions-with-the-highest-score-of-a-binary-array | Python | One-Pass Solution with Explanation | Runtime 99%, Memory Usage 95% | SakretteAmi | 0 | 4 | all divisions with the highest score of a binary array | 2,155 | 0.634 | Medium | 29,944 |
https://leetcode.com/problems/all-divisions-with-the-highest-score-of-a-binary-array/discuss/2137222/python-3-oror-very-simple-solution | class Solution:
def maxScoreIndices(self, nums: List[int]) -> List[int]:
curScore = maxScore = sum(nums)
res = [0]
for i, num in enumerate(nums, start=1):
if num == 0:
curScore += 1
else:
curScore -= 1
if curScore > maxScore:
res.clear()
res.append(i)
maxScore = curScore
elif curScore == maxScore:
res.append(i)
return res | all-divisions-with-the-highest-score-of-a-binary-array | python 3 || very simple solution | dereky4 | 0 | 50 | all divisions with the highest score of a binary array | 2,155 | 0.634 | Medium | 29,945 |
https://leetcode.com/problems/all-divisions-with-the-highest-score-of-a-binary-array/discuss/2034204/java-python-prefixsum-and-two-counters-(time-On-space-On) | class Solution:
def maxScoreIndices(self, nums: List[int]) -> List[int]:
z = 0
summ = sum(nums)
maxi = 0
ans = []
for i in range (len(nums)) :
if z + summ > maxi :
ans.clear()
maxi = z + summ
ans.append(i)
elif z + summ == maxi :
ans.append(i)
if nums[i] == 0 : z+=1
else : summ -= 1
if z + summ > maxi :
ans.clear()
ans.append(len(nums))
elif z + summ == maxi :
ans.append(len(nums))
return ans | all-divisions-with-the-highest-score-of-a-binary-array | java, python - prefixsum & two counters (time - On, space - On) | ZX007java | 0 | 52 | all divisions with the highest score of a binary array | 2,155 | 0.634 | Medium | 29,946 |
https://leetcode.com/problems/all-divisions-with-the-highest-score-of-a-binary-array/discuss/1954938/Py3%3A-O(n)-one-pass-left-to-right-using-nums-as-a-stack-(beats-100) | class Solution:
def maxScoreIndices(self, nums: List[int]) -> List[int]:
nums = nums[::-1]
maxScore = score = sum(nums)
indices = [0]
i = 1
while nums:
v = nums.pop()
if v == 1: score -= 1
else: score += 1
if score > maxScore:
indices = [i]
maxScore = score
elif score == maxScore:
indices.append(i)
i += 1
return indices | all-divisions-with-the-highest-score-of-a-binary-array | Py3: O(n) one pass left to right, using nums as a stack (beats 100%) | zfac062 | 0 | 39 | all divisions with the highest score of a binary array | 2,155 | 0.634 | Medium | 29,947 |
https://leetcode.com/problems/all-divisions-with-the-highest-score-of-a-binary-array/discuss/1835886/python-linear-time-solution | class Solution:
def maxScoreIndices(self, nums: List[int]) -> List[int]:
n = len(nums)
ones = nums.count(1)
zeroes = n-ones
maxDiv = max(ones, zeroes)
temp = [0]*(n+1)
temp[0] = ones; temp[n] = zeroes
left = 0; right = ones
for i in range(1, n):
if nums[i-1] == 1:
right -= 1
else:
left += 1
maxDiv = max(maxDiv, left + right)
temp[i] = left+right
return [i for i in range(n+1) if temp[i]==maxDiv] | all-divisions-with-the-highest-score-of-a-binary-array | python linear time solution | abkc1221 | 0 | 37 | all divisions with the highest score of a binary array | 2,155 | 0.634 | Medium | 29,948 |
https://leetcode.com/problems/all-divisions-with-the-highest-score-of-a-binary-array/discuss/1734440/Using-groupby-83-speed | class Solution:
def maxScoreIndices(self, nums: List[int]) -> List[int]:
zeros, ones = 0, nums.count(1)
max_score, ans = ones, [0]
i = 0
for key, grp in groupby(nums):
len_group = len(list(grp))
i += len_group
if key:
ones -= len_group
else:
zeros += len_group
score = zeros + ones
if score > max_score:
max_score = score
ans = [i]
elif score == max_score:
ans.append(i)
return ans | all-divisions-with-the-highest-score-of-a-binary-array | Using groupby, 83% speed | EvgenySH | 0 | 20 | all divisions with the highest score of a binary array | 2,155 | 0.634 | Medium | 29,949 |
https://leetcode.com/problems/all-divisions-with-the-highest-score-of-a-binary-array/discuss/1730875/Python-O(n)-time-solution-using-dictionary | class Solution:
def maxScoreIndices(self, nums: List[int]) -> List[int]:
res=[]
d=defaultdict(int)
left=0
right=nums.count(1)
ans=0
for i,n in enumerate(nums):
d[i]=left+right
ans=max(ans,left+right)
if n==0:
left+=1
if n==1:
right-=1
ans=max(ans,left+right)
d[len(nums)]=left+right
for i in d.keys():
if d[i]==ans:
res.append(i)
return res | all-divisions-with-the-highest-score-of-a-binary-array | Python O(n) time solution using dictionary | amlanbtp | 0 | 32 | all divisions with the highest score of a binary array | 2,155 | 0.634 | Medium | 29,950 |
https://leetcode.com/problems/all-divisions-with-the-highest-score-of-a-binary-array/discuss/1730698/Simple-Python-%3A | class Solution:
def maxScoreIndices(self, nums: List[int]) -> List[int]:
maxx=0
m=[]
one=nums.count(1)
zero=0
m.append(one)
for i in range(len(nums)):
if nums[i]==0:
zero+=1
else:
one-=1
m.append(zero+one)
maxx=max(m)
return [i for i in range(len(m)) if m[i]==maxx] | all-divisions-with-the-highest-score-of-a-binary-array | Simple Python : | goxy_coder | 0 | 51 | all divisions with the highest score of a binary array | 2,155 | 0.634 | Medium | 29,951 |
https://leetcode.com/problems/all-divisions-with-the-highest-score-of-a-binary-array/discuss/1730363/Python-clean-and-readable-linear-time-solution | class Solution:
def maxScoreIndices(self, nums: List[int]) -> List[int]:
ans=[]
i=0
final=[]
lc=(nums[0:i]).count(0)
rc=(nums[i:len(nums)]).count(1)
ans.append(lc+rc)
while i < (len(nums)):
a=nums[i]
if a==0:
lc+=1
else:
rc-=1
ans.append(lc+rc)
i+=1
maxx=max(ans)
for i in range(len(ans)):
if ans[i] ==maxx:
final.append(i)
return final | all-divisions-with-the-highest-score-of-a-binary-array | Python clean and readable linear time solution | RaghavGupta22 | 0 | 22 | all divisions with the highest score of a binary array | 2,155 | 0.634 | Medium | 29,952 |
https://leetcode.com/problems/all-divisions-with-the-highest-score-of-a-binary-array/discuss/1730182/Prefix-and-Suffix-Sum-Approach-Python3-or-O(N)-Solution | class Solution:
def maxScoreIndices(self, nums: List[int]) -> List[int]:
t1 = [0] #Initialise array for prefix sum
t2 = [0]*(len(nums)+1) #Initialise array for suffix sum
#Create preffix sum
for i in nums:
if not t1:
t1.append(0)
else:
if i == 0:
t1.append(t1[-1]+1)
else:
t1.append(t1[-1])
#Create suffix sum
for i in range(len(nums) - 1, -1, -1):
t2[i] = nums[i]+t2[i+1]
ans = []
#Assign score to each index
for i in range(len(t1)):
ans.append((t1[i]+t2[i], i))
#Sort in reverse order
ans.sort(reverse = True)
#Take the max score
ans1 = ans[0][0]
t = []
#Check for indexes having the max score
for i in ans:
if i[0] == ans1:
t.append(i[1])
return t | all-divisions-with-the-highest-score-of-a-binary-array | Prefix and Suffix Sum Approach Python3 | O(N) Solution | sdasstriver9 | 0 | 44 | all divisions with the highest score of a binary array | 2,155 | 0.634 | Medium | 29,953 |
https://leetcode.com/problems/find-substring-with-given-hash-value/discuss/1732936/Python3-rolling-hash | class Solution:
def subStrHash(self, s: str, power: int, modulo: int, k: int, hashValue: int) -> str:
pp = pow(power, k-1, modulo)
hs = ii = 0
for i, ch in enumerate(reversed(s)):
if i >= k: hs -= (ord(s[~(i-k)]) - 96)*pp
hs = (hs * power + (ord(ch) - 96)) % modulo
if i >= k-1 and hs == hashValue: ii = i
return s[~ii:~ii+k or None] | find-substring-with-given-hash-value | [Python3] rolling hash | ye15 | 2 | 79 | find substring with given hash value | 2,156 | 0.221 | Hard | 29,954 |
https://leetcode.com/problems/find-substring-with-given-hash-value/discuss/1735511/Python-Simple-Python-Solution-Using-Sliding-Window-and-Rolling-Hash | class Solution:
def subStrHash(self, s: str, power: int, modulo: int, k: int, hashValue: int) -> str:
sc=0
i=len(s)-1
a=0
m = (power**k)%modulo
while i>-1:
sc=(sc*power + ord(s[i])-97+1)%modulo
if i+k<len(s):
sc=(sc-((ord(s[i+k])-97+1)*m))%modulo
if sc==hashValue:
a=i
i=i-1
return s[a:a+k] | find-substring-with-given-hash-value | [ Python ] ✔✔ Simple Python Solution Using Sliding Window and Rolling Hash ✌🔥 | ASHOK_KUMAR_MEGHVANSHI | 1 | 125 | find substring with given hash value | 2,156 | 0.221 | Hard | 29,955 |
https://leetcode.com/problems/find-substring-with-given-hash-value/discuss/1731390/Self-Understandable-Python-(Rabin-Karp-Algorithm)-%3A | class Solution:
def subStrHash(self, s: str, power: int, modulo: int, k: int, hashValue: int) -> str:
p=0
mul=1
for i in range(k):
p=p+(ord(s[i])-ord('a')+1)*mul
mul=mul*power
mul=mul//power
for j in range(len(s)-k+1):
if p%modulo==hashValue:
return s[j:j+k]
if j<len(s)-k:
p=(p-(ord(s[j])-ord('a')+1))//power+(ord(s[j+k])-ord('a')+1)*mul | find-substring-with-given-hash-value | Self Understandable Python (Rabin Karp Algorithm) : | goxy_coder | 1 | 100 | find substring with given hash value | 2,156 | 0.221 | Hard | 29,956 |
https://leetcode.com/problems/find-substring-with-given-hash-value/discuss/1730481/Python-TLE-solved-by-using-pow(x-y-z)-with-modulo | class Solution:
def subStrHash(self, s: str, power: int, modulo: int, k: int, hashValue: int) -> str:
n = len(s)
if n == k: return s
l = 0
val = 0
mapp = pow(power,k-1, modulo)
s = s[::-1]
for i in range(k):
val+= ( (ord(s[i]) - ord('a') + 1) * pow(power,k-1 - i, modulo) ) % modulo
val = val % modulo
res = []
for i in range(k, n):
if i - l >= k:
if val % modulo == hashValue:
res.append( (l,i))
val = ( val - ((ord(s[l])-ord('a') + 1) * mapp) % modulo + modulo) % modulo
val = (val*power) % modulo
l+=1
val+= (ord(s[i]) - ord('a') + 1)
val = val % modulo
if val == hashValue:
res.append( (l,n) )
return s[ res[-1][0]: res[-1][1] ][::-1]
``` | find-substring-with-given-hash-value | Python TLE solved by using pow(x, y, z) with modulo | juanfirst | 1 | 74 | find substring with given hash value | 2,156 | 0.221 | Hard | 29,957 |
https://leetcode.com/problems/find-substring-with-given-hash-value/discuss/1730643/Python-or-Sliding-Window | class Solution:
def subStrHash(self, s: str, power: int, modulo: int, k: int, hashValue: int) -> str:
end = 0
start = 0
total = 0
powr = 1
for i in range(k):
total = total + ((ord(s[end]) - ord('a') + 1)) * powr
powr = powr * power
end +=1
res = total % modulo
if res == hashValue:
return s[start:end]
start = 1
end = k
powr = powr // power
while end < len(s):
total = total - ((ord(s[start-1]) - ord('a') +1))
total //=power
total = total + ((ord(s[end]) - ord('a') +1) * powr)
if total % modulo == hashValue:
return s[start:end+1]
end +=1
start +=1 | find-substring-with-given-hash-value | Python | Sliding Window | pillaimanish | 0 | 46 | find substring with given hash value | 2,156 | 0.221 | Hard | 29,958 |
https://leetcode.com/problems/find-substring-with-given-hash-value/discuss/1730360/Don't-use-Python3-use-Python.-Got-acccepted | class Solution(object):
def subStrHash(self, s, power, modulo, k, hashValue):
"""
:type s: str
:type power: int
:type modulo: int
:type k: int
:type hashValue: int
:rtype: str
"""
val = {chr(i): i-96 for i in range(97, 123)}
from collections import deque
q = deque(maxlen=k)
window_sum = 0
power_base = 1
for i in range(k):
new_letter = s[i]
q.append(new_letter)
window_sum += val[new_letter] * power_base
power_base *= power
modulo_result = window_sum % modulo
if modulo_result == hashValue:
return s[:k]
new_multiplier = power ** (k-1)
for i in range(k, len(s)):
new_letter = s[i]
old_val = val[q.popleft()]
new_val = val[new_letter] * new_multiplier
window_sum = (window_sum - old_val)//power + new_val
q.append(new_letter)
if window_sum % modulo == hashValue:
break
result = ''.join(list(q))
return result | find-substring-with-given-hash-value | Don't use Python3, use Python. Got acccepted | ck2w | 0 | 68 | find substring with given hash value | 2,156 | 0.221 | Hard | 29,959 |
https://leetcode.com/problems/find-substring-with-given-hash-value/discuss/1730325/Sliding-Window-oror-Python-oror-Simple-Solution | class Solution:
def subStrHash(self, s: str, power: int, modulo: int, k: int, hashValue: int) -> str:
sol = 0
powr = 1
for i in range(0, k, 1):
sol += (ord(s[i]) - ord('a') + 1) * powr
powr = powr * power
if sol % modulo == hashValue:
return s[:k]
powr = powr // power
for i in range(1, len(s) - k + 1, 1):
sol = ((sol - (ord(s[i - 1]) - ord('a') + 1)) // power) + ((ord(s[i + k - 1]) - ord('a') + 1) * powr)
if (sol % modulo == hashValue):
return s[i:i + k]
return "" | find-substring-with-given-hash-value | Sliding Window || Python || Simple Solution | siddp6 | 0 | 222 | find substring with given hash value | 2,156 | 0.221 | Hard | 29,960 |
https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/discuss/1747018/Python-simple-and-fast-with-explanation-no-permutation | class Solution:
def minimumSum(self, num: int) -> int:
num = sorted(str(num),reverse=True)
n = len(num)
res = 0
even_iteration = False
position = 0
for i in range(n):
res += int(num[i])*(10**position)
if even_iteration:
position += 1
even_iteration = False
else:
even_iteration = True
return res | minimum-sum-of-four-digit-number-after-splitting-digits | Python - simple & fast with explanation - no permutation | fatamorgana | 43 | 4,300 | minimum sum of four digit number after splitting digits | 2,160 | 0.879 | Easy | 29,961 |
https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/discuss/1747018/Python-simple-and-fast-with-explanation-no-permutation | class Solution:
def minimumSum(self, num: int) -> int:
num = sorted(str(num),reverse=True)
return int(num[0]) + int(num[1]) + int(num[2])*10 + int(num[3])*10 | minimum-sum-of-four-digit-number-after-splitting-digits | Python - simple & fast with explanation - no permutation | fatamorgana | 43 | 4,300 | minimum sum of four digit number after splitting digits | 2,160 | 0.879 | Easy | 29,962 |
https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/discuss/1747393/Very-easy-solution-(python) | class Solution:
def minimumSum(self, num: int) -> int:
list_digits = []
while num > 0:
list_digits.append(num % 10)
num = num // 10
a, b, c, d= sorted(list_digits)
return 10 * a + c + 10*b + d | minimum-sum-of-four-digit-number-after-splitting-digits | Very easy solution (python) | jonathan-nguyen | 4 | 559 | minimum sum of four digit number after splitting digits | 2,160 | 0.879 | Easy | 29,963 |
https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/discuss/2821979/Python-oror-Easy-oror-3Lines-oror-Sorting-oror | class Solution:
def minimumSum(self, num: int) -> int:
n=[num//1000,(num//100)%10,(num//10)%10,num%10] #This line will convert the four digit no. into array
n.sort() #It will sort the digits in ascending order
return (n[0]*10+n[3])+(n[1]*10+n[2]) # Combination of first and last and the remaining two digits will give us the minimum value | minimum-sum-of-four-digit-number-after-splitting-digits | Python || Easy || 3Lines || Sorting || | DareDevil_007 | 2 | 142 | minimum sum of four digit number after splitting digits | 2,160 | 0.879 | Easy | 29,964 |
https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/discuss/1890716/Easy-and-Simple-Solution | class Solution:
def minimumSum(self, num: int) -> int:
num=sorted(str(num))
a1= num[0]+num[3]
a2=num[1]+num[2]
return int(a1)+int(a2) | minimum-sum-of-four-digit-number-after-splitting-digits | Easy and Simple Solution | sangam92 | 2 | 132 | minimum sum of four digit number after splitting digits | 2,160 | 0.879 | Easy | 29,965 |
https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/discuss/1753758/Python-or-Simple | class Solution:
def minimumSum(self, num: int) -> int:
nums = []
while num:
nums.append(num % 10)
num //= 10
nums.sort()
return nums[0] * 10 + nums[2] + nums[1] * 10 + nums[3] | minimum-sum-of-four-digit-number-after-splitting-digits | Python | Simple | delimitry | 2 | 121 | minimum sum of four digit number after splitting digits | 2,160 | 0.879 | Easy | 29,966 |
https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/discuss/1747316/Python-two-lines | class Solution:
def minimumSum(self, num: int) -> int:
d = sorted(str(num))
return int(d[0]+d[2]) + int(d[1]+d[3]) | minimum-sum-of-four-digit-number-after-splitting-digits | Python two lines | blue_sky5 | 2 | 125 | minimum sum of four digit number after splitting digits | 2,160 | 0.879 | Easy | 29,967 |
https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/discuss/2454111/Python-or-Sort | class Solution:
def minimumSum(self, num: int) -> int:
res = sorted(str(num))
res = ''.join(res)
a = res[0] + res[2]
b = res[1] + res[3]
return int(a) + int(b) | minimum-sum-of-four-digit-number-after-splitting-digits | Python | Sort | Wartem | 1 | 91 | minimum sum of four digit number after splitting digits | 2,160 | 0.879 | Easy | 29,968 |
https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/discuss/2119901/Python-Easy-solution-with-complexities | class Solution:
def minimumSum(self, num: int) -> int:
arr = list(map(int,str(num))) #worst case time complexity of type conversion from string to integer is O(n*n) if number is very very big
arr.sort() #time O(nlogn)
num1 = arr[0] * 10 + arr[2]
num2 = arr[1] * 10 + arr[3]
return (num1 + num2)
# time O(n*n) - assuming number is very big
# time O(nlogn) - in this question
# space O(4) | minimum-sum-of-four-digit-number-after-splitting-digits | [Python] Easy solution with complexities | mananiac | 1 | 83 | minimum sum of four digit number after splitting digits | 2,160 | 0.879 | Easy | 29,969 |
https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/discuss/2119901/Python-Easy-solution-with-complexities | class Solution:
def minimumSum(self, num: int) -> int:
s = sorted(list(str(num))) #nlogn time for sorting
# print(s)
return int(s[0]+s[2])+int(s[1]+s[3])
# time O(n*n) - assuming number is very big
# time O(nlogn) - in this question
# space O(4) | minimum-sum-of-four-digit-number-after-splitting-digits | [Python] Easy solution with complexities | mananiac | 1 | 83 | minimum sum of four digit number after splitting digits | 2,160 | 0.879 | Easy | 29,970 |
https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/discuss/1904596/python-3-oror-easy-solution-without-strings | class Solution:
def minimumSum(self, num: int) -> int:
digits = []
while num:
digits.append(num % 10)
num //= 10
digits.sort()
return (digits[0]*10 + digits[2]) + (digits[1]*10 + digits[3]) | minimum-sum-of-four-digit-number-after-splitting-digits | python 3 || easy solution without strings | dereky4 | 1 | 158 | minimum sum of four digit number after splitting digits | 2,160 | 0.879 | Easy | 29,971 |
https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/discuss/1817791/Python-Easy-and-Fast-Solution-to-double-beats-90 | class Solution:
def minimumSum(self, num: int) -> int:
nums = sorted(str(num))
res = ['','']
while nums :
try :
res[0] += nums.pop(0)
res[1] += nums.pop(0)
except :
break
return int(res[0])+int(res[1]) | minimum-sum-of-four-digit-number-after-splitting-digits | [Python] Easy & Fast Solution to double beats 90% | crazypuppy | 1 | 82 | minimum sum of four digit number after splitting digits | 2,160 | 0.879 | Easy | 29,972 |
https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/discuss/1778061/Python3-Faster-Than-95.54 | class Solution:
def minimumSum(self, num: int) -> int:
s = sorted(str(num))
num1, num2 = "".join(s[0:len(s):2]), "".join(s[1:len(s):2])
return int(num1) + int(num2) | minimum-sum-of-four-digit-number-after-splitting-digits | Python3, Faster Than 95.54% | Hejita | 1 | 100 | minimum sum of four digit number after splitting digits | 2,160 | 0.879 | Easy | 29,973 |
https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/discuss/1747385/Only-2-lines-code-JavaScript-and-Python3 | class Solution:
def minimumSum(self, num: int) -> int:
arr = list(str(num))
arr.sort()
return int(int(arr[0] + arr[2]) + int(arr[1] + arr[3])) | minimum-sum-of-four-digit-number-after-splitting-digits | Only 2 lines code - JavaScript & Python3 | shakilbabu | 1 | 49 | minimum sum of four digit number after splitting digits | 2,160 | 0.879 | Easy | 29,974 |
https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/discuss/1747098/Python-3-Easy-Solution | class Solution:
def minimumSum(self, num: int) -> int:
arr=[]
while num>0:
arr.append(num%10)
num=num//10
arr.sort()
return arr[0]*10+arr[-1]+arr[1]*10+arr[-2] | minimum-sum-of-four-digit-number-after-splitting-digits | Python 3 Easy Solution | aryanagrawal2310 | 1 | 47 | minimum sum of four digit number after splitting digits | 2,160 | 0.879 | Easy | 29,975 |
https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/discuss/1747013/Simple-Python-Solution-using-Heap | class Solution:
def minimumSum(self, num: int) -> int:
heap, n = [], num
while n > 0: # Push all digits of num into heap
heappush(heap, n % 10)
n = n // 10
nums1 = nums2 = 0
while len(heap) > 0: # Use smallest digits to construct each number
v1 = heappop(heap)
nums1 = nums1 * 10 + v1
if len(heap) > 0:
v2 = heappop(heap)
nums2 = nums2 * 10 + v2
return nums1 + nums2 | minimum-sum-of-four-digit-number-after-splitting-digits | Simple Python Solution using Heap | anCoderr | 1 | 156 | minimum sum of four digit number after splitting digits | 2,160 | 0.879 | Easy | 29,976 |
https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/discuss/2826267/Easy-and-Fast-Python-Solution-(Beats-99.5) | class Solution:
def minimumSum(self, num: int) -> int:
digits = list(str(num))
digits.sort()
return int(digits[0] + digits[3]) + int(digits[1] + digits[2]) | minimum-sum-of-four-digit-number-after-splitting-digits | Easy and Fast Python Solution (Beats 99.5%) | PranavBhatt | 0 | 1 | minimum sum of four digit number after splitting digits | 2,160 | 0.879 | Easy | 29,977 |
https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/discuss/2820972/Simple-Python-solution | class Solution:
def minimumSum(self, num: int) -> int:
s=str(num)
res="".join(sorted(s))
s1=int(res[0]+res[2])
s2=int(res[1]+res[3])
return s1+s2 | minimum-sum-of-four-digit-number-after-splitting-digits | Simple Python solution | adi434 | 0 | 1 | minimum sum of four digit number after splitting digits | 2,160 | 0.879 | Easy | 29,978 |
https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/discuss/2819228/Python-Simple-Solution | class Solution:
def minimumSum(self, num: int) -> int:
li=list(str(num))
li.sort()
return int(li[0]+li[2])+int(li[1]+li[-1]) | minimum-sum-of-four-digit-number-after-splitting-digits | Python Simple Solution | findakshaybhat | 0 | 2 | minimum sum of four digit number after splitting digits | 2,160 | 0.879 | Easy | 29,979 |
https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/discuss/2762773/python-faster-solution | class Solution:
def minimumSum(self, num: int) -> int:
num = sorted(str(num),reverse=True)
n = len(num)
res = 0
even_iteration = False
position = 0
for i in range(n):
res += int(num[i])*(10**position)
if even_iteration:
position += 1
even_iteration = False
else:
even_iteration = True
return res | minimum-sum-of-four-digit-number-after-splitting-digits | python faster solution | avs-abhishek123 | 0 | 3 | minimum sum of four digit number after splitting digits | 2,160 | 0.879 | Easy | 29,980 |
https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/discuss/2757965/Simple-Python-Solution-Using-Sort | class Solution:
def minimumSum(self, num: int) -> int:
num = str(num)
num = [i for i in num]
num = [int(i) for i in num]
num.sort()
left,right = 0 , len(num)-1
ans = []
while left < right:
ans.append(str(num[left]) + str(num[right]))
left+= 1
right -=1
ans = [int(i) for i in ans]
return sum(ans) | minimum-sum-of-four-digit-number-after-splitting-digits | Simple Python Solution Using Sort | vijay_2022 | 0 | 4 | minimum sum of four digit number after splitting digits | 2,160 | 0.879 | Easy | 29,981 |
https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/discuss/2744911/Fastest-Python-Solution | class Solution:
def minimumSum(self, num: int) -> int:
num = sorted(str(num))
return int(num[0]+num[2])+int(num[1]+num[3]) | minimum-sum-of-four-digit-number-after-splitting-digits | Fastest Python Solution | keioon | 0 | 5 | minimum sum of four digit number after splitting digits | 2,160 | 0.879 | Easy | 29,982 |
https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/discuss/2717832/Python-solution-oror-Faster-than-98-and-beats-96-in-memory-usage. | class Solution:
def minimumSum(self, num: int) -> int:
s = str(num)
lst = []
num1 = ''
num2 = ''
for i in s:
lst.append(int(i))
lst.sort()
num1 += str(lst[0])
num1 += str(lst[2])
num2 += str(lst[1])
num2 += str(lst[3])
return int(num1) + int(num2) | minimum-sum-of-four-digit-number-after-splitting-digits | Python solution || Faster than 98% and beats 96% in memory usage. | el_ju1ce | 0 | 3 | minimum sum of four digit number after splitting digits | 2,160 | 0.879 | Easy | 29,983 |
https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/discuss/2682043/Mr.Easiest | class Solution:
def minimumSum(self, num: int) -> int:
a=sorted(str(num))
return int(a[0]+a[3])+int(a[1]+a[2]) | minimum-sum-of-four-digit-number-after-splitting-digits | Mr.Easiest | liontech_123 | 0 | 12 | minimum sum of four digit number after splitting digits | 2,160 | 0.879 | Easy | 29,984 |
https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/discuss/2678302/easy-solution-Python | class Solution:
def minimumSum(self, num: int) -> int:
num = str(num)
num_lst = [int(x) for x in num]
num_lst.sort()
num1=list()
num2=list()
num1_boolean = True
for i in num_lst:
if num1_boolean == True:
num1.append(i)
num1_boolean = False
else:
num2.append(i)
num1_boolean = True
num1 = [str(x) for x in num1]
num2 = [str(x) for x in num2]
num1 = int(''.join(num1))
num2 = int(''.join(num2))
return num1+num2 | minimum-sum-of-four-digit-number-after-splitting-digits | easy solution Python | MaryLuz | 0 | 4 | minimum sum of four digit number after splitting digits | 2,160 | 0.879 | Easy | 29,985 |
https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/discuss/2666929/SIMPLE-PYTHON-SOLUTION(99-fast) | class Solution:
def minimumSum(self, num: int) -> int:
l=[]
while num>0:
l.append(num%10)
num//=10
l.sort()
n1=l[0]*10+l[2]
n2=l[1]*10+l[3]
return n1+n2 | minimum-sum-of-four-digit-number-after-splitting-digits | SIMPLE PYTHON SOLUTION(99% fast) | Hashir311 | 0 | 6 | minimum sum of four digit number after splitting digits | 2,160 | 0.879 | Easy | 29,986 |
https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/discuss/2664432/Python-solution-without-sorting-beginner-please-give-criticism! | class Solution:
def minimumSum(self,num: int) -> int:
num = str(num)
num1Digit1 = int(num[0])
num1Digit1Index = 0
num1Digit2 = 0
num2Digit1 = math.inf
num2Digit1Index = 0
num2Digit2 = 0
num1Digit2Picked = False
# Get smallest digit in num as first digit in num1
for i,n in enumerate(num):
if int(n) < num1Digit1:
num1Digit1 = int(n)
num1Digit1Index = i
# Get second smallest digit in num as first digit in num2
for i,n in enumerate(num):
if int(n) <= num2Digit1 and i != num1Digit1Index:
num2Digit1 = int(n)
num2Digit1Index = i
# Set second digits of num1 and num2
for i,n in enumerate(num):
if i != num1Digit1Index and i != num2Digit1Index and num1Digit2Picked == False:
num1Digit2 = n
num1Digit2Picked = True
elif i != num1Digit1Index and i != num2Digit1Index:
num2Digit2 = n
num1 = int(str(num1Digit1) + num1Digit2)
num2 = int(str(num2Digit1) + num2Digit2)
return num1 + num2 | minimum-sum-of-four-digit-number-after-splitting-digits | Python solution without sorting - beginner, please give criticism! | flufe | 0 | 8 | minimum sum of four digit number after splitting digits | 2,160 | 0.879 | Easy | 29,987 |
https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/discuss/2655759/easy-logical-python-solution | class Solution:
def minimumSum(self, num: int) -> int:
nums= (sorted(str(num)))
return int(nums[0] +nums[2]) + int(nums[1]+nums[3]) | minimum-sum-of-four-digit-number-after-splitting-digits | easy logical python solution | asamarka | 0 | 5 | minimum sum of four digit number after splitting digits | 2,160 | 0.879 | Easy | 29,988 |
https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/discuss/2633182/Python-solution-97.33-faster | class Solution:
def minimumSum(self, num: int) -> int:
num = sorted(str(num))
min_sum = int(num[0]+num[3]) + int(num[1]+num[2])
return min_sum | minimum-sum-of-four-digit-number-after-splitting-digits | Python solution 97.33% faster | samanehghafouri | 0 | 24 | minimum sum of four digit number after splitting digits | 2,160 | 0.879 | Easy | 29,989 |
https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/discuss/2479943/Python3-or99-faster | ```class Solution:
def minimumSum(self, num: int) -> int:
l=[0]*4
s=str(num)
for i in range(0,4):
l[i]=int(s[i])
l.sort()
add=int(str(l[0])+str(l[2]))+int(str(l[1])+str(l[3]))
return add | minimum-sum-of-four-digit-number-after-splitting-digits | Python3 |99% faster | KavitaPatel966 | 0 | 43 | minimum sum of four digit number after splitting digits | 2,160 | 0.879 | Easy | 29,990 |
https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/discuss/2470239/Very-simple-python-solution | class Solution:
def minimumSum(self, num: int) -> int:
s = str(num)
s1 = sorted(s)
new1 = ""
new1 += s1[0] + s1[2]
new2 = s1[1] + s1[3]
return int(new1) + int(new2) | minimum-sum-of-four-digit-number-after-splitting-digits | Very simple python solution | aruj900 | 0 | 49 | minimum sum of four digit number after splitting digits | 2,160 | 0.879 | Easy | 29,991 |
https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/discuss/2449268/Python3-Simple-with-Explanation | class Solution:
def minimumSum(self, num: int) -> int:
# Make a list of the digits in num and sort them
nums = list(str(num))
nums.sort()
num1 = num2 = 0
i = 1
# Now loop through the digits, assigning one to num1 and one to num2 each time
# Start by taking the larget numbers in the list, then move on to the smaller ones
# This is so that higher digits (9) have lower 10 positions (39 < 93), and smaller digits have higher ones
# Each time through, multiply by an increasing 10 position multiplier, i
while nums:
num1 += int(nums.pop()) * i
num2 += int(nums.pop()) * i
i *= 10
return num1 + num2 | minimum-sum-of-four-digit-number-after-splitting-digits | [Python3] Simple with Explanation | connorthecrowe | 0 | 50 | minimum sum of four digit number after splitting digits | 2,160 | 0.879 | Easy | 29,992 |
https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/discuss/2448276/Python3-or-Efficient-two-liner-with-explanations | class Solution:
def minimumSum(self, num: int) -> int:
'''Time complexity: Olog(n)'''
# Note that we just need to care about pairs of digits as their sums are always smaller than the sums of single digit + triple digits
# Sort num
num = sorted(str(num))
# Smallest sum of pairs is simply (num[0], num[2]) + (num[1], num[3])
return int(num[0] + num[2]) + int(num[1] + num[3]) | minimum-sum-of-four-digit-number-after-splitting-digits | Python3 | Efficient two-liner with explanations | romejj | 0 | 39 | minimum sum of four digit number after splitting digits | 2,160 | 0.879 | Easy | 29,993 |
https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/discuss/2375900/Python3-Solution-with-using-maxmin-search | class Solution:
def minimumSum(self, num: int) -> int:
min1, min2 = -1, -1
max1, max2 = -1, -1
while num > 0:
digit = num % 10
num //= 10
if digit > max1:
max2, min1, min2 = max1, max2, min1
max1 = digit
elif digit > max2:
min1, min2 = max2, min1
max2 = digit
else:
min2 = min1
min1 = digit
return min1 * 10 + min2 * 10 + max1 + max2 | minimum-sum-of-four-digit-number-after-splitting-digits | [Python3] Solution with using max/min search | maosipov11 | 0 | 39 | minimum sum of four digit number after splitting digits | 2,160 | 0.879 | Easy | 29,994 |
https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/discuss/2279556/Python3-easy-solution-Runtime%3A-41-ms-faster-than-65.68-Memory-Usage%3A-13.8-MB-less-than-56.27 | class Solution:
def minimumSum(self, num: int) -> int:
digits = sorted([int(x) for x in str(num)])
print (digits)
new1 = int(str(digits[0])+str(digits[2]))
new2 = int(str(digits[1])+str(digits[3]))
return (new1+new2) | minimum-sum-of-four-digit-number-after-splitting-digits | Python3 easy solution - Runtime: 41 ms, faster than 65.68%, Memory Usage: 13.8 MB, less than 56.27% | psnakhwa | 0 | 41 | minimum sum of four digit number after splitting digits | 2,160 | 0.879 | Easy | 29,995 |
https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/discuss/2255978/Easy-python-solution-or-sorting | '''class Solution:
def minimumSum(self, num: int) -> int:
l=list(str(num))
l.sort()
firstPair=l[0]+l[3]
secondPair=l[1]+l[2]
summ=int(firstPair)+int(secondPair)
return summ
print(minimumSum(num))''' | minimum-sum-of-four-digit-number-after-splitting-digits | Easy python solution | sorting | keertika27 | 0 | 47 | minimum sum of four digit number after splitting digits | 2,160 | 0.879 | Easy | 29,996 |
https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/discuss/2247077/clean-o(1) | class Solution:
def minimumSum(self, num: int) -> int:
num = list(str(num))
num.sort()
dit1 = int(num[0]+num[2])
dit2 = int(num[1]+num[3])
print(dit1)
print(dit2)
return (dit1+dit2) | minimum-sum-of-four-digit-number-after-splitting-digits | clean o(1) | dinesh3reddy | 0 | 36 | minimum sum of four digit number after splitting digits | 2,160 | 0.879 | Easy | 29,997 |
https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/discuss/2205845/Python-oror-Simple | class Solution:
def minimumSum(self, num: int) -> int:
nums = sorted( str(num)[:])
a = nums[0] + nums[-1]
b = nums[1] + nums[2]
return int(a) + int(b) | minimum-sum-of-four-digit-number-after-splitting-digits | Python || Simple | morpheusdurden | 0 | 60 | minimum sum of four digit number after splitting digits | 2,160 | 0.879 | Easy | 29,998 |
https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/discuss/2148268/Python-or-Easy-and-Understanding-Solution | class Solution:
def minimumSum(self, num: int) -> int:
lis=[0]*4
for i in range(4):
lis[i]=num%10
num//=10
lis.sort()
num1=(10*lis[0])+lis[3]
num2=(10*lis[1])+lis[2]
return num1+num2 | minimum-sum-of-four-digit-number-after-splitting-digits | Python | Easy & Understanding Solution | backpropagator | 0 | 54 | minimum sum of four digit number after splitting digits | 2,160 | 0.879 | Easy | 29,999 |
Subsets and Splits