post_href stringlengths 57 213 | python_solutions stringlengths 71 22.3k | slug stringlengths 3 77 | post_title stringlengths 1 100 | user stringlengths 3 29 | upvotes int64 -20 1.2k | views int64 0 60.9k | problem_title stringlengths 3 77 | number int64 1 2.48k | acceptance float64 0.14 0.91 | difficulty stringclasses 3
values | __index_level_0__ int64 0 34k |
|---|---|---|---|---|---|---|---|---|---|---|---|
https://leetcode.com/problems/minimum-moves-to-reach-target-score/discuss/1694801/python-simple-recursive-solution | class Solution:
def minMoves(self, target: int, maxDoubles: int) -> int:
@lru_cache(maxsize=1000)
def count(n, doubles):
if n == 1:
return 0
elif doubles == 0:
return n-1
elif n % 2 == 1:
return count(n... | minimum-moves-to-reach-target-score | python simple recursive solution | byuns9334 | 0 | 19 | minimum moves to reach target score | 2,139 | 0.484 | Medium | 29,700 |
https://leetcode.com/problems/minimum-moves-to-reach-target-score/discuss/1693736/python3-ez-greedy-solution | class Solution:
def minMoves(self, target: int, maxDoubles: int) -> int:
step = 0
while target > 1:
if maxDoubles == 0:
return target - 1 + step
if target % 2 == 0 and maxDoubles > 0:
target //= 2
maxDoubles -= 1
els... | minimum-moves-to-reach-target-score | python3 ez greedy solution | yingziqing123 | 0 | 13 | minimum moves to reach target score | 2,139 | 0.484 | Medium | 29,701 |
https://leetcode.com/problems/minimum-moves-to-reach-target-score/discuss/1693601/Easy-Python-Solution(100) | class Solution:
def minMoves(self, target: int, maxDoubles: int) -> int:
count=0
while maxDoubles:
if target==1:
break
if target%2==0:
target//=2
maxDoubles-=1
count+=1
else:
target-=1... | minimum-moves-to-reach-target-score | Easy Python Solution(100%) | Sneh17029 | 0 | 45 | minimum moves to reach target score | 2,139 | 0.484 | Medium | 29,702 |
https://leetcode.com/problems/minimum-moves-to-reach-target-score/discuss/1693505/Python-or-Reduce-target-to-1 | class Solution:
def minMoves(self, target: int, maxDoubles: int) -> int:
ans = 0
while target > 1:
if maxDoubles < 1:
ans += target - 1
break
half = target // 2
ans = ans + 1 + target % 2
maxDoubles = maxDoubles - 1
... | minimum-moves-to-reach-target-score | Python | Reduce target to 1 | AsifIqbal1997 | 0 | 10 | minimum moves to reach target score | 2,139 | 0.484 | Medium | 29,703 |
https://leetcode.com/problems/minimum-moves-to-reach-target-score/discuss/1693295/Python-3-Go-backwards-solution | class Solution:
def minMoves(self, target: int, maxDoubles: int) -> int:
step = 0
while target > 1:
if target % 2 == 0:
if maxDoubles > 0:
target //= 2
maxDoubles -= 1
step += 1
else:
... | minimum-moves-to-reach-target-score | [Python 3] Go backwards solution | Lanzhou | 0 | 14 | minimum moves to reach target score | 2,139 | 0.484 | Medium | 29,704 |
https://leetcode.com/problems/minimum-moves-to-reach-target-score/discuss/1693291/python3-solution | class Solution:
def minMoves(self, target: int, maxDoubles: int) -> int:
ans=0
while target>1:
if maxDoubles and target%2==0:
ans+=1
maxDoubles-=1
target//=2
elif maxDoubles==0:
return ans+target-1
el... | minimum-moves-to-reach-target-score | python3 solution | shreyasjain0912 | 0 | 10 | minimum moves to reach target score | 2,139 | 0.484 | Medium | 29,705 |
https://leetcode.com/problems/minimum-moves-to-reach-target-score/discuss/1693216/Python3-O(lg-maxDoubles)-greedy-algo.-w-explanation | class Solution:
def minMoves(self, target: int, maxDoubles: int) -> int:
steps = 0
while target != 1:
if maxDoubles == 0:
return steps + target- 1
elif target & 1:
target -= 1
steps += 1
elif maxDoubles:
... | minimum-moves-to-reach-target-score | [Python3] O(lg maxDoubles) greedy algo. w/ explanation | dwschrute | 0 | 18 | minimum moves to reach target score | 2,139 | 0.484 | Medium | 29,706 |
https://leetcode.com/problems/minimum-moves-to-reach-target-score/discuss/1693203/Python-3-Solution-Faster-than-100-of-Python-online-submissions-Easy-way | class Solution:
def minMoves(self, target: int, maxDoubles: int) -> int:
step = 0
while target != 1 :
if target % 2 == 1:
if maxDoubles == 0:
step += target -1
target = 1
else:
target -= 1
... | minimum-moves-to-reach-target-score | Python 3 - Solution - Faster than 100% of Python online submissions - Easy way | Cheems_Coder | 0 | 17 | minimum moves to reach target score | 2,139 | 0.484 | Medium | 29,707 |
https://leetcode.com/problems/minimum-moves-to-reach-target-score/discuss/1693200/Python-or-Easy-and-fast-solution | class Solution:
def minMoves(self, target: int, maxDoubles: int) -> int:
if target == 1:
return 0
step = 0
while target:
if target == 2:
step += 1
break
if target % 2:
target -= 1
... | minimum-moves-to-reach-target-score | Python | Easy and fast solution | ljinuw | 0 | 14 | minimum moves to reach target score | 2,139 | 0.484 | Medium | 29,708 |
https://leetcode.com/problems/minimum-moves-to-reach-target-score/discuss/1693164/Time-limit-exceeded-how-to-make-it-time-efficient | class Solution:
def minMoves(self, target: int, maxDoubles: int) -> int:
# target = 10
# maxDoubles = 4
steps = 0
if maxDoubles == 0:
return(target-1)
else:
while target!=1 :
if target%2 == 0 and maxDoubles > 0:
target = ta... | minimum-moves-to-reach-target-score | Time limit exceeded , how to make it time efficient? | menlam3 | 0 | 31 | minimum moves to reach target score | 2,139 | 0.484 | Medium | 29,709 |
https://leetcode.com/problems/minimum-moves-to-reach-target-score/discuss/1692970/Python3-Solution-oror-For-beginner-oror-Easy-way | class Solution:
def minMoves(self, target: int, maxDoubles: int) -> int:
step = 0
while target != 1 :
if target % 2 == 1:
if maxDoubles == 0:
step += target -1
target = 1
else:
target -= 1
... | minimum-moves-to-reach-target-score | [Python3] - Solution || For beginner || Easy way | Cheems_Coder | 0 | 21 | minimum moves to reach target score | 2,139 | 0.484 | Medium | 29,710 |
https://leetcode.com/problems/solving-questions-with-brainpower/discuss/1692963/DP | class Solution:
def mostPoints(self, q: List[List[int]]) -> int:
@cache
def dfs(i: int) -> int:
return 0 if i >= len(q) else max(dfs(i + 1), q[i][0] + dfs(i + 1 + q[i][1]))
return dfs(0) | solving-questions-with-brainpower | DP | votrubac | 132 | 5,500 | solving questions with brainpower | 2,140 | 0.46 | Medium | 29,711 |
https://leetcode.com/problems/solving-questions-with-brainpower/discuss/1693569/Simple-DP-%2B-Memoization-Solution-or-Python-or-Beats-100-Time-and-Space | class Solution:
def mostPoints(self, questions: List[List[int]]) -> int:
n = len(questions) # total number of questions
memo = [-1] * n # memo array of size n.
# If memo[i] is not computed then its value must be -1 and we need to find memo[i]
# If memo[i] != -1, this means we have already calcu... | solving-questions-with-brainpower | Simple DP + Memoization Solution | Python | Beats 100% Time and Space | anCoderr | 4 | 111 | solving questions with brainpower | 2,140 | 0.46 | Medium | 29,712 |
https://leetcode.com/problems/solving-questions-with-brainpower/discuss/1692910/Python3-House-Robber-Variation-or-DP-Bottom-Up | class Solution:
def mostPoints(self, questions: List[List[int]]) -> int:
dp = [(0, 0)] * (len(questions) + 1)
for i in range(len(questions) - 1, -1, -1):
score, delay = questions[i]
dp[i] = score + (max(dp[i + delay + 1]) if i + delay + 1 < len(questions) else 0), max(dp[i + ... | solving-questions-with-brainpower | ✅ [Python3] House Robber Variation | DP Bottom-Up | PatrickOweijane | 2 | 168 | solving questions with brainpower | 2,140 | 0.46 | Medium | 29,713 |
https://leetcode.com/problems/solving-questions-with-brainpower/discuss/1695208/Python3-Dp-sol-for-reference. | class Solution:
def mostPoints(self, questions: List[List[int]]) -> int:
dp = [0 for _ in range(len(questions)+1)]
for index, value in enumerate(questions):
points, brainpower = value
if brainpower + index + 1 < len(questions):
dp[brainpo... | solving-questions-with-brainpower | [Python3] Dp sol for reference. | vadhri_venkat | 1 | 41 | solving questions with brainpower | 2,140 | 0.46 | Medium | 29,714 |
https://leetcode.com/problems/solving-questions-with-brainpower/discuss/1694979/Python3Heap-A-different-heap-solution-iterate-from-left-to-right | class Solution:
def mostPoints(self, questions: List[List[int]]) -> int:
n = len(questions)
h = [] # (point, idx)
candi = [] # (idx, point)
for i in range(n):
while candi: # we have candidates
if candi[0][0] < i: # this means the cu... | solving-questions-with-brainpower | [Python3/Heap] A different heap solution - iterate from left to right | pureme | 1 | 25 | solving questions with brainpower | 2,140 | 0.46 | Medium | 29,715 |
https://leetcode.com/problems/solving-questions-with-brainpower/discuss/1693993/Memoization-soln | class Solution:
def mostPoints(self, questions: List[List[int]]) -> int:
dp=[-1 for i in range(len(questions)+1)]
return self.recur(0,questions,dp)
def recur(self,i,questions,dp):
if i>len(questions)-1:
return 0
if dp[i]!=-1:
return dp[i]
dp[i]=max... | solving-questions-with-brainpower | Memoization soln | Adolf988 | 1 | 43 | solving questions with brainpower | 2,140 | 0.46 | Medium | 29,716 |
https://leetcode.com/problems/solving-questions-with-brainpower/discuss/1692942/Python3-dp | class Solution:
def mostPoints(self, questions: List[List[int]]) -> int:
n = len(questions)
dp = [0]*(n+1)
for i in range(n-1, -1, -1):
dp[i] = dp[i+1]
cand = questions[i][0]
if i+questions[i][1]+1 <= n: cand += dp[i+questions[i][1]+1]
dp[i] =... | solving-questions-with-brainpower | [Python3] dp | ye15 | 1 | 64 | solving questions with brainpower | 2,140 | 0.46 | Medium | 29,717 |
https://leetcode.com/problems/solving-questions-with-brainpower/discuss/2797240/Python-(Simple-Dynamic-Programming) | class Solution:
def mostPoints(self, questions):
n = len(questions)
dp = [0]*(n+1)
for i in range(n-1,-1,-1):
points, jump = questions[i]
dp[i] = max(points + dp[min(i+1+jump,n)],dp[i+1])
return dp[0] | solving-questions-with-brainpower | Python (Simple Dynamic Programming) | rnotappl | 0 | 3 | solving questions with brainpower | 2,140 | 0.46 | Medium | 29,718 |
https://leetcode.com/problems/solving-questions-with-brainpower/discuss/2774506/Python3-Commented-and-Concise-DP-Solution | class Solution:
def mostPoints(self, questions: List[List[int]]) -> int:
# this is a dynamic programming problem
qss = len(questions)
dp = [0]*(qss+1)
# go through the questions, for each question make the two decisions
for idx, (points, brainpower) in enumerate(questions):... | solving-questions-with-brainpower | [Python3] - Commented and Concise DP Solution | Lucew | 0 | 2 | solving questions with brainpower | 2,140 | 0.46 | Medium | 29,719 |
https://leetcode.com/problems/solving-questions-with-brainpower/discuss/2422983/Python-memoization | class Solution:
def mostPoints(self, questions: List[List[int]]) -> int:
n = len(questions)
lookup ={}
def f(ind, lookup):
if ind == n-1: return questions[n-1][0]
if ind >= n: return 0
if ind in look... | solving-questions-with-brainpower | Python memoization | logeshsrinivasans | 0 | 22 | solving questions with brainpower | 2,140 | 0.46 | Medium | 29,720 |
https://leetcode.com/problems/solving-questions-with-brainpower/discuss/2317655/Python-Solution-or-Recursion-or-DP-or-O(n) | class Solution:
def mostPoints(self, questions: List[List[int]]) -> int:
n=len(questions)
dp=[-1]*(n)
def helper(ind):
if ind==n-1:
return questions[n-1][0]
if ind>=n:
return 0
if dp[ind]!=-1:
return dp[ind]
... | solving-questions-with-brainpower | Python Solution | Recursion | DP | O(n) | Siddharth_singh | 0 | 21 | solving questions with brainpower | 2,140 | 0.46 | Medium | 29,721 |
https://leetcode.com/problems/solving-questions-with-brainpower/discuss/2184764/Fast-easy-python-solution-with-recursion-and-memoization | class Solution:
def mostPoints(self, questions: List[List[int]]) -> int:
memo = {}
def dfs(i: int = 0):
if i >= len(questions):
return 0
elif i in memo:
return memo[i]
else:
points, brainpower = que... | solving-questions-with-brainpower | Fast easy python solution with recursion and memoization | user2855PM | 0 | 33 | solving questions with brainpower | 2,140 | 0.46 | Medium | 29,722 |
https://leetcode.com/problems/solving-questions-with-brainpower/discuss/1918455/python-3-oror-recursive-memoization | class Solution:
def mostPoints(self, questions: List[List[int]]) -> int:
dp = {}
def helper(i):
if i >= len(questions):
return 0
if i in dp:
return dp[i]
points, power = questions[i]
dp... | solving-questions-with-brainpower | python 3 || recursive memoization | dereky4 | 0 | 48 | solving questions with brainpower | 2,140 | 0.46 | Medium | 29,723 |
https://leetcode.com/problems/solving-questions-with-brainpower/discuss/1825229/python-recursive-to-DP | class Solution:
def mostPoints(self, q: List[List[int]]) -> int:
n = len(q)
def dp(i):
if i >= n:
return 0
return max(q[i][0] + dp(i+q[i][1]+1), dp(i+1))
return dp(0) | solving-questions-with-brainpower | python recursive to DP | abkc1221 | 0 | 36 | solving questions with brainpower | 2,140 | 0.46 | Medium | 29,724 |
https://leetcode.com/problems/solving-questions-with-brainpower/discuss/1825229/python-recursive-to-DP | class Solution:
def mostPoints(self, q: List[List[int]]) -> int:
n = len(q)
@functools.lru_cache(None)
def dp(i):
if i >= n:
return 0
return max(q[i][0] + dp(i+q[i][1]+1), dp(i+1))
return dp(0) | solving-questions-with-brainpower | python recursive to DP | abkc1221 | 0 | 36 | solving questions with brainpower | 2,140 | 0.46 | Medium | 29,725 |
https://leetcode.com/problems/solving-questions-with-brainpower/discuss/1825229/python-recursive-to-DP | class Solution:
def mostPoints(self, q: List[List[int]]) -> int:
n = len(q)
dp = [0]*(n+1)
for i in range(n-1, -1, -1):
dp[i] = max(q[i][0] + dp[min(n, i + q[i][1] + 1)], dp[i+1])
return dp[0] | solving-questions-with-brainpower | python recursive to DP | abkc1221 | 0 | 36 | solving questions with brainpower | 2,140 | 0.46 | Medium | 29,726 |
https://leetcode.com/problems/solving-questions-with-brainpower/discuss/1723015/Easy-Python-Solution | class Solution:
def mostPoints(self, questions: List[List[int]]) -> int:
n=len(questions)
profit=[0]*n
rightmax=[0]*n
profit[-1]=questions[-1][0]
rightmax[-1]=profit[-1]
for i in range(n-1,-1,-1):
profit[i]=questions[i][0]
nextindex=i+questions... | solving-questions-with-brainpower | Easy Python Solution | Sneh17029 | 0 | 88 | solving questions with brainpower | 2,140 | 0.46 | Medium | 29,727 |
https://leetcode.com/problems/solving-questions-with-brainpower/discuss/1699852/DP-solution-of-python3(hope-it's-good-to-understand) | class Solution:
def mostPoints(self, questions: List[List[int]]) -> int:
#define dp[i] is the most point I can get before land on question i.
n = len(questions)
dp = [0 for _ in range(n+1)]
ans = 0
for i in range(n):
dp[i+1] = max(dp[i+1],dp[i])
if i+q... | solving-questions-with-brainpower | DP solution of python3(hope it's good to understand) | Jeff871025 | 0 | 33 | solving questions with brainpower | 2,140 | 0.46 | Medium | 29,728 |
https://leetcode.com/problems/solving-questions-with-brainpower/discuss/1694707/Python-3-oror-recursion-%2B-memoization-oror-self-understandable-oror-easy-understanding | class Solution:
def mostPoints(self, questions: List[List[int]]) -> int:
memo={}
def getPoints(index):
if index in memo:
return memo[index]
if index>=len(questions):
return 0
p1=questions[index][0]+getPoints(index+(questions[index][... | solving-questions-with-brainpower | Python 3 || recursion + memoization || self-understandable || easy-understanding | bug_buster | 0 | 20 | solving questions with brainpower | 2,140 | 0.46 | Medium | 29,729 |
https://leetcode.com/problems/solving-questions-with-brainpower/discuss/1694672/Python-DP-solution-with-explanation | class Solution:
def mostPoints(self, questions: List[List[int]]) -> int:
dp = [0 for _ in range(len(questions))] # stores max score if started from that point
dp[-1] = questions[-1][0]
i = len(questions)-2
dp_max = [-1 for _ in range(len(questions))] # stores max seen upto t... | solving-questions-with-brainpower | Python DP solution with explanation | 96sayak | 0 | 33 | solving questions with brainpower | 2,140 | 0.46 | Medium | 29,730 |
https://leetcode.com/problems/solving-questions-with-brainpower/discuss/1693834/Python-3-Approaches-or-Brute-Force-To-Optimal-or-Time-and-Space-Complexity. | class Solution:
def mostPoints(self, questions: List[List[int]]) -> int:
def max_points_earned(ind,questions):
if ind>=len(questions):
return 0
left=questions[ind][0]+max_points_earned(ind+questions[ind][1]+1,questions)
right=max_points_earned(ind+1,ques... | solving-questions-with-brainpower | Python 3 Approaches | Brute Force To Optimal | Time and Space Complexity. | aryanagrawal2310 | 0 | 35 | solving questions with brainpower | 2,140 | 0.46 | Medium | 29,731 |
https://leetcode.com/problems/solving-questions-with-brainpower/discuss/1693834/Python-3-Approaches-or-Brute-Force-To-Optimal-or-Time-and-Space-Complexity. | class Solution:
def mostPoints(self, questions: List[List[int]]) -> int:
def max_points_earned(ind,questions,dp):
if ind>=len(questions):
return 0
if dp[ind]!=-1:
return dp[ind]
left=questions[ind][0]+max_points_earned(ind+questions[ind][1... | solving-questions-with-brainpower | Python 3 Approaches | Brute Force To Optimal | Time and Space Complexity. | aryanagrawal2310 | 0 | 35 | solving questions with brainpower | 2,140 | 0.46 | Medium | 29,732 |
https://leetcode.com/problems/solving-questions-with-brainpower/discuss/1693834/Python-3-Approaches-or-Brute-Force-To-Optimal-or-Time-and-Space-Complexity. | class Solution:
def mostPoints(self, questions: List[List[int]]) -> int:
def max_points_earned(questions):
n=len(questions)
dp=[-1]*n
dp[n-1]=questions[n-1][0]
for i in range(n-2,-1,-1):
first=dp[i+1]
second=questions[i][0]
... | solving-questions-with-brainpower | Python 3 Approaches | Brute Force To Optimal | Time and Space Complexity. | aryanagrawal2310 | 0 | 35 | solving questions with brainpower | 2,140 | 0.46 | Medium | 29,733 |
https://leetcode.com/problems/solving-questions-with-brainpower/discuss/1693628/DP-with-explanation | class Solution:
def mostPoints(self, questions: List[List[int]]) -> int:
dp = [0 for i in range(len(questions))]
dp[-1] = questions[-1][0]
for i in range(len(questions) - 2, -1, -1):
points, brainpower = questions[i]
toAdd = 0
if i + brainpower + ... | solving-questions-with-brainpower | DP with explanation | jdot593 | 0 | 21 | solving questions with brainpower | 2,140 | 0.46 | Medium | 29,734 |
https://leetcode.com/problems/solving-questions-with-brainpower/discuss/1693515/Python3-or-DP-%2B-DFS-%2B-Cache | class Solution:
def mostPoints(self, questions: List[List[int]]) -> int:
dp = {}
def dfs(i):
if i >= len(questions):
return 0
if i in dp:
return dp[i]
dp[i] = max( dfs(i+questions[i][1]+1)... | solving-questions-with-brainpower | Python3 | DP + DFS + Cache | AsifIqbal1997 | 0 | 18 | solving questions with brainpower | 2,140 | 0.46 | Medium | 29,735 |
https://leetcode.com/problems/solving-questions-with-brainpower/discuss/1693487/Python3-Dynamic-Programming | class Solution:
def mostPoints(self, questions: List[List[int]]) -> int:
@cache
def helper(i):
pts, skip = questions[i]
if i+skip+1 < len(questions):
notSkip = pts + helper(i+skip+1)
else:
notSkip = pts
skip = helper(i+1... | solving-questions-with-brainpower | [Python3] Dynamic Programming | Rainyforest | 0 | 14 | solving questions with brainpower | 2,140 | 0.46 | Medium | 29,736 |
https://leetcode.com/problems/solving-questions-with-brainpower/discuss/1693313/Python3-or-DP-or-Simple | class Solution:
def mostPoints(self, questions: List[List[int]]) -> int:
n = len(questions)
matrix = [ 0 for _ in range(n)]
matrix[n-1] = questions[n-1][0]
for i in range(n-2, -1, -1):
points = questions[i][0]
skip = questions[i][1]
... | solving-questions-with-brainpower | Python3 | DP | Simple | letyrodri | 0 | 23 | solving questions with brainpower | 2,140 | 0.46 | Medium | 29,737 |
https://leetcode.com/problems/solving-questions-with-brainpower/discuss/1693147/Python3-13-lines-DP%3A-Top-down-1D-memo-w-explanation | class Solution:
@cache
def dp(self, i):
if i >= self.n:
return 0
return max(
self.dp(i + self.Q[i][1] + 1) + self.Q[i][0],
self.dp(i + 1)
)
def mostPoints(self, questions: List[List[int]]) -> int:
self.n = len(questions)
self.Q = qu... | solving-questions-with-brainpower | [Python3] 13 lines DP: Top-down 1D memo w/ explanation | dwschrute | 0 | 36 | solving questions with brainpower | 2,140 | 0.46 | Medium | 29,738 |
https://leetcode.com/problems/solving-questions-with-brainpower/discuss/1692943/Dynamic-Programming-oror-Python | class Solution:
def mostPoints(self, questions: List[List[int]]) -> int:
length = len(questions)
dp = [0]*length
dp[-1] = questions[-1][0];
for i in range(length - 2, -1, - 1):
if (i + questions[i][1] + 1 < length and i + 1 < length):
dp[i] = ... | solving-questions-with-brainpower | Dynamic Programming || Python | siddp6 | 0 | 78 | solving questions with brainpower | 2,140 | 0.46 | Medium | 29,739 |
https://leetcode.com/problems/solving-questions-with-brainpower/discuss/1692926/Python-or-Bottom-Up-Dynamic-Programming-or-O(N)-Time-and-Space | class Solution:
def mostPoints(self, questions: List[List[int]]) -> int:
n = len(questions)
# set dp array
dp = [0]*n
# start from last
dp [-1] = questions[-1][0]
# start setting the rest
for i in reversed(range(n-1)):
po... | solving-questions-with-brainpower | Python | Bottom Up Dynamic Programming | O(N) Time and Space | rbhandu | 0 | 38 | solving questions with brainpower | 2,140 | 0.46 | Medium | 29,740 |
https://leetcode.com/problems/maximum-running-time-of-n-computers/discuss/1692965/Python3-greedy | class Solution:
def maxRunTime(self, n: int, batteries: List[int]) -> int:
batteries.sort()
extra = sum(batteries[:-n])
batteries = batteries[-n:]
ans = prefix = 0
for i, x in enumerate(batteries):
prefix += x
if i+1 < len(batteries) and ba... | maximum-running-time-of-n-computers | [Python3] greedy | ye15 | 39 | 1,400 | maximum running time of n computers | 2,141 | 0.389 | Hard | 29,741 |
https://leetcode.com/problems/maximum-running-time-of-n-computers/discuss/1695541/Python3-Not-fancy-simulation-solution.-(make-a-barrel-that-can-hold-most-water-with-planks) | class Solution:
def maxRunTime(self, n: int, batteries: List[int]) -> int:
batteries.sort(reverse=True)
refills = batteries[n:]
s = sum(refills)
res = 0
for i in range(n-1, 0, -1):
cur = batteries[i]
prev = batteries[i-1]
if prev == cur:
... | maximum-running-time-of-n-computers | [Python3] Not-fancy, simulation solution. (make a barrel that can hold most water with planks) | mteng8 | 4 | 88 | maximum running time of n computers | 2,141 | 0.389 | Hard | 29,742 |
https://leetcode.com/problems/maximum-running-time-of-n-computers/discuss/1694815/Python-3-Binary-search-with-comments | class Solution:
def maxRunTime(self, n: int, batteries: List[int]) -> int:
l, h = min(batteries), sum(batteries)
batteries.sort()
cands = batteries[-n:]
rest = sum(batteries[:-n])
def bs(t):
tmp = rest
for x in cands:
# all res... | maximum-running-time-of-n-computers | [Python 3] Binary search with comments | chestnut890123 | 0 | 61 | maximum running time of n computers | 2,141 | 0.389 | Hard | 29,743 |
https://leetcode.com/problems/minimum-cost-of-buying-candies-with-discount/discuss/1712364/Python-Simple-solution-or-100-faster-or-O(N-logN)-Time-or-O(1)-Space | class Solution:
def minimumCost(self, cost: List[int]) -> int:
cost.sort(reverse=True)
res, i, N = 0, 0, len(cost)
while i < N:
res += sum(cost[i : i + 2])
i += 3
return res | minimum-cost-of-buying-candies-with-discount | [Python] Simple solution | 100% faster | O(N logN) Time | O(1) Space | eshikashah | 9 | 454 | minimum cost of buying candies with discount | 2,144 | 0.609 | Easy | 29,744 |
https://leetcode.com/problems/minimum-cost-of-buying-candies-with-discount/discuss/1709772/Greedy-solution-in-Python-beats-100-(36ms) | class Solution:
def minimumCost(self, cost: List[int]) -> int:
cost.sort(reverse=True)
res, idx, N = 0, 0, len(cost)
while idx < N:
res += sum(cost[idx : idx + 2])
idx += 3
return res | minimum-cost-of-buying-candies-with-discount | Greedy solution in Python, beats 100% (36ms) | kryuki | 6 | 273 | minimum cost of buying candies with discount | 2,144 | 0.609 | Easy | 29,745 |
https://leetcode.com/problems/minimum-cost-of-buying-candies-with-discount/discuss/1709683/Python3-greedy-1-line | class Solution:
def minimumCost(self, cost: List[int]) -> int:
return sum(x for i, x in enumerate(sorted(cost, reverse=True)) if (i+1)%3) | minimum-cost-of-buying-candies-with-discount | [Python3] greedy 1-line | ye15 | 3 | 126 | minimum cost of buying candies with discount | 2,144 | 0.609 | Easy | 29,746 |
https://leetcode.com/problems/minimum-cost-of-buying-candies-with-discount/discuss/1710875/Python3-Sort-%2B-One-Pass-%2B-Greedy-or-O(n)-Time-or-O(1)-Space | class Solution:
def minimumCost(self, cost: List[int]) -> int:
cost.sort(reverse=True)
bought = res = 0
for p in cost:
if bought < 2:
res += p
bought += 1
else:
bought = 0
return res | minimum-cost-of-buying-candies-with-discount | [Python3] Sort + One Pass + Greedy | O(n) Time | O(1) Space | PatrickOweijane | 2 | 85 | minimum cost of buying candies with discount | 2,144 | 0.609 | Easy | 29,747 |
https://leetcode.com/problems/minimum-cost-of-buying-candies-with-discount/discuss/1709673/Python-Solution-using-Heap-or-Sorting | class Solution:
def minimumCost(self, cost: List[int]) -> int:
cost.sort()
cost = cost[::-1]
ans = 0
n = len(cost)
for i in range(n):
if (i+1)%3!=0:
ans += cost[i]
return ans | minimum-cost-of-buying-candies-with-discount | Python Solution using Heap or Sorting | anCoderr | 2 | 66 | minimum cost of buying candies with discount | 2,144 | 0.609 | Easy | 29,748 |
https://leetcode.com/problems/minimum-cost-of-buying-candies-with-discount/discuss/1709673/Python-Solution-using-Heap-or-Sorting | class Solution:
def minimumCost(self, cost: List[int]) -> int:
max_heap = []
for i in cost:
heappush(max_heap, -i) # make max heap with given costs
ans, n = 0, len(cost)
while n > 0:
# take 2 candies out with their costs added to ans
ans += -heappop(max_heap... | minimum-cost-of-buying-candies-with-discount | Python Solution using Heap or Sorting | anCoderr | 2 | 66 | minimum cost of buying candies with discount | 2,144 | 0.609 | Easy | 29,749 |
https://leetcode.com/problems/minimum-cost-of-buying-candies-with-discount/discuss/1855077/Python-solution-faster-than-94 | class Solution:
def minimumCost(self, cost: List[int]) -> int:
cost.sort(reverse = True)
hops = 1
min_cost = 0
for i in range(len(cost)):
if hops == 1 or hops == 2:
min_cost += cost[i]
hops += 1
elif hops == 3:
h... | minimum-cost-of-buying-candies-with-discount | Python solution faster than 94% | alishak1999 | 1 | 49 | minimum cost of buying candies with discount | 2,144 | 0.609 | Easy | 29,750 |
https://leetcode.com/problems/minimum-cost-of-buying-candies-with-discount/discuss/2801866/python-sort-and-sum | class Solution:
def minimumCost(self, cost: List[int]) -> int:
desc_sorted_cost = sorted(cost, reverse=True)
num_of_candy = len(desc_sorted_cost)
required_cost = 0
for i in range(num_of_candy):
if i % 3 == 0 or i % 3 == 1:
required_cost += desc_sorted_cos... | minimum-cost-of-buying-candies-with-discount | python - sort and sum | prravda | 0 | 6 | minimum cost of buying candies with discount | 2,144 | 0.609 | Easy | 29,751 |
https://leetcode.com/problems/minimum-cost-of-buying-candies-with-discount/discuss/2799747/Python-sorting-buy-and-jump | class Solution:
def minimumCost(self, cost: List[int]) -> int:
cost.sort(reverse=True)
jump = 0
res = 0
cnt = 0
for i in range(len(cost)):
if jump == 1:
jump = 0
cnt = 0
continue
cnt += 1... | minimum-cost-of-buying-candies-with-discount | Python sorting - buy and jump | pandish | 0 | 1 | minimum cost of buying candies with discount | 2,144 | 0.609 | Easy | 29,752 |
https://leetcode.com/problems/minimum-cost-of-buying-candies-with-discount/discuss/2756254/Pythn3-simple-solution | class Solution:
def minimumCost(self, cost: List[int]) -> int:
cost.sort()
count = 0
i = len(cost)-1
while i >= 0:
count += cost[i]
i -= 1
if i >= 0:
count += cost[i]
i -= 1
i -= 1
return count | minimum-cost-of-buying-candies-with-discount | Pythn3 simple solution | EklavyaJoshi | 0 | 1 | minimum cost of buying candies with discount | 2,144 | 0.609 | Easy | 29,753 |
https://leetcode.com/problems/minimum-cost-of-buying-candies-with-discount/discuss/2753252/Reverse-Sort-and-then-skip-every-third-element | class Solution:
def minimumCost(self, cost: List[int]) -> int:
cost.sort()
k,sums=0,0
for i in range(len(cost)-1,-1,-1):
k+=1
if k%3==0:
continue
else:
sums+=cost[i]
return sums | minimum-cost-of-buying-candies-with-discount | Reverse Sort and then skip every third element | sowmika_chaluvadi | 0 | 2 | minimum cost of buying candies with discount | 2,144 | 0.609 | Easy | 29,754 |
https://leetcode.com/problems/minimum-cost-of-buying-candies-with-discount/discuss/2651190/Simple-Python-Solution-or-Sorting | class Solution:
def minimumCost(self, cost: List[int]) -> int:
cost.sort()
n=len(cost)
if n<=2:
return sum(cost)
i=n-1
ans=0
while i>0:
print(cost[i-1], cost[i])
ans+=cost[i-1]+cost[i]
i-=3
if i==0:
a... | minimum-cost-of-buying-candies-with-discount | Simple Python Solution | Sorting | Siddharth_singh | 0 | 2 | minimum cost of buying candies with discount | 2,144 | 0.609 | Easy | 29,755 |
https://leetcode.com/problems/minimum-cost-of-buying-candies-with-discount/discuss/2623179/sorting-approach | class Solution:
def minimumCost(self, cost: List[int]) -> int:
# the goal is to buy the cost of candies for cheap
# sort the array in ascending order
# we can buy the two most expensive
# then take the next most cheap candy (skip)
# we can repeat this process until there's no... | minimum-cost-of-buying-candies-with-discount | sorting approach | andrewnerdimo | 0 | 5 | minimum cost of buying candies with discount | 2,144 | 0.609 | Easy | 29,756 |
https://leetcode.com/problems/minimum-cost-of-buying-candies-with-discount/discuss/2586072/Simple-Python3-Optimal-solution-easy | class Solution:
def minimumCost(self, cost: List[int]) -> int:
total_cost = 0
cost.sort(reverse=True)
for i in range(1,len(cost)+1):
if i%3==0:
continue
tot... | minimum-cost-of-buying-candies-with-discount | Simple Python3 Optimal solution easy | abhisheksanwal745 | 0 | 24 | minimum cost of buying candies with discount | 2,144 | 0.609 | Easy | 29,757 |
https://leetcode.com/problems/minimum-cost-of-buying-candies-with-discount/discuss/1940988/Python-dollarolution-(reverse-sort-and-subtract-every-3rd-element) | class Solution:
def minimumCost(self, cost: List[int]) -> int:
cost = sorted(cost, reverse= True)
s = sum(cost)
for i in range(2,len(cost),3):
s -= cost[i]
return s | minimum-cost-of-buying-candies-with-discount | Python $olution (reverse sort & subtract every 3rd element) | AakRay | 0 | 41 | minimum cost of buying candies with discount | 2,144 | 0.609 | Easy | 29,758 |
https://leetcode.com/problems/minimum-cost-of-buying-candies-with-discount/discuss/1920911/Python-Greedy-Solution-Easy-To-Understand | class Solution:
def minimumCost(self, cost: List[int]) -> int:
if len(cost) < 3:
return sum(cost)
cost.sort()
sm = 0
for i in range(len(cost) - 1, -1, -3):
sm += cost[i]
if i > 0:
sm += cost[i - 1]
... | minimum-cost-of-buying-candies-with-discount | Python Greedy Solution Easy To Understand | Hejita | 0 | 41 | minimum cost of buying candies with discount | 2,144 | 0.609 | Easy | 29,759 |
https://leetcode.com/problems/minimum-cost-of-buying-candies-with-discount/discuss/1794202/1-Line-Python-Solution-oror-40-Faster-oror-Memory-less-than-92 | class Solution:
def minimumCost(self, cost: List[int]) -> int:
return sum([sorted(cost)[::-1][i] for i in range(len(cost)) if i%3!=2]) | minimum-cost-of-buying-candies-with-discount | 1-Line Python Solution || 40% Faster || Memory less than 92% | Taha-C | 0 | 50 | minimum cost of buying candies with discount | 2,144 | 0.609 | Easy | 29,760 |
https://leetcode.com/problems/minimum-cost-of-buying-candies-with-discount/discuss/1775190/Python-Solution-or-O(n)-or-92.93-Memory-efficient | class Solution:
def minimumCost(self, cost: List[int]) -> int:
cost = sorted(cost)
i = -1
c = 0
while(i>=(-1*len(cost))): # Reverse Traversal
if i%3 == 0: # Skip every 3rd index
i -= 1
continue
c += cost[i]
i -= 1
... | minimum-cost-of-buying-candies-with-discount | Python Solution | O(n) | 92.93% Memory efficient | Coding_Tan3 | 0 | 65 | minimum cost of buying candies with discount | 2,144 | 0.609 | Easy | 29,761 |
https://leetcode.com/problems/minimum-cost-of-buying-candies-with-discount/discuss/1762478/Simple-and-Easy-Python-Solution | class Solution:
def minimumCost(self, cost: List[int]) -> int:
if len(cost)<3:
return sum(cost)
cost = sorted(cost,reverse=True)
total=sum(cost)
for i in range(0,len(cost)-2,3):
total-=cost[i+2]
return total | minimum-cost-of-buying-candies-with-discount | Simple and Easy Python Solution | sangam92 | 0 | 26 | minimum cost of buying candies with discount | 2,144 | 0.609 | Easy | 29,762 |
https://leetcode.com/problems/minimum-cost-of-buying-candies-with-discount/discuss/1720579/Python-3-two-line-solution | class Solution:
def minimumCost(self, costs: List[int]) -> int:
costs.sort(reverse=True)
return sum(cost for i, cost in enumerate(costs) if i % 3 != 2) | minimum-cost-of-buying-candies-with-discount | Python 3, two line solution | dereky4 | 0 | 55 | minimum cost of buying candies with discount | 2,144 | 0.609 | Easy | 29,763 |
https://leetcode.com/problems/minimum-cost-of-buying-candies-with-discount/discuss/1710804/Python-using-a-heap-queue | class Solution:
def minimumCost(self, cost: List[int]) -> int:
q = [-v for v in cost]
heapq.heapify(q)
ans = 0
while len(q) >= 3:
ans += heapq.heappop(q)
ans += heapq.heappop(q)
heapq.heappop(q)
return -1 * (ans + sum(q)) | minimum-cost-of-buying-candies-with-discount | Python, using a heap queue | emwalker | 0 | 21 | minimum cost of buying candies with discount | 2,144 | 0.609 | Easy | 29,764 |
https://leetcode.com/problems/minimum-cost-of-buying-candies-with-discount/discuss/1710318/Python-sort | class Solution:
def minimumCost(self, cost: List[int]) -> int:
cost.sort(reverse=True)
return sum(cost) - sum(cost[2::3]) | minimum-cost-of-buying-candies-with-discount | Python, sort | blue_sky5 | 0 | 29 | minimum cost of buying candies with discount | 2,144 | 0.609 | Easy | 29,765 |
https://leetcode.com/problems/minimum-cost-of-buying-candies-with-discount/discuss/1709876/Reversed-and-skipped-every-third-but-still-got-wrong-answer | class Solution:
def minimumCost(self, cost: List[int]) -> int:
l= sorted(cost,reverse=True)
totco = 0
# print(sorted(cost,reverse=True))
if len(cost) < 3:
totco = totco + sum(cost)
else:
free = 3
for i in range(0,len(l)):
if free !=... | minimum-cost-of-buying-candies-with-discount | Reversed and skipped every third but still got wrong answer | menlam3 | 0 | 13 | minimum cost of buying candies with discount | 2,144 | 0.609 | Easy | 29,766 |
https://leetcode.com/problems/minimum-cost-of-buying-candies-with-discount/discuss/1709839/Python3-Easy-greedy-algorithm | class Solution:
def minimumCost(self, cost: List[int]) -> int:
cost.sort(reverse=True)
ans = 0
count = 0
for i in range(len(cost)):
if count % 3 == 2:
pass
else:
ans += cost[i]
count += 1
return ans | minimum-cost-of-buying-candies-with-discount | [Python3] Easy greedy algorithm | dwschrute | 0 | 21 | minimum cost of buying candies with discount | 2,144 | 0.609 | Easy | 29,767 |
https://leetcode.com/problems/minimum-cost-of-buying-candies-with-discount/discuss/1709829/python-simple-sort | class Solution:
def minimumCost(self, cost: List[int]) -> int:
cost.sort(reverse=True)
res = sum(cost)
i = 2
while i < len(cost):
res -= cost[i]
i += 3 #skip every 2 elements to eliminate the max possible price
return res | minimum-cost-of-buying-candies-with-discount | python simple sort | abkc1221 | 0 | 27 | minimum cost of buying candies with discount | 2,144 | 0.609 | Easy | 29,768 |
https://leetcode.com/problems/count-the-hidden-sequences/discuss/1714246/Right-Left | class Solution:
def numberOfArrays(self, diff: List[int], lower: int, upper: int) -> int:
diff = list(accumulate(diff, initial = 0))
return max(0, upper - lower - (max(diff) - min(diff)) + 1) | count-the-hidden-sequences | Right - Left | votrubac | 5 | 266 | count the hidden sequences | 2,145 | 0.365 | Medium | 29,769 |
https://leetcode.com/problems/count-the-hidden-sequences/discuss/1709694/Python3-compare-range | class Solution:
def numberOfArrays(self, differences: List[int], lower: int, upper: int) -> int:
prefix = mn = mx = 0
for x in differences:
prefix += x
mn = min(mn, prefix)
mx = max(mx, prefix)
return max(0, (upper-lower) - (mx-mn) + 1) | count-the-hidden-sequences | [Python3] compare range | ye15 | 2 | 68 | count the hidden sequences | 2,145 | 0.365 | Medium | 29,770 |
https://leetcode.com/problems/count-the-hidden-sequences/discuss/2824240/Easiest-possible-solution-with-explaination. | class Solution:
def numberOfArrays(self, differences: List[int], lower: int, upper: int) -> int:
l = lower
r = upper
right = float("-inf")
while l <= r:
mid = l+r>>1
isHidden,condition = self.isHiddenSequence(differences,mid,lower,upper)
if isHid... | count-the-hidden-sequences | Easiest possible solution with explaination. | shriyansnaik | 0 | 1 | count the hidden sequences | 2,145 | 0.365 | Medium | 29,771 |
https://leetcode.com/problems/count-the-hidden-sequences/discuss/2675107/Python3-Solution-oror-O(N)-Time-and-O(1)-Space-Complexity | class Solution:
def numberOfArrays(self, differences: List[int], lower: int, upper: int) -> int:
prev=0
minVal,maxVal=0,0
#min max initialize
for i in differences:
curr=i+prev
if curr<minVal:
minVal=curr
elif curr>maxVal:
... | count-the-hidden-sequences | Python3 Solution || O(N) Time & O(1) Space Complexity | akshatkhanna37 | 0 | 3 | count the hidden sequences | 2,145 | 0.365 | Medium | 29,772 |
https://leetcode.com/problems/count-the-hidden-sequences/discuss/2201845/Python-Easy-understand-solution | class Solution:
def numberOfArrays(self, differences: List[int], lower: int, upper: int) -> int:
cum_sum = [0]
for diff in differences:
cum_sum.append(cum_sum[-1]+diff)
max_diff = max(cum_sum)-min(cum_sum)
if upper-lower < max_diff:
return 0
... | count-the-hidden-sequences | Python Easy understand solution | prejudice23 | 0 | 32 | count the hidden sequences | 2,145 | 0.365 | Medium | 29,773 |
https://leetcode.com/problems/count-the-hidden-sequences/discuss/1979873/python-3-oror-prefix-sum-oror-O(n)O(1) | class Solution:
def numberOfArrays(self, differences: List[int], lower: int, upper: int) -> int:
low = high = cur = 0
for diff in differences:
cur += diff
low = min(low, cur)
high = max(high, cur)
return max(0, 1 + upper - lower - (high - low)) | count-the-hidden-sequences | python 3 || prefix sum || O(n)/O(1) | dereky4 | 0 | 87 | count the hidden sequences | 2,145 | 0.365 | Medium | 29,774 |
https://leetcode.com/problems/count-the-hidden-sequences/discuss/1721112/One-pass-75-speed | class Solution:
def numberOfArrays(self, differences: List[int], lower: int, upper: int) -> int:
n = min_n = max_n = 0
for diff in differences:
n += diff
min_n = min(min_n, n)
max_n = max(max_n, n)
upper_start_n = upper - max_n
lower_start_n = lowe... | count-the-hidden-sequences | One pass, 75% speed | EvgenySH | 0 | 29 | count the hidden sequences | 2,145 | 0.365 | Medium | 29,775 |
https://leetcode.com/problems/count-the-hidden-sequences/discuss/1709824/Python3-one-pass-straight-forward-solution-O(N) | class Solution:
def numberOfArrays(self, differences: List[int], lower: int, upper: int) -> int:
min_diff_sum, max_diff_sum = 0, 0
cur_diff_sum = 0
for diff in differences:
cur_diff_sum += diff
min_diff_sum = min(min_diff_sum, cur_diff_sum)
max_diff_sum = ... | count-the-hidden-sequences | [Python3] one-pass straight forward solution O(N) | dwschrute | 0 | 26 | count the hidden sequences | 2,145 | 0.365 | Medium | 29,776 |
https://leetcode.com/problems/count-the-hidden-sequences/discuss/1709740/Python-prefix-sum | class Solution:
def numberOfArrays(self, d: List[int], l: int, h: int) -> int:
n = len(d)
res = 0
preSum = [d[0]] + [0]*(n-1)
max_d = min_d = d[0]
for i in range(1, n):
preSum[i] = preSum[i-1] + d[i]
min_d = min(min_d, preSum[i])
m... | count-the-hidden-sequences | Python prefix sum | abkc1221 | 0 | 42 | count the hidden sequences | 2,145 | 0.365 | Medium | 29,777 |
https://leetcode.com/problems/count-the-hidden-sequences/discuss/1709669/Python-O(N) | class Solution:
def numberOfArrays(self, differences: List[int], lower: int, upper: int) -> int:
total_sequences = 0
sequence = [lower]
for j in range(len(differences)):
x = sequence[j] + differences[j]
sequence.append(x)
minn, maxx = min(s... | count-the-hidden-sequences | Python - O(N) | rbhandu | 0 | 53 | count the hidden sequences | 2,145 | 0.365 | Medium | 29,778 |
https://leetcode.com/problems/k-highest-ranked-items-within-a-price-range/discuss/1709702/Python3-bfs | class Solution:
def highestRankedKItems(self, grid: List[List[int]], pricing: List[int], start: List[int], k: int) -> List[List[int]]:
m, n = len(grid), len(grid[0])
ans = []
queue = deque([(0, *start)])
grid[start[0]][start[1]] *= -1
while queue:
x, i, j = queu... | k-highest-ranked-items-within-a-price-range | [Python3] bfs | ye15 | 1 | 32 | k highest ranked items within a price range | 2,146 | 0.412 | Medium | 29,779 |
https://leetcode.com/problems/k-highest-ranked-items-within-a-price-range/discuss/1718454/Using-heap-for-ranking-no-sorting-76-speed | class Solution:
def highestRankedKItems(self, grid: List[List[int]], pricing: List[int], start: List[int], k: int) -> List[List[int]]:
allowed = dict()
for r, row in enumerate(grid):
for c, v in enumerate(row):
if v > 0:
allowed[(r, c)] = v
he... | k-highest-ranked-items-within-a-price-range | Using heap for ranking, no sorting, 76% speed | EvgenySH | 0 | 50 | k highest ranked items within a price range | 2,146 | 0.412 | Medium | 29,780 |
https://leetcode.com/problems/k-highest-ranked-items-within-a-price-range/discuss/1710142/python-bfs-solution | class Solution:
def highestRankedKItems(self, grid: List[List[int]], pricing: List[int], start: List[int], k: int) -> List[List[int]]:
m, n = len(grid), len(grid[0])
row, col = start
seen = set()
seen.add((row, col))
q = collections.deque([(0, grid[row][col], row, col)])
... | k-highest-ranked-items-within-a-price-range | python bfs solution | abkc1221 | 0 | 43 | k highest ranked items within a price range | 2,146 | 0.412 | Medium | 29,781 |
https://leetcode.com/problems/k-highest-ranked-items-within-a-price-range/discuss/1709795/Python3-iterative-BFS-%2B-sorting-with-explanation | class Solution:
def highestRankedKItems(self, grid: List[List[int]], pricing: List[int], start: List[int], k: int) -> List[List[int]]:
q = deque([tuple(start)])
m, n = len(grid), len(grid[0])
lower, upper = pricing[0], pricing[1]
ranked_arr = []
visited = {tuple(start)}
... | k-highest-ranked-items-within-a-price-range | [Python3] iterative BFS + sorting - with explanation | dwschrute | 0 | 31 | k highest ranked items within a price range | 2,146 | 0.412 | Medium | 29,782 |
https://leetcode.com/problems/number-of-ways-to-divide-a-long-corridor/discuss/1709706/simple-Python-solution-(time%3A-O(N)-space%3A-O(1)) | class Solution:
def numberOfWays(self, corridor: str) -> int:
#edge case
num_S = corridor.count('S')
if num_S == 0 or num_S % 2 == 1:
return 0
mod = 10 ** 9 + 7
curr_s = 0
divide_spots = []
for char in corridor:
curr_s += (char... | number-of-ways-to-divide-a-long-corridor | simple Python solution (time: O(N), space: O(1)) | kryuki | 2 | 92 | number of ways to divide a long corridor | 2,147 | 0.399 | Hard | 29,783 |
https://leetcode.com/problems/number-of-ways-to-divide-a-long-corridor/discuss/1709706/simple-Python-solution-(time%3A-O(N)-space%3A-O(1)) | class Solution:
def numberOfWays(self, corridor: str) -> int:
#edge case
num_S = corridor.count('S')
if num_S == 0 or num_S % 2 == 1:
return 0
mod = 10 ** 9 + 7
curr_s = 0
res = 1
spots = 0
for char in corridor:
... | number-of-ways-to-divide-a-long-corridor | simple Python solution (time: O(N), space: O(1)) | kryuki | 2 | 92 | number of ways to divide a long corridor | 2,147 | 0.399 | Hard | 29,784 |
https://leetcode.com/problems/number-of-ways-to-divide-a-long-corridor/discuss/1710028/Easy-python-O(n)-time-and-O(n)-space-complexity-with-explanation | class Solution:
def numberOfWays(self, corridor: str) -> int:
seat_idx = list()
for i in range(len(corridor)):
if corridor[i] == 'S':
seat_idx.append(i)
if len(seat_idx) == 0 or len(seat_idx) % 2:
# if there are 0 or odd number of seats, we cann... | number-of-ways-to-divide-a-long-corridor | Easy python O(n) time and O(n) space complexity with explanation | himanshushah808 | 1 | 26 | number of ways to divide a long corridor | 2,147 | 0.399 | Hard | 29,785 |
https://leetcode.com/problems/number-of-ways-to-divide-a-long-corridor/discuss/1709713/Python3-multiplication | class Solution:
def numberOfWays(self, corridor: str) -> int:
ans = 1
seats = ii = 0
for i, x in enumerate(corridor):
if x == 'S':
if seats and seats % 2 == 0: ans = ans * (i-ii) % 1_000_000_007
seats += 1
ii = i
return a... | number-of-ways-to-divide-a-long-corridor | [Python3] multiplication | ye15 | 1 | 37 | number of ways to divide a long corridor | 2,147 | 0.399 | Hard | 29,786 |
https://leetcode.com/problems/number-of-ways-to-divide-a-long-corridor/discuss/2774207/Python3-Commented-Single-Pass-Solution | class Solution:
def numberOfWays(self, corridor: str) -> int:
# go through the corridor and count seats
# after that check whether there are plants
# and we have several places to divide
# go through the chairs and count
chairs = 0
positions = 1
plants = 1
... | number-of-ways-to-divide-a-long-corridor | [Python3] - Commented Single Pass Solution | Lucew | 0 | 1 | number of ways to divide a long corridor | 2,147 | 0.399 | Hard | 29,787 |
https://leetcode.com/problems/number-of-ways-to-divide-a-long-corridor/discuss/2666017/Python3-Indicies-Combinatorial | class Solution:
def numberOfWays(self, A: str) -> int:
# edge cases
count = Counter(A)
if count["S"] % 2 != 0:
return 0
if count["S"] == 0:
return 0
if count["S"] == 2:
return 1
# count chairs by twos, between which there must be pu... | number-of-ways-to-divide-a-long-corridor | Python3 Indicies Combinatorial | jbradleyglenn | 0 | 3 | number of ways to divide a long corridor | 2,147 | 0.399 | Hard | 29,788 |
https://leetcode.com/problems/number-of-ways-to-divide-a-long-corridor/discuss/2476121/python-3-or-simple-O(n)O(1) | class Solution:
M = 10 ** 9 + 7
def numberOfWays(self, corridor: str) -> int:
prevSeat = corridor.find('S')
divide = False
res = 1
for i in range(prevSeat + 1, len(corridor)):
if corridor[i] == 'P':
continue
if divide:
... | number-of-ways-to-divide-a-long-corridor | python 3 | simple O(n)/O(1) | dereky4 | 0 | 16 | number of ways to divide a long corridor | 2,147 | 0.399 | Hard | 29,789 |
https://leetcode.com/problems/number-of-ways-to-divide-a-long-corridor/discuss/1731884/Linear-solution-83-speed | class Solution:
def numberOfWays(self, corridor: str) -> int:
seat_idx = [i for i, c in enumerate(corridor) if c == "S"]
len_seat_idx = len(seat_idx)
if not len_seat_idx % 2 and len_seat_idx > 1:
ans = 1
for i, idx in enumerate(seat_idx):
if i > 0 and ... | number-of-ways-to-divide-a-long-corridor | Linear solution, 83% speed | EvgenySH | 0 | 49 | number of ways to divide a long corridor | 2,147 | 0.399 | Hard | 29,790 |
https://leetcode.com/problems/number-of-ways-to-divide-a-long-corridor/discuss/1710512/Python-3-Just-do-it! | class Solution:
def numberOfWays(self, corridor: str) -> int:
loc = []
seats = 0
# keep track of the start and end position of two seat cluster
for i, x in enumerate(corridor):
if x == 'S':
seats += 1
if seats % 2:
... | number-of-ways-to-divide-a-long-corridor | [Python 3] Just do it! | chestnut890123 | 0 | 23 | number of ways to divide a long corridor | 2,147 | 0.399 | Hard | 29,791 |
https://leetcode.com/problems/number-of-ways-to-divide-a-long-corridor/discuss/1710030/Simple-Python-solution | class Solution:
def numberOfWays(self, corridor: str) -> int:
n = len(corridor)
if n==1:
return 0
d = defaultdict(int)
numS = 0
for i in range(n):
if (numS==0 or numS%2==1) and corridor[i]=='P':
continue
if corridor... | number-of-ways-to-divide-a-long-corridor | Simple Python solution | 1579901970cg | 0 | 16 | number of ways to divide a long corridor | 2,147 | 0.399 | Hard | 29,792 |
https://leetcode.com/problems/count-elements-with-strictly-smaller-and-greater-elements/discuss/2507825/Very-very-easy-code-in-just-3-lines-using-Python | class Solution:
def countElements(self, nums: List[int]) -> int:
res = 0
mn = min(nums)
mx = max(nums)
for i in nums:
if i > mn and i < mx:
res += 1
return res | count-elements-with-strictly-smaller-and-greater-elements | Very very easy code in just 3 lines using Python | ankurbhambri | 7 | 44 | count elements with strictly smaller and greater elements | 2,148 | 0.6 | Easy | 29,793 |
https://leetcode.com/problems/count-elements-with-strictly-smaller-and-greater-elements/discuss/1711447/Easy-Python-Solution(100) | class Solution:
def countElements(self, nums: List[int]) -> int:
mi=min(nums)
ma=max(nums)
c=0
for i in range(len(nums)):
if nums[i]>mi and nums[i]<ma:
c+=1
return c | count-elements-with-strictly-smaller-and-greater-elements | Easy Python Solution(100%) | Sneh17029 | 3 | 116 | count elements with strictly smaller and greater elements | 2,148 | 0.6 | Easy | 29,794 |
https://leetcode.com/problems/count-elements-with-strictly-smaller-and-greater-elements/discuss/1711239/Python-Solution-Using-Sorting-and-Counter | class Solution:
def countElements(self, nums: List[int]) -> int:
nums.sort()
freq_table = Counter(nums)
arr = list(freq_table.keys())
arr.sort()
ans = len(nums)
ans -= freq_table[arr[0]]
ans -= freq_table[arr[-1]]
return max(ans,0) | count-elements-with-strictly-smaller-and-greater-elements | Python Solution Using Sorting and Counter | anCoderr | 3 | 58 | count elements with strictly smaller and greater elements | 2,148 | 0.6 | Easy | 29,795 |
https://leetcode.com/problems/count-elements-with-strictly-smaller-and-greater-elements/discuss/2372110/Very-easy-Python | class Solution:
def countElements(self, nums: List[int]) -> int:
min_=min(nums)
max_=max(nums)
c=0
for i in nums:
if min_<i<max_:
c+=1
return c | count-elements-with-strictly-smaller-and-greater-elements | Very easy [Python] | sunakshi132 | 1 | 17 | count elements with strictly smaller and greater elements | 2,148 | 0.6 | Easy | 29,796 |
https://leetcode.com/problems/count-elements-with-strictly-smaller-and-greater-elements/discuss/1818673/1-Line-Python-Solution-oror-Memory-less-than-99 | class Solution:
def countElements(self, nums: List[int]) -> int:
return len([num for num in nums if num not in {min(nums),max(nums)}]) | count-elements-with-strictly-smaller-and-greater-elements | 1-Line Python Solution || Memory less than 99% | Taha-C | 1 | 41 | count elements with strictly smaller and greater elements | 2,148 | 0.6 | Easy | 29,797 |
https://leetcode.com/problems/count-elements-with-strictly-smaller-and-greater-elements/discuss/1715168/Python-oror-Easy-oror-100-faster-oror-2-lines-oror-Simple-Method | class Solution:
def countElements(self, nums: List[int]) -> int:
count=len(nums)-nums.count(max(nums))-nums.count(min(nums))
return max(count,0) | count-elements-with-strictly-smaller-and-greater-elements | Python || Easy || 100% faster || 2 lines || Simple Method | rushi_javiya | 1 | 27 | count elements with strictly smaller and greater elements | 2,148 | 0.6 | Easy | 29,798 |
https://leetcode.com/problems/count-elements-with-strictly-smaller-and-greater-elements/discuss/1712661/!-min-and-!max | class Solution:
def countElements(self, nums: List[int]) -> int:
mn = min(nums)
mx = max(nums)
res = 0
for i in nums:
if i!=mn and i!=mx:
res += 1
return res | count-elements-with-strictly-smaller-and-greater-elements | != min and !=max | lokeshsenthilkumar | 1 | 22 | count elements with strictly smaller and greater elements | 2,148 | 0.6 | Easy | 29,799 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.