post_href stringlengths 57 213 | python_solutions stringlengths 71 22.3k | slug stringlengths 3 77 | post_title stringlengths 1 100 | user stringlengths 3 29 | upvotes int64 -20 1.2k | views int64 0 60.9k | problem_title stringlengths 3 77 | number int64 1 2.48k | acceptance float64 0.14 0.91 | difficulty stringclasses 3 values | __index_level_0__ int64 0 34k |
|---|---|---|---|---|---|---|---|---|---|---|---|
https://leetcode.com/problems/minimum-cost-to-cut-a-stick/discuss/2808149/PYTHON-DFS%2BMEMO-EASY-TO-UNDERSTAND | class Solution:
def minCost(self, n: int, cuts: List[int]) -> int:
@cache
def dfs(start,end):
# Base case if start == end, no cuts can be made
if start == end:
return 0
# Keep track of lowest cut, start at infinity so we can do min(res, newResult)
res = float('inf')
# For every cut, check if it is within the range of your start and end.
# If it is within range, add length of current ruler to result then cut it and perform dfs on both sides of the cut
for cut in cuts:
if start < cut < end:
tryCut = end-start
startToCut = dfs(start,cut)
cutToEnd = dfs(cut,end)
newResult = tryCut+startToCut+cutToEnd
res = min(res,newResult)
# If no cut was possible, return 0 else return res
return res if res != float('inf') else 0
return dfs(0,n) | minimum-cost-to-cut-a-stick | PYTHON DFS+MEMO EASY TO UNDERSTAND | tupsr | 0 | 10 | minimum cost to cut a stick | 1,547 | 0.57 | Hard | 22,900 |
https://leetcode.com/problems/minimum-cost-to-cut-a-stick/discuss/2632973/One-line-DP-solution-in-Python | class Solution:
@cache
def dp(self, l, r):
return 0 if l == r - 1 else min(self.dp(l, i) + self.dp(i, r) for i in range(l+1, r)) + self.cuts[r] - self.cuts[l]
def minCost(self, n: int, cuts: List[int]) -> int:
self.cuts = sorted(cuts + [0, n])
return self.dp(0, len(self.cuts) - 1) | minimum-cost-to-cut-a-stick | One line DP solution in Python | metaphysicalist | 0 | 34 | minimum cost to cut a stick | 1,547 | 0.57 | Hard | 22,901 |
https://leetcode.com/problems/minimum-cost-to-cut-a-stick/discuss/1460207/PyPy3-Solution-using-recursion-with-memoization-w-comments | class Solution:
def minCost(self, n: int, cuts: List[int]) -> int:
# Function to make cut
def makeCut(start, end, t=dict()):
# Make key
key = (start, end)
# If key doesn't exist
if key not in t:
# Init
currMin = float("inf")
# For all cuts
for cut in cuts:
# If cuts exist between the start and end, it's a valid cut.
if start < cut < end:
# Get the current cost of the cut
cost = end - start
# Update currMin
currMin = min(currMin, cost + makeCut(start, cut, t) + makeCut(cut, end, t))
# Update the currenty key, if nothing is processed, the value is 0
t[key] = currMin if currMin != float("inf") else 0
# return the current key
return t[key]
return makeCut(0,n) | minimum-cost-to-cut-a-stick | [Py/Py3] Solution using recursion with memoization w/ comments | ssshukla26 | 0 | 321 | minimum cost to cut a stick | 1,547 | 0.57 | Hard | 22,902 |
https://leetcode.com/problems/three-consecutive-odds/discuss/794097/Python3-straight-forward-solution | class Solution:
def threeConsecutiveOdds(self, arr: List[int]) -> bool:
count = 0
for i in range(0, len(arr)):
if arr[i] %2 != 0:
count += 1
if count == 3:
return True
else:
count = 0
return False | three-consecutive-odds | Python3 straight forward solution | sjha2048 | 20 | 1,600 | three consecutive odds | 1,550 | 0.636 | Easy | 22,903 |
https://leetcode.com/problems/three-consecutive-odds/discuss/2329183/Simple-loop-python | class Solution:
def threeConsecutiveOdds(self, arr: List[int]) -> bool:
c=0
for i in arr:
if i%2==0:
c=0
else:
c+=1
if c==3:
return True
return False | three-consecutive-odds | Simple loop python | sunakshi132 | 2 | 78 | three consecutive odds | 1,550 | 0.636 | Easy | 22,904 |
https://leetcode.com/problems/three-consecutive-odds/discuss/1899511/Python-One-Liners-x4! | class Solution:
def threeConsecutiveOdds(self, a):
return any(True for i,x in enumerate(a) if i<len(a)-2 and a[i]%2==a[i+1]%2==a[i+2]%2==1) | three-consecutive-odds | Python - One-Liners x4! | domthedeveloper | 2 | 60 | three consecutive odds | 1,550 | 0.636 | Easy | 22,905 |
https://leetcode.com/problems/three-consecutive-odds/discuss/1899511/Python-One-Liners-x4! | class Solution:
def threeConsecutiveOdds(self, arr):
return "111" in "".join(map(lambda x:str(x%2), arr)) | three-consecutive-odds | Python - One-Liners x4! | domthedeveloper | 2 | 60 | three consecutive odds | 1,550 | 0.636 | Easy | 22,906 |
https://leetcode.com/problems/three-consecutive-odds/discuss/1899511/Python-One-Liners-x4! | class Solution:
def threeConsecutiveOdds(self, arr):
return 3 in accumulate(arr,lambda x,y:x+y%2 if y%2 else 0, initial=0) | three-consecutive-odds | Python - One-Liners x4! | domthedeveloper | 2 | 60 | three consecutive odds | 1,550 | 0.636 | Easy | 22,907 |
https://leetcode.com/problems/three-consecutive-odds/discuss/1899511/Python-One-Liners-x4! | class Solution:
def threeConsecutiveOdds(self, arr):
return max(sum(g) for k,g in groupby(map(lambda x:x%2,arr))) >= 3 | three-consecutive-odds | Python - One-Liners x4! | domthedeveloper | 2 | 60 | three consecutive odds | 1,550 | 0.636 | Easy | 22,908 |
https://leetcode.com/problems/three-consecutive-odds/discuss/1072922/Python-one-liner | class Solution:
def threeConsecutiveOdds(self, arr: List[int]) -> bool:
return str([i & 1 for i in arr]).find('1, 1, 1') > 0 | three-consecutive-odds | [Python] one liner | angelique_ | 2 | 135 | three consecutive odds | 1,550 | 0.636 | Easy | 22,909 |
https://leetcode.com/problems/three-consecutive-odds/discuss/1053233/Python-simple-one-liner | class Solution:
def threeConsecutiveOdds(self, arr: List[int]) -> bool:
return "111" in "".join(str(num % 2) for num in arr) | three-consecutive-odds | Python simple one-liner | heildever | 2 | 75 | three consecutive odds | 1,550 | 0.636 | Easy | 22,910 |
https://leetcode.com/problems/three-consecutive-odds/discuss/794201/Python3-self-explained | class Solution:
def threeConsecutiveOdds(self, arr: List[int]) -> bool:
cnt = 0
for x in arr:
cnt = cnt + 1 if x % 2 else 0
if cnt == 3: return True
return False | three-consecutive-odds | [Python3] self-explained | ye15 | 2 | 112 | three consecutive odds | 1,550 | 0.636 | Easy | 22,911 |
https://leetcode.com/problems/three-consecutive-odds/discuss/2094591/Using-list-comp | class Solution:
def threeConsecutiveOdds(self, arr: List[int]) -> bool:
for i in range(len(arr) - 2):
sub = [n for n in arr[i:i+3] if n % 2]
if len(sub) == 3:
return True
return False | three-consecutive-odds | Using list comp | andrewnerdimo | 1 | 39 | three consecutive odds | 1,550 | 0.636 | Easy | 22,912 |
https://leetcode.com/problems/three-consecutive-odds/discuss/1758993/Python-dollarolution | class Solution:
def threeConsecutiveOdds(self, arr: List[int]) -> bool:
count = 0
for i in range(len(arr)):
if arr[i] % 2 != 0:
count += 1
if count == 3:
return True
else:
count = 0
return False | three-consecutive-odds | Python $olution | AakRay | 1 | 49 | three consecutive odds | 1,550 | 0.636 | Easy | 22,913 |
https://leetcode.com/problems/three-consecutive-odds/discuss/1583362/Python-3-simple-solution | class Solution:
def threeConsecutiveOdds(self, arr: List[int]) -> bool:
odds = 0
for num in arr:
if num % 2:
if odds == 2:
return True
odds += 1
else:
odds = 0
return False | three-consecutive-odds | Python 3 simple solution | dereky4 | 1 | 67 | three consecutive odds | 1,550 | 0.636 | Easy | 22,914 |
https://leetcode.com/problems/three-consecutive-odds/discuss/1057697/Python3-simple-solution-with-two-approaches | class Solution:
def threeConsecutiveOdds(self, arr: List[int]) -> bool:
count = 0
for i in range(len(arr)):
if arr[i] % 2 != 0:
count += 1
else:
count = 0
if count == 3:
return True
return False | three-consecutive-odds | Python3 simple solution with two approaches | EklavyaJoshi | 1 | 47 | three consecutive odds | 1,550 | 0.636 | Easy | 22,915 |
https://leetcode.com/problems/three-consecutive-odds/discuss/1057697/Python3-simple-solution-with-two-approaches | class Solution:
def threeConsecutiveOdds(self, arr: List[int]) -> bool:
for i in range(len(arr)-2):
if arr[i] & 1 == 1 and arr[i+1] & 1 == 1 and arr[i+2] & 1 == 1:
return True
return False | three-consecutive-odds | Python3 simple solution with two approaches | EklavyaJoshi | 1 | 47 | three consecutive odds | 1,550 | 0.636 | Easy | 22,916 |
https://leetcode.com/problems/three-consecutive-odds/discuss/963301/Python-Simple-Solution | class Solution:
def threeConsecutiveOdds(self, arr: List[int]) -> bool:
for i in range(len(arr)-2):
if arr[i]%2 and arr[i+1]%2 and arr[i+2]%2:
return 1
return 0 | three-consecutive-odds | Python Simple Solution | lokeshsenthilkumar | 1 | 149 | three consecutive odds | 1,550 | 0.636 | Easy | 22,917 |
https://leetcode.com/problems/three-consecutive-odds/discuss/2841222/Simplest-one-pass-python-solution | class Solution:
def threeConsecutiveOdds(self, arr: List[int]) -> bool:
for i in range(len(arr)-2):
if arr[i] % 2 == 1 and arr[i+1] % 2 == 1 and arr[i+2] %2 == 1:
return True
return False | three-consecutive-odds | Simplest one pass python solution | aruj900 | 0 | 1 | three consecutive odds | 1,550 | 0.636 | Easy | 22,918 |
https://leetcode.com/problems/three-consecutive-odds/discuss/2755579/Simple-solution-oror-Beats-95 | class Solution:
def threeConsecutiveOdds(self, arr: List[int]) -> bool:
if len(arr)<3: return False
if len(arr) == 3:
if arr[0]%2!=0 and arr[1]%2!=0 and arr[2]%2!=0:
return True
for i in range(len(arr)-2):
if (arr[i]*arr[i+1] * arr[i+2])%2 != 0:
return True
return False | three-consecutive-odds | Simple solution || Beats 95% | MockingJay37 | 0 | 1 | three consecutive odds | 1,550 | 0.636 | Easy | 22,919 |
https://leetcode.com/problems/three-consecutive-odds/discuss/2732747/easy-approach | class Solution:
def threeConsecutiveOdds(self, arr: List[int]) -> bool:
count=0
for i in range(len(arr)):
if arr[i]%2 !=0:
count=count+1
else:
count=0
if count==3:
return True
else:
return False | three-consecutive-odds | easy approach | sindhu_300 | 0 | 5 | three consecutive odds | 1,550 | 0.636 | Easy | 22,920 |
https://leetcode.com/problems/three-consecutive-odds/discuss/2718481/Python3-simple-solution | class Solution:
def threeConsecutiveOdds(self, arr: List[int]) -> bool:
c = 0
for i in arr:
if i % 2 != 0:
c += 1
if c == 3:
return True
else:
c = 0
return False | three-consecutive-odds | Python3 simple solution | chakalivinith | 0 | 3 | three consecutive odds | 1,550 | 0.636 | Easy | 22,921 |
https://leetcode.com/problems/three-consecutive-odds/discuss/2626202/Nested-if-elses-and-bit-manipulation-ftw | class Solution:
def threeConsecutiveOdds(self, arr: List[int]) -> bool:
if len(arr) < 3:
return False
i = 0
while i + 2 < len(arr):
if arr[i] & 1:
if arr[i + 1] & 1:
if arr[i + 2] & 1:
return True
else:
i += 3
else:
i += 2
else:
i += 1
return False | three-consecutive-odds | Nested if-elses and bit manipulation ftw | kcstar | 0 | 2 | three consecutive odds | 1,550 | 0.636 | Easy | 22,922 |
https://leetcode.com/problems/three-consecutive-odds/discuss/2594931/Python-Cleanest-and-Simplest-Solution | class Solution:
def threeConsecutiveOdds(self, arr: List[int]) -> bool:
count = 0
for i in range(len(arr)):
if arr[i]%2==1:
count += 1
else: count = 0
if count == 3:
return True | three-consecutive-odds | Python Cleanest and Simplest Solution 💯 | YuviGill | 0 | 15 | three consecutive odds | 1,550 | 0.636 | Easy | 22,923 |
https://leetcode.com/problems/three-consecutive-odds/discuss/2217859/Python-solution-for-beginners-by-beginner. | class Solution:
def threeConsecutiveOdds(self, arr: List[int]) -> bool:
count = 0
for i in arr:
if i%2 == 1:
count +=1
else:
count = 0
if count == 3:
return True
return False | three-consecutive-odds | Python solution for beginners by beginner. | EbrahimMG | 0 | 18 | three consecutive odds | 1,550 | 0.636 | Easy | 22,924 |
https://leetcode.com/problems/three-consecutive-odds/discuss/2069058/Python-Straightforward | class Solution:
def threeConsecutiveOdds(self, arr: List[int]) -> bool:
odds = 0
for n in arr:
if n % 2 == 0:
odds = 0
elif n % 2 != 0:
odds += 1
if odds == 3:
return True
return False | three-consecutive-odds | Python Straightforward | GigaMoksh | 0 | 20 | three consecutive odds | 1,550 | 0.636 | Easy | 22,925 |
https://leetcode.com/problems/three-consecutive-odds/discuss/2047624/Python-simple-solution | class Solution:
def threeConsecutiveOdds(self, arr: List[int]) -> bool:
ans = []
for i in arr:
if len(ans) == 3:
return True
if i%2 == 0:
ans = []
else:
ans.append(i)
return len(ans) == 3 | three-consecutive-odds | Python simple solution | StikS32 | 0 | 30 | three consecutive odds | 1,550 | 0.636 | Easy | 22,926 |
https://leetcode.com/problems/three-consecutive-odds/discuss/1893703/Python-easy-solution-using-one-for-loop.-Faster-than-82 | class Solution:
def threeConsecutiveOdds(self, arr: List[int]) -> bool:
for i in range(len(arr)-2):
if arr[i] % 2 !=0 and arr[i+1] % 2 != 0 and arr[i+2] % 2 != 0:
return True
return False | three-consecutive-odds | Python easy solution using one for loop. Faster than 82% | alishak1999 | 0 | 41 | three consecutive odds | 1,550 | 0.636 | Easy | 22,927 |
https://leetcode.com/problems/three-consecutive-odds/discuss/1821445/2-Lines-Python-Solution-oror-80-Faster-oror-Memory-less-than-80 | class Solution:
def threeConsecutiveOdds(self, arr: List[int]) -> bool:
odd = [idx for idx,val in enumerate(arr) if val%2==1]
return '11' in ''.join([str(odd[i+1]-odd[i]) for i in range(len(odd)-1)]) | three-consecutive-odds | 2-Lines Python Solution || 80% Faster || Memory less than 80% | Taha-C | 0 | 44 | three consecutive odds | 1,550 | 0.636 | Easy | 22,928 |
https://leetcode.com/problems/three-consecutive-odds/discuss/1480292/One-pass-98-speed | class Solution:
def threeConsecutiveOdds(self, arr: List[int]) -> bool:
odds = 0
for n in arr:
if n % 2:
odds += 1
else:
odds = 0
if odds == 3:
return True
return False | three-consecutive-odds | One pass, 98% speed | EvgenySH | 0 | 52 | three consecutive odds | 1,550 | 0.636 | Easy | 22,929 |
https://leetcode.com/problems/three-consecutive-odds/discuss/1354950/Python3-Simplest-solution-36-ms-runtime-faster-than-99-submissions | class Solution:
def threeConsecutiveOdds(self, arr: list) -> bool:
possible = False
odds = []
for num in arr:
if num % 2 == 1:
odds.append(num)
else:
odds = []
if len(odds) == 3:
possible = True
break
return possible | three-consecutive-odds | [Python3] Simplest solution, 36 ms runtime, faster than 99% submissions | GauravKK08 | 0 | 33 | three consecutive odds | 1,550 | 0.636 | Easy | 22,930 |
https://leetcode.com/problems/three-consecutive-odds/discuss/1114025/Simple-python-solution(Fast-and-easy-to-understand) | class Solution:
def threeConsecutiveOdds(self, arr: List[int]) -> bool:
odd = 0
odds = []
for i in range(len(arr)-2):
if arr[i] % 2 == 1:
odd+=1
if arr[i+1] % 2 == 1:
odd+=1
if arr[i+2] % 2 == 1:
odd+=1
odds.append(odd)
odd = 0
if len(odds) > 0:
return max(odds) >= 3
else:
return False
``` | three-consecutive-odds | Simple python solution(Fast and easy to understand) | Sissi0409 | 0 | 69 | three consecutive odds | 1,550 | 0.636 | Easy | 22,931 |
https://leetcode.com/problems/three-consecutive-odds/discuss/806555/Intuitive-approach-by-checking-111 | class Solution:
def threeConsecutiveOdds(self, arr: List[int]) -> bool:
return '111' in ''.join(map(lambda e: str(e%2), arr)) | three-consecutive-odds | Intuitive approach by checking `111` | puremonkey2001 | 0 | 27 | three consecutive odds | 1,550 | 0.636 | Easy | 22,932 |
https://leetcode.com/problems/three-consecutive-odds/discuss/799512/python-easy | class Solution:
def threeConsecutiveOdds(self, arr: List[int]) -> bool:
for i in range(0,len(arr)-2):
if arr[i]%2!=0 and arr[i+1]%2!=0 and arr[i+2]%2!=0:
return True
return False | three-consecutive-odds | python easy | realname | 0 | 48 | three consecutive odds | 1,550 | 0.636 | Easy | 22,933 |
https://leetcode.com/problems/three-consecutive-odds/discuss/797120/Python3-(100-time-and-space-) | class Solution:
def threeConsecutiveOdds(self, arr: List[int]) -> bool:
count=0
for a in range(len(arr)):
if count==3:return True
if arr[a]&1: # To check if arr[a] is odd?
count+=1
else:
count=0 # Reset the count if a even term is found.
if count==3:return True
return False | three-consecutive-odds | Python3 (100% time and space ) | harshitCode13 | 0 | 43 | three consecutive odds | 1,550 | 0.636 | Easy | 22,934 |
https://leetcode.com/problems/three-consecutive-odds/discuss/795375/Solution-or-Python-or-O(n) | class Solution:
def threeConsecutiveOdds(self, arr: List[int]) -> bool:
# initialize the count variable for odd values
oddCount = 0
# iterate over the given array
for x in arr:
# check if the number is odd and increment the odd counter
if x % 2 != 0:
oddCount += 1
else:
# reset the counter if we get an even value
oddCount = 0
# check the odd counter value, if it is 3 return True
if oddCount == 3:
return True
# return False if there are no three consecutive odds
return False | three-consecutive-odds | Solution | Python | O(n) | rushirg | 0 | 71 | three consecutive odds | 1,550 | 0.636 | Easy | 22,935 |
https://leetcode.com/problems/three-consecutive-odds/discuss/794274/Python3-1-liner-Three-Consecutive-Odds | class Solution:
def threeConsecutiveOdds(self, arr: List[int]) -> bool:
return any(a % 2 == b % 2 == c % 2 == 1 for a, b, c in zip(arr, arr[1:], arr[2:])) | three-consecutive-odds | Python3 1 liner - Three Consecutive Odds | r0bertz | 0 | 44 | three consecutive odds | 1,550 | 0.636 | Easy | 22,936 |
https://leetcode.com/problems/minimum-operations-to-make-array-equal/discuss/1704407/Understandable-code-for-beginners-like-me-in-python-!! | class Solution:
def minOperations(self, n: int) -> int:
if(n%2!=0):
n=n//2
return n*(n+1)
else:
n=n//2
return n**2 | minimum-operations-to-make-array-equal | Understandable code for beginners like me in python !! | kabiland | 2 | 92 | minimum operations to make array equal | 1,551 | 0.811 | Medium | 22,937 |
https://leetcode.com/problems/minimum-operations-to-make-array-equal/discuss/963316/C%2B%2B-Java-Python-1-liners | class Solution:
def minOperations(self, n: int) -> int:
return (n*n)//4 | minimum-operations-to-make-array-equal | [C++ / Java / Python] 1-liners | lokeshsenthilkumar | 2 | 97 | minimum operations to make array equal | 1,551 | 0.811 | Medium | 22,938 |
https://leetcode.com/problems/minimum-operations-to-make-array-equal/discuss/2846613/Python-O(1)-solution-using-math | class Solution:
def minOperations(self, n: int) -> int:
return (n ** 2) // 4 | minimum-operations-to-make-array-equal | [Python] O(1) solution using math | Mark_computer | 1 | 8 | minimum operations to make array equal | 1,551 | 0.811 | Medium | 22,939 |
https://leetcode.com/problems/minimum-operations-to-make-array-equal/discuss/1952543/1-Line-Python-Solution-oror-90-Faster-oror-Memory-less-than-97 | class Solution:
def minOperations(self, n: int) -> int:
return n**2//4 | minimum-operations-to-make-array-equal | 1-Line Python Solution || 90% Faster || Memory less than 97% | Taha-C | 1 | 119 | minimum operations to make array equal | 1,551 | 0.811 | Medium | 22,940 |
https://leetcode.com/problems/minimum-operations-to-make-array-equal/discuss/794303/Python3-1-liner-Minimum-Operations-to-Make-Array-Equal | class Solution:
def minOperations(self, n: int) -> int:
return (n+1)*(n-1)//4 if n % 2 else n*n//4 | minimum-operations-to-make-array-equal | Python3 1 liner - Minimum Operations to Make Array Equal | r0bertz | 1 | 116 | minimum operations to make array equal | 1,551 | 0.811 | Medium | 22,941 |
https://leetcode.com/problems/minimum-operations-to-make-array-equal/discuss/794210/Python3-O(N)-or-O(1) | class Solution:
def minOperations(self, n: int) -> int:
return sum(abs(2*i+1 - n) for i in range(n))//2 | minimum-operations-to-make-array-equal | [Python3] O(N) or O(1) | ye15 | 1 | 71 | minimum operations to make array equal | 1,551 | 0.811 | Medium | 22,942 |
https://leetcode.com/problems/minimum-operations-to-make-array-equal/discuss/794210/Python3-O(N)-or-O(1) | class Solution:
def minOperations(self, n: int) -> int:
return (n+1)*(n-1)//4 if n%2 else n*n//4 | minimum-operations-to-make-array-equal | [Python3] O(N) or O(1) | ye15 | 1 | 71 | minimum operations to make array equal | 1,551 | 0.811 | Medium | 22,943 |
https://leetcode.com/problems/minimum-operations-to-make-array-equal/discuss/2804995/Simple-4-line-Python-Solution-no-complicated-logic | class Solution:
def minOperations(self, n: int) -> int:
vals = [i*2 +1 for i in range(n)]
mid = len(vals)//2
vals = [abs(vals[mid] - vals[i]) for i in range(len(vals))]
return sum(vals)//2 | minimum-operations-to-make-array-equal | Simple 4-line Python Solution no complicated logic | vijay_2022 | 0 | 1 | minimum operations to make array equal | 1,551 | 0.811 | Medium | 22,944 |
https://leetcode.com/problems/minimum-operations-to-make-array-equal/discuss/2767785/Python-1-line-code | class Solution:
def minOperations(self, n: int) -> int:
return (n//2)*((2*(n-1)+2)//2)-sum([2*x+1 for x in range(n//2)]) | minimum-operations-to-make-array-equal | Python 1 line code | kumar_anand05 | 0 | 3 | minimum operations to make array equal | 1,551 | 0.811 | Medium | 22,945 |
https://leetcode.com/problems/minimum-operations-to-make-array-equal/discuss/2758568/Python-Solution-dollardollarO(1)dollardollar | class Solution:
def minOperations(self, n: int) -> int:
if n%2==1:
return (n**2-1)//4
else:
return (n**2)//4 | minimum-operations-to-make-array-equal | Python Solution $$O(1)$$ | cleon082 | 0 | 3 | minimum operations to make array equal | 1,551 | 0.811 | Medium | 22,946 |
https://leetcode.com/problems/minimum-operations-to-make-array-equal/discuss/2753225/1-Liner | class Solution:
def minOperations(self, n: int) -> int:
return sum(range(not n&1, n, 2)) | minimum-operations-to-make-array-equal | 1-Liner | Mencibi | 0 | 5 | minimum operations to make array equal | 1,551 | 0.811 | Medium | 22,947 |
https://leetcode.com/problems/minimum-operations-to-make-array-equal/discuss/2747255/Python3-Simple-Solution | class Solution:
def minOperations(self, n: int) -> int:
ops = 0
for i in range(n // 2):
ops += n - (2 * i + 1)
return ops | minimum-operations-to-make-array-equal | Python3 Simple Solution | mediocre-coder | 0 | 3 | minimum operations to make array equal | 1,551 | 0.811 | Medium | 22,948 |
https://leetcode.com/problems/minimum-operations-to-make-array-equal/discuss/2741733/Python3-Solution-one-liner | class Solution:
def minOperations(self, n: int) -> int:
return sum([n-i for i in range(n) if i&1]) | minimum-operations-to-make-array-equal | Python3 Solution - one-liner | sipi09 | 0 | 2 | minimum operations to make array equal | 1,551 | 0.811 | Medium | 22,949 |
https://leetcode.com/problems/minimum-operations-to-make-array-equal/discuss/2730185/Easy-Python-3-solution | class Solution:
def minOperations(self, n: int) -> int:
res = 0
arr = [(2*i)+1 for i in range(n)]
for i in arr:
if i > n:
res += i-n
elif i < n:
res += n-i
return res//2 | minimum-operations-to-make-array-equal | Easy Python 3 solution | mr6nie | 0 | 7 | minimum operations to make array equal | 1,551 | 0.811 | Medium | 22,950 |
https://leetcode.com/problems/minimum-operations-to-make-array-equal/discuss/2577723/python-easy-solution | class Solution:
def minOperations(self, n: int) -> int:
res = 0
for i in range(1, n, 2):
res += n-i
return res | minimum-operations-to-make-array-equal | python easy solution | Jack_Chang | 0 | 31 | minimum operations to make array equal | 1,551 | 0.811 | Medium | 22,951 |
https://leetcode.com/problems/minimum-operations-to-make-array-equal/discuss/2539897/Python-or-Math-or-O(n)-Time-or-O(1)-Space | class Solution:
def minOperations(self, n: int) -> int:
ans = 0
x = 1
while x<n:
ans += (n-x)
x+=2
return ans | minimum-operations-to-make-array-equal | Python | Math | O(n) Time | O(1) Space | coolakash10 | 0 | 10 | minimum operations to make array equal | 1,551 | 0.811 | Medium | 22,952 |
https://leetcode.com/problems/minimum-operations-to-make-array-equal/discuss/2488537/Simple-python-code-with-explanation | class Solution:
def minOperations(self, n: int) -> int:
#create an empty list -->a
a = []
#iterate untill n
for i in range(n):
#add the element (2*i) + 1 in a
a.append((2*i) + 1)
#minimum number will be at 0th index
minimum = a[0]
#maximum number will be at last index
maximum = a[-1]
#find avg val of min and max to get middle val
mid = (minimum + maximum)//2
#create a variable and initialise it to zero
ans = 0
#iterate untill middle index
for i in range(n//2):
#add the difference between the middle value and value at ith index to ans
ans = ans + mid - (a[i])
#return the ans
return ans | minimum-operations-to-make-array-equal | Simple python code with explanation | thomanani | 0 | 32 | minimum operations to make array equal | 1,551 | 0.811 | Medium | 22,953 |
https://leetcode.com/problems/minimum-operations-to-make-array-equal/discuss/2285021/O(1)-95-faster | class Solution:
def minOperations(self, n: int) -> int:
count = 0
mid = n//2
mid_e = 2*mid+1
for i in range(1,2*n,2):
count+= abs(i-mid_e)
return count//2 | minimum-operations-to-make-array-equal | O(1) 95% faster | Abhi_009 | 0 | 37 | minimum operations to make array equal | 1,551 | 0.811 | Medium | 22,954 |
https://leetcode.com/problems/minimum-operations-to-make-array-equal/discuss/2218298/2-Python-self-understandable-solutions-Slight-change-to-%22Leetcode-462%22-i.e.-Minimum-moves-problem | class Solution:
def minOperations(self, n: int) -> int:
# Method 1 : Median
nums = []
for i in range(n):
nums.append(2*i + 1)
median_ish = nums[len(nums)//2]
count = 0
for i in nums:
count += abs(i - median_ish)
return count//2
# we divided it by 2 because increment and decrement operation can be done
# at the same time | minimum-operations-to-make-array-equal | 2 Python self-understandable solutions [ Slight change to "Leetcode 462" i.e. Minimum moves problem] | maydev22 | 0 | 34 | minimum operations to make array equal | 1,551 | 0.811 | Medium | 22,955 |
https://leetcode.com/problems/minimum-operations-to-make-array-equal/discuss/2218298/2-Python-self-understandable-solutions-Slight-change-to-%22Leetcode-462%22-i.e.-Minimum-moves-problem | class Solution:
def minOperations(self, n: int) -> int:
# Method 2 : Using concept of median i.e. minimize the number of moves
nums = []
for i in range(n):
nums.append(2*i + 1)
start, end = 0, len(nums)-1
count = 0
while(start < end):
count += abs(nums[end] - nums[start])
start += 1
end -= 1
return count//2
# we divided it by 2 because increment and decrement operation can be done
# at the same time | minimum-operations-to-make-array-equal | 2 Python self-understandable solutions [ Slight change to "Leetcode 462" i.e. Minimum moves problem] | maydev22 | 0 | 34 | minimum operations to make array equal | 1,551 | 0.811 | Medium | 22,956 |
https://leetcode.com/problems/minimum-operations-to-make-array-equal/discuss/2164534/python-3-or-simple-O(1)-solution | class Solution:
def minOperations(self, n: int) -> int:
q, r = divmod(n, 2)
return q * (q + r) | minimum-operations-to-make-array-equal | python 3 | simple O(1) solution | dereky4 | 0 | 39 | minimum operations to make array equal | 1,551 | 0.811 | Medium | 22,957 |
https://leetcode.com/problems/minimum-operations-to-make-array-equal/discuss/1724200/Easy-Python-solution | class Solution:
def minOperations(self, n: int) -> int:
num_op = 0
for i in range(n//2):
num_op = num_op + n-(2*i+1)
return num_op | minimum-operations-to-make-array-equal | Easy Python solution | ankansharma1998 | 0 | 112 | minimum operations to make array equal | 1,551 | 0.811 | Medium | 22,958 |
https://leetcode.com/problems/minimum-operations-to-make-array-equal/discuss/1145969/Most-Simple-Solution-Ever | class Solution:
def minOperations(self, n: int) -> int:
return (n * n) >> 2 | minimum-operations-to-make-array-equal | Most Simple Solution Ever | kamaci | 0 | 26 | minimum operations to make array equal | 1,551 | 0.811 | Medium | 22,959 |
https://leetcode.com/problems/minimum-operations-to-make-array-equal/discuss/827474/Intuitive-approach-by-observation | class Solution:
def __init__(self):
self.even_cache = [1]
self.odd_cache = [2]
def minOperations(self, n: int) -> int:
if n == 1:
return 0
r'''
1, 3: 1 (even)
1, 3, 5: 2 (odd)
1, 3, 5, 7: 1 + 3 = 4 (even)
1, 3, 5, 7, 9: 2 + 4 = 6 (odd)
1, 3, 5, 7, 9, 11: 1 + 3 + 5 = 9 (even)
1, 3, 5, 7, 9, 11, 13: 2 + 4 + 6 = 12 (odd)
1, 3, 5, 7, 9, 11, 13, 15: 1 + 3 + 5 + 7 = 16 (even)
...
'''
def get_odd_cache(i):
while len(self.odd_cache) <= i:
self.odd_cache.append(self.odd_cache[-1] + (len(self.odd_cache)+1) * 2)
return self.odd_cache[i]
def get_even_cache(i):
while len(self.even_cache) <= i:
self.even_cache.append(self.even_cache[-1] + (len(self.even_cache)*2+1))
return self.even_cache[i]
d, r = divmod(n, 2)
if r == 0:
# even: 1 -> 4 -> 9 -> 16 -> ...
return get_even_cache(d-1)
else:
# odd: 2 -> 6 -> 12 -> ...
return get_odd_cache(d-1) | minimum-operations-to-make-array-equal | Intuitive approach by observation | puremonkey2001 | 0 | 39 | minimum operations to make array equal | 1,551 | 0.811 | Medium | 22,960 |
https://leetcode.com/problems/minimum-operations-to-make-array-equal/discuss/1693220/Python-Basic-Solution. | class Solution:
def minOperations(self, n: int) -> int:
k=[]
val = [j for j in range(n)]
arr = list(map(lambda i:((2*i)+1),val))
avg = int((sum(arr))/n)
for s in arr:
if s<avg:
k.append(avg-s)
return(sum(k)) | minimum-operations-to-make-array-equal | Python Basic Solution. | steuxnet | -1 | 79 | minimum operations to make array equal | 1,551 | 0.811 | Medium | 22,961 |
https://leetcode.com/problems/magnetic-force-between-two-balls/discuss/794249/Python3-binary-search-distance-space | class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
def fn(d):
"""Return True if d is a feasible distance."""
ans, prev = 0, -inf # where previous ball is put
for x in position:
if x - prev >= d:
ans += 1
if ans == m: return True
prev = x
return False
# "last True" binary search (in contrast to "first True" binary search)
lo, hi = 1, position[-1] - position[0]
while lo < hi:
mid = lo + hi + 1 >> 1
if fn(mid): lo = mid
else: hi = mid - 1
return lo | magnetic-force-between-two-balls | [Python3] binary search distance space | ye15 | 4 | 473 | magnetic force between two balls | 1,552 | 0.57 | Medium | 22,962 |
https://leetcode.com/problems/magnetic-force-between-two-balls/discuss/1441336/Simple-Python-O(nlogn)-solution-using-binary-search-and-greedy | class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
def verify(x):
'''
Returns true if we can put m balls in the buskets
while maintining at least x distance between them
'''
count, prev = 1, position[0]
for i in range(1, len(position)):
if position[i]-prev >= x:
prev = position[i]
count += 1
return count >= m
position.sort()
low, high, ret = 1, (position[-1]-position[0])//(m-1)+1, -1
while low <= high:
mid = low+(high-low)//2
if verify(mid):
# this is a solution but we are looking for the maximum
# update ret and continue looking for a even larger one
ret = mid
low = mid+1
else:
high = mid-1
return ret | magnetic-force-between-two-balls | Simple Python O(nlogn) solution using binary search and greedy | Charlesl0129 | 2 | 239 | magnetic force between two balls | 1,552 | 0.57 | Medium | 22,963 |
https://leetcode.com/problems/magnetic-force-between-two-balls/discuss/2813915/python3-binary-search-O(n-log-n)-time-and-constant-space-simple-solution | class Solution:
def helper(self, positions, m, distance):
current = positions[0]
ball = 1
for position in positions:
if position - current >= distance:
current = position
ball += 1
return ball >= m
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
left, right = 1, position[-1] - position[0]
while left + 1 < right:
mid = (left + right) // 2
print(mid, left, right)
if self.helper(position, m, mid):
left = mid
else:
right = mid - 1
return right if self.helper(position, m, right) else left | magnetic-force-between-two-balls | python3 binary search O(n log n) time and constant space, simple solution | miladBentaiba | 0 | 3 | magnetic force between two balls | 1,552 | 0.57 | Medium | 22,964 |
https://leetcode.com/problems/magnetic-force-between-two-balls/discuss/2812904/Binary-search-to-find-the-maximun-minimun-force-that-can-fills-no-less-than-m-balls | class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
def max_balls(force):
res = 1
start = position[0]
for i in range(1, len(position)):
if position[i] - start >= force:
res += 1
start = position[i]
return res
left, right = 1, position[-1] - position[0]
while left < right:
mid = left + (right - left + 1) // 2
if max_balls(mid) >= m:
left = mid
else:
right = mid - 1
return left | magnetic-force-between-two-balls | Binary search to find the maximun minimun force that can fills no less than m balls | michaelniki | 0 | 4 | magnetic force between two balls | 1,552 | 0.57 | Medium | 22,965 |
https://leetcode.com/problems/magnetic-force-between-two-balls/discuss/2058310/binary-search-llessr) | class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position = sorted(position)
def helper(x):
prev = float('-inf')
counter = 0
for p in position:
if abs(p-prev) >= x:
counter += 1
prev = p
if counter >= m:
return True
else:
return False
l, r = 1, max(position)+1
while l < r:
mid = (l+r) // 2
if not helper(mid):
r = mid
else:
l = mid+1
return l-1 | magnetic-force-between-two-balls | binary search [l<r) | cybernanana | 0 | 87 | magnetic force between two balls | 1,552 | 0.57 | Medium | 22,966 |
https://leetcode.com/problems/magnetic-force-between-two-balls/discuss/1333466/Python3%3A-Binary-Search | class Solution:
def canPlace(self,m,n,position,sep):
count = 1
location = position[0]
for i in range(1,n):
current_loc = position[i]
if current_loc - location >= sep:
count += 1
location = current_loc
if count == m:
return True
return False
def maxDistance(self, position: List[int], m: int) -> int:
n = len(position)
position.sort()
low = 0
#high will be difference of the sorted array
high = position[-1]-position[0]
ans = -1
while low <= high:
mid = (low+high)//2
place = self.canPlace(m,n,position,mid)
if place:
ans = mid
low = mid+1
else:
high = mid-1
return ans | magnetic-force-between-two-balls | Python3: Binary Search | s_m_d_29 | 0 | 66 | magnetic force between two balls | 1,552 | 0.57 | Medium | 22,967 |
https://leetcode.com/problems/magnetic-force-between-two-balls/discuss/1111469/Python-C%2B%2B-or-Binary-Search-or-100-Speed | class Solution:
def maxDistance(self, A, n):
A.sort()
L = len(A)
lo = 1
hi = (A[-1]-A[0])//(n-1)
best = 1
n -= 1
def valid(mid):
prev = A[0]
i = 0
for j in range(n):
d = prev + mid
while i<L and A[i]<d:
i += 1
if i==L:
return False
prev = A[i]
return True
while lo<=hi:
mid = (lo+hi) >> 1
if valid(mid):
best = mid
lo = mid + 1
else:
hi = mid - 1
return best | magnetic-force-between-two-balls | Python, C++ | Binary Search | 100% Speed | Aragorn_ | 0 | 128 | magnetic force between two balls | 1,552 | 0.57 | Medium | 22,968 |
https://leetcode.com/problems/minimum-number-of-days-to-eat-n-oranges/discuss/794275/Python3-bfs | class Solution:
def minDays(self, n: int) -> int:
ans = 0
queue = [n]
seen = set()
while queue: #bfs
newq = []
for x in queue:
if x == 0: return ans
seen.add(x)
if x-1 not in seen: newq.append(x-1)
if x % 2 == 0 and x//2 not in seen: newq.append(x//2)
if x % 3 == 0 and x//3 not in seen: newq.append(x//3)
ans += 1
queue = newq | minimum-number-of-days-to-eat-n-oranges | [Python3] bfs | ye15 | 26 | 1,500 | minimum number of days to eat n oranges | 1,553 | 0.346 | Hard | 22,969 |
https://leetcode.com/problems/minimum-number-of-days-to-eat-n-oranges/discuss/794275/Python3-bfs | class Solution:
def minDays(self, n: int) -> int:
@lru_cache(None)
def fn(n):
if n <= 1: return n
return 1 + min(n%2 + fn(n//2), n%3 + fn(n//3))
return fn(n) | minimum-number-of-days-to-eat-n-oranges | [Python3] bfs | ye15 | 26 | 1,500 | minimum number of days to eat n oranges | 1,553 | 0.346 | Hard | 22,970 |
https://leetcode.com/problems/minimum-number-of-days-to-eat-n-oranges/discuss/1730598/Python-BFS | class Solution:
def minDays(self, n: int) -> int:
ans = 0
q = [n]
visit = set()
visit.add(n)
while q:
for i in range(len(q)):
num = q.pop(0)
if num == 0:
return ans
if num and (num-1) not in visit:
visit.add(num-1)
q.append(num-1)
if num % 2 == 0 and num-(num//2) not in visit:
visit.add(num-(num//2))
q.append(num-(num//2))
if num % 3 == 0 and num-2*(num//3) not in visit:
visit.add(num-2*(num//3))
q.append(num-2*(num//3))
ans += 1 | minimum-number-of-days-to-eat-n-oranges | Python BFS | sanial2001 | 1 | 125 | minimum number of days to eat n oranges | 1,553 | 0.346 | Hard | 22,971 |
https://leetcode.com/problems/minimum-number-of-days-to-eat-n-oranges/discuss/1562979/Python-BFS-Solution | class Solution:
def minDays(self, n: int) -> int:
q = [(n,0)]
seen = set()
while q:
data = q.pop(0)
left, days = data[0],data[1]
if left == 0:
return days
if (left) in seen:
continue
seen.add((left))
q.append((left-1, days+1))
one = left % 2 == 0
if one:
q.append((left//2 , days +1))
two = left % 3 == 0
if two:
q.append(((left//3), days+1))
``` | minimum-number-of-days-to-eat-n-oranges | Python BFS Solution | swolecoder | 0 | 79 | minimum number of days to eat n oranges | 1,553 | 0.346 | Hard | 22,972 |
https://leetcode.com/problems/minimum-number-of-days-to-eat-n-oranges/discuss/1500977/Top-Down-DP-oror-Easy-Coded-oror-Clean-and-Concise | class Solution:
def minDays(self, n: int) -> int:
def count(n):
nonlocal dp
if n<=1:
return 1
if n in dp:
return dp[n]
dp[n] = 1 + min(n%2 + count(n//2), n%3 + count(n//3))
return dp[n]
dp = defaultdict(int)
return count(n) | minimum-number-of-days-to-eat-n-oranges | 📌📌 Top - Down DP || Easy-Coded || Clean & Concise 🐍 | abhi9Rai | 0 | 232 | minimum number of days to eat n oranges | 1,553 | 0.346 | Hard | 22,973 |
https://leetcode.com/problems/minimum-number-of-days-to-eat-n-oranges/discuss/794829/Python3-Beats-100-of-all-solutions-with-time-complexity | class Solution:
def minDays(self, n: int) -> int:
memo = dict()
def minimumDays(n):
if n in memo:
return memo[n]
if n == 1:
return 1
if n== 2 or n == 3:
return 2
memo[n] = min(n%2+minimumDays(n//2)+1, n%3+minimumDays(n//3)+1)
return memo[n]
return minimumDays(n) | minimum-number-of-days-to-eat-n-oranges | [Python3] Beats 100% of all solutions with time complexity | rahul113 | 0 | 108 | minimum number of days to eat n oranges | 1,553 | 0.346 | Hard | 22,974 |
https://leetcode.com/problems/thousand-separator/discuss/805712/Python3-1-line | class Solution:
def thousandSeparator(self, n: int) -> str:
return f"{n:,}".replace(",", ".") | thousand-separator | [Python3] 1-line | ye15 | 25 | 980 | thousand separator | 1,556 | 0.549 | Easy | 22,975 |
https://leetcode.com/problems/thousand-separator/discuss/805712/Python3-1-line | class Solution:
def thousandSeparator(self, n: int) -> str:
ans = deque()
while n:
n, d = divmod(n, 1000)
ans.appendleft(f"{d:03}" if n else str(d))
return ".".join(ans) or "0" | thousand-separator | [Python3] 1-line | ye15 | 25 | 980 | thousand separator | 1,556 | 0.549 | Easy | 22,976 |
https://leetcode.com/problems/thousand-separator/discuss/1094361/python-1-line-with-regex | class Solution:
def thousandSeparator(self, n: int) -> str:
return re.sub('(?<=\d)(?=(\d{3})+$)', '.', str(n)) | thousand-separator | python 1-line with regex | Hoke_luo | 3 | 67 | thousand separator | 1,556 | 0.549 | Easy | 22,977 |
https://leetcode.com/problems/thousand-separator/discuss/1227820/Python-or-Using-List | class Solution:
def thousandSeparator(self, n: int) -> str:
if(len(str(n))<=3):
return str(n)
s = list(str(n))
for i in range(len(s)-3,0,-3):
s.insert(i,'.')
ans = ''.join(s)
return ans | thousand-separator | Python | Using List | hirrangd | 1 | 100 | thousand separator | 1,556 | 0.549 | Easy | 22,978 |
https://leetcode.com/problems/thousand-separator/discuss/865271/Easy-intuition-based-solution | class Solution:
def thousandSeparator(self, n: int) -> str:
n = list(str(n))
current_count = 0
# Idea is to loop in reverse and add a dot after every three digits
for index in range(len(n) - 1, 0, -1):
current_count += 1
if current_count % 3 == 0:
n.insert(index, '.')
return (''.join(n)) | thousand-separator | Easy intuition based solution | rassel | 1 | 57 | thousand separator | 1,556 | 0.549 | Easy | 22,979 |
https://leetcode.com/problems/thousand-separator/discuss/2626383/Iterative-approach-with-a-bit-of-math | class Solution:
def thousandSeparator(self, n: int) -> str:
if n < 1000:
return str(n)
digits, count = [], 1
while True:
digits.append(str(n % 10))
n //= 10
if n == 0:
break
if count % 3 == 0:
digits.append('.')
count += 1
return "".join(reversed(digits)) | thousand-separator | Iterative approach with a bit of math | kcstar | 0 | 4 | thousand separator | 1,556 | 0.549 | Easy | 22,980 |
https://leetcode.com/problems/thousand-separator/discuss/2357010/Easy-python | class Solution:
def thousandSeparator(self, n: int) -> str:
n=str(n)
l=len(n)
if l<=3:
return n
ans=""
for i in range(-1,-(l+1),-1):
#print(i,n[i])
if (i%3==0) :
ans+=n[i]
ans+="."
else:
ans+=n[i]
ans=ans[::-1]
if ans[0]==".":
return ans[1:]
elif ans[-1] == ".":
return ans[:-1]
return ans | thousand-separator | Easy python | sunakshi132 | 0 | 35 | thousand separator | 1,556 | 0.549 | Easy | 22,981 |
https://leetcode.com/problems/thousand-separator/discuss/2289467/Python-or-Easy-to-understand-or-fast-or-Easy-Solution | class Solution:
def thousandSeparator(self, n: int) -> str:
if n<1000:
return str(n)
n=str(n)[::-1]
i=0
j=0
st=''
while(i!=len(n)):
if j == 3:
st+='.'+n[i]
j=0
else:
st+=n[i]
i+=1
j+=1
return st[::-1] | thousand-separator | Python | Easy to understand | fast | Easy Solution | ajay_keelu | 0 | 29 | thousand separator | 1,556 | 0.549 | Easy | 22,982 |
https://leetcode.com/problems/thousand-separator/discuss/2155315/Thousand-Separator-for-Python-way | class Solution:
def thousandSeparator(self, n: int) -> str:
lens = len(str(n))
ans = []
add = 1
reverseN = list(str(n)[::-1])
for i in range(lens):
ans.append(reverseN[i])
if add % 3 == 0 :
ans.append('.')
add += 1
ans.reverse()
if ans[0] == '.':
ans.pop(0)
number = ''.join(map(str, ans))
return number | thousand-separator | Thousand Separator for Python way | YangJenHao | 0 | 31 | thousand separator | 1,556 | 0.549 | Easy | 22,983 |
https://leetcode.com/problems/thousand-separator/discuss/2017955/WEEB-DOES-PYTHONC%2B%2B | class Solution:
def thousandSeparator(self, n: int) -> str:
nums = str(n)
count = 0
result = ""
i = len(nums)-1
while i >= 0:
result = nums[i] + result
count +=1
if count == 3 and i != 0:
count = 0
result = "." + result
i-=1
return result | thousand-separator | WEEB DOES PYTHON/C++ | Skywalker5423 | 0 | 35 | thousand separator | 1,556 | 0.549 | Easy | 22,984 |
https://leetcode.com/problems/thousand-separator/discuss/1922129/Python-easy-solution-for-beginners-by-reversing-and-iterating | class Solution:
def thousandSeparator(self, n: int) -> str:
if len(str(n)) < 4:
return str(n)
res = ""
rev = str(n)[::-1]
for i in range(0, len(rev), 3):
res += rev[i:i+3] + "."
res = res[:len(res)-1]
return res[::-1] | thousand-separator | Python easy solution for beginners by reversing and iterating | alishak1999 | 0 | 67 | thousand separator | 1,556 | 0.549 | Easy | 22,985 |
https://leetcode.com/problems/thousand-separator/discuss/1908107/Full-and-Easiest-Explanation-oror-Easy-to-Understand-oror-7-line-code | class Solution:
def thousandSeparator(self, n: int) -> str:
n = str(n)
l = len(n)
if l == 3: return n
res = ""
res += n[0]
for i in range(1,l):
if len(n[i::])%3 == 0:
res += "."
res += n[i]
return res | thousand-separator | ✅Full and Easiest Explanation || Easy to Understand || 7 line code | Dev_Kesarwani | 0 | 27 | thousand separator | 1,556 | 0.549 | Easy | 22,986 |
https://leetcode.com/problems/thousand-separator/discuss/1889933/Python-Simple-and-Clean!-No-string-reversal! | class Solution:
def thousandSeparator(self, n):
s, ans = str(n), ""
for i in range(len(s)):
ans += s[i]
if (len(s)-i-1) % 3 == 0 and i < len(s)-1:
ans += "."
return ans | thousand-separator | Python - Simple and Clean! No string reversal! | domthedeveloper | 0 | 64 | thousand separator | 1,556 | 0.549 | Easy | 22,987 |
https://leetcode.com/problems/thousand-separator/discuss/1889933/Python-Simple-and-Clean!-No-string-reversal! | class Solution:
def thousandSeparator(self, n):
s = list(str(n))
for i in range(len(s)-3,0,-3):
s.insert(i,'.')
return "".join(s) | thousand-separator | Python - Simple and Clean! No string reversal! | domthedeveloper | 0 | 64 | thousand separator | 1,556 | 0.549 | Easy | 22,988 |
https://leetcode.com/problems/thousand-separator/discuss/1884481/Easiest-and-Simplest-Python3-Solution-oror-100-Faster-Clean-and-Straight-Forward | class Solution:
def thousandSeparator(self, n: int) -> str:
x=str(n)
ss=""
res=""
ct=0
temp=[]
if len(x)>3 and len(x)%3!=0:
i=len(x)-1
while i>=0:
if i not in temp:
ct=ct+1
if ct<3:
temp.append(x[i])
elif ct==3:
temp.append(x[i])
temp.append(".")
ct=0
i=i-1
ss="".join(temp)
ss=ss[::-1]
return (ss)
elif len(x)>3 and len(x)%3==0:
i=len(x)-1
while i>=0:
if i not in temp:
ct=ct+1
if ct<3:
temp.append(x[i])
elif ct==3:
temp.append(x[i])
temp.append(".")
ct=0
i=i-1
ss="".join(temp)
ss=ss[::-1]
res=ss[1:]
return (res)
else:
return (x) | thousand-separator | Easiest & Simplest Python3 Solution || 100% Faster, Clean & Straight Forward | RatnaPriya | 0 | 27 | thousand separator | 1,556 | 0.549 | Easy | 22,989 |
https://leetcode.com/problems/thousand-separator/discuss/1877993/Python3-or-Simple | class Solution:
def thousandSeparator(self, n: int) -> str:
n_str = str(n)
n_str = n_str[::-1]
ans = ""
for index, digit in enumerate(n_str):
if not (index + 1)%3 and index < len(n_str)-1:
ans = ans + digit + "."
else:
ans += digit
return ans[::-1] | thousand-separator | Python3 | Simple | user0270as | 0 | 21 | thousand separator | 1,556 | 0.549 | Easy | 22,990 |
https://leetcode.com/problems/thousand-separator/discuss/1791732/4-Lines-Python-Solution-oror-98-Faster-(24ms)-oror-Memory-Less-than-85 | class Solution:
def thousandSeparator(self, n: int) -> str:
if n < 1000: return str(n)
s, ans = str(n)[::-1], ''
for i in range(0,len(s),3): ans += s[i:i+3] + '.'
return ans[::-1].lstrip('.') | thousand-separator | 4-Lines Python Solution || 98% Faster (24ms) || Memory Less than 85% | Taha-C | 0 | 84 | thousand separator | 1,556 | 0.549 | Easy | 22,991 |
https://leetcode.com/problems/thousand-separator/discuss/1759028/Python-dollarolution | class Solution:
def thousandSeparator(self, n: int) -> str:
n = str(n)
l = len(n)
for i in range(3,l,3):
n = n[0:l-i] + '.' +n[l-i:]
return n | thousand-separator | Python $olution | AakRay | 0 | 55 | thousand separator | 1,556 | 0.549 | Easy | 22,992 |
https://leetcode.com/problems/thousand-separator/discuss/1642606/One-liner-Python3-solution | class Solution:
def thousandSeparator(self, n: int) -> str:
return '{:,}'.format(n).replace(',', '.') | thousand-separator | One-liner Python3 solution | y-arjun-y | 0 | 65 | thousand separator | 1,556 | 0.549 | Easy | 22,993 |
https://leetcode.com/problems/thousand-separator/discuss/1121962/Python3-one-line-solution-why-you-guys-reinventing-the-wheel-by-yourself | class Solution:
def thousandSeparator(self, n: int) -> str:
return f"{n:,}".replace(",", ".") | thousand-separator | Python3 one line solution, why you guys reinventing the wheel by yourself? | BrianHu | 0 | 58 | thousand separator | 1,556 | 0.549 | Easy | 22,994 |
https://leetcode.com/problems/thousand-separator/discuss/1112478/Python3-simple-solution | class Solution:
def thousandSeparator(self, n: int) -> str:
return '{:,}'.format(n).replace(',','.') | thousand-separator | Python3 simple solution | EklavyaJoshi | 0 | 69 | thousand separator | 1,556 | 0.549 | Easy | 22,995 |
https://leetcode.com/problems/thousand-separator/discuss/1107217/Python-1-line-fast-and-pythonic-solution | class Solution:
def thousandSeparator(self, n: int) -> str:
return '.'.join([str(n)[::-1][i:i+3][::-1] for i in range(0, len(str(n)), 3)][::-1]) | thousand-separator | Python, 1 line, fast and pythonic solution | cruim | 0 | 95 | thousand separator | 1,556 | 0.549 | Easy | 22,996 |
https://leetcode.com/problems/thousand-separator/discuss/995526/Simple-solution-in-python3-using-string-manipulation | class Solution:
def thousandSeparator(self, n: int) -> str:
ans = ""
if len(str(n)) <= 3:
return str(n)
n = str(n)[::-1]
for i in range(len(n)):
ans += n[i]
if (i + 1) % 3 == 0:
ans += '.'
return ans[::-1][1:] if len(str(n)) % 3 == 0 else ans[::-1] | thousand-separator | Simple solution in python3 using string manipulation | amoghrajesh1999 | 0 | 47 | thousand separator | 1,556 | 0.549 | Easy | 22,997 |
https://leetcode.com/problems/thousand-separator/discuss/946708/Python-3-%3A-reverse-solution | class Solution:
def thousandSeparator(self, n: int) -> str:
if(n == 0):
return '0'
s = ''
x = 0
new_n = str(n)
for i in reversed(range(len(new_n))):
if(x == 3):
s += '.'
x = 0
s+= new_n[i]
x+=1
return s[::-1] | thousand-separator | Python 3 : reverse solution | abhijeetmallick29 | 0 | 98 | thousand separator | 1,556 | 0.549 | Easy | 22,998 |
https://leetcode.com/problems/thousand-separator/discuss/816492/Intuitive-approach-by-using-divmod | class Solution:
def thousandSeparator(self, n: int) -> str:
ans = ''
n, r = divmod(n, 1000)
while n:
ans = "{:03d}.".format(r) + ans if ans else "{:03d}".format(r)
n, r = divmod(n, 1000)
ans = f"{r}." + ans if ans else str(r)
return ans | thousand-separator | Intuitive approach by using divmod | puremonkey2001 | 0 | 25 | thousand separator | 1,556 | 0.549 | Easy | 22,999 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.