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/minimum-sum-of-four-digit-number-after-splitting-digits/discuss/2082479/java-python-easy
|
class Solution:
def minimumSum(self, num: int) -> int:
digits = []
while num != 0 :
digits.append(num % 10)
num //= 10
digits.sort()
return (digits[0] + digits[1])*10 + digits[2] + digits[3]
|
minimum-sum-of-four-digit-number-after-splitting-digits
|
java, python - easy
|
ZX007java
| 0
| 135
|
minimum sum of four digit number after splitting digits
| 2,160
| 0.879
|
Easy
| 30,000
|
https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/discuss/1988944/Python-Solution-or-Simple-Sorting-Based-or-Straight-Forward-Solution
|
class Solution:
def minimumSum(self, num: int) -> int:
num = sorted(str(num))
return (int(num[0]) * 10) + int(num[3]) + (int(num[1]) * 10) + int(num[2])
|
minimum-sum-of-four-digit-number-after-splitting-digits
|
Python Solution | Simple Sorting Based | Straight-Forward Solution
|
Gautam_ProMax
| 0
| 89
|
minimum sum of four digit number after splitting digits
| 2,160
| 0.879
|
Easy
| 30,001
|
https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/discuss/1884080/Python3-simple-solution
|
class Solution:
def minimumSum(self, num: int) -> int:
num = str(num)
l = [int(num[0]),int(num[1]),int(num[2]),int(num[3])]
l.sort()
return (l[0]*10 + l[3]) + (l[1]*10 + l[2])
|
minimum-sum-of-four-digit-number-after-splitting-digits
|
Python3 simple solution
|
EklavyaJoshi
| 0
| 55
|
minimum sum of four digit number after splitting digits
| 2,160
| 0.879
|
Easy
| 30,002
|
https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/discuss/1863176/Python-easy-solution-by-sorting-digits
|
class Solution:
def minimumSum(self, num: int) -> int:
num_str = sorted(str(num))
return int(num_str[0] + num_str[2]) + int(num_str[1] + num_str[3])
|
minimum-sum-of-four-digit-number-after-splitting-digits
|
Python easy solution by sorting digits
|
alishak1999
| 0
| 77
|
minimum sum of four digit number after splitting digits
| 2,160
| 0.879
|
Easy
| 30,003
|
https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/discuss/1838025/Using-list-and-sorting
|
class Solution:
def minimumSum(self, num: int) -> int:
l = []
while num:
n = num%10
l.append(n)
num = num//10
l.sort()
num1 = num2 = 0
num1 = (num1+l[0])*10+(l[2])
num2 = (num2+l[1])*10+(l[3])
return num1+num2
|
minimum-sum-of-four-digit-number-after-splitting-digits
|
Using list and sorting
|
prajwalahluwalia
| 0
| 45
|
minimum sum of four digit number after splitting digits
| 2,160
| 0.879
|
Easy
| 30,004
|
https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/discuss/1802163/Python-16ms-solution
|
class Solution:
def minimumSum(self, num: int) -> int:
yst=list(str(num))
yst.sort()
x,y=int(yst[0]+yst[3]),int(yst[1]+yst[2])
return x+y
|
minimum-sum-of-four-digit-number-after-splitting-digits
|
Python 16ms solution
|
AjayKadiri
| 0
| 85
|
minimum sum of four digit number after splitting digits
| 2,160
| 0.879
|
Easy
| 30,005
|
https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/discuss/1764364/Python-95-Faster.-Simple-Solution-with-comments
|
class Solution:
def minimumSum(self, num: int) -> int:
#break down the number into 4 elements in a list sort asc
sl = sorted(list(str(num)))
#combine positions [0,3] and [1,2] of the list and sum it
return(int(sl[0] + sl[3])+int(sl[1] + sl[2]))
|
minimum-sum-of-four-digit-number-after-splitting-digits
|
Python 95% Faster. Simple Solution with comments
|
ovidaure
| 0
| 105
|
minimum sum of four digit number after splitting digits
| 2,160
| 0.879
|
Easy
| 30,006
|
https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/discuss/1761625/Python-easy-commented-solution
|
class Solution:
def minimumSum(self, num: int) -> int:
# Making num a string in order to operate with the digits later
num = str(num)
new1, new2 = '', ''
aux = []
final = []
# Appending digits to the list to sort it later
for i in num:
aux.append(int(i))
print(aux)
# Sorting the list
aux = str(sorted(aux))
# This for is for getting rid of the '[,]' and ','
for i in aux:
if i.isnumeric():
final.append(i)
# And we've come to the final and essential part
new1 += str(final[0]) + str(final[2])
new2 += str(final[1]) + str(final[3])
return int(new1) + int(new2)
|
minimum-sum-of-four-digit-number-after-splitting-digits
|
Python easy commented solution
|
alexmeaun99
| 0
| 54
|
minimum sum of four digit number after splitting digits
| 2,160
| 0.879
|
Easy
| 30,007
|
https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/discuss/1754704/Absolute-abomination-of-an-answer-(Top-95)
|
class Solution:
def minimumSum(self, num: int) -> int:
numlist = sorted(str(num))
return ((int(numlist[0])*10)+int(numlist[3]))+((int(numlist[1])*10)+int(numlist[2]))
|
minimum-sum-of-four-digit-number-after-splitting-digits
|
Absolute abomination of an answer (Top 95%)
|
griggs0445
| 0
| 47
|
minimum sum of four digit number after splitting digits
| 2,160
| 0.879
|
Easy
| 30,008
|
https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/discuss/1752600/Python3-one-liner-sort%2Bgreedy
|
class Solution:
def minimumSum(self, num: int) -> int:
return sum(w*int(d) for w,d in zip([10,10,1,1],sorted(str(num))))
|
minimum-sum-of-four-digit-number-after-splitting-digits
|
Python3 one-liner, sort+greedy
|
vsavkin
| 0
| 36
|
minimum sum of four digit number after splitting digits
| 2,160
| 0.879
|
Easy
| 30,009
|
https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/discuss/1749342/My-python-solution
|
class Solution:
def minimumSum(self, num: int) -> int:
new_arr = []
number1 = ""
number2 = ""
summ = 0
num_str = str(num)
for digit in num_str:
new_arr.append(digit)
new_arr.sort()
number1 += new_arr[0]
number1 += new_arr[2]
number2 += new_arr[1]
number2 += new_arr[3]
summ = int(number1) + int(number2)
return summ
|
minimum-sum-of-four-digit-number-after-splitting-digits
|
My python solution
|
andrew_fd
| 0
| 37
|
minimum sum of four digit number after splitting digits
| 2,160
| 0.879
|
Easy
| 30,010
|
https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/discuss/1748608/Python-Simple-and-Clean-Python-Solution-by-creating-all-permutations
|
class Solution:
def minimumSum(self, num: int) -> int:
from itertools import permutations
n=[]
for i in str(num):
n.append(i)
comb = permutations(n, 4)
ans=10000000000000
for t in comb:
a=list(t)
for i in range(1,len(a)):
ans=min(ans,int(''.join(a[:i]))+int(''.join(a[i:])) )
return ans
|
minimum-sum-of-four-digit-number-after-splitting-digits
|
[ Python ] ✔✔ Simple and Clean Python Solution by creating all permutations
|
ASHOK_KUMAR_MEGHVANSHI
| 0
| 47
|
minimum sum of four digit number after splitting digits
| 2,160
| 0.879
|
Easy
| 30,011
|
https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/discuss/1747704/Python-or-Straightforward-or-Simple
|
class Solution:
def minimumSum(self, num: int) -> int:
string = str(num)
arr = [char for char in string]
arr.sort()
# two possible combination
result1 = int(arr[0]+ arr[2]) + int(arr[1] + arr[3])
result2 = int(arr[1]+ arr[2]) + int(arr[0] + arr[3])
return min(result1, result2)
|
minimum-sum-of-four-digit-number-after-splitting-digits
|
Python | Straightforward | Simple
|
Mikey98
| 0
| 31
|
minimum sum of four digit number after splitting digits
| 2,160
| 0.879
|
Easy
| 30,012
|
https://leetcode.com/problems/partition-array-according-to-given-pivot/discuss/1748566/Python-Simple-and-Clean-Python-Solution-by-Removing-and-Appending
|
class Solution:
def pivotArray(self, nums: List[int], pivot: int) -> List[int]:
ans=[]
nums.remove(pivot)
i=0
ans.append(pivot)
for j in nums:
if j<pivot:
ans.insert(i,j)
i=i+1
elif j==pivot:
ans.insert(i+1,j)
else:
ans.append(j)
return ans
|
partition-array-according-to-given-pivot
|
[ Python ] ✔✔ Simple and Clean Python Solution by Removing and Appending
|
ASHOK_KUMAR_MEGHVANSHI
| 5
| 475
|
partition array according to given pivot
| 2,161
| 0.845
|
Medium
| 30,013
|
https://leetcode.com/problems/partition-array-according-to-given-pivot/discuss/2082649/java-python3-easy-to-understand
|
class Solution:
def pivotArray(self, nums: List[int], pivot: int) -> List[int]:
less = 0
equal = 0
for n in nums :
if n < pivot : less += 1
elif n == pivot : equal += 1
greater = less + equal
equal = less
less = 0
ans = [0]*len(nums)
for n in nums :
if n < pivot :
ans[less] = n
less += 1
elif n == pivot :
ans[equal] = n
equal += 1
else :
ans[greater] = n
greater += 1
return ans
|
partition-array-according-to-given-pivot
|
java, python3 - easy to understand
|
ZX007PI
| 1
| 121
|
partition array according to given pivot
| 2,161
| 0.845
|
Medium
| 30,014
|
https://leetcode.com/problems/partition-array-according-to-given-pivot/discuss/1747309/Simple-Python-Solution-in-O(N)-Time
|
class Solution:
def pivotArray(self, nums: List[int], pivot: int) -> List[int]:
less = []
greater = []
equal = []
for n in nums:
if n < pivot:
less.append(n)
elif n > pivot:
greater.append(n)
else:
equal.append(n)
return less+equal+greater
|
partition-array-according-to-given-pivot
|
Simple Python Solution in O(N) Time
|
a_knotty_mathematician
| 1
| 78
|
partition array according to given pivot
| 2,161
| 0.845
|
Medium
| 30,015
|
https://leetcode.com/problems/partition-array-according-to-given-pivot/discuss/2820953/O(n)-really-easy-to-understand-solution
|
class Solution:
def pivotArray(self, nums: List[int], pivot: int) -> List[int]:
#left side:
i = 0
left = []
while i < len(nums):
if nums[i] < pivot:
left.append(nums[i])
i += 1
#right side:
i = 0
right = []
while i < len(nums):
if nums[i] > pivot:
right.append(nums[i])
i += 1
#pivot and equals to it
i = 0
pivot_equals = []
while i < len(nums):
if nums[i] == pivot:
pivot_equals.append(nums[i])
i += 1
nums = left + pivot_equals + right
return nums
|
partition-array-according-to-given-pivot
|
O(n) really easy to understand solution
|
khaled_achech
| 0
| 5
|
partition array according to given pivot
| 2,161
| 0.845
|
Medium
| 30,016
|
https://leetcode.com/problems/partition-array-according-to-given-pivot/discuss/2820205/Python3-solution
|
class Solution:
def pivotArray(self, nums: List[int], pivot: int) -> List[int]:
lesser = []
greater = []
for num in nums:
if (num < pivot):
lesser.append(num)
elif num > pivot:
greater.append(num)
return lesser + ([pivot] * nums.count(pivot)) + greater
|
partition-array-according-to-given-pivot
|
Python3 solution
|
SupriyaArali
| 0
| 2
|
partition array according to given pivot
| 2,161
| 0.845
|
Medium
| 30,017
|
https://leetcode.com/problems/partition-array-according-to-given-pivot/discuss/2814639/Python-or-O(n)-or-Easy-to-understand-or-Basic-logic
|
class Solution:
def pivotArray(self, nums: List[int], pivot: int) -> List[int]:
l1 = []
l2 = []
l3 = []
for i in nums:
if i < pivot:
l1 += [i]
elif i > pivot:
l3 += [i]
else:
l2 += [pivot]
l = l1+l2+l3
return l
|
partition-array-according-to-given-pivot
|
Python | O(n) | Easy to understand | Basic logic
|
bhuvneshwar906
| 0
| 4
|
partition array according to given pivot
| 2,161
| 0.845
|
Medium
| 30,018
|
https://leetcode.com/problems/partition-array-according-to-given-pivot/discuss/2804954/Python-Solution-EASY-TO-UNDERSTAND
|
class Solution:
def pivotArray(self, nums: List[int], pivot: int) -> List[int]:
l=[]
r=[]
p=[]
for i in nums:
if i<pivot:
l.append(i)
for i in nums:
if i>pivot:
r.append(i)
for i in nums:
if i==pivot:
p.append(i)
return l+p+r
|
partition-array-according-to-given-pivot
|
Python Solution - EASY TO UNDERSTAND
|
T1n1_B0x1
| 0
| 4
|
partition array according to given pivot
| 2,161
| 0.845
|
Medium
| 30,019
|
https://leetcode.com/problems/partition-array-according-to-given-pivot/discuss/2789596/Easy-to-understand-Python-solution
|
class Solution:
def pivotArray(self, nums: List[int], pivot: int) -> List[int]:
count, arr = 0, []
for num in nums:
if num < pivot:
arr.append(num)
elif num == pivot:
count += 1
arr.extend([pivot] * count)
for num in nums:
if num > pivot:
arr.append(num)
return arr
|
partition-array-according-to-given-pivot
|
Easy to understand Python solution
|
Aayush3014
| 0
| 5
|
partition array according to given pivot
| 2,161
| 0.845
|
Medium
| 30,020
|
https://leetcode.com/problems/partition-array-according-to-given-pivot/discuss/2788969/Basic-python-solutionor-easy-to-understand-or-O(n)
|
class Solution:
def pivotArray(self, nums: List[int], pivot: int) -> List[int]:
left = []
right = []
pivots = []
for i in nums:
if i<pivot:
left.append(i)
elif i > pivot:
right.append(i)
else:
pivots.append(i)
for j in pivots:
left.append(j)
for k in right:
left.append(k)
return left
|
partition-array-according-to-given-pivot
|
Basic python solution| easy to understand | O(n)
|
sheetalpatne
| 0
| 5
|
partition array according to given pivot
| 2,161
| 0.845
|
Medium
| 30,021
|
https://leetcode.com/problems/partition-array-according-to-given-pivot/discuss/2788711/Python-1-line-code
|
class Solution:
def pivotArray(self, nums: List[int], pivot: int) -> List[int]:
return [x for x in nums if x<pivot]+[pivot]*(nums.count(pivot))+[t for t in nums if t>pivot]
|
partition-array-according-to-given-pivot
|
Python 1 line code
|
kumar_anand05
| 0
| 2
|
partition array according to given pivot
| 2,161
| 0.845
|
Medium
| 30,022
|
https://leetcode.com/problems/partition-array-according-to-given-pivot/discuss/2772811/Python-simple-one-liner
|
class Solution:
def pivotArray(self, nums: List[int], pivot: int) -> List[int]:
return [i for i in nums if i<pivot]+[i for i in nums if i==pivot]+[i for i in nums if i>pivot]
|
partition-array-according-to-given-pivot
|
Python simple one liner
|
Jishnu_69
| 0
| 4
|
partition array according to given pivot
| 2,161
| 0.845
|
Medium
| 30,023
|
https://leetcode.com/problems/partition-array-according-to-given-pivot/discuss/2751978/Simple-Python-Solution
|
class Solution:
def pivotArray(self, nums: List[int], pivot: int) -> List[int]:
less = []; equal = []; greater = []
for i in nums:
if (i < pivot): less.append(i);
for i in nums:
if (i == pivot): equal.append(i);
for i in nums:
if (i > pivot): greater.append(i);
return less + equal + greater
|
partition-array-according-to-given-pivot
|
Simple Python Solution
|
avinashdoddi2001
| 0
| 5
|
partition array according to given pivot
| 2,161
| 0.845
|
Medium
| 30,024
|
https://leetcode.com/problems/partition-array-according-to-given-pivot/discuss/2718032/Python3-solution-clean-code-with-full-comments.-93.55-space.
|
class Solution:
def pivotArray(self, nums: List[int], pivot: int) -> List[int]:
queue_1 = [] # Queue for integers that are smaller than pivot.
queue_2 = [] # Queue for integers that are bigger than pivot.
queue_p = [] # Queue for integers that are equal to the pivot value.
for i in range(len(nums)):
if nums[i] < pivot:
queue_1.append(nums[i])
elif nums[i] == pivot:
queue_p.append(nums[i])
else:
queue_2.append(nums[i])
# Queue FIFO attribute will give us the wanted order of the list.
return list(queue_1 + queue_p + queue_2)
# Runtime: 2203 ms, faster than 72.74% of Python3 online submissions for Partition Array According to Given Pivot.
# Memory Usage: 30.8 MB, less than 93.55% of Python3 online submissions for Partition Array According to Given Pivot.
# If you like mt work and found it helpful, then I'll appreciate a like. Thanks!
|
partition-array-according-to-given-pivot
|
Python3 solution, clean code with full comments. 93.55% space.
|
375d
| 0
| 20
|
partition array according to given pivot
| 2,161
| 0.845
|
Medium
| 30,025
|
https://leetcode.com/problems/partition-array-according-to-given-pivot/discuss/2698299/Python3-Simple-Solution
|
class Solution:
def pivotArray(self, nums: List[int], pivot: int) -> List[int]:
res = []
for n in nums:
if n < pivot: res.append(n)
for n in nums:
if n == pivot: res.append(n)
for n in nums:
if n > pivot: res.append(n)
return res
|
partition-array-according-to-given-pivot
|
Python3 Simple Solution
|
mediocre-coder
| 0
| 2
|
partition array according to given pivot
| 2,161
| 0.845
|
Medium
| 30,026
|
https://leetcode.com/problems/partition-array-according-to-given-pivot/discuss/2693509/Python3-One-Liner-(Silly)
|
class Solution:
def pivotArray(self, nums: List[int], pivot: int) -> List[int]:
return [i for i in nums if i < pivot] + [i for i in nums if i == pivot] + [i for i in nums if i > pivot]
|
partition-array-according-to-given-pivot
|
Python3 One Liner (Silly)
|
godshiva
| 0
| 8
|
partition array according to given pivot
| 2,161
| 0.845
|
Medium
| 30,027
|
https://leetcode.com/problems/partition-array-according-to-given-pivot/discuss/2670168/Python3-one-line-solution!-time-complexity-beats-92.17
|
class Solution:
def pivotArray(self, nums: List[int], pivot: int) -> List[int]:
return [x for x in nums if x < pivot] + [x for x in nums if x == pivot] + [x for x in nums if x > pivot]
|
partition-array-according-to-given-pivot
|
Python3 one-line solution! time complexity beats 92.17%
|
sipi09
| 0
| 6
|
partition array according to given pivot
| 2,161
| 0.845
|
Medium
| 30,028
|
https://leetcode.com/problems/partition-array-according-to-given-pivot/discuss/2665862/Python-O(N)
|
class Solution:
def pivotArray(self, nums: List[int], pivot: int) -> List[int]:
left = []
right = []
middle = []
for i in range(len(nums)):
if nums[i] < pivot:
left.append(nums[i])
elif nums[i] > pivot:
right.append(nums[i])
else:
middle.append(nums[i])
nums = left + middle + right
return nums
|
partition-array-according-to-given-pivot
|
Python O(N)
|
Noisy47
| 0
| 6
|
partition array according to given pivot
| 2,161
| 0.845
|
Medium
| 30,029
|
https://leetcode.com/problems/partition-array-according-to-given-pivot/discuss/2659921/Partition-Array-According-to-Given-Pivot-oror-Python3
|
class Solution:
def pivotArray(self, nums: List[int], pivot: int) -> List[int]:
l=[]
s=[]
h=[]
for i in nums:
if i<pivot:
l.append(i)
elif i>pivot:
h.append(i)
else:
s.append(i)
return l+s+h
|
partition-array-according-to-given-pivot
|
Partition Array According to Given Pivot || Python3
|
shagun_pandey
| 0
| 7
|
partition array according to given pivot
| 2,161
| 0.845
|
Medium
| 30,030
|
https://leetcode.com/problems/partition-array-according-to-given-pivot/discuss/2642635/Python-or-Simple-solution-or-O(-n-)-Time-Complexity.
|
class Solution:
def pivotArray(self, nums: List[int], pivot: int) -> List[int]:
lessPivot = []
GreaterPivot = []
k = 0
for num in nums:
if num < pivot:
lessPivot.insert(k, num) # Element less than pivot inserted at front of list
k += 1
elif num > pivot:
GreaterPivot.append(num) # Element greater than pivot appended in other list
else:
lessPivot.append(num)
return lessPivot + GreaterPivot # Return merged list
|
partition-array-according-to-given-pivot
|
Python | Simple solution | O( n ) Time Complexity.
|
devshailesh
| 0
| 62
|
partition array according to given pivot
| 2,161
| 0.845
|
Medium
| 30,031
|
https://leetcode.com/problems/partition-array-according-to-given-pivot/discuss/2626029/One-Liner-or-Python-Super-Easy
|
class Solution:
def pivotArray(self, nums: List[int], pivot: int) -> List[int]:
return [i for i in nums if i<pivot]+[i for i in nums if i==pivot]+[i for i in nums if i>pivot]
|
partition-array-according-to-given-pivot
|
One Liner | Python Super Easy
|
RajatGanguly
| 0
| 14
|
partition array according to given pivot
| 2,161
| 0.845
|
Medium
| 30,032
|
https://leetcode.com/problems/partition-array-according-to-given-pivot/discuss/2535109/Python-or-Array-or-O(n)-Time-or-O(n)-Space
|
class Solution:
def pivotArray(self, nums: List[int], pivot: int) -> List[int]:
less, equal, greater = [], [], []
for num in nums:
if num<pivot:
less.append(num)
elif num>pivot:
greater.append(num)
else:
equal.append(num)
return less+equal+greater
|
partition-array-according-to-given-pivot
|
Python | Array | O(n) Time | O(n) Space
|
coolakash10
| 0
| 32
|
partition array according to given pivot
| 2,161
| 0.845
|
Medium
| 30,033
|
https://leetcode.com/problems/partition-array-according-to-given-pivot/discuss/2513615/Easy-Python-solution-using-Arrays
|
class Solution:
def pivotArray(self, nums: List[int], pivot: int) -> List[int]:
less=[]
Pivot=[]
greater=[]
for i in nums:
if i<pivot:
less.append(i)
elif i==pivot:
Pivot.append(i)
else:
greater.append(i)
ans=less+Pivot+greater
return ans
|
partition-array-according-to-given-pivot
|
Easy Python solution using Arrays
|
keertika27
| 0
| 26
|
partition array according to given pivot
| 2,161
| 0.845
|
Medium
| 30,034
|
https://leetcode.com/problems/partition-array-according-to-given-pivot/discuss/2411141/Simple-python-code-with-explanation
|
class Solution:
def pivotArray(self, nums: List[int], pivot: int) -> List[int]:
#create a less-->list to store the values less than pivot
less = []
#create a pivotval-->list to store the values equal to pivot
pivotval = []
#create a more-->list to store the values more than pivot
more = []
#iterate over the elements in nums-->list
for i in nums:
#if the element is less than pivot
if i < pivot:
#than add that element in less-->list
less.append(i)
#if the element is equal to pivot
elif i == pivot:
#than add that element in pivotval-->list
pivotval.append(i)
#if the element is greater than pivot
else:
#then add that element in pivot
more.append(i)
#return the list after contactinating three lists
return less + pivotval + more
|
partition-array-according-to-given-pivot
|
Simple python code with explanation
|
thomanani
| 0
| 31
|
partition array according to given pivot
| 2,161
| 0.845
|
Medium
| 30,035
|
https://leetcode.com/problems/partition-array-according-to-given-pivot/discuss/2336521/Python-easy-beginner-solution
|
class Solution:
def pivotArray(self, nums: List[int], pivot: int) -> List[int]:
smol = []
bhig = []
same = []
for i in nums:
if i < pivot:
smol.append(i)
elif i > pivot:
bhig.append(i)
elif i == pivot:
same.append(i)
ans = []
for i in smol:
ans.append(i)
for i in same:
ans.append(i)
for i in bhig:
ans.append(i)
return ans
|
partition-array-according-to-given-pivot
|
Python easy beginner solution
|
EbrahimMG
| 0
| 51
|
partition array according to given pivot
| 2,161
| 0.845
|
Medium
| 30,036
|
https://leetcode.com/problems/partition-array-according-to-given-pivot/discuss/2298004/Python-really-easy-to-understand-solution.-O(n)
|
class Solution:
def pivotArray(self, nums: List[int], pivot: int) -> List[int]:
lst=[]
for i in nums:
if i<pivot:
lst.append(i)
for i in nums:
if i==pivot:
lst.append(i)
for i in nums:
if i>pivot:
lst.append(i)
return lst
|
partition-array-according-to-given-pivot
|
Python really easy to understand solution. O(n)
|
guneet100
| 0
| 54
|
partition array according to given pivot
| 2,161
| 0.845
|
Medium
| 30,037
|
https://leetcode.com/problems/partition-array-according-to-given-pivot/discuss/1989938/Simple-Python-Solution
|
class Solution:
def pivotArray(self, nums: List[int], pivot: int) -> List[int]:
pos=0
ans=[-1]*len(nums)
for i in range(len(nums)):
if nums[i] != 'v' and nums[i]<pivot:
ans[pos]=nums[i]
pos+=1
nums[i]='v'
for i in range(len(nums)):
if nums[i] != 'v' and nums[i]==pivot:
ans[pos]=nums[i]
pos+=1
nums[i]='v'
for i in range(len(nums)):
if nums[i] != 'v' and nums[i]>pivot:
ans[pos]=nums[i]
pos+=1
nums[i]='v'
return ans
|
partition-array-according-to-given-pivot
|
Simple Python Solution
|
Siddharth_singh
| 0
| 62
|
partition array according to given pivot
| 2,161
| 0.845
|
Medium
| 30,038
|
https://leetcode.com/problems/partition-array-according-to-given-pivot/discuss/1939603/Python3-Simple-Solution
|
class Solution:
def pivotArray(self, nums: List[int], pivot: int) -> List[int]:
left = []
mid = []
right = []
for n in nums:
if n < pivot:
left.append(n)
elif n == pivot:
mid.append(n)
elif n > pivot:
right.append(n)
return left + mid + right
|
partition-array-according-to-given-pivot
|
[Python3] Simple Solution
|
terrencetang
| 0
| 83
|
partition array according to given pivot
| 2,161
| 0.845
|
Medium
| 30,039
|
https://leetcode.com/problems/partition-array-according-to-given-pivot/discuss/1918533/WEEB-DOES-PYTHONC%2B%2B
|
class Solution:
def pivotArray(self, nums: List[int], pivot: int) -> List[int]:
left, mid, right = [], [], []
for i in range(len(nums)):
if nums[i] < pivot:
left.append(nums[i])
elif nums[i] > pivot:
right.append(nums[i])
else:
mid.append(nums[i])
return left + mid + right
|
partition-array-according-to-given-pivot
|
WEEB DOES PYTHON/C++
|
Skywalker5423
| 0
| 42
|
partition array according to given pivot
| 2,161
| 0.845
|
Medium
| 30,040
|
https://leetcode.com/problems/partition-array-according-to-given-pivot/discuss/1867107/Python-solution-easy-for-beginners
|
class Solution:
def pivotArray(self, nums: List[int], pivot: int) -> List[int]:
piv = []
low = []
high = []
for i in nums:
if i == pivot:
piv.append(i)
elif i < pivot:
low.append(i)
else:
high.append(i)
return low + piv + high
|
partition-array-according-to-given-pivot
|
Python solution, easy for beginners
|
alishak1999
| 0
| 57
|
partition array according to given pivot
| 2,161
| 0.845
|
Medium
| 30,041
|
https://leetcode.com/problems/partition-array-according-to-given-pivot/discuss/1789402/Simple-solution-faster-than-99.7
|
class Solution:
def pivotArray(self, nums: List[int], pivot: int) -> List[int]:
small_list = [item for item in nums if item < pivot]
large_list = [item for item in nums if item > pivot]
pivot_count = nums.count(pivot)
return small_list + [pivot]*pivot_count + large_list
|
partition-array-according-to-given-pivot
|
Simple solution faster than 99.7%
|
blackmishra
| 0
| 91
|
partition array according to given pivot
| 2,161
| 0.845
|
Medium
| 30,042
|
https://leetcode.com/problems/partition-array-according-to-given-pivot/discuss/1787624/Simple-Python-solution-(n-log-n)
|
class Solution:
def pivotArray(self, nums: List[int], pivot: int) -> List[int]:
def key(v):
if v < pivot:
return -1
if v > pivot:
return 1
return 0
return sorted(nums, key=key)
|
partition-array-according-to-given-pivot
|
Simple Python solution, (n log n)
|
emwalker
| 0
| 83
|
partition array according to given pivot
| 2,161
| 0.845
|
Medium
| 30,043
|
https://leetcode.com/problems/partition-array-according-to-given-pivot/discuss/1787288/Python3-one-liner
|
class Solution:
def pivotArray(self, nums: List[int], pivot: int) -> List[int]:
return ([x for x in nums if x < pivot] + [x for x in nums if x == pivot] + [x for x in nums if x > pivot])
|
partition-array-according-to-given-pivot
|
Python3 one-liner
|
rrrares
| 0
| 41
|
partition array according to given pivot
| 2,161
| 0.845
|
Medium
| 30,044
|
https://leetcode.com/problems/partition-array-according-to-given-pivot/discuss/1774450/Simple-and-faster-than-99.67
|
class Solution:
def pivotArray(self, nums: List[int], pivot: int) -> List[int]:
les = []
great = []
for item in nums:
if item < pivot:
les.append(item)
elif item > pivot:
great.append(item)
return les+nums.count(pivot)*[pivot] + great
|
partition-array-according-to-given-pivot
|
Simple and faster than 99.67%
|
blackmishra
| 0
| 29
|
partition array according to given pivot
| 2,161
| 0.845
|
Medium
| 30,045
|
https://leetcode.com/problems/partition-array-according-to-given-pivot/discuss/1762927/Python3-simple-solution
|
class Solution:
def pivotArray(self, nums: List[int], pivot: int) -> List[int]:
left = []
equal = []
right = []
i = 0
while i < len(nums):
if nums[i] < pivot:
left.append(nums[i])
elif nums[i] == pivot:
equal.append(nums[i])
else:
right.append(nums[i])
i += 1
return left + equal + right
|
partition-array-according-to-given-pivot
|
Python3 simple solution
|
EklavyaJoshi
| 0
| 33
|
partition array according to given pivot
| 2,161
| 0.845
|
Medium
| 30,046
|
https://leetcode.com/problems/partition-array-according-to-given-pivot/discuss/1748055/Python3-3-parts
|
class Solution:
def pivotArray(self, nums: List[int], pivot: int) -> List[int]:
small, equal, large = [], [], []
for x in nums:
if x < pivot: small.append(x)
elif x == pivot: equal.append(x)
else: large.append(x)
return small + equal + large
|
partition-array-according-to-given-pivot
|
[Python3] 3 parts
|
ye15
| 0
| 30
|
partition array according to given pivot
| 2,161
| 0.845
|
Medium
| 30,047
|
https://leetcode.com/problems/partition-array-according-to-given-pivot/discuss/1747411/Easy-solution-JavaScript-and-Python3
|
class Solution:
def pivotArray(self, nums: List[int], pivot: int) -> List[int]:
l,e,g = [], [], []
for item in nums :
if item == pivot :
e.append(item)
if item < pivot :
l.append(item)
if item > pivot :
g.append(item)
return l + e + g
|
partition-array-according-to-given-pivot
|
Easy solution - JavaScript & Python3
|
shakilbabu
| 0
| 25
|
partition array according to given pivot
| 2,161
| 0.845
|
Medium
| 30,048
|
https://leetcode.com/problems/partition-array-according-to-given-pivot/discuss/1747136/Python3-Solution
|
class Solution:
def pivotArray(self, nums: List[int], pivot: int) -> List[int]:
less = []
equal = []
greater = []
for i in range(len(nums)):
if nums[i]<pivot:
less.append(nums[i])
elif nums[i]==pivot:
equal.append(nums[i])
else:
greater.append(nums[i])
return less + equal + greater
|
partition-array-according-to-given-pivot
|
Python3 Solution
|
light_1
| 0
| 25
|
partition array according to given pivot
| 2,161
| 0.845
|
Medium
| 30,049
|
https://leetcode.com/problems/partition-array-according-to-given-pivot/discuss/1747065/Easy-Python-Solutions%3A-3-List-and-1-list
|
class Solution:
def pivotArray(self, nums: List[int], pivot: int) -> List[int]:
# get all less than pivot elements
less_than = [i for i in nums if i < pivot]
# get all pivot elements
eq = [i for i in nums if i == pivot]
# get all greater than pivot elements
greater_than = [i for i in nums if i > pivot]
return less_than + eq + greater_than
|
partition-array-according-to-given-pivot
|
Easy Python Solutions: 3 List and 1 list
|
theanilbajar
| 0
| 26
|
partition array according to given pivot
| 2,161
| 0.845
|
Medium
| 30,050
|
https://leetcode.com/problems/partition-array-according-to-given-pivot/discuss/1747065/Easy-Python-Solutions%3A-3-List-and-1-list
|
class Solution:
def pivotArray(self, nums: List[int], pivot: int) -> List[int]:
n = len(nums)
res = [0]*(n)
less_index = 0
pivot_start_index = 0
greater_start_index = n
# update start indices to add pivot and greater than pivot elements
for i in range(n):
if nums[i] < pivot:
pivot_start_index+= 1
elif nums[i] > pivot:
greater_start_index -= 1
# add elements in less, pivot, and greater order
for i in range(n):
# add less than pivot value
if nums[i] < pivot:
res[less_index] = nums[i]
less_index +=1
# add greater than pivot value
elif nums[i] > pivot:
res[greater_start_index] = nums[i]
greater_start_index +=1
# add pivot elements
else:
res[pivot_start_index] = nums[i]
pivot_start_index +=1
return res
|
partition-array-according-to-given-pivot
|
Easy Python Solutions: 3 List and 1 list
|
theanilbajar
| 0
| 26
|
partition array according to given pivot
| 2,161
| 0.845
|
Medium
| 30,051
|
https://leetcode.com/problems/partition-array-according-to-given-pivot/discuss/1747037/Easy-Python3-Solution-Brute-Force
|
class Solution:
def pivotArray(self, nums: List[int], pivot: int) -> List[int]:
lessthan = []
for i in nums:
if i < pivot:
lessthan.append(i)
equal = []
for i in nums:
if i == pivot:
equal.append(i)
great = []
for i in nums:
if i > pivot:
great.append(i)
return lessthan + equal + great
|
partition-array-according-to-given-pivot
|
Easy Python3 Solution Brute Force
|
sdasstriver9
| 0
| 34
|
partition array according to given pivot
| 2,161
| 0.845
|
Medium
| 30,052
|
https://leetcode.com/problems/minimum-cost-to-set-cooking-time/discuss/1746996/Simple-Python-Solution
|
class Solution:
def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, targetSeconds: int) -> int:
def count_cost(minutes, seconds): # Calculates cost for certain configuration of minutes and seconds
time = f'{minutes // 10}{minutes % 10}{seconds // 10}{seconds % 10}' # mm:ss
time = time.lstrip('0') # since 0's are prepended we remove the 0's to the left to minimize cost
t = [int(i) for i in time]
current = startAt
cost = 0
for i in t:
if i != current:
current = i
cost += moveCost
cost += pushCost
return cost
ans = float('inf')
for m in range(100): # Check which [mm:ss] configuration works out
for s in range(100):
if m * 60 + s == targetSeconds:
ans = min(ans, count_cost(m, s))
return ans
|
minimum-cost-to-set-cooking-time
|
Simple Python Solution
|
anCoderr
| 10
| 513
|
minimum cost to set cooking time
| 2,162
| 0.396
|
Medium
| 30,053
|
https://leetcode.com/problems/minimum-cost-to-set-cooking-time/discuss/1758788/Cooking-time-as-string-80-speed
|
class Solution:
def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, targetSeconds: int) -> int:
start_c, min_cost = str(startAt), inf
def cost(t: str) -> int:
return (pushCost * len(t) + moveCost *
sum(a != b for a, b in zip(start_c + t, t)))
for minutes in range(max(0, targetSeconds - 99) // 60,
ceil(targetSeconds / 60) + 1):
seconds = targetSeconds - minutes * 60
if -1 < minutes < 100 and -1 < seconds < 100:
set_time = str(minutes) if minutes else ""
set_time += f"{seconds:0>2d}" if set_time else f"{seconds}"
min_cost = min(min_cost, cost(set_time))
return min_cost
|
minimum-cost-to-set-cooking-time
|
Cooking time as string, 80% speed
|
EvgenySH
| 0
| 65
|
minimum cost to set cooking time
| 2,162
| 0.396
|
Medium
| 30,054
|
https://leetcode.com/problems/minimum-cost-to-set-cooking-time/discuss/1748828/python-solution-faster-than-87.5
|
class Solution:
def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, t: int) -> int:
res = []
minu,min_sec = divmod(t,60)
if minu <= 99:
res.append([minu,min_sec])
if min_sec + 60 <= 99 and minu -1 >= 0:
res.append([minu-1,min_sec + 60])
_min = inf
res2 = []
for m,s in res:
if s < 10:
s = str(0) + str(s)
res2.append(int(str(m) + str(s)))
for r in res2:
c = 0
r = str(r)
if startAt != int(r[0]):
c+=moveCost
c+=pushCost
for i in range(1,len(r)):
if r[i] != r[i-1]:
c += moveCost
c += pushCost
_min = min(_min,c)
return _min
|
minimum-cost-to-set-cooking-time
|
python solution faster than 87.5%
|
biancai3939
| 0
| 28
|
minimum cost to set cooking time
| 2,162
| 0.396
|
Medium
| 30,055
|
https://leetcode.com/problems/minimum-cost-to-set-cooking-time/discuss/1747280/Python3-simulation
|
class Solution:
def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, targetSeconds: int) -> int:
m, s = divmod(targetSeconds, 60)
cands = []
if m < 100: cands.append((str(m) + str(s).zfill(2)).lstrip('0'))
if m and s+60 < 100: cands.append((str(m-1) + str(s+60).zfill(2)).lstrip('0'))
ans = inf
for cand in cands:
cost = 0
prev = str(startAt)
for ch in cand:
if prev != ch: cost += moveCost
prev = ch
cost += pushCost
ans = min(ans, cost)
return ans
|
minimum-cost-to-set-cooking-time
|
[Python3] simulation
|
ye15
| 0
| 29
|
minimum cost to set cooking time
| 2,162
| 0.396
|
Medium
| 30,056
|
https://leetcode.com/problems/minimum-cost-to-set-cooking-time/discuss/1747226/Python-simple-solution
|
class Solution:
def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, targetSeconds: int) -> int:
mins1,secs1 = (0,targetSeconds) if targetSeconds<100 else (targetSeconds//60,targetSeconds%60)
if mins1>99:
mins1 -= 1
secs1 += 60
l = [(mins1,secs1)]
if mins1>0 and secs1<40:
mins2 = mins1-1
secs2 = secs1+60
l.append((mins2,secs2))
if mins1<99 and secs1>=60:
mins2 = mins1+1
secs2 = secs1-60
l.append((mins2,secs2))
ret = inf
for mins,secs in l:
mins = str(mins) if mins>0 else ''
if secs>=10:
secs = str(secs)
else:
if mins:
secs = '0'+str(secs)
else:
secs = str(secs)
t = mins+secs
#print(t)
total = 0
for i in range(len(t)):
if i==0 and t[i]==str(startAt):
total += pushCost
elif i>0 and t[i]==t[i-1]:
total += pushCost
else:
total += moveCost + pushCost
ret = min(ret,total)
return ret
|
minimum-cost-to-set-cooking-time
|
Python simple solution
|
1579901970cg
| 0
| 25
|
minimum cost to set cooking time
| 2,162
| 0.396
|
Medium
| 30,057
|
https://leetcode.com/problems/minimum-cost-to-set-cooking-time/discuss/1747185/Python-O(1)-solution
|
class Solution:
def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, targetSeconds: int) -> int:
mins = targetSeconds // 60
secs = targetSeconds % 60
min_cost = math.inf
if mins <= 99:
cost1 = cost(mins, secs, startAt, moveCost, pushCost)
min_cost = min(min_cost, cost1)
if secs <= 39:
cost2 = cost(mins - 1, secs + 60, startAt, moveCost, pushCost)
min_cost = min(min_cost, cost2)
return min_cost
def cost(mins, secs, startAt, moveCost, pushCost):
cost = 0
has_prev = False
for digit in [mins // 10, mins % 10, secs // 10, secs % 10]:
if has_prev or digit != 0:
if digit != startAt:
cost += moveCost
startAt = digit
cost += pushCost
has_prev = True
return cost
|
minimum-cost-to-set-cooking-time
|
[Python] O(1) solution
|
mostafaa
| 0
| 36
|
minimum cost to set cooking time
| 2,162
| 0.396
|
Medium
| 30,058
|
https://leetcode.com/problems/minimum-difference-in-sums-after-removal-of-elements/discuss/2392142/Python-solution-or-O(nlogn)-or-explained-with-diagram-or-heap-and-dp-solution
|
class Solution:
def minimumDifference(self, nums: List[int]) -> int:
n = len(nums) // 3
# calculate max_sum using min_heap for second part
min_heap = nums[(2 * n) :]
heapq.heapify(min_heap)
max_sum = [0] * (n + 2)
max_sum[n + 1] = sum(min_heap)
for i in range((2 * n) - 1, n - 1, -1):
# push current
heapq.heappush(min_heap, nums[i])
# popout minimum from heap
val = heapq.heappop(min_heap)
# max_sum for this partition
max_sum[i - n + 1] = max_sum[i - n + 2] - val + nums[i]
# calculate min_sum using max_heap for first part
max_heap = [-x for x in nums[:n]]
heapq.heapify(max_heap)
min_sum = [0] * (n + 2)
min_sum[0] = -sum(max_heap)
for i in range(n, (2 * n)):
# push current
heapq.heappush(max_heap, -nums[i])
# popout maximum from heap
val = -heapq.heappop(max_heap)
# min_sum for this partition
min_sum[i - n + 1] = min_sum[i - n] - val + nums[i]
# find min difference bw second part (max_sum) and first part (min_sum)
ans = math.inf
for i in range(0, n + 1):
print(i, min_sum[i], max_sum[i])
ans = min((min_sum[i] - max_sum[i + 1]), ans)
return ans
|
minimum-difference-in-sums-after-removal-of-elements
|
Python solution | O(nlogn) | explained with diagram | heap and dp solution
|
wilspi
| 1
| 64
|
minimum difference in sums after removal of elements
| 2,163
| 0.467
|
Hard
| 30,059
|
https://leetcode.com/problems/minimum-difference-in-sums-after-removal-of-elements/discuss/1747399/Python-3-SortedList-solution-time-O(NlogN)-space-O(N)-equivalent-to-2-heaps
|
class Solution:
def minimumDifference(self, nums: List[int]) -> int:
# Python solution using SortedList, time complexity O(nlogn), space complexity O(n)
n = len(nums) // 3
from sortedcontainers import SortedList
leftArr = SortedList(nums[ : n])
rightArr = SortedList(nums[n : ])
# leftSum is the minimum subsequence sum of leftArr, rightSum is the maximum subsequence sum of rightArr
leftSum, rightSum = sum(leftArr), sum(rightArr[n : ])
minDiff = leftSum - rightSum
for i in range(n, n * 2):
# if nums[i] belongs to the n smallest values of leftArr, thus leftSum needs to be updated
if nums[i] < leftArr[-1]:
leftSum += nums[i] - leftArr[-1]
leftArr.add(nums[i])
leftArr.pop(-1)
# if nums[i] belongs to the n largest values of rightArr, thus rightSum needs to be updated
if rightArr.bisect_left(nums[i]) - len(rightArr) >= -n:
rightSum += rightArr[-(n + 1)] - nums[i]
rightArr.remove(nums[i])
minDiff = min(minDiff, leftSum - rightSum)
return minDiff
|
minimum-difference-in-sums-after-removal-of-elements
|
Python 3 SortedList solution, time O(NlogN) space O(N), equivalent to 2 heaps
|
xil899
| 1
| 54
|
minimum difference in sums after removal of elements
| 2,163
| 0.467
|
Hard
| 30,060
|
https://leetcode.com/problems/minimum-difference-in-sums-after-removal-of-elements/discuss/1748061/Python3-priority-queue
|
class Solution:
def minimumDifference(self, nums: List[int]) -> int:
n = len(nums)//3
pq0 = [-x for x in nums[:n]]
pq1 = nums[-n:]
heapify(pq0)
heapify(pq1)
ans = -sum(pq0) - sum(pq1)
prefix = [0]
for i in range(n, 2*n):
prefix.append(prefix[-1])
if nums[i] < -pq0[0]:
prefix[-1] += nums[i] + pq0[0]
heapreplace(pq0, -nums[i])
extra = prefix[-1]
suffix = 0
for i in reversed(range(n, 2*n)):
if nums[i] > pq1[0]:
suffix += pq1[0] - nums[i]
heapreplace(pq1, nums[i])
extra = min(extra, prefix[i-n] + suffix)
return ans + extra
|
minimum-difference-in-sums-after-removal-of-elements
|
[Python3] priority queue
|
ye15
| 0
| 30
|
minimum difference in sums after removal of elements
| 2,163
| 0.467
|
Hard
| 30,061
|
https://leetcode.com/problems/sort-even-and-odd-indices-independently/discuss/2214566/Python-oror-In-Place-Sorting
|
class Solution:
def sortEvenOdd(self, nums: List[int]) -> List[int]:
n = len(nums)
for i in range(0,n,2):
for j in range(i+2,n,2):
if nums[i] > nums[j]:
nums[i],nums[j] = nums[j], nums[i]
for i in range(1,n,2):
for j in range(i+2,n,2):
if nums[i] < nums[j]:
nums[i],nums[j] = nums[j], nums[i]
return nums
|
sort-even-and-odd-indices-independently
|
Python || In Place Sorting
|
morpheusdurden
| 1
| 186
|
sort even and odd indices independently
| 2,164
| 0.664
|
Easy
| 30,062
|
https://leetcode.com/problems/sort-even-and-odd-indices-independently/discuss/1748459/Python-or-Easy-Solution
|
class Solution:
def sortEvenOdd(self, nums: List[int]) -> List[int]:
even_list = []
odd_list = []
for i in range(len(nums)):
if((i%2)==0):
even_list.append(nums[i])
else:
odd_list.append(nums[i])
even_list = sorted(even_list)
odd_list = sorted(odd_list, reverse = True)
i=0; j=0
for m in range(len(nums)):
if m&1:
nums[m] = odd_list[j]
j+=1
else:
nums[m] = even_list[i]
i+=1
return nums
|
sort-even-and-odd-indices-independently
|
👨💻 ✔Python | Easy Solution ✔👨💻
|
DaemonStark
| 1
| 90
|
sort even and odd indices independently
| 2,164
| 0.664
|
Easy
| 30,063
|
https://leetcode.com/problems/sort-even-and-odd-indices-independently/discuss/2782963/Best-Easy-Solution
|
class Solution:
def sortEvenOdd(self, nums: List[int]) -> List[int]:
lst=[]
dst=[]
bst=[]
mst=[]
cst=[0]*len(nums)
for i in range(len(nums)):
if i%2==0:
lst.append(nums[i])
dst.append(i)
else:
bst.append(nums[i])
mst.append(i)
lst.sort()
bst.sort()
bst=bst[::-1]
for i in range(len(lst)):
cst[dst[i]]=lst[i]
for j in range(len(bst)):
cst[mst[j]]=bst[j]
return cst
|
sort-even-and-odd-indices-independently
|
Best Easy Solution
|
Kaustubhmishra
| 0
| 7
|
sort even and odd indices independently
| 2,164
| 0.664
|
Easy
| 30,064
|
https://leetcode.com/problems/sort-even-and-odd-indices-independently/discuss/2782962/Best-Easy-Solution
|
class Solution:
def sortEvenOdd(self, nums: List[int]) -> List[int]:
lst=[]
dst=[]
bst=[]
mst=[]
cst=[0]*len(nums)
for i in range(len(nums)):
if i%2==0:
lst.append(nums[i])
dst.append(i)
else:
bst.append(nums[i])
mst.append(i)
lst.sort()
bst.sort()
bst=bst[::-1]
for i in range(len(lst)):
cst[dst[i]]=lst[i]
for j in range(len(bst)):
cst[mst[j]]=bst[j]
return cst
|
sort-even-and-odd-indices-independently
|
Best Easy Solution
|
Kaustubhmishra
| 0
| 3
|
sort even and odd indices independently
| 2,164
| 0.664
|
Easy
| 30,065
|
https://leetcode.com/problems/sort-even-and-odd-indices-independently/discuss/2776742/Simple-Python-or-Two-liner
|
class Solution:
def sortEvenOdd(self, nums: List[int]) -> List[int]:
nums[0::2], nums[1::2] = sorted(nums[::2]), sorted(nums[1::2])[::-1]
return nums
|
sort-even-and-odd-indices-independently
|
Simple Python | Two liner
|
blindspot_vikram
| 0
| 4
|
sort even and odd indices independently
| 2,164
| 0.664
|
Easy
| 30,066
|
https://leetcode.com/problems/sort-even-and-odd-indices-independently/discuss/2690531/Easy-and-optimal-one-line-code
|
class Solution:
def sortEvenOdd(self, nums: List[int]) -> List[int]:
nums[::2],nums[1::2]=sorted(nums[::2]),sorted(nums[1::2])[::-1]
return nums
|
sort-even-and-odd-indices-independently
|
Easy and optimal one line code
|
Raghunath_Reddy
| 0
| 10
|
sort even and odd indices independently
| 2,164
| 0.664
|
Easy
| 30,067
|
https://leetcode.com/problems/sort-even-and-odd-indices-independently/discuss/2572870/Python3-Counting-sort%3A-O(n)-constant-space%3A-O(1)
|
class Solution:
def sortEvenOdd(self, nums: List[int]) -> List[int]:
self.evencount(nums)
self.oddcount(nums)
return nums
def evencount(self, arr: List[int]) -> List[int]:
valToFre = [0] * 101
for i in range(0, len(arr), 2):
valToFre[arr[i]] += 1
i = 0
for val, fre in enumerate(valToFre):
times = fre
while times:
arr[i] = val
times -= 1
i += 2
return arr
def oddcount(self, arr: List[int]) -> List[int]:
valToFre = [0] * 101
for i in range(1, len(arr), 2):
valToFre[arr[i]] += 1
val = 100
i = 1
while val >= 0:
fre = valToFre[val]
while fre:
arr[i] = val
fre -= 1
i += 2
val -= 1
return arr
|
sort-even-and-odd-indices-independently
|
[Python3] Counting sort: O(n), constant space: O(1)
|
DG_stamper
| 0
| 32
|
sort even and odd indices independently
| 2,164
| 0.664
|
Easy
| 30,068
|
https://leetcode.com/problems/sort-even-and-odd-indices-independently/discuss/2186234/Python-simple-solution
|
class Solution:
def sortEvenOdd(self, nums: List[int]) -> List[int]:
x = sorted([nums[x] for x in range(0, len(nums), 2)])
y = sorted([nums[y] for y in range(1, len(nums), 2)], reverse=True)
z = []
for i in range(len(nums)//2+1):
if i < len(x):
z.append(x[i])
if i < len(y):
z.append(y[i])
return z
|
sort-even-and-odd-indices-independently
|
Python simple solution
|
StikS32
| 0
| 97
|
sort even and odd indices independently
| 2,164
| 0.664
|
Easy
| 30,069
|
https://leetcode.com/problems/sort-even-and-odd-indices-independently/discuss/2036168/Python-Clear-and-Concise-3-Lines!
|
class Solution:
def sortEvenOdd(self, nums):
evenNums = deque(sorted([nums for i,nums in enumerate(nums) if i % 2 == 0]))
oddNums = deque(sorted([nums for i,nums in enumerate(nums) if i % 2 == 1], reverse=True))
return [evenNums.popleft() if i % 2 == 0 else oddNums.popleft() for i in range(len(nums))]
|
sort-even-and-odd-indices-independently
|
Python - Clear and Concise [3 Lines]!
|
domthedeveloper
| 0
| 99
|
sort even and odd indices independently
| 2,164
| 0.664
|
Easy
| 30,070
|
https://leetcode.com/problems/sort-even-and-odd-indices-independently/discuss/1980541/Python-(Simple-Approach-and-Beginner-Friendly)
|
class Solution:
def sortEvenOdd(self, nums: List[int]) -> List[int]:
if len(nums) <= 2: return nums
odd = []
even = []
for i in range(0, len(nums), 2):
even.append(nums[i])
for i in range(1, len(nums), 2):
odd.append(nums[i])
odd.sort(reverse = True)
even.sort()
a = even+odd
b = [0]*len(a)
j = 0
for i in range(0, len(b), 2):
b[i] = even[j]
j+=1
j = 0
for i in range(1, len(b), 2):
b[i] = odd[j]
j+=1
return b
|
sort-even-and-odd-indices-independently
|
Python (Simple Approach and Beginner-Friendly)
|
vishvavariya
| 0
| 73
|
sort even and odd indices independently
| 2,164
| 0.664
|
Easy
| 30,071
|
https://leetcode.com/problems/sort-even-and-odd-indices-independently/discuss/1841455/Python3-Faster-Than-99.06-Easy-Understand
|
class Solution:
def sortEvenOdd(self, nums: List[int]) -> List[int]:
odd, even = [], []
for i in range(len(nums)):
if i % 2 == 0:
even.append(nums[i])
else:
odd.append(nums[i])
y, x = sorted(odd, reverse = True), sorted(even)
z, j = [-1] * len(nums), 0
for i in x:
z[j] = i
j += 2
j = 1
for i in y:
z[j] = i
j += 2
return z
|
sort-even-and-odd-indices-independently
|
Python3, Faster Than 99.06%, Easy Understand
|
Hejita
| 0
| 70
|
sort even and odd indices independently
| 2,164
| 0.664
|
Easy
| 30,072
|
https://leetcode.com/problems/sort-even-and-odd-indices-independently/discuss/1800801/Python3-accepted-solution
|
class Solution:
def sortEvenOdd(self, nums: List[int]) -> List[int]:
even = []; odd = []; ans =[]
for i in range(len(nums)):
if(i%2==0):
even.append(nums[i])
else: odd.append(nums[i])
even.sort()
odd.sort(reverse=True)
for i in range(len(odd)):
ans.append(even[i])
ans.append(odd[i])
if(len(nums)>len(ans)):
ans.append(even[-1])
return ans
|
sort-even-and-odd-indices-independently
|
Python3 accepted solution
|
sreeleetcode19
| 0
| 61
|
sort even and odd indices independently
| 2,164
| 0.664
|
Easy
| 30,073
|
https://leetcode.com/problems/sort-even-and-odd-indices-independently/discuss/1752572/Python3-simple-solution
|
class Solution:
def sortEvenOdd(self, nums: List[int]) -> List[int]:
even = []
odd = []
if len(nums) < 3:
return nums
for i in range(len(nums)):
if i % 2 == 0:
even.append(nums[i])
else:
odd.append(nums[i])
even.sort()
odd.sort(reverse=True)
result = []
ptr1 = ptr2 = 0
for i in range(len(nums)):
if i % 2 == 0:
result.append(even[ptr1])
ptr1 += 1
else:
result.append(odd[ptr2])
ptr2 += 1
return result
|
sort-even-and-odd-indices-independently
|
Python3 simple solution
|
noobj097
| 0
| 32
|
sort even and odd indices independently
| 2,164
| 0.664
|
Easy
| 30,074
|
https://leetcode.com/problems/sort-even-and-odd-indices-independently/discuss/1751805/pythonic-by-slicing
|
class Solution:
def sortEvenOdd(self, nums):
return self.pythonic(nums)
def pythonic(self, nums):
nums[::2], nums[1::2] = sorted(nums[::2]), sorted(nums[1::2], reverse=True)
return nums
|
sort-even-and-odd-indices-independently
|
pythonic, by slicing
|
steve-jokes
| 0
| 64
|
sort even and odd indices independently
| 2,164
| 0.664
|
Easy
| 30,075
|
https://leetcode.com/problems/sort-even-and-odd-indices-independently/discuss/1751021/Python-3-counting-sort-O(n)-O(n)
|
class Solution:
def sortEvenOdd(self, nums: List[int]) -> List[int]:
odd_count = collections.defaultdict(lambda: 0)
even_count = collections.defaultdict(lambda: 0)
for i, num in enumerate(nums):
if i % 2:
odd_count[num] += 1
else:
even_count[num] += 1
cur_odd, cur_even = 100, 1
for i in range(len(nums)):
if i % 2:
while odd_count[cur_odd] == 0:
cur_odd -= 1
odd_count[cur_odd] -= 1
nums[i] = cur_odd
else:
while even_count[cur_even] == 0:
cur_even += 1
even_count[cur_even] -= 1
nums[i] = cur_even
return nums
|
sort-even-and-odd-indices-independently
|
Python 3, counting sort, O(n) / O(n)
|
dereky4
| 0
| 81
|
sort even and odd indices independently
| 2,164
| 0.664
|
Easy
| 30,076
|
https://leetcode.com/problems/sort-even-and-odd-indices-independently/discuss/1750494/Python-Easy-Readable-Solution
|
class Solution:
def sortEvenOdd(self, nums: List[int]) -> List[int]:
even_list = []
odd_list = []
for i,c in enumerate(nums):
if i%2 == 0:
even_list.append(c)
else:
odd_list.append(c)
even_list = sorted(even_list,reverse=True)
odd_list = sorted(odd_list, reverse=False)
ans = []
for i in range(len(nums)):
if i % 2 == 0:
ans.append(even_list.pop())
else:
ans.append(odd_list.pop())
return ans
|
sort-even-and-odd-indices-independently
|
[Python] Easy, Readable Solution
|
user9454R
| 0
| 50
|
sort even and odd indices independently
| 2,164
| 0.664
|
Easy
| 30,077
|
https://leetcode.com/problems/sort-even-and-odd-indices-independently/discuss/1749107/Intuitive-approach-by-using-list-index-%3A%3A2
|
class Solution:
def sortEvenOdd(self, nums: List[int]) -> List[int]:
nums[::2] = sorted(nums[::2])
nums[1::2] = sorted(nums[1::2], reverse=True)
return nums
|
sort-even-and-odd-indices-independently
|
Intuitive approach by using list index [::2]
|
puremonkey2001
| 0
| 14
|
sort even and odd indices independently
| 2,164
| 0.664
|
Easy
| 30,078
|
https://leetcode.com/problems/sort-even-and-odd-indices-independently/discuss/1748633/Python-Simple-and-Clean-Python-by-Separating-Even-and-Odd-index-in-two-arrays
|
class Solution:
def sortEvenOdd(self, nums: List[int]) -> List[int]:
arr_even=[]
arr_odd=[]
for i in range(len(nums)):
if i%2==0:
arr_even.append(nums[i])
else:
arr_odd.append(nums[i])
arr_even=sorted(arr_even)
arr_odd=sorted(arr_odd)[::-1]
ans=[]
l=min(len(arr_even),len(arr_odd))
for i in range(l):
ans.append(arr_even[i])
ans.append(arr_odd[i])
if len(arr_even)==len(arr_odd):
return ans
elif len(arr_even)<len(arr_odd):
ans.append(arr_odd[-1])
return ans
elif len(arr_even)>len(arr_odd):
ans.append(arr_even[-1])
return ans
|
sort-even-and-odd-indices-independently
|
[ Python ] ✔✔ Simple and Clean Python by Separating Even and Odd index in two arrays
|
ASHOK_KUMAR_MEGHVANSHI
| 0
| 75
|
sort even and odd indices independently
| 2,164
| 0.664
|
Easy
| 30,079
|
https://leetcode.com/problems/sort-even-and-odd-indices-independently/discuss/1748558/Self-understandable-Python-%3A
|
class Solution:
def sortEvenOdd(self, nums: List[int]) -> List[int]:
a=[]
b=[]
for i in range(len(nums)):
if i%2==0:
a.append(nums[i])
else:
b.append(nums[i])
a.sort()
b=sorted(b,reverse=True)
c=0
d=0
for i in range(len(nums)):
if i%2==0:
nums[i]=a[c]
c+=1
else:
nums[i]=b[d]
d+=1
return nums
|
sort-even-and-odd-indices-independently
|
Self understandable Python :
|
goxy_coder
| 0
| 60
|
sort even and odd indices independently
| 2,164
| 0.664
|
Easy
| 30,080
|
https://leetcode.com/problems/sort-even-and-odd-indices-independently/discuss/2298627/Python-1-Liner-with-explanation
|
class Solution:
def sortEvenOdd(self, nums: List[int]) -> List[int]:
return [num for num in chain.from_iterable(itertools.zip_longest(sorted(nums[::2]), sorted(nums[1::2], reverse=True))) if num is not None]
|
sort-even-and-odd-indices-independently
|
Python 1-Liner with explanation
|
amaargiru
| -1
| 78
|
sort even and odd indices independently
| 2,164
| 0.664
|
Easy
| 30,081
|
https://leetcode.com/problems/sort-even-and-odd-indices-independently/discuss/2298627/Python-1-Liner-with-explanation
|
class Solution:
def sortEvenOdd(self, nums: List[int]) -> List[int]:
evens = sorted(nums[::2])
odds = sorted(nums[1::2], reverse=True)
# 🠓 Made flat zip(), see stackoverflow.com/questions/61943924/python-flat-zip
total = [num for num in chain.from_iterable(itertools.zip_longest(evens, odds)) if num is not None]
# 🠑 zip_longest() inserts 'None' for two lists with different length, we must delete it
return total
|
sort-even-and-odd-indices-independently
|
Python 1-Liner with explanation
|
amaargiru
| -1
| 78
|
sort even and odd indices independently
| 2,164
| 0.664
|
Easy
| 30,082
|
https://leetcode.com/problems/sort-even-and-odd-indices-independently/discuss/1807620/1-Line-Python-Solution-oror-92-Faster-oror-Memory-less-than-80
|
class Solution:
def sortEvenOdd(self, nums: List[int]) -> List[int]:
return reduce(add, zip_longest(sorted([nums[i] for i in range(0,len(nums),2)]), sorted([nums[i] for i in range(1,len(nums),2)])[::-1]))[:len(nums)]
|
sort-even-and-odd-indices-independently
|
1-Line Python Solution || 92% Faster || Memory less than 80%
|
Taha-C
| -1
| 121
|
sort even and odd indices independently
| 2,164
| 0.664
|
Easy
| 30,083
|
https://leetcode.com/problems/smallest-value-of-the-rearranged-number/discuss/1755847/PYTHON3-or-EASY-SOLUTION-or
|
class Solution:
def smallestNumber(self, num: int) -> int:
if num == 0 : return 0
snum = sorted(str(num))
if snum[0] == '-' :
return -int("".join(snum[:0:-1]))
elif snum[0] == '0' :
x = snum.count('0')
return "".join([snum[x]]+['0'*x]+snum[x+1:])
else :
return "".join(snum)
|
smallest-value-of-the-rearranged-number
|
PYTHON3 | EASY SOLUTION |
|
rohitkhairnar
| 1
| 108
|
smallest value of the rearranged number
| 2,165
| 0.513
|
Medium
| 30,084
|
https://leetcode.com/problems/smallest-value-of-the-rearranged-number/discuss/1748482/Easy-Python
|
class Solution:
def smallestNumber(self, num: int) -> int:
if num==0:
return 0
if num>=0:
num=str(num)
num=sorted(num)
ans=''
z=num.count('0')
for i in range(z):
num.pop(0)
if len(num)>0:
ans+=num[0]
ans+='0'*z
for i in range(1,len(num)):
ans+=num[i]
else:
num=str(num)
num=sorted(num[1:])
num.sort(reverse=True)
ans='-'
for i in range(len(num)):
ans+=num[i]
return ans
|
smallest-value-of-the-rearranged-number
|
Easy Python
|
ro_hit2013
| 1
| 64
|
smallest value of the rearranged number
| 2,165
| 0.513
|
Medium
| 30,085
|
https://leetcode.com/problems/smallest-value-of-the-rearranged-number/discuss/2540010/use-SORTING-cleaver-solution-O(n-logn)-beats-98.86
|
class Solution:
def smallestNumber(self, num: int) -> int:
if num ==0 : return 0
res , zero = '',''
for i in str(num):
if i != '-':
if i=='0':
zero += i
else:
res += i
res = ''.join(sorted(res))
if num < 0:
return '-'+ res[::-1] + zero
elif res and zero:
return res[0] + zero + res[1:]
else:
return res
|
smallest-value-of-the-rearranged-number
|
use SORTING cleaver solution O(n logn) [beats 98.86%]
|
_jorjis
| 0
| 48
|
smallest value of the rearranged number
| 2,165
| 0.513
|
Medium
| 30,086
|
https://leetcode.com/problems/smallest-value-of-the-rearranged-number/discuss/2473513/Python-Without-String-and-with-NumberSort-O(N)
|
class Solution:
def smallestNumber(self, num: int) -> int:
# no digits contained, can return zero
if not num:
return 0
# make an array to count the digits
digits = [0]*10
# check whether number is negative
negative = num < 0
num = abs(num)
# find the digits and the smallest number not beeing zero
# if we have a positive number (no leading zeros)
starting_number = 9
while num:
# get the digits
num, res = divmod(num, 10)
# track digit frequency
digits[res] += 1
# save the smallest number in case we have a positive number
if not negative and res:
starting_number = min(starting_number, res)
# initialize the result (for positive numbers we already put the smallest number)
result = 0
if not negative:
result = starting_number
digits[starting_number] -= 1
if negative:
# go through the numbers in reverse and attach them
for index, amount in enumerate(reversed(digits)):
# attach the digit amount times
while amount:
result = result*10 - (9-index)
amount -= 1
else:
# go through the numbers and attach them
for index, amount in enumerate(digits):
# attach the digit amount times
while amount:
result = result*10 + index
amount -= 1
return result
|
smallest-value-of-the-rearranged-number
|
[Python] Without String and with NumberSort - O(N)
|
Lucew
| 0
| 25
|
smallest value of the rearranged number
| 2,165
| 0.513
|
Medium
| 30,087
|
https://leetcode.com/problems/smallest-value-of-the-rearranged-number/discuss/2271241/Python-Easy
|
class Solution:
def smallestNumber(self, num: int) -> int:
isnegative = True if num<0 else False
nums = str(num)
if isnegative:
nums = [(ch) for ch in nums[1:]]
nums.sort(reverse = True)
return -int(''.join(nums))
if not isnegative:
nums = [ch for ch in nums]
nums.sort()
## now find the range of 0roes
if nums[0] == '0':
i = 0
ans = '0'
while i<len(nums) and nums[i]=='0':
i+=1
if len(nums)>1:
ans = ''.join(nums[i:i+1]+nums[0:i]+nums[i+1:])
return ans
else:
return int(''.join(nums))
|
smallest-value-of-the-rearranged-number
|
Python Easy
|
Abhi_009
| 0
| 16
|
smallest value of the rearranged number
| 2,165
| 0.513
|
Medium
| 30,088
|
https://leetcode.com/problems/smallest-value-of-the-rearranged-number/discuss/2214584/Python-oror-Simple
|
class Solution:
def smallestNumber(self, num: int) -> int:
num = str(num)
n = len(num)
if n <= 1: return num
if int(num) < 0:
num = sorted(num, reverse = True)
sign = -1
else:
sign = 1
num = sorted(num)
i = 0
while i < n and num[i] == '0': i+= 1
num = [num[i]] + num[:i] + num[i+1:]
if sign == -1:
return -1*int(''.join(num[:-1]) )
return int(''.join(num))
|
smallest-value-of-the-rearranged-number
|
Python || Simple
|
morpheusdurden
| 0
| 21
|
smallest value of the rearranged number
| 2,165
| 0.513
|
Medium
| 30,089
|
https://leetcode.com/problems/smallest-value-of-the-rearranged-number/discuss/1838797/python-sorting-or-swapping-or-string
|
class Solution:
def smallestNumber(self, num: int) -> int:
sign = 1 if num >= 0 else -1
temp = None
if sign == 1:
temp = sorted(str(num))
else:
temp = sorted(str(num)[1:], reverse = True)
if temp[0] == '0':
i = 0
while i < len(temp) and temp[i] == '0':
i += 1
if i < len(temp):
temp[i], temp[0] = temp[0], temp[i]
return sign*int(''.join(temp))
|
smallest-value-of-the-rearranged-number
|
python sorting | swapping | string
|
abkc1221
| 0
| 82
|
smallest value of the rearranged number
| 2,165
| 0.513
|
Medium
| 30,090
|
https://leetcode.com/problems/smallest-value-of-the-rearranged-number/discuss/1791856/Python3-accepted-solution-(using-lstrip)
|
class Solution:
def smallestNumber(self, num: int) -> int:
if(num==0):return 0
if(num>0):
s = "".join(sorted(str(num)))
return(int(s.lstrip("0")[0] + "0"*(len(s)-(len(s.lstrip("0")))) + s.lstrip("0")[1:]))
if(num<0):
num = (-1)*num
return(int("-" + "".join(sorted(str(num),reverse=True))))
|
smallest-value-of-the-rearranged-number
|
Python3 accepted solution (using lstrip)
|
sreeleetcode19
| 0
| 27
|
smallest value of the rearranged number
| 2,165
| 0.513
|
Medium
| 30,091
|
https://leetcode.com/problems/smallest-value-of-the-rearranged-number/discuss/1761450/Easy-Brute-Force-Solution-for-Beginners-!!!-82-Faster
|
class Solution:
def smallestNumber(self, num: int) -> int:
if num==0:
return 0
if num>0:
num=str(num)
li_num=[]
for i in num:
li_num.append(int(i))
li_num.sort()
if li_num[0]==0:
for j in range(1,len(li_num)):
if li_num[j]!=0:
li_num[0],li_num[j]=li_num[j],li_num[0]
break
st=""
for x in li_num:
st+=str(x)
return st
else:
st=""
for x in li_num:
st+=str(x)
return st
if num<0:
num=str(num)
li_num=[]
for i in num[1:]:
li_num.append(int(i))
li_num.sort(reverse=True)
st=""
for b in li_num:
st+=str(b)
st=int(st)
return st*(-1)
|
smallest-value-of-the-rearranged-number
|
Easy Brute Force Solution for Beginners !!! 82% Faster
|
pratiyush_ray
| 0
| 60
|
smallest value of the rearranged number
| 2,165
| 0.513
|
Medium
| 30,092
|
https://leetcode.com/problems/smallest-value-of-the-rearranged-number/discuss/1748520/Self-Understandable-Python-%3A
|
class Solution:
def smallestNumber(self, num: int) -> int:
if num//10==0:
return num
if num<0:
sign=-1
else:
sign=1
num=abs(num)
if sign==-1:
s=sorted(str(num),reverse=True)
return sign*int("".join(s))
else:
s=sorted(str(num))
zeros=s.count('0')
if zeros!=0:
s[0],s[zeros]=s[zeros],s[0]
return sign*int("".join(s))
|
smallest-value-of-the-rearranged-number
|
Self Understandable Python :
|
goxy_coder
| 0
| 37
|
smallest value of the rearranged number
| 2,165
| 0.513
|
Medium
| 30,093
|
https://leetcode.com/problems/minimum-time-to-remove-all-cars-containing-illegal-goods/discuss/2465604/Python3-dp
|
class Solution:
def minimumTime(self, s: str) -> int:
ans = inf
prefix = 0
for i, ch in enumerate(s):
if ch == '1': prefix = min(2 + prefix, i+1)
ans = min(ans, prefix + len(s)-1-i)
return ans
|
minimum-time-to-remove-all-cars-containing-illegal-goods
|
[Python3] dp
|
ye15
| 0
| 24
|
minimum time to remove all cars containing illegal goods
| 2,167
| 0.402
|
Hard
| 30,094
|
https://leetcode.com/problems/minimum-time-to-remove-all-cars-containing-illegal-goods/discuss/1768795/Python-or-prefix_sum-or-easy-to-understand
|
class Solution:
def minimumTime(self, s: str) -> int:
costL2R = [None]*len(s)
cost = 0 if s[0]=='0' else 1
costL2R[0]=cost
for i in range(1,len(s)):
if s[i]=='1':
cost = min(cost+2,i+1)
costL2R[i]=cost
costR2L = [None]*len(s)
cost = 0 if s[-1]=='0' else 1
costR2L[-1] = cost
for i in range(len(s)-2,-1,-1):
if s[i]=='1':
cost = min(cost+2,len(s) - (i+1)+1)
costR2L[i] = cost
costR2L.append(0)
minCost = len(s)
for i in range(len(s)):
minCost = min(minCost,costL2R[i]+costR2L[i+1])
return minCost
|
minimum-time-to-remove-all-cars-containing-illegal-goods
|
Python | prefix_sum | easy to understand
|
albertnew2018
| 0
| 84
|
minimum time to remove all cars containing illegal goods
| 2,167
| 0.402
|
Hard
| 30,095
|
https://leetcode.com/problems/count-operations-to-obtain-zero/discuss/1766882/Python3-simulation
|
class Solution:
def countOperations(self, num1: int, num2: int) -> int:
ans = 0
while num1 and num2:
ans += num1//num2
num1, num2 = num2, num1%num2
return ans
|
count-operations-to-obtain-zero
|
[Python3] simulation
|
ye15
| 4
| 385
|
count operations to obtain zero
| 2,169
| 0.755
|
Easy
| 30,096
|
https://leetcode.com/problems/count-operations-to-obtain-zero/discuss/1768763/Python3-or-2-approaches-Normal-and-Optimized-in-time
|
class Solution:
def countOperations(self, num1: int, num2: int) -> int:
ct=0
while num2 and num1:
if num1>=num2:
num1=num1-num2
else:
num2=num2-num1
ct+=1
return ct
|
count-operations-to-obtain-zero
|
Python3 | 2 approaches Normal and Optimized in time
|
Anilchouhan181
| 3
| 273
|
count operations to obtain zero
| 2,169
| 0.755
|
Easy
| 30,097
|
https://leetcode.com/problems/count-operations-to-obtain-zero/discuss/1768763/Python3-or-2-approaches-Normal-and-Optimized-in-time
|
class Solution:
def countOperations(self, num1: int, num2: int) -> int:
ans = 0
if num1 < num2:
num1, num2 = num2, num1
while num2:
num1, num2 = num2, num1 - num2
if num1 < num2:
num1, num2 = num2, num1
ans += 1
return ans
|
count-operations-to-obtain-zero
|
Python3 | 2 approaches Normal and Optimized in time
|
Anilchouhan181
| 3
| 273
|
count operations to obtain zero
| 2,169
| 0.755
|
Easy
| 30,098
|
https://leetcode.com/problems/count-operations-to-obtain-zero/discuss/2731626/PythonororO(N)
|
class Solution:
def countOperations(self, num1: int, num2: int) -> int:
count=0
while num1!=0 and num2!=0:
if num1>num2:
num1=num1-num2
count+=1
else:
num2=num2-num1
count+=1
return count
|
count-operations-to-obtain-zero
|
Python||O(N)
|
Sneh713
| 1
| 99
|
count operations to obtain zero
| 2,169
| 0.755
|
Easy
| 30,099
|
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.