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/sort-array-by-parity/discuss/2151481/Python-97.35-faster-ororSimplest-solution-with-explanationoror-Beg-to-Advoror-Two-Pointer | class Solution:
def sortArrayByParity(self, nums: List[int]) -> List[int]:
left = 0 # taking first pointer
right = len(nums) - 1 # second pointer
while left < right: # as we are using two pointer technique, one pointer should be smaller then other one.
if nums[left] % 2... | sort-array-by-parity | Python 97.35% faster ||Simplest solution with explanation|| Beg to Adv|| Two Pointer | rlakshay14 | 0 | 25 | sort array by parity | 905 | 0.757 | Easy | 14,700 |
https://leetcode.com/problems/sort-array-by-parity/discuss/2102977/Python-very-easy-solution | class Solution:
def sortArrayByParity(self, nums: List[int]) -> List[int]:
numeven = []
numodd =[]
for i in range(len(nums)):
if(nums[i]%2==0):
numeven.append(nums[i])
else:
numodd.append(nums[i])
return numeven+numodd | sort-array-by-parity | Python very easy solution | yashkumarjha | 0 | 26 | sort array by parity | 905 | 0.757 | Easy | 14,701 |
https://leetcode.com/problems/sort-array-by-parity/discuss/2096765/Python3-%3A-Easy-to-understand-solution-for-beginners-(One-Liner) | class Solution:
def sortArrayByParity(self, nums: List[int]) -> List[int]:
return sorted(nums,key=lambda x: x%2) | sort-array-by-parity | Python3 : Easy to understand solution for beginners (One Liner) | kushal2201 | 0 | 12 | sort array by parity | 905 | 0.757 | Easy | 14,702 |
https://leetcode.com/problems/sort-array-by-parity/discuss/2031377/Python-solution | class Solution:
def sortArrayByParity(self, nums: List[int]) -> List[int]:
ans = []
for i in nums:
if i%2 == 0:
ans.insert(0,i)
else:
ans.append(i)
return ans | sort-array-by-parity | Python solution | StikS32 | 0 | 35 | sort array by parity | 905 | 0.757 | Easy | 14,703 |
https://leetcode.com/problems/sort-array-by-parity/discuss/2027123/Python-in-place-swap-without-changing-order-easy-understanding. | class Solution:
def sortArrayByParity(self, nums: List[int]) -> List[int]:
even = 0 # The index currently contain odd, waiting for accepting a even num
for odd in range(len(nums)):
if nums[odd] % 2 == 0: ... | sort-array-by-parity | Python in place swap without changing order, easy-understanding. | byroncharly3 | 0 | 21 | sort array by parity | 905 | 0.757 | Easy | 14,704 |
https://leetcode.com/problems/super-palindromes/discuss/1198991/Runtime%3A-Faster-than-94.87-of-Python3-Memory-Usage-less-than-100 | class Solution:
nums = []
for i in range(1, 10**5):
odd = int(str(i)+str(i)[:-1][::-1])**2
even = int(str(i)+str(i)[::-1])**2
if str(odd) == str(odd)[::-1]:
nums.append(odd)
if str(even) == str(even)[::-1]:
... | super-palindromes | Runtime: Faster than 94.87% of Python3 Memory Usage less than 100% | pranshusharma712 | 1 | 102 | super palindromes | 906 | 0.392 | Hard | 14,705 |
https://leetcode.com/problems/super-palindromes/discuss/2226814/Python3-Solution-with-explanation | class Solution:
def superpalindromesInRange(self, left: str, right: str) -> int:
min_num, max_num = int(left), int(right)
count, limit = 0, 20001
# odd pals
for num in range(limit + 1):
num_str = str(num)
if num_str[0] != 1 or num_str[0] != 4 or num_... | super-palindromes | Python3 Solution with explanation | frolovdmn | 0 | 31 | super palindromes | 906 | 0.392 | Hard | 14,706 |
https://leetcode.com/problems/super-palindromes/discuss/1325185/Explained-Clean-Modular-Generate-All-Palindromes-and-check-their-squares | class Solution:
def pal(self, x: int) -> bool: # return whether x am pal
rev = 0
normal = x
while x:
rev = rev*10 + x%10 # push last digit to 'rev'
x = x//10 # remove last digit
return rev == normal
# O(root(n) x log (n)) -> since i generate left h... | super-palindromes | Explained Clean Modular Generate All Palindromes and check their squares | yozaam | 0 | 84 | super palindromes | 906 | 0.392 | Hard | 14,707 |
https://leetcode.com/problems/super-palindromes/discuss/1198108/python-ez-understanding-solution | class Solution:
def superpalindromesInRange(self, left: str, right: str) -> int:
left,right = int(left),int(right)
limit = 100000
count = 0
for i in range(limit):
s = str(i)
# 121 -> 12121 and 121121
s1 = s + s[::-1][1:]
s2 = s + s[::-1... | super-palindromes | python ez understanding solution | yingziqing123 | 0 | 74 | super palindromes | 906 | 0.392 | Hard | 14,708 |
https://leetcode.com/problems/super-palindromes/discuss/1197664/PythonPython3-solution-with-Explanation | class Solution:
def superpalindromesInRange(self, left: str, right: str) -> int:
left,right = int(left),int(right)
#Just to travese the loop declare a variable and set the limit
limit = 100000
#to count super Palindromes
cnt = 0
#count odd number length palindromes
for i in ran... | super-palindromes | Python/Python3 solution with Explanation | prasanthksp1009 | 0 | 178 | super palindromes | 906 | 0.392 | Hard | 14,709 |
https://leetcode.com/problems/super-palindromes/discuss/1197616/python3-build-palindromes-solution-for-reference. | class Solution:
def superpalindromesInRange(self, left: str, right: str) -> int:
a = set()
ileft = int(left)
iright = int(right)
res = 0
for i in range(10):
isq = i**2
if isq >= ileft and isq <= iright and str(isq) == str(isq)[::-1]:
... | super-palindromes | [python3] build palindromes solution for reference. | vadhri_venkat | 0 | 92 | super palindromes | 906 | 0.392 | Hard | 14,710 |
https://leetcode.com/problems/super-palindromes/discuss/1168245/Python3-Easy-approach-or-Explanation-and-Comments-added | class Solution:
def superpalindromesInRange(self, left: str, right: str) -> int:
'''
1. For each number in the range [floor(square root of integer value of left), floor(square root of integer value of right)],
- need to check if the number is palindrome and (number^2) is palindrome
... | super-palindromes | Python3 Easy approach | Explanation and Comments added | bPapan | 0 | 201 | super palindromes | 906 | 0.392 | Hard | 14,711 |
https://leetcode.com/problems/sum-of-subarray-minimums/discuss/2846444/Python-3Monotonic-stack-boundry | class Solution:
def sumSubarrayMins(self, arr: List[int]) -> int:
M = 10 ** 9 + 7
# right bound for current number as minimum
q = []
n = len(arr)
right = [n-1] * n
for i in range(n):
# must put the equal sign to one of the bound (left or right)... | sum-of-subarray-minimums | [Python 3]Monotonic stack boundry | chestnut890123 | 10 | 475 | sum of subarray minimums | 907 | 0.346 | Medium | 14,712 |
https://leetcode.com/problems/sum-of-subarray-minimums/discuss/2212520/Python-solution-not-sure-whether-it-is-easy-understanding.... | class Solution:
def sumSubarrayMins(self, arr: List[int]) -> int:
res = 0
stack = [-1] # We are adopting increasing stack to solve this problem.
arr += [0] # The trick is as same as problem 84,
... | sum-of-subarray-minimums | Python solution not sure whether it is easy-understanding.... | byroncharly3 | 7 | 373 | sum of subarray minimums | 907 | 0.346 | Medium | 14,713 |
https://leetcode.com/problems/sum-of-subarray-minimums/discuss/2847805/Python-O(n)-Stack-Video-Solution-Picture | class Solution:
def sumSubarrayMins(self, A: List[int]) -> int:
A = [-math.inf] + A + [-math.inf]
n = len(A)
st = []
res = 0
for i in range(n):
while st and A[st[-1]] > A[i]: # monotonic increasing stack
mid ... | sum-of-subarray-minimums | [Python] O(n) - Stack - Video Solution - Picture | cheatcode-ninja | 3 | 64 | sum of subarray minimums | 907 | 0.346 | Medium | 14,714 |
https://leetcode.com/problems/sum-of-subarray-minimums/discuss/1732965/python-3-monotonic-stack-O(n)-O(n) | class Solution:
def sumSubarrayMins(self, nums: List[int]) -> int:
M = 10 ** 9 + 7
res = 0
stack = []
n = len(nums)
nums.append(0)
for i, num in enumerate(nums):
while stack and (i == n or num < nums[stack[-1]]):
top = stack.pop()
... | sum-of-subarray-minimums | python 3, monotonic stack, O(n) / O(n) | dereky4 | 3 | 1,000 | sum of subarray minimums | 907 | 0.346 | Medium | 14,715 |
https://leetcode.com/problems/sum-of-subarray-minimums/discuss/2847814/Python-(Faster-than-99.9)-or-Stack-with-DP-solution | class Solution:
def sumSubarrayMins(self, arr: List[int]) -> int:
mod = (10 ** 9) + 7
stack = []
dp = [0] * len(arr)
for i, n in enumerate(arr):
while stack and arr[stack[-1]] >= n:
stack.pop()
if stack:
dp[i] = dp[stack[-1]] +... | sum-of-subarray-minimums | Python (Faster than 99.9%) | Stack with DP solution | KevinJM17 | 2 | 40 | sum of subarray minimums | 907 | 0.346 | Medium | 14,716 |
https://leetcode.com/problems/sum-of-subarray-minimums/discuss/2847050/Easy-using-Stack-O(n)-Solution | class Solution:
def sumSubarrayMins(self, nums):
MOD = 10**9+7
stack = []
res = 0
prevsum = 0
for index, value in enumerate(nums):
count = 1
while stack and stack[-1][0]>=value:
v, c = stack.pop()
count+=c
... | sum-of-subarray-minimums | Easy using Stack O(n) Solution | namanxk | 2 | 200 | sum of subarray minimums | 907 | 0.346 | Medium | 14,717 |
https://leetcode.com/problems/sum-of-subarray-minimums/discuss/2106623/Python-O(n)-m*n-trick-explained | class Solution:
def sumSubarrayMins(self, A: List[int]) -> int:
n = len(A)
next_smaller = [n] * n
prev_smaller = [0] * n
ns_s = []
ps_s = []
for i, a in enumerate(A):
while ns_s and A[ns_s[-1]] > a:
j = ns_s.pop()
n... | sum-of-subarray-minimums | Python O(n), m*n trick explained | rajabi | 2 | 261 | sum of subarray minimums | 907 | 0.346 | Medium | 14,718 |
https://leetcode.com/problems/sum-of-subarray-minimums/discuss/2848658/Python-solution | class Solution:
def sumSubarrayMins(self, arr: List[int]) -> int:
stack = []
res = 0
arr = [float('-inf')] + arr + [float('-inf')]
for i, num in enumerate(arr):
while stack and arr[stack[-1]] > num:
cur = stack.pop()
res += arr[cur] * (i - ... | sum-of-subarray-minimums | Python solution | user2854aZ | 1 | 7 | sum of subarray minimums | 907 | 0.346 | Medium | 14,719 |
https://leetcode.com/problems/sum-of-subarray-minimums/discuss/2847891/Monotic-Stack-Based-Solutionoror-TC%3AO(n)-oror-SC%3A-O(n) | class Solution:
def sumSubarrayMins(self, arr: List[int]) -> int:
size = len(arr)
mod = (10 ** 9) + 7
#NSE_L, NSE_R stores the idx of nearest left and nearest right elements
def nextSmaller_Left(arr):
ans = [-1] * size
stack = [0]
for idx ... | sum-of-subarray-minimums | Monotic Stack Based Solution|| TC:O(n) || SC: O(n) | s_m_d_29 | 1 | 24 | sum of subarray minimums | 907 | 0.346 | Medium | 14,720 |
https://leetcode.com/problems/sum-of-subarray-minimums/discuss/2847025/7-LINES-oror-EASY-PYTHON-SOLUTIONoror-BEGINER-FRIENDLYoror-USING-STACK | class Solution:
def sumSubarrayMins(self, arr: List[int]) -> int:
mod=10**9+7
stack=[]
dp=[0]*len(arr)
for i in range(len(arr)):
while stack and arr[stack[-1]]>=arr[i]:
stack.pop()
if stack:
presmall=stack[-1]
dp... | sum-of-subarray-minimums | 7 LINES || EASY PYTHON SOLUTION|| BEGINER FRIENDLY|| USING STACK | thezealott | 1 | 81 | sum of subarray minimums | 907 | 0.346 | Medium | 14,721 |
https://leetcode.com/problems/sum-of-subarray-minimums/discuss/2846538/python3-Solution | class Solution:
def sumSubarrayMins(self, arr: List[int]) -> int:
n=len(arr)
left=[1]*n
dec_q=[(arr[0],1)]
for i in range(1,n):
while dec_q and arr[i]<=dec_q[-1][0]:
left[i]+=dec_q.pop()[1]
dec_q.append((arr[i],left[i]))
... | sum-of-subarray-minimums | python3 Solution | Motaharozzaman1996 | 1 | 90 | sum of subarray minimums | 907 | 0.346 | Medium | 14,722 |
https://leetcode.com/problems/sum-of-subarray-minimums/discuss/949064/Python3-stack-O(N) | class Solution:
def sumSubarrayMins(self, arr: List[int]) -> int:
ans, stack = [], []
for i, x in enumerate(arr):
while stack and arr[stack[-1]] >= x: stack.pop() # mono-stack (increasing)
if stack:
ii = stack[-1]
ans.append(ans[ii] + x*(i-ii... | sum-of-subarray-minimums | [Python3] stack O(N) | ye15 | 1 | 574 | sum of subarray minimums | 907 | 0.346 | Medium | 14,723 |
https://leetcode.com/problems/sum-of-subarray-minimums/discuss/949064/Python3-stack-O(N) | class Solution:
def sumSubarrayMins(self, arr: List[int]) -> int:
ans = 0
stack = []
for i in range(len(arr)+1):
while stack and (i == len(arr) or arr[stack[-1]] > arr[i]):
mid = stack.pop()
ii = stack[-1] if stack else -1
ans +... | sum-of-subarray-minimums | [Python3] stack O(N) | ye15 | 1 | 574 | sum of subarray minimums | 907 | 0.346 | Medium | 14,724 |
https://leetcode.com/problems/sum-of-subarray-minimums/discuss/2848867/Why-my-brute-force-O(n2)-solution-failed-in-big-test-case | class Solution:
def sumSubarrayMins(self, arr: List[int]) -> int:
res = 0
cur_min = 0
for l in range(len(arr)):
cur_min = arr[l]
for r in range(l, len(arr)):
cur_min = min(cur_min, arr[r])
res += cur_min
return res | sum-of-subarray-minimums | Why my brute force O(n^2) solution failed in big test case? | TestCeline | 0 | 4 | sum of subarray minimums | 907 | 0.346 | Medium | 14,725 |
https://leetcode.com/problems/sum-of-subarray-minimums/discuss/2848492/Python-O(N)-using-monotonic-stack | class Solution:
def sumSubarrayMins(self, arr: List[int]) -> int:
stack, rez = deque(), 0
for a in arr:
cnt_subarrays, cnt_ends = 0, 1
while stack and stack[-1][0] > a:
val, cnt_starts = stack.pop()
cnt_subarrays = cnt_starts*cnt_ends
... | sum-of-subarray-minimums | [Python] O(N) using monotonic stack | nonchalant-enthusiast | 0 | 9 | sum of subarray minimums | 907 | 0.346 | Medium | 14,726 |
https://leetcode.com/problems/sum-of-subarray-minimums/discuss/2848330/Monotonic-Stack-O(n)-next-smaller-number | class Solution:
def sumSubarrayMins(self, arr: List[int]) -> int:
l = len(arr)
stack = []
total = 0
for i in range(l+1):
while stack and (i==l or arr[stack[-1]] >= arr[i] ):
mid = stack.pop()
right_bound = i
left_bound = st... | sum-of-subarray-minimums | Monotonic Stack - O(n) - next smaller number | DavidCastillo | 0 | 16 | sum of subarray minimums | 907 | 0.346 | Medium | 14,727 |
https://leetcode.com/problems/sum-of-subarray-minimums/discuss/2848294/Python-easy-to-read-and-understand-or-stack | class Solution:
def nsr(self, nums):
n = len(nums)
stack, index, res = [], [], []
i = len(nums)-1
while i >= 0:
if len(stack) == 0:
res.append(n-i)
elif len(stack) > 0 and stack[-1] < nums[i]:
res.append(index[-1]-i)
... | sum-of-subarray-minimums | Python easy to read and understand | stack | sanial2001 | 0 | 12 | sum of subarray minimums | 907 | 0.346 | Medium | 14,728 |
https://leetcode.com/problems/sum-of-subarray-minimums/discuss/2848080/Python3-solution | class Solution:
def sumSubarrayMins(self, arr: List[int]) -> int:
s, res = [-1], 0
arr += [0] # a trick to get rid of boundary checks
for i, n in enumerate(arr):
while arr[s[-1]] > n:
j, k = s.pop(), s[-1]
res ... | sum-of-subarray-minimums | Python3 solution | avs-abhishek123 | 0 | 17 | sum of subarray minimums | 907 | 0.346 | Medium | 14,729 |
https://leetcode.com/problems/sum-of-subarray-minimums/discuss/2847967/Best-approach-ever-with-less-time-complexity | class Solution:
def sumSubarrayMins(self, arr: List[int]) -> int:
kMod = 1_000_000_007
n = len(arr)
ans = 0
# prev[i] := index k s.t. arr[k] is the prev min in arr[:i]
prev = [-1] * n
# next[i] := index k s.t. arr[k] is the next min in arr[i + 1:]
next = [n] * n
stack = []
for i, ... | sum-of-subarray-minimums | Best approach ever with less time complexity | VivekSingh05 | 0 | 15 | sum of subarray minimums | 907 | 0.346 | Medium | 14,730 |
https://leetcode.com/problems/sum-of-subarray-minimums/discuss/2847704/Python3-easy-DP-Stack | class Solution:
def sumSubarrayMins(self, arr: List[int]) -> int:
ans = 0
cache = 0
stack = []
count = []
for val in arr:
num = 1
while stack and stack[-1] >= val:
cache -= stack.pop() * count[-1]
num += count.pop()
... | sum-of-subarray-minimums | Python3 easy DP Stack | Nesop | 0 | 12 | sum of subarray minimums | 907 | 0.346 | Medium | 14,731 |
https://leetcode.com/problems/sum-of-subarray-minimums/discuss/2847672/Simple-approach | class Solution:
def sumSubarrayMins(self, arr: List[int]) -> int:
s1,s2,left,right=[],[],[0]*len(arr),[0]*len(arr)
for i in range(len(arr)):
curr = arr[i]
count=1
while s1 and s1[-1][0]>curr:
count+=s1[-1][1]
s1.pop()
le... | sum-of-subarray-minimums | Simple approach | parasgarg31 | 0 | 10 | sum of subarray minimums | 907 | 0.346 | Medium | 14,732 |
https://leetcode.com/problems/sum-of-subarray-minimums/discuss/2847643/Monotonic-Stack-Solution | class Solution:
def sumSubarrayMins(self, arr: List[int]) -> int:
mod = 10**9 + 7
stack = []
total = 0
for i in range(len(arr)+1):
while stack and (len(arr)==i or arr[stack[-1]] >= arr[i]):
mid = stack.pop()
end = stack[-1] if stack else -1... | sum-of-subarray-minimums | Monotonic Stack Solution | Karthikjb | 0 | 7 | sum of subarray minimums | 907 | 0.346 | Medium | 14,733 |
https://leetcode.com/problems/sum-of-subarray-minimums/discuss/2847520/Easy-to-understand-optimal-python-O(n)-solution | class Solution:
def sumSubarrayMins(self, arr: List[int]) -> int:
res = 0
min_stack = [] # (min, count, total_sum)
for i in range(len(arr)):
num = arr[i]
count = 1
while min_stack and num <= min_stack[-1][0]:
count += min_stack.pop... | sum-of-subarray-minimums | Easy to understand optimal python O(n) solution | Priceincoding | 0 | 10 | sum of subarray minimums | 907 | 0.346 | Medium | 14,734 |
https://leetcode.com/problems/sum-of-subarray-minimums/discuss/2847154/Python-official-solution-(Fast) | class Solution:
def sumSubarrayMins(self, arr: List[int]) -> int:
MOD = 10 ** 9 + 7
# monotonic increasing stack
stack = []
# make a dp array of the same size as the input array
dp = [0] * len(arr)
# populate monotonically increasing stack
for i in range(le... | sum-of-subarray-minimums | Python official solution (Fast) | subidit | 0 | 19 | sum of subarray minimums | 907 | 0.346 | Medium | 14,735 |
https://leetcode.com/problems/sum-of-subarray-minimums/discuss/2846950/Python3O(n)-Two-mono-stack-to-get-left-and-right | class Solution:
def sumSubarrayMins(self, arr: List[int]) -> int:
n = len(arr)
left, right = list(range(n)), list(range(n))
stk = []
for i in range(n):
while stk and arr[stk[-1]] >= arr[i]: right[stk.pop()] = i - 1
stk.append(i)
while stk: right[stk.p... | sum-of-subarray-minimums | [Python3]O(n) Two mono stack to get left and right | cava | 0 | 10 | sum of subarray minimums | 907 | 0.346 | Medium | 14,736 |
https://leetcode.com/problems/sum-of-subarray-minimums/discuss/2846918/DP-or-Top-down-and-Bottom-Up-or-Short-solution | class Solution:
def sumSubarrayMins(self, arr: List[int]) -> int:
n = len(arr)
MOD = 10**9 + 7
next_smaller = [n] * n
# next_smaller[i] = index of next element which is smaller than arr[i]
# if no such element exist, next_smaller[i] = n
stack = []
for i in r... | sum-of-subarray-minimums | DP | Top-down & Bottom-Up | Short solution | xyp7x | 0 | 44 | sum of subarray minimums | 907 | 0.346 | Medium | 14,737 |
https://leetcode.com/problems/sum-of-subarray-minimums/discuss/2846918/DP-or-Top-down-and-Bottom-Up-or-Short-solution | class Solution:
def sumSubarrayMins(self, arr: List[int]) -> int:
n = len(arr)
MOD = 10**9 + 7
next_smaller = [n] * n
# next_smaller[i] = index of next element which is smaller than arr[i]
# if no such element exist, next_smaller[i] = n
stack = []
for i in r... | sum-of-subarray-minimums | DP | Top-down & Bottom-Up | Short solution | xyp7x | 0 | 44 | sum of subarray minimums | 907 | 0.346 | Medium | 14,738 |
https://leetcode.com/problems/sum-of-subarray-minimums/discuss/2846682/Python-solution-without-using-stack-or-easy-solution-and-another-one-with-stack | class Solution:
def sumSubarrayMins(self, arr: List[int]) -> int:
res=[[]]
s=0
for i in range(len(arr)+1):
for j in range(i):
res.append(arr[j:i])
for i in range(1,len(res)):
s+=min(res[i])
return s
################ Solution using st... | sum-of-subarray-minimums | Python solution without using stack | easy solution and another one with stack | ashishneo | 0 | 16 | sum of subarray minimums | 907 | 0.346 | Medium | 14,739 |
https://leetcode.com/problems/sum-of-subarray-minimums/discuss/2652363/Python-solution-with-stack | class Solution:
def sumSubarrayMins(self, arr: List[int]) -> int:
# get the index of the first previous value that is less than the current one.
# if there is no value less than the current one in the previous array
# set the index as -1 which means all previous values are larger than
... | sum-of-subarray-minimums | Python solution with stack | michaelniki | 0 | 35 | sum of subarray minimums | 907 | 0.346 | Medium | 14,740 |
https://leetcode.com/problems/smallest-range-i/discuss/535164/Python-O(n)-by-min-and-Max.-85%2B-w-Visualization | class Solution:
def smallestRangeI(self, A: List[int], K: int) -> int:
M, m = max(A), min(A)
diff, extension = M - m, 2*K
if diff <= extension:
return 0
else:
return diff - extension | smallest-range-i | Python O(n) by min & Max. 85%+ [w/ Visualization ] | brianchiang_tw | 17 | 872 | smallest range i | 908 | 0.678 | Easy | 14,741 |
https://leetcode.com/problems/smallest-range-i/discuss/1383929/pytthon3-%3A-simple-approach | class Solution:
def smallestRangeI(self, nums: List[int], k: int) -> int:
if len(nums) <=1:
return 0
diff=max(nums)-min(nums)
## diff after
new_diff=diff-2*k
if new_diff < 0:
return 0
else:
return new_diff | smallest-range-i | pytthon3 : simple approach | p_a | 2 | 119 | smallest range i | 908 | 0.678 | Easy | 14,742 |
https://leetcode.com/problems/smallest-range-i/discuss/362139/Solution-in-Python-3-(beats-~100)-(one-line) | class Solution:
def smallestRangeI(self, A: List[int], K: int) -> int:
return max(0, max(A) - min(A) - 2*K)
- Junaid Mansuri
(LeetCode ID)@hotmail.com | smallest-range-i | Solution in Python 3 (beats ~100%) (one line) | junaidmansuri | 2 | 342 | smallest range i | 908 | 0.678 | Easy | 14,743 |
https://leetcode.com/problems/smallest-range-i/discuss/2848427/Simple-Python-Solution-with-Min-and-Max | class Solution:
def smallestRangeI(self, nums: List[int], k: int) -> int:
if len(set(nums)) == 1:
return 0
mini,maxi = min(nums),max(nums)
if abs(mini - maxi) <=2*k:
return 0
else:
return (maxi - k ) - (mini + k) | smallest-range-i | Simple Python Solution with Min and Max | vijay_2022 | 0 | 1 | smallest range i | 908 | 0.678 | Easy | 14,744 |
https://leetcode.com/problems/smallest-range-i/discuss/2848017/Easy-To-Understand-Python-Solution-Beats-79.12! | class Solution:
def smallestRangeI(self, nums: List[int], k: int) -> int:
a = max(nums)
b = min(nums)
#Since the minimum output we can get is 0 due to ranges being unable to have negative length,
#the ideal situation is: max(nums)-x == min(nums)-y, x and y in range [-k,k]
... | smallest-range-i | Easy To Understand Python Solution Beats 79.12%! | dmeirLmas | 0 | 2 | smallest range i | 908 | 0.678 | Easy | 14,745 |
https://leetcode.com/problems/smallest-range-i/discuss/2787650/Very-Easy-to-Understand-or-Aditya-Bahl-or-C%2B%2B-Java-Python-Python-C-JavaScript-Kotlin | class Solution(object):
def smallestRangeI(self, nums, k):
return max(0, max(nums) - min(nums) - 2 * k) | smallest-range-i | Very Easy to Understand 💯💯 | Aditya Bahl | C++, Java, Python, Python, C#, JavaScript, Kotlin | adityabahl | 0 | 12 | smallest range i | 908 | 0.678 | Easy | 14,746 |
https://leetcode.com/problems/smallest-range-i/discuss/2787650/Very-Easy-to-Understand-or-Aditya-Bahl-or-C%2B%2B-Java-Python-Python-C-JavaScript-Kotlin | class Solution:
def smallestRangeI(self, nums: List[int], k: int) -> int:
return max(0, max(nums) - min(nums) - 2 * k) | smallest-range-i | Very Easy to Understand 💯💯 | Aditya Bahl | C++, Java, Python, Python, C#, JavaScript, Kotlin | adityabahl | 0 | 12 | smallest range i | 908 | 0.678 | Easy | 14,747 |
https://leetcode.com/problems/smallest-range-i/discuss/2545356/python-easy-solution | class Solution:
def smallestRangeI(self, nums: List[int], k: int) -> int:
mi = min(nums)+k
mx = max(nums)-k
if mi >= mx:
return 0
return mx-mi | smallest-range-i | python easy solution | anshsharma17 | 0 | 48 | smallest range i | 908 | 0.678 | Easy | 14,748 |
https://leetcode.com/problems/smallest-range-i/discuss/2476935/Easy-94-Faster-Python-Solution. | class Solution:
def smallestRangeI(self, nums: List[int], k: int) -> int:
nums.sort()
min_val = nums[0]
max_val = nums[-1]
if min_val + k >= max_val - k:
return 0
else:
return (max_val - min_val - 2*k) | smallest-range-i | Easy 94% Faster Python Solution. | Siyuan_Wu | 0 | 45 | smallest range i | 908 | 0.678 | Easy | 14,749 |
https://leetcode.com/problems/smallest-range-i/discuss/1827891/1-Line-Python-Solution-oror-55-Faster-oror-Memory-less-than-95 | class Solution:
def smallestRangeI(self, nums: List[int], k: int) -> int:
return 0 if max(nums)-min(nums)<=2*k else max(nums)-min(nums)-2*k | smallest-range-i | 1-Line Python Solution || 55% Faster || Memory less than 95% | Taha-C | 0 | 75 | smallest range i | 908 | 0.678 | Easy | 14,750 |
https://leetcode.com/problems/smallest-range-i/discuss/1725288/Python-3-one-line-O(n) | class Solution:
def smallestRangeI(self, nums: List[int], k: int) -> int:
return max(0, max(nums) - min(nums) - 2*k) | smallest-range-i | Python 3, one line, O(n) | dereky4 | 0 | 138 | smallest range i | 908 | 0.678 | Easy | 14,751 |
https://leetcode.com/problems/smallest-range-i/discuss/1565678/Python3-Solution-or-1-line-answer | class Solution:
def smallestRangeI(self, nums: List[int], k: int) -> int:
return max(max(nums) - min(nums) - 2*k , 0) | smallest-range-i | Python3 Solution | 1 line answer | satyam2001 | 0 | 56 | smallest range i | 908 | 0.678 | Easy | 14,752 |
https://leetcode.com/problems/smallest-range-i/discuss/1368386/Python3-dollarolution | class Solution:
def smallestRangeI(self, nums: List[int], k: int) -> int:
i, j = min(nums) + k, max(nums) - k
if i > j:
return 0
else:
return (j - i) | smallest-range-i | Python3 $olution | AakRay | 0 | 115 | smallest range i | 908 | 0.678 | Easy | 14,753 |
https://leetcode.com/problems/smallest-range-i/discuss/1237960/Python3-or-One-Liner-or-Min-Max | class Solution:
def smallestRangeI(self, nums: List[int], k: int) -> int:
return max(max(nums)-min(nums)-2*k, 0) | smallest-range-i | Python3 | One Liner | Min-Max | Sanjaychandak95 | 0 | 61 | smallest range i | 908 | 0.678 | Easy | 14,754 |
https://leetcode.com/problems/smallest-range-i/discuss/1063098/Python3-simple-solution | class Solution:
def smallestRangeI(self, A: List[int], K: int) -> int:
return max(0,max(A)-min(A)-2*K) | smallest-range-i | Python3 simple solution | EklavyaJoshi | 0 | 55 | smallest range i | 908 | 0.678 | Easy | 14,755 |
https://leetcode.com/problems/snakes-and-ladders/discuss/2491448/Python-3-oror-BFS-Solution-Using-board-mapping | class Solution:
def snakesAndLadders(self, board: List[List[int]]) -> int:
# creating a borad map to loop-up the square value
board_map = {}
i = 1
b_rev = board[::-1]
for d, r in enumerate(b_rev):
# reverse for even rows - here d is taken as direction
... | snakes-and-ladders | Python 3 || BFS Solution - Using board mapping | kevintoms | 1 | 161 | snakes and ladders | 909 | 0.409 | Medium | 14,756 |
https://leetcode.com/problems/snakes-and-ladders/discuss/1524927/Python-easy-understand-solutoin-with-explaination | class Solution:
def snakesAndLadders(self, board: List[List[int]]) -> int:
N = len(board)
seen = set()
queue = collections.deque()
queue.append((1,0))
flatten = self.getFlattenBoard(board)
# bfs
while queue:
label, step = queue.popleft()
... | snakes-and-ladders | Python easy understand solutoin with explaination | qaz6209031 | 1 | 300 | snakes and ladders | 909 | 0.409 | Medium | 14,757 |
https://leetcode.com/problems/snakes-and-ladders/discuss/2104750/python-3-oror-simple-bfs | class Solution:
def snakesAndLadders(self, board: List[List[int]]) -> int:
n = len(board)
size = n * n
def numToCell(num):
row = n - 1 - (num - 1) // n
if (n - row) % 2 == 0:
col = n - 1 - (num - 1) % n
else:
col = ... | snakes-and-ladders | python 3 || simple bfs | dereky4 | 0 | 110 | snakes and ladders | 909 | 0.409 | Medium | 14,758 |
https://leetcode.com/problems/snakes-and-ladders/discuss/955232/Python3-BFS-O(N) | class Solution:
def snakesAndLadders(self, board: List[List[int]]) -> int:
n = len(board)
ans = 0
queue = [1]
seen = {1}
while queue:
newq = []
for x in queue:
if x == n*n: return ans
for xx in range(x+1, x+7):
... | snakes-and-ladders | [Python3] BFS O(N) | ye15 | 0 | 94 | snakes and ladders | 909 | 0.409 | Medium | 14,759 |
https://leetcode.com/problems/smallest-range-ii/discuss/980784/Python-3-Solution-Explained-(video-%2B-code) | class Solution:
def smallestRangeII(self, A: List[int], K: int) -> int:
A.sort()
res = A[-1] - A[0]
for indx in range(0, len(A) - 1):
# assuming that A[indx] is the max val
min_val = min(A[0] + K, A[indx + 1] - K)
max_val = max(A[indx] + K, A[-1] ... | smallest-range-ii | [Python 3] Solution Explained (video + code) | spec_he123 | 3 | 361 | smallest range ii | 910 | 0.346 | Medium | 14,760 |
https://leetcode.com/problems/smallest-range-ii/discuss/955300/Python3-greedy-O(NlogN) | class Solution:
def smallestRangeII(self, A: List[int], K: int) -> int:
A.sort()
ans = A[-1] - A[0]
for i in range(1, len(A)):
mn = min(A[0] + K, A[i] - K) # move up A[:i]
mx = max(A[i-1]+K, A[-1] - K) # move down A[i:]
ans = min(ans, mx - mn)
ret... | smallest-range-ii | [Python3] greedy O(NlogN) | ye15 | 3 | 91 | smallest range ii | 910 | 0.346 | Medium | 14,761 |
https://leetcode.com/problems/smallest-range-ii/discuss/2156417/Simple-Python-Solution-oror-Faster-than-100 | class Solution:
def smallestRangeII(self, nums: List[int], k: int) -> int:
# Remove duplicates and sort
arr = sorted(list(set(nums)))
res = arr[-1] - arr[0]
for i in range(len(arr) - 1):
res = min(res, max(arr[i] + k, arr[-1] - k) - min(arr[0] + k, arr[i... | smallest-range-ii | Simple { Python } Solution || Faster than 100% | vofinaev | 0 | 86 | smallest range ii | 910 | 0.346 | Medium | 14,762 |
https://leetcode.com/problems/sort-an-array/discuss/461394/Python-3-(Eight-Sorting-Algorithms)-(With-Explanation) | class Solution:
def sortArray(self, N: List[int]) -> List[int]:
L = len(N)
return [N.pop(min(range(L-i), key = lambda x: N[x])) for i in range(L)] | sort-an-array | Python 3 (Eight Sorting Algorithms) (With Explanation) | junaidmansuri | 125 | 9,200 | sort an array | 912 | 0.594 | Medium | 14,763 |
https://leetcode.com/problems/sort-an-array/discuss/461394/Python-3-(Eight-Sorting-Algorithms)-(With-Explanation) | class Solution:
def sortArray(self, N: List[int]) -> List[int]:
L, B = len(N), 1
while B:
B = 0
for i in range(L-1):
if N[i] > N[i+1]: N[i], N[i+1], B = N[i+1], N[i], 1
return N | sort-an-array | Python 3 (Eight Sorting Algorithms) (With Explanation) | junaidmansuri | 125 | 9,200 | sort an array | 912 | 0.594 | Medium | 14,764 |
https://leetcode.com/problems/sort-an-array/discuss/461394/Python-3-(Eight-Sorting-Algorithms)-(With-Explanation) | class Solution:
def sortArray(self, N: List[int]) -> List[int]:
L = len(N)
for i in range(1,L):
for j in range(0,i):
if N[i] < N[j]:
N.insert(j, N.pop(i))
break
return N | sort-an-array | Python 3 (Eight Sorting Algorithms) (With Explanation) | junaidmansuri | 125 | 9,200 | sort an array | 912 | 0.594 | Medium | 14,765 |
https://leetcode.com/problems/sort-an-array/discuss/461394/Python-3-(Eight-Sorting-Algorithms)-(With-Explanation) | class Solution:
def sortArray(self, N: List[int]) -> List[int]:
L = len(N)
for i in range(1,L): bisect.insort_left(N, N.pop(i), 0, i)
return N | sort-an-array | Python 3 (Eight Sorting Algorithms) (With Explanation) | junaidmansuri | 125 | 9,200 | sort an array | 912 | 0.594 | Medium | 14,766 |
https://leetcode.com/problems/sort-an-array/discuss/461394/Python-3-(Eight-Sorting-Algorithms)-(With-Explanation) | class Solution:
def sortArray(self, N: List[int]) -> List[int]:
C, m, M, S = collections.Counter(N), min(N), max(N), []
for n in range(m,M+1): S.extend([n]*C[n])
return S | sort-an-array | Python 3 (Eight Sorting Algorithms) (With Explanation) | junaidmansuri | 125 | 9,200 | sort an array | 912 | 0.594 | Medium | 14,767 |
https://leetcode.com/problems/sort-an-array/discuss/461394/Python-3-(Eight-Sorting-Algorithms)-(With-Explanation) | class Solution:
def sortArray(self, N: List[int]) -> List[int]:
def quicksort(A, I, J):
if J - I <= 1: return
p = partition(A, I, J)
quicksort(A, I, p), quicksort(A, p + 1, J)
def partition(A, I, J):
A[J-1], A[(I + J - 1)//2], i = A[(I + J - 1... | sort-an-array | Python 3 (Eight Sorting Algorithms) (With Explanation) | junaidmansuri | 125 | 9,200 | sort an array | 912 | 0.594 | Medium | 14,768 |
https://leetcode.com/problems/sort-an-array/discuss/461394/Python-3-(Eight-Sorting-Algorithms)-(With-Explanation) | class Solution:
def sortArray(self, N: List[int]) -> List[int]:
def mergesort(A):
LA = len(A)
if LA == 1: return A
LH, RH = mergesort(A[:LA//2]), mergesort(A[LA//2:])
return merge(LH,RH)
def merge(LH, RH):
LLH, LRH = len(LH), len(RH)
... | sort-an-array | Python 3 (Eight Sorting Algorithms) (With Explanation) | junaidmansuri | 125 | 9,200 | sort an array | 912 | 0.594 | Medium | 14,769 |
https://leetcode.com/problems/sort-an-array/discuss/461394/Python-3-(Eight-Sorting-Algorithms)-(With-Explanation) | class Solution:
def sortArray(self, N: List[int]) -> List[int]:
def insertion_sort(A):
for i in range(1,len(A)):
for j in range(0,i):
if A[i] < A[j]:
A.insert(j, A.pop(i))
break
return A
... | sort-an-array | Python 3 (Eight Sorting Algorithms) (With Explanation) | junaidmansuri | 125 | 9,200 | sort an array | 912 | 0.594 | Medium | 14,770 |
https://leetcode.com/problems/sort-an-array/discuss/905913/mergesort-in-python3 | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
#mergesort
if len(nums) <= 1:
return nums
middle = len(nums) // 2
left = self.sortArray(nums[:middle])
right = self.sortArray(nums[middle:])
merged = []
while left and right:
... | sort-an-array | mergesort in python3 | dqdwsdlws | 10 | 602 | sort an array | 912 | 0.594 | Medium | 14,771 |
https://leetcode.com/problems/sort-an-array/discuss/1677667/Python-merge-sort | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
if len(nums)>1:
mid=len(nums)//2
l=nums[:mid]
r=nums[mid:]
self.sortArray(l)
self.sortArray(r)
i=j=k=0
while i<len(l) and j<len(r):
if l[i]<r... | sort-an-array | Python merge sort | amannarayansingh10 | 3 | 543 | sort an array | 912 | 0.594 | Medium | 14,772 |
https://leetcode.com/problems/sort-an-array/discuss/905905/python3-shell-sort | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
#shell
sub = len(nums) // 2
while sub > 0:
for start in range(sub):
self.shell(nums, start, sub)
sub = sub // 2
return nums
def shell(self, alist, start, gap):
... | sort-an-array | python3 shell sort | dqdwsdlws | 3 | 114 | sort an array | 912 | 0.594 | Medium | 14,773 |
https://leetcode.com/problems/sort-an-array/discuss/2162769/Python3-5-Common-Sorting-Algorithms-(selection-bubble-insertion-merge-quick) | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
# self.selectionSort(nums)
# self.bubbleSort(nums)
# self.insertionSort(nums)
# self.mergeSort(nums)
self.quickSort(nums)
return nums
'''
Selection Sort (TLE)
TC: O(n^2) ; SC:... | sort-an-array | [Python3] 5 Common Sorting Algorithms (selection, bubble, insertion, merge, quick) | __PiYush__ | 2 | 311 | sort an array | 912 | 0.594 | Medium | 14,774 |
https://leetcode.com/problems/sort-an-array/discuss/1815762/Solution-using-mergeSort | class Solution(object):
def merge(self, a, b):
i,j = 0, 0
ans = []
while i < len(a) and j < len(b):
if a[i] < b[j]:
ans.append(a[i])
i+=1
else :
ans.append(b[j])
j+=1
if i < ... | sort-an-array | Solution using - mergeSort | shakilbabu | 2 | 293 | sort an array | 912 | 0.594 | Medium | 14,775 |
https://leetcode.com/problems/sort-an-array/discuss/2799651/Python-oror-Easy-oror-Merge-Sort-oror-O(nlogn) | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
def MergeSort(arr):
if len(arr)>1:
mid=len(arr)//2
L=arr[:mid]
R=arr[mid:]
MergeSort(L)
MergeSort(R)
i=j=k=0
while i<... | sort-an-array | Python || Easy || Merge Sort || O(nlogn) | DareDevil_007 | 1 | 222 | sort an array | 912 | 0.594 | Medium | 14,776 |
https://leetcode.com/problems/sort-an-array/discuss/955399/Python3-O(NlogN)-quick-sort-and-O(N)-bucket-sort | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
shuffle(nums) # statistical guarantee of O(NlogN)
def part(lo, hi):
"""Return a random partition of nums[lo:hi]."""
i, j = lo+1, hi-1
while i <= j:
if nums[i] < nums[lo]:... | sort-an-array | [Python3] O(NlogN) quick sort & O(N) bucket sort | ye15 | 1 | 211 | sort an array | 912 | 0.594 | Medium | 14,777 |
https://leetcode.com/problems/sort-an-array/discuss/955399/Python3-O(NlogN)-quick-sort-and-O(N)-bucket-sort | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
def fn(nums, aux, lo, hi):
"""Sort nums via merge sort."""
if lo+1 >= hi: return
mid = lo + hi >> 1
fn(aux, nums, lo, mid)
fn(aux, nums, mid, hi)
i, j = lo, m... | sort-an-array | [Python3] O(NlogN) quick sort & O(N) bucket sort | ye15 | 1 | 211 | sort an array | 912 | 0.594 | Medium | 14,778 |
https://leetcode.com/problems/sort-an-array/discuss/955399/Python3-O(NlogN)-quick-sort-and-O(N)-bucket-sort | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
bucket = [0]*100001
for x in nums: bucket[x + 50000] += 1
ans = []
for i, x in enumerate(bucket, -50000):
ans.extend([i]*x)
return ans | sort-an-array | [Python3] O(NlogN) quick sort & O(N) bucket sort | ye15 | 1 | 211 | sort an array | 912 | 0.594 | Medium | 14,779 |
https://leetcode.com/problems/sort-an-array/discuss/2845645/Python-merge-sort-solution | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
return self.mergeSort(nums,0,len(nums)-1)
def mergeLists(self, arr1,arr2):
mergeArr=[]
i,j=0,0
n1,n2=len(arr1),len(arr2)
while i<n1 and j<n2:
if arr1[i] <= arr2[j]:
mergeArr.a... | sort-an-array | Python merge sort solution | welin | 0 | 2 | sort an array | 912 | 0.594 | Medium | 14,780 |
https://leetcode.com/problems/sort-an-array/discuss/2841294/Python3-or-Counting-Sort | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
a = min(nums)
b = max(nums)
r = b-a+1
c = [0 for _ in range(r)]
for i in range(len(nums)):
c[nums[i]-a] += 1
print(c)
k = 0
for i in range(r):
for j in range... | sort-an-array | Python3 | Counting Sort | chakalivinith | 0 | 2 | sort an array | 912 | 0.594 | Medium | 14,781 |
https://leetcode.com/problems/sort-an-array/discuss/2833845/O(nlogn) | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
half_nums: List[int] = nums[0 : len(nums) // 2]
second_nums: List[int] = nums[len(nums) // 2 : len(nums)]
second_nums.sort()
half_nums.sort()
sort_nums = self.merge_arrays(half_nums, second_nums)
retur... | sort-an-array | O(nlogn) | danyalex | 0 | 6 | sort an array | 912 | 0.594 | Medium | 14,782 |
https://leetcode.com/problems/sort-an-array/discuss/2818515/Merge-Sort-or-Python-or-O(nlonn) | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
def merge_sort(arr, start, end):
if start == end:
return
mid = (start+end)//2
merge_sort(arr, start, mid)
merge_sort(arr, mid+1, end)
i = start
j = m... | sort-an-array | Merge Sort | Python | O(nlonn) | ajay_gc | 0 | 5 | sort an array | 912 | 0.594 | Medium | 14,783 |
https://leetcode.com/problems/sort-an-array/discuss/2812604/Python-or-Heap-Sort-or-O(nlogn) | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
def left(i):
return 2*i
def right(i):
return 2*i+1
def min_heapify(A, i):
l = left(i)
r = right(i)
smallest = i
if l <= len(A):
if A[l-... | sort-an-array | Python | Heap-Sort | O(nlogn) | IanChen0718 | 0 | 5 | sort an array | 912 | 0.594 | Medium | 14,784 |
https://leetcode.com/problems/sort-an-array/discuss/2802907/912.-Sort-an-Array-oror-Python3-oror-Merge_Sort | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
return merge(nums)
def merge(nums):
if len(nums)>1:
mid=len(nums)//2
l=nums[:mid]
r=nums[mid:]
#print(l,r)
merge(l)
merge(r)
i=0
j=0
k=0
while i<len(l) and j... | sort-an-array | 912. Sort an Array || Python3 || Merge_Sort | shagun_pandey | 0 | 3 | sort an array | 912 | 0.594 | Medium | 14,785 |
https://leetcode.com/problems/sort-an-array/discuss/2742288/Python-Merge-Sort | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
def mergeSort(nums,s,e):
if e-s+1 <= 1:
return nums
#middle index
middle = (s+e)//2
#left half
mergeSort(nums,s,middle)
#right half
... | sort-an-array | Python Merge Sort | piyush_54 | 0 | 16 | sort an array | 912 | 0.594 | Medium | 14,786 |
https://leetcode.com/problems/sort-an-array/discuss/2724921/Merge-Sort | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
s = 0
e = len(nums)-1
return self.mergeSortArray(nums, s, e)
def mergeSortArray(self, A, s, e):
if s == e:
return A
mid = (s+e)//2
self.mergeSortArray(A, s, mid)
self.mergeSortArray(A, mid+1, e)... | sort-an-array | Merge Sort | ihimanshu25 | 0 | 8 | sort an array | 912 | 0.594 | Medium | 14,787 |
https://leetcode.com/problems/sort-an-array/discuss/2703928/merge-sort | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
# use merge sort to divide into sub arrays
# a sub array with a single element is sorted (base case)
# otherwise keep splitting down the middle of the arr
# until the base case is reached
# do this for subarra... | sort-an-array | merge sort | andrewnerdimo | 0 | 13 | sort an array | 912 | 0.594 | Medium | 14,788 |
https://leetcode.com/problems/sort-an-array/discuss/2521292/Python-runtime-10.48-memory-31.84 | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
def mergeSort(nums, low, high):
if low < high:
mid = (low+high)//2
left = mergeSort(nums, low, mid)
right = mergeSort(nums, mid+1, high)
... | sort-an-array | Python, runtime 10.48%, memory 31.84% | tsai00150 | 0 | 155 | sort an array | 912 | 0.594 | Medium | 14,789 |
https://leetcode.com/problems/sort-an-array/discuss/2399927/Sort-an-Array | class Solution:
def merge (self, arr,L ,R):
i = j =k = 0
while i <len(L) and j<len(R):
if L[i]>R[j]:
arr[k]= R[j]
j+=1
else:
arr[k]= L[i]
i+=1
k+=1
while i < len(L):
arr[k]= L[i... | sort-an-array | Sort an Array | dhananjayaduttmishra | 0 | 54 | sort an array | 912 | 0.594 | Medium | 14,790 |
https://leetcode.com/problems/sort-an-array/discuss/2366909/minheap-Python-short-and-simple | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
heapify(nums)
ans=[]
while nums:
ans.append(heappop(nums))
return ans | sort-an-array | minheap Python -short and simple | sunakshi132 | 0 | 57 | sort an array | 912 | 0.594 | Medium | 14,791 |
https://leetcode.com/problems/sort-an-array/discuss/2310013/Python-Very-simple-solution-oror-Easy-to-understand-oror-Documented | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
# inbuilt function, it will not make you understand how the merge sort is working
# return sorted(nums)
temp = [0] * len(nums)
# merge the two different sorted arrays
# nums[low:mid] and nums[mid+1:high]
... | sort-an-array | [Python] Very simple solution || Easy to understand || Documented | Buntynara | 0 | 63 | sort an array | 912 | 0.594 | Medium | 14,792 |
https://leetcode.com/problems/sort-an-array/discuss/2024335/Python-3-Solution-O(n)-Counting-Sort | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
max_elm = max(nums)
min_elm = min(nums)
count_nums = [0 for _ in range(min_elm, max_elm + 1)]
for i in range(len(nums)):
count_nums[nums[i] - min_elm] += 1
for i in range(1, len(count_nums)):
... | sort-an-array | Python 3 Solution, O(n) Counting Sort | AprDev2011 | 0 | 79 | sort an array | 912 | 0.594 | Medium | 14,793 |
https://leetcode.com/problems/sort-an-array/discuss/1920078/Python-oror-Clean-Merge-Sort | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
def MergeSort(nums):
n = len(nums)
if n <= 1: return nums
mid = n//2
first_half = MergeSort(nums[:mid])
second_half = MergeSort(nums[mid:])
merge = [0]*n
if not first_half: return second_half
if not second_half: return fir... | sort-an-array | Python || Clean Merge Sort | morpheusdurden | 0 | 139 | sort an array | 912 | 0.594 | Medium | 14,794 |
https://leetcode.com/problems/sort-an-array/discuss/1845629/Merge-sort-with-explanation-or-Python3-or-Time-%3A-O(nlogn) | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
'''
Implementing this Question through Merge Sort
'''
#Edge Case - Already sorted!
if len(nums) <= 1 : return nums
#Find the mid point
mid = len(nums) // 2
... | sort-an-array | Merge sort with explanation | Python3 | Time : O(nlogn) | athrvb | 0 | 74 | sort an array | 912 | 0.594 | Medium | 14,795 |
https://leetcode.com/problems/sort-an-array/discuss/1811900/Python-3-Solution | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
if len(nums) > 1:
mid = len(nums) // 2
left_list = nums[:mid]
right_list = nums[mid:]
self.sortArray(left_list)
self.sortArray(right_list)
i = 0
j = 0
... | sort-an-array | Python 3 Solution | AprDev2011 | 0 | 91 | sort an array | 912 | 0.594 | Medium | 14,796 |
https://leetcode.com/problems/sort-an-array/discuss/1742082/912.-Sort-an-Array-Python3 | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
return sorted(nums) | sort-an-array | 912. Sort an Array Python3 | debo75 | 0 | 111 | sort an array | 912 | 0.594 | Medium | 14,797 |
https://leetcode.com/problems/sort-an-array/discuss/1559534/Python3-Counting-sort-solution | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
_max = max(nums)
_min = min(nums)
positive_arr = [0] * (_max + 1)
negative_arr = [0] * (abs(_min) + 1)
for num in nums:
if num >= 0:
positive_arr[num] += 1
... | sort-an-array | [Python3] Counting sort solution | maosipov11 | 0 | 86 | sort an array | 912 | 0.594 | Medium | 14,798 |
https://leetcode.com/problems/sort-an-array/discuss/1101405/O(N)-time-O(N)-space-Beats-90 | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
vals = [0]*100000
res = []
for num in nums:
vals[num+50000] += 1
for pos, val in enumerate(vals):
if val > 0:
res.extend([pos-50000]*val)... | sort-an-array | O(N) time O(N) space Beats 90% | IKM98 | 0 | 292 | sort an array | 912 | 0.594 | Medium | 14,799 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.