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-1, doubles) + 1 else: # doubles >= 1 and n % 2 == 0 return count(n//2, doubles-1) +1 return count(target, maxDoubles)
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 else: target -= 1 step += 1 return step
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 count+=1 count+=target-1 return count
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 target = half return ans
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: n = target - 1 step += n break else: target -= 1 step += 1 return step
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 else: target-=1 ans+=1 return ans
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 &amp; 1: target -= 1 steps += 1 elif maxDoubles: target = target // 2 steps += 1 maxDoubles -= 1 else: target -= 1 steps +=1 return steps
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 step +=1 else: if maxDoubles > 0: target = target/ 2 step +=1 maxDoubles -=1 else: step +=1 target -=1 return int(step)
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 step += 1 else: if maxDoubles: target = target // 2 maxDoubles -= 1 step += 1 else: step += target - 1 break return step
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 = target/2 steps = steps + 1 maxDoubles = maxDoubles - 1 else: target = target - 1 steps = steps + 1 # while target!= 1: # if maxDoubles > 0: # if target%2 == 0: # target = target/2 # maxDoubles = maxDoubles - 1 # else: # target = target -1 # # while maxDoubles!= 0: # # if target%2 == 0: # # target = target/2 # # maxDoubles = maxDoubles - 1 # # else: # # target = target - 1 # steps = steps + 1 return(steps)
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 step +=1 else: if maxDoubles > 0: target = target/ 2 step +=1 maxDoubles -=1 else: step +=1 target -=1 return int(step)
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 calculated this and we dont need to recompute it def rec_func(index, current_val) -> int: # index is the current question number to process and current_val is the max marks upto this question if index >= n: # It means that we have gone through all the questions thus return the current_val return current_val if memo[index] == -1: # memo[i] == -1, not computed before and so we need to solve it points = questions[index][0] # points for current question brainpower = questions[index][1] # brainpower for current question a = rec_func(index + brainpower + 1, points) # Recursive call considering we solve the current question b = rec_func(index + 1, 0) # Recursive call considering we skip the current question memo[index] = max(a, b) # choose which ever choice yields us the best result return current_val+memo[index] return rec_func(0, 0)
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 + 1]) return max(dp[0])
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[brainpower+index+1] = max(dp[brainpower+index+1], points+dp[index]) else: dp[-1] = max(dp[-1], dp[index]+points) if index + 1 < len(questions): dp[index+1] = max(dp[index], dp[index+1]) return dp[-1]
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 current i bigger than the right bound of the candidate which has the smallest bound idx, point = heappop(candi) heappush(h, (point, idx)) else: break if h: point, idx = h[0] # h[0] is the highest points we can get from prevous questions, and we can access heappush(candi, (i + questions[i][1], point - questions[i][0])) else: heappush(candi, (i + questions[i][1], -questions[i][0])) r1 = -h[0][0] if h else 0 r2 = max([-v[1] for v in candi]) if candi else 0 return max(r1, r2)
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(questions[i][0]+self.recur(i+questions[i][1]+1,questions,dp),self.recur(i+1,questions,dp)) return dp[i] ```
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] = max(dp[i], cand) return dp[0]
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): # calculate our jumping point (and keep it in bounds) next_q = min(idx + brainpower + 1, qss) # update the dp array right next to us dp[idx+1] = max(dp[idx+1], dp[idx]) # update the dp array of the jumping point dp[next_q] = max(dp[next_q], points + dp[idx]) return dp[-1]
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 lookup: return lookup[ind] skip = f(ind + 1, lookup) solve = questions[ind][0] + f(ind + questions[ind][1] + 1, lookup) lookup[ind] = max(solve, skip) return lookup[ind] return f(0, lookup)
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] notTake=helper(ind+1) take=questions[ind][0] + helper(ind+questions[ind][1]+1) dp[ind] = max(take, notTake) return dp[ind] return helper(0)
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 = questions[i] a = dfs(i + 1 + brainpower) + points b = dfs(i+1) best = max(a, b) memo[i] = best return best return dfs() ```
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[i] = max(helper(i + 1), helper(i + 1 + power) + points) return dp[i] return helper(0)
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[i][1]+1 if nextindex<n: profit[i]+=rightmax[nextindex] if i!=n-1: rightmax[i]=max(rightmax[i+1],profit[i]) return rightmax[0]
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+questions[i][1]+1<n+1: dp[i+questions[i][1]+1] = max(dp[i+questions[i][1]+1], questions[i][0]+dp[i]) else: ans=max(ans,questions[i][0]+dp[i]) return ans ```
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][1]+1)) p2=getPoints(index+1) memo[index]=max(p1,p2) return memo[index] return getPoints(0)
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 that point in reverse direction dp_max[-1] = questions[-1][0] while i >= 0: dp[i] = questions[i][0] if i+questions[i][1]+1 < len(questions): dp[i] += dp_max[i+questions[i][1]+1] dp_max[i] = max(dp[i], dp_max[i+1]) i -= 1 return max(dp)
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,questions) return max(left,right) ind=0 return max_points_earned(ind,questions)
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]+1,questions,dp) right=max_points_earned(ind+1,questions,dp) dp[ind]=max(left,right) return dp[ind] ind=0 n=len(questions) dp=[-1]*n return max_points_earned(ind,questions,dp)
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] if (i+questions[i][1])+1<n: second=questions[i][0]+dp[i+questions[i][1]+1] dp[i]=max(first,second) return dp[0] return max_points_earned(questions)
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 + 1 < len(questions): toAdd = dp[i + brainpower + 1] dp[i] = max(dp[i + 1], points + toAdd) # print(dp) return dp[0]
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) + questions[i][0], dfs(i+1) ) return dp[i] dfs(0) return dp[0]
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) if i + 1 < len(questions) else 0 return max(notSkip, skip) maxPoints = 0 for i in range(len(questions)): maxPoints = max(helper(i),maxPoints) return maxPoints
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] max_value = float("-inf") if i+skip+1 <= n-1: max_value = max(max_value, points+matrix[i+skip+1]) else: max_value = max(max_value, points) max_value = max(max_value, matrix[i+1]) matrix[i] = max_value return max(matrix)
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 = questions return self.dp(0)
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] = max(questions[i][0] + dp[i + questions[i][1] + 1], dp[i + 1]) else: dp[i] = max(questions[i][0], dp[i + 1]) return dp[0]
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)): points, skip = questions[i] # dp formula if i + skip + 1 < n : dp[i] = max(dp[i+1], points + dp[i + skip + 1]) else: dp [i] = max(points, dp[i+1]) return dp[0]
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 batteries[i+1]*(i+1) - prefix > extra: return (prefix + extra) // (i+1) return (prefix + extra) // n
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: continue smaller_batteries = n-i need_for_refill = smaller_batteries * (prev-cur) if need_for_refill <= s: s -= need_for_refill else: return cur + s // smaller_batteries return batteries[0] + s // n
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 rest batteries on computer can run more than t time if x >= t: return True # need t - x batteries to fill tmp -= t - x if tmp < 0: return False return True while l < h: mid = l + (h - l + 1) // 2 if bs(mid): l = mid else: h = mid - 1 return l
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) # - used to negate the -ve sign n -= 1 if n > 0: ans += -heappop(max_heap) # - used to negate the -ve sign n -= 1 # if heap is not empty take 3rd candy out with discount, so costs not added to ans if n > 0: heappop(max_heap) n -= 1 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,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: hops = 1 return min_cost
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_cost[i] return required_cost
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 if cnt == 2: jump = 1 res += cost[i] return res
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: ans+=cost[0] return ans
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 more candies # we can keep a accumulative sum as we process # time O(n^2) space O(1) cost.sort() n = len(cost) count = 0 res = 0 for i in range(n - 1, -1, -1): if count < 2: count += 1 res += cost[i] else: count = 0 return res
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 total_cost += cost[i-1] return total_cost
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] return sm
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 return c
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 != 1: totco = totco + l[i] free = free - 1 else: free = free + 1 return(totco)
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 isHidden: right = mid l = mid + 1 else: if condition == "HIGH": r = mid - 1 else: l = mid + 1 l = lower r = upper left = float("inf") while l <= r: mid = l+r>>1 isHidden,condition = self.isHiddenSequence(differences,mid,lower,upper) if isHidden: left = mid r = mid - 1 else: if condition == "HIGH": r = mid - 1 else: l = mid + 1 if right == float("-inf") or left == float("inf"): return 0 return right - left + 1 def isHiddenSequence(self,differences,start,lower,upper): cur_sum = start for difference in differences: cur_sum += difference if cur_sum < lower: return False,"LOW" if cur_sum > upper: return False,"HIGH" return True,"PERFECT"
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: maxVal=curr prev=curr if lower-minVal<=upper-maxVal: return (upper-maxVal)-(lower-minVal)+1 return 0
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 return upper-lower-max_diff+1
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 = lower - min_n return (upper_start_n - lower_start_n + 1 if upper_start_n >= lower_start_n else 0)
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 = max(max_diff_sum, cur_diff_sum) # a0 >= lower - min_diff_sum a0_lower = lower - min_diff_sum # a0 <= upper - max_d_s a0_upper = upper - max_diff_sum len_range = a0_upper - a0_lower + 1 return max(len_range, 0)
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]) max_d = max(max_d, preSum[i]) for i in range(l, h+1): if i + min_d >= l and i + max_d <= h: res += 1 return res
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(sequence), max(sequence) if lower <= minn <= upper and lower <= maxx <= upper: total_sequences += 1 for i in range(lower + 1, upper+1): minn = minn + 1 maxx = maxx + 1 if lower <= minn <= upper and lower <= maxx <= upper: total_sequences += 1 return total_sequences
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 = queue.popleft() if pricing[0] <= -grid[i][j] <= pricing[1]: ans.append((x, -grid[i][j], i, j)) for ii, jj in (i-1, j), (i, j-1), (i, j+1), (i+1, j): if 0 <= ii < m and 0 <= jj < n and grid[ii][jj] > 0: queue.append((x+1, ii, jj)) grid[ii][jj] *= -1 return [[i, j] for _, _, i, j in sorted(ans)[:k]]
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 heap_ans = [] low_price, high_price = pricing start_r, start_c = start start_tpl = (start_r, start_c) distance = 0 if low_price <= allowed[start_tpl] <= high_price: heappush(heap_ans, (distance, allowed[start_tpl], start_r, start_c)) allowed.pop(start_tpl) level = [start_tpl] while len(heap_ans) < k and level: new_level = [] distance += 1 for r, c in level: for new_tpl in [(r + 1, c), (r, c + 1), (r - 1, c), (r, c - 1)]: if new_tpl in allowed: if low_price <= allowed[new_tpl] <= high_price: heappush(heap_ans, (distance, allowed[new_tpl], new_tpl[0], new_tpl[1])) allowed.pop(new_tpl) new_level.append(new_tpl) level = new_level ans = [] count = 0 while heap_ans and count < k: _, _, r, c = heappop(heap_ans) ans.append([r, c]) count += 1 return ans
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)]) res = [] while q: dist, cost, row, col = q.popleft() if pricing[0] <= cost <= pricing[1]: res += [(dist, cost, row, col)] for x, y in (row - 1, col), (row + 1, col), (row, col - 1), (row, col + 1): if 0 <= x <= m-1 and 0 <= y <= n-1 and (x, y) not in seen and grid[x][y] != 0: q.append((dist + 1, grid[x][y], x, y)) seen.add((x, y)) res.sort() return [[x, y] for _, _, x, y in res[:k]]
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)} adj = [(-1, 0), (0, -1), (0, 1), (1, 0)] while q: next_q = [] for _ in range(len(q)): cur = q.popleft() x, y = cur if lower <= grid[x][y] <= upper: ranked_arr.append(list(cur)) for dx, dy in adj: if ((x + dx, y + dy) in visited or not (0 <= x + dx < m and 0 <= y + dy < n) or grid[x + dx][y + dy] == 0 ): continue next_q.append((x + dx, y + dy)) visited.add((x + dx, y + dy)) next_q.sort(key=lambda xy: xy[1]) next_q.sort(key=lambda xy: xy[0]) next_q.sort(key=lambda xy: grid[xy[0]][xy[1]]) q = deque(next_q) return ranked_arr[:k]
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 == 'S') if curr_s > 0 and curr_s % 2 == 0: divide_spots[-1] += 1 else: if not divide_spots or divide_spots[-1] > 0: divide_spots.append(0) res = 1 for num in divide_spots[:-1]: res = res * num % mod return res
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: curr_s += (char == 'S') if curr_s > 0 and curr_s % 2 == 0: spots += 1 else: if spots != 0: res = res * spots % mod spots = 0 return res
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 cannot divide sections with 2 seats each return 0 ways = 1 for i in range(2, len(seat_idx)-1, 2): # ignore first and last seat ways *= seat_idx[i] - seat_idx[i-1] return ways % (10**9 + 7)
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 ans if seats and seats % 2 == 0 else 0
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 for idx, element in enumerate(corridor): # check if we reached two chairs if chairs > 0 and chairs % 2 == 0: # check if current element is plant if element == 'S': positions *= plants plants = 1 elif element == 'P': plants += 1 # count the chairs if element == 'S': chairs += 1 return (positions % 1_000_000_007) if chairs > 0 and chairs % 2 == 0 else 0
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 put a divider # if there are n plants between two pairs, there are n + 1 options # for where to put the divider # multiply these options to get the final count chairlocs = [i for i, val in enumerate(A) if val == "S"] pairdists = [chairlocs[i] - chairlocs[i-1] for i in range(2, len(chairlocs), 2)] return reduce((lambda x, y: (x * y) % (10 ** 9 + 7)), pairdists)
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: res = (res * (i - prevSeat)) % Solution.M prevSeat = i divide = not divide return res if divide else 0
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 not i % 2: ans *= (idx - seat_idx[i - 1]) return ans % 1_000_000_007 return 0
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: loc.append([i]) else: loc[-1] += [i] # corner case (no seats or odd number seats) if not loc or len(loc[-1]) < 2: return 0 M = 10 ** 9 + 7 ans = 1 for i in range(1, len(loc)): ans *= loc[i][0] - loc[i-1][1] ans %= M return ans
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[i]=='P': d[numS//2] += 1 else: numS += 1 if numS==0 or numS%2: return 0 if corridor[-1]=='P': d[numS//2] = 0 ret = 1 for v in d.values(): ret *= (v+1) return ret%(10**9+7)
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