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-minimum-in-rotated-sorted-array/discuss/1461222/Python-or-Beats-97.97-Solutions-or-Time%3A-log(n)-or-Explained | class Solution:
def findMin(self, nums: List[int]) -> int:
if len(nums)==1:
return nums[0]
if nums[0]<nums[-1]:
return nums[0]
n = len(nums)
st = 0
end = n-1
while(st<=end):
mid = (st+end)//2
if mid!=0 and nums[mid-1]>nums[mid]:
return nums[mid]
if mid!=n-1 and nums[mid]>nums[mid+1]:
return nums[mid+1]
if nums[st]>nums[mid]:
end = mid-1
else:
st = mid+1
return nums[-1]
``` | find-minimum-in-rotated-sorted-array | Python | Beats 97.97% Solutions | Time: log(n) | Explained | g-divyanshu | 1 | 228 | find minimum in rotated sorted array | 153 | 0.485 | Medium | 2,400 |
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/discuss/1350850/Python-Solution-Beats-99.59-of-Python-submissions | class Solution:
def findMin(self, arr: List[int]) -> int:
low,high=0,len(arr)-1
if arr[low]<=arr[high]:return arr[low]
while low<=high:
if low==high:return low
mid=(low+high)//2
if mid<high and arr[mid+1]<arr[mid]:return arr[mid+1]
if mid>low and arr[mid]<arr[mid-1]:return arr[mid]
if arr[mid]<arr[high]:high=mid-1
else:low=mid+1 | find-minimum-in-rotated-sorted-array | Python Solution Beats 99.59% of Python submissions | reaper_27 | 1 | 216 | find minimum in rotated sorted array | 153 | 0.485 | Medium | 2,401 |
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/discuss/1297873/Python-The-Good-The-Bad-The-Ugly-Solution | class Solution(object):
def findMin(self, nums):
left = 0;right=len(nums)-1
while left<right:
mid = (left+right)//2
if nums[mid]>nums[right]:
left=mid+1
else:
right=mid
return nums[left] | find-minimum-in-rotated-sorted-array | [Python] The Good, The Bad, The Ugly Solution | akashadhikari | 1 | 116 | find minimum in rotated sorted array | 153 | 0.485 | Medium | 2,402 |
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/discuss/1297873/Python-The-Good-The-Bad-The-Ugly-Solution | class Solution(object):
def findMin(self, nums):
return min(nums) | find-minimum-in-rotated-sorted-array | [Python] The Good, The Bad, The Ugly Solution | akashadhikari | 1 | 116 | find minimum in rotated sorted array | 153 | 0.485 | Medium | 2,403 |
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/discuss/1297873/Python-The-Good-The-Bad-The-Ugly-Solution | class Solution(object):
def findMin(self, nums):
for i in range(1,len(nums)):
if nums[i] < nums[i-1]:
return nums[i]
return nums[0] | find-minimum-in-rotated-sorted-array | [Python] The Good, The Bad, The Ugly Solution | akashadhikari | 1 | 116 | find minimum in rotated sorted array | 153 | 0.485 | Medium | 2,404 |
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/discuss/2847961/Python-Solution | class Solution:
def findMin(self, nums: List[int]) -> int:
return min(nums) | find-minimum-in-rotated-sorted-array | Python Solution | abdulrahmanphy64 | 0 | 1 | find minimum in rotated sorted array | 153 | 0.485 | Medium | 2,405 |
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/discuss/2844486/Python-Easy-Solution-Simple-to-understand.-Beats-99.43-and-96.15-in-memory. | class Solution:
def findMin(self, nums: List[int]) -> int:
l=nums[0]
for i in nums:
if i<l:
l=i
return l | find-minimum-in-rotated-sorted-array | Python Easy Solution, Simple to understand. Beats 99.43% and 96.15% in memory. | user7478F | 0 | 3 | find minimum in rotated sorted array | 153 | 0.485 | Medium | 2,406 |
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/discuss/2842868/41ms-95-or-Easy-and-Simple-to-Read-or-Binary-Search-Approach-or-O(logn)-time-or-O(1)-space | class Solution:
def findMin(self, nums: List[int]) -> int:
left = 0
right = len(nums) - 1
while left <= right:
mid = (left + right) >> 1
if nums[mid] > nums[-1]:
left = mid + 1
else:
right = mid -1
return nums[left] | find-minimum-in-rotated-sorted-array | 41ms 95% | Easy and Simple to Read | Binary Search Approach | O(logn) time | O(1) space | advanced-bencoding | 0 | 1 | find minimum in rotated sorted array | 153 | 0.485 | Medium | 2,407 |
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/discuss/2831309/Python-3-9-lines-simple-solution | class Solution:
def findMin(self, nums: List[int]) -> int:
prev_val = nums[0]
l, r = 0, len(nums) - 1
if nums[l] <= nums[r]: return nums[l]
while l < r:
m = (l + r) // 2
if nums[m] < prev_val: r = m
else: l = m + 1
prev_val = min(nums[m], prev_val)
return nums[l] | find-minimum-in-rotated-sorted-array | Python 3 - 9 lines - simple solution | noob_in_prog | 0 | 4 | find minimum in rotated sorted array | 153 | 0.485 | Medium | 2,408 |
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/discuss/2818580/Python-or-3-ways(OneLine-solution) | class Solution:
def findMin(self, nums: List[int]) -> int:
# 1. one line sorting
# since we know the smallest number is the first item in the sorted list
return sorted(nums)[0]
# 2. it should follow the raising order, therefore, once the previous one(nums[i]) is greater than latter one (nums[i+1]), the latter one would be the start of the sorting array.
for i in range(len(nums)-1):
if nums[i]< nums[i+1]:
pass
else:
return nums[i+1]
return nums[0]
# 3. typical binary search
n = len(nums)
l = 0
r = n-1
while l< r:
mid = (l+r)//2
if nums[mid] > nums[r]:
l = mid +1
elif nums[mid]< nums[r]:
r = mid
return nums[r] | find-minimum-in-rotated-sorted-array | Python | 3 ways(OneLine solution) | vinafu0305 | 0 | 4 | find minimum in rotated sorted array | 153 | 0.485 | Medium | 2,409 |
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/discuss/2809851/Easy-Python-solution-beats-97 | class Solution:
def findMin(self, nums: List[int]) -> int:
if nums[0] <= nums[-1]:
return nums[0]
l, r = 0, len(nums)-1
while True:
m = (l+r)//2
if nums[l] > nums[l+1]: #only remain 2 elements, 2nd is smaller than 1st element => Found rotation point
return nums[l+1]
if nums[m] < nums[l]: #Rotation pivot is on the left, case 1
r = m
elif nums[m] > nums[r]: #Rotation pivot is on the right, case 2
l = m | find-minimum-in-rotated-sorted-array | Easy Python solution beats 97% | dominhnhut01 | 0 | 3 | find minimum in rotated sorted array | 153 | 0.485 | Medium | 2,410 |
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/discuss/2801081/Minimum-in-Rotated-Sorted-Array | class Solution:
def findMin(self, nums: List[int]) -> int:
self.nums = nums
left = 0
right = len(nums) - 1
ans = nums[0]
if len(nums) == 0:
return 0
elif len(nums) == 1:
return nums[0]
while(left <= right ): #while the left index is less than the right index
if nums[left] < nums[right]: #how to know it is sorted( default = asc)
ans = min(ans, nums[left])
mid = (left + right) // 2 #floor division of left and right avg value
ans = min(ans, nums[mid])
if nums[left] <= nums[mid]: #it means the smallest value btw index left and mid is at index left
left = mid + 1
else:
right = mid - 1
return ans | find-minimum-in-rotated-sorted-array | Minimum in Rotated Sorted Array | AlhajiDot | 0 | 1 | find minimum in rotated sorted array | 153 | 0.485 | Medium | 2,411 |
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/discuss/2776165/Intuitive-Binary-Search-PYTHON3-SOLUTION-90-Faster | class Solution:
def findMin(self, nums: List[int]) -> int:
l, r = 0, len(nums) - 1
minimum = 999
if nums[-1] > nums[0]:
return nums[0]
while l <= r:
mid = (l + r) // 2
#mid is in left subarray
if nums[mid] >= nums[0]:
l = mid + 1
if nums[mid] < minimum:
minimum = nums[mid]
#mid is in right subarray
else:
r = mid - 1
if nums[mid] < minimum:
minimum = nums[mid]
return minimum if len(nums) >= 0 else -1 | find-minimum-in-rotated-sorted-array | Intuitive Binary Search - PYTHON3 SOLUTION 90% Faster | gjayakumar3 | 0 | 4 | find minimum in rotated sorted array | 153 | 0.485 | Medium | 2,412 |
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/discuss/2764882/Python-Binary-Search | class Solution:
def findMin(self, nums: List[int]) -> int:
N = len(nums)
left, right = 0 , len(nums) - 1
if len(nums) <= 3:
return min(nums)
while left<= right:
mid = (left + right ) // 2
if nums[left] < nums[right]:
return nums[left]
if nums[(mid + N-1)% N] > nums[mid] < nums[(mid +1)%N]:
return nums[mid]
if nums[mid] >= nums[left]:
left = mid +1
else:
right = mid -1 | find-minimum-in-rotated-sorted-array | Python Binary Search | vijay_2022 | 0 | 2 | find minimum in rotated sorted array | 153 | 0.485 | Medium | 2,413 |
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/discuss/2744726/Python3-Binary-Search-(with-comments) | class Solution:
def findMin(self, nums: List[int]) -> int:
l, r = 0, len(nums)-1
res = float('inf')
while l <= r:
mid = (l+r)//2
# left half sorted
if nums[l] <= nums[mid]:
res = min(res, nums[l])
l = mid+1
# right half sorted
else:
res = min(res, nums[mid])
r = mid-1
return res | find-minimum-in-rotated-sorted-array | Python3 Binary Search (with comments) | jonathanbrophy47 | 0 | 4 | find minimum in rotated sorted array | 153 | 0.485 | Medium | 2,414 |
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/discuss/2740850/Find-Minimum-in-Rotated-Sorted-Array-or-PYTHON-or-Accepted | class Solution:
def findMin(self, nums: List[int]) -> int:
l=0
h=len(nums)-1
while(l<h):
if nums[l] > nums[h]:
l+=1
else:
return nums[l]
return nums[l] | find-minimum-in-rotated-sorted-array | Find Minimum in Rotated Sorted Array | PYTHON | Accepted | saptarishimondal | 0 | 3 | find minimum in rotated sorted array | 153 | 0.485 | Medium | 2,415 |
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/discuss/2740038/Simple-Beginner-Friendly-Approach-or-One-Pass-or-no-built-in-function-or-O(LogN)-or-O(1) | class Solution:
def findMin(self, nums: List[int]) -> int:
left, right = 0, len(nums) - 1
while left <= right:
mid = (left + right) // 2
if left == mid:
return min(nums[left], nums[right])
elif nums[left] < nums[right]:
return nums[left]
elif nums[mid] > nums[left]:
left = mid + 1
elif nums[mid] < nums[right]:
right = mid | find-minimum-in-rotated-sorted-array | Simple Beginner Friendly Approach | One Pass | no built-in function | O(LogN) | O(1) | sonnylaskar | 0 | 3 | find minimum in rotated sorted array | 153 | 0.485 | Medium | 2,416 |
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/discuss/2730119/Binary-search-ffs | class Solution:
def findMin(self, nums: List[int]) -> int:
res = nums[0]
l, r = 0, len(nums) - 1
while l <= r:
# if already sorted
if nums[l] < nums[r]:
res = min(res, nums[l])
break
# if updates in l,r below break while -> this mid = res
mid = (l + r) // 2
res = min(res, nums[mid])
# left most portion
if nums[mid] >= nums[l]:
l = mid + 1
# eight most portion
else:
r = mid - 1
return res | find-minimum-in-rotated-sorted-array | 😂 Binary search ffs | meechos | 0 | 7 | find minimum in rotated sorted array | 153 | 0.485 | Medium | 2,417 |
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/discuss/2729360/Python-Binary-Search-Simple-When-Drawn-Out | class Solution:
def findMin(self, nums: List[int]) -> int:
''' 0 1 2 3 4
Let's say we have: [3,4,5,1,2]
L M R
Mid (5) is larger than Right (2) which means right portion includes a rotation
so set the new Left to the right of current Mid since it is large
and our target is the smallest number
3 4
[_,_,_,1,2]
L R
M
Mid (3) is not larger than Right (2) which means at least the range from
Mid to Right is sorted, which means at the very least that at most,
Mid is the current smallest number we know of, so we can set this
as the new Right and continue search leftwards
3
[_,_,_,1,_]
L
At this point L will no longer meet the while condition L < M and so exit while
and proceed to return the current leftmost/smallest number at position L
Which is 1
'''
L, R = 0, len(nums)-1
while L < R:
M = (L + R) // 2
# If the Mid number is larger than the Right number
# then it includes a rotation and we want to continue
# search in the right portion excluding the large Mid number
if nums[M] > nums[R]:
L = M + 1
# Otherwise, Mid number is at least smaller than the Right number
# so continue search in the left portion with the Mid as the new Right
# because at least it is the smaller of the two, and we're looking for smallest
else:
R = M
return nums[L] | find-minimum-in-rotated-sorted-array | [Python] Binary Search Simple When Drawn Out | graceiscoding | 0 | 8 | find minimum in rotated sorted array | 153 | 0.485 | Medium | 2,418 |
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/discuss/2728690/Python3-or-deque-solution | class Solution:
def findMin(self, nums: List[int]) -> int:
q = collections.deque()
q.extend(nums)
while q[0] > q[-1]:
_ = q.popleft()
return q[0] | find-minimum-in-rotated-sorted-array | Python3 | deque solution | puppydog91111 | 0 | 2 | find minimum in rotated sorted array | 153 | 0.485 | Medium | 2,419 |
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/discuss/2704222/Simplest-python3-solution | class Solution:
def findMin(self, nums: List[int]) -> int:
N = len(nums)
left = 0
right = N - 1
while left < right:
mid = left + (right - left) // 2
if nums[mid] <= nums[right]: # pre-rotated section
right = mid
else:
left = mid + 1
return nums[right] | find-minimum-in-rotated-sorted-array | Simplest python3 solution | sandeshnep | 0 | 2 | find minimum in rotated sorted array | 153 | 0.485 | Medium | 2,420 |
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/discuss/2672897/Simple-and-Straight-Forward-Solution | class Solution:
def findMin(self, nums: List[int]) -> int:
start = 0
end = len(nums)-1
while start<=end:
mid = start + (end-start)//2
if start==end:
return nums[start]
if nums[mid] >=nums[end]:
start = mid+1
else:
end = mid | find-minimum-in-rotated-sorted-array | Simple and Straight Forward Solution | anusha_anil | 0 | 29 | find minimum in rotated sorted array | 153 | 0.485 | Medium | 2,421 |
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/discuss/2670495/Python-Solution-or-Binary-search | class Solution:
def findMin(self, nums: List[int]) -> int:
left = 0
right = len(nums)-1
if len(nums) == 1:
return nums[0]
while right > left:
mid = (right + left)//2
if nums[mid] > nums[right]:
left = mid + 1
elif nums[mid] < nums[right]:
right = mid
ans = nums[left]
return ans | find-minimum-in-rotated-sorted-array | Python Solution | Binary search | maomao1010 | 0 | 45 | find minimum in rotated sorted array | 153 | 0.485 | Medium | 2,422 |
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/discuss/2656342/Python-Solution | class Solution:
def findMin(self, nums: List[int]) -> int:
res= nums[0]
l = 0
r = len(nums) - 1
while l <= r:
if nums[l] < nums[r]:
res = min(res, nums[l])
break
m = (l + r) // 2
res = min(res, nums[m])
if nums[m] >= nums[l]:
l = m + 1
else:
r = m - 1
return res | find-minimum-in-rotated-sorted-array | Python Solution | brianshen333 | 0 | 1 | find minimum in rotated sorted array | 153 | 0.485 | Medium | 2,423 |
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/discuss/2631925/Python-Solution-or-Binary-Search-or-99-Faster | class Solution:
def findMin(self, nums: List[int]) -> int:
n=len(nums)
low=0
high=n-1
while low<=high:
mid=(low+high)//2
if nums[mid]<nums[high]:
high=mid
else:
low=mid+1
return nums[high] | find-minimum-in-rotated-sorted-array | Python Solution | Binary Search | 99% Faster | Siddharth_singh | 0 | 7 | find minimum in rotated sorted array | 153 | 0.485 | Medium | 2,424 |
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/discuss/2470840/Python-Simple-Binary-Search-Solution-With-Comments | class Solution:
def findMin(self, nums: List[int]) -> int:
# Edge case #
if len(nums) == 0:
return 0
# Binary Search #
res = nums[0]
left = 0
right = len(nums) - 1
while left <= right:
# If the left value is less than the right value, then we have sorted array and just return the minimum value without doing binary search #
if nums[left] < nums[right]:
res = min(nums[left], res)
break
mid = (left + right)//2
res = min(res, nums[mid])
# We are in the left sorted portion, we need to search right if mid value is greater than left and the min values will be on the right side because array is rotated #
if nums[mid] >= nums[left]:
left = mid + 1
else:
right = mid - 1
return res | find-minimum-in-rotated-sorted-array | Python Simple Binary Search Solution With Comments | PythonicLava | 0 | 123 | find minimum in rotated sorted array | 153 | 0.485 | Medium | 2,425 |
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/discuss/2452165/easy-python-code-or-binary-search-or-O(log-n) | class Solution:
def findMin(self, nums: List[int]) -> int:
l,r = 0,len(nums)-1
if nums[l]<=nums[r]:
return nums[l]
while(l<=r):
if r-l <= 1:
if nums[l] <= nums[r]:
return nums[l]
else:
return nums[r]
m = (l+r)//2
if nums[l] < nums[m]:
l = m
else:
r = m | find-minimum-in-rotated-sorted-array | easy python code | binary search | O(log n) | dakash682 | 0 | 38 | find minimum in rotated sorted array | 153 | 0.485 | Medium | 2,426 |
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/discuss/2401410/Easy-to-understand-93-python3-iterative-solution | class Solution:
def findMin(self, nums: List[int]) -> int:
n = len(nums)
def binarySearchMin(lo, hi) -> int:
while lo <= hi:
mid = (lo+hi)//2
prev = (mid-1)%n
nxt = (mid+1)%n
if nums[mid] < nums[prev] and nums[mid] < nums[nxt]:
return nums[mid]
elif nums[mid] > nums[hi]:
lo = mid+1
else:
hi = mid-1
return nums[lo]
return binarySearchMin(0, n-1) | find-minimum-in-rotated-sorted-array | Easy to understand 93% python3 iterative solution | destifo | 0 | 8 | find minimum in rotated sorted array | 153 | 0.485 | Medium | 2,427 |
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/discuss/2393123/PYTHON3-Simple-Binary-Search-Approach-(To-get-min-and-max-element) | class Solution:
def findMin(self, nums: List[int]) -> int:
l = 0
r = len(nums)-1
while(l<=r):
mid = (l+r)//2
if(nums[mid]<nums[l]):
r=mid
elif(nums[mid]>nums[r]):
l=mid+1
else:
return nums[l]
return -1 | find-minimum-in-rotated-sorted-array | PYTHON3 - Simple Binary Search Approach (To get min and max element) | pkoder | 0 | 14 | find minimum in rotated sorted array | 153 | 0.485 | Medium | 2,428 |
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/discuss/2376771/40ms-Solution-in-3-Lines-that-Beat-96.14-So-Simple-No-Need-for-Explanation | class Solution:
def findMin(self, nums: List[int]) -> int:
for i in range(1, len(nums)):
if nums[i - 1] > nums[i]: return nums[i]
return nums[0] | find-minimum-in-rotated-sorted-array | 40ms Solution in 3 Lines that Beat 96.14%, So Simple No Need for Explanation | HappyLunchJazz | 0 | 32 | find minimum in rotated sorted array | 153 | 0.485 | Medium | 2,429 |
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/discuss/2375954/Python-3-or-2-Solutions-or-O(logn)-or-Simple-and-Fast | class Solution:
def findMin(self, nums: List[int]) -> int:
# T.C: O(n) S.C: O(1)
currmin = 99999
globalmin = 99999
for num in nums:
if num < currmin:
currmin = num
globalmin = min(currmin,globalmin)
return globalmin
# Modified Binary Search T.C: O(logn) S.C: O(1)
if len(nums) == 0:
return nums[0]
left = 0
right = len(nums)-1
if nums[left] < nums[right]:
return nums[left]
mid = len(nums)//2
if nums[mid] > nums[left]: # inflection point on right side
while nums[mid] < nums[mid+1]:
if nums[mid] > nums[mid+1]:
return nums[mid+1]
mid += 1
else:
while nums[left] < nums[mid+1]:
if nums[left] > nums[mid+1]:
return nums[mid+1]
left += 1 | find-minimum-in-rotated-sorted-array | Python 3 | 2 Solutions | O(logn) | Simple and Fast | chawlashivansh | 0 | 30 | find minimum in rotated sorted array | 153 | 0.485 | Medium | 2,430 |
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/discuss/2356223/Python-Simple-Python-Solution-Using-Binary-Search | class Solution:
def findMin(self, nums: List[int]) -> int:
low = 0
high = len(nums) - 1
while low < high:
mid = (low + high) // 2
if nums[mid] > nums[high]:
low = mid + 1
else:
high = mid
return nums[low] | find-minimum-in-rotated-sorted-array | [ Python ] ✅✅ Simple Python Solution Using Binary Search 🥳✌👍 | ASHOK_KUMAR_MEGHVANSHI | 0 | 94 | find minimum in rotated sorted array | 153 | 0.485 | Medium | 2,431 |
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/discuss/2351634/6-line-O(LogN)-optimised-binary-search-solution-with-explanation | class Solution:
def findMin(self, nums: List[int]) -> int:
"""
the array after rotation will have 2( or 1) incremental part(s)
on binary search it would be the process of finding the first m in the right part
- if m is the in the right part already, r = m
- if m is not in the right part (in the left part), l = m+1 -> until l is out of the left part
- then m will always be in the right part, by repeating m = (l+m)//2, m will be l
- in case there's only 1 incremental part, m will be reducing to 0
edge cases:
- length 1 nums: [1]
"""
l, r = 0, len(nums) - 1
while l < r:
m = (l+r) // 2
# m in the right incremental part
if nums[m] < nums[r]: r = m
# m in the left incremental part
else: l = m+1
return nums[l] | find-minimum-in-rotated-sorted-array | 6-line O(LogN) optimised binary search solution with explanation | zhenyulin | 0 | 31 | find minimum in rotated sorted array | 153 | 0.485 | Medium | 2,432 |
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/discuss/2350809/Python-Solution-or-Faster-than-88-or-Binary-Search | class Solution:
def findMin(self, nums: List[int]) -> int:
if len(nums) == 1:
return nums[0]
l = 0
r = len(nums) - 1
if nums[r] > nums[0]:
return nums[0]
while r >= l:
m = l + (r - l) // 2
if nums[m] > nums[m + 1]:
return nums[m + 1]
if nums[m - 1] > nums[m]:
return nums[m]
if nums[m] > nums[0]:
l = m + 1
else:
r = m - 1 | find-minimum-in-rotated-sorted-array | Python Solution | Faster than 88% | Binary Search | zip_demons | 0 | 34 | find minimum in rotated sorted array | 153 | 0.485 | Medium | 2,433 |
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/discuss/2314356/CPP-or-Java-or-Python-or-All-approaches-or-TC-O(n)-or | class Solution:
def findMin(self, nums: List[int]) -> int:
start, end = 0, len(nums)-1
while(start < end):
mid = start + (end - start)//2
if nums[start] < nums[end]: return nums[start]
elif nums[mid+1] < nums[mid]: return nums[mid+1]
elif nums[mid-1] > nums[mid]: return nums[mid]
elif nums[0] < nums[mid]: start = mid + 1
else: end = mid - 1
return nums[start] | find-minimum-in-rotated-sorted-array | CPP | Java | Python | All approaches | TC - O(n) | | devilmind116 | 0 | 31 | find minimum in rotated sorted array | 153 | 0.485 | Medium | 2,434 |
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/discuss/2258230/Python3-Clean-solution-with-comments | class Solution:
def findMin(self, nums: List[int]) -> int:
# Using Binary search concept...
low, high = 0, len(nums)-1
minv = inf
while low <= high:
mid = low + (high - low) // 2
minv = min(minv, nums[mid])
# Move on side where we can find smaller minv
if nums[high] < nums[mid]:
# minimum can be found on right side
low = mid + 1
else:
# minimum can be found on left side
high = mid - 1
return minv | find-minimum-in-rotated-sorted-array | [Python3] Clean solution with comments | Gp05 | 0 | 28 | find minimum in rotated sorted array | 153 | 0.485 | Medium | 2,435 |
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/discuss/2219077/Python-with-full-working-examples-explanation | class Solution: # we have to search for minimum element in a sorted array after rotation
def findMin(self, nums: List[int]) -> int: # Time: O(n) and Space:O(1)
res = nums[0]
l, r = 0, len(nums) - 1
while l <= r:
if nums[l] < nums[r]: # means the min element among the current array is at the left index
res = min(res, nums[l])
break
m = (l + r) // 2
res = min(res, nums[m])
if nums[m] >= nums[l]: # means from l to m elements are in increasing order
l = m + 1 # so, we need to search the right side
else: # if it's not in increasing order we need to search the left side
r = m - 1
return res | find-minimum-in-rotated-sorted-array | Python with full working examples explanation | DanishKhanbx | 0 | 21 | find minimum in rotated sorted array | 153 | 0.485 | Medium | 2,436 |
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/discuss/2181317/Python-O(Log-N)-very-simply-made-Binary-Search-solution-intuition-%2B-Code | class Solution:
def findMin(self, nums: List[int]) -> int:
res,left,right = 0,0,len(nums)-1
while(left <= right):
mid = (left + right) >> 1
if nums[mid] <= nums[mid-1]:
return nums[mid]
elif nums[mid] >= nums[right]:
left = mid + 1
else:
right = mid - 1 | find-minimum-in-rotated-sorted-array | Python O(Log N) very simply made Binary Search solution, intuition + Code | thota_datta | 0 | 87 | find minimum in rotated sorted array | 153 | 0.485 | Medium | 2,437 |
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/discuss/2031726/Python-w-Explanation-or-O(logn) | class Solution:
def findMin(self, nums: List[int]) -> int:
# Binary search
lo = 0
hi = len(nums) - 1
global_min = inf
while lo <= hi:
mid = (lo + hi) // 2
# Update minimum if needed
global_min = min(nums[mid], global_min)
# Case: if the pivot index is within the top half and the top half array is unsorted
# e.g. [4,5,6,7|mid|8,0,1,2]
if nums[mid] > nums[hi]:
lo = mid + 1
# Case: if the pivot index is within the top half but the top half of array is sorted
# e.g. [5,6,7,8|mid|0,1,2,3]
elif mid + 1 < len(nums) and nums[mid + 1] < nums[mid]:
return nums[mid + 1]
else:
hi = mid - 1
return global_min | find-minimum-in-rotated-sorted-array | Python w/ Explanation | O(logn) | user5622HA | 0 | 36 | find minimum in rotated sorted array | 153 | 0.485 | Medium | 2,438 |
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/discuss/2087295/Binary-Search-oror-Explained-oror-PYTHON | class Solution:
def findMin(self, a: List[int]) -> int:
def solve(l,h):
while l<h:
m=(l+h)//2
if a[m]<a[m-1]:
return a[m]
elif a[m]>a[h-1]:
l=m+1
elif a[m]<a[h-1]:
h=m
else:
if len(set(a[l:m+1]))==1:
return min(a[m],solve(m+1,h))
else:
return min(a[m],solve(l,m))
return a[min(l,len(a)-1)]
return solve(0,len(a)) | find-minimum-in-rotated-sorted-array-ii | Binary Search || Explained || PYTHON | karan_8082 | 2 | 69 | find minimum in rotated sorted array ii | 154 | 0.434 | Hard | 2,439 |
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/discuss/725072/Python3-binary-search | class Solution:
def findMin(self, nums: List[int]) -> int:
lo, hi = 0, len(nums)-1
while lo < hi:
mid = lo + hi >> 1
if nums[mid] < nums[hi]: hi = mid
elif nums[mid] == nums[hi]: hi -= 1 # duplicates
else: lo = mid + 1
return nums[lo] | find-minimum-in-rotated-sorted-array-ii | [Python3] binary search | ye15 | 1 | 76 | find minimum in rotated sorted array ii | 154 | 0.434 | Hard | 2,440 |
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/discuss/2842989/Python-Binary-Search. | class Solution:
def findMin(self, nums: List[int]) -> int:
new=self.removedupli(nums)
print(new)
res=self.binary(new)
return res
def binary(self,nums):
l,r,ans=0,len(nums)-1,nums[0]
while l<=r:
if nums[l]<nums[r]:
ans=min(ans,nums[l])
break
mid=l+(r-l)//2
ans=min(ans,nums[mid])
if nums[mid]>=nums[l]:
l=mid+1
else:
r=mid-1
return ans
def removedupli(self,items):
list1 = []
for i in items:
if i not in list1:
list1.append(i)
return list1 | find-minimum-in-rotated-sorted-array-ii | Python Binary Search. | jainatishay072 | 0 | 1 | find minimum in rotated sorted array ii | 154 | 0.434 | Hard | 2,441 |
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/discuss/2800483/Python3-solution | class Solution:
def findMin(self, nums: List[int]) -> int:
return(min(nums)) | find-minimum-in-rotated-sorted-array-ii | Python3 solution | SupriyaArali | 0 | 4 | find minimum in rotated sorted array ii | 154 | 0.434 | Hard | 2,442 |
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/discuss/2311632/Attention-Simplest-one-line-solution | class Solution:
def findMin(self, nums: List[int]) -> int:
return min(nums) | find-minimum-in-rotated-sorted-array-ii | Attention ⚠️⚠️⚠️⚠️ Simplest one line solution | Akash2907 | 0 | 59 | find minimum in rotated sorted array ii | 154 | 0.434 | Hard | 2,443 |
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/discuss/2150260/Python-easy-solution-with-explanation | class Solution:
def findMin(self, nums: List[int]) -> int:
# Go in loop from index 1 till end.
for i in range(1,len(nums)):
# Checking if 0th index element is greater than any other element in the list.
if(nums[0]>nums[i]):
# If yes then return that element.
return nums[i]
# Otherwise if all the loop ends and condition doesn't matches then return the 0th index element.
return nums[0] | find-minimum-in-rotated-sorted-array-ii | Python easy solution with explanation | yashkumarjha | 0 | 53 | find minimum in rotated sorted array ii | 154 | 0.434 | Hard | 2,444 |
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/discuss/1956511/Python-easy-to-read-and-understand-or-binary-search | class Solution:
def findMin(self, nums: List[int]) -> int:
n = len(nums)
if n == 1:
return nums[0]
start, end = 0, n-1
while start <= end:
mid = (start+end) // 2
prev, nxt = (mid-1+n)%n, (mid+1)%n
if nums[mid] < nums[prev] and nums[mid] < nums[nxt]:
return nums[mid]
if nums[mid] > nums[end]:
start = mid+1
else:
end = mid
return nums[mid] | find-minimum-in-rotated-sorted-array-ii | Python easy to read and understand | binary-search | sanial2001 | 0 | 70 | find minimum in rotated sorted array ii | 154 | 0.434 | Hard | 2,445 |
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/discuss/1956511/Python-easy-to-read-and-understand-or-binary-search | class Solution:
def findMin(self, nums: List[int]) -> int:
n = len(nums)
start, end = 0, n-1
while start <= end:
mid = (start+end) // 2
prev, nxt = (mid-1+n)%n, (mid+1)%n
if nums[mid] < nums[prev] and nums[mid] < nums[nxt]:
return nums[mid]
if nums[mid] > nums[end]:
start = mid+1
elif nums[mid] == nums[end]:
end = end-1
else:
end = mid
return nums[mid] | find-minimum-in-rotated-sorted-array-ii | Python easy to read and understand | binary-search | sanial2001 | 0 | 70 | find minimum in rotated sorted array ii | 154 | 0.434 | Hard | 2,446 |
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/discuss/1537405/Python-Solution-very-Simple-and-really-intuitive-with-explanation. | class Solution:
def findMin(self, nums: List[int]) -> int:
is_rotated = False # set up a variable which tells us that the array was rotated
for i in range(len(nums)-1): # loop through the array till one less than the last index
if nums[i] > nums[i+1]: # If element is greater than the next element
rotate = i+1 # rotate point = i+1
is_rotated = True
break
else:
is_rotated = False
if is_rotated == True:
return nums[i+1] # if rotated is true then return the element at the rotation point
else:
return nums[0] # Else return the element at the start of the array.
``` | find-minimum-in-rotated-sorted-array-ii | Python Solution very Simple and really intuitive with explanation. | anantinfinity9796 | 0 | 53 | find minimum in rotated sorted array ii | 154 | 0.434 | Hard | 2,447 |
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/discuss/1350957/Python-Solution-beats-99.48-Python-Submissons | class Solution:
def findMin(self, arr: List[int]) -> int:
low,high=0,len(arr)-1
if arr[0]<arr[-1]:return arr[0]
while low<=high:
if low==high:return arr[low]
mid=(low+high)//2
if arr[mid]==arr[low]==arr[high]:high-=1
elif arr[mid]==arr[high]:high=mid
elif mid<high and arr[mid]>arr[mid+1]:return arr[mid+1]
elif mid>low and arr[mid-1]>arr[mid]:return arr[mid]
elif arr[low]<arr[mid]:low=mid+1
elif arr[mid]<arr[high]:high=mid-1
else: low+=1
return arr[low] | find-minimum-in-rotated-sorted-array-ii | Python Solution beats 99.48% Python Submissons | reaper_27 | 0 | 95 | find minimum in rotated sorted array ii | 154 | 0.434 | Hard | 2,448 |
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/discuss/1181396/One-line-code-solution-for-all-4-questions | class Solution:
def search(self, nums: List[int], target: int) -> int:
return nums.index(target) if target in nums else -1 | find-minimum-in-rotated-sorted-array-ii | One line code solution for all 4 questions | easyz2016 | 0 | 45 | find minimum in rotated sorted array ii | 154 | 0.434 | Hard | 2,449 |
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/discuss/1181396/One-line-code-solution-for-all-4-questions | class Solution:
def search(self, nums: List[int], target: int) -> bool:
return (target in nums) | find-minimum-in-rotated-sorted-array-ii | One line code solution for all 4 questions | easyz2016 | 0 | 45 | find minimum in rotated sorted array ii | 154 | 0.434 | Hard | 2,450 |
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/discuss/1181396/One-line-code-solution-for-all-4-questions | class Solution:
def findMin(self, nums: List[int]) -> int:
return sorted(nums)[0] | find-minimum-in-rotated-sorted-array-ii | One line code solution for all 4 questions | easyz2016 | 0 | 45 | find minimum in rotated sorted array ii | 154 | 0.434 | Hard | 2,451 |
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/discuss/1181396/One-line-code-solution-for-all-4-questions | class Solution:
def findMin(self, nums: List[int]) -> int:
return sorted(nums)[0] | find-minimum-in-rotated-sorted-array-ii | One line code solution for all 4 questions | easyz2016 | 0 | 45 | find minimum in rotated sorted array ii | 154 | 0.434 | Hard | 2,452 |
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/discuss/1143757/Very-easy-solution-with-one-function | class Solution:
def findMin(self, nums: List[int]) -> int:
return min(nums) | find-minimum-in-rotated-sorted-array-ii | Very easy solution with one function | vashisht7 | 0 | 68 | find minimum in rotated sorted array ii | 154 | 0.434 | Hard | 2,453 |
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/discuss/957466/Simple-Python3-Solution-(Binary-Search) | class Solution:
def findMin(self, nums: List[int]) -> int:
if len(nums)==1:
return nums[0]
low,high = 0,len(nums)-1
if nums[low]<nums[high]:
return nums[low]
while low<=high:
while((low+1)<len(nums) and nums[low] == nums[low+1]):
low += 1
if(low==len(nums)-1):
return nums[low]
while((high-1)>=0 and nums[high] == nums[high-1]):
high -= 1
mid = low + (high-low)//2
if nums[mid]>nums[mid+1]:
return nums[mid+1]
if nums[mid]<nums[mid-1]:
return nums[mid]
if nums[mid]>nums[0]:
low = mid+1
else:
high = mid-1 | find-minimum-in-rotated-sorted-array-ii | Simple Python3 Solution (Binary Search) | swap2001 | 0 | 81 | find minimum in rotated sorted array ii | 154 | 0.434 | Hard | 2,454 |
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/discuss/759575/binary-search-with-special-handling-of-duplicated-values | class Solution:
def findMin(self, nums: List[int]) -> int:
start, end = 0, len(nums)-1
ans = float('inf')
while start <= end:
mid = (start+end)//2
# right part is sorted
if nums[mid] < nums[end]:
ans = min(ans, nums[mid])
end = mid-1
# left part is sorted
elif nums[mid] > nums[end]:
ans = min(ans, nums[start])
start = mid+1
# neither part is sorted for sure
else:
ans = min(ans, nums[mid])
end -= 1
return ans | find-minimum-in-rotated-sorted-array-ii | binary search with special handling of duplicated values | ytb_algorithm | 0 | 67 | find minimum in rotated sorted array ii | 154 | 0.434 | Hard | 2,455 |
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/discuss/1385553/Half-length-pass-97-speed | class Solution:
def findMin(self, nums: List[int]) -> int:
len_nums = len(nums)
len_nums1, len_nums2 = len_nums - 1, len_nums - 2
for i in range(len_nums // 2):
if nums[i] > nums[i + 1]:
return nums[i + 1]
if nums[len_nums2 - i] > nums[len_nums1 - i]:
return nums[len_nums1 - i]
return nums[0] | find-minimum-in-rotated-sorted-array-ii | Half-length pass, 97% speed | EvgenySH | -2 | 153 | find minimum in rotated sorted array ii | 154 | 0.434 | Hard | 2,456 |
https://leetcode.com/problems/min-stack/discuss/825972/Python-3-greater-91-faster-using-namedtuple | class MinStack:
stackWithMinElements = collections.namedtuple("stackWithMinElements", ("element", "minimum"))
def __init__(self):
self.stack : List[self.stackWithMinElements] = []
def push(self, x: int) -> None:
self.stack.append(self.stackWithMinElements(
x, min(x, self.getMin()) if len(self.stack)>0 else x))
def pop(self) -> None:
self.stack.pop()
def top(self) -> int:
return self.stack[-1].element
def getMin(self) -> int:
return self.stack[-1].minimum | min-stack | Python 3 -> 91% faster using namedtuple | mybuddy29 | 5 | 554 | min stack | 155 | 0.519 | Medium | 2,457 |
https://leetcode.com/problems/min-stack/discuss/825972/Python-3-greater-91-faster-using-namedtuple | class MinStack:
def __init__(self):
self.stack = []
self.count = 0
def push(self, val: int) -> None:
topMin = val
if self.count > 0:
topMin = min(self.stack[-1][1], val)
self.stack.append((val, topMin))
self.count += 1
def pop(self) -> None:
self.stack.pop()
self.count -= 1
def top(self) -> int:
return self.stack[-1][0]
def getMin(self) -> int:
return self.stack[-1][1] | min-stack | Python 3 -> 91% faster using namedtuple | mybuddy29 | 5 | 554 | min stack | 155 | 0.519 | Medium | 2,458 |
https://leetcode.com/problems/min-stack/discuss/1534170/python3-stack | class MinStack:
def __init__(self):
self.stack = []
def push(self, val: int) -> None:
if not self.stack:
self.stack.append((val,val)) # storing tuple of current value , min of curr and prev value of stack
else:
self.stack.append((val, min(val,self.stack[-1][1]))) # min of prev value & curr
def pop(self) -> None:
if self.stack:
self.stack.pop()
def top(self) -> int:
if self.stack:
return self.stack[-1][0] # curr value at 1st index of tuple
else:
return None
def getMin(self) -> int:
if not self.stack:
return None
return self.stack[-1][1] # min value at 2nd index of tuple
```
feel free to ask Q...
#happytohelpu | min-stack | python3 - stack | Shubham_Muramkar | 4 | 372 | min stack | 155 | 0.519 | Medium | 2,459 |
https://leetcode.com/problems/min-stack/discuss/2252739/Python-~99.9-Faster-with-Explanation-and-Comments | class MinStack:
def __init__(self):
self.stack = []
self.min_val = 0
def push(self, val: int) -> None:
#When empty, set first element as min_val
if not self.stack:
self.min_val = val
cur_min = self.getMin()
if val < cur_min:
self.min_val = val
#Append tuple of (value,minimum value at the time)
self.stack.append((val,self.min_val))
def pop(self) -> None:
self.stack.pop()
def top(self) -> int:
val, _ = self.stack[-1]
return val
def getMin(self) -> int:
#If empty, return default min_val
if not self.stack:
return self.min_val
#Unpack tuple into min_val
_, self.min_val = self.stack[-1]
return self.min_val | min-stack | Python ~99.9% Faster with Explanation and Comments | beochinh | 3 | 138 | min stack | 155 | 0.519 | Medium | 2,460 |
https://leetcode.com/problems/min-stack/discuss/1137100/Python3-Easy-Explanation-O(1)-Space-O(1)-Time | class MinStack:
def __init__(self):
self.Stack = []
self.minVal = None
def push(self, val: int) -> None:
if not self.Stack or self.minVal == None:
self.minVal = val
if not self.Stack:
self.Stack.append(val)
elif val < self.minVal:
self.Stack.append(2*val - self.minVal)
self.minVal = val
else:
self.Stack.append(val)
def pop(self) -> None:
if not self.Stack:
return
elif self.Stack[-1] < self.minVal:
self.minVal = 2*self.minVal - self.Stack[-1]
self.Stack.pop()
else:
self.Stack.pop()
def top(self) -> int:
if self.Stack[-1] > self.minVal :
return self.Stack[-1]
else:
return self.minVal
def getMin(self) -> int:
return self.minVal
# Your MinStack object will be instantiated and called as such:
# obj = MinStack()
# obj.push(val)
# obj.pop()
# param_3 = obj.top()
# param_4 = obj.getMin() | min-stack | Python3 - Easy Explanation, O(1) Space, O(1) Time | piyushagg19 | 2 | 547 | min stack | 155 | 0.519 | Medium | 2,461 |
https://leetcode.com/problems/min-stack/discuss/2812426/Python-Solution-With-Explanation | class MinStack:
def __init__(self):
self.stack = []
self.minimums = deque()
def push(self, val: int) -> None:
self.stack.append(val)
if self.minimums and val <= self.minimums[0]:
self.minimums.appendleft(val)
return
self.minimums.append(val)
def pop(self) -> None:
val = self.stack.pop()
if self.minimums[0] == val:
self.minimums.popleft()
return
self.minimums.pop()
def top(self) -> int:
return self.stack[-1]
def getMin(self) -> int:
return min(self.minimums[0], self.minimums[-1])
# Your MinStack object will be instantiated and called as such:
# obj = MinStack()
# obj.push(val)
# obj.pop()
# param_3 = obj.top()
# param_4 = obj.getMin() | min-stack | Python Solution With Explanation | really_cool_person | 1 | 443 | min stack | 155 | 0.519 | Medium | 2,462 |
https://leetcode.com/problems/min-stack/discuss/2401340/Python-or-Easy-and-clean-solution-or-91-faster | class MinStack:
def __init__(self):
self.stack = []
self.minStack = []
def push(self, val: int) -> None:
self.stack.append(val)
val = min(val, self.minStack[-1] if self.minStack else val)
self.minStack.append(val)
def pop(self) -> None:
self.stack.pop()
self.minStack.pop()
def top(self) -> int:
return self.stack[-1]
def getMin(self) -> int:
return self.minStack[-1]
# Your MinStack object will be instantiated and called as such:
# obj = MinStack()
# obj.push(val)
# obj.pop()
# param_3 = obj.top()
# param_4 = obj.getMin() | min-stack | Python | Easy and clean solution | 91% faster | __Asrar | 1 | 157 | min stack | 155 | 0.519 | Medium | 2,463 |
https://leetcode.com/problems/min-stack/discuss/2086436/Python3-Runtime%3A-87ms-53.60 | class MinStack:
def __init__(self):
self.stack = []
self.minStack = []
def push(self, val):
newMinStack = {'min':val}
if len(self.minStack) > 0:
newMinStack['min'] = min(self.minStack[-1]['min'], val)
self.minStack.append(newMinStack)
self.stack.append(val)
def pop(self):
self.minStack.pop()
self.stack.pop()
def top(self):
return self.stack[len(self.stack)-1]
def getMin(self):
return self.minStack[len(self.minStack)-1]['min']
# Your MinStack object will be instantiated and called as such:
# obj = MinStack()
# obj.push(val)
# obj.pop()
# param_3 = obj.top()
# param_4 = obj.getMin() | min-stack | Python3 Runtime: 87ms 53.60% | arshergon | 1 | 75 | min stack | 155 | 0.519 | Medium | 2,464 |
https://leetcode.com/problems/min-stack/discuss/2028324/Python-runtime-67.51-memory-71.24 | class MinStack:
def __init__(self):
self.stack = []
self.min = []
def push(self, val: int) -> None:
self.stack.append(val)
if self.min == [] or val <= self.min[-1]:
self.min.append(val)
def pop(self) -> None:
element = self.stack.pop()
if element == self.min[-1]:
self.min.pop()
def top(self) -> int:
element = self.stack[-1]
return element
def getMin(self) -> int:
return self.min[-1] | min-stack | Python, runtime 67.51%, memory 71.24% | tsai00150 | 1 | 105 | min stack | 155 | 0.519 | Medium | 2,465 |
https://leetcode.com/problems/min-stack/discuss/1958359/Python-Clean-Code-Easy-Understanding | class MinStack:
def __init__(self):
self.arr = []
self.min = []
def push(self, val: int) -> None:
self.arr.append(val)
if len(self.min) == 0 or self.min[-1] >= val:
self.min.append(val)
def pop(self) -> None:
val = self.arr.pop()
if self.min[-1] == val:
self.min.pop()
def top(self) -> int:
return self.arr[-1]
def getMin(self) -> int:
return self.min[-1] | min-stack | [Python] Clean Code, Easy Understanding | jamil117 | 1 | 110 | min stack | 155 | 0.519 | Medium | 2,466 |
https://leetcode.com/problems/min-stack/discuss/1893071/Python3-one-stack-and-two-stacks-runtime-O(1)-solutions-with-comments | class MinStack:
def __init__(self):
self.stack = []
def push(self, val: int) -> None:
if not self.stack:
self.stack.append((val, val))
else:
# at every push append the new pair with the new val with the smallest val in the list
self.stack.append((val, min(self.stack[-1][1], val)))
def pop(self) -> None:
self.stack.pop()
def top(self) -> int:
# the stack maintain the structure of (val, smallest val)
return self.stack[-1][0]
def getMin(self) -> int:
# the stack maintain the structure of (val, smallest val)
return self.stack[-1][1] | min-stack | Python3 one stack and two stacks runtime O(1) solutions with comments | v0vbs | 1 | 63 | min stack | 155 | 0.519 | Medium | 2,467 |
https://leetcode.com/problems/min-stack/discuss/1893071/Python3-one-stack-and-two-stacks-runtime-O(1)-solutions-with-comments | class MinStack:
def __init__(self):
self.stack = []
self.minStack = []
def push(self, val: int) -> None:
self.stack.append(val)
# if the new val is smallest than the curr smallest val in the list then we append it to the minStack
if not self.minStack or val <= self.minStack[-1]:
self.minStack.append(val)
def pop(self) -> None:
val = self.stack.pop()
# if the popped val is the curr smallest val in minStack then we pop it
if val == self.minStack[-1]:
self.minStack.pop()
def top(self) -> int:
return self.stack[-1]
def getMin(self) -> int:
return self.minStack[-1] | min-stack | Python3 one stack and two stacks runtime O(1) solutions with comments | v0vbs | 1 | 63 | min stack | 155 | 0.519 | Medium | 2,468 |
https://leetcode.com/problems/min-stack/discuss/1540272/Python3-solution | class MinStack:
elem = namedtuple('elem', ['val', 'minval'])
def __init__(self):
self.s = [] # stack of elem
def push(self, val: int) -> None:
minval = min(val, self.s[-1].minval) if self.s else val
self.s.append(MinStack.elem(val=val, minval=minval))
def pop(self) -> None:
self.s.pop()
def top(self) -> int:
return self.s[-1].val
def getMin(self) -> int:
return self.s[-1].minval | min-stack | Python3 solution | dalechoi | 1 | 75 | min stack | 155 | 0.519 | Medium | 2,469 |
https://leetcode.com/problems/min-stack/discuss/1501564/52-ms-faster-than-96.96-of-Python3-online-submissions-by-tuple-and-list | class MinStack:
def __init__(self):
self.stack = []
def push(self, val: int) -> None:
if not self.stack:
self.stack.append((val,val))
else:
self.stack.append((val, min(val, self.stack[-1][1])))
def pop(self) -> None:
self.stack.pop()
def top(self) -> int:
return self.stack[-1][0]
def getMin(self) -> int:
return self.stack[-1][1] | min-stack | 52 ms, faster than 96.96% of Python3 online submissions by tuple and list | zixin123 | 1 | 219 | min stack | 155 | 0.519 | Medium | 2,470 |
https://leetcode.com/problems/min-stack/discuss/1233506/Python3-simple-solution-beats-96-users | class MinStack:
def __init__(self):
self.stack = []
def push(self, val: int) -> None:
if self.stack:
if val <= self.stack[-1][1]:
self.stack.append((val,val))
else:
self.stack.append((val,(self.stack[-1][1])))
else:
self.stack.append((val,val))
def pop(self) -> None:
self.stack.pop()
def top(self) -> int:
return self.stack[-1][0]
def getMin(self) -> int:
return self.stack[-1][1] | min-stack | Python3 simple solution beats 96% users | EklavyaJoshi | 1 | 150 | min stack | 155 | 0.519 | Medium | 2,471 |
https://leetcode.com/problems/min-stack/discuss/572752/Python3-stack | class MinStack:
def __init__(self):
self.stack = []
def push(self, val: int) -> None:
if self.stack: self.stack.append((val, min(val, self.stack[-1][1])))
else: self.stack.append((val, val))
def pop(self) -> None:
self.stack.pop()
def top(self) -> int:
return self.stack[-1][0]
def getMin(self) -> int:
return self.stack[-1][1] | min-stack | [Python3] stack | ye15 | 1 | 119 | min stack | 155 | 0.519 | Medium | 2,472 |
https://leetcode.com/problems/min-stack/discuss/572752/Python3-stack | class MinStack:
def __init__(self):
self.stack = []
self.min = inf
def push(self, x: int) -> None:
if x <= self.min:
self.stack.append(self.min)
self.min = x
self.stack.append(x)
def pop(self) -> None:
if self.stack.pop() == self.min:
self.min = self.stack.pop()
def top(self) -> int:
return self.stack[-1]
def getMin(self) -> int:
return self.min | min-stack | [Python3] stack | ye15 | 1 | 119 | min stack | 155 | 0.519 | Medium | 2,473 |
https://leetcode.com/problems/min-stack/discuss/236242/Python3-52-ms-100-Deque | class MinStack:
def __init__(self):
self.dq = collections.deque([None])
def push(self, x: 'int') -> 'None':
if self.dq[0] == None or self.dq[0] >= x:
self.dq.appendleft(x)
self.dq.append(x)
def pop(self) -> 'None':
if self.dq[0] != None and self.dq[-1] <= self.dq[0]:
self.dq.popleft()
return self.dq.pop()
def top(self) -> 'int':
return self.dq[-1]
def getMin(self) -> 'int':
return self.dq[0] | min-stack | Python3, 52 ms, 100%, Deque | jimmyyentran | 1 | 183 | min stack | 155 | 0.519 | Medium | 2,474 |
https://leetcode.com/problems/min-stack/discuss/2819208/Python-Stack-Easy | class MinStack:
from collections import deque
def __init__(self):
self.stack = deque()
def push(self, val: int) -> None:
self.stack.appendleft(val)
def pop(self) -> None:
self.stack.popleft()
def top(self) -> int:
return self.stack[0]
def getMin(self) -> int:
return min(self.stack)
# Your MinStack object will be instantiated and called as such:
# obj = MinStack()
# obj.push(val)
# obj.pop()
# param_3 = obj.top()
# param_4 = obj.getMin() | min-stack | Python Stack Easy | lucasschnee | 0 | 3 | min stack | 155 | 0.519 | Medium | 2,475 |
https://leetcode.com/problems/min-stack/discuss/2801248/Python-or-Stack-or-TC-O(1)-SC-O(n) | class MinStack:
def __init__(self):
self.stack = []
self.min_stack = []
def push(self, val: int) -> None:
self.stack.append(val)
val = self.min_stack[-1] if len(self.min_stack) > 0 and self.min_stack[-1] < val else val
self.min_stack.append(val)
def pop(self) -> None:
self.stack.pop()
self.min_stack.pop()
def top(self) -> int:
return self.stack[-1]
def getMin(self) -> int:
return self.min_stack[-1]
# Your MinStack object will be instantiated and called as such:
# obj = MinStack()
# obj.push(val)
# obj.pop()
# param_3 = obj.top()
# param_4 = obj.getMin() | min-stack | Python | Stack | TC O(1), SC O(n) | baskvava | 0 | 1 | min stack | 155 | 0.519 | Medium | 2,476 |
https://leetcode.com/problems/min-stack/discuss/2800934/Python3-beats-99.9-using-one-stack-of-tuples-and-one-int | class MinStack:
def __init__(self):
self.stack = []
self.minimum = None
def push(self, val: int) -> None:
if self.stack:
self.stack.append((val, self.minimum))
self.minimum = min(self.minimum, val)
else:
self.stack.append((val, self.minimum))
self.minimum = val # Because min(None,x)=None
def pop(self) -> None:
val, newMin = self.stack.pop()
self.minimum = newMin
return val
def top(self) -> int:
return self.stack[-1][0]
def getMin(self) -> int:
return self.minimum | min-stack | Python3 beats 99.9% using one stack of tuples and one int | lucieperrotta | 0 | 2 | min stack | 155 | 0.519 | Medium | 2,477 |
https://leetcode.com/problems/min-stack/discuss/2786255/Best-Approach-using-one-stack.-Aditya-Verma-.O(1)-time-and-O(1)-extra-spaceorPython | class MinStack:
def __init__(self):
self.minval=float('inf')
self.stack=[]
def push(self, val: int) -> None:
if len(self.stack)==0:
self.minval=val
self.stack.append(val)
else:
if val >=self.minval:
self.stack.append(val)
elif val < self.minval:
self.stack.append(2*val-self.minval)
self.minval=val
def pop(self) -> None:
if self.stack[-1] >=self.minval:
self.stack.pop()
elif self.stack[-1] < self.minval:
self.minval=2*self.minval-self.stack[-1]
self.stack.pop()
def top(self) -> int:
if self.stack[-1] >=self.minval:
return self.stack[-1]
elif self.stack[-1] < self.minval:
return self.minval
def getMin(self) -> int:
return self.minval
# Your MinStack object will be instantiated and called as such:
# obj = MinStack()
# obj.push(val)
# obj.pop()
# param_3 = obj.top()
# param_4 = obj.getMin() | min-stack | Best Approach using one stack. Aditya Verma .O(1) time and O(1) extra space|Python | abro_01 | 0 | 10 | min stack | 155 | 0.519 | Medium | 2,478 |
https://leetcode.com/problems/min-stack/discuss/2781893/Easy-Python-Solution-O(1)-time-every-function | class MinStack:
def __init__(self):
self.s=[]
self.minstack=[inf]
def push(self, val: int) -> None:
self.s.append(val)
if(self.minstack[-1]>val):
self.minstack.append(val)
else:
self.minstack.append(self.minstack[-1])
def pop(self) -> None:
self.minstack.pop()
return self.s.pop()
def top(self) -> int:
return self.s[-1]
def getMin(self) -> int:
return self.minstack[-1] | min-stack | Easy Python Solution O(1) time every function | liontech_123 | 0 | 2 | min stack | 155 | 0.519 | Medium | 2,479 |
https://leetcode.com/problems/min-stack/discuss/2763295/Python-or-Simple-list-solution | class MinStack:
def __init__(self):
self.st = []
self.min = 1e18
def push(self, val: int) -> None:
self.st.append((val, self.min))
if val < self.min:
self.min = val
def pop(self) -> None:
if self.min == self.st[-1][0]:
self.min = self.st[-1][1]
self.st.pop()
def top(self) -> int:
return self.st[-1][0]
def getMin(self) -> int:
return self.min
# Your MinStack object will be instantiated and called as such:
# obj = MinStack()
# obj.push(val)
# obj.pop()
# param_3 = obj.top()
# param_4 = obj.getMin() | min-stack | Python | Simple list solution | LordVader1 | 0 | 20 | min stack | 155 | 0.519 | Medium | 2,480 |
https://leetcode.com/problems/min-stack/discuss/2757598/Python3-83-faster-with-explanation | class MinStack:
def __init__(self):
self.array = []
self.minimum = 0
def push(self, val: int) -> None:
if len(self.array) == 0:
self.minimum = val
else:
self.minimum = min(self.minimum, val)
self.array.append(val)
def pop(self) -> None:
if self.minimum == self.array[-1]:
del self.array[-1]
if len(self.array) >= 1:
self.minimum = min(self.array)
else:
self.minimum = 0
else:
del self.array[-1]
def top(self) -> int:
return self.array[-1]
def getMin(self) -> int:
return self.minimum | min-stack | Python3 83% faster with explanation | cvelazquez322 | 0 | 5 | min stack | 155 | 0.519 | Medium | 2,481 |
https://leetcode.com/problems/min-stack/discuss/2753742/Python-two-stacks-solution | class MinStack:
def __init__(self):
self.stack = []
self.tracker = [float('inf')]
def push(self, val: int) -> None:
self.stack.append(val)
if self.tracker and val <= self.tracker[-1]:
self.tracker.append(val)
def pop(self) -> None:
if self.stack[-1] <= self.tracker[-1]:
self.tracker.pop()
self.stack.pop()
def top(self) -> int:
return self.stack[-1]
def getMin(self) -> int:
return self.tracker[-1] if self.tracker[-1] != float('inf') else None
# Your MinStack object will be instantiated and called as such:
# obj = MinStack()
# obj.push(val)
# obj.pop()
# param_3 = obj.top()
# param_4 = obj.getMin() | min-stack | Python two stacks solution | leetcodesquad | 0 | 3 | min stack | 155 | 0.519 | Medium | 2,482 |
https://leetcode.com/problems/min-stack/discuss/2736835/Python-Solution-and-Pretty-Efficient | class MinStack:
def __init__(self):
self.stack = []
self.minStack = []
def push(self, val: int) -> None:
self.stack.append(val)
minimum = min(val,self.minStack[-1] if self.minStack else val)
self.minStack.append(minimum)
def pop(self) -> None:
self.stack.pop()
self.minStack.pop()
def top(self) -> int:
return self.stack[-1]
def getMin(self) -> int:
return self.minStack[-1]
# Your MinStack object will be instantiated and called as such:
# obj = MinStack()
# obj.push(val)
# obj.pop()
# param_3 = obj.top()
# param_4 = obj.getMin() | min-stack | Python Solution and Pretty Efficient | abe40 | 0 | 9 | min stack | 155 | 0.519 | Medium | 2,483 |
https://leetcode.com/problems/min-stack/discuss/2729550/Python3-two-stack-implementation | class MinStack:
def __init__(self):
self. stack = []
self.min_stack = []
def push(self, val: int) -> None:
self.stack.append(val)
if self.min_stack == [] or self.min_stack[-1] > val:
self.min_stack.append(val)
else:
self.min_stack.append(self.min_stack[-1])
def pop(self) -> None:
self.min_stack.pop()
return self.stack.pop()
def top(self) -> int:
return self.stack[-1]
def getMin(self) -> int:
return self.min_stack[-1]
# Your MinStack object will be instantiated and called as such:
# obj = MinStack()
# obj.push(val)
# obj.pop()
# param_3 = obj.top()
# param_4 = obj.getMin() | min-stack | Python3, two stack implementation | paul1202 | 0 | 3 | min stack | 155 | 0.519 | Medium | 2,484 |
https://leetcode.com/problems/min-stack/discuss/2665189/Min-Stack-with-python-Using-Single-stack-with-less-time(beats-98.10)-and-space(beats-95.94) | class MinStack:
def __init__(self):
self.stack = []
self.min_value = float('inf')
def push(self, val: int) -> None:
if val <= self.min_value:
self.stack.append(self.min_value)
self.min_value = val
self.stack.append(val)
def pop(self) -> None:
val = self.stack.pop(-1)
if self.min_value == val:
self.min_value = self.stack.pop(-1)
def top(self) -> int:
return self.stack[-1]
def getMin(self) -> int:
return self.min_value
# Your MinStack object will be instantiated and called as such:
# obj = MinStack()
# obj.push(val)
# obj.pop()
# param_3 = obj.top()
# param_4 = obj.getMin() | min-stack | Min-Stack with python - Using Single stack with less time(beats 98.10%) and space(beats 95.94%) | user2385PN | 0 | 7 | min stack | 155 | 0.519 | Medium | 2,485 |
https://leetcode.com/problems/min-stack/discuss/2652294/Faster-than-95.26 | class MinStack:
def __init__(self):
self.stack = []
self.minStack = []
def push(self, val: int) -> None:
self.stack.append(val)
val = min(val,self.minStack[-1] if self.minStack else val)
self.minStack.append(val)
def pop(self) -> None:
self.stack.pop()
self.minStack.pop()
def top(self) -> int:
return self.stack[-1]
def getMin(self) -> int:
return self.minStack[-1]
# Your MinStack object will be instantiated and called as such:
# obj = MinStack()
# obj.push(val)
# obj.pop()
# param_3 = obj.top()
# param_4 = obj.getMin() | min-stack | Faster than 95.26% | jayeshvarma | 0 | 62 | min stack | 155 | 0.519 | Medium | 2,486 |
https://leetcode.com/problems/min-stack/discuss/2640979/python-solution | class MinStack:
def __init__(self):
self.stack = []
self.minn_s = []
def push(self, val: int) -> None:
self.stack.append(val)
if len(self.stack) == 1:
self.minn_s.append(val)
elif self.minn_s[-1] > val:
self.minn_s.append(val)
else:
self.minn_s.append(self.minn_s[-1])
def pop(self) -> None:
if len(self.stack) == 0:
return
self.stack.pop()
self.minn_s.pop()
def top(self) -> int:
return self.stack[-1]
def getMin(self) -> int:
if self.minn_s:
return self.minn_s[-1]
else:
return | min-stack | python solution | sarthakchawande14 | 0 | 4 | min stack | 155 | 0.519 | Medium | 2,487 |
https://leetcode.com/problems/min-stack/discuss/2617666/Python-Solution-or-60-ms-or-Faster-than-95.96-or-Using-List | class MinStack:
def __init__(self):
self.a = []
self.min_ = float("inf")
def push(self, val: int) -> None:
self.a.append(val)
if val<self.min_:
self.min_ = val
def pop(self) -> None:
x = self.a.pop()
if x==self.min_:
if len(self.a):
self.min_=min(self.a)
else:
self.min_=float("inf")
def top(self) -> int:
return self.a[-1]
def getMin(self) -> int:
return self.min_
# Your MinStack object will be instantiated and called as such:
# obj = MinStack()
# obj.push(val)
# obj.pop()
# param_3 = obj.top()
# param_4 = obj.getMin() | min-stack | Python Solution | 60 ms | Faster than 95.96% | Using List | rorschach86 | 0 | 64 | min stack | 155 | 0.519 | Medium | 2,488 |
https://leetcode.com/problems/min-stack/discuss/2594848/python-easy-solution | class MinStack:
def __init__(self):
self.stack = []
self.minstack = []
def push(self, val: int) -> None:
self.stack.append(val)
newval = min(val,self.minstack[-1] if self.minstack else val)
self.minstack.append(newval)
def pop(self) -> None:
self.stack.pop()
self.minstack.pop()
def top(self) -> int:
return self.stack[-1]
def getMin(self) -> int:
return self.minstack[-1] | min-stack | python easy solution | al5861 | 0 | 42 | min stack | 155 | 0.519 | Medium | 2,489 |
https://leetcode.com/problems/min-stack/discuss/2531686/Python3-or-Optimal-Constant-Time-Solution | class MinStack:
def __init__(self):
self.main = deque()
self.min = deque()
def push(self, val: int) -> None:
self.main.append(val)
minval = min(val, self.min[-1] if self.min else val)
self.min.append(minval)
def pop(self) -> None:
self.min.pop()
self.main.pop()
def top(self) -> int:
return self.main[-1]
def getMin(self) -> int:
return self.min[-1]
# Search with tag chawlashivansh for my solutions. | min-stack | Python3 | Optimal Constant Time Solution | chawlashivansh | 0 | 77 | min stack | 155 | 0.519 | Medium | 2,490 |
https://leetcode.com/problems/min-stack/discuss/2504258/Simple-python-code-not-so-fast-though | class MinStack:
def __init__(self):
self.stack=[]
self.length=0
def push(self, val:int)->None:
self.length+=1
self.stack.append(val)
def pop(self)->None:
self.length-=1
self.stack.pop()
def top(self)->int:
return self.stack[-1]
def getMin(self)->int:
return min(self.stack) | min-stack | Simple python code, not so fast though | CarolChang | 0 | 35 | min stack | 155 | 0.519 | Medium | 2,491 |
https://leetcode.com/problems/min-stack/discuss/2485293/50-faster-than-other-Python-submission-optimized-for-interviews | class MinStack:
def __init__(self):
self.bucket = []
def push(self, val: int) -> None:
if len(self.bucket) == 0:
self.bucket.append((val, val))
else:
currMin = self.getMin() if self.getMin() < val else val
self.bucket.append((val, currMin))
def pop(self) -> None:
return self.bucket.pop()
def top(self) -> int:
return self.bucket[-1][0]
def getMin(self) -> int:
if len(self.bucket) == 0:
return None
return self.bucket[-1][1] | min-stack | 50% faster than other Python submission optimized for interviews | sajidrsk | 0 | 25 | min stack | 155 | 0.519 | Medium | 2,492 |
https://leetcode.com/problems/min-stack/discuss/2474150/BRUTALLY-OPTIMISED-Python-Solution-Mind-Blown-5-liner | class MinStack(object):
def __init__(self):
self.stck = []
self.min = [float('inf')]
def push(self, val):
self.stck += [val]
if self.min[-1]>=val:
self.min += [val]
def pop(self):
pop = self.stck.pop()
if pop == self.min[-1]: self.min.pop()
return pop
def top(self): return self.stck[-1]
def getMin(self): return self.min[-1] | min-stack | BRUTALLY OPTIMISED Python Solution, Mind == Blown 5 liner | Nj-0 | 0 | 62 | min stack | 155 | 0.519 | Medium | 2,493 |
https://leetcode.com/problems/min-stack/discuss/2474150/BRUTALLY-OPTIMISED-Python-Solution-Mind-Blown-5-liner | class MinStack(object):
__init__ = lambda self: setattr(self,'stck',[])
top = lambda self: self.stck[-1][0]
getMin = lambda self: self.stck[-1][1] if self.stck else float('inf')
push = lambda self, v: self.stck.append((v, min(self.getMin(),v)))
pop = lambda self: self.stck.pop()[0] | min-stack | BRUTALLY OPTIMISED Python Solution, Mind == Blown 5 liner | Nj-0 | 0 | 62 | min stack | 155 | 0.519 | Medium | 2,494 |
https://leetcode.com/problems/min-stack/discuss/2446557/Python3.-Two-stacks-solution | class MinStack:
def __init__(self):
# Init variables
self._stack = []
self._min_stack = []
def push(self, val: int) -> None: # O(1) time complexity
# Find new minimum value
new_min_value = val
if len(self._stack) > 0:
new_min_value = min(new_min_value, self._min_stack[-1])
# Push values
self._stack.append(val)
self._min_stack.append(new_min_value)
def pop(self) -> None: # O(1) time complexity
self._stack.pop()
self._min_stack.pop()
def top(self) -> int: # O(1) time complexity
# It's guaranteed we have non-empty stack while calling this function
return self._stack[-1]
def getMin(self) -> int: # O(1) time complexity
# It's guaranteed we have non-empty stack while calling this function
return self._min_stack[-1] | min-stack | Python3. Two stacks solution | NonameDeadinside | 0 | 19 | min stack | 155 | 0.519 | Medium | 2,495 |
https://leetcode.com/problems/min-stack/discuss/2420789/Noob-Nested-Py-Code | class MinStack:
def __init__(self):
self.minimum = float("inf")
self.nums = []
def push(self, val: int) -> None:
self.minimum = min(val,self.minimum)
self.nums.append(val)
def pop(self) -> None:
if self.nums:
if self.nums[-1] == self.minimum:
self.nums.pop()
if self.nums:
self.minimum = min(self.nums)
else:
self.nums.pop()
if len(self.nums) == 0:
self.minimum = float("inf")
def top(self) -> int:
return self.nums[-1]
def getMin(self) -> int:
return self.minimum | min-stack | Noob Nested Py Code | ms1241721_lc | 0 | 18 | min stack | 155 | 0.519 | Medium | 2,496 |
https://leetcode.com/problems/min-stack/discuss/2398745/Python3-solution-easy-solution-87-faster | class MinStack:
def __init__(self):
self.data = []
#O(1) time complexity
def push(self, val: int) -> None:
if not self.data:
self.data.append([val, val])
else:
self.data.append([val, min(self.data[-1][1], val)])
#O(1) time complexity
def pop(self) -> None:
self.data.pop()
#O(1) time complexity
def top(self) -> int:
return self.data[-1][0]
#O(1) time complexity
def getMin(self) -> int:
return self.data[-1][1] | min-stack | Python3 solution, easy solution, 87% faster | matteogianferrari | 0 | 63 | min stack | 155 | 0.519 | Medium | 2,497 |
https://leetcode.com/problems/min-stack/discuss/2250743/Python3-or-Super-Easy-or-Two-Stacks | class MinStack:
def __init__(self):
self.stack = []
self.min = []
def push(self, val: int) -> None:
self.stack.append(val)
val = min(val, self.min[-1] if self.min else val)
self.min.append(val)
def pop(self) -> None:
self.stack.pop()
self.min.pop()
def top(self) -> int:
return self.stack[-1]
def getMin(self) -> int:
return self.min[-1] | min-stack | ✅Python3 | Super Easy | Two Stacks | thesauravs | 0 | 13 | min stack | 155 | 0.519 | Medium | 2,498 |
https://leetcode.com/problems/min-stack/discuss/2189141/Python3-or-Using-2-stacks | class MinStack:
def __init__(self):
self.s=[]
self.mins=[]
def push(self, val: int):
self.s.append(val)
if len(self.mins)==0:
self.mins.append(val)
elif val<=self.mins[-1]:
self.mins.append(val)
def pop(self):
x=self.s.pop()
if x==self.mins[-1]:
self.mins.pop()
return x
def top(self):
return self.s[-1]
def getMin(self):
return self.mins[-1] | min-stack | Python3 | Using 2 stacks | sogarwal | 0 | 47 | min stack | 155 | 0.519 | Medium | 2,499 |
Subsets and Splits