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/length-of-longest-fibonacci-subsequence/discuss/942291/Python3-dp-O(N2) | class Solution:
def lenLongestFibSubseq(self, A: List[int]) -> int:
ss = set(A)
seen = {}
for i in range(len(A)):
for j in range(i):
if A[i] - A[j] < A[j] and A[i] - A[j] in ss:
seen[A[i], A[j]] = 1 + seen.get((A[j], A[i] - A[j]), 2)
... | length-of-longest-fibonacci-subsequence | [Python3] dp O(N^2) | ye15 | 0 | 140 | length of longest fibonacci subsequence | 873 | 0.486 | Medium | 14,200 |
https://leetcode.com/problems/length-of-longest-fibonacci-subsequence/discuss/352955/Solution-in-Python-3 | class Solution:
def lenLongestFibSubseq(self, A: List[int]) -> int:
L, M, AA = len(A), 0, {i for i in A}
for i in range(L-1):
for j in range(i+1,L):
a, b, s = A[i], A[j], 0
while a in AA: a, b, s = b, a + b, s + 1
if s > M: M = s
if a > A[-1]:
if j == i + 1:
... | length-of-longest-fibonacci-subsequence | Solution in Python 3 | junaidmansuri | 0 | 443 | length of longest fibonacci subsequence | 873 | 0.486 | Medium | 14,201 |
https://leetcode.com/problems/walking-robot-simulation/discuss/381840/Solution-in-Python-3 | class Solution:
def robotSim(self, c: List[int], b: List[List[int]]) -> int:
x, y, d, b, M = 0, 0, 0, set([tuple(i) for i in b]), 0
for i in c:
if i < 0: d = (d + 2*i + 3)%4
else:
if d in [1,3]:
for x in range(x, x+(i+1)*(2-d), 2-d):
... | walking-robot-simulation | Solution in Python 3 | junaidmansuri | 3 | 513 | walking robot simulation | 874 | 0.384 | Medium | 14,202 |
https://leetcode.com/problems/walking-robot-simulation/discuss/2782108/Python-logic-step-breakdown | class Solution:
def robotSim(self, commands: List[int], obstacles: List[List[int]]) -> int:
directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
facing = best = 0
distance = lambda x, y: x**2 + y**2
current = (0, 0)
obstacles = {(p[0], p[1]) for p in obstacles}
f... | walking-robot-simulation | Python logic step breakdown | modusV | 0 | 8 | walking robot simulation | 874 | 0.384 | Medium | 14,203 |
https://leetcode.com/problems/walking-robot-simulation/discuss/1511730/Python-very-clear-simple-solution | class Solution:
def robotSim(self, commands: List[int], obstacles: List[List[int]]) -> int:
maxd = 0
left = {'N':'W', 'W':'S', 'S':'E', 'E':'N'}
right = {'N':'E', 'E':'S', 'S':'W', 'W':'N'}
step = {'N':[0,1], 'W':[-1, 0], 'E':[1,0], 'S':[0,-1]}
obstacle = set()
... | walking-robot-simulation | Python very clear, simple solution | byuns9334 | 0 | 133 | walking robot simulation | 874 | 0.384 | Medium | 14,204 |
https://leetcode.com/problems/walking-robot-simulation/discuss/1425765/Put-obstacles-into-a-set-of-tuples-93-speed | class Solution:
def robotSim(self, commands: List[int], obstacles: List[List[int]]) -> int:
# direction: 0-north, 1-east, 2, south, 3-west
direction = x = y = 0
max_distance = 0
obstacles = set((x, y) for x, y in obstacles)
for command in commands:
if command == -... | walking-robot-simulation | Put obstacles into a set of tuples, 93% speed | EvgenySH | 0 | 107 | walking robot simulation | 874 | 0.384 | Medium | 14,205 |
https://leetcode.com/problems/walking-robot-simulation/discuss/1346995/Python3%3A-Intuitively-broken-up-with-functions | class Solution:
def robotSim(self, commands: List[int], obstacles: List[List[int]]) -> int:
# Initialize
self.obsticles_dict = {f"{x},{y}": 1 for [x,y] in obstacles}
self.cur_position = {'x':0, 'y':0}
self.cur_direction = 0
self.max_distance = 0
# R... | walking-robot-simulation | Python3: Intuitively broken up with functions | jdouitsis | 0 | 145 | walking robot simulation | 874 | 0.384 | Medium | 14,206 |
https://leetcode.com/problems/walking-robot-simulation/discuss/1240309/Time-limit-exceeded-when-I-click-%22Submit%22-not-when-I-click-%22Run-Code%22-for-same-input | class Solution:
def robotSim(self, commands: List[int], obstacles: List[List[int]]) -> int:
# A utility function to move robot east
def go_east(num, obstacles, curr):
new = curr
for i in range(1,num):
if [curr[0]+i, curr[1]] in obstacles:
... | walking-robot-simulation | Time limit exceeded when I click "Submit", not when I click "Run Code" for same input | shuklaeshita0209 | 0 | 80 | walking robot simulation | 874 | 0.384 | Medium | 14,207 |
https://leetcode.com/problems/walking-robot-simulation/discuss/695625/Python3-beats-99.5.-Movement-simulation | class Solution:
def robotSim(self, commands: List[int], obstacles: List[List[int]]) -> int:
obstacles = set(tuple(x) for x in obstacles)
i = 0
x,y = 0,0
max_dist = float('-inf')
direction = "N"
while(i<len(commands)):
if(commands[i]==-1):
... | walking-robot-simulation | Python3 - beats 99.5%. - Movement simulation | vs152 | 0 | 257 | walking robot simulation | 874 | 0.384 | Medium | 14,208 |
https://leetcode.com/problems/walking-robot-simulation/discuss/638677/Intuitive-and-cumbersome-approach-with-comment-as-explanation | class Solution:
def robotSim(self, commands: List[int], obstacles: List[List[int]]) -> int:
# 0) Keep obstacke in set for future reference
obstacle_set = set()
for o in obstacles:
obstacle_set.add((o[0], o[1]))
# 1) Define movement functions and way to turn direction
... | walking-robot-simulation | Intuitive and cumbersome approach with comment as explanation | puremonkey2001 | 0 | 111 | walking robot simulation | 874 | 0.384 | Medium | 14,209 |
https://leetcode.com/problems/koko-eating-bananas/discuss/1705145/Python-BinarySearch-%2B-Optimizations-or-Explained | class Solution:
def minEatingSpeed(self, piles: List[int], h: int) -> int:
k = 1
while True:
total_time = 0
for i in piles:
total_time += ceil(i / k)
if total_time > h:
k += 1
else:
return k | koko-eating-bananas | [Python] BinarySearch + Optimizations | Explained | anCoderr | 20 | 814 | koko eating bananas | 875 | 0.521 | Medium | 14,210 |
https://leetcode.com/problems/koko-eating-bananas/discuss/1705145/Python-BinarySearch-%2B-Optimizations-or-Explained | class Solution:
def minEatingSpeed(self, piles: List[int], h: int) -> int:
k = ceil(sum(piles)/h)
while True:
total_time = 0
for i in piles:
total_time += ceil(i/k)
if total_time > h:
break # as time exceeds H
if... | koko-eating-bananas | [Python] BinarySearch + Optimizations | Explained | anCoderr | 20 | 814 | koko eating bananas | 875 | 0.521 | Medium | 14,211 |
https://leetcode.com/problems/koko-eating-bananas/discuss/1705145/Python-BinarySearch-%2B-Optimizations-or-Explained | class Solution:
def minEatingSpeed(self, piles: List[int], h: int) -> int:
left = ceil(sum(piles) / h) # lower bound of Binary Search
right = max(piles) # upper bound of Binary Search
while left < right:
mid = (left + right) // 2 # we check for k=mid
total_time = 0
... | koko-eating-bananas | [Python] BinarySearch + Optimizations | Explained | anCoderr | 20 | 814 | koko eating bananas | 875 | 0.521 | Medium | 14,212 |
https://leetcode.com/problems/koko-eating-bananas/discuss/1484486/Binary-search-solution | class Solution:
def minEatingSpeed(self, piles: List[int], h: int) -> int:
def feasible(speed):
return sum((pile-1) //speed+1 for pile in piles) <= h # faster
#hour=1
#s=0
#for pile in piles:
#s+=pile
#if s>speed:
#s=pile
#if hour>h:
#return False
#return True
left, r... | koko-eating-bananas | Binary search solution | Qyum | 3 | 131 | koko eating bananas | 875 | 0.521 | Medium | 14,213 |
https://leetcode.com/problems/koko-eating-bananas/discuss/1537814/Python3-From-Linear-Search-to-Binary-Search | class Solution:
def minEatingSpeed(self, piles: List[int], h: int) -> int:
"""
3 6 7 11 , h =8
speed = 1 3 6 7 11 = 28h > 8h
speed = 2 3 3 4 6 = 16h > 8h
speed = 3 1 2 3 4 = 10h > 8h
minimum ----> speed = 4 1 2 ... | koko-eating-bananas | [Python3] From Linear Search to Binary Search | zhanweiting | 2 | 263 | koko eating bananas | 875 | 0.521 | Medium | 14,214 |
https://leetcode.com/problems/koko-eating-bananas/discuss/1705284/Easy-to-understand-oror-Binary-Search-oror-faster-than-80-oror-python3 | class Solution:
def minEatingSpeed(self, piles: List[int], h: int) -> int:
def canfinsh(k):
h_needed = 0
for p in piles:
h_needed += ceil(p/k)
return h_needed <= h
left = 1
... | koko-eating-bananas | Easy to understand || Binary Search || faster than 80% || python3 | code_Shinobi | 1 | 114 | koko eating bananas | 875 | 0.521 | Medium | 14,215 |
https://leetcode.com/problems/koko-eating-bananas/discuss/1704253/Python-Solution | class Solution:
def minEatingSpeed(self, piles: List[int], h: int) -> int:
low, high = 1, max(piles)
while low <= high:
mid = low + (high - low) // 2
total_hours = 0
for pile in piles:
total_hours += ceil(pile / mid)
if total_hours > h:... | koko-eating-bananas | Python Solution | mariandanaila01 | 1 | 116 | koko eating bananas | 875 | 0.521 | Medium | 14,216 |
https://leetcode.com/problems/koko-eating-bananas/discuss/1703516/Python3-Binary-Search-Simple-and-Logic-Explained | class Solution:
def minEatingSpeed(self, piles: List[int], h: int) -> int:
l,r = 1,max(piles) # declare boundaries
while l<r: # loop performing BS
m = l+(r-l)//2 # calculate medium
hours ... | koko-eating-bananas | [Python3] Binary Search Simple and Logic Explained | Rainyforest | 1 | 181 | koko eating bananas | 875 | 0.521 | Medium | 14,217 |
https://leetcode.com/problems/koko-eating-bananas/discuss/2826568/Python%3A-Optimal-and-Clean-with-explanation-O(nlog(max(piles)))-time-and-O(1)-space | class Solution:
# Hint: try binary searching over the solution space. Given a fixed speed k, it is easy to check if Koko can eat all the bananas within h hours. If it works for k, we recurse by setting right = k. Otherwise, we recurse by setting left = k+1.
# O(nlog(max(piles))) time : O(1) space
def ... | koko-eating-bananas | Python: Optimal and Clean with explanation - O(nlog(max(piles))) time and O(1) space | topswe | 0 | 4 | koko eating bananas | 875 | 0.521 | Medium | 14,218 |
https://leetcode.com/problems/koko-eating-bananas/discuss/2794593/Python-binary-search | class Solution:
def minEatingSpeed(self, piles: List[int], h: int) -> int:
l, r = 1, max(piles)
k = max(piles)
while l <= r:
mid = (l + r) // 2
hours = 0
for p in piles:
hours += math.ceil(p / mid)
if hours <= h:
... | koko-eating-bananas | Python binary search | zananpech9 | 0 | 3 | koko eating bananas | 875 | 0.521 | Medium | 14,219 |
https://leetcode.com/problems/koko-eating-bananas/discuss/2747725/Python3-Binary-Search | class Solution:
def minEatingSpeed(self, piles: List[int], h: int) -> int:
if h == len(piles):
return max(piles)
l, r = 1, max(piles)
res = r
while l < r:
k = (l+r)//2
hours = sum([ceil(pile/k) for pile in piles])
if hours <= ... | koko-eating-bananas | Python3 Binary Search | jonathanbrophy47 | 0 | 1 | koko eating bananas | 875 | 0.521 | Medium | 14,220 |
https://leetcode.com/problems/koko-eating-bananas/discuss/2695269/python3or-easy-orsame-as-Minimum-Limit-of-Balls-in-a-Bag | class Solution:
def minEatingSpeed(self, piles: List[int], h: int) -> int:
def check(nums,m,hr):
for i in nums:
if i//m == 0:
hr-=1
else:
hr-=(i//m)+1
if i%m == 0:
hr+... | koko-eating-bananas | python3| easy |same as Minimum Limit of Balls in a Bag | rohannayar8 | 0 | 4 | koko eating bananas | 875 | 0.521 | Medium | 14,221 |
https://leetcode.com/problems/koko-eating-bananas/discuss/2599249/Python-Binary-Search-Solution | class Solution:
def minEatingSpeed(self, piles: List[int], h: int) -> int:
# instead of doing brute force, when we find a solution we will look for a smaller k if possible
# time complexity: O(log(max(p)) * p)
l,r = 1,max(piles)
res = r
while l <= r:
mid = (l + r) //... | koko-eating-bananas | Python Binary Search Solution | al5861 | 0 | 24 | koko eating bananas | 875 | 0.521 | Medium | 14,222 |
https://leetcode.com/problems/koko-eating-bananas/discuss/2181981/Python-99.8-solution.-Superfast | class Solution:
def minEatingSpeed(self, piles: List[int], h: int) -> int:
def can_eat(speed):
return sum((pile - 1) // speed + 1 for pile in piles) <= h
nPiles = len(piles)
sumBanana = sum(piles)
left = math.ceil(sumBanana/h)
right = math.... | koko-eating-bananas | Python 99.8% solution. Superfast | arnavjaiswal149 | 0 | 20 | koko eating bananas | 875 | 0.521 | Medium | 14,223 |
https://leetcode.com/problems/koko-eating-bananas/discuss/2090460/Python-or-Binary-Search-Simple-Solution | class Solution:
def minEatingSpeed(self, piles: List[int], h: int) -> int:
l,r = 1, max(piles)
res = r
while l <= r:
k = (l+r) // 2
hours = 0
for p in piles:
hours += math.ceil(p/k)
if hours <= h:
re... | koko-eating-bananas | Python | Binary Search Simple Solution | __Asrar | 0 | 62 | koko eating bananas | 875 | 0.521 | Medium | 14,224 |
https://leetcode.com/problems/koko-eating-bananas/discuss/2012336/Python-3-Interview-Answer-w-DETAILED-Comments | class Solution:
def minEatingSpeed(self, piles: List[int], h: int) -> int:
left, right = 1, max(piles)
while left < right:
# 1. left = 1, right = 11 | 1 + 11 = 12 | 12 / 2 = 6 | mid = 6
# 2. left = 1, right = 6 | 1 + 6 = 7 | 7 / 2 = 3 | mid = 3
# 3. left = 4, right = 6 | 4 + 6 = 10 | 10 / 2 = 5 |... | koko-eating-bananas | [Python 3] Interview Answer w/ DETAILED Comments | Cut | 0 | 57 | koko eating bananas | 875 | 0.521 | Medium | 14,225 |
https://leetcode.com/problems/koko-eating-bananas/discuss/1932602/6-Lines-Python-Solution-oror-97-Faster-oror-Memory-less-than-80 | class Solution:
def minEatingSpeed(self, piles: List[int], h: int) -> int:
lo=1 ; hi=max(piles)
while lo<hi:
mid=(lo+hi)//2
if sum(ceil(pile/mid) for pile in piles)<=h: hi=mid
else: lo=mid+1
return hi | koko-eating-bananas | 6-Lines Python Solution || 97% Faster || Memory less than 80% | Taha-C | 0 | 35 | koko eating bananas | 875 | 0.521 | Medium | 14,226 |
https://leetcode.com/problems/koko-eating-bananas/discuss/1914619/Python-Easy-reverse-engineering | class Solution:
def minEatingSpeed(self, h: List[int], l: int) -> int:
def helper(x):
count = 0
for i in range(len(h)):
count+=((h[i]-1)//x)+1
# print(((h[i]-1)//x)+1)
return count<=l
... | koko-eating-bananas | Python Easy reverse engineering | Brillianttyagi | 0 | 17 | koko eating bananas | 875 | 0.521 | Medium | 14,227 |
https://leetcode.com/problems/koko-eating-bananas/discuss/1704378/Python-3-or-O(n-logn)-or-Binary-Search | class Solution:
def minEatingSpeed(self, piles: List[int], h: int) -> int:
def OK(key):
ans = 0
for pile in piles:
ans += pile // key + (pile % key > 0)
return ans <= h
start, end = 1, 10 ** 9
while start < end:
... | koko-eating-bananas | [Python 3] | O(n logn) | Binary Search | BrijGwala | 0 | 37 | koko eating bananas | 875 | 0.521 | Medium | 14,228 |
https://leetcode.com/problems/koko-eating-bananas/discuss/1704335/python3-and-golang-binary-search-easy-to-understand | class Solution:
from math import ceil
def minEatingSpeed(self, piles: List[int], h: int) -> int:
if len(piles) == h:
return max(piles)
piles.sort()
result = 1 # final result
start, end = 1, piles[-1] # the `result` must be greater than or equal to the `start` and... | koko-eating-bananas | python3 & golang binary-search, easy to understand | yangfan9702 | 0 | 33 | koko eating bananas | 875 | 0.521 | Medium | 14,229 |
https://leetcode.com/problems/koko-eating-bananas/discuss/1704121/Python-Simple-Solution-Using-Binary-Search-!!-Clear-Code | class Solution:
def minEatingSpeed(self, piles: List[int], h: int) -> int:
def FindKValue(mid,piles,h):
length = len(piles)
total = 0
for i in range(length):
div = piles[i] // mid
rem = piles[i] % mid
if rem==0:
total = total + div
else:
total = total + div + 1
if total<=h... | koko-eating-bananas | [ Python ] Simple Solution Using Binary Search !! Clear Code | ASHOK_KUMAR_MEGHVANSHI | 0 | 40 | koko eating bananas | 875 | 0.521 | Medium | 14,230 |
https://leetcode.com/problems/koko-eating-bananas/discuss/1282180/Python-Almost-perfect-space-and-time | class Solution:
def minEatingSpeed(self, piles: List[int], h: int) -> int:
def check(mid):
time = 0
for x in range(len(piles)):
time += -(-piles[x] // mid)
if time > h:
return False
if time > h:
... | koko-eating-bananas | [Python] Almost perfect space and time | Fizzybepis | 0 | 114 | koko eating bananas | 875 | 0.521 | Medium | 14,231 |
https://leetcode.com/problems/koko-eating-bananas/discuss/1273172/python-or-binary-search | class Solution:
def minEatingSpeed(self, piles: List[int], hour: int) -> int:
l=1
h=max(piles)
def fun(speed):
ans=0
for i in piles:
ans+=ceil(i/speed)
#print(ans)
if ans>hour:
return False
return Tru... | koko-eating-bananas | python | binary search | heisenbarg | 0 | 68 | koko eating bananas | 875 | 0.521 | Medium | 14,232 |
https://leetcode.com/problems/middle-of-the-linked-list/discuss/526372/PythonJSJavaGoC%2B%2B-O(n)-by-two-pointers-90%2B-w-Diagram | class Solution:
def middleNode(self, head: ListNode) -> ListNode:
slow, fast = head, head
while fast:
fast = fast.next
if fast:
fast = fast.next
else:
# fast has reached the end of linked list
... | middle-of-the-linked-list | Python/JS/Java/Go/C++ O(n) by two-pointers 90%+ [w/ Diagram] | brianchiang_tw | 45 | 2,800 | middle of the linked list | 876 | 0.739 | Easy | 14,233 |
https://leetcode.com/problems/middle-of-the-linked-list/discuss/2688781/Python-2-Easy-Way-To-Find-Middle-of-the-Linked-List-or-93-Faster-or-Fast-and-Simple-Solution | class Solution:
def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
count = 0
** tmp = head
while tmp:
count+=1
tmp = tmp.next
middle = count//2
l = 0
while l < middle:
head = head.next
l+=1... | middle-of-the-linked-list | ✔️ Python 2 Easy Way To Find Middle of the Linked List | 93% Faster | Fast and Simple Solution | pniraj657 | 16 | 1,500 | middle of the linked list | 876 | 0.739 | Easy | 14,234 |
https://leetcode.com/problems/middle-of-the-linked-list/discuss/2688781/Python-2-Easy-Way-To-Find-Middle-of-the-Linked-List-or-93-Faster-or-Fast-and-Simple-Solution | class Solution:
def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
slow = head
fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
return slow | middle-of-the-linked-list | ✔️ Python 2 Easy Way To Find Middle of the Linked List | 93% Faster | Fast and Simple Solution | pniraj657 | 16 | 1,500 | middle of the linked list | 876 | 0.739 | Easy | 14,235 |
https://leetcode.com/problems/middle-of-the-linked-list/discuss/2240481/Python-Floyd's-Tortoise-and-Hare-Algorithm-Time-O(N)-or-Space-O(1) | class Solution:
def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
slow = head
fast = head
while fast and fast.next:
slow = slow.next # Move by one node ahead
fast = fast.next.next # Move by two nodes ahead
return slow | middle-of-the-linked-list | [Python] Floyd's Tortoise & Hare Algorithm - Time O(N) | Space O(1) | Symbolistic | 11 | 318 | middle of the linked list | 876 | 0.739 | Easy | 14,236 |
https://leetcode.com/problems/middle-of-the-linked-list/discuss/2216680/Python-oror-2-pointer-oror-explanation-oror-analysis | class Solution:
def middleNode(self, head: ListNode) -> ListNode:
fast=slow=head
while(fast and fast.next):
fast=fast.next.next
slow=slow.next
return slow | middle-of-the-linked-list | Python || 2-pointer || explanation || analysis | palashbajpai214 | 5 | 96 | middle of the linked list | 876 | 0.739 | Easy | 14,237 |
https://leetcode.com/problems/middle-of-the-linked-list/discuss/1856260/2-Python-Solutions | class Solution:
def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
ans=[]
while head: ans.append(head) ; head=head.next
return ans[len(ans)//2] | middle-of-the-linked-list | 2 Python Solutions | Taha-C | 4 | 134 | middle of the linked list | 876 | 0.739 | Easy | 14,238 |
https://leetcode.com/problems/middle-of-the-linked-list/discuss/1856260/2-Python-Solutions | class Solution:
def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
slow=fast=head
while fast and fast.next: slow=slow.next ; fast=fast.next.next
return slow | middle-of-the-linked-list | 2 Python Solutions | Taha-C | 4 | 134 | middle of the linked list | 876 | 0.739 | Easy | 14,239 |
https://leetcode.com/problems/middle-of-the-linked-list/discuss/1561140/Python-easy-fast-solution-with-explanation-(one-pass)-Memory-Usage-less-than-96.79 | class Solution(object):
def middleNode(self, head):
slow = head
fast = head
while fast != None and fast.next != None: #if fast.next is not None yet, then fast.next.next would only be none the worst case scinario, it wouldn't throw it doesn't exist error
slow = ... | middle-of-the-linked-list | Python easy fast solution with explanation (one pass) Memory Usage less than 96.79% | Abeni | 4 | 234 | middle of the linked list | 876 | 0.739 | Easy | 14,240 |
https://leetcode.com/problems/middle-of-the-linked-list/discuss/1767217/Python-3-(20ms)-or-Slow-and-Fast-Pointers-or-3-Lines-One-Pass-Solution | class Solution:
def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
s=head
while head and head.next:
s,head=s.next,head.next.next
return s | middle-of-the-linked-list | Python 3 (20ms) | Slow and Fast Pointers | 3 Lines One Pass Solution | MrShobhit | 2 | 212 | middle of the linked list | 876 | 0.739 | Easy | 14,241 |
https://leetcode.com/problems/middle-of-the-linked-list/discuss/1651972/Python3-SLOW-AND-FAST-Explained | class Solution:
def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
return slow | middle-of-the-linked-list | ✔️ [Python3] SLOW AND FAST, Explained | artod | 2 | 96 | middle of the linked list | 876 | 0.739 | Easy | 14,242 |
https://leetcode.com/problems/middle-of-the-linked-list/discuss/2088422/Python-solution-using-slow-and-fast-pointers | class Solution(object):
def middleNode(self, head):
fast_pointer=slow_pointer=head
#fastP moves two steps at a time faster and slowP moves one step at a time
# 1 2 3 4 5
# f/s --> initially
# s ... | middle-of-the-linked-list | Python solution using slow and fast pointers | varudinesh225 | 1 | 111 | middle of the linked list | 876 | 0.739 | Easy | 14,243 |
https://leetcode.com/problems/middle-of-the-linked-list/discuss/2010166/Python-Solution-greater-99 | class Solution(object):
def middleNode(self, head):
res = []
oldHead = head
counter = 0
secondCount = 0
#first find the middle
while(head != None):
head = head.next
counter += 1
# return the head once you reached the middle
while(oldHead !... | middle-of-the-linked-list | Python Solution > 99% | felixplease | 1 | 147 | middle of the linked list | 876 | 0.739 | Easy | 14,244 |
https://leetcode.com/problems/middle-of-the-linked-list/discuss/1881754/Python-3-or-two-solution-(1)-1-and-half-pass-(2)-one-pass | class Solution:
def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
ct=0
curr=head
while curr:
curr=curr.next
ct+=1
ct=ct//2
while ct:
head=head.next
ct-=1
return head | middle-of-the-linked-list | Python 3 | two solution (1) 1 and half pass (2) one pass | Anilchouhan181 | 1 | 85 | middle of the linked list | 876 | 0.739 | Easy | 14,245 |
https://leetcode.com/problems/middle-of-the-linked-list/discuss/1881754/Python-3-or-two-solution-(1)-1-and-half-pass-(2)-one-pass | class Solution:
def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
single,double=head,head
while double and double.next:
single=single.next
double=double.next.next
return single | middle-of-the-linked-list | Python 3 | two solution (1) 1 and half pass (2) one pass | Anilchouhan181 | 1 | 85 | middle of the linked list | 876 | 0.739 | Easy | 14,246 |
https://leetcode.com/problems/middle-of-the-linked-list/discuss/1714884/Python-Simple-approach-(calculating-mid-)-O(n)-Time | class Solution:
def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
"""Approach: First count total no of nodes, then decide the mid node. Then move head till it reaches mid node."""
curr = head
count = 1
headPosition = 1
mid = 0
"""... | middle-of-the-linked-list | Python Simple approach (calculating mid ), O(n) Time | abhisheksharma5023 | 1 | 64 | middle of the linked list | 876 | 0.739 | Easy | 14,247 |
https://leetcode.com/problems/middle-of-the-linked-list/discuss/1602927/3-methods-explained-oror-O(n)-Faster-than-96 | class Solution:
def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
mid = head
count = 0
while(head!=None):
if count&1:
mid = mid.next
count+=1
head = head.next
return mid | middle-of-the-linked-list | 3 methods - explained || O(n) Faster than 96% | ana_2kacer | 1 | 164 | middle of the linked list | 876 | 0.739 | Easy | 14,248 |
https://leetcode.com/problems/middle-of-the-linked-list/discuss/1602927/3-methods-explained-oror-O(n)-Faster-than-96 | class Solution:
def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
return slow | middle-of-the-linked-list | 3 methods - explained || O(n) Faster than 96% | ana_2kacer | 1 | 164 | middle of the linked list | 876 | 0.739 | Easy | 14,249 |
https://leetcode.com/problems/middle-of-the-linked-list/discuss/1602927/3-methods-explained-oror-O(n)-Faster-than-96 | class Solution:
def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
ptr = head
n = 0
while(ptr!=None):
n+=1
ptr = ptr.next
ptr = head
# print(n)
i = 0
while i!=n//2 and ptr!=None:
# print(i)
... | middle-of-the-linked-list | 3 methods - explained || O(n) Faster than 96% | ana_2kacer | 1 | 164 | middle of the linked list | 876 | 0.739 | Easy | 14,250 |
https://leetcode.com/problems/middle-of-the-linked-list/discuss/1586696/python3-two-pointers-soln-96-faster | class Solution:
def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
p = head
pf = head
while pf and pf.next:
pf = pf.next.next
p = p.next
return p | middle-of-the-linked-list | python3 two pointers soln 96% faster | msugamsingh | 1 | 152 | middle of the linked list | 876 | 0.739 | Easy | 14,251 |
https://leetcode.com/problems/middle-of-the-linked-list/discuss/1483447/Python3-2-pointers | class Solution:
def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
slow = fast = head
while fast and fast.next:
fast = fast.next.next
slow = slow.next
return slow | middle-of-the-linked-list | [Python3] 2 pointers | ye15 | 1 | 36 | middle of the linked list | 876 | 0.739 | Easy | 14,252 |
https://leetcode.com/problems/middle-of-the-linked-list/discuss/1386950/Python3-Fast-Pointer-Slow-Pointer-99-90 | class Solution:
def middleNode(self, head: ListNode) -> ListNode:
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
return slow | middle-of-the-linked-list | [Python3] Fast Pointer Slow Pointer 99%, 90% | whitehatbuds | 1 | 139 | middle of the linked list | 876 | 0.739 | Easy | 14,253 |
https://leetcode.com/problems/middle-of-the-linked-list/discuss/1340306/Python3-faster-than-99.16 | class Solution:
def middleNode(self, head: ListNode) -> ListNode:
fast=head
slow=head
length=0
curr=head
while(curr):
curr=curr.next
length+=1
while(fast.next and fast.next.next):
fast=fast.next.next
slow=slow.next
return slow if length%2!=0 else slow.next | middle-of-the-linked-list | Python3 faster than 99.16% | samarthnehe | 1 | 118 | middle of the linked list | 876 | 0.739 | Easy | 14,254 |
https://leetcode.com/problems/middle-of-the-linked-list/discuss/468329/Python-3-(four-lines)-(beats-~100) | class Solution:
def middleNode(self, H: ListNode) -> ListNode:
N, L = H, 0
while N != None: L, N = L + 1, N.next
for i in range(L//2): H = H.next
return H
- Junaid Mansuri
- Chicago, IL | middle-of-the-linked-list | Python 3 (four lines) (beats ~100%) | junaidmansuri | 1 | 353 | middle of the linked list | 876 | 0.739 | Easy | 14,255 |
https://leetcode.com/problems/middle-of-the-linked-list/discuss/461860/Tortoise-and-Hare-Algorithm-in-Python. | class Solution:
def middleNode(self, head: ListNode) -> ListNode:
temp=head
temp1=head
while temp1!=None and temp1.next!=None:
temp=temp.next
temp1=temp1.next.next
return temp | middle-of-the-linked-list | Tortoise and Hare Algorithm in Python. | atriraha | 1 | 49 | middle of the linked list | 876 | 0.739 | Easy | 14,256 |
https://leetcode.com/problems/middle-of-the-linked-list/discuss/2828001/Skipping-The-Best | class Solution:
def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
first, second = head, head.next
while second:
first = first.next
second = second.next.next if second.next else None
return first | middle-of-the-linked-list | Skipping, The Best | Triquetra | 0 | 1 | middle of the linked list | 876 | 0.739 | Easy | 14,257 |
https://leetcode.com/problems/middle-of-the-linked-list/discuss/2817857/Easy | class Solution:
def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
counter = 0
temp = head
while temp is not None:
counter += 1
temp = temp.next
index = math.ceil(counter / 2)
counter = 1 if counter % 2 != 0 else 0
while h... | middle-of-the-linked-list | Easy | pkozhem | 0 | 4 | middle of the linked list | 876 | 0.739 | Easy | 14,258 |
https://leetcode.com/problems/middle-of-the-linked-list/discuss/2755935/876.-Middle-of-the-Linked-List-or-Python-Solution | class Solution(object):
def middleNode(self, head):
temp = head
count = 0
while temp:
temp = temp.next
count+=1
mid = (count // 2)
temp = head
for i in range(mid):
temp = temp.next
return temp
```
Please upvote if helpful... | middle-of-the-linked-list | 876. Middle of the Linked List | Python Solution | ygygupta0 | 0 | 5 | middle of the linked list | 876 | 0.739 | Easy | 14,259 |
https://leetcode.com/problems/middle-of-the-linked-list/discuss/2730634/O(n2)-and-O(n)-two-python-solutions-(faster-than-99.94) | class Solution:
def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
mid = l1 = head
while l1:
if l1.next:
if l1.next.next:
mid = mid.next
l1 = l1.next.next
continue
return mid.ne... | middle-of-the-linked-list | O(n/2) and O(n) two python solutions (faster than 99.94%) | naseemh119 | 0 | 7 | middle of the linked list | 876 | 0.739 | Easy | 14,260 |
https://leetcode.com/problems/middle-of-the-linked-list/discuss/2730634/O(n2)-and-O(n)-two-python-solutions-(faster-than-99.94) | class Solution:
def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
mid = l1 = head
c = 1
m = 1
while l1:
t = m
m = int(c/2) + 1
if t != m:
mid = mid.next
c += 1
l1 = l1.next
... | middle-of-the-linked-list | O(n/2) and O(n) two python solutions (faster than 99.94%) | naseemh119 | 0 | 7 | middle of the linked list | 876 | 0.739 | Easy | 14,261 |
https://leetcode.com/problems/middle-of-the-linked-list/discuss/2724160/Easy-Python3-Solution | class Solution:
def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
pos=0
curr=head
while(curr!=None):
pos+=1
curr=curr.next
curr=head
for i in range(pos//2):
curr=curr.next
return curr | middle-of-the-linked-list | Easy Python3 Solution | ankitr8055 | 0 | 5 | middle of the linked list | 876 | 0.739 | Easy | 14,262 |
https://leetcode.com/problems/stone-game/discuss/643412/Python-O(-n2-)-by-top-down-DP-w-Comment | class Solution:
def stoneGame(self, piles: List[int]) -> bool:
# Alex always win finally, no matter which step he takes first.
return True | stone-game | Python O( n^2 ) by top-down DP [w/ Comment] | brianchiang_tw | 4 | 642 | stone game | 877 | 0.697 | Medium | 14,263 |
https://leetcode.com/problems/stone-game/discuss/2765540/One-word-solution-oror-return-True | class Solution:
def stoneGame(self, piles: List[int]) -> bool:
return True | stone-game | One word solution || return True | Sneh713 | 1 | 165 | stone game | 877 | 0.697 | Medium | 14,264 |
https://leetcode.com/problems/stone-game/discuss/1709156/O(1)-solution-for-CC%2B%2BCJavaJavaScriptPythonPython3Ruby | class Solution(object):
def stoneGame(self, piles):
"""
:type piles: List[int]
:rtype: bool
"""
return True | stone-game | ✔O(1) solution for C/C++/C#/Java/JavaScript/Python/Python3/Ruby | milochen | 1 | 376 | stone game | 877 | 0.697 | Medium | 14,265 |
https://leetcode.com/problems/stone-game/discuss/1709156/O(1)-solution-for-CC%2B%2BCJavaJavaScriptPythonPython3Ruby | class Solution:
def stoneGame(self, piles: List[int]) -> bool:
return True | stone-game | ✔O(1) solution for C/C++/C#/Java/JavaScript/Python/Python3/Ruby | milochen | 1 | 376 | stone game | 877 | 0.697 | Medium | 14,266 |
https://leetcode.com/problems/stone-game/discuss/876027/Python-One-line-oror-Always-True | class Solution:
def stoneGame(self, piles: List[int]) -> bool:
return True | stone-game | Python One line || Always True | airksh | 1 | 59 | stone game | 877 | 0.697 | Medium | 14,267 |
https://leetcode.com/problems/stone-game/discuss/520725/Python3-dp-O(N2) | class Solution:
def stoneGame(self, piles: List[int]) -> bool:
return True | stone-game | [Python3] dp O(N^2) | ye15 | 1 | 169 | stone game | 877 | 0.697 | Medium | 14,268 |
https://leetcode.com/problems/stone-game/discuss/2814843/Optimal-and-Clean-with-explanation-O(1)-time-and-O(1)-space | class Solution:
# sum(piles) is odd. => NO TIES...
# len(piles) is even => each takes len(piles)//2 piles
# since Alice goes first, she will always pick the greater size piles and WIN
# O(1) time : O(1) space
def stoneGame(self, piles: List[int]) -> bool:
return True | stone-game | Optimal and Clean with explanation - O(1) time and O(1) space | topswe | 0 | 2 | stone game | 877 | 0.697 | Medium | 14,269 |
https://leetcode.com/problems/stone-game/discuss/2811211/Simple-Logic-in-python | class Solution:
def stoneGame(self, piles: List[int]) -> bool:
a = []
b = []
c = len(piles)
for i in range(c):
a.append(max(piles))
piles.remove(max(piles))
if sum(a) > sum(b):
return 1
else:
return 0 | stone-game | Simple Logic in python | Himakar_C | 0 | 1 | stone game | 877 | 0.697 | Medium | 14,270 |
https://leetcode.com/problems/stone-game/discuss/2779791/Python-Easy-Solution-or-Faster-Than-85-or-Easy-To-Understand | class Solution:
def stoneGame(self, piles: List[int]) -> bool:
if len(piles)%2 == 0:
return True
else:
return False | stone-game | Python Easy Solution | Faster Than 85% | Easy To Understand | beingdillig | 0 | 3 | stone game | 877 | 0.697 | Medium | 14,271 |
https://leetcode.com/problems/stone-game/discuss/2677380/East-python-solution-with-DP-and-memo | class Solution:
def stoneGame(self, piles: List[int]) -> bool:
n = len(piles)
memo = [[0] * n for i in range(n)]
def dp(i, j):
if (i > j): return 0
if (memo[i][j] != 0):
return memo[i][j]
player_turn = (n - (j - i)) % ... | stone-game | East python solution with DP and memo | leqinancy | 0 | 10 | stone game | 877 | 0.697 | Medium | 14,272 |
https://leetcode.com/problems/stone-game/discuss/2653032/Easy-Understandable-Python-Solution-Beats-99-or-O(N) | class Solution:
def stoneGame(self, piles: List[int]) -> bool:
alicePile = 0
bobPile = 0
length = len(piles)
for i in range(length):
currentValue = 0
if piles[0] > piles[-1]:
currentValue = piles.pop(0)
else:
curren... | stone-game | Easy Understandable Python Solution Beats 99% | O(N) | perabjoth | 0 | 5 | stone game | 877 | 0.697 | Medium | 14,273 |
https://leetcode.com/problems/stone-game/discuss/2562806/python3or-easy | class Solution:
def stoneGame(self, piles: List[int]) -> bool:
a =0
b = 0
while piles:
if piles[0]>=piles[-1]:
a+=piles.pop(0)
else:
a+=piles.pop()
if piles[0]>=piles[-1]:
a+=piles.pop(0)
else:
... | stone-game | python3| easy | rohannayar8 | 0 | 27 | stone game | 877 | 0.697 | Medium | 14,274 |
https://leetcode.com/problems/stone-game/discuss/1544957/VERY-SIMPLE-AND-EASY-SOLN-(faster-than-94) | class Solution:
def stoneGame(self, piles: List[int]) -> bool:
Alice = Bob = idx = 0
piles.sort()
piles.reverse()
while idx != len(piles):
Alice += piles[idx]
idx += 1
Bob += piles[idx]
idx += 1
return True if Alice>Bob else Fal... | stone-game | VERY SIMPLE AND EASY SOLN (faster than 94%) | anandanshul001 | 0 | 204 | stone game | 877 | 0.697 | Medium | 14,275 |
https://leetcode.com/problems/stone-game/discuss/1388120/Simple-Python-Solution | class Solution:
def stoneGame(self, piles: List[int]) -> bool:
alexs_turn = True
alex_score = lee_score = 0
while piles:
if alexs_turn:
if piles[0] > piles[-1]:
alex_score += piles.pop(0)
else:
... | stone-game | Simple Python Solution | anandudit | 0 | 81 | stone game | 877 | 0.697 | Medium | 14,276 |
https://leetcode.com/problems/stone-game/discuss/1386037/Python-2-pointer-approach | class Solution:
def stoneGame(self, piles: List[int]) -> bool:
alexsTurn = True
alex = lee = left = 0
right = len(piles) - 1
while left <= right:
if piles[left] > piles[right]:
if alexsTurn:
alex += piles[left]
... | stone-game | [Python] 2 pointer approach | genefever | 0 | 75 | stone game | 877 | 0.697 | Medium | 14,277 |
https://leetcode.com/problems/stone-game/discuss/1385971/Python-Solution-Faster-than-96-with-Sorting. | class Solution(object):
def stoneGame(self, piles):
s= []
piles.sort(reverse=True)
for i in range(len(piles)):
s.append(i)
if sum(piles)>sum(s):
return True | stone-game | [Python] Solution; Faster than 96%, with Sorting. | balajimj2824 | 0 | 89 | stone game | 877 | 0.697 | Medium | 14,278 |
https://leetcode.com/problems/stone-game/discuss/1331561/Simple-DFS-DP-returning-difference-with-which-current-player-wins-(caller-of-function) | class Solution:
def stoneGame(self, piles: List[int]) -> bool:
@functools.cache
def doIWin(l,r):
if l > r: return 0
left = piles[l] - doIWin(l+1,r) # `-` because my opponent!
right= piles[r] - doIWin(l,r-1)
return... | stone-game | Simple DFS DP, returning difference with which current player wins (caller of function) | yozaam | 0 | 151 | stone game | 877 | 0.697 | Medium | 14,279 |
https://leetcode.com/problems/nth-magical-number/discuss/1545825/Python3-binary-search | class Solution:
def nthMagicalNumber(self, n: int, a: int, b: int) -> int:
# inclusion-exclusion principle
ab = lcm(a,b)
lo, hi = 0, n*min(a, b)
while lo < hi:
mid = lo + hi >> 1
if mid//a + mid//b - mid//ab < n: lo = mid + 1
else: hi = mid
... | nth-magical-number | [Python3] binary search | ye15 | 1 | 106 | nth magical number | 878 | 0.357 | Hard | 14,280 |
https://leetcode.com/problems/nth-magical-number/discuss/1049740/A-pattern-based-solution-using-lcm-in-Python3 | class Solution:
def nthMagicalNumber(self, n: int, a: int, b: int) -> int:
def gcd(x,y):
if(x==0):
return y
return gcd(y%x,x)
lcm=(a*b)//gcd(a,b)
s=set()
x=a
while(x<=lcm):
s.add(x)
x+=a
x=b
while... | nth-magical-number | A pattern based solution using lcm in Python3 | _Rehan12 | 0 | 75 | nth magical number | 878 | 0.357 | Hard | 14,281 |
https://leetcode.com/problems/profitable-schemes/discuss/2661178/Python3-DP | class Solution:
def profitableSchemes(self, n: int, minProfit: int, group: List[int], profit: List[int]) -> int:
# A[i][j][k] = # schemes using subset of first i crimes, using <= j people, with total profit >= k
A = [[[0 for k in range(minProfit + 1)] for j in range(n + 1)] for i in range(len(profi... | profitable-schemes | Python3 DP | jbradleyglenn | 0 | 11 | profitable schemes | 879 | 0.404 | Hard | 14,282 |
https://leetcode.com/problems/profitable-schemes/discuss/1516885/Python3-dp | class Solution:
def profitableSchemes(self, n: int, minProfit: int, group: List[int], profit: List[int]) -> int:
m = len(group)
dp = [[[0] * (minProfit+1) for _ in range(n+1)] for _ in range(m+1)]
for j in range(n+1): dp[m][j][0] = 1
for i in range(m-1, -1, -1):
for j in... | profitable-schemes | [Python3] dp | ye15 | 0 | 73 | profitable schemes | 879 | 0.404 | Hard | 14,283 |
https://leetcode.com/problems/profitable-schemes/discuss/1516885/Python3-dp | class Solution:
def profitableSchemes(self, n: int, minProfit: int, group: List[int], profit: List[int]) -> int:
dp = [[0]*(1 + n) for _ in range(1 + minProfit)]
dp[0][0] = 1
for p, g in zip(profit, group):
for i in range(minProfit, -1, -1):
for j in range(n - g... | profitable-schemes | [Python3] dp | ye15 | 0 | 73 | profitable schemes | 879 | 0.404 | Hard | 14,284 |
https://leetcode.com/problems/profitable-schemes/discuss/1516885/Python3-dp | class Solution:
def profitableSchemes(self, n: int, minProfit: int, group: List[int], profit: List[int]) -> int:
MOD = 1_000_000_007
@cache
def fn(i, n, p):
"""Return count at i with n people remaining and p profit to make."""
if n < 0: return 0
... | profitable-schemes | [Python3] dp | ye15 | 0 | 73 | profitable schemes | 879 | 0.404 | Hard | 14,285 |
https://leetcode.com/problems/decoded-string-at-index/discuss/1585059/Python3-Solution-with-using-stack | class Solution:
def decodeAtIndex(self, s: str, k: int) -> str:
lens = [0]
for c in s:
if c.isalpha():
lens.append(lens[-1] + 1)
else:
lens.append(lens[-1] * int(c))
for idx in range(len(s), 0, -1):
... | decoded-string-at-index | [Python3] Solution with using stack | maosipov11 | 2 | 217 | decoded string at index | 880 | 0.283 | Medium | 14,286 |
https://leetcode.com/problems/decoded-string-at-index/discuss/980937/Python3-stack-O(N) | class Solution:
def decodeAtIndex(self, S: str, K: int) -> str:
K -= 1 # 0-indexed
stack = []
i = 0
for c in S:
if c.isdigit(): i *= int(c)
else:
stack.append((i, c))
if K <= i: break
i += 1
... | decoded-string-at-index | [Python3] stack O(N) | ye15 | 1 | 162 | decoded string at index | 880 | 0.283 | Medium | 14,287 |
https://leetcode.com/problems/decoded-string-at-index/discuss/980937/Python3-stack-O(N) | class Solution:
def decodeAtIndex(self, S: str, K: int) -> str:
k = 0
for i, c in enumerate(S):
k = k+1 if c.isalpha() else k*int(c)
if K <= k: break
for ii in reversed(range(i+1)):
if S[ii].isalpha():
if K in (k, 0): re... | decoded-string-at-index | [Python3] stack O(N) | ye15 | 1 | 162 | decoded string at index | 880 | 0.283 | Medium | 14,288 |
https://leetcode.com/problems/decoded-string-at-index/discuss/979627/Beats-89-in-time-time-complexity-O(n) | class Solution:
def decodeAtIndex(self, S: str, K: int) -> str:
# e.g. S == 'ab2c3d'
# ls_len: list of lengths of new patterns [2, 5] (ab, ababc)
# ls_total_len: list of lengths of times * new pattern [4, 15] (abab, ababcababcababc)
# ls_pattern: list of new additional pattern ['ab', 'c']
ls_len = [... | decoded-string-at-index | Beats 89% in time, time complexity O(n) | leonine9 | 0 | 71 | decoded string at index | 880 | 0.283 | Medium | 14,289 |
https://leetcode.com/problems/boats-to-save-people/discuss/1878155/Explained-Python-2-Pointers-Solution | class Solution:
def numRescueBoats(self, people: List[int], limit: int) -> int:
people.sort()
lo = 0
hi = len(people)-1
boats = 0
while lo <= hi:
if people[lo] + people[hi] <= limit:
lo += 1
hi -= 1
else:
... | boats-to-save-people | ⭐Explained Python 2 Pointers Solution | anCoderr | 26 | 3,400 | boats to save people | 881 | 0.527 | Medium | 14,290 |
https://leetcode.com/problems/boats-to-save-people/discuss/1879381/Python-Easy-Solution | class Solution:
def numRescueBoats(self, people: List[int], limit: int) -> int:
people.sort()
lo, hi, ans = 0, len(people) - 1, 0
while lo <= hi:
if people[hi] + people[lo] <= limit: lo += 1
hi -= 1; ans += 1
return ans | boats-to-save-people | ✅ Python Easy Solution | dhananjay79 | 3 | 147 | boats to save people | 881 | 0.527 | Medium | 14,291 |
https://leetcode.com/problems/boats-to-save-people/discuss/1015671/Easy-and-Clear-Solution-Python-3 | class Solution:
def numRescueBoats(self, people: List[int], limit: int) -> int:
i,j,res=0,len(people)-1,0
people.sort(reverse=True)
while i<j:
if people[i]+people[j]<=limit:
j-=1
res+=1
i+=1
if i==j:
res+=1
ret... | boats-to-save-people | Easy & Clear Solution Python 3 | moazmar | 2 | 201 | boats to save people | 881 | 0.527 | Medium | 14,292 |
https://leetcode.com/problems/boats-to-save-people/discuss/1880035/Easy-Python-Solution | class Solution:
def numRescueBoats(self, people: List[int], limit: int) -> int:
people.sort()
res = 0
while(len(people)):
res += 1
if(len(people) == 1): break
if(people[0] + people[-1] <= limit): people.pop(0)
people.pop()
return(res) | boats-to-save-people | Easy Python Solution | demonKing_253 | 1 | 23 | boats to save people | 881 | 0.527 | Medium | 14,293 |
https://leetcode.com/problems/boats-to-save-people/discuss/2841802/Two-pointers-solution | class Solution:
def numRescueBoats(self, people: List[int], limit: int) -> int:
res = 0
people.sort()
left = 0
right = len(people) - 1
while left <= right:
weight = people[left] + people[right]
if weight <= limit:
left ... | boats-to-save-people | Two pointers solution | khaled_achech | 0 | 1 | boats to save people | 881 | 0.527 | Medium | 14,294 |
https://leetcode.com/problems/boats-to-save-people/discuss/2795074/Two-pointer-approach-or-Easy-Python-Solution | class Solution:
def numRescueBoats(self, people: List[int], limit: int) -> int:
people.sort()
p1, p2,res = 0, len(people) - 1, 0
while p1 <= p2:
if people[p1] + people[p2] <= limit:
p1 += 1
res += 1
p2 -= 1
return res | boats-to-save-people | Two pointer approach | Easy Python Solution | mrpranavr | 0 | 1 | boats to save people | 881 | 0.527 | Medium | 14,295 |
https://leetcode.com/problems/boats-to-save-people/discuss/2749034/two-pointers! | class Solution(object):
def numRescueBoats(self, people, limit):
people.sort()
left, right = 0, len(people) - 1
boats_num = 0
while left <= right:
if(left==right):
boats_num += 1
break
if people[left] + people[right] <= limit:
... | boats-to-save-people | two pointers! | sanjeevpathak | 0 | 2 | boats to save people | 881 | 0.527 | Medium | 14,296 |
https://leetcode.com/problems/boats-to-save-people/discuss/2749018/Easy-Solution!!! | class Solution(object):
def numRescueBoats(self, people, limit):
people.sort()
i, j = 0, len(people) - 1
ans = 0
while i <= j:
ans += 1
if people[i] + people[j] <= limit:
i += 1
j -= 1
return ans | boats-to-save-people | Easy Solution!!! | sanjeevpathak | 0 | 1 | boats to save people | 881 | 0.527 | Medium | 14,297 |
https://leetcode.com/problems/boats-to-save-people/discuss/2728477/python-working-solution | class Solution:
def numRescueBoats(self, people: List[int], limit: int) -> int:
people.sort()
l = 0
r = len(people)-1
boats = 0
while l<=r:
if people[l] + people[r] > limit:
boats+=1
r-=1
elif people[l] + people[r] <= li... | boats-to-save-people | python working solution | Sayyad-Abdul-Latif | 0 | 2 | boats to save people | 881 | 0.527 | Medium | 14,298 |
https://leetcode.com/problems/boats-to-save-people/discuss/2714231/Python-3-Solution | class Solution:
def numRescueBoats(self, people: List[int], limit: int) -> int:
people.sort()
l, r = 0, len(people)-1
boats = 0
while l<=r:
if people[l] + people[r] <= limit:
l += 1
boats += 1
r -= 1
return b... | boats-to-save-people | Python 3 Solution | mati44 | 0 | 1 | boats to save people | 881 | 0.527 | Medium | 14,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.