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/check-if-array-pairs-are-divisible-by-k/discuss/1522233/Python3-Solution-with-using-hashmap | class Solution:
def canArrange(self, arr: List[int], k: int) -> bool:
d = {}
for idx, val in enumerate(arr):
mod = val % k
if mod == 0:
continue
if k - mod in d and d[k - mod] != 0:
d[k - mod] -= 1
if d[k - mod] == 0:
del d[k - mod]
else:
if mod not in d:
d[mod] = 0
d[mod] += 1
return len(d) == 0 | check-if-array-pairs-are-divisible-by-k | [Python3] Solution with using hashmap | maosipov11 | 0 | 128 | check if array pairs are divisible by k | 1,497 | 0.396 | Medium | 22,300 |
https://leetcode.com/problems/check-if-array-pairs-are-divisible-by-k/discuss/1120798/Simple-code-in-Python3 | class Solution:
def canArrange(self, a: List[int], k: int) -> bool:
d=[0]*k
for i in a:
d[i%k]+=1
for i in range(k):
if i==0:
if d[i]%2!=0:
return 0
elif(d[i]!=d[k-i]):
return 0
return 1 | check-if-array-pairs-are-divisible-by-k | Simple code in Python3 | vkn84527 | 0 | 183 | check if array pairs are divisible by k | 1,497 | 0.396 | Medium | 22,301 |
https://leetcode.com/problems/number-of-subsequences-that-satisfy-the-given-sum-condition/discuss/1703899/python-easy-two-pointers-%2B-sorting-solution | class Solution:
def numSubseq(self, nums: List[int], target: int) -> int:
n = len(nums)
nums.sort()
i, j = 0, n-1
res = 0
NUM = 10**9+7
while i <= j:
if nums[i] + nums[j] > target:
j -= 1
elif nums[i] + nums[j] <= target:
res += pow(2, j-i, NUM)
i += 1
#else: # nums[i] + nums[j] == target
return res % NUM | number-of-subsequences-that-satisfy-the-given-sum-condition | python easy two-pointers + sorting solution | byuns9334 | 3 | 755 | number of subsequences that satisfy the given sum condition | 1,498 | 0.381 | Medium | 22,302 |
https://leetcode.com/problems/number-of-subsequences-that-satisfy-the-given-sum-condition/discuss/709579/Python3-number-of-power-of-2-Number-of-Subsequences-That-Satisfy-the-Given-Sum-Condition | class Solution:
def numSubseq(self, nums: List[int], target: int) -> int:
mod = 10 ** 9 + 7
ans = 0
nums.sort()
for i, n in enumerate(nums):
if 2 * n > target:
break
j = bisect.bisect(nums, target - n, lo=i)
ans += pow(2, j - i - 1, mod)
return ans % mod | number-of-subsequences-that-satisfy-the-given-sum-condition | Python3 number of power of 2 - Number of Subsequences That Satisfy the Given Sum Condition | r0bertz | 2 | 785 | number of subsequences that satisfy the given sum condition | 1,498 | 0.381 | Medium | 22,303 |
https://leetcode.com/problems/number-of-subsequences-that-satisfy-the-given-sum-condition/discuss/986887/Python3-Sort-Binary-Search-Count-subsets | class Solution:
def numSubseq(self, nums: List[int], target: int) -> int:
mod = 10 ** 9 + 7
ct = 0
n = len(nums)
nums.sort()
def bsearch(x: List[int], t: int, l: int, n: int = n) -> int:
# Returns the rightmost valid index for number <= target
r = n-1
while(l < r):
m = l + (r - l) // 2 + 1
if x[m] <= t:
l = m
else:
r = m - 1
return r
# loop on min value
for i in range(n):
if 2 * nums[i] > target:
# no need to process further (sorted array)
break
# find max j for valid subarray
j = bsearch(nums, target - nums[i], i)
if nums[i] + nums[j] <= target:
# add 1 * 2^((j - i + 1) - 1) for no. of subsets
# from subarray with one element fixed
ct += pow(2 , j - i, mod) # for efficient handling of large no.s
return ct % mod | number-of-subsequences-that-satisfy-the-given-sum-condition | [Python3] Sort, Binary Search, Count subsets | kj7kunal | 1 | 459 | number of subsequences that satisfy the given sum condition | 1,498 | 0.381 | Medium | 22,304 |
https://leetcode.com/problems/number-of-subsequences-that-satisfy-the-given-sum-condition/discuss/2798490/Two-pointer-approach-or-Python-easy-solution | class Solution:
def numSubseq(self, nums: List[int], target: int) -> int:
nums.sort()
res = 0
mod = (10**9+ 7)
l, r = 0, len(nums) - 1
while l <= r:
if nums[l] * 2 > target:
break
if nums[l] + nums[r] > target:
r -= 1
else :
res += pow(2, r-l, mod)
l += 1
return res % mod | number-of-subsequences-that-satisfy-the-given-sum-condition | Two pointer approach | Python easy solution | mrpranavr | 0 | 15 | number of subsequences that satisfy the given sum condition | 1,498 | 0.381 | Medium | 22,305 |
https://leetcode.com/problems/number-of-subsequences-that-satisfy-the-given-sum-condition/discuss/1689111/Preprocess-Input-to-save-50-runtime | class Solution:
def numSubseq(self, nums: List[int], target: int) -> int:
# sort
nums.sort()
# easy corner case
if nums[0] > target:
return 0
# preprocess the nums to remove useless numbers that are already greater than target
left = 0
right = len(nums)
while left < right:
mid = (left + right) >> 1
if nums[mid] <= target:
left = mid + 1
else:
right = mid
nums = nums[:left]
# regular two-pointer move
right = left-1
left = 0
result = 0
mod = (10**9 + 7)
while left <= right:
if nums[left] + nums[right] <= target:
result += pow(2, right - left, mod)
left += 1
else:
right -= 1
return result % mod | number-of-subsequences-that-satisfy-the-given-sum-condition | Preprocess Input to save 50% runtime | lcatny | 0 | 114 | number of subsequences that satisfy the given sum condition | 1,498 | 0.381 | Medium | 22,306 |
https://leetcode.com/problems/number-of-subsequences-that-satisfy-the-given-sum-condition/discuss/709365/Python3-Sort-%2B-Binary-Search-with-detailed-comments-O(NlogN) | class Solution:
def numSubseq(self, nums: List[int], target: int) -> int:
nums.sort()
ans = 0
for i in range(len(nums)):
# single entry
if 2*nums[i] <= target:
ans += 1
# minimum is nums[i]
# binary search to find the maximum feasible number associated with nums[i]
lo, hi = i+1, len(nums)-1
while lo < hi:
m = (lo + hi) // 2 + 1
if nums[i] + nums[m] > target:
hi = m - 1
else:
lo = m
# In the subarray [i+1, ..., hi], at least one number has to be taken with nums[i],
# count all such combinations
if nums[i] + nums[hi] <= target:
ans += (1 << (hi - i)) - 1
return ans % (10**9 + 7) | number-of-subsequences-that-satisfy-the-given-sum-condition | [Python3] Sort + Binary Search with detailed comments, O(NlogN) | coco987 | 0 | 282 | number of subsequences that satisfy the given sum condition | 1,498 | 0.381 | Medium | 22,307 |
https://leetcode.com/problems/number-of-subsequences-that-satisfy-the-given-sum-condition/discuss/709280/Python3-9-line-locate-the-upper-bound | class Solution:
def numSubseq(self, nums: List[int], target: int) -> int:
nums.sort()
ans = 0
lo, hi = 0, len(nums)-1
while lo <= hi:
if nums[lo] + nums[hi] > target: hi -= 1
else:
ans += pow(2, hi - lo, 1_000_000_007)
lo += 1
return ans % 1_000_000_007 | number-of-subsequences-that-satisfy-the-given-sum-condition | [Python3] 9-line locate the upper bound | ye15 | 0 | 149 | number of subsequences that satisfy the given sum condition | 1,498 | 0.381 | Medium | 22,308 |
https://leetcode.com/problems/max-value-of-equation/discuss/709364/Python3-heap | class Solution:
def findMaxValueOfEquation(self, points: List[List[int]], k: int) -> int:
ans = -inf
hp = []
for xj, yj in points:
while hp and xj - hp[0][1] > k: heappop(hp)
if hp:
ans = max(ans, xj + yj - hp[0][0])
heappush(hp, (xj-yj, xj))
return ans | max-value-of-equation | [Python3] heap | ye15 | 8 | 721 | max value of equation | 1,499 | 0.464 | Hard | 22,309 |
https://leetcode.com/problems/max-value-of-equation/discuss/1746172/Python-3-monotonic-deque-O(n)-time-O(n)-space | class Solution:
def findMaxValueOfEquation(self, points: List[List[int]], k: int) -> int:
deque = collections.deque()
res = -math.inf
for point in points:
x, y = point[0], point[1]
while deque and x - deque[0][0] > k:
deque.popleft()
if deque:
res = max(res, (y + deque[0][1]) + (x - deque[0][0]))
# while deque AND any equation including (x, y) will always yield a greater
# (or ==) result than any equation including (deque[-1][0], deque[-1][1])
while deque and (deque[-1][0] - x) + (y - deque[-1][1]) >= 0:
deque.pop()
deque.append(point)
return res | max-value-of-equation | Python 3, monotonic deque, O(n) time, O(n) space | dereky4 | 0 | 156 | max value of equation | 1,499 | 0.464 | Hard | 22,310 |
https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/discuss/720103/Python3-2-line-(sorting) | class Solution:
def canMakeArithmeticProgression(self, arr: List[int]) -> bool:
arr.sort()
return len(set(arr[i-1] - arr[i] for i in range(1, len(arr)))) == 1 | can-make-arithmetic-progression-from-sequence | [Python3] 2-line (sorting) | ye15 | 17 | 1,100 | can make arithmetic progression from sequence | 1,502 | 0.681 | Easy | 22,311 |
https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/discuss/720429/Python3-two-lines-Can-Make-Arithmetic-Progression-From-Sequence | class Solution:
def canMakeArithmeticProgression(self, arr: List[int]) -> bool:
arr.sort()
return len(set(a - b for a, b in zip(arr, arr[1:]))) == 1 | can-make-arithmetic-progression-from-sequence | Python3 two lines - Can Make Arithmetic Progression From Sequence | r0bertz | 7 | 538 | can make arithmetic progression from sequence | 1,502 | 0.681 | Easy | 22,312 |
https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/discuss/1065142/Python.-faster-than-99.40.-cool-solution | class Solution:
def canMakeArithmeticProgression(self, arr: List[int]) -> bool:
arr = sorted(arr)
for i in range(len(arr) - 2):
if arr[i + 2] - arr[i + 1] != arr[i + 1] - arr[i]:
return False
return True | can-make-arithmetic-progression-from-sequence | Python. faster than 99.40%. cool solution | m-d-f | 4 | 352 | can make arithmetic progression from sequence | 1,502 | 0.681 | Easy | 22,313 |
https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/discuss/2304749/96-Faster-Python-Solution-or-Easy-or-O(n)-solution | class Solution:
def canMakeArithmeticProgression(self, arr: List[int]) -> bool:
arr.sort()
i = 0
j = 1
m = arr[j] - arr[i]
while(j < len(arr)-1):
if(arr[j+1]-arr[i+1] != m):
return False
i += 1
j += 1
return True | can-make-arithmetic-progression-from-sequence | 96% Faster Python Solution | Easy | O(n) solution | AngelaInfantaJerome | 3 | 118 | can make arithmetic progression from sequence | 1,502 | 0.681 | Easy | 22,314 |
https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/discuss/721251/Python-Simple-Solution-with-O(log-n-*-n)-Time-Complexity | class Solution:
def canMakeArithmeticProgression(self, arr: List[int]) -> bool:
# O(log n * n)
arr.sort()
dif = arr[1] - arr[0]
for i in range(len(arr) - 1):
if arr[i+1] - arr[i] != dif:
return False
return True | can-make-arithmetic-progression-from-sequence | Python Simple Solution with O(log n * n) Time Complexity | parkershamblin | 3 | 228 | can make arithmetic progression from sequence | 1,502 | 0.681 | Easy | 22,315 |
https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/discuss/2224879/Python3-solution-faster-than-94 | class Solution:
def canMakeArithmeticProgression(self, arr: List[int]) -> bool:
arr.sort()
d = arr[1]-arr[0]
for i in range(2,len(arr)):
if arr[i]-arr[i-1]!=d:
return False
return True | can-make-arithmetic-progression-from-sequence | 📌 Python3 solution faster than 94% | Dark_wolf_jss | 2 | 31 | can make arithmetic progression from sequence | 1,502 | 0.681 | Easy | 22,316 |
https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/discuss/1962342/easy-python-code | class Solution:
def canMakeArithmeticProgression(self, arr: List[int]) -> bool:
arr.sort()
diff = None
for i in range(len(arr)-1):
if diff == None:
diff = arr[i]-arr[i+1]
elif arr[i]-arr[i+1] != diff:
return False
return True | can-make-arithmetic-progression-from-sequence | easy python code | dakash682 | 2 | 75 | can make arithmetic progression from sequence | 1,502 | 0.681 | Easy | 22,317 |
https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/discuss/1988646/Python-time-O(nlogn)-space-O(1)-or-faster-than-93 | class Solution:
def canMakeArithmeticProgression(self, arr: List[int]) -> bool:
arr.sort()
diff = abs(arr[0] - arr[1])
right = len(arr) - 1
left = 0
while left <= right:
if abs(arr[left] - arr[left+1]) != diff:
return False
if abs(arr[right] - arr[right-1]) != diff:
return False
left += 1
right -= 1
return True | can-make-arithmetic-progression-from-sequence | [Python] time O(nlogn), space O(1) | faster than 93% | jamil117 | 1 | 68 | can make arithmetic progression from sequence | 1,502 | 0.681 | Easy | 22,318 |
https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/discuss/833541/Runtime%3A-44-ms-Memory-Usage%3A-13.8-MB-less-than-92.61 | class Solution:
def canMakeArithmeticProgression(self, arr: List[int]) -> bool:
arr.sort()
for i in range(1,len(arr)-1):
if arr[i]-arr[i-1]==arr[i+1]-arr[i]:
continue
else:
return False
return True | can-make-arithmetic-progression-from-sequence | Runtime: 44 ms, Memory Usage: 13.8 MB, less than 92.61% | shadowx01 | 1 | 88 | can make arithmetic progression from sequence | 1,502 | 0.681 | Easy | 22,319 |
https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/discuss/2829064/Runtime%3A-89-ms-faster-than-24.49-of-Python3-or-Arithmetic-Progression-or-BruteForce | class Solution:
def canMakeArithmeticProgression(self, arr: List[int]) -> bool:
arr.sort()
diff_a = arr[1] - arr[0]
for i in range(1,len(arr)):
diff = (arr[i] - arr[i-1])
if diff_a != diff:
return False
return True | can-make-arithmetic-progression-from-sequence | Runtime: 89 ms, faster than 24.49% of Python3 | Arithmetic Progression | BruteForce | liontech_123 | 0 | 1 | can make arithmetic progression from sequence | 1,502 | 0.681 | Easy | 22,320 |
https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/discuss/2820756/Simple-Python-solution | class Solution:
def canMakeArithmeticProgression(self, arr: List[int]) -> bool:
arr.sort()
diff_a = arr[1] - arr[0]
for i in range(1,len(arr)):
diff = (arr[i] - arr[i-1])
if diff_a != diff:
return False
return True | can-make-arithmetic-progression-from-sequence | Simple Python solution | aruj900 | 0 | 3 | can make arithmetic progression from sequence | 1,502 | 0.681 | Easy | 22,321 |
https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/discuss/2813751/Simple-Python-Solution | class Solution:
def canMakeArithmeticProgression(self, arr: List[int]) -> bool:
arr.sort()
diff = abs(arr[0] - arr[1])
for i in range(1,len(arr)):
if abs(arr[i] - arr[i-1]) != diff:
return False
return True | can-make-arithmetic-progression-from-sequence | Simple Python Solution | danishs | 0 | 3 | can make arithmetic progression from sequence | 1,502 | 0.681 | Easy | 22,322 |
https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/discuss/2804499/O(n)-time-and-O(1)-space-solution-using-math | class Solution:
def canMakeArithmeticProgression(self, arr: List[int]) -> bool:
maxi, mini = -1000000, 1000000
n=len(arr)
for i in arr:
if i > maxi:
maxi = i
if i < mini:
mini=i
r=(maxi-mini)/(n-1)
if r!=int(r):
return False
arr=set(arr)
r=int(r)
if r==0:
return True
if len(arr)!=n:
return False
for i in arr:
if (i-mini)%r!=0:
return False
return True | can-make-arithmetic-progression-from-sequence | O(n) time and O(1) space solution using math | thunder34 | 0 | 2 | can make arithmetic progression from sequence | 1,502 | 0.681 | Easy | 22,323 |
https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/discuss/2759322/Simple-Python3-Solution | class Solution:
def canMakeArithmeticProgression(self, arr: List[int]) -> bool:
arr.sort()
prev = None
for i in range(len(arr) - 1):
cnt = abs(arr[i] - arr[i+1])
if i == 0:
prev = cnt
if prev != cnt:
return False
prev = cnt
return True | can-make-arithmetic-progression-from-sequence | Simple Python3 Solution | vivekrajyaguru | 0 | 2 | can make arithmetic progression from sequence | 1,502 | 0.681 | Easy | 22,324 |
https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/discuss/2732622/Python3 | class Solution:
def canMakeArithmeticProgression(self, arr: List[int]) -> bool:
arr.sort()
res = set()
for i in range(1,len(arr)):
r = arr[i] - arr[i-1]
res.add(r)
return len(res)==1 | can-make-arithmetic-progression-from-sequence | Python3 | chakalivinith | 0 | 3 | can make arithmetic progression from sequence | 1,502 | 0.681 | Easy | 22,325 |
https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/discuss/2730692/Simple-Python-solution-with-comments! | class Solution:
def canMakeArithmeticProgression(self, arr: List[int]) -> bool:
# sort array - can't be arithmetic progression if it's not constantly increasing or decreasing
arr.sort()
# store difference between first two components
first2 = arr[1] - arr[0]
# only one direction matters - opposite wouldn't make a different difference (pun totally intended)
# skip first two, difference already done there
for x in range(2,len(arr)):
# check difference between previous values
if first2 == (arr[x]-arr[x-1]):
continue
# if any are inequal, break loop by returning False
else:
return False
# only return True when all conditions are pased
return True | can-make-arithmetic-progression-from-sequence | Simple Python solution with comments! | gbiagini | 0 | 3 | can make arithmetic progression from sequence | 1,502 | 0.681 | Easy | 22,326 |
https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/discuss/2723142/python-solution | class Solution:
def canMakeArithmeticProgression(self, arr: List[int]) -> bool:
arr.sort()
# minVal=min(arr)
# maxVal=max(arr)
# avg=(minVal+maxVal)/2
# actAVG=sum(arr)/len(arr)
# return avg==actAVG
diff=arr[1]-arr[0]
for i in range(len(arr)-1):
curdif=arr[i+1]-arr[i]
if diff!=curdif:
return False
return True
# differ = [arr[i+1]-arr[i] for i in range(len(arr)-1)]
# return all((differ[0]==val for val in differ)) | can-make-arithmetic-progression-from-sequence | python solution | Anil_8940 | 0 | 4 | can make arithmetic progression from sequence | 1,502 | 0.681 | Easy | 22,327 |
https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/discuss/2711231/Easy-Solution-for-Beginners-or-python3 | class Solution:
def canMakeArithmeticProgression(self, arr: List[int]) -> bool:
arr.sort()
n=len(arr)
for i in range(1,n-1):
if arr[i]-arr[i-1]!=arr[i+1]-arr[i]:
return False
return True | can-make-arithmetic-progression-from-sequence | Easy Solution for Beginners | python3 | 19121A04N7 | 0 | 1 | can make arithmetic progression from sequence | 1,502 | 0.681 | Easy | 22,328 |
https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/discuss/2705481/Python-Straight-Forward-Solution | class Solution:
def canMakeArithmeticProgression(self, arr: List[int]) -> bool:
arr.sort()
common_diff = arr[1] - arr[0]
for i in range(1, len(arr)):
if arr[i] - arr[i-1] != common_diff: return False
return True | can-make-arithmetic-progression-from-sequence | Python Straight Forward Solution | anandanshul001 | 0 | 6 | can make arithmetic progression from sequence | 1,502 | 0.681 | Easy | 22,329 |
https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/discuss/2696580/O(N)-solution | class Solution:
def canMakeArithmeticProgression(self, arr: List[int]) -> bool:
hashmap = defaultdict(int)
min_val, max_val = inf, -inf
for n in arr:
if len(hashmap) > 1 and hashmap[n] > 0:
return False
hashmap[n] += 1
min_val = min(min_val, n)
max_val = max(max_val, n)
if max_val == min_val: return True
d = (max_val - min_val) / (len(arr) - 1)
for n in arr:
if hashmap[n] > 1:
return False
if (n - min_val) % d:
return False
return True | can-make-arithmetic-progression-from-sequence | O(N) solution | michaelniki | 0 | 3 | can make arithmetic progression from sequence | 1,502 | 0.681 | Easy | 22,330 |
https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/discuss/2683056/Pyhton3-or-Easy | class Solution:
def canMakeArithmeticProgression(self, arr: List[int]) -> bool:
arr.sort() # sort the list
dif = arr[1] - arr[0] # difference
for i in range(len(arr) - 1):
if arr[i + 1] - arr[i] != dif:
return False
return True | can-make-arithmetic-progression-from-sequence | Pyhton3 | Easy | AnzheYuan1217 | 0 | 20 | can make arithmetic progression from sequence | 1,502 | 0.681 | Easy | 22,331 |
https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/discuss/2679165/simple-python | class Solution:
def canMakeArithmeticProgression(self, arr: List[int]) -> bool:
arr.sort()
return len(set(arr[i-1] - arr[i] for i in range(1, len(arr)))) == 1 | can-make-arithmetic-progression-from-sequence | simple python | gnani_dw | 0 | 2 | can make arithmetic progression from sequence | 1,502 | 0.681 | Easy | 22,332 |
https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/discuss/2667816/Python-or-Sorting-solution-or-O(nlog(n)) | class Solution:
def canMakeArithmeticProgression(self, arr: List[int]) -> bool:
arr.sort()
d = arr[1] - arr[0]
for i in range(1, len(arr) - 1):
if arr[i + 1] - arr[i] != d:
return False
return True | can-make-arithmetic-progression-from-sequence | Python | Sorting solution | O(nlog(n)) | LordVader1 | 0 | 6 | can make arithmetic progression from sequence | 1,502 | 0.681 | Easy | 22,333 |
https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/discuss/2657332/Easy-Python-Solution | class Solution:
def canMakeArithmeticProgression(self, arr: List[int]) -> bool:
arr = sorted(arr)
diff = arr[1] - arr[0]
for i in range(1, len(arr)):
if (arr[i] - arr[i-1])!=diff:return False
return True | can-make-arithmetic-progression-from-sequence | 🐍 Easy Python Solution ✔⭐ | kanvi26 | 0 | 1 | can make arithmetic progression from sequence | 1,502 | 0.681 | Easy | 22,334 |
https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/discuss/2641384/Python-Code-with-three-lines | class Solution:
def canMakeArithmeticProgression(self, arr: List[int]) -> bool:
arr.sort()
differ = [arr[i+1]-arr[i] for i in range(len(arr)-1)]
return all((differ[0]==val for val in differ)) | can-make-arithmetic-progression-from-sequence | Python Code with three lines✔✔ | Anil_8940 | 0 | 3 | can make arithmetic progression from sequence | 1,502 | 0.681 | Easy | 22,335 |
https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/discuss/2577874/Python-easy-solution-using-set-faster-than-98.36! | class Solution:
def canMakeArithmeticProgression(self, arr: List[int]) -> bool:
arr.sort()
set_diffs = set()
for i in range(len(arr) - 1):
diff = arr[i + 1] - arr[i]
set_diffs.add(diff)
if len(set_diffs) == 1:
return True
return False | can-make-arithmetic-progression-from-sequence | Python easy solution using set, faster than 98.36%! | samanehghafouri | 0 | 18 | can make arithmetic progression from sequence | 1,502 | 0.681 | Easy | 22,336 |
https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/discuss/2492610/Python-Sort-%2B-For-loop-solution-O(n)-time-O(1)-space | class Solution:
def canMakeArithmeticProgression(self, arr: List[int]) -> bool:
arr.sort()
for i in range(len(arr) - 2):
if arr[i] - arr[i+1] != arr[i+1] - arr[i+2]:
return False
return True | can-make-arithmetic-progression-from-sequence | [Python] Sort + For loop solution, O(n) time O(1) space | duynl | 0 | 19 | can make arithmetic progression from sequence | 1,502 | 0.681 | Easy | 22,337 |
https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/discuss/2298102/Python3-or-Solution | class Solution:
def canMakeArithmeticProgression(self, arr: List[int]) -> bool:
diff = []
arr.sort()
for i in range(0,len(arr)-1):
diff.append(abs(arr[i] - arr[i+1]))
return (len(set(diff)) <= 1) | can-make-arithmetic-progression-from-sequence | Python3 | Solution | arvindchoudhary33 | 0 | 11 | can make arithmetic progression from sequence | 1,502 | 0.681 | Easy | 22,338 |
https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/discuss/2247908/Python-easy-to-read-and-understand-or-sorting | class Solution:
def canMakeArithmeticProgression(self, arr: List[int]) -> bool:
arr.sort()
cnt = arr[1]-arr[0]
prev = arr[1]
for val in arr[2:]:
if val-prev != cnt:
return False
prev = val
return True | can-make-arithmetic-progression-from-sequence | Python easy to read and understand | sorting | sanial2001 | 0 | 31 | can make arithmetic progression from sequence | 1,502 | 0.681 | Easy | 22,339 |
https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/discuss/2177080/Python-Simple-Python-Solution-Using-Sorting | class Solution:
def canMakeArithmeticProgression(self, arr: List[int]) -> bool:
arr = sorted(arr)
diffrence = arr[1] - arr[0]
for i in range(1,len(arr)-1):
if arr[i+1] - arr[i] != diffrence:
return False
return True | can-make-arithmetic-progression-from-sequence | [ Python ] ✅✅ Simple Python Solution Using Sorting 🥳✌👍 | ASHOK_KUMAR_MEGHVANSHI | 0 | 53 | can make arithmetic progression from sequence | 1,502 | 0.681 | Easy | 22,340 |
https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/discuss/2130337/Can-Make-Arithmetic-Progression-From-Sequence | class Solution:
def canMakeArithmeticProgression(self, arr: List[int]) -> bool:
arr.sort()
c=arr[1]-arr[0]
for i in range(1,len(arr)):
if arr[i]-arr[i-1]==c:
continue
else:
return False
return True | can-make-arithmetic-progression-from-sequence | Can Make Arithmetic Progression From Sequence | Sai_Prasoona_Moolpuru | 0 | 25 | can make arithmetic progression from sequence | 1,502 | 0.681 | Easy | 22,341 |
https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/discuss/2095931/PYTHON-or-Easy-To-Understand-Python-Solution | class Solution:
def canMakeArithmeticProgression(self, arr: List[int]) -> bool:
arr = sorted(arr)
diff = abs(arr[0] - arr[1])
for i in range(len(arr) - 1):
if abs(arr[i] - arr[i+1]) != diff:
return False
return True | can-make-arithmetic-progression-from-sequence | PYTHON | Easy To Understand Python Solution | shreeruparel | 0 | 44 | can make arithmetic progression from sequence | 1,502 | 0.681 | Easy | 22,342 |
https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/discuss/2081097/Python3-Using-Sort | class Solution:
def canMakeArithmeticProgression(self, arr: List[int]) -> bool:
# Handle Edge Cases
if len(arr) < 2:
return True
arr.sort()
diff = arr[1] - arr [0]
for i in range(1, len(arr)-1):
if arr[i+1] - arr [i] != diff:
return False
return True | can-make-arithmetic-progression-from-sequence | Python3 Using Sort | pe-mn | 0 | 24 | can make arithmetic progression from sequence | 1,502 | 0.681 | Easy | 22,343 |
https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/discuss/2073507/Easy-to-understand-python-solution-with-complexities | class Solution:
def canMakeArithmeticProgression(self, arr: List[int]) -> bool:
arr.sort() # time O(nlogn)
difference = arr[1] - arr[0]
for i in range (0,len(arr)-1): # time O(n)
if arr[i+1] - arr[i] != difference :
return False
return True
# time O(nlogn)
# space O (1) | can-make-arithmetic-progression-from-sequence | Easy to understand python solution with complexities | mananiac | 0 | 23 | can make arithmetic progression from sequence | 1,502 | 0.681 | Easy | 22,344 |
https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/discuss/2047885/python3-List-Comprehension | class Solution:
def canMakeArithmeticProgression(self, arr: List[int]) -> bool:
arr.sort()
if len(set(arr[i] - arr[i+1] for i in range(len(arr)-1))) == 1:
return True
else:
return False | can-make-arithmetic-progression-from-sequence | python3 List Comprehension | kedeman | 0 | 18 | can make arithmetic progression from sequence | 1,502 | 0.681 | Easy | 22,345 |
https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/discuss/1918317/Python-Clean-and-Simple-or-Sort-or-Divmod | class Solution:
def canMakeArithmeticProgression(self, arr):
arr.sort()
diff = arr[1]-arr[0]
for i in range(1,len(arr)-1):
if arr[i+1]-arr[i] != diff:
return False
return True | can-make-arithmetic-progression-from-sequence | Python - Clean and Simple | Sort | Divmod | domthedeveloper | 0 | 75 | can make arithmetic progression from sequence | 1,502 | 0.681 | Easy | 22,346 |
https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/discuss/1918317/Python-Clean-and-Simple-or-Sort-or-Divmod | class Solution:
def canMakeArithmeticProgression(self, arr):
return (lambda s:len(set(s[i+1]-s[i] for i in range(len(s)-1))) == 1)(sorted(arr)) | can-make-arithmetic-progression-from-sequence | Python - Clean and Simple | Sort | Divmod | domthedeveloper | 0 | 75 | can make arithmetic progression from sequence | 1,502 | 0.681 | Easy | 22,347 |
https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/discuss/1918317/Python-Clean-and-Simple-or-Sort-or-Divmod | class Solution:
def canMakeArithmeticProgression(self, a):
s, e = min(a), max(a)
diff, r = divmod(e-s, len(a)-1)
if r: return False
a = set(a)
return len(a) == 1 or set(range(s,e+1,diff)) == a | can-make-arithmetic-progression-from-sequence | Python - Clean and Simple | Sort | Divmod | domthedeveloper | 0 | 75 | can make arithmetic progression from sequence | 1,502 | 0.681 | Easy | 22,348 |
https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/discuss/1860385/faster-than-92.59-of-Python3 | class Solution:
def canMakeArithmeticProgression(self, arr: List[int]) -> bool:
arr.sort()
if len(arr)==2:
return 1
dif=abs(arr[1]-arr[0])
for i in range(1,len(arr)-1):
new=abs(arr[i+1]-arr[i])
if dif != new:
return 0
return 1 | can-make-arithmetic-progression-from-sequence | faster than 92.59% of Python3 | shane6123 | 0 | 64 | can make arithmetic progression from sequence | 1,502 | 0.681 | Easy | 22,349 |
https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/discuss/1851714/Python-beginners-solution | class Solution:
def canMakeArithmeticProgression(self, arr: List[int]) -> bool:
arr.sort()
diff = arr[1] - arr[0]
for i in range(1, len(arr) - 1):
if arr[i+1] - arr[i] != diff:
return False
return True | can-make-arithmetic-progression-from-sequence | Python beginners solution | alishak1999 | 0 | 40 | can make arithmetic progression from sequence | 1,502 | 0.681 | Easy | 22,350 |
https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/discuss/1816154/2-Lines-Python-Solution-oror-97-Faster-(36ms)-oror-Memory-less-than-60 | class Solution:
def canMakeArithmeticProgression(self, arr: List[int]) -> bool:
arr.sort()
return len(set([arr[i+1]-arr[i] for i in range(len(arr)-1)]))==1 | can-make-arithmetic-progression-from-sequence | 2-Lines Python Solution || 97% Faster (36ms) || Memory less than 60% | Taha-C | 0 | 65 | can make arithmetic progression from sequence | 1,502 | 0.681 | Easy | 22,351 |
https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/discuss/1756404/Python-dollarolution | class Solution:
def canMakeArithmeticProgression(self, arr: List[int]) -> bool:
arr = sorted(arr)
d = arr[0]- arr[1]
for i in range(1,len(arr)-1):
if d != arr[i]- arr[i+1]:
return False
return True | can-make-arithmetic-progression-from-sequence | Python $olution | AakRay | 0 | 59 | can make arithmetic progression from sequence | 1,502 | 0.681 | Easy | 22,352 |
https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/discuss/1723830/Simple-solution-using-(Python3-%2B-JavaScript) | class Solution:
def canMakeArithmeticProgression(self, arr: List[int]) -> bool:
arr.sort(reverse=True)
li = []
for i in range(len(arr) - 1):
diff = arr[i] - arr[i+1]
li.append(diff)
ano = []
it = li[0]
for item in li:
if item == it:
ano.append(True)
else:
ano.append(False)
return all(ano) | can-make-arithmetic-progression-from-sequence | Simple solution using - (Python3 + JavaScript) | shakilbabu | 0 | 28 | can make arithmetic progression from sequence | 1,502 | 0.681 | Easy | 22,353 |
https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/discuss/1344033/Easy-Python-Solution | class Solution:
def canMakeArithmeticProgression(self, arr: List[int]) -> bool:
arr.sort()
diff=arr[0]-arr[1]
for i in range(1,len(arr)-1):
if arr[i]-arr[i+1]!=diff:
return False
else:
return True | can-make-arithmetic-progression-from-sequence | Easy Python Solution | sangam92 | 0 | 44 | can make arithmetic progression from sequence | 1,502 | 0.681 | Easy | 22,354 |
https://leetcode.com/problems/last-moment-before-all-ants-fall-out-of-a-plank/discuss/720202/PythonPython3-Last-Moment-Before-All-Ants-Fall-Out-of-a-Plank | class Solution:
def getLastMoment(self, n: int, left: List[int], right: List[int]) -> int:
if left and not right:
return max(left)
if not left and right:
return n - min(right)
if not left and not right:
return 0
if left and right:
return max(max(left), n - min(right)) | last-moment-before-all-ants-fall-out-of-a-plank | [Python/Python3] Last Moment Before All Ants Fall Out of a Plank | newborncoder | 1 | 87 | last moment before all ants fall out of a plank | 1,503 | 0.553 | Medium | 22,355 |
https://leetcode.com/problems/last-moment-before-all-ants-fall-out-of-a-plank/discuss/720202/PythonPython3-Last-Moment-Before-All-Ants-Fall-Out-of-a-Plank | class Solution:
def getLastMoment(self, n: int, left: List[int], right: List[int]) -> int:
return max(max(left,default=0), n - min(right, default=n)) | last-moment-before-all-ants-fall-out-of-a-plank | [Python/Python3] Last Moment Before All Ants Fall Out of a Plank | newborncoder | 1 | 87 | last moment before all ants fall out of a plank | 1,503 | 0.553 | Medium | 22,356 |
https://leetcode.com/problems/last-moment-before-all-ants-fall-out-of-a-plank/discuss/1953035/Python-Simple-Mathematical-Solution | class Solution:
def getLastMoment(self, n: int, left: List[int], right: List[int]) -> int:
if n==0:return 0
if len(left)==0:return n-min(right)
if len(right)==0:return max(left)-0
if len(left) and len(right):
return max(n-min(right),max(left)-0) | last-moment-before-all-ants-fall-out-of-a-plank | Python Simple Mathematical Solution | Whitedevil07 | 0 | 39 | last moment before all ants fall out of a plank | 1,503 | 0.553 | Medium | 22,357 |
https://leetcode.com/problems/last-moment-before-all-ants-fall-out-of-a-plank/discuss/1397093/python3-(One-Line-Solution) | class Solution:
def getLastMoment(self, n: int, left: List[int], right: List[int]) -> int:
return max(n - min(right,default = n), max(left,default = 0)) | last-moment-before-all-ants-fall-out-of-a-plank | python3 (One-Line Solution) | terrencetang | 0 | 81 | last moment before all ants fall out of a plank | 1,503 | 0.553 | Medium | 22,358 |
https://leetcode.com/problems/last-moment-before-all-ants-fall-out-of-a-plank/discuss/720134/Easy-Python-solution-O(n) | class Solution:
def getLastMoment(self, n: int, left: List[int], right: List[int]) -> int:
left_max = max(left) if left else 0
right_max = n - min(right) if right else 0
return max(left_max, right_max) | last-moment-before-all-ants-fall-out-of-a-plank | Easy Python solution O(n) | coco987 | 0 | 49 | last moment before all ants fall out of a plank | 1,503 | 0.553 | Medium | 22,359 |
https://leetcode.com/problems/count-submatrices-with-all-ones/discuss/721999/Python3-O(MN)-histogram-model | class Solution:
def numSubmat(self, mat: List[List[int]]) -> int:
m, n = len(mat), len(mat[0])
#precipitate mat to histogram
for i in range(m):
for j in range(n):
if mat[i][j] and i > 0:
mat[i][j] += mat[i-1][j] #histogram
ans = 0
for i in range(m):
stack = [] #mono-stack of indices of non-decreasing height
cnt = 0
for j in range(n):
while stack and mat[i][stack[-1]] > mat[i][j]:
jj = stack.pop() #start
kk = stack[-1] if stack else -1 #end
cnt -= (mat[i][jj] - mat[i][j])*(jj - kk) #adjust to reflect lower height
cnt += mat[i][j] #count submatrices bottom-right at (i, j)
ans += cnt
stack.append(j)
return ans | count-submatrices-with-all-ones | [Python3] O(MN) histogram model | ye15 | 111 | 7,600 | count submatrices with all ones | 1,504 | 0.578 | Medium | 22,360 |
https://leetcode.com/problems/count-submatrices-with-all-ones/discuss/1975947/In-line-with-the-Hints-66-speed | class Solution:
def numSubmat(self, mat: List[List[int]]) -> int:
rows, cols = len(mat), len(mat[0])
for r in range(1, rows):
for c, (a, b) in enumerate(zip(mat[r], mat[r - 1])):
if a and b:
mat[r][c] += b
ans = 0
for row in mat:
for c in range(cols):
min_val = row[c]
for val in row[c:]:
if val == 0:
break
elif val < min_val:
min_val = val
ans += min_val
return ans | count-submatrices-with-all-ones | In line with the Hints, 66% speed | EvgenySH | 0 | 219 | count submatrices with all ones | 1,504 | 0.578 | Medium | 22,361 |
https://leetcode.com/problems/count-submatrices-with-all-ones/discuss/1951348/O(N3)-but-works!! | class Solution:
def numSubmat(self, mat: List[List[int]]) -> int:
num_cols = len(mat[0])
num_rows = len(mat)
vertical = [[0]*num_cols for _ in range(num_rows)]
for row in range(num_rows):
for col in range(num_cols):
if mat[row][col]:
if row > 0:
vertical[row][col] = vertical[row-1][col] + 1
else:
vertical[row][col] = 1
response = 0
for row in range(num_rows):
for col in range(num_cols):
before = response
if vertical[row][col]:
response += vertical[row][col]
for c in range(col+1, num_cols):
if vertical[row][c] == 0:
break
response += min(vertical[row][col:c+1])
return response | count-submatrices-with-all-ones | O(N^3) but works!! | amegahed | 0 | 114 | count submatrices with all ones | 1,504 | 0.578 | Medium | 22,362 |
https://leetcode.com/problems/count-submatrices-with-all-ones/discuss/1404410/Python3-O(n3)-solution | class Solution:
def numSubmat(self, mat: List[List[int]]) -> int:
n,m = len(mat), len(mat[0])
nums = []
for i in range(n):
num = []
for j in range(m-1,-1,-1):
if mat[i][j] == 0:
num = [0] + num
elif mat[i][j] == 1 and j == m-1:
num = [1] + num
elif mat[i][j] == 1 and j != m-1:
num = [1 + num[0]] + num
nums.append(num)
ans = 0
for i in range(n):
for j in range(m):
x = m + 1
for k in range(i,n):
x = min(x,nums[k][j])
ans += x
return ans | count-submatrices-with-all-ones | Python3 O(n^3) solution | EklavyaJoshi | -1 | 149 | count submatrices with all ones | 1,504 | 0.578 | Medium | 22,363 |
https://leetcode.com/problems/minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits/discuss/720123/Python3-brute-force | class Solution:
def minInteger(self, num: str, k: int) -> str:
n = len(num)
if k >= n*(n-1)//2: return "".join(sorted(num)) #special case
#find smallest elements within k swaps
#and swap it to current position
num = list(num)
for i in range(n):
if not k: break
#find minimum within k swaps
ii = i
for j in range(i+1, min(n, i+k+1)):
if num[ii] > num[j]: ii = j
#swap the min to current position
if ii != i:
k -= ii-i
for j in range(ii, i, -1):
num[j-1], num[j] = num[j], num[j-1]
return "".join(num) | minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | [Python3] brute force | ye15 | 2 | 374 | minimum possible integer after at most k adjacent swaps on digits | 1,505 | 0.383 | Hard | 22,364 |
https://leetcode.com/problems/reformat-date/discuss/1861804/Python-3-Easy-Dict.-Solution-wout-imports-(98) | class Solution:
def reformatDate(self, date: str) -> str:
s = date.split() # Divides the elements into 3 individual parts
monthDict = {'Jan': '01', 'Feb': '02',
'Mar': '03', 'Apr': '04',
'May': '05', 'Jun': '06',
'Jul': '07', 'Aug': '08',
'Sep': '09', 'Oct': '10',
'Nov': '11', 'Dec': '12'}
day = s[0][:-2] # Removes the last 2 elements of the day
month = s[1]
year = s[2]
if int(day) < 10: # Adds 0 to the front of day if day < 10
day = '0' + day
return ''.join(f'{year}-{monthDict[month]}-{day}') # Joins it all together. Month is used to draw out the corresponding number from the dict. | reformat-date | Python 3 - Easy Dict. Solution w/out imports (98%) | IvanTsukei | 12 | 768 | reformat date | 1,507 | 0.626 | Easy | 22,365 |
https://leetcode.com/problems/reformat-date/discuss/1918218/Python-solution-with-comments | class Solution:
def reformatDate(self, date: str) -> str:
months = {
'Jan' : '01',
'Feb' : '02',
'Mar' : '03',
'Apr' : '04',
'May' : '05',
'Jun' : '06',
'Jul' : '07',
'Aug' : '08',
'Sep' : '09',
'Oct' : '10',
'Nov' : '11',
'Dec' : '12',
}
day, month, year = date.split()
day = day[ : -2 ] # remove st or nd or rd or th
day = '0' + day if len( day ) == 1 else day # needs to be 2 digits
return year + '-' + months[ month ] + '-' + day | reformat-date | Python solution with comments | ssarpal | 2 | 180 | reformat date | 1,507 | 0.626 | Easy | 22,366 |
https://leetcode.com/problems/reformat-date/discuss/1808248/Python-Solution-94.00 | class Solution:
def reformatDate(self, date: str) -> str:
d, m, y = date.split(' ')
d = d[0]+d[1] if len(d) == 4 else '0'+d[0]
mBank = {
"Jan": "01",
"Feb": "02",
"Mar": "03",
"Apr": "04",
"May": "05",
"Jun": "06",
"Jul": "07",
"Aug": "08",
"Sep": "09",
"Oct": "10",
"Nov": "11",
"Dec": "12"
}
return y + '-' + mBank[m] + '-' + d
``` | reformat-date | Python Solution -- 94.00% | jwhunt19 | 2 | 85 | reformat date | 1,507 | 0.626 | Easy | 22,367 |
https://leetcode.com/problems/reformat-date/discuss/1788878/Simple-Python-solution-or-Constant-Time | class Solution:
def reformatDate(self, date: str) -> str:
date = date.split()
d = {"Jan":"01", "Feb":"02", "Mar":"03", "Apr":"04", "May":"05", "Jun":"06", "Jul":"07", "Aug":"08", "Sep":"09", "Oct":"10", "Nov":"11", "Dec":"12"}
if int(date[-3][:-2]) < 10:
date[-3] = "0"+date[-3]
return date[-1]+"-"+d[date[-2]]+"-"+date[-3][:-2] | reformat-date | Simple Python solution | Constant Time | Coding_Tan3 | 1 | 142 | reformat date | 1,507 | 0.626 | Easy | 22,368 |
https://leetcode.com/problems/reformat-date/discuss/1770508/Python3-Solution-with-using-map | class Solution:
def reformatDate(self, date: str) -> str:
months_dict = {"Jan": "01", "Feb":"02", "Mar":"03", "Apr":"04", "May":"05", "Jun":"06", "Jul":"07", "Aug":"08", "Sep":"09", "Oct":"10", "Nov":"11", "Dec":"12"}
parts = date.split(' ')
parts[1] = months_dict[parts[1]]
parts[0] = parts[0][:-2]
if len(parts[0]) == 1:
parts[0] = '0' + parts[0]
return '-'.join(reversed(parts)) | reformat-date | [Python3] Solution with using map | maosipov11 | 1 | 89 | reformat date | 1,507 | 0.626 | Easy | 22,369 |
https://leetcode.com/problems/reformat-date/discuss/1174668/python-formatted-string-solution | class Solution:
def reformatDate(self, date: str) -> str:
Months=["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
Input=date.split()
year=Input[2]
day=Input[0][:-2]
if len(day)<2:day="0"+day
month=str(Months.index(Input[1])+1)
if len(month)<2:month="0"+month
return f"{year}-{month}-{day}" | reformat-date | python formatted string solution ; | _jorjis | 1 | 186 | reformat date | 1,507 | 0.626 | Easy | 22,370 |
https://leetcode.com/problems/reformat-date/discuss/730717/Python3-3-line-(string-and-date-approaches) | class Solution:
def reformatDate(self, date: str) -> str:
d, m, y = date.split()
m = {"Jan":1, "Feb":2, "Mar":3, "Apr":4, "May":5, "Jun":6, "Jul":7, "Aug":8, "Sep":9, "Oct":10, "Nov":11, "Dec":12}[m]
return f"{y}-{m:>02}-{d[:-2]:>02}" | reformat-date | [Python3] 3-line (string & date approaches) | ye15 | 1 | 225 | reformat date | 1,507 | 0.626 | Easy | 22,371 |
https://leetcode.com/problems/reformat-date/discuss/730593/PythonPython3-Reformate-Date | class Solution:
def reformatDate(self, date: str) -> str:
month_dict = {
'Jan':'01', 'Feb':'02', 'Mar':'03', 'Apr':'04', 'May':'05', 'Jun':'06',
'Jul':'07', 'Aug':'08', 'Sep':'09', 'Oct':'10', 'Nov':'11', 'Dec':'12',
}
date_list = date.split()
#defining empty string
date_string = ''
#year
date_string = date_string + date_list[2] + '-'
#month
date_string = date_string + month_dict[date_list[1]] + '-'
#day
date_day = [x for x in date_list[0] if not x.isalpha()]
date_day_digits = ''.join(date_day)
if len(date_day_digits) == 1:
date_string = date_string+'0'+date_day_digits
else:
date_string = date_string+date_day_digits
return date_string
# date_String = date_string + date_list[] | reformat-date | [Python/Python3] Reformate Date | newborncoder | 1 | 211 | reformat date | 1,507 | 0.626 | Easy | 22,372 |
https://leetcode.com/problems/reformat-date/discuss/2828562/Simple-Python-Solution | class Solution:
def reformatDate(self, date: str) -> str:
date_sections = date.split(' ')
month = self.convertMonth(date_sections[1])
year = date_sections[2]
day = self.convertDay(date_sections[0])
return f'{year}-{month}-{day}'
def convertDay(self,day):
output = day[0:len(day)-2]
if len(output) == 1:
output = f'0{output}'
return output
def convertMonth(self,month):
month_dict = {
'Jan' : '01',
'Feb' : '02',
'Mar' : '03',
'Apr' : '04',
'May' : '05',
'Jun' : '06',
'Jul' : '07',
'Aug' : '08',
'Sep' : '09',
'Oct' : '10',
'Nov' : '11',
'Dec' : '12'
}
return month_dict[month] | reformat-date | Simple Python Solution | lornedot | 0 | 3 | reformat date | 1,507 | 0.626 | Easy | 22,373 |
https://leetcode.com/problems/reformat-date/discuss/2625971/One-dictionary-%2B-list-slicing-%2B-string-formatting | class Solution:
def reformatDate(self, date: str) -> str:
months = {"Jan": "01", "Feb": "02", "Mar": "03", "Apr": "04",
"May": "05", "Jun": "06", "Jul": "07", "Aug": "08",
"Sep": "09", "Oct": "10", "Nov": "11", "Dec": "12"}
if len(date) == 12:
return f'{date[8:12]}-{months[date[4:7]]}-0{date[0]}'
return f'{date[9:13]}-{months[date[5:8]]}-{date[0:2]}' | reformat-date | One dictionary + list slicing + string formatting | kcstar | 0 | 9 | reformat date | 1,507 | 0.626 | Easy | 22,374 |
https://leetcode.com/problems/reformat-date/discuss/2464888/String-functions-using-python | class Solution:
def reformatDate(self, date: str) -> str:
a=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]
b=["st","nd","rd","th"]
for i in b:
if(i in date):
date=date.replace(i,"")
date=date.split(" ")
date=date[::-1]
b=a.index(date[1])
if(b<9):
date[1]="0"+str(b+1)
else:
date[1]=str(b+1)
if(len(date[2])==1):
date[2]="0"+date[2]
date=" ".join(date)
date=date.replace(" ","-")
return date | reformat-date | String functions using python | Durgavamsi | 0 | 15 | reformat date | 1,507 | 0.626 | Easy | 22,375 |
https://leetcode.com/problems/reformat-date/discuss/2456054/Python-Clean-Solution | class Solution:
def reformatDate(self, s: str) -> str:
months = {
"Jan":'01', "Feb":'02', "Mar":'03', "Apr":'04', "May":'05', "Jun":'06',
"Jul":'07', "Aug":'08', "Sep":'09', "Oct":'10', "Nov":'11', "Dec":'12'
}
def get_date(my_day):
day = my_day[2]
removed_last_two_char = day[0:len(day)-2]
if int(removed_last_two_char) < 10:
return"0" + str(removed_last_two_char)
return str(removed_last_two_char)
def get_month(my_day):
return months[my_day[1]]
def get_year(my_day):
return my_day[0]
date = s.split(" ")[::-1]
year = get_year(date)
month = get_month(date)
day = get_date(date)
return f'{year}-{month}-{day}' | reformat-date | Python Clean Solution | temvvlena | 0 | 26 | reformat date | 1,507 | 0.626 | Easy | 22,376 |
https://leetcode.com/problems/reformat-date/discuss/2367668/Readable-python-solution | class Solution:
def reformatDate(self, date: str) -> str:
m_dict_={"Jan":"01", "Feb":"02", "Mar":"03", "Apr":"04", "May":"05", "Jun":"06", "Jul":"07", "Aug":"08", "Sep":"09", "Oct":"10", "Nov":"11", "Dec":"12"}
day=date[:-11]
if len(day)==1:
day="0"+day
return(date[-4:] + "-" + m_dict_[date[-8:-5]] + "-" + day) | reformat-date | Readable python solution | sunakshi132 | 0 | 71 | reformat date | 1,507 | 0.626 | Easy | 22,377 |
https://leetcode.com/problems/reformat-date/discuss/2147979/Simple-python-solution | class Solution:
def reformatDate(self, date: str) -> str:
months = {
"Jan": "01",
"Feb": "02",
"Mar": "03",
"Apr": "04",
"May": "05",
"Jun": "06",
"Jul": "07",
"Aug": "08",
"Sep": "09",
"Oct": "10",
"Nov": "11",
"Dec": "12",
}
date_parts = date.split(" ")
day_raw = date_parts[0]
day = "0" + day_raw[:1] if len(day_raw) == 3 else day_raw[:2]
return f"{date_parts[2]}-{months[date_parts[1]]}-{day}" | reformat-date | Simple python solution | arhitiron | 0 | 62 | reformat date | 1,507 | 0.626 | Easy | 22,378 |
https://leetcode.com/problems/reformat-date/discuss/2145356/python-3-oror-simple-solution | class Solution:
def reformatDate(self, date: str) -> str:
months = {"Jan": "01", "Feb": "02", "Mar": "03", "Apr": "04",
"May": "05", "Jun": "06", "Jul": "07", "Aug": "08",
"Sep": "09", "Oct": "10", "Nov": "11", "Dec": "12"}
year = date[-4:]
month = months[date[-8:-5]]
day = date[:2] if date[1].isdigit() else "0" + date[0]
return f"{year}-{month}-{day}" | reformat-date | python 3 || simple solution | dereky4 | 0 | 85 | reformat date | 1,507 | 0.626 | Easy | 22,379 |
https://leetcode.com/problems/reformat-date/discuss/2108003/Python3-code | class Solution:
def reformatDate(self, date: str) -> str:
MONTH_NUM = dict(zip(
["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
[i + 1 for i in range(12)]
))
day, month, year = date.split()
converted_day = f'{day[:-2]:0>2}'
converted_month = f'{MONTH_NUM[month]:0>2}'
converted_year = year
return f'{converted_year}-{converted_month}-{converted_day}' | reformat-date | Python3 code | yzhao156 | 0 | 81 | reformat date | 1,507 | 0.626 | Easy | 22,380 |
https://leetcode.com/problems/reformat-date/discuss/2047957/Python-solution | class Solution:
def reformatDate(self, date: str) -> str:
m = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
date = date.split()
d = date[-3][:-2] if int(date[-3][:-2]) >= 10 else '0'+date[-3][:-2]
m = m.index(date[-2])+1 if (m.index(date[-2])+1) >= 10 else '0'+str(m.index(date[-2])+1)
return f'{date[-1]}-{m}-{d}' | reformat-date | Python solution | StikS32 | 0 | 69 | reformat date | 1,507 | 0.626 | Easy | 22,381 |
https://leetcode.com/problems/reformat-date/discuss/1896230/Python-simple-solution-using-dictionaries | class Solution:
def reformatDate(self, date: str) -> str:
month = {"Jan":'01', "Feb":'02', "Mar":'03', "Apr":'04', "May":'05', "Jun":'06', "Jul":'07', "Aug":'08', "Sep":'09', "Oct":'10', "Nov":'11', "Dec":'12'}
date_split = date.split()
res = ""
res += date_split[2] + "-" + month[date_split[1]] + "-"
if len(date_split[0][:-2]) == 1:
res += "0" + date_split[0][:-2]
else:
res += date_split[0][:-2]
return res | reformat-date | Python simple solution using dictionaries | alishak1999 | 0 | 73 | reformat date | 1,507 | 0.626 | Easy | 22,382 |
https://leetcode.com/problems/reformat-date/discuss/1859702/Python-Simple-and-Elegant! | class Solution(object):
def reformatDate(self, date):
months = {"Jan": "01", "Feb": "02", "Mar": "03", "Apr": "04", "May": "05", "Jun": "06", "Jul": "07", "Aug": "08", "Sep": "09", "Oct": "10", "Nov": "11", "Dec": "12"}
d, m, y = date.split()
d, m, y = zfill(filter(str.isdigit, str(d)),2), months[m], y
return y+"-"+m+"-"+d | reformat-date | Python - Simple and Elegant! | domthedeveloper | 0 | 51 | reformat date | 1,507 | 0.626 | Easy | 22,383 |
https://leetcode.com/problems/reformat-date/discuss/1859702/Python-Simple-and-Elegant! | class Solution(object):
def reformatDate(self, date):
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
d, m, y = date.split()
d, m, y = zfill(filter(str.isdigit, str(d)),2), zfill(months.index(m)+1,2), y
return y+"-"+m+"-"+d | reformat-date | Python - Simple and Elegant! | domthedeveloper | 0 | 51 | reformat date | 1,507 | 0.626 | Easy | 22,384 |
https://leetcode.com/problems/reformat-date/discuss/1859594/Python3-95.38-or-String-Builder-or-Easy-Implementation | class Solution:
def reformatDate(self, date: str) -> str:
d, m, y = date.split(' ')
dict_m = {
'Jan': '01',
'Feb':'02',
'Mar':'03',
'Apr':'04',
'May':'05',
'Jun':'06',
'Jul':'07',
'Aug':'08',
'Sep':'09',
'Oct':'10',
'Nov':'11',
'Dec':'12'
}
sb_d = []
for cha in d:
if cha.isdigit():
sb_d.append(cha)
else:
break
d = ''.join(sb_d)
if len(d) != 2:
d = '0' + d
m = dict_m[m]
return '-'.join([y, m, d]) | reformat-date | Python3 95.38% | String Builder | Easy Implementation | doneowth | 0 | 31 | reformat date | 1,507 | 0.626 | Easy | 22,385 |
https://leetcode.com/problems/reformat-date/discuss/1837944/Short-and-simple-Python-solution | class Solution:
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
months_dict = {val: i for i, val in enumerate(months, start = 1)}
def reformatDate(self, date: str) -> str:
d, m, y = date.split(' ')
def day_to_int(day): return int(''.join(takewhile(str.isnumeric, d)))
return '{0}-{1:02d}-{2:02d}'.format(y, self.months_dict[m], day_to_int(d)) | reformat-date | Short and simple Python solution | Lrnx | 0 | 62 | reformat date | 1,507 | 0.626 | Easy | 22,386 |
https://leetcode.com/problems/reformat-date/discuss/1756434/Python-dollarolution | class Solution:
def reformatDate(self, date: str) -> str:
d = {"Jan":'01', "Feb":'02', "Mar":'03', "Apr":'04', "May":'05', "Jun":'06', "Jul":'07', "Aug":'08', "Sep":'09', "Oct":'10', "Nov":'11', "Dec":'12'}
x = len(date)
s = date[x-4:] + '-'
if x == 13:
s += d[date[5:8]] + '-' + date[0:2]
else:
s += d[date[4:7]] + '-' + '0' + date[0:1]
return s | reformat-date | Python $olution | AakRay | 0 | 115 | reformat date | 1,507 | 0.626 | Easy | 22,387 |
https://leetcode.com/problems/reformat-date/discuss/1736014/Python3-or-easy-solution | class Solution:
def reformatDate(self, date: str) -> str:
month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
date_sep = date.split()
return f"{date_sep[2]}-{str(month.index(date_sep[1])+1).zfill(2)}-{date_sep[0][:-2].zfill(2)}" | reformat-date | Python3 | easy solution | khalidhassan3011 | 0 | 174 | reformat date | 1,507 | 0.626 | Easy | 22,388 |
https://leetcode.com/problems/reformat-date/discuss/1544668/Dictionary-%2B-split%2B-rejoin | class Solution:
def reformatDate(self, date: str) -> str:
month = {'Jan':'01', 'Feb':'02', 'Mar':'03', 'Apr':'04', 'May':'05', 'Jun':'06', 'Jul':'07', 'Aug':'08', 'Sep':'09', 'Oct':'10', 'Nov':'11', 'Dec':'12'}
dlist = date.replace("st", "").replace("nd", "").replace("rd", "").replace("th", "").split()
dlist[1] = month.get(dlist[1])
dlist[0], dlist[2] = dlist[2], dlist[0]
if len(dlist[2]) < 2:
dlist[2] = "0"+dlist[2]
return "-".join(dlist) | reformat-date | Dictionary + split+ rejoin | chandler20708 | 0 | 87 | reformat date | 1,507 | 0.626 | Easy | 22,389 |
https://leetcode.com/problems/reformat-date/discuss/1137807/Python-2-liner-easy-solution-99-faster | class Solution:
def reformatDate(self, date: str) -> str:
l=["an", "eb", "ar", "pr", "ay", "un", "ul", "ug", "ep", "ct", "ov", "ec"]
return date[-4:]+'-'+str(l.index(date[-7:-5])+1).zfill(2)+'-'+date[:-11].zfill(2) | reformat-date | Python 2 liner easy solution 99% faster | Rajashekar_Booreddy | 0 | 393 | reformat date | 1,507 | 0.626 | Easy | 22,390 |
https://leetcode.com/problems/reformat-date/discuss/1131509/Python-simple-solution | class Solution:
def reformatDate(self, date: str) -> str:
dates = date.split()
year = dates[2]
Months = {"Jan":'01', "Feb":'02', "Mar":'03', "Apr":'04', "May":'05', "Jun":'06', "Jul":'07', "Aug":'08', "Sep":'09', "Oct":'10', "Nov":'11', "Dec":'12'}
month = Months.get(dates[1])
datess = dates[0].rstrip(dates[0][-1])
day = datess.rstrip(datess[-1])
if len(day) < 2:
day = '0' + day
return '{}-{}-{}'.format(year,month,day)
``` | reformat-date | Python simple solution | Sissi0409 | 0 | 311 | reformat date | 1,507 | 0.626 | Easy | 22,391 |
https://leetcode.com/problems/reformat-date/discuss/1104911/Python-3-Regex-solution | class Solution:
def reformatDate(self, date: str) -> str:
months_re='(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)'
months=[None,'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
match=re.match(r'(\d{1,2})[a-z]{2}\s+' + months_re +'\s+' + '(\d{4})',date)
if match:
day=str(match.group(1))
month=str(months.index(match.group(2)))
year=str(match.group(3))
if len(month)==1:
month='0'+month
if len(day)==1:
day='0'+day
return year+'-'+month+'-'+day | reformat-date | Python 3 Regex solution | Salman_Baqri | 0 | 95 | reformat date | 1,507 | 0.626 | Easy | 22,392 |
https://leetcode.com/problems/reformat-date/discuss/1093252/Python3-simple-solution | class Solution:
def reformatDate(self, date: str) -> str:
x = [0,"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
l = date.split()
day = l[0][:-2]
if int(day) < 10:
day = '0' + day
month = str(x.index(l[1]))
if int(month) < 10:
month = '0' + month
year = l[2]
return '-'.join([year, month, day]) | reformat-date | Python3 simple solution | EklavyaJoshi | 0 | 100 | reformat date | 1,507 | 0.626 | Easy | 22,393 |
https://leetcode.com/problems/reformat-date/discuss/1051743/Python-Readable-and-Easy-to-Understand-5-Lines | class Solution:
def reformatDate(self, date: str) -> str:
day,month,year = date.split(' ')
months = {"Jan":"01", "Feb":"02", "Mar":"03", "Apr":"04", "May":"05", "Jun":"06", "Jul":"07", "Aug":"08", "Sep":"09", "Oct":"10", "Nov":"11", "Dec":"12"}
dayDigits = "".join(char if char.isdigit() else "" for char in day)
dayDigits = dayDigits if len(dayDigits) > 1 else "0" + dayDigits
return year + "-" + months[month] + "-" + dayDigits | reformat-date | Python Readable & Easy to Understand 5 Lines | jkp5380 | 0 | 152 | reformat date | 1,507 | 0.626 | Easy | 22,394 |
https://leetcode.com/problems/reformat-date/discuss/969715/Python-4-lines | class Solution:
def reformatDate(self, date: str) -> str:
l=["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
d,m,y=date.split()
D=y + '-' + str(l.index(m)+1).zfill(2) + '-' + d[:-2].zfill(2)
return D | reformat-date | Python 4-lines | lokeshsenthilkumar | 0 | 346 | reformat date | 1,507 | 0.626 | Easy | 22,395 |
https://leetcode.com/problems/reformat-date/discuss/807779/Python3-Month-dictionary | class Solution:
def reformatDate(self, date: str) -> str:
months = {
"Jan":"01", "Feb": "02", "Mar":"03", "Apr":"04", "May":"05", "Jun":"06", "Jul":"07", "Aug":"08", "Sep":"09", "Oct":"10", "Nov":"11", "Dec":"12"}
splitted = date.split()
if len(splitted[0]) == 4:
return splitted[-1] + "-" + months[splitted[1]] + "-"+ splitted[0][0:2]
else:
return splitted[-1] + "-" + months[splitted[1]] + "-" + "0" + splitted[0][0] | reformat-date | Python3 - Month dictionary | spsufawi | 0 | 83 | reformat date | 1,507 | 0.626 | Easy | 22,396 |
https://leetcode.com/problems/reformat-date/discuss/1792400/2-Lines-Python-Solution-oror-50-Faster-oror-Memory-less-than-90 | class Solution:
def reformatDate(self, date: str) -> str:
month, date = {"Jan":"01", "Feb":"02", "Mar":"03", "Apr":"04", "May":"05", "Jun":"06", "Jul":"07", "Aug":"08", "Sep":"09", "Oct":"10", "Nov":"11", "Dec":"12"}, date.split()
return date[2] + '-' + month[date[1]] + '-' + ''.join(['0']+[d for d in date[0] if d.isdigit()])[-2:] | reformat-date | 2-Lines Python Solution || 50% Faster || Memory less than 90% | Taha-C | -1 | 80 | reformat date | 1,507 | 0.626 | Easy | 22,397 |
https://leetcode.com/problems/range-sum-of-sorted-subarray-sums/discuss/730973/Python3-priority-queue | class Solution:
def rangeSum(self, nums: List[int], n: int, left: int, right: int) -> int:
ans = []
for i in range(len(nums)):
prefix = 0
for ii in range(i, len(nums)):
prefix += nums[ii]
ans.append(prefix)
ans.sort()
return sum(ans[left-1:right]) % 1_000_000_007 | range-sum-of-sorted-subarray-sums | [Python3] priority queue | ye15 | 46 | 3,600 | range sum of sorted subarray sums | 1,508 | 0.593 | Medium | 22,398 |
https://leetcode.com/problems/range-sum-of-sorted-subarray-sums/discuss/730973/Python3-priority-queue | class Solution:
def rangeSum(self, nums: List[int], n: int, left: int, right: int) -> int:
h = [(x, i) for i, x in enumerate(nums)] #min-heap
heapify(h)
ans = 0
for k in range(1, right+1): #1-indexed
x, i = heappop(h)
if k >= left: ans += x
if i+1 < len(nums):
heappush(h, (x + nums[i+1], i+1))
return ans % 1_000_000_007 | range-sum-of-sorted-subarray-sums | [Python3] priority queue | ye15 | 46 | 3,600 | range sum of sorted subarray sums | 1,508 | 0.593 | Medium | 22,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.