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/replace-all-digits-with-characters/discuss/1661711/Python3-or-O(n)-time-complexity-or-Easy-to-Understand | class Solution:
def replaceDigits(self, s: str) -> str:
if not len(s):
return s
alpha_list = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
output_str = ""
for i in range(1,len(s),2): #iter... | replace-all-digits-with-characters | Python3 | O(n) time complexity | Easy to Understand | Meow1303 | 0 | 78 | replace all digits with characters | 1,844 | 0.798 | Easy | 26,300 |
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/1554196/EASY-AND-SIMPLE-SOLN-(faster-than-99) | class Solution:
def replaceDigits(self, s: str) -> str:
string,i,j = "abcdefghijklmnopqrstuvwxyz",0,1
L = list(s)
while j < len(s):
idx = string.index(s[i])
L[j] = string[idx+int(s[j])]
i += 2
j += 2
return "".join([i for... | replace-all-digits-with-characters | EASY AND SIMPLE SOLN (faster than 99%) | anandanshul001 | 0 | 85 | replace all digits with characters | 1,844 | 0.798 | Easy | 26,301 |
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/1487311/1-line-solution-in-Python | class Solution:
def replaceDigits(self, s: str) -> str:
return "".join(chr(ord(s[i - 1]) + int(s[i])) if i & 1 else s[i] for i in range(len(s))) | replace-all-digits-with-characters | 1-line solution in Python | mousun224 | 0 | 94 | replace all digits with characters | 1,844 | 0.798 | Easy | 26,302 |
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/1369024/Python3-ASCII-Solution | class Solution:
def replaceDigits(self, s: str) -> str:
retstring = ""
for i in range(0, len(s)):
if not s[i].isdigit():
retstring += s[i]
else:
prev_ascii = ord(s[i - 1])
new_ascii = prev_ascii + int(s[i])
to_l... | replace-all-digits-with-characters | Python3 ASCII Solution | RobertObrochta | 0 | 120 | replace all digits with characters | 1,844 | 0.798 | Easy | 26,303 |
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/1277278/Simple-Python-Solution-or-O(N)-time-or-O(N)-space | class Solution:
def replaceDigits(self, s: str) -> str:
def shift(d, n):
return chr(ord(d) + int(n))
answer = ""
for i in range(len(s)):
if i % 2 == 0: answer += s[i]
else: answer += shift(s[i - 1], s[i])
return answer | replace-all-digits-with-characters | Simple Python Solution | O(N) time | O(N) space | vanigupta20024 | 0 | 159 | replace all digits with characters | 1,844 | 0.798 | Easy | 26,304 |
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/1241798/python-or-easy-solution | class Solution:
def replaceDigits(self, s: str) -> str:
u='abcdefghijklmnopqrstuvwxyz'
s=list(s)
for j in range(0,len(s),2):
if j+1<len(s):
s[j+1] = u[(u.index(s[j])+int(s[j+1]))%26]
return ''.join(s) | replace-all-digits-with-characters | python | easy solution | chikushen99 | 0 | 100 | replace all digits with characters | 1,844 | 0.798 | Easy | 26,305 |
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/1236696/PYTHON-CONVERT-TO-ASCII-VALUE-AND-AGAIN-TO-CHARACTER | class Solution:
def replaceDigits(self, s: str) -> str:
res=""
for i in range(len(s)):
try:
j = int(s[i])
res+=chr(ord(s[i-1]) + j)
except:
print(s[i])
res+=s[i]
... | replace-all-digits-with-characters | PYTHON -- CONVERT TO ASCII VALUE AND AGAIN TO CHARACTER | shubhamdec10 | 0 | 60 | replace all digits with characters | 1,844 | 0.798 | Easy | 26,306 |
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/1191307/Python3-linear-sweep | class Solution:
def replaceDigits(self, s: str) -> str:
s = list(s)
for i in range(1, len(s), 2):
s[i] = chr(ord(s[i-1]) + int(s[i]))
return "".join(s) | replace-all-digits-with-characters | [Python3] linear sweep | ye15 | 0 | 76 | replace all digits with characters | 1,844 | 0.798 | Easy | 26,307 |
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/1187880/Python3-simple-solution-beat-100-users | class Solution:
def replaceDigits(self, s: str) -> str:
x = ''
for i in range(len(s)):
if i % 2 == 0:
x += s[i]
else:
x += chr(ord(s[i-1]) + int(s[i]))
return x | replace-all-digits-with-characters | Python3 simple solution beat 100% users | EklavyaJoshi | 0 | 46 | replace all digits with characters | 1,844 | 0.798 | Easy | 26,308 |
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/1186767/Python3-100-simple-understandable-solution | class Solution:
def replaceDigits(self, s: str) -> str:
o=''
for i in range(0,len(s),2):
if i!= len(s)-1:
o+=s[i]+chr((ord(s[i])+int(s[i+1])))
else:
o+=s[i]
return o | replace-all-digits-with-characters | Python3 100% simple understandable solution | coderash1998 | 0 | 43 | replace all digits with characters | 1,844 | 0.798 | Easy | 26,309 |
https://leetcode.com/problems/maximum-element-after-decreasing-and-rearranging/discuss/1503918/Python-O(N)-Time-and-Space.-Easy-to-understand | class Solution:
def maximumElementAfterDecrementingAndRearranging(self, arr: List[int]) -> int:
counter = collections.Counter(arr)
available = sum(n > len(arr) for n in arr)
i = ans = len(arr)
while i > 0:
# This number is not in arr
if not counter[i]:
... | maximum-element-after-decreasing-and-rearranging | [Python] O(N) Time and Space. Easy to understand | JummyEgg | 1 | 205 | maximum element after decreasing and rearranging | 1,846 | 0.591 | Medium | 26,310 |
https://leetcode.com/problems/maximum-element-after-decreasing-and-rearranging/discuss/1185914/Pyhotn3-Sorting-Solution | class Solution:
def maximumElementAfterDecrementingAndRearranging(self, arr: List[int]) -> int:
arr.sort()
arr[0] = 1
for i in range(1,len(arr)):
arr[i] = min(arr[i-1]+1,arr[i])
return max(arr) | maximum-element-after-decreasing-and-rearranging | Pyhotn3 Sorting Solution | swap2001 | 1 | 36 | maximum element after decreasing and rearranging | 1,846 | 0.591 | Medium | 26,311 |
https://leetcode.com/problems/maximum-element-after-decreasing-and-rearranging/discuss/2749012/python-using-sorting | class Solution:
def maximumElementAfterDecrementingAndRearranging(self, arr: List[int]) -> int:
arr.sort()
c = 1
for i in range(0 , len(arr)):
if c <= arr[i]:
c += 1
return c - 1 | maximum-element-after-decreasing-and-rearranging | python using sorting | akashp2001 | 0 | 5 | maximum element after decreasing and rearranging | 1,846 | 0.591 | Medium | 26,312 |
https://leetcode.com/problems/maximum-element-after-decreasing-and-rearranging/discuss/2321142/Only-Sort-or-Python-3 | class Solution:
def maximumElementAfterDecrementingAndRearranging(self, arr: List[int]) -> int:
arr.sort()
if arr[0]!= 1:
arr[0] = 1
maxi = arr[0]
print(arr)
for i in range(1,len(arr)):
if abs(arr[i]-arr[i-1])>1:
arr[i] = arr[i... | maximum-element-after-decreasing-and-rearranging | Only Sort | Python 3 | user7457RV | 0 | 31 | maximum element after decreasing and rearranging | 1,846 | 0.591 | Medium | 26,313 |
https://leetcode.com/problems/maximum-element-after-decreasing-and-rearranging/discuss/1949481/Sorting-and-one-pass-98-speed | class Solution:
def maximumElementAfterDecrementingAndRearranging(self, arr: List[int]) -> int:
arr.sort()
ans = 1
for n in arr[1:]:
if n > ans:
ans += 1
return ans | maximum-element-after-decreasing-and-rearranging | Sorting and one pass, 98% speed | EvgenySH | 0 | 48 | maximum element after decreasing and rearranging | 1,846 | 0.591 | Medium | 26,314 |
https://leetcode.com/problems/maximum-element-after-decreasing-and-rearranging/discuss/1702367/Python-simple-sorting-solution | class Solution:
def maximumElementAfterDecrementingAndRearranging(self, arr: List[int]) -> int:
arr.sort()
n = len(arr)
if n == 1:
return 1
arr[0] = 1
for i in range(n-1):
arr[i+1] = min(arr[i+1], arr[i]+1)
return arr[n-1]-(arr[0]-... | maximum-element-after-decreasing-and-rearranging | Python simple sorting solution | byuns9334 | 0 | 60 | maximum element after decreasing and rearranging | 1,846 | 0.591 | Medium | 26,315 |
https://leetcode.com/problems/maximum-element-after-decreasing-and-rearranging/discuss/1346617/Python-simple-and-pythonic | class Solution:
def maximumElementAfterDecrementingAndRearranging(self, arr: List[int]) -> int:
arr.sort()
arr[0] = 1
for index, value in enumerate(arr[1:], 1):
if 1 < (value - arr[index-1]):
arr[index] = arr[index-1] + 1
return arr[-1] | maximum-element-after-decreasing-and-rearranging | [Python] simple and pythonic | cruim | 0 | 49 | maximum element after decreasing and rearranging | 1,846 | 0.591 | Medium | 26,316 |
https://leetcode.com/problems/maximum-element-after-decreasing-and-rearranging/discuss/1191310/Python3-greedy | class Solution:
def maximumElementAfterDecrementingAndRearranging(self, arr: List[int]) -> int:
arr.sort()
ans = 0
for x in arr:
ans = min(ans+1, x)
return ans | maximum-element-after-decreasing-and-rearranging | [Python3] greedy | ye15 | 0 | 38 | maximum element after decreasing and rearranging | 1,846 | 0.591 | Medium | 26,317 |
https://leetcode.com/problems/maximum-element-after-decreasing-and-rearranging/discuss/1186935/Straightforward-Python3-solution-with-explanation | class Solution:
def maximumElementAfterDecrementingAndRearranging(self, arr: List[int]) -> int:
#####################################################
# Two conditions required
# 1. the first element is 1
# 2. arr[i-1] <= arr[i] <= arr[i-1]+1
###############################... | maximum-element-after-decreasing-and-rearranging | Straightforward Python3 solution with explanation | tkuo-tkuo | 0 | 25 | maximum element after decreasing and rearranging | 1,846 | 0.591 | Medium | 26,318 |
https://leetcode.com/problems/maximum-element-after-decreasing-and-rearranging/discuss/1186051/PythonPython3-Solution-with-explanation | class Solution:
def maximumElementAfterDecrementingAndRearranging(self, arr: List[int]) -> int:
arr.sort() # sort the elements
if arr[0] == 1: # if arr[0] is 1 then simply give pass as it satisfies the 1st condition
pass
else: # else make arr[0] to 1 to satisfy condition 1
... | maximum-element-after-decreasing-and-rearranging | Python/Python3 Solution with explanation | prasanthksp1009 | 0 | 49 | maximum element after decreasing and rearranging | 1,846 | 0.591 | Medium | 26,319 |
https://leetcode.com/problems/closest-room/discuss/1186155/Python-3-Aggregate-sorted-list-detailed-explanation-(2080-ms) | class Solution:
def closestRoom(self, rooms: List[List[int]], queries: List[List[int]]) -> List[int]:
ans = [0] * len(queries)
# sort queries to handle largest size queries first
q = deque(sorted([(size, room, i) for i, (room, size) in enumerate(queries)], key=lambda a: (-a[0], a[1]... | closest-room | [Python 3] Aggregate sorted list, detailed explanation (2080 ms) | chestnut890123 | 1 | 111 | closest room | 1,847 | 0.353 | Hard | 26,320 |
https://leetcode.com/problems/minimum-distance-to-the-target-element/discuss/1186862/Python3-linear-sweep | class Solution:
def getMinDistance(self, nums: List[int], target: int, start: int) -> int:
ans = inf
for i, x in enumerate(nums):
if x == target:
ans = min(ans, abs(i - start))
return ans | minimum-distance-to-the-target-element | [Python3] linear sweep | ye15 | 9 | 302 | minimum distance to the target element | 1,848 | 0.585 | Easy | 26,321 |
https://leetcode.com/problems/minimum-distance-to-the-target-element/discuss/1204659/Python3-simple-solution-beats-90-users | class Solution:
def getMinDistance(self, nums: List[int], target: int, start: int) -> int:
if nums[start] == target:return 0
i, j = start-1, start+1
while j < len(nums) or i > -1:
if i > -1:
if nums[i] == target:
return start-i
... | minimum-distance-to-the-target-element | Python3 simple solution beats 90% users | EklavyaJoshi | 2 | 135 | minimum distance to the target element | 1,848 | 0.585 | Easy | 26,322 |
https://leetcode.com/problems/minimum-distance-to-the-target-element/discuss/1584649/Python-sol-faster-than-99 | class Solution:
def getMinDistance(self, nums: List[int], target: int, start: int) -> int:
if nums[start] == target:
return 0
i = start
j = start
left = 0
right = 0
while i < len(nums) or j > 0 :
if nums[i] == target :
... | minimum-distance-to-the-target-element | Python sol faster than 99% | elayan | 1 | 109 | minimum distance to the target element | 1,848 | 0.585 | Easy | 26,323 |
https://leetcode.com/problems/minimum-distance-to-the-target-element/discuss/1187500/Python-3-simple-solution | class Solution:
def getMinDistance(self, nums: List[int], target: int, start: int) -> int:
n = len(nums)
f = start
b = start
idx = None
while f < n or b >= 0:
if f < n:
if nums[f] == target:
idx = f
break
... | minimum-distance-to-the-target-element | Python 3 simple solution | pankaj17n | 1 | 59 | minimum distance to the target element | 1,848 | 0.585 | Easy | 26,324 |
https://leetcode.com/problems/minimum-distance-to-the-target-element/discuss/2781774/python-solution-Beats-97.33-And-memory-Beats-53.45 | class Solution:
def getMinDistance(self, nums: List[int], target: int, start: int) -> int:
x = len(nums)+1;
for i in range(0 , len(nums)):
if(nums[i] == target):
x = min(x ,abs(start-i));
return x; | minimum-distance-to-the-target-element | python solution Beats 97.33% And memory Beats 53.45% | seifsoliman | 0 | 3 | minimum distance to the target element | 1,848 | 0.585 | Easy | 26,325 |
https://leetcode.com/problems/minimum-distance-to-the-target-element/discuss/2699068/Python-or-Two-loops-with-early-exit | class Solution:
def getMinDistance(self, nums: List[int], target: int, start: int) -> int:
n = len(nums)
ans = float('inf')
i = start
while i < n and nums[i] != target:
i += 1
if i < n:
ans = i - start
i = start - 1
while i >= 0 and n... | minimum-distance-to-the-target-element | Python | Two loops with early exit | on_danse_encore_on_rit_encore | 0 | 5 | minimum distance to the target element | 1,848 | 0.585 | Easy | 26,326 |
https://leetcode.com/problems/minimum-distance-to-the-target-element/discuss/2660184/Optimized-Python-Approach-Without-Min-and-Abs-or-O(n)-time-O(1)-space | class Solution:
def getMinDistance(self, nums: List[int], target: int, start: int) -> int:
i, j = start, start + 1
while i >= 0 and j < len(nums):
if nums[i] == target:
return start - i
if nums[j] == target:
return j - start
i -= 1
... | minimum-distance-to-the-target-element | Optimized Python Approach Without Min and Abs | O(n) time, O(1) space | kcstar | 0 | 5 | minimum distance to the target element | 1,848 | 0.585 | Easy | 26,327 |
https://leetcode.com/problems/minimum-distance-to-the-target-element/discuss/2633820/Simple-one-liner-or-Python | class Solution:
def getMinDistance(self, nums: List[int], target: int, start: int) -> int:
return min(abs(key - start) for key, val in enumerate(nums) if val == target) | minimum-distance-to-the-target-element | Simple one liner | Python | Abhi_-_- | 0 | 5 | minimum distance to the target element | 1,848 | 0.585 | Easy | 26,328 |
https://leetcode.com/problems/minimum-distance-to-the-target-element/discuss/2054301/Python-Solution-or-O(n)-or-One-Liner-or-Min-of-List | class Solution:
def getMinDistance(self, nums: List[int], target: int, start: int) -> int:
return min([abs(idx - start) for idx in range(len(nums)) if nums[idx] == target]) | minimum-distance-to-the-target-element | Python Solution | O(n) | One-Liner | Min of List | Gautam_ProMax | 0 | 71 | minimum distance to the target element | 1,848 | 0.585 | Easy | 26,329 |
https://leetcode.com/problems/minimum-distance-to-the-target-element/discuss/2022150/Python-Middle-Out-or-Two-Pointers-or-Start-at-the-Start-or-Clean-and-Simple | class Solution:
def getMinDistance(self, nums, target, start):
n = len(nums)
l = r = start
while l >= 0 or r < n:
if l >= 0:
if nums[l]==target: return start-l
l -= 1
if r < n:
if nums[r]==target: return r-start
... | minimum-distance-to-the-target-element | Python - Middle-Out | Two-Pointers | Start at the Start | Clean and Simple | domthedeveloper | 0 | 50 | minimum distance to the target element | 1,848 | 0.585 | Easy | 26,330 |
https://leetcode.com/problems/minimum-distance-to-the-target-element/discuss/1963869/Brute-force-list-comp | class Solution:
def getMinDistance(self, nums: List[int], target: int, start: int) -> int:
# find the smallest absolute difference between all indicies that contain the target & start
indicies = [i for i, n in enumerate(nums) if n == target]
dist = [abs(start - i) for i in indicies]
ret... | minimum-distance-to-the-target-element | Brute force list comp | andrewnerdimo | 0 | 28 | minimum distance to the target element | 1,848 | 0.585 | Easy | 26,331 |
https://leetcode.com/problems/minimum-distance-to-the-target-element/discuss/1829262/4-Lines-Python-Solution-oror-50-Faster-oror-Memory-less-than-75 | class Solution:
def getMinDistance(self, nums: List[int], target: int, start: int) -> int:
ans=10**4
for i in range(len(nums)):
if nums[i]==target and abs(i-start)<ans: ans=abs(i-start)
return ans | minimum-distance-to-the-target-element | 4-Lines Python Solution || 50% Faster || Memory less than 75% | Taha-C | 0 | 43 | minimum distance to the target element | 1,848 | 0.585 | Easy | 26,332 |
https://leetcode.com/problems/minimum-distance-to-the-target-element/discuss/1765258/Simple-Python3-one-liner-(beats-~77) | class Solution:
def getMinDistance(self, nums: List[int], target: int, start: int) -> int:
return min(abs(i - start) for i in range(len(nums)) if nums[i] == target) | minimum-distance-to-the-target-element | Simple Python3 one-liner (beats ~77%) | y-arjun-y | 0 | 40 | minimum distance to the target element | 1,848 | 0.585 | Easy | 26,333 |
https://leetcode.com/problems/minimum-distance-to-the-target-element/discuss/1736947/Python-Simple-or-O(n)-or-Beats-99.56-in-Memory | class Solution:
def getMinDistance(self, nums: List[int], target: int, start: int) -> int:
min_sum = 10**5
for i in range(len(nums)):
if nums[i]==target:
if abs(i-start) < min_sum:
min_sum = abs(i-start)
return min_sum | minimum-distance-to-the-target-element | Python Simple | O(n) | Beats 99.56% in Memory | veerbhansari | 0 | 44 | minimum distance to the target element | 1,848 | 0.585 | Easy | 26,334 |
https://leetcode.com/problems/minimum-distance-to-the-target-element/discuss/1665541/Python-1-liner%3A-O(1)-Space-O(n)-Time-Almost-Never-Visits-All-Elements | class Solution:
def getMinDistance(self, nums: List[int], target: int, start: int) -> int:
return next(j for j in range(len(nums)) if (start+j < len(nums) and nums[start + j] == target) or (start >= j and nums[start - j] == target)) | minimum-distance-to-the-target-element | Python 1-liner: O(1) Space O(n) Time, Almost Never Visits All Elements | SamRagusa | 0 | 62 | minimum distance to the target element | 1,848 | 0.585 | Easy | 26,335 |
https://leetcode.com/problems/minimum-distance-to-the-target-element/discuss/1519218/Walk-from-start-to-both-directions-87-speed | class Solution:
def getMinDistance(self, nums: List[int], target: int, start: int) -> int:
len_nums = len(nums)
for i in range(len_nums):
if (start + i < len_nums and nums[start + i] == target or
start - i > - 1 and nums[start - i] == target):
return i... | minimum-distance-to-the-target-element | Walk from start to both directions, 87% speed | EvgenySH | 0 | 56 | minimum distance to the target element | 1,848 | 0.585 | Easy | 26,336 |
https://leetcode.com/problems/minimum-distance-to-the-target-element/discuss/1389412/Python-simple-solution | class Solution:
def getMinDistance(self, nums: List[int], target: int, start: int) -> int:
l = len(nums)
an=[]
for i in range(0,l):
if nums[i] == target:
an.append(abs(i-start))
return min(an) | minimum-distance-to-the-target-element | Python simple solution | kdevharsh2001 | 0 | 58 | minimum distance to the target element | 1,848 | 0.585 | Easy | 26,337 |
https://leetcode.com/problems/minimum-distance-to-the-target-element/discuss/1192674/Intuitive-approach-by-list-comprehension-map-and-sort | class Solution:
def getMinDistance(self, nums: List[int], target: int, start: int) -> int:
# 0): nums = [1,2,3,4,5], target=5, start=3
# 1): plist = [4]
# 2): dlist = [1]
# 3): ans = dlist[0] = 1
plist = [i for i, v in enumerate(nums) if v == target]
dlist = list(map(... | minimum-distance-to-the-target-element | Intuitive approach by list comprehension, map and sort | puremonkey2001 | 0 | 37 | minimum distance to the target element | 1,848 | 0.585 | Easy | 26,338 |
https://leetcode.com/problems/minimum-distance-to-the-target-element/discuss/1191568/Simple-python-99-faster | class Solution:
def getMinDistance(self, nums: List[int], target: int, start: int) -> int:
min_abs = float('inf')
for i in range(len(nums)):
if nums[i]==target:
min_abs = min(abs(i-start),min_abs)
return min_abs | minimum-distance-to-the-target-element | Simple python 99% faster | pheobhe | 0 | 69 | minimum distance to the target element | 1,848 | 0.585 | Easy | 26,339 |
https://leetcode.com/problems/minimum-distance-to-the-target-element/discuss/1187113/PythonPython3-Solution | class Solution:
def getMinDistance(self, nums: List[int], target: int, start: int) -> int:
mini = 10**9
for i,num in enumerate(nums):
if num == target:
mini = min(mini,abs(i-start))
return mini | minimum-distance-to-the-target-element | Python/Python3 Solution | prasanthksp1009 | 0 | 70 | minimum distance to the target element | 1,848 | 0.585 | Easy | 26,340 |
https://leetcode.com/problems/splitting-a-string-into-descending-consecutive-values/discuss/2632013/Clear-Python-DFS-with-comments | class Solution:
def splitString(self, s: str) -> bool:
"""
Time = O(2^N)
Space = O(N) space from stack
"""
def dfs(index: int, last: int) -> bool:
if index == len(s):
return True
# j: [index, len(s)-1]
... | splitting-a-string-into-descending-consecutive-values | Clear Python DFS with comments | changyou1009 | 0 | 8 | splitting a string into descending consecutive values | 1,849 | 0.323 | Medium | 26,341 |
https://leetcode.com/problems/splitting-a-string-into-descending-consecutive-values/discuss/1837563/Slow-but-easy-solution | class Solution:
def splitString(self, s: str) -> bool:
s = str(int(s))
def check(l):
if len(l)<2:
return False
#print(l)
for i in range(len(l)-1):
if int(l[i])!=int(l[i+1])+1:
return False
return Tru... | splitting-a-string-into-descending-consecutive-values | Slow but easy solution | aazad20 | 0 | 56 | splitting a string into descending consecutive values | 1,849 | 0.323 | Medium | 26,342 |
https://leetcode.com/problems/splitting-a-string-into-descending-consecutive-values/discuss/1755227/Python3-or-Recursive-solution-with-a-helper-function-or-24ms-beats-99.24 | class Solution:
def splitString(self, s: str) -> bool:
self.res = False
def helper(s, k):
n1 = int(s[:k+1])
for j in range(k+1, len(s)):
n2 = int(s[k+1:j+1])
if n1 - n2 == 1:
if j == len(s)-1:
self.re... | splitting-a-string-into-descending-consecutive-values | Python3 | Recursive solution with a helper function | 24ms beats 99.24% | nandhakiran366 | 0 | 39 | splitting a string into descending consecutive values | 1,849 | 0.323 | Medium | 26,343 |
https://leetcode.com/problems/splitting-a-string-into-descending-consecutive-values/discuss/1357639/String-manipulation-96-speed | class Solution:
def splitString(self, s: str) -> bool:
s = s.lstrip("0")
if s:
for end in range(1, len(s)):
next_num = str(int(s[:end]) - 1)
remaining_s = s[end:]
if set(remaining_s) == {"0"}:
remaining_s = "0"
... | splitting-a-string-into-descending-consecutive-values | String manipulation, 96% speed | EvgenySH | 0 | 66 | splitting a string into descending consecutive values | 1,849 | 0.323 | Medium | 26,344 |
https://leetcode.com/problems/splitting-a-string-into-descending-consecutive-values/discuss/1188042/Python3-Recursion-solution-for-reference | class Solution:
def splitString(self, s: str) -> bool:
# convert the string into digits.
s = [int(i) for i in list(s)]
def r(base, index, nums):
## increment power of 10.
d = 0
## accumulate the value for 10.
acc = 0
res = False
## chec... | splitting-a-string-into-descending-consecutive-values | [Python3] Recursion solution for reference | vadhri_venkat | 0 | 51 | splitting a string into descending consecutive values | 1,849 | 0.323 | Medium | 26,345 |
https://leetcode.com/problems/splitting-a-string-into-descending-consecutive-values/discuss/1186877/Python3-backtracking | class Solution:
def splitString(self, s: str) -> bool:
def fn(i, x):
"""Return True if s[i:] can be split following x."""
if i == len(s): return True
if x == 0: return False
ans = False
for ii in range(i, len(s) - int(i == 0)):
... | splitting-a-string-into-descending-consecutive-values | [Python3] backtracking | ye15 | 0 | 24 | splitting a string into descending consecutive values | 1,849 | 0.323 | Medium | 26,346 |
https://leetcode.com/problems/minimum-adjacent-swaps-to-reach-the-kth-smallest-number/discuss/1186887/Python3-brute-force | class Solution:
def getMinSwaps(self, num: str, k: int) -> int:
num = list(num)
orig = num.copy()
for _ in range(k):
for i in reversed(range(len(num)-1)):
if num[i] < num[i+1]:
ii = i+1
while ii < len(num) and n... | minimum-adjacent-swaps-to-reach-the-kth-smallest-number | [Python3] brute-force | ye15 | 7 | 803 | minimum adjacent swaps to reach the kth smallest number | 1,850 | 0.719 | Medium | 26,347 |
https://leetcode.com/problems/minimum-adjacent-swaps-to-reach-the-kth-smallest-number/discuss/1467322/Python-3-or-Permutation-Brute-Force-or-Explanation | class Solution:
def getMinSwaps(self, num: str, k: int) -> int:
def next_permutation(nums):
small = len(nums) - 2
while small >= 0 and nums[small] >= nums[small+1]: small -= 1 # find last place there is an increase
if small == -1: nums.reverse() ... | minimum-adjacent-swaps-to-reach-the-kth-smallest-number | Python 3 | Permutation, Brute Force | Explanation | idontknoooo | 2 | 378 | minimum adjacent swaps to reach the kth smallest number | 1,850 | 0.719 | Medium | 26,348 |
https://leetcode.com/problems/minimum-adjacent-swaps-to-reach-the-kth-smallest-number/discuss/1928733/Python-or-using-next_perm-with-explanation | class Solution:
def getMinSwaps(self, num: str, k: int) -> int:
#find kth
target=num
for i in range(k):
target=self.next_perm(target)
#count step
res=0
num=list(num)
for i in range(len(num)):
if num[i]!=target[i]:
j=i
#... | minimum-adjacent-swaps-to-reach-the-kth-smallest-number | Python | using next_perm with explanation | ginaaunchat | 1 | 218 | minimum adjacent swaps to reach the kth smallest number | 1,850 | 0.719 | Medium | 26,349 |
https://leetcode.com/problems/minimum-adjacent-swaps-to-reach-the-kth-smallest-number/discuss/1431107/O(n*(k%2Bn-log-n))-T-or-O(n)-S-or-implicit-treap-%2B-next-permutation-or-Python-3 | class Solution:
def getMinSwaps(self, num: str, k: int) -> int:
import random
class ImplicitTreap:
class ImplicitTreapNode:
def __init__(self, y, value, left=None, right=None):
self.y = y
self.value = value
... | minimum-adjacent-swaps-to-reach-the-kth-smallest-number | O(n*(k+n log n)) T | O(n) S | implicit treap + next permutation | Python 3 | CiFFiRO | 0 | 167 | minimum adjacent swaps to reach the kth smallest number | 1,850 | 0.719 | Medium | 26,350 |
https://leetcode.com/problems/minimum-adjacent-swaps-to-reach-the-kth-smallest-number/discuss/1188750/Python3-Solution-for-reference-combining-next-permutation-and-adjacent-swapping | class Solution:
def getMinSwaps(self, num: str, k: int) -> int:
nums = [int(i) for i in num]
orig = [int(i) for i in num]
right = len(nums) - 1
ans = 0
for _ in range(k):
for x in range(right, 0, -1):
if nums[x] > nums[x-1]:
it... | minimum-adjacent-swaps-to-reach-the-kth-smallest-number | [Python3] Solution for reference combining next permutation and adjacent swapping | vadhri_venkat | 0 | 263 | minimum adjacent swaps to reach the kth smallest number | 1,850 | 0.719 | Medium | 26,351 |
https://leetcode.com/problems/minimum-interval-to-include-each-query/discuss/1422509/For-Beginners-oror-Easy-Approach-oror-Well-Explained-oror-Clean-and-Concise | class Solution:
def minInterval(self, intervals: List[List[int]], queries: List[int]) -> List[int]:
intervals.sort(key = lambda x:x[1]-x[0])
q = sorted([qu,i] for i,qu in enumerate(queries))
res=[-1]*len(queries)
for left,right in intervals:
ind = bisect.bisect(q,[left])
while ind... | minimum-interval-to-include-each-query | 📌📌 For Beginners || Easy-Approach || Well-Explained || Clean & Concise 🐍 | abhi9Rai | 4 | 274 | minimum interval to include each query | 1,851 | 0.479 | Hard | 26,352 |
https://leetcode.com/problems/minimum-interval-to-include-each-query/discuss/2652142/Clean-Python3-or-Sorting-and-Heap | class Solution:
def minInterval(self, intervals: List[List[int]], queries: List[int]) -> List[int]:
queries_asc = sorted((q, i) for i, q in enumerate(queries))
intervals.sort()
i, num_intervals = 0, len(intervals)
size_heap = [] # (size, left)
for pos, qnu... | minimum-interval-to-include-each-query | Clean Python3 | Sorting & Heap | ryangrayson | 2 | 56 | minimum interval to include each query | 1,851 | 0.479 | Hard | 26,353 |
https://leetcode.com/problems/minimum-interval-to-include-each-query/discuss/1187214/PythonPython3-solution-using-heap-and-sorting-method | class Solution:
def minInterval(self, intervals: List[List[int]], queries: List[int]) -> List[int]:
# To store the output result
lis = [0 for i in range(len(queries))]
#sort the intervals in the reverse order
intervals.sort(reverse = True)
#View the intervals list
print(intervals)
#s... | minimum-interval-to-include-each-query | Python/Python3 solution using heap and sorting method | prasanthksp1009 | 2 | 112 | minimum interval to include each query | 1,851 | 0.479 | Hard | 26,354 |
https://leetcode.com/problems/minimum-interval-to-include-each-query/discuss/1186916/Python3-priority-queue | class Solution:
def minInterval(self, intervals: List[List[int]], queries: List[int]) -> List[int]:
intervals.sort()
pq = []
k = 0
ans = [-1] * len(queries)
for query, i in sorted(zip(queries, range(len(queries)))):
while k < len(intervals) and intervals[k][0] <... | minimum-interval-to-include-each-query | [Python3] priority queue | ye15 | 2 | 81 | minimum interval to include each query | 1,851 | 0.479 | Hard | 26,355 |
https://leetcode.com/problems/minimum-interval-to-include-each-query/discuss/2315278/Python-3-oror-hashMap-priority-queue-min-heap | class Solution:
def minInterval(self, intervals: List[List[int]], queries: List[int]) -> List[int]:
hashMap = {}
intervals.sort()
minHeap = []
i_l = len(intervals)
i = 0
for q in sorted(queries):
while i < i_l and intervals[i][0] <= q:
start, end = intervals[i]
heapq.heappush(minHeap, [(end-st... | minimum-interval-to-include-each-query | Python 3 || hashMap, priority queue, min-heap | sagarhasan273 | 1 | 114 | minimum interval to include each query | 1,851 | 0.479 | Hard | 26,356 |
https://leetcode.com/problems/minimum-interval-to-include-each-query/discuss/1187869/Python-3-Dynamic-heap-with-detailed-explanation-(1900ms) | class Solution:
def minInterval(self, intervals: List[List[int]], queries: List[int]) -> List[int]:
# sort queries from small to large
q = deque(sorted([(x, i) for i, x in enumerate(queries)]))
# answer to queries, initial state set to -1
ans = [-1] * len(queries)
... | minimum-interval-to-include-each-query | [Python 3] Dynamic heap with detailed explanation (1900ms) | chestnut890123 | 0 | 78 | minimum interval to include each query | 1,851 | 0.479 | Hard | 26,357 |
https://leetcode.com/problems/maximum-population-year/discuss/1210686/Python3-O(N) | class Solution:
def maximumPopulation(self, logs: List[List[int]]) -> int:
# the timespan 1950-2050 covers 101 years
delta = [0] * 101
# to make explicit the conversion from the year (1950 + i) to the ith index
conversionDiff = 1950
for l in logs:
# the log's first entry, birth, ... | maximum-population-year | Python3 O(N) | signifying | 21 | 2,000 | maximum population year | 1,854 | 0.599 | Easy | 26,358 |
https://leetcode.com/problems/maximum-population-year/discuss/1198702/Python3-greedy | class Solution:
def maximumPopulation(self, logs: List[List[int]]) -> int:
vals = []
for x, y in logs:
vals.append((x, 1))
vals.append((y, -1))
ans = prefix = most = 0
for x, k in sorted(vals):
prefix += k
if prefix > most:
... | maximum-population-year | [Python3] greedy | ye15 | 7 | 886 | maximum population year | 1,854 | 0.599 | Easy | 26,359 |
https://leetcode.com/problems/maximum-population-year/discuss/1206057/python-solution | class Solution:
def maximumPopulation(self, logs: List[List[int]]) -> int:
b=[logs[i][0] for i in range(len(logs))]
d=[logs[i][1] for i in range(len(logs))]
m=0 #max population
a=0 #alive
r=1950
for i in range(1950,2051):
a+=b.count(i)-d.count(i)
... | maximum-population-year | python solution | _Light_ | 2 | 226 | maximum population year | 1,854 | 0.599 | Easy | 26,360 |
https://leetcode.com/problems/maximum-population-year/discuss/2714948/Simple-python-solution | class Solution:
def maximumPopulation(self, logs: List[List[int]]) -> int:
dic={}
logs.sort()
initial=logs[0][0]
final=logs[-1][1]
for i in range(initial,final):
dic[i]=0
for j in logs:
if(i>=j[0] and i<j[1]):
dic[i]... | maximum-population-year | Simple python solution | Kiran_Rokkam | 0 | 11 | maximum population year | 1,854 | 0.599 | Easy | 26,361 |
https://leetcode.com/problems/maximum-population-year/discuss/2560790/using-heapq | class Solution:
def maximumPopulation(self, logs: List[List[int]]) -> int:
# sort based on birth
logs.sort()
# an array to save info
death_birth_years = []
# maximum length
maxlen = 0
# output year
year = None
# itearte over logs
for i... | maximum-population-year | using heapq | krishnamsgn | 0 | 24 | maximum population year | 1,854 | 0.599 | Easy | 26,362 |
https://leetcode.com/problems/maximum-population-year/discuss/2481447/Python3-differential-array | class Solution:
def maximumPopulation(self, logs: List[List[int]]) -> int:
# Year_Converted = Year - 1949
# The converted year is between 1 and 101
diff_array = [0] * 102
for log in logs:
birth_converted = log[0] - 1949
die_converted = log[1] - 1949
... | maximum-population-year | Python3 differential array | Aden-Q | 0 | 41 | maximum population year | 1,854 | 0.599 | Easy | 26,363 |
https://leetcode.com/problems/maximum-population-year/discuss/2021335/Python-Array-and-Dictionary-or-Clean-and-Simple! | class Solution:
def maximumPopulation(self, logs):
start = 1950
population = [0] * 101
for b,d in logs:
b,d = b-start, d-start
for year in range(b, d): population[year] += 1
return population.index(max(population)) + start | maximum-population-year | Python - Array and Dictionary | Clean and Simple! | domthedeveloper | 0 | 94 | maximum population year | 1,854 | 0.599 | Easy | 26,364 |
https://leetcode.com/problems/maximum-population-year/discuss/2021335/Python-Array-and-Dictionary-or-Clean-and-Simple! | class Solution:
def maximumPopulation(self, logs):
population = Counter()
for b,d in logs:
for year in range(b, d): population[year] += 1
maxPopulation = max(population.values())
return min(k for k,v in population.items() if v == maxPopulation) | maximum-population-year | Python - Array and Dictionary | Clean and Simple! | domthedeveloper | 0 | 94 | maximum population year | 1,854 | 0.599 | Easy | 26,365 |
https://leetcode.com/problems/maximum-population-year/discuss/1857572/Simple-Python-Solution-oror-55-Faster-oror-Memory-less-than-60 | class Solution:
def maximumPopulation(self, logs: List[List[int]]) -> int:
years=sorted(list(chain(*logs))) ; ans=0 ; mx=0
for year in years:
cnt=0
for i in range(len(logs)):
if logs[i][0]<=year and logs[i][1]>year: cnt+=1
if cnt>mx: mx=cnt ; ans=y... | maximum-population-year | Simple Python Solution || 55% Faster || Memory less than 60% | Taha-C | 0 | 88 | maximum population year | 1,854 | 0.599 | Easy | 26,366 |
https://leetcode.com/problems/maximum-population-year/discuss/1857572/Simple-Python-Solution-oror-55-Faster-oror-Memory-less-than-60 | class Solution:
def maximumPopulation(self, logs: List[List[int]]) -> int:
d = defaultdict(int)
for birth, death in logs:
for i in range(birth, death): d[i] += 1
return max(d.items(), key=lambda x:(x[1],-x[0]))[0] | maximum-population-year | Simple Python Solution || 55% Faster || Memory less than 60% | Taha-C | 0 | 88 | maximum population year | 1,854 | 0.599 | Easy | 26,367 |
https://leetcode.com/problems/maximum-population-year/discuss/1529568/One-pass-over-logs-and-years | class Solution:
def maximumPopulation(self, logs: List[List[int]]) -> int:
years = defaultdict(int)
max_population = max_year = 0
for start, end in logs:
for y in range(start, end):
years[y] += 1
if (years[y] > max_population or
... | maximum-population-year | One pass over logs and years | EvgenySH | 0 | 182 | maximum population year | 1,854 | 0.599 | Easy | 26,368 |
https://leetcode.com/problems/maximum-population-year/discuss/1388920/Explained-Python3-Simulation-with-two-events-in-timeline-'born'-'dead' | class Solution:
def maximumPopulation(self, logs: List[List[int]]) -> int:
events = []
for born, died in logs:
events.extend([(born, '1_born'), (died, '0_died')])
# let us simulate the events in timeline order
# the order in which they happened
events... | maximum-population-year | Explained Python3 Simulation, with two events in timeline, 'born' 'dead' | yozaam | 0 | 144 | maximum population year | 1,854 | 0.599 | Easy | 26,369 |
https://leetcode.com/problems/maximum-population-year/discuss/1229181/Python3-simple-solution-using-dictionary | class Solution:
def maximumPopulation(self, logs: List[List[int]]) -> int:
d = {}
for i,j in enumerate(logs):
for k in range(j[0],j[1]):
d[k] = d.get(k,0)+1
return sorted([i for i in d.keys() if d[i] == max(d.values())])[0] | maximum-population-year | Python3 simple solution using dictionary | EklavyaJoshi | -1 | 121 | maximum population year | 1,854 | 0.599 | Easy | 26,370 |
https://leetcode.com/problems/maximum-population-year/discuss/1223429/easy-solution-or-array-difference-algorithmor-python | class Solution:
def maximumPopulation(self, logs: List[List[int]]) -> int:
u=[0]*(3000)
for j in logs:
u[j[0]]+=1
u[j[1]]-=1
p=[u[0]]
for j in range(1,len(u)):
p.append(p[-1]+u[... | maximum-population-year | easy solution | array difference algorithm| python | chikushen99 | -1 | 170 | maximum population year | 1,854 | 0.599 | Easy | 26,371 |
https://leetcode.com/problems/maximum-distance-between-a-pair-of-values/discuss/2209743/Python3-simple-solution-using-two-pointers | class Solution:
def maxDistance(self, nums1: List[int], nums2: List[int]) -> int:
length1, length2 = len(nums1), len(nums2)
i,j = 0,0
result = 0
while i < length1 and j < length2:
if nums1[i] > nums2[j]:
i+=1
else:
resu... | maximum-distance-between-a-pair-of-values | 📌 Python3 simple solution using two pointers | Dark_wolf_jss | 6 | 69 | maximum distance between a pair of values | 1,855 | 0.527 | Medium | 26,372 |
https://leetcode.com/problems/maximum-distance-between-a-pair-of-values/discuss/1198697/Python-Solution-Simple-2-Pointer | class Solution:
def maxDistance(self, nums1: List[int], nums2: List[int]) -> int:
max_diff = 0
i = 0
j = 0
while i<len(nums1) and j <len(nums2):
if i <= j:
if nums1[i] <= nums2[j]:
max_diff=max(max_diff,j-i)
j += 1
... | maximum-distance-between-a-pair-of-values | Python Solution- Simple -2 Pointer | SaSha59 | 3 | 132 | maximum distance between a pair of values | 1,855 | 0.527 | Medium | 26,373 |
https://leetcode.com/problems/maximum-distance-between-a-pair-of-values/discuss/1198720/Python3-sliding-window | class Solution:
def maxDistance(self, nums1: List[int], nums2: List[int]) -> int:
ans = ii = 0
for i, x in enumerate(nums2):
while ii <= i and ii < len(nums1) and nums1[ii] > nums2[i]: ii += 1
if ii < len(nums1) and nums1[ii] <= nums2[i]: ans = max(ans, i - ii)
retu... | maximum-distance-between-a-pair-of-values | [Python3] sliding window | ye15 | 2 | 68 | maximum distance between a pair of values | 1,855 | 0.527 | Medium | 26,374 |
https://leetcode.com/problems/maximum-distance-between-a-pair-of-values/discuss/1944437/Python-Binary-Search | class Solution:
def maxDistance(self, nums1: List[int], nums2: List[int]) -> int:
result = 0
max_dist = 0
m, n = len(nums1), len(nums2)
#iterate from left of nums1
for i in range(m):
#for every element, find the index of nums2 with value <= nums1[i] in the range nums2[i:len(nums... | maximum-distance-between-a-pair-of-values | Python Binary Search | eforean | 1 | 46 | maximum distance between a pair of values | 1,855 | 0.527 | Medium | 26,375 |
https://leetcode.com/problems/maximum-distance-between-a-pair-of-values/discuss/2775161/Easy-Linear-solution-O(1)-space-complexity | class Solution:
def maxDistance(self, nums1: List[int], nums2: List[int]) -> int:
res = 0
m, n = len(nums1), len(nums2)
i, j = 0, 0
while i < m and j < n:
if nums1[i] <= nums2[j]:
res = max(res, j-i)
j += 1
else:
... | maximum-distance-between-a-pair-of-values | Easy Linear solution, O(1) space complexity | byuns9334 | 0 | 5 | maximum distance between a pair of values | 1,855 | 0.527 | Medium | 26,376 |
https://leetcode.com/problems/maximum-distance-between-a-pair-of-values/discuss/2599020/Python3-or-Solved-By-Performing-Binary-Search-on-nums2-to-find-RightMost-Limit-for-Every-index-I | class Solution:
#let n = len(nums1) and m = len(nums2)!
#Time-Complexity: O(n * m)
#Space-Complexity: O(1)
def maxDistance(self, nums1: List[int], nums2: List[int]) -> int:
#Approach: Linearly try each and every element in nums1 array! Perform binary
#search to find rightmost element in ... | maximum-distance-between-a-pair-of-values | Python3 | Solved By Performing Binary Search on nums2 to find RightMost Limit for Every index I | JOON1234 | 0 | 10 | maximum distance between a pair of values | 1,855 | 0.527 | Medium | 26,377 |
https://leetcode.com/problems/maximum-distance-between-a-pair-of-values/discuss/2494547/Python-O(N)-Solution | class Solution:
def maxDistance(self, nums1: List[int], nums2: List[int]) -> int:
i = 0
j = 0
max_distance = float("-inf")
while i < len(nums1) and j < len(nums2):
if nums1[i] <= nums2[j]:
max_distance = max(max_distance, j-i)
else:
... | maximum-distance-between-a-pair-of-values | Python O(N) Solution | yash921 | 0 | 32 | maximum distance between a pair of values | 1,855 | 0.527 | Medium | 26,378 |
https://leetcode.com/problems/maximum-distance-between-a-pair-of-values/discuss/2459975/C%2B%2BPython-Faster-Optimized-solution | class Solution:
def maxDistance(self, nums1: List[int], nums2: List[int]) -> int:
i=0
j=0
res = 0
n = len(nums1)
m = len(nums2)
while i<n and j<m:
if nums1[i]>nums2[j]:
i+=1
else:
res=max(res,j-i)
j+=1
return res | maximum-distance-between-a-pair-of-values | C++/Python Faster Optimized solution | arpit3043 | 0 | 48 | maximum distance between a pair of values | 1,855 | 0.527 | Medium | 26,379 |
https://leetcode.com/problems/maximum-distance-between-a-pair-of-values/discuss/2393307/Python-Accurate-Faster-Solution-oror-Documented | class Solution:
def maxDistance(self, nums1: List[int], nums2: List[int]) -> int:
maxD = 0 # initialize max distance with 0
i = j = 0 # initialize array pointers with 0
# traverse until both pointers < array-length
while i < len(n... | maximum-distance-between-a-pair-of-values | [Python] Accurate Faster Solution || Documented | Buntynara | 0 | 10 | maximum distance between a pair of values | 1,855 | 0.527 | Medium | 26,380 |
https://leetcode.com/problems/maximum-distance-between-a-pair-of-values/discuss/2200574/Easy-2-pointer-approach | class Solution:
def maxDistance(self, nums1: List[int], nums2: List[int]) -> int:
i, j, n, m, maxDistance = 0, 0, len(nums1), len(nums2), 0
while i < n and j < m:
if nums1[i] <= nums2[j]:
maxDistance = max(maxDistance, j - i)
else:
i +... | maximum-distance-between-a-pair-of-values | Easy 2 pointer approach | Vaibhav7860 | 0 | 32 | maximum distance between a pair of values | 1,855 | 0.527 | Medium | 26,381 |
https://leetcode.com/problems/maximum-distance-between-a-pair-of-values/discuss/1215959/Python3-Simple-Two-Pointers-Approach-Explained-with-Comments | class Solution:
def maxDistance(self, nums1: List[int], nums2: List[int]) -> int:
'''
1. Start from the rightmost index of nums2 and nums1. If len(nums1)>len(nums2), start from index len(nums2)-1 of array nums1
2. Let j be the current index of nums2 and i be the current index of nums1
... | maximum-distance-between-a-pair-of-values | Python3 Simple Two Pointers Approach, Explained with Comments | bPapan | 0 | 72 | maximum distance between a pair of values | 1,855 | 0.527 | Medium | 26,382 |
https://leetcode.com/problems/maximum-distance-between-a-pair-of-values/discuss/1199013/Straightforward-Python3-O(n)-two-pointers-solution-with-explanation | class Solution:
def maxDistance(self, nums1: List[int], nums2: List[int]) -> int:
###################################################################
# Assumption/Requirement
###################################################################
# nums1 and nums2 are both decreasing
... | maximum-distance-between-a-pair-of-values | Straightforward Python3 O(n) two-pointers solution with explanation | tkuo-tkuo | 0 | 38 | maximum distance between a pair of values | 1,855 | 0.527 | Medium | 26,383 |
https://leetcode.com/problems/maximum-subarray-min-product/discuss/1198800/Python3-mono-stack | class Solution:
def maxSumMinProduct(self, nums: List[int]) -> int:
prefix = [0]
for x in nums: prefix.append(prefix[-1] + x)
ans = 0
stack = []
for i, x in enumerate(nums + [-inf]): # append "-inf" to force flush all elements
while stack and stack[-1][1... | maximum-subarray-min-product | [Python3] mono-stack | ye15 | 10 | 345 | maximum subarray min product | 1,856 | 0.378 | Medium | 26,384 |
https://leetcode.com/problems/maximum-subarray-min-product/discuss/1200240/Python-monotonic-stack-beats-100 | class Solution:
def maxSumMinProduct(self, nums: List[int]) -> int:
mod=int(1e9+7)
stack=[] # (index, prefix sum at index)
rsum=0
res=0
nums.append(0)
for i, v in enumerate(nums):
while stack and nums[stack[-1][0]] >= v:
i... | maximum-subarray-min-product | Python monotonic stack beats 100% | alan9259 | 5 | 509 | maximum subarray min product | 1,856 | 0.378 | Medium | 26,385 |
https://leetcode.com/problems/maximum-subarray-min-product/discuss/2411914/Python3-or-Similar-to-Max-Rectangle-in-Histogram | class Solution:
def maxSumMinProduct(self, nums: List[int]) -> int:
n,ans=len(nums),-float('inf')
mod=10**9+7
left=[0 for i in range(n)]
right=[0 for i in range(n)]
prefixSum=[0 for i in range(n)]
prefixSum[0]=nums[0]
for i in range(1,n):
prefixSum... | maximum-subarray-min-product | [Python3] | Similar to Max Rectangle in Histogram | swapnilsingh421 | 0 | 54 | maximum subarray min product | 1,856 | 0.378 | Medium | 26,386 |
https://leetcode.com/problems/maximum-subarray-min-product/discuss/2057686/python3-dp-and-stack-solutions-for-reference. | class Solution:
def dp(self, nums):
N = len(nums)
dp = [(0, 0) for _ in range(N)]
ans = 0
for x in range(N):
for y in range(x, N):
if x == y:
dp[y] = (nums[y], nums[y])
else:
dp[y] = (min(dp[... | maximum-subarray-min-product | [python3] dp and stack solutions for reference. | vadhri_venkat | 0 | 76 | maximum subarray min product | 1,856 | 0.378 | Medium | 26,387 |
https://leetcode.com/problems/maximum-subarray-min-product/discuss/1436397/Python-Clean | class Solution:
def maxSumMinProduct(self, nums: List[int]) -> int:
N, maximum = len(nums), 0
cumulative, stack = [0], []
for num in nums:
cumulative.append(cumulative[-1] + num)
for i, num in enumerate(nums + [-float(inf)]):
start = i
... | maximum-subarray-min-product | [Python] Clean | soma28 | 0 | 223 | maximum subarray min product | 1,856 | 0.378 | Medium | 26,388 |
https://leetcode.com/problems/maximum-subarray-min-product/discuss/1203747/very-easy-python-soln | class Solution(object):
def maxSumMinProduct(self, nums):
n,ans,mod=len(nums),0,10**9 + 7
lft,st=[0],[[nums[0],0]]
for i in range(1,n):
cur=0
while st and nums[i]<=st[-1][0]:
cur+=st.pop()[1]+1
st.append([nums[i],cur])
lft.appen... | maximum-subarray-min-product | very easy python soln | aayush_chhabra | 0 | 257 | maximum subarray min product | 1,856 | 0.378 | Medium | 26,389 |
https://leetcode.com/problems/maximum-subarray-min-product/discuss/1201859/python-simple-intuitive-greedy-solution | class Solution:
def maxSumMinProduct(self, nums: List[int]) -> int:
n = len(nums)
s = list(accumulate(nums)) # sum
t = sorted([(v, i) for i, v in enumerate(nums)], reverse=True) # to greedily iterate over values
j = [i for i in range(n)] # jump array, at first one can only jump in it... | maximum-subarray-min-product | [python] simple intuitive greedy solution | iamlegend123 | 0 | 154 | maximum subarray min product | 1,856 | 0.378 | Medium | 26,390 |
https://leetcode.com/problems/largest-color-value-in-a-directed-graph/discuss/1200668/easy-python-sol-.. | class Solution(object):
def largestPathValue(self, colors, edges):
n=len(colors)
graph=defaultdict(list)
indegree=defaultdict(int)
for u,v in edges:
graph[u].append(v)
indegree[v]+=1
queue=[]
dp=[[0]*26 for _ in range(n)]
... | largest-color-value-in-a-directed-graph | easy python sol .. | aayush_chhabra | 9 | 299 | largest color value in a directed graph | 1,857 | 0.407 | Hard | 26,391 |
https://leetcode.com/problems/largest-color-value-in-a-directed-graph/discuss/1199847/Python3-dp | class Solution:
def largestPathValue(self, colors: str, edges: List[List[int]]) -> int:
graph = {}
indeg = [0] * len(colors)
for u, v in edges:
indeg[v] += 1
graph.setdefault(u, []).append(v)
# Kahn's algo
roots = [x for x in range(len(co... | largest-color-value-in-a-directed-graph | [Python3] dp | ye15 | 2 | 98 | largest color value in a directed graph | 1,857 | 0.407 | Hard | 26,392 |
https://leetcode.com/problems/largest-color-value-in-a-directed-graph/discuss/1206392/Python-Iterative-DFS-Solution | class Solution:
def largestPathValue(self, colors: str, edges: List[List[int]]) -> int:
d = defaultdict(list)
for v1, v2 in edges:
d[v1].append(v2)
visited = {}
counts = [[0] * 26 for _ in range(len(colors))]
stack = []
global_max = 0
for root in d... | largest-color-value-in-a-directed-graph | Python Iterative DFS Solution | kylergao | 0 | 130 | largest color value in a directed graph | 1,857 | 0.407 | Hard | 26,393 |
https://leetcode.com/problems/sorting-the-sentence/discuss/1219040/Python3-99.60-Fast-Solution | class Solution:
def sortSentence(self, s: str) -> str:
arr = [i[-1] + i[:-1] for i in s.split()]
arr.sort()
ans = ""
for i in arr:
ans += i[1:] + ' '
return ans[:-1] | sorting-the-sentence | [Python3] 99.60% Fast Solution | VoidCupboard | 23 | 2,000 | sorting the sentence | 1,859 | 0.844 | Easy | 26,394 |
https://leetcode.com/problems/sorting-the-sentence/discuss/1210285/2-Lines-or-Python-Solution-or-Linear-TIme-or-Linear-Memory | class Solution:
def sortSentence(self, s: str) -> str:
# split the string and sort the words based upon the last letter
word_list = sorted(s.split(), key = lambda word: word[-1], reverse = False)
return " ".join([word[:-1] for word in word_list]) # join the words, after removing the last letter i... | sorting-the-sentence | 2 Lines | Python Solution | Linear TIme | Linear Memory | ramanaditya | 19 | 1,800 | sorting the sentence | 1,859 | 0.844 | Easy | 26,395 |
https://leetcode.com/problems/sorting-the-sentence/discuss/1210285/2-Lines-or-Python-Solution-or-Linear-TIme-or-Linear-Memory | class Solution:
def sortSentence(self, s: str) -> str:
word_list = s.split() # form a list of words
n = len(word_list) # total words in the list, at max 9
# dict to make k, v pairs as there are at max 9 words in the array
# key as position of word, value as word without position
# b... | sorting-the-sentence | 2 Lines | Python Solution | Linear TIme | Linear Memory | ramanaditya | 19 | 1,800 | sorting the sentence | 1,859 | 0.844 | Easy | 26,396 |
https://leetcode.com/problems/sorting-the-sentence/discuss/1343270/PYTHON-3-or-faster-than-95.14-or-USING-DICTIONARY | class Solution:
def sortSentence(self, s: str) -> str:
x = s.split()
dic = {}
for i in x :
dic[i[-1]] = i[:-1]
return ' '.join([dic[j] for j in sorted(dic)]) | sorting-the-sentence | PYTHON 3 | faster than 95.14% | USING DICTIONARY | rohitkhairnar | 14 | 933 | sorting the sentence | 1,859 | 0.844 | Easy | 26,397 |
https://leetcode.com/problems/sorting-the-sentence/discuss/1986261/Python-Easiest-Solution-With-Explanationor-99.72-Faster-or-Sorting-or-Beg-to-adv | class Solution:
def sortSentence(self, s: str) -> str:
splited_string = s[::-1].split() # here first we are reversing the sting and then spliting it, split() function make each word of the string as a separate element of the list. For example: ['3a', '1sihT', '4ecnetnes', '2si']
splited_st... | sorting-the-sentence | Python Easiest Solution With Explanation| 99.72% Faster | Sorting | Beg to adv | rlakshay14 | 3 | 219 | sorting the sentence | 1,859 | 0.844 | Easy | 26,398 |
https://leetcode.com/problems/sorting-the-sentence/discuss/1492510/1-line-solution-in-Python | class Solution:
def sortSentence(self, s: str) -> str:
return " ".join(word[:-1] for word in sorted(s.split(), key=lambda w: w[-1])) | sorting-the-sentence | 1-line solution in Python | mousun224 | 2 | 292 | sorting the sentence | 1,859 | 0.844 | Easy | 26,399 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.