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/find-smallest-letter-greater-than-target/discuss/2078998/Easy-python-solution | class Solution:
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
letters.sort()
for i in letters:
if (i>target):
return i
return letters[0] | find-smallest-letter-greater-than-target | Easy python solution | tusharkhanna575 | 3 | 137 | find smallest letter greater than target | 744 | 0.448 | Easy | 12,200 |
https://leetcode.com/problems/find-smallest-letter-greater-than-target/discuss/1846914/94-faster-python-solution.-Easy-to-understand. | class Solution:
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
start = 0
end = len(letters)-1
while start <= end:
mid = start + (end-start)//2
if letters[mid] > target:
end = mid - 1
else :
start = mid + 1
return letters[start%len(letters)] | find-smallest-letter-greater-than-target | 94% faster python solution. Easy to understand. | 1903480100017_A | 2 | 114 | find smallest letter greater than target | 744 | 0.448 | Easy | 12,201 |
https://leetcode.com/problems/find-smallest-letter-greater-than-target/discuss/2150374/Simple-Python-Solution-Binary-Search | class Solution:
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
l=0
r=len(letters)-1
res=letters[0]
while(l<=r):
mid=(l+r)//2
if letters[mid]>target:
r=mid-1
res=letters[mid]
else:
l=mid+1
return res | find-smallest-letter-greater-than-target | Simple Python Solution - Binary Search | pruthashouche | 1 | 114 | find smallest letter greater than target | 744 | 0.448 | Easy | 12,202 |
https://leetcode.com/problems/find-smallest-letter-greater-than-target/discuss/1992106/Python-Short-and-Clean-Binary-Search-(without-bisect) | class Solution:
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
lo, hi = 0, len(letters)
while lo != hi:
mid = (lo + hi) // 2
if target < letters[mid]:
hi = mid
else:
lo = mid + 1
return letters[lo % len(letters)] | find-smallest-letter-greater-than-target | ✅ Python, Short and Clean Binary Search (without bisect) | AntonBelski | 1 | 89 | find smallest letter greater than target | 744 | 0.448 | Easy | 12,203 |
https://leetcode.com/problems/find-smallest-letter-greater-than-target/discuss/1910398/Python3-binary-search-implementation | class Solution:
def search(self,letters,target):
low=0
high=len(letters)-1
idx=-1
while low<=high:
mid=(low+high)//2
if letters[mid]==target:
idx=mid
break
elif target<letters[mid]:
high=mid-1
else:
low=mid+1
return idx
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
letters=sorted(list(set(letters)))
idx=self.search(letters,target)
if idx==-1:
letters.append(target)
letters.sort()
idx=self.search(letters,target)
if idx==len(letters)-1:
return letters[0]
return letters[idx+1]
if idx==len(letters)-1:
return letters[0]
return letters[idx+1] | find-smallest-letter-greater-than-target | Python3 binary search implementation | amannarayansingh10 | 1 | 103 | find smallest letter greater than target | 744 | 0.448 | Easy | 12,204 |
https://leetcode.com/problems/find-smallest-letter-greater-than-target/discuss/1892592/O(logn)-Time-Complexity-Solution | class Solution:
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
left = 0
right = len(letters) - 1
while left <= right:
mid = (left + right) // 2
if letters[mid] <= target:
if mid + 1 < len(letters) and letters[mid + 1] > target:
return letters[mid + 1]
left = mid + 1
else:
right = mid - 1
return letters[0] | find-smallest-letter-greater-than-target | O(logn) Time Complexity Solution | EdwinJagger | 1 | 50 | find smallest letter greater than target | 744 | 0.448 | Easy | 12,205 |
https://leetcode.com/problems/find-smallest-letter-greater-than-target/discuss/1875289/Python-Easiest-Solution-With-Explanation-or-99.2-Faster-or-Binary-Search-O(logn-or-Beg-to-Adv | class Solution:
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
left = 0
right = len(letters) - 1
# our array is a circular one, that implies that the next greatest value must be the first value in the array.
if target < letters[left] or target >= letters[right]:
return letters[left] # so we`ll return the first value of the array if both above conditions are satisfied.
while left <= right:
mid = (right + left) // 2
if target < letters[mid]:
right = mid - 1
# as we want the smallest value thats greater then the target
# we can merge the usual cases where target == letters[mid into the
# the target > letters[mid].
if target >= letters[mid]:
left = mid + 1
return letters[left] | find-smallest-letter-greater-than-target | Python Easiest Solution With Explanation | 99.2 % Faster | Binary Search O(logn | Beg to Adv | rlakshay14 | 1 | 80 | find smallest letter greater than target | 744 | 0.448 | Easy | 12,206 |
https://leetcode.com/problems/find-smallest-letter-greater-than-target/discuss/1818343/Python-using-Binary-Search-with-constant-space.-Time-O(logn)-Space-O(1) | class Solution:
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
left = 0
right = len(letters)
if ord(target) < ord(letters[0]) or ord(target) >= ord(letters[right-1]):
return letters[0]
if right == 2 and (ord(target) == ord(letters[0]) or ord(target) < ord(letters[1])):
return letters[1]
while left <= right:
mid = (left+right)//2
if ord(target) == ord(letters[mid]):
if ord(target) == ord(letters[mid+1]):
right += 1
else:
return letters[mid+1]
elif ord(letters[mid+1]) > ord(target) and ord(letters[mid]) < ord(target):
return letters[mid+1]
elif ord(target) > ord(letters[mid]):
left += 1
else:
right -= 1 | find-smallest-letter-greater-than-target | [Python] using Binary Search with constant space. Time-O(logn), Space-O(1) | jamil117 | 1 | 55 | find smallest letter greater than target | 744 | 0.448 | Easy | 12,207 |
https://leetcode.com/problems/find-smallest-letter-greater-than-target/discuss/1496779/EASY-Python-Solution | class Solution:
def nextGreatestLetter(self, arr: List[str], t: str) -> str:
strt=0
end=len(arr)-1
res=arr[0]
while strt<=end:
mid=strt+(end-strt)//2
if ord(arr[mid])>ord(t):
res=arr[mid]
end=mid-1
else:
strt=mid+1
return res | find-smallest-letter-greater-than-target | EASY Python Solution | dashrathsingh | 1 | 92 | find smallest letter greater than target | 744 | 0.448 | Easy | 12,208 |
https://leetcode.com/problems/find-smallest-letter-greater-than-target/discuss/1280735/Easy-Python-Solution(96.57) | class Solution(object):
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
if letters[0]>target or letters[-1]<=target:
return letters[0]
l=0
h=len(letters)-1
while l <=h:
m = l+ (h - l)// 2
if letters[m]<=target:
l = m + 1
elif letters[m] > target:
h = m - 1
return letters[l] | find-smallest-letter-greater-than-target | Easy Python Solution(96.57%) | Sneh17029 | 1 | 337 | find smallest letter greater than target | 744 | 0.448 | Easy | 12,209 |
https://leetcode.com/problems/find-smallest-letter-greater-than-target/discuss/577769/Straightforward-Solution-from-Scratch | class Solution:
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
# Getting rid of duplicates - set()
letters = sorted(set(letters))
#-----------------------------------------------
# where target character is in 'letters'
if target in letters:
tindex = letters.index(target)
if tindex + 1 < len(letters):
return letters[tindex+1]
elif tindex+1 == len(letters):
return letters[0]
# Where target character is not in 'letters'
else:
begin = letters[0]
end = letters[-1]
# Boundary conditions
if target < begin or target >=end:
return begin
# If we reach here - it means, it's between one of the characters in 'letters'
else:
for i, data in enumerate(letters):
if data > target:
return letters[i] | find-smallest-letter-greater-than-target | Straightforward Solution from Scratch | cppygod | 1 | 79 | find smallest letter greater than target | 744 | 0.448 | Easy | 12,210 |
https://leetcode.com/problems/find-smallest-letter-greater-than-target/discuss/2748155/99-.44-faster-than-any-other-python-solution | class Solution:
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
st=0
end=len(letters)
result=0
if target >= letters[-1] or target < letters[0]:
return letters[0]
while st<=end:
mid= st+(end-st)//2
if letters[mid]==target:
st=mid+1
elif letters[mid]<target:
st= mid+1
elif letters[mid]>target:
result = letters[mid]
end=mid-1
return result | find-smallest-letter-greater-than-target | 99 .44% faster than any other python solution | sushants007 | 0 | 7 | find smallest letter greater than target | 744 | 0.448 | Easy | 12,211 |
https://leetcode.com/problems/find-smallest-letter-greater-than-target/discuss/2716796/Binary-Search-Easy-Solution | class Solution:
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
N = len(letters)
left = 0
right = N - 1
if letters[N - 1] <= target or letters[0] > target:
return letters[0]
while left <= right:
mid = left + (right - left + 1) // 2
if letters[mid] <= target:
left = mid + 1
else:
right = mid - 1
return letters[left] | find-smallest-letter-greater-than-target | Binary Search Easy Solution | user6770yv | 0 | 4 | find smallest letter greater than target | 744 | 0.448 | Easy | 12,212 |
https://leetcode.com/problems/find-smallest-letter-greater-than-target/discuss/2699039/Simple-Python3-solution | class Solution:
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
l=0
r=len(letters)-1
res=letters[0]
while l<=r:
mid=l+(r-l)//2
if letters[mid]<=target:
l=mid+1
else:
res=letters[mid]
r=mid-1
return res | find-smallest-letter-greater-than-target | Simple Python3 solution | anirudh422 | 0 | 6 | find smallest letter greater than target | 744 | 0.448 | Easy | 12,213 |
https://leetcode.com/problems/find-smallest-letter-greater-than-target/discuss/2661251/Python3-or-binary-search | class Solution:
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
st=0
end=len(letters)-1
#edgecase1 ==> when given target is greater than elements in letters
#edgecase2 ==> when given target is less than elements in letters
if letters[-1]<=target or letters[0]>target:
return letters[0]
while st<=end:
mid=st+(end-st)//2
if letters[mid]>target:
end=mid-1
elif letters[mid]<=target:
st=mid+1
return letters[st]
# smallest character in the array that is larger than target ==> start poniter
# incase biggest character in the array that is smaller than target ==> end poniter | find-smallest-letter-greater-than-target | Python3 | binary search | 19pa1a1257 | 0 | 16 | find smallest letter greater than target | 744 | 0.448 | Easy | 12,214 |
https://leetcode.com/problems/find-smallest-letter-greater-than-target/discuss/2659437/Pyhton-or-Easy-Solution-or-Loop | class Solution:
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
letters.sort()
target_ord = ord(target)
curr_diff = 99999
ans = ""
for i in letters:
if int(curr_diff) > int(ord(i)) - int(target_ord) and int(ord(i)) - int(target_ord) > 0:
curr_diff = int(ord(i)) - int(target_ord)
ans = i
if ans == '':
ans = letters[0]
return ans | find-smallest-letter-greater-than-target | Pyhton | Easy Solution | Loop | atharva77 | 0 | 1 | find smallest letter greater than target | 744 | 0.448 | Easy | 12,215 |
https://leetcode.com/problems/find-smallest-letter-greater-than-target/discuss/2389149/Python-Binary-Search-Solution | class Solution:
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
beg = 0
end = len(letters)-1
while beg <= end:
mid = (beg+end)//2
if letters[mid]>target:
end = mid -1
else:
beg = mid +1
return letters[beg] if beg<len(letters) else letters[0] | find-smallest-letter-greater-than-target | Python Binary Search Solution | Harshi_Tyagi | 0 | 68 | find smallest letter greater than target | 744 | 0.448 | Easy | 12,216 |
https://leetcode.com/problems/find-smallest-letter-greater-than-target/discuss/2361434/Python-Simple-Solution-using-Binary-Searchoror-Documented | class Solution:
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
low, high = 0, len(letters)-1
# if first letter is greater than target or
# if target is greater than even last element, return first letter
if target < letters[low] or target >= letters[high]:
return letters[low]
# Repeat until the pointers low and high meet each other
while low <= high:
mid = (low + high) // 2 # middle point - pivot
if letters[mid] <= target:
low = mid + 1; # go right side
else:
high = mid - 1 # go left side
return letters[low] | find-smallest-letter-greater-than-target | [Python] Simple Solution using Binary Search|| Documented | Buntynara | 0 | 19 | find smallest letter greater than target | 744 | 0.448 | Easy | 12,217 |
https://leetcode.com/problems/find-smallest-letter-greater-than-target/discuss/2332486/Python-or-Java-or-CPP-orO(log-n-) | class Solution:
def nextGreatestLetter(self, nums: List[str], target: str) -> str:
start, end = 0, len(nums)-1
out = nums[0]
while(start <= end):
mid = start + (end - start)//2
if nums[mid] > target:
out = nums[mid]
end = mid - 1
else:
start = mid + 1
return out | find-smallest-letter-greater-than-target | Python | Java | CPP |O(log n ) | devilmind116 | 0 | 22 | find smallest letter greater than target | 744 | 0.448 | Easy | 12,218 |
https://leetcode.com/problems/find-smallest-letter-greater-than-target/discuss/2214085/Python-solution-with-and-without-binary-search | class Solution:
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
for i in letters:
if ord(i)>ord(target):
return (i)
break;
return(letters[0]) | find-smallest-letter-greater-than-target | Python solution with and without binary search | SakshiMore22 | 0 | 66 | find smallest letter greater than target | 744 | 0.448 | Easy | 12,219 |
https://leetcode.com/problems/find-smallest-letter-greater-than-target/discuss/2214085/Python-solution-with-and-without-binary-search | class Solution:
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
n = len(letters)
if n == 0:
return ""
low = 0
high = n - 1
res = 0
while low <= high:
mid =(low + high)// 2
if letters[mid] > target:
res= mid
high = mid - 1
else:
low = mid + 1
return letters[res] | find-smallest-letter-greater-than-target | Python solution with and without binary search | SakshiMore22 | 0 | 66 | find smallest letter greater than target | 744 | 0.448 | Easy | 12,220 |
https://leetcode.com/problems/find-smallest-letter-greater-than-target/discuss/2127872/Python-Easy-solution-with-complexities | class Solution:
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
for letter in letters:
if letter > target:
return letter
return letters[0]
# time O(n)
# space O(1) | find-smallest-letter-greater-than-target | [Python] Easy solution with complexities | mananiac | 0 | 48 | find smallest letter greater than target | 744 | 0.448 | Easy | 12,221 |
https://leetcode.com/problems/find-smallest-letter-greater-than-target/discuss/2127872/Python-Easy-solution-with-complexities | class Solution:
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
if letters[0] > target or letters[-1] <= target:
return letters[0]
left = 0
right = len(letters) -1
while (left <= right):
mid = (left +right)//2
if letters[mid] == target and letters[mid] != letters[mid+1]:
return letters[mid+1]
if letters[mid] == target and letters[mid] == letters[mid+1]:
left=mid + 1
if letters[mid] < target:
left = mid +1
if letters[mid] > target:
right = mid - 1
return letters[left]
# time O(logn)
# space O(1) | find-smallest-letter-greater-than-target | [Python] Easy solution with complexities | mananiac | 0 | 48 | find smallest letter greater than target | 744 | 0.448 | Easy | 12,222 |
https://leetcode.com/problems/find-smallest-letter-greater-than-target/discuss/2083312/4-Lines-Python-Solution-oror-Easy | class Solution:
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
target_ord=ord(target)
for i in letters:
if ord(i)>target_ord:
return i
return letters[0] # If target not found, then it is wrapped arround case, so return 1st element | find-smallest-letter-greater-than-target | 4 Lines Python Solution || Easy | aksgupta98 | 0 | 44 | find smallest letter greater than target | 744 | 0.448 | Easy | 12,223 |
https://leetcode.com/problems/find-smallest-letter-greater-than-target/discuss/2031339/Python-solution | class Solution:
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
for i in sorted(letters):
if i > target:
return i
else:
return letters[0] | find-smallest-letter-greater-than-target | Python solution | StikS32 | 0 | 55 | find smallest letter greater than target | 744 | 0.448 | Easy | 12,224 |
https://leetcode.com/problems/find-smallest-letter-greater-than-target/discuss/2024000/Faster-than-93%3A-Beginner-Python-Solution | class Solution:
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
for i in letters:
if i > target:
return i
return letters[0] | find-smallest-letter-greater-than-target | Faster than 93%: Beginner Python Solution | 7yler | 0 | 42 | find smallest letter greater than target | 744 | 0.448 | Easy | 12,225 |
https://leetcode.com/problems/find-smallest-letter-greater-than-target/discuss/2000366/Python-Linear-Search-%2B-Binary-Search | class Solution:
def nextGreatestLetter(self, letters, target):
return min(filter(lambda x : x > target, letters), default=letters[0]) | find-smallest-letter-greater-than-target | Python - Linear Search + Binary Search | domthedeveloper | 0 | 56 | find smallest letter greater than target | 744 | 0.448 | Easy | 12,226 |
https://leetcode.com/problems/find-smallest-letter-greater-than-target/discuss/2000366/Python-Linear-Search-%2B-Binary-Search | class Solution:
def nextGreatestLetter(self, letters, target):
i = bisect_right(letters,target)
return letters[i] if i < len(letters) else letters[0] | find-smallest-letter-greater-than-target | Python - Linear Search + Binary Search | domthedeveloper | 0 | 56 | find smallest letter greater than target | 744 | 0.448 | Easy | 12,227 |
https://leetcode.com/problems/find-smallest-letter-greater-than-target/discuss/1964302/Python-oror-Simple-and-Clean-Binary-Search | class Solution:
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
n = len(letters)
start , end = 0, n
while start < end:
mid = (start + end)//2
if target < letters[mid] :
end = mid
else: start = mid +1
return letters[start%n] | find-smallest-letter-greater-than-target | Python || Simple and Clean Binary Search | morpheusdurden | 0 | 67 | find smallest letter greater than target | 744 | 0.448 | Easy | 12,228 |
https://leetcode.com/problems/find-smallest-letter-greater-than-target/discuss/1949713/java-python-easy-binary-search-(Time-Ologn-space-O1) | class Solution:
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
l = 0
r = len(letters) - 1
if target < letters[0] or target >= letters[r] : return letters[0]
while True :
if r - l == 1 : return letters[r]
m = (l+r)>>1
if letters[m] == letters[l] : l = m
elif letters[m] == letters[r] : r = m
elif letters[m] <= target : l = m
else : r = m | find-smallest-letter-greater-than-target | java, python - easy binary search (Time Ologn, space O1) | ZX007java | 0 | 32 | find smallest letter greater than target | 744 | 0.448 | Easy | 12,229 |
https://leetcode.com/problems/find-smallest-letter-greater-than-target/discuss/1937304/Python-Linear-Search-%2B-Binary-Search-Easy-To-Understand-Both | class Solution:
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
# ******** Linear Search Solution ********
# for c in letters:
# if c > target:
# return c
# return letters[0]
# ******** Binary Search Solution ********
high, low = len(letters) - 1, 0
if target >= letters[-1]:
return letters[0]
while low <= high:
mid = (low + high) // 2
c = letters[mid]
if c > target:
high = mid - 1
elif c <= target:
low = mid + 1
return letters[low] | find-smallest-letter-greater-than-target | Python Linear Search + Binary Search, Easy To Understand Both | Hejita | 0 | 42 | find smallest letter greater than target | 744 | 0.448 | Easy | 12,230 |
https://leetcode.com/problems/find-smallest-letter-greater-than-target/discuss/1870563/Python-Easy-Solutions-or-LogN | class Solution:
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
for i in letters:
if i > target: return i
return letters[0] | find-smallest-letter-greater-than-target | ✅ Python Easy Solutions | LogN | dhananjay79 | 0 | 62 | find smallest letter greater than target | 744 | 0.448 | Easy | 12,231 |
https://leetcode.com/problems/find-smallest-letter-greater-than-target/discuss/1870563/Python-Easy-Solutions-or-LogN | class Solution:
def nextGreatestLetter(self, arr: List[str], target: str) -> str:
lo, hi = 0, len(arr) - 1
while lo <= hi:
mid = (lo + hi) // 2
if target >= arr[mid]: lo = mid + 1
if target < arr[mid]: hi = mid - 1
return arr[lo % len(arr)] | find-smallest-letter-greater-than-target | ✅ Python Easy Solutions | LogN | dhananjay79 | 0 | 62 | find smallest letter greater than target | 744 | 0.448 | Easy | 12,232 |
https://leetcode.com/problems/find-smallest-letter-greater-than-target/discuss/1857532/Python3-Solution-(99-Faster-and-easy-to-understand) | class Solution:
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
letters = list(set(letters))
if target not in letters:
letters.append(target)
letters.sort()
index_of_target = letters.index(target)
if index_of_target + 1 == len(letters):
return letters[0]
return letters[index_of_target + 1] | find-smallest-letter-greater-than-target | Python3 Solution (99% Faster and easy to understand) | hardik097 | 0 | 48 | find smallest letter greater than target | 744 | 0.448 | Easy | 12,233 |
https://leetcode.com/problems/find-smallest-letter-greater-than-target/discuss/1849557/Python3-Faster-Than-98.4-Using-Binary-Search | class Solution:
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
left = 0
right = len(letters) - 1
next_largest = letters[0]
while left <= right:
mid = left + (right - left) // 2
# case: left or right end met
if left == right:
# case: letters[-1] is first char larger than target
if letters[left] > target:
return letters[left]
# case: letters[-1] == target
return letters[0]
# case: letter > target --> go left, mark current letter as largest char after target
elif letters[mid] > target:
right = mid
next_largest = letters[mid]
# case: letter <= target --> go right
else:
left = mid + 1
return next_largest | find-smallest-letter-greater-than-target | Python3 Faster Than 98.4% Using Binary Search | justtestingn | 0 | 39 | find smallest letter greater than target | 744 | 0.448 | Easy | 12,234 |
https://leetcode.com/problems/find-smallest-letter-greater-than-target/discuss/1706864/simple-python-solution-faster-than-99.63 | class Solution:
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
low, high = 0, len(letters)-1
while low <= high:
mid = (low+high)//2
if letters[mid] <= target:
low = mid+1
else:
high = mid-1
return letters[low % len(letters)] | find-smallest-letter-greater-than-target | simple python solution faster than 99.63% | fatmakahveci | 0 | 108 | find smallest letter greater than target | 744 | 0.448 | Easy | 12,235 |
https://leetcode.com/problems/find-smallest-letter-greater-than-target/discuss/1599328/Simple-Python-Solution | class Solution:
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
asc = ord(target)
for i in letters:
if ord(i) > asc:
return i
return letters[0] | find-smallest-letter-greater-than-target | Simple Python Solution | jodyzhou | 0 | 40 | find smallest letter greater than target | 744 | 0.448 | Easy | 12,236 |
https://leetcode.com/problems/find-smallest-letter-greater-than-target/discuss/1510852/Simplest-or-Python-3 | class Solution:
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
for letter in letters:
if ord(letter) > ord(target):
return letter
return letters[0] | find-smallest-letter-greater-than-target | Simplest | Python 3 | deep765 | 0 | 187 | find smallest letter greater than target | 744 | 0.448 | Easy | 12,237 |
https://leetcode.com/problems/find-smallest-letter-greater-than-target/discuss/1495107/Multiple-Ways-to-Approach-a-Problem-for-BEGINNERS-greater-Step-by-Step | class Solution:
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
# Approach 1 : O(N) Time and O(1) Space -- Brute Force Thinking
# Step 1 : We will simply append the given target in letters and sort it
# This way, if the target is present at ith index, answer will be at (i+1)th index
# Step 2 : If it is at (n-1)th index or last index, simply return letteres[0] (as per condition in the question)
# Step 3 : Else, just iterate from back and as soon as we find the target's index and
# simply return the element at (i+1)th index
#if input is ["c", "f", "j"] ; target = "a"
#Step 1
letters.append(target) #letters = ["c", "f", "j", "a"]
letters.sort() #letters = ["a", "c", "f", "j"]
n = len(letters)
#Step 2
if letters[n-1] == target:
return letters[0]
#Step 3
for i in range(n-1, -1, -1):
if letters[i] == target:
return letters[i+1]
#-------------------------------------------------------------------------
# Approach 2 : O(N) Time and O(1) Space -- Linear Search Algorithm
# Given elements in the letters are sorted, so as soon as we find an element > target, return it
# Else, as per question, if we do not find any element greater than target, we will return letters[0]
for i in letters:
if i > target:
return i
return letters[0] #as per condition given in question
#--------------------------------------------------------------------------
# Approach 3 : O(log(N)) Time and O(1) Space -- Binary Search Algorithm
#Will be adding a detailed explanation here soon
n = len(letters)
#condition as per question (read the NOTE section properly)
if target < letters[0] or target >= letters[n-1]:
return letters[0]
left = 0
right = n - 1
while left <= right:
mid = left + (right - left)//2
if letters[mid] > target:
right = mid - 1
if letters[mid] <= target :
left = mid + 1
return letters[left] | find-smallest-letter-greater-than-target | Multiple Ways to Approach a Problem for BEGINNERS --> Step by Step | aarushsharmaa | 0 | 36 | find smallest letter greater than target | 744 | 0.448 | Easy | 12,238 |
https://leetcode.com/problems/find-smallest-letter-greater-than-target/discuss/1365804/Python-Solution | class Solution:
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
letters.append(target)
letters = sorted(set(letters))
ind = letters.index(target)
return letters[ind + 1] if len(letters) > ind+1 else letters[0] | find-smallest-letter-greater-than-target | Python Solution | _Mansiii_ | 0 | 31 | find smallest letter greater than target | 744 | 0.448 | Easy | 12,239 |
https://leetcode.com/problems/find-smallest-letter-greater-than-target/discuss/1238839/Python3-simple-solution-using-binary-search | class Solution(object):
def nextGreatestLetter(self, letters, target):
x = bisect.bisect(letters, target)
return letters[x % len(letters)] | find-smallest-letter-greater-than-target | Python3 simple solution using binary search | EklavyaJoshi | 0 | 42 | find smallest letter greater than target | 744 | 0.448 | Easy | 12,240 |
https://leetcode.com/problems/find-smallest-letter-greater-than-target/discuss/1172475/Python3-Simple-Approach-with-Explanation-and-Comments | class Solution:
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
'''
1. Starting from the next letter in alphabetical order of the 'target' letter, check if the letter is in list 'letters'.
2. Let the index of target letter in alphabet be i. So start checking from index i+1.
3. For wrapping around, always take mod(i+1, 26)
4. If found in the list, return the letter
'''
def search(chars, target):
'''
1. Binary Search method implementation.
2. Initialize the leftmost and rightmost search indices.
3. Input list must be sorted, if the letter at the middle index of the leftmost and rightmost indices is greater than the target letter,
- then the target letter must be within the leftmost index to the previous of middle postion
4. If the value at the middle index of the list is less than the target,
- then the target letter must be within the next of the middle index to the rightmost index
5. Otherwise, the middle position contains the target, return the index.
6. Update the leftmost and rightmost indices accordingly until target is reached.
'''
L = 0 #initially set the leftmost search index and 0
R = len(chars) - 1 #and rightmost search index as len(chars)-1
while L <= R: #check all the indices from the leftmost position to the rightmost one
m = (L + R) // 2 #compare the target to the letter in the middle of the two positions
if chars[m] < target:
L = m + 1 #if target is greater than current value, start checking from the next index
elif chars[m] > target:
R = m - 1 #if target is less than current value, check upto the previous index
else:
return m #current index holds the target, return the index
return -1 #target not found
alphabet='abcdefghijklmnopqrstuvwxyz' #store all the letters in of the alphabet in a string
indx = alphabet.index(target) #index of target letter in alphabet
target = alphabet[(indx+1)%26] #starting index of finding the next minimum letter, for wrapping around mod by 26
letters = sorted(letters) #for binary search, list must be sorted
while True:
indx=(indx+1)%26
target = alphabet[indx] #getting the letter of the next index
in_letters = search(letters, target)#using binary search to check whether this letter is in the list letters
if not in_letters == -1: #this letter is in list letters
return target #return the letter | find-smallest-letter-greater-than-target | Python3 Simple Approach with Explanation and Comments | bPapan | 0 | 28 | find smallest letter greater than target | 744 | 0.448 | Easy | 12,241 |
https://leetcode.com/problems/find-smallest-letter-greater-than-target/discuss/1106310/Python-simple-solution | class Solution:
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
letters = list(set(letters))
ind = "abcdefghijklmnopqrstuvwxyz"
lis=[-1 for i in range(26)]
for i in range(len(letters)):
lis[ord(letters[i])-ord('a')] = 1
for i in range(ord(target)-ord('a')+1,26):
if lis[i] != -1:
return ind[i]
return ind[lis.index(1)] | find-smallest-letter-greater-than-target | Python simple solution | abhisek_ | 0 | 35 | find smallest letter greater than target | 744 | 0.448 | Easy | 12,242 |
https://leetcode.com/problems/find-smallest-letter-greater-than-target/discuss/1032349/Faster-than-95.99-of-Python-3-online-submissions-using-BINARY-SEARCH-ONLY | class Solution:
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
arr=letters
ele=target
res=-1
low=0
high=len(arr)-1
if ele>=arr[high]:
return arr[low]
if ele<arr[low]:
res=arr[low]
while low<=high:
mid=low+(high-low)//2
if arr[mid]==ele:
low=mid+1
elif arr[mid]<ele:
low=mid+1
elif arr[mid]>ele:
res=arr[mid]
high=mid-1
return res | find-smallest-letter-greater-than-target | Faster than 95.99% of Python 3 online submissions using BINARY SEARCH ONLY | sr_snehil | 0 | 35 | find smallest letter greater than target | 744 | 0.448 | Easy | 12,243 |
https://leetcode.com/problems/find-smallest-letter-greater-than-target/discuss/1018576/python3 | class Solution:
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
if letters[-1] <= target :
return letters[0]
start = 0
end = len(letters)-1
boundary = -1
while start <= end :
mid = start + (end-start)//2
if letters[mid] > target :
end = mid - 1
boundary = letters[mid]
else :
start = mid + 1
return boundary | find-smallest-letter-greater-than-target | python3 | ankur1801 | 0 | 39 | find smallest letter greater than target | 744 | 0.448 | Easy | 12,244 |
https://leetcode.com/problems/find-smallest-letter-greater-than-target/discuss/867845/Python-Simple-Python-Solution-Using-Two-Approach | class Solution:
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
low = 0
high = len(letters) - 1
while low < high:
mid = ( low + high ) //2
if letters[mid] <= target:
low = mid + 1
else:
high = mid
if letters[low] > target:
return letters[low]
else:
return letters[0] | find-smallest-letter-greater-than-target | [ Python ] ✅✅ Simple Python Solution Using Two Approach🥳✌👍 | ASHOK_KUMAR_MEGHVANSHI | 0 | 70 | find smallest letter greater than target | 744 | 0.448 | Easy | 12,245 |
https://leetcode.com/problems/find-smallest-letter-greater-than-target/discuss/867845/Python-Simple-Python-Solution-Using-Two-Approach | class Solution:
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
for i in letters:
if i>target:
return i
if letters[-1]<=target:
return letters[0] | find-smallest-letter-greater-than-target | [ Python ] ✅✅ Simple Python Solution Using Two Approach🥳✌👍 | ASHOK_KUMAR_MEGHVANSHI | 0 | 70 | find smallest letter greater than target | 744 | 0.448 | Easy | 12,246 |
https://leetcode.com/problems/find-smallest-letter-greater-than-target/discuss/772522/Simple-Python-Solution | class Solution:
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
letrs = [ord(l) for l in letters]
trgt = ord(target)
found=False
i=0
while i < len(letrs) and not found:
if letrs[i]>trgt:
found=True
else:
i+=1
return letters[i] if found else letters[0] | find-smallest-letter-greater-than-target | Simple Python Solution | bharatgg | 0 | 37 | find smallest letter greater than target | 744 | 0.448 | Easy | 12,247 |
https://leetcode.com/problems/find-smallest-letter-greater-than-target/discuss/1848926/3-Lines-Python-Solution-oror-80-Faster-oror-Memory-less-than-97 | class Solution:
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
alphas = [chr(i) for i in range(97,123)]*2 ; idx = alphas.index(target)+1
for i in range(idx, len(alphas)):
if alphas[i] in letters: return alphas[i] | find-smallest-letter-greater-than-target | 3-Lines Python Solution || 80% Faster || Memory less than 97% | Taha-C | -1 | 26 | find smallest letter greater than target | 744 | 0.448 | Easy | 12,248 |
https://leetcode.com/problems/find-smallest-letter-greater-than-target/discuss/1848926/3-Lines-Python-Solution-oror-80-Faster-oror-Memory-less-than-97 | class Solution:
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
for letter in letters:
if letter > target: return letter
return letters[0] | find-smallest-letter-greater-than-target | 3-Lines Python Solution || 80% Faster || Memory less than 97% | Taha-C | -1 | 26 | find smallest letter greater than target | 744 | 0.448 | Easy | 12,249 |
https://leetcode.com/problems/find-smallest-letter-greater-than-target/discuss/1526768/Python-Pseudo-O(lg-n)-solution-using-Binary-Search | class Solution:
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
start, end = 0, len(letters)-1
while start<=end:
mid = (start+end)//2
if ord(letters[mid])<ord(target): start = mid+1
elif ord(letters[mid])>ord(target): end = mid-1
else: break
while mid<len(letters)-1 and letters[mid]==letters[mid+1]: mid+=1 #For preventing same letter problems
return letters[(mid+1)%len(letters)] if (ord(letters[mid])<ord(target) or letters[mid]==target) else letters[mid] | find-smallest-letter-greater-than-target | Python Pseudo O(lg n) solution using Binary Search | abrarjahin | -1 | 84 | find smallest letter greater than target | 744 | 0.448 | Easy | 12,250 |
https://leetcode.com/problems/find-smallest-letter-greater-than-target/discuss/336477/Solution-in-Python-3 | class Solution:
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
t, n = ord(target), list(map(ord,letters))
if t >= max(n): return chr(min(n))
d = {c : i for i,c in enumerate(n) if c > t}
return letters[d[min(d)]]
- Python 3
- Junaid Mansuri | find-smallest-letter-greater-than-target | Solution in Python 3 | junaidmansuri | -3 | 325 | find smallest letter greater than target | 744 | 0.448 | Easy | 12,251 |
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/2260902/94-FASTER-97-Space-Efficient-Python-solutions-Different-approaches | class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
cur = 0
dp0 = cost[0]
if len(cost) >= 2:
dp1 = cost[1]
for i in range(2, len(cost)):
cur = cost[i] + min(dp0, dp1)
dp0 = dp1
dp1 = cur
return min(dp0, dp1) | min-cost-climbing-stairs | 94% FASTER 97% Space Efficient Python solutions Different approaches | anuvabtest | 34 | 2,300 | min cost climbing stairs | 746 | 0.625 | Easy | 12,252 |
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/2260902/94-FASTER-97-Space-Efficient-Python-solutions-Different-approaches | class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
if not cost:
return 0
cur=0
dp0=cost[0]
if len(cost) >= 2:
dp1=cost[1]
for i in range(2, len(cost)):
cur=cost[i] + min(dp0,dp1)
dp0=dp1
dp1=cur
return min(dp0,dp1) | min-cost-climbing-stairs | 94% FASTER 97% Space Efficient Python solutions Different approaches | anuvabtest | 34 | 2,300 | min cost climbing stairs | 746 | 0.625 | Easy | 12,253 |
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/2260902/94-FASTER-97-Space-Efficient-Python-solutions-Different-approaches | class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
for i in range(len(cost) - 3, -1, -1):
cost[i] += min(cost[i+1], cost[i+2])
return min(cost[0], cost[1]) | min-cost-climbing-stairs | 94% FASTER 97% Space Efficient Python solutions Different approaches | anuvabtest | 34 | 2,300 | min cost climbing stairs | 746 | 0.625 | Easy | 12,254 |
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/2260902/94-FASTER-97-Space-Efficient-Python-solutions-Different-approaches | class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
if not cost:return 0
cur=0
dp0=cost[0]
dp1=cost[1]
for i in range(2, len(cost)):
cur=cost[i] + min(dp0,dp1)
dp0=dp1
dp1=cur
return min(dp0,dp1) | min-cost-climbing-stairs | 94% FASTER 97% Space Efficient Python solutions Different approaches | anuvabtest | 34 | 2,300 | min cost climbing stairs | 746 | 0.625 | Easy | 12,255 |
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/530398/EASY-Python-Solution-100-faster-and-less-memory-three-simple-lines | class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
# Loop through every cost after the first two steps
for i in range(2, len(cost)):
# Update the cheapest cost to step here
cost[i] += min(cost[i-2], cost[i-1])
# Cheapest cost of the last two steps is the answer
return min(cost[len(cost)-2], cost[len(cost)-1]) | min-cost-climbing-stairs | EASY Python Solution 100% faster and less memory, three simple lines | jhacker | 23 | 1,800 | min cost climbing stairs | 746 | 0.625 | Easy | 12,256 |
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/2519502/Very-Easy-oror-0-ms-oror-100-oror-Fully-Explained-oror-C%2B%2B-Java-Python-JS-Python3-oror-DP | class Solution(object):
def minCostClimbingStairs(self, cost):
# Traverse all stairs through the loop...
for idx in range(2,len(cost)):
# Recurrence formula: cost[idx] = math.min(cost[idx - 1], cost[idx - 2]) + cost[idx];
# Get the minimum cost that will be counted to reach the idx-th step...
cost[idx] += min(cost[idx-1], cost[idx-2])
return min(cost[-2], cost[-1]) | min-cost-climbing-stairs | Very Easy || 0 ms || 100% || Fully Explained || C++, Java, Python, JS, Python3 || DP | PratikSen07 | 10 | 409 | min cost climbing stairs | 746 | 0.625 | Easy | 12,257 |
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/394083/Four-Solutions-in-Python-3-(DP)-(Bottom-Up-and-Top-Down) | class Solution:
def minCostClimbingStairs(self, c: List[int]) -> int:
c1, c2 = c[0], c[1]
for i in range(2,len(c)): c1, c2 = c2, c[i] + min(c1,c2)
return min(c1, c2) | min-cost-climbing-stairs | Four Solutions in Python 3 (DP) (Bottom Up and Top Down) | junaidmansuri | 10 | 1,300 | min cost climbing stairs | 746 | 0.625 | Easy | 12,258 |
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/394083/Four-Solutions-in-Python-3-(DP)-(Bottom-Up-and-Top-Down) | class Solution:
def minCostClimbingStairs(self, c: List[int]) -> int:
c1, c2 = c[-2], c[-1]
for i in range(len(c)-3,-1,-1): c1, c2 = c[i] + min(c1,c2), c1
return min(c1, c2) | min-cost-climbing-stairs | Four Solutions in Python 3 (DP) (Bottom Up and Top Down) | junaidmansuri | 10 | 1,300 | min cost climbing stairs | 746 | 0.625 | Easy | 12,259 |
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/394083/Four-Solutions-in-Python-3-(DP)-(Bottom-Up-and-Top-Down) | class Solution:
def minCostClimbingStairs(self, c: List[int]) -> int:
L, _ = len(c), c.append(0)
def MinCost(i):
if i < 2: return c[i]
return c[i] + min(MinCost(i-1),MinCost(i-2))
return MinCost(L) | min-cost-climbing-stairs | Four Solutions in Python 3 (DP) (Bottom Up and Top Down) | junaidmansuri | 10 | 1,300 | min cost climbing stairs | 746 | 0.625 | Easy | 12,260 |
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/394083/Four-Solutions-in-Python-3-(DP)-(Bottom-Up-and-Top-Down) | class Solution:
def minCostClimbingStairs(self, c: List[int]) -> int:
DP, L, _ = c[:2] + [-1]*len(c), len(c), c.append(0)
def MinCost(i):
if DP[i] != -1: return DP[i]
DP[i] = c[i] + min(MinCost(i-1),MinCost(i-2))
return DP[i]
return MinCost(L)
- Junaid Mansuri
(LeetCode ID)@hotmail.com | min-cost-climbing-stairs | Four Solutions in Python 3 (DP) (Bottom Up and Top Down) | junaidmansuri | 10 | 1,300 | min cost climbing stairs | 746 | 0.625 | Easy | 12,261 |
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/1695178/Python-Simple-Python-Solution-Using-Dynamic-Programming-!!-O(N) | class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
if len(cost)==2:
return min(cost)
dp=[0]*len(cost)
dp[0]=cost[0]
dp[1]=cost[1]
for i in range(2,len(cost)):
dp[i]=min(dp[i-1],dp[i-2])+cost[i]
return min(dp[-1],dp[-2]) | min-cost-climbing-stairs | [ Python ] ✅✅ Simple Python Solution Using Dynamic Programming !! O(N) 🥳✌👍 | ASHOK_KUMAR_MEGHVANSHI | 6 | 302 | min cost climbing stairs | 746 | 0.625 | Easy | 12,262 |
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/1541553/Python-Easy-to-Understand-Solution | class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
for i in range(2, len(cost)):
cost[i] += min(cost[i-2], cost[i-1])
return min(cost[-2], cost[-1]) | min-cost-climbing-stairs | Python Easy to Understand Solution | aaffriya | 5 | 338 | min cost climbing stairs | 746 | 0.625 | Easy | 12,263 |
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/1546085/Short-python-solution-with-comments-O(n)-time-and-O(1)-space. | class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
# The problem description tells us our staircase is at least 2 steps long.
# a and b track the most cost-effective paths to the last 2 steps we looked at.
# Let's look at the first 2 steps of our staircase:
a, b = cost[0], cost[1]
# We update a and b until we reach the end of the staircase:
for i in range(2, len(cost)):
# c is the cost of the current step + the smaller of:
# a - mininum cost of reaching two steps behind it.
# b - minimum cost of reaching one step behind it.
# By comparing the two preceeding steps, we can be sure we're taking the shortest path.
# This is because we can only reach the current step from either preceeding step. Whichever one is less costly is always the best.
c = cost[i] + min(a, b)
# Before we start the next iteration of our loop, we need to update the steps preceeding our next step:
# a - we are no longer concerned about this step, because it is out of our range now. We can't jump 3 steps.
# b - previously one step away, now two steps away, so still in our range.
# c - previously the current step, now one step away.
a, b = b, c
# Once we've looked at all of the steps, a and b represent the most cost-effective ways to get to the last two steps.
# We choose the smaller one and finish climbing the stairs.
return min(a, b) | min-cost-climbing-stairs | Short python solution with comments, O(n) time and O(1) space. | imterrible | 4 | 345 | min cost climbing stairs | 746 | 0.625 | Easy | 12,264 |
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/1546085/Short-python-solution-with-comments-O(n)-time-and-O(1)-space. | class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
for i in range(2, len(cost)):
cost[i] = cost[i] + min(cost[i-1], cost[i-2])
return min(cost[-1], cost[-2]) | min-cost-climbing-stairs | Short python solution with comments, O(n) time and O(1) space. | imterrible | 4 | 345 | min cost climbing stairs | 746 | 0.625 | Easy | 12,265 |
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/1832953/Python3-Dynamic-programming | class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
"""
[10, 15, 20]
10 15
"""
n = len(cost)
dp = [0] * n
dp[0] = cost[0]
dp[1] = cost[1]
for i in range(2,n):
dp[i] = min(dp [i-2], dp[i-1]) + cost[i]
return min(dp[-1], dp[n - 2]) | min-cost-climbing-stairs | [Python3] Dynamic programming | zhanweiting | 3 | 193 | min cost climbing stairs | 746 | 0.625 | Easy | 12,266 |
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/1447910/python3-solution-dp | class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
prev1=cost[0]
prev2=cost[1]
for i in range(2,len(cost)):
temp=cost[i]+min(prev1,prev2)
prev1=prev2
prev2=temp
return min(prev2,prev1) | min-cost-climbing-stairs | python3 solution dp | abhi_n_001 | 3 | 401 | min cost climbing stairs | 746 | 0.625 | Easy | 12,267 |
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/2824595/oror-O(1)-space-oror-EASIEST-and-SHORTEST-solution-oror | class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
a=b=0
for c in cost:a,b=b,min(a,b)+c
return min(a,b) | min-cost-climbing-stairs | || O(1) space || EASIEST and SHORTEST solution || | m9810223new1 | 2 | 121 | min cost climbing stairs | 746 | 0.625 | Easy | 12,268 |
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/2695518/O(1)-Space-O(n)-Time-DP-Solution | class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:dyan
a = cost[0]
b = cost[1]
#This is the same as:
#memo[0] = cost[0]
#memo[1] = cost[1]
cost.append(0) #For some reason, we want to get to the last index + 1, which is free. This is the same as appending a 0 cost step.
for i in range(2, len(cost)):
c = min(a, b) + cost[i] #This is the same as memo[i] = min(memo[i-1] + memo[i-2]) + cost[i].
a = b #Shift variables over 1
b = c #Shift variables over 1
#Example
#[10, 15, 20, 15, 30]
# a, b, c
# a, b, c
# a, b, c
return c | min-cost-climbing-stairs | O(1) Space O(n) Time DP Solution | mephiticfire | 2 | 524 | min cost climbing stairs | 746 | 0.625 | Easy | 12,269 |
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/2261130/python3-or-explained-or-very-easy-or-dp | class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
n = len(cost)
if n==2:
return min(cost) # minimum of two elements as n == 2
dp = [0]*n
dp[0], dp[1] = cost[0], cost[1] # initializing the dp
for i in range(2, n):
dp[i] = min(dp[i-1], dp[i-2]) + cost[i] # minimum steps to reach index i
return min(dp[-1], dp[-2]) # as given you can climb one or two steps | min-cost-climbing-stairs | python3 | explained | very easy | dp | H-R-S | 2 | 138 | min cost climbing stairs | 746 | 0.625 | Easy | 12,270 |
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/2260874/Python-Fibonacci-it-is.-98.19 | class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
if len(cost) < 3:
return min(cost)
dp = [0]*(len(cost)+1)
dp[0] = cost[0]
dp[1] = cost[1]
for i in range(2, len(cost)):
dp[i] = cost[i] + min(dp[i-1],dp[i-2])
return min(dp[-2],dp[-3]) | min-cost-climbing-stairs | [Python] Fibonacci it is. 98.19% | justinjia0201 | 2 | 370 | min cost climbing stairs | 746 | 0.625 | Easy | 12,271 |
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/1960654/Simple-python3-dp-solution | class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
n = len(cost)
dp = [0]*(n)
dp[0] = cost[0]
dp[1] = cost[1]
for i in range(2, n):
dp[i] = cost[i] + min(dp[i-1], dp[i-2])
return min(dp[n-2], dp[n-1]) | min-cost-climbing-stairs | Simple python3 dp solution | codesankalp | 2 | 63 | min cost climbing stairs | 746 | 0.625 | Easy | 12,272 |
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/1216127/Python-Recursive-Solution-and-Bottom-Up-Solution | class Solution:
def helper(self, cost):
# Base Condition
if len(cost)==1 or len(cost)==2:
return cost[0]
# Choices
return min(cost[0] + self.helper(cost[1:]), cost[0] + self.helper(cost[2:]) )
def minCostClimbingStairs(self, cost: List[int]) -> int:
return min(self.helper(cost), self.helper(cost[1:])) | min-cost-climbing-stairs | Python Recursive Solution & Bottom Up Solution | paramvs8 | 2 | 145 | min cost climbing stairs | 746 | 0.625 | Easy | 12,273 |
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/1027451/Simple-and-easy | class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
dp=[0]*(len(cost))
dp[0],dp[1]=cost[0],cost[1]
for i in range(2,len(cost)):
dp[i]=min(dp[i-1]+cost[i],dp[i-2]+cost[i])
return min(dp[-1],dp[-2]) | min-cost-climbing-stairs | Simple and easy | thisisakshat | 2 | 222 | min cost climbing stairs | 746 | 0.625 | Easy | 12,274 |
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/452506/Beats-97-in-run-time-and-100-in-memory. | class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
for i in range(0,len(cost) -2):
cost[i+2] += min(cost[i], cost[i+1])
return min(cost[-2], cost[-1]) | min-cost-climbing-stairs | Beats 97% in run time and 100% in memory. | sudhirkumarshahu80 | 2 | 322 | min cost climbing stairs | 746 | 0.625 | Easy | 12,275 |
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/2465942/Python-(Simple-Solution-and-Beginner-Friendly) | class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
cost.append(0)
for i in range(len(cost)-3, -1, -1):
cost[i]+= min(cost[i+1], cost[i+2])
return min(cost[0], cost[1]) | min-cost-climbing-stairs | Python (Simple Solution and Beginner-Friendly) | vishvavariya | 1 | 87 | min cost climbing stairs | 746 | 0.625 | Easy | 12,276 |
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/2323337/Top-to-Bottom-oror-Bottom-to-Top-Approach-oror-Two-way-Solutions | class Solution(object):
def minCostClimbingStairs(self, cost):
"""
:type cost: List[int]
:rtype: int
"""
for i in range(2,len(cost)):
cost[i] = min(cost[i-1],cost[i-2])+cost[i]
return min(cost[-1],cost[-2]) | min-cost-climbing-stairs | Top-to-Bottom || Bottom-to-Top Approach || Two way Solutions | mridulbhas | 1 | 150 | min cost climbing stairs | 746 | 0.625 | Easy | 12,277 |
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/2323337/Top-to-Bottom-oror-Bottom-to-Top-Approach-oror-Two-way-Solutions | class Solution(object):
def minCostClimbingStairs(self, cost):
"""
:type cost: List[int]
:rtype: int
"""
#Top-to-Bottom Approach
dp = [-1]*(len(cost))
return min(self.topToBottom(cost,dp,len(cost)-1),self.topToBottom(cost,dp,len(cost)-2))
def topToBottom(self,cost,dp,n):
if(n == 0 or n ==1):
return cost[n]
elif(dp[n]!=-1):
return dp[n]
dp[n] = cost[n]+min(self.topToBottom(cost,dp,n-1),self.topToBottom(cost,dp,n-2))
return dp[n] | min-cost-climbing-stairs | Top-to-Bottom || Bottom-to-Top Approach || Two way Solutions | mridulbhas | 1 | 150 | min cost climbing stairs | 746 | 0.625 | Easy | 12,278 |
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/2261150/Python3-DP-solution-top-down-approach | class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
dp, length = {}, len(cost)
def getResult(index):
if index >= length:
return 0
if index in dp:
return dp[index]
dp[index] = cost[index] + min(getResult(index+1), getResult(index+2))
return dp[index]
getResult(0)
return min(dp[0], dp[1]) | min-cost-climbing-stairs | 📌 Python3 DP solution top down approach | Dark_wolf_jss | 1 | 5 | min cost climbing stairs | 746 | 0.625 | Easy | 12,279 |
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/2087773/Python3-O(N)-oror-O(1)-72ms-62.89 | class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
return self.solTwo(cost)
# return self.solOneByDpList(cost)
# O(n) || O(1)
# 72ms 62.89%
def solTwo(self, cost):
if not cost:
return 0
current, next = 0, 0
for i in reversed(range(len(cost))):
current, next = next, min(next, current) + cost[i]
return min(current, next)
# O(N) || O(N)
# 104ms 21.83%
def solOneByDpList(self, cost):
if not cost:
return 0
dp = [0] * len(cost)
dp[0] = cost[0]
if len(cost) > 1:
dp[1] = cost[1]
for i in range(2, len(cost)):
dp[i] = cost[i] + min(dp[i-2], dp[i-1])
return min(dp[-2], dp[-1]) | min-cost-climbing-stairs | Python3 O(N) || O(1) 72ms 62.89% | arshergon | 1 | 87 | min cost climbing stairs | 746 | 0.625 | Easy | 12,280 |
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/2058908/Python3-easy-understanding | class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
f = [0]*len(cost)
f[0] = cost[0]
f[1] = cost[1]
for i in range(2, len(cost)):
f[i] = min(f[i-2]+cost[i], f[i-1]+cost[i])
return min(f[-1], f[-2]) | min-cost-climbing-stairs | Python3 easy understanding | Shirley_lx | 1 | 76 | min cost climbing stairs | 746 | 0.625 | Easy | 12,281 |
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/1885895/Python | class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
a0=cost[0]
a1=cost[1]
for i in range(2, len(cost)):
a0, a1=a1, cost[i]+min(a0, a1)
return min(a0,a1) | min-cost-climbing-stairs | Python | zouhair11elhadi | 1 | 165 | min cost climbing stairs | 746 | 0.625 | Easy | 12,282 |
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/1709240/Python-Easy-O(1)-Space-Complexity-Solution | class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
i2 = 0
i1 = 0
for i in range(2, len(cost)+1):
i2, i1 = i1, min(i1 + cost[i-1],i2+cost[i-2])
return i1 | min-cost-climbing-stairs | Python Easy O(1) Space Complexity Solution | atiq1589 | 1 | 135 | min cost climbing stairs | 746 | 0.625 | Easy | 12,283 |
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/1565669/Python3-Solution-or-DP-Solution | class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
n = len(cost)
for i in range(2,n):
cost[i] += min(cost[i-1],cost[i-2])
return min(cost[n-1],cost[n-2]) | min-cost-climbing-stairs | Python3 Solution | DP Solution | satyam2001 | 1 | 343 | min cost climbing stairs | 746 | 0.625 | Easy | 12,284 |
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/1478960/Python3-DP-solution | class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
if len(cost) == 0:
return 0
if len(cost) == 1:
return cost[0]
dp = [0]*len(cost)
dp[0] = cost[0]
dp[1] = cost[1]
i = 2
while i < len(cost):
dp[i] = cost[i] + min(dp[i-1], dp[i-2])
i += 1
return min(dp[len(cost)-1], dp[len(cost)-2]) | min-cost-climbing-stairs | Python3 DP solution | Janetcxy | 1 | 149 | min cost climbing stairs | 746 | 0.625 | Easy | 12,285 |
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/1367715/A-Bottom-Up-Dynamic-Programming-Solution-for-absolute-beginners | class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
if(len(cost) == 1):
return cost[0]
if(len(cost) == 2):
return min(cost[0], cost[1])
dp = [0]*len(cost)
dp[0] = cost[0]
dp[1] = cost[1]
for i in range(2, len(cost)):
dp[i] = min(dp[i-1] + cost[i], dp[i-2] + cost[i])
return min(dp[-1], dp[-2]) | min-cost-climbing-stairs | A Bottom Up Dynamic Programming Solution for absolute beginners | Amogh23 | 1 | 278 | min cost climbing stairs | 746 | 0.625 | Easy | 12,286 |
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/1363401/Python-Code-or-95-Faster-in-Time | class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
f,s=cost[0],cost[1]
for i in cost[2:]:
x=min(i+f,i+s)
f=s
s=x
return min(f,s) | min-cost-climbing-stairs | Python Code | 95% Faster in Time | rackle28 | 1 | 134 | min cost climbing stairs | 746 | 0.625 | Easy | 12,287 |
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/1257579/Python-Solution | class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
first = second = 0
for c in cost:
first, second = second, c + min(first, second)
return min(first, second) | min-cost-climbing-stairs | Python Solution | mariandanaila01 | 1 | 175 | min cost climbing stairs | 746 | 0.625 | Easy | 12,288 |
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/1221676/Python3-simple-solution-beats-97-users | class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
n = len(cost)
a = cost[1]
b = cost[0]
for i in range(2,n):
x = cost[i] + min(a, b)
b = a
a = x
return min(a, b) | min-cost-climbing-stairs | Python3 simple solution beats 97% users | EklavyaJoshi | 1 | 69 | min cost climbing stairs | 746 | 0.625 | Easy | 12,289 |
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/1012637/Python3-DP-Solution-or-Beats-97-or-Easy-to-understand | class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
n=len(cost)
dp=[0]*n
dp[0]=cost[0]
dp[1]=cost[1]
for i in range(2,n):
dp[i]=min(dp[i-1], dp[i-2])+cost[i]
return min(dp[n-1], dp[n-2]) | min-cost-climbing-stairs | [Python3] DP Solution | Beats 97% | Easy to understand | _vaishalijain | 1 | 260 | min cost climbing stairs | 746 | 0.625 | Easy | 12,290 |
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/493976/Python-52ms12.8MB-Solution-(92100) | class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
f0, f1 = cost[0], cost[1]
for i in range(2,len(cost)):
tmp = cost[i] + min(f0, f1)
f0 = f1
f1 = tmp
return min(f0, f1) | min-cost-climbing-stairs | Python 52ms/12.8MB Solution (92%/100%) | X_D | 1 | 208 | min cost climbing stairs | 746 | 0.625 | Easy | 12,291 |
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/2841965/Solution-version-01 | class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
self.cost = cost
one , two = cost[len(cost)-1] , 0
pointer = len(cost) - 2
while pointer > -1:
temp = min(cost[pointer] + one,cost[pointer] + two)
pointer -= 1
two = one
one = temp
return min(one,two) | min-cost-climbing-stairs | Solution version 01 | MahsanulNirjhor | 0 | 2 | min cost climbing stairs | 746 | 0.625 | Easy | 12,292 |
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/2818376/746.-Min-Cost-Climbing-Stairs-Solutionor-TC%3A-O(n)-or-SC%3A-O(1) | class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
n = len(cost)
if n <= 2:
return min(cost)
prev2 = cost[0]
prev = cost[1]
curr = 0
for i in range(2, n):
curr = min(prev2, prev) + cost[i]
prev2 = prev
prev = curr
return min(prev2, prev) | min-cost-climbing-stairs | 746. Min Cost Climbing Stairs Solution| TC: O(n) | SC: O(1) | khritish17 | 0 | 3 | min cost climbing stairs | 746 | 0.625 | Easy | 12,293 |
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/2818135/Python-Each-step-visualized-concise-single-screen-O(n)-time%2Bspace | class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
'''
The idea is, given we can take either 1 or 2 steps,
and the goal is to get to the step after last step
(as in, if cost in [10,15,20], we want to get to index 3, after 20)
We can start from 2 steps before step after last, and go backwards:
idx 0 1 2 3 4 5 6 7 8 9 10
cost [1, 100, 1, 1, 1, 100, 1, 1, 100, 1] 0 next-step OR next-next-step
mins [ 100 1 0 ] either 100+1 or 100+0
[ 2 100 1 0 ] either 1+100 or 1+1
[ 3 2 100 1 0 ] either 1+2 or 1+100
[ 102 3 2 100 1 0 ] either 100+3 or 100+3
[ 4 102 3 2 100 1 0 ] either 1+100 or 1+3
[ 5 4 102 3 2 100 1 0 ] either 1+4 or 1+102
[ 5 5 4 102 3 2 100 1 0 ] either 1+5 or 1+4
[ 105 5 5 4 102 3 2 100 1 0 ] either 100+5 or 100+5
[ 6 105 5 5 4 102 3 2 100 1 0 ] either 1+105 or 1+5
breaking down the problem to just the last 3 steps at a time
at each step, what is the minimum cost from current step, step+1 or step+2?
then at last, since we can start from either 0th or 1th step, return min of the 2
'''
cost.append(0)
for step in range(len(cost)-3, -1, -1):
cost[step] += min(cost[step+1], cost[step+2])
return min(cost[0], cost[1]) | min-cost-climbing-stairs | [Python] Each step visualized concise single screen O(n) time+space | graceiscoding | 0 | 1 | min cost climbing stairs | 746 | 0.625 | Easy | 12,294 |
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/2815330/Min-Cost-Climbing-Stairs-using-Dynamic-Programming | class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
n = len(cost)
def solve(n,cost,dp):
if n==1 or n==0:
return cost[n]
if dp[n] != -1:
return dp[n]
dp[n] = cost[n] + min(solve(n-1,cost,dp), solve(n-2,cost,dp))
return dp[n]
dp = [-1 for i in range(n+1)]
ans = min(solve(n-1,cost,dp), solve(n-2,cost,dp))
return ans | min-cost-climbing-stairs | Min Cost Climbing Stairs using Dynamic Programming | sarthakchawande14 | 0 | 2 | min cost climbing stairs | 746 | 0.625 | Easy | 12,295 |
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/2814037/Python-or-Simple-or-Recursion-or-DP | class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
cost = [0] + cost + [0]
dp = {}
def dfs(idx):
if idx<1: return 0
if dp.get(idx,None) is not None: return dp[idx]
dp[idx] = min(cost[idx] + dfs(idx-1), cost[idx] + dfs(idx-2))
return dp[idx]
return dfs(len(cost)-1) | min-cost-climbing-stairs | Python | Simple | Recursion | DP | girraj_14581 | 0 | 5 | min cost climbing stairs | 746 | 0.625 | Easy | 12,296 |
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/2812698/Python-solutionororEasy-to-Understand | class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
dp = [-1]*len(cost)
dp[0] = cost[0]
dp[1] = cost[1]
n = len(cost)
for i in range(2,n):
dp[i] = min(cost[i]+dp[i-1],cost[i]+dp[i-2])
return min(dp[n-1],dp[n-2]) | min-cost-climbing-stairs | Python solution||Easy to Understand | sajit9285 | 0 | 3 | min cost climbing stairs | 746 | 0.625 | Easy | 12,297 |
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/2790364/Bottom-Up-Approach-or-O(n)-time-O(1)-space | class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
cost.append(0)
for i in range(len(cost)-3, -1, -1):
cost[i] += min(cost[i+1], cost[i+2])
return min(cost[0], cost[1]) | min-cost-climbing-stairs | Bottom-Up Approach | O(n) time, O(1) space | wakadoodle | 0 | 7 | min cost climbing stairs | 746 | 0.625 | Easy | 12,298 |
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/2750659/DP-Short-Python-O(n)-time-and-no-extra-space-(re-use-existing-list) | class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
for i in range(2, len(cost)):
cost[i] += min(cost[i-1], cost[i-2])
return min(cost[-2:]) | min-cost-climbing-stairs | DP Short Python O(n) time and no extra space (re-use existing list) | MVR11 | 0 | 8 | min cost climbing stairs | 746 | 0.625 | Easy | 12,299 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.