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/paint-house-iii/discuss/1344473/Python-3-Recursion-with-pruning-(368ms)
class Solution: def minCost(self, houses: List[int], cost: List[List[int]], m: int, n: int, target: int) -> int: @lru_cache(None) def dp(idx, target, prev): # remaining house < remaining neighbor to form if target < 0 or target > m - idx: return float('inf') if idx == m: return 0 # houses painted if houses[idx]: return dp(idx + 1, target - (prev != houses[idx]), houses[idx]) # no more neighbor need to be formed (all remaining must be painted as previous one) if target == 0: return cost[idx][prev-1] + dp(idx + 1, target, prev) ans = float('inf') for paint in range(n): ans = min(ans, cost[idx][paint] + dp(idx + 1, target - (prev != paint+1), paint+1)) return ans ans = dp(0, target, -1) return ans if ans < float("inf") else -1
paint-house-iii
[Python 3] Recursion with pruning (368ms)
chestnut890123
1
136
paint house iii
1,473
0.619
Hard
22,000
https://leetcode.com/problems/paint-house-iii/discuss/2686492/Clean-Fast-Python3-or-O(m-*-t-*-n2)
class Solution: def minCost(self, houses: List[int], cost: List[List[int]], m: int, n: int, target: int) -> int: @cache def dp(house, last_color, neis): # to many neighborhoods or not enough if neis < 0 or house + neis > m: return float('inf') # after last house, 0 neighborhoods left -> valid if house == m and neis == 0: return 0 # already painted if houses[house] > 0: if last_color != houses[house] - 1: neis -= 1 return dp(house + 1, houses[house] - 1, neis) min_cost = float('inf') for color in range(n): if last_color == color: # same neighborhood min_cost = min(min_cost, cost[house][color] + dp(house + 1, color, neis) ) else: # start a new neighborhood min_cost = min(min_cost, cost[house][color] + dp(house + 1, color, neis - 1) ) return min_cost cost = dp(0, -1, target) return -1 if cost >= float('inf') else cost
paint-house-iii
Clean, Fast Python3 | O(m * t * n^2)
ryangrayson
0
7
paint house iii
1,473
0.619
Hard
22,001
https://leetcode.com/problems/paint-house-iii/discuss/2257137/Python3-Top-Down-with-edge-case-initial-condition-and-regular-case
class Solution: def minCost(self, houses: List[int], cost: List[List[int]], m: int, n: int, target: int) -> int: """Top down. dp(i, j, k) is the min cost of painting house i with paint j while having currently k neighborboods. First, the edge cases are met when k < 1 (the number of neighborhoods cannot be smaller than 1) or k > i + 1 (the number of neighborhoods cannot be more than the number of houses encountered). Then, the intial condition when i == 0. If houses[0] has a color, we have cost zero if j == houses[0], otherwise it's impossible. If houses[0] has not been painted, we can paint it with color j. Finally, the regular cases. If houses[i] has a color, it is impossible if j != houses[i]. Otherwise, the min of our current cost is the min of all possibilities of the previous house. We go through the previous color pj and find dp(i - 1, pj, k - (pj != j)). If houses[i] does not have a color, we can paint it j. The logic is the same as above, except that now we must add cost[i][j - 1]. O(MNK), 688 ms, faster than 79.54% """ @lru_cache(maxsize=None) def dp(i: int, j: int, k: int) -> int: if k < 1 or k > i + 1: # important edge cases return math.inf if i == 0: # initial condition if houses[0]: return 0 if j == houses[0] else math.inf return cost[i][j - 1] if houses[i]: if houses[i] != j: return math.inf return min(dp(i - 1, pj, k - (pj != j)) for pj in range(1, n + 1)) return min(dp(i - 1, pj, k - (pj != j)) + cost[i][j - 1] for pj in range(1, n + 1)) res = min(dp(len(houses) - 1, j, target) for j in range(1, n + 1)) return res if res < math.inf else -1
paint-house-iii
[Python3] Top Down with edge case, initial condition, and regular case
FanchenBao
0
5
paint house iii
1,473
0.619
Hard
22,002
https://leetcode.com/problems/paint-house-iii/discuss/2253697/GoGolangPython-O(n-2-*-m-*-target)-time-or-O(n-*-m-*-target)-space
class Solution: def minCost(self, houses: List[int], cost: List[List[int]], m: int, n: int, target: int) -> int: # dp[i][c][k]: i means the ith house, c means the cth color, k means k neighbor groups dp = [[[math.inf for _ in range(n)] for _ in range(target + 1)] for _ in range(m)] for c in range(1, n + 1): if houses[0] == c: dp[0][1][c - 1] = 0 elif not houses[0]: dp[0][1][c - 1] = cost[0][c - 1] for i in range(1, m): for k in range(1, min(target, i + 1) + 1): for c in range(1, n + 1): if houses[i] and c != houses[i]: continue same_neighbor_cost = dp[i - 1][k][c - 1] diff_neighbor_cost = float("inf") for c_ in range(n): if c_ == c-1: continue diff_neighbor_cost = min(diff_neighbor_cost,dp[i - 1][k - 1][c_]) paint_cost = cost[i][c - 1] * (not houses[i]) dp[i][k][c - 1] = min(same_neighbor_cost, diff_neighbor_cost) + paint_cost res = min(dp[-1][-1]) return res if res != math.inf else -1
paint-house-iii
Go/Golang/Python O(n ^2 * m * target) time | O(n * m * target) space
vtalantsev
0
57
paint house iii
1,473
0.619
Hard
22,003
https://leetcode.com/problems/paint-house-iii/discuss/2253461/Python3-bottom-up-solution
class Solution: def minCost(self, houses: List[int], cost: List[List[int]], m: int, n: int, target: int) -> int: # check how many neighbors in initial state neighbor = 0 prev = 0 for house in houses: if house and house != prev: prev = house neighbor += 1 if neighbor > target: return -1 # dp[# index][# color][# neighbor] dp = [[[inf] * (target+1) for _ in range(n)] for _ in range(m)] if not houses[0]: for color in range(n): dp[0][color][1] = cost[0][color] else: dp[0][houses[0]-1][1] = 0 for i in range(1, m): for color in range(n): for neighbor in range(1, target+1): # i-th house is not painted yet if not houses[i]: # same color with i-1 dp[i][color][neighbor] = dp[i-1][color][neighbor] + cost[i][color] # different color with i-1 for diff_color in range(n): if color != diff_color: dp[i][color][neighbor] = min(dp[i][color][neighbor], dp[i-1][diff_color][neighbor-1] + cost[i][color]) # i-th house is already painted else: if color+1 == houses[i]: dp[i][color][neighbor] = dp[i-1][color][neighbor] for diff_color in range(n): if color != diff_color: dp[i][color][neighbor] = min(dp[i][color][neighbor], dp[i-1][diff_color][neighbor-1]) else: dp[i][color][neighbor] = inf minCost = inf for j in range(n): minCost = min(minCost, dp[-1][j][target]) return minCost if minCost != inf else -1
paint-house-iii
[Python3] bottom up solution
Jhyatt
0
17
paint house iii
1,473
0.619
Hard
22,004
https://leetcode.com/problems/final-prices-with-a-special-discount-in-a-shop/discuss/685429/PythonPython3-Final-Prices-with-a-Special-Discount-in-a-Shop
class Solution: def finalPrices(self, prices: List[int]) -> List[int]: len_prices = len(prices) i = 0 while i <= len_prices-2: for j in range(i+1, len(prices)): if prices[i] >= prices[j] and j > i: prices[i] = prices[i] - prices[j] break i += 1 return prices
final-prices-with-a-special-discount-in-a-shop
[Python/Python3] Final Prices with a Special Discount in a Shop
newborncoder
4
541
final prices with a special discount in a shop
1,475
0.755
Easy
22,005
https://leetcode.com/problems/final-prices-with-a-special-discount-in-a-shop/discuss/2495765/Python-Stack-99.71-faster-or-Simplest-solution-with-explanation-or-Beg-to-Adv-or-Monotonic-Stack
class Solution: def finalPrices(self, prices: List[int]) -> List[int]: stack = [] # taking a empty stack. for i in range(len(prices)): # traversing through the provided prices list. while stack and (prices[stack[-1]] >= prices[i]): # comparing the previous element with current element as descibed in the question to calculate the discount. prices[stack.pop()] -= prices[i] # reducing the price of the current elem from previous. stack.append(i) # In stack we`ll just stores the index of the prices. Using those indexes will make changes in the prices list itself. return prices # returing the updated prices as we made changes in the provided list itself.
final-prices-with-a-special-discount-in-a-shop
Python Stack 99.71% faster | Simplest solution with explanation | Beg to Adv | Monotonic Stack
rlakshay14
3
250
final prices with a special discount in a shop
1,475
0.755
Easy
22,006
https://leetcode.com/problems/final-prices-with-a-special-discount-in-a-shop/discuss/2275490/PYTHONoror-FASTER-THAN-88-oror-LESS-TAHN-95SPACE-oror-EASY
class Solution: def finalPrices(self, prices: List[int]) -> List[int]: for i in range(len(prices)): for j in range(i+1,len(prices)): if prices[j]<=prices[i]: prices[i]=prices[i]-prices[j] break return (prices)
final-prices-with-a-special-discount-in-a-shop
PYTHON|| FASTER THAN 88% || LESS TAHN 95%SPACE || EASY
vatsalg2002
3
127
final prices with a special discount in a shop
1,475
0.755
Easy
22,007
https://leetcode.com/problems/final-prices-with-a-special-discount-in-a-shop/discuss/2625384/PythonororO(N2)
class Solution: def finalPrices(self, prices: List[int]) -> List[int]: ans=[] for i in range(len(prices)-1): flag=False for j in range(i+1,len(prices)): if prices[i]>=prices[j]: ans.append(abs(prices[i]-prices[j])) flag=True break if flag==False: ans.append(prices[i]) ans.append(prices[-1]) return ans
final-prices-with-a-special-discount-in-a-shop
[Python]||O(N^2)
Sneh713
2
125
final prices with a special discount in a shop
1,475
0.755
Easy
22,008
https://leetcode.com/problems/final-prices-with-a-special-discount-in-a-shop/discuss/1292222/Beats-97-or-python
class Solution: def finalPrices(self, p: List[int]) -> List[int]: n=len(p) stack =[] ans=[] for j in range(n-1,-1,-1): while stack and stack[-1]>p[j]: stack.pop() if len(stack)==0: ans.append(p[j]) else: ans.append(p[j]-stack[-1]) stack.append(p[j]) return ans[::-1]
final-prices-with-a-special-discount-in-a-shop
Beats 97% | python
chikushen99
2
283
final prices with a special discount in a shop
1,475
0.755
Easy
22,009
https://leetcode.com/problems/final-prices-with-a-special-discount-in-a-shop/discuss/1030342/Python3-easy-solution
class Solution: def finalPrices(self, prices: List[int]) -> List[int]: for i in range(len(prices)-1): for j in range(i+1,len(prices)): if prices[j] <= prices[i]: prices[i] -= prices[j] break return prices
final-prices-with-a-special-discount-in-a-shop
Python3 easy solution
EklavyaJoshi
2
207
final prices with a special discount in a shop
1,475
0.755
Easy
22,010
https://leetcode.com/problems/final-prices-with-a-special-discount-in-a-shop/discuss/2244418/Python-Monotonic-Stack-t.c-O(n)-s.c-O(1)-with-comments
class Solution: def finalPrices(self, prices: List[int]) -> List[int]: #create a monotonic stack mono_stack = [] #loop through the elements of prices for i,p in enumerate(prices): #subtract current prices from prices at last index of mono_stack until it is greater or equal to the current prices while len(mono_stack)>0 and prices[mono_stack[-1]]>=p: prices[mono_stack.pop()]-=p #at last append current price to the mono_stack mono_stack.append(i) return prices
final-prices-with-a-special-discount-in-a-shop
Python Monotonic Stack t.c-O(n) s.c-O(1) with comments
Brillianttyagi
1
59
final prices with a special discount in a shop
1,475
0.755
Easy
22,011
https://leetcode.com/problems/final-prices-with-a-special-discount-in-a-shop/discuss/1928442/easy-python-code
class Solution: def finalPrices(self, prices: List[int]) -> List[int]: output = [] for i in range(len(prices)): for j in range(i+1,len(prices)): if j == len(prices)-1 and prices[j] > prices[i]: output.append(prices[i]) elif j == len(prices)-1 and prices[j] == prices[i]: output.append(prices[i]-prices[j]) elif prices[j] <= prices[i]: output.append(prices[i]-prices[j]) break output.append(prices[-1]) return output
final-prices-with-a-special-discount-in-a-shop
easy python code
dakash682
1
78
final prices with a special discount in a shop
1,475
0.755
Easy
22,012
https://leetcode.com/problems/final-prices-with-a-special-discount-in-a-shop/discuss/1571710/WEEB-DOES-PYTHON
class Solution: def finalPrices(self, prices: List[int]) -> List[int]: ans=[i for i in prices] stack=[] #index stack # increasing stack for i in range(len(prices)): while stack and prices[stack[-1]]>=prices[i]: idx=stack.pop() ans[idx]=ans[idx]-prices[i] stack.append(i) return ans
final-prices-with-a-special-discount-in-a-shop
WEEB DOES PYTHON
Skywalker5423
1
120
final prices with a special discount in a shop
1,475
0.755
Easy
22,013
https://leetcode.com/problems/final-prices-with-a-special-discount-in-a-shop/discuss/1412690/Python-3-Recursive-approach
class Solution: def finalPrices(self, prices: List[int]) -> List[int]: if len(prices) == 1: return prices for j in range(1, len(prices)): if prices[j] <= prices[0]: return [prices[0] - prices[j]] + self.finalPrices(prices[1:]) return [prices[0]] + self.finalPrices(prices[1:])
final-prices-with-a-special-discount-in-a-shop
[Python 3] Recursive approach
shreyamittal117
1
61
final prices with a special discount in a shop
1,475
0.755
Easy
22,014
https://leetcode.com/problems/final-prices-with-a-special-discount-in-a-shop/discuss/1113677/Python-Stack-Approach-or-faster-than-97.74
class Solution: def finalPrices(self, prices: List[int]) -> List[int]: stack = [] n = len(prices) for i in range(n): while stack and prices[stack[-1]] >= prices[i]: prices[stack.pop()] -= prices[i] stack.append(i) return prices
final-prices-with-a-special-discount-in-a-shop
Python Stack Approach | faster than 97.74%
Sanjaychandak95
1
201
final prices with a special discount in a shop
1,475
0.755
Easy
22,015
https://leetcode.com/problems/final-prices-with-a-special-discount-in-a-shop/discuss/829319/Brute-Force-and-optimal-solution-in-python
class Solution: #O(N) Space O(N) Time def finalPrices(self, prices: List[int]) -> List[int]: #[8, 4, 6, 2, 3] #[0, 1, 2, 3, 4] stack = [] for index, price in enumerate(prices): while stack and prices[stack[-1]] >= price: prices[stack.pop()] -= price stack.append(index) return prices #O(N^2) Time and O(N) space -> for building the answer array def finalPrices(self, prices: List[int]) -> List[int]: answer =[] for i in range(len(prices)): for j in range(i+1, len(prices)): if prices[j] <= prices[i]: answer.append(prices[i]-prices[j]) break if len(answer) < i + 1: answer.append(prices[i]) return answer
final-prices-with-a-special-discount-in-a-shop
Brute Force and optimal solution in python
akashgkrishnan
1
136
final prices with a special discount in a shop
1,475
0.755
Easy
22,016
https://leetcode.com/problems/final-prices-with-a-special-discount-in-a-shop/discuss/2821044/PYTHON-Easy-Solution-oror-Next-Smaller-Element-(similar-Ques)-oror-brief-explanation
class Solution: def finalPrices(self, prices: List[int]) -> List[int]: s =[] v =[] for i in range (len(prices)-1,-1,-1): while (len(s)!= 0 and s[-1]> prices[i]): s.pop() if len(s) == 0: v.append(prices[i]) else: p = prices[i]- s[-1] v.append(p) s.append(prices[i]) return v[::-1]
final-prices-with-a-special-discount-in-a-shop
PYTHON Easy Solution || Next Smaller Element (similar Ques) || brief explanation
arjuna07
0
6
final prices with a special discount in a shop
1,475
0.755
Easy
22,017
https://leetcode.com/problems/final-prices-with-a-special-discount-in-a-shop/discuss/2668629/Final-Prices-With-a-Special-Discount-in-a-Shop
class Solution: def finalPrices(self, prices): stack = [] results = [] for i in range(len(prices)-1,-1,-1): if len(stack) == 0: results.append(prices[i]) elif (stack[-1] <= prices[i]) and (len(stack) > 0): results.append(prices[i] - stack[-1]) elif (stack[-1] > prices[i]) and (len(stack) > 0): while stack and (stack[-1] > prices[i]): stack.pop() if not stack: results.append(prices[i]) else: results.append(prices[i] - stack[-1]) stack.append(prices[i]) return results[::-1]
final-prices-with-a-special-discount-in-a-shop
Final Prices With a Special Discount in a Shop
PravinBorate
0
4
final prices with a special discount in a shop
1,475
0.755
Easy
22,018
https://leetcode.com/problems/final-prices-with-a-special-discount-in-a-shop/discuss/2337538/Simple-Python3-Solution
class Solution: def finalPrices(self, prices: List[int]) -> List[int]: if len(prices) == 1: return prices else: for i in range(len(prices)-1): for j in range(i+1, len(prices)): if prices[i] >= prices[j]: prices[i] = prices[i] - prices[j] break return prices
final-prices-with-a-special-discount-in-a-shop
Simple Python3 Solution
vem5688
0
64
final prices with a special discount in a shop
1,475
0.755
Easy
22,019
https://leetcode.com/problems/final-prices-with-a-special-discount-in-a-shop/discuss/2138518/Python-oror-Easy-Monotonic-Stack
class Solution: def finalPrices(self, prices: List[int]) -> List[int]: mono = [] for i, p in enumerate(prices): while mono and prices[mono[-1]] >= p: prices[mono.pop()] -= p mono.append(i) return prices
final-prices-with-a-special-discount-in-a-shop
Python || Easy Monotonic Stack
morpheusdurden
0
124
final prices with a special discount in a shop
1,475
0.755
Easy
22,020
https://leetcode.com/problems/final-prices-with-a-special-discount-in-a-shop/discuss/2123284/Python-Beginner-Easy-Solution
class Solution: def finalPrices(self, prices: List[int]) -> List[int]: for i in range(len(prices)-1): if prices[i] >= prices[i+1]: prices[i] = prices[i] - prices[i+1] elif prices[i] < prices[i+1]: for j in range(i+1,len(prices)): if prices[i] >= prices[j]: prices[i] = prices[i] - prices[j] break return prices
final-prices-with-a-special-discount-in-a-shop
Python Beginner Easy Solution
itsmeparag14
0
67
final prices with a special discount in a shop
1,475
0.755
Easy
22,021
https://leetcode.com/problems/final-prices-with-a-special-discount-in-a-shop/discuss/2025020/Clean-O(n)-solution-w-comments-in-Python-using-monotonic-stack
class Solution: def finalPrices(self, nums: List[int]) -> List[int]: stack = [nums[-1]] # note that we must start with the rear in order to pop out "greater" elements following nums[i] for i in range(len(nums) - 2, -1, -1): # typical operation over monotonic stack, popping the element that is greater nums[i] # eventually top of stack would be the closet element smaller than nums[i] aka the corresponding discount while stack and stack[-1] > nums[i]: stack.pop() discnt = stack[-1] if stack else 0 stack += nums[i], nums[i] -= discnt return nums
final-prices-with-a-special-discount-in-a-shop
Clean O(n) solution w/ comments in Python, using monotonic stack
mousun224
0
93
final prices with a special discount in a shop
1,475
0.755
Easy
22,022
https://leetcode.com/problems/final-prices-with-a-special-discount-in-a-shop/discuss/2010761/Python-straightforward-solution-for-beginners
class Solution: def finalPrices(self, prices: List[int]) -> List[int]: res = [-1] * len(prices) flag = 0 for i in range(len(prices)): for j in range(i+1, len(prices)): if prices[j] <= prices[i]: res[i] = prices[i] - prices[j] flag = 1 break if flag == 0: res[i] = prices[i] flag = 0 return res
final-prices-with-a-special-discount-in-a-shop
Python straightforward solution for beginners
alishak1999
0
42
final prices with a special discount in a shop
1,475
0.755
Easy
22,023
https://leetcode.com/problems/final-prices-with-a-special-discount-in-a-shop/discuss/1881853/solution-using-slicing-(python3)
class Solution: def finalPrices(self, prices: List[int]) -> List[int]: out = [] l = len(prices) for i in range(l): curr = prices[i] my_prices = prices[i+1:] for price in my_prices: if price <= curr: curr -= price break out.append(curr) return out
final-prices-with-a-special-discount-in-a-shop
solution using slicing (python3)
azazellooo
0
28
final prices with a special discount in a shop
1,475
0.755
Easy
22,024
https://leetcode.com/problems/final-prices-with-a-special-discount-in-a-shop/discuss/1866987/Python-Solution-or-Linear-Time-and-Space-or-Monotonic-Stack-Based
class Solution: def finalPrices(self, prices: List[int]) -> List[int]: stack = [] for idx,price in enumerate(prices): print(idx,price) while stack != [] and prices[stack[-1]] >= prices[idx]: prices[stack.pop()] -= prices[idx] stack.append(idx) return prices
final-prices-with-a-special-discount-in-a-shop
Python Solution | Linear Time and Space | Monotonic Stack Based
Gautam_ProMax
0
71
final prices with a special discount in a shop
1,475
0.755
Easy
22,025
https://leetcode.com/problems/final-prices-with-a-special-discount-in-a-shop/discuss/1804349/4-Lines-Python-Solution-oror-80-Faster-oror-Memory-less-than-60
class Solution: def finalPrices(self, prices: List[int]) -> List[int]: for i in range(len(prices)-1): for j in range(i+1, len(prices)): if prices[i]>=prices[j]: prices[i] = (prices[i]-prices[j]) ; break return prices
final-prices-with-a-special-discount-in-a-shop
4-Lines Python Solution || 80% Faster || Memory less than 60%
Taha-C
0
57
final prices with a special discount in a shop
1,475
0.755
Easy
22,026
https://leetcode.com/problems/final-prices-with-a-special-discount-in-a-shop/discuss/1721330/Easy-Python-3-solution-using-Stack-in-O(n)
class Solution: def finalPrices(self, prices: List[int]) -> List[int]: l=[] res=[-1]*len(prices) i=0 while i<len(prices): if len(l)>0: x=l[-1] if x[0]>=prices[i]: res[x[1]]=prices[i] l.pop() else: l.append([prices[i],i]) i+=1 else: l.append([prices[i],i]) i+=1 #print(res) for i in range(len(res)): if res[i]!=-1: res[i]=abs(prices[i]-res[i]) else: res[i]=prices[i] return res
final-prices-with-a-special-discount-in-a-shop
Easy Python 3 solution using Stack in O(n)
amisha26
0
85
final prices with a special discount in a shop
1,475
0.755
Easy
22,027
https://leetcode.com/problems/final-prices-with-a-special-discount-in-a-shop/discuss/1526545/Python3-Solution-with-using-stack
class Solution: def finalPrices(self, prices: List[int]) -> List[int]: stack = [] for i in range(len(prices) - 1, -1, -1): while stack and prices[i] < stack[-1]: stack.pop() price = prices[i] if stack: prices[i] -= stack[-1] stack.append(price) return prices
final-prices-with-a-special-discount-in-a-shop
[Python3] Solution with using stack
maosipov11
0
47
final prices with a special discount in a shop
1,475
0.755
Easy
22,028
https://leetcode.com/problems/final-prices-with-a-special-discount-in-a-shop/discuss/1446197/Python3-Increasing-Stack
class Solution: def finalPrices(self, prices: List[int]) -> List[int]: # increasing stack stack = [] ans = prices.copy() n = len(prices) for i in range(n): p = prices[i] while stack != [] and prices[stack[-1]] >= p: higher_index = stack.pop() ans[higher_index] = ans[higher_index] - p stack.append(i) return ans
final-prices-with-a-special-discount-in-a-shop
[Python3] Increasing Stack
zhanweiting
0
84
final prices with a special discount in a shop
1,475
0.755
Easy
22,029
https://leetcode.com/problems/final-prices-with-a-special-discount-in-a-shop/discuss/1158776/Easy-Python-Soltion
class Solution: def finalPrices(self, prices: List[int]) -> List[int]: final=[] for i in range(len(prices)-1): status= False for j in range(i+1,len(prices)): if prices[i] >= prices[j]: final.append(prices[i]-prices[j]) status = True break if status == False: final.append(prices[i]) final.append(prices[-1]) return(final)
final-prices-with-a-special-discount-in-a-shop
Easy Python Soltion
YashashriShiral
0
109
final prices with a special discount in a shop
1,475
0.755
Easy
22,030
https://leetcode.com/problems/final-prices-with-a-special-discount-in-a-shop/discuss/1129876/Python-solution-faster-than-90
class Solution: def finalPrices(self, prices: List[int]) -> List[int]: l = [-1]*len(prices) for i in range(len(prices)): for j in range(i+1, len(prices)): if prices[i] >= prices[j]: l[i] = prices[i]-prices[j] break if l[i] == -1: l[i] = prices[i] return l
final-prices-with-a-special-discount-in-a-shop
Python solution faster than 90%
Annushams
0
320
final prices with a special discount in a shop
1,475
0.755
Easy
22,031
https://leetcode.com/problems/final-prices-with-a-special-discount-in-a-shop/discuss/930704/Python-Solution-or-Stack
class Solution: def finalPrices(self, prices: List[int]) -> List[int]: stack = [] res = [0]*len(prices) for ind,price in enumerate(prices): while stack and stack[-1][0]>=price: elem,idx = stack.pop() res[idx] = elem-price stack.append((price,ind)) while stack: elem,idx = stack.pop() res[idx] = elem return res
final-prices-with-a-special-discount-in-a-shop
Python Solution | Stack
bharatgg
0
77
final prices with a special discount in a shop
1,475
0.755
Easy
22,032
https://leetcode.com/problems/final-prices-with-a-special-discount-in-a-shop/discuss/1395580/python3-solution-with-explanation
class Solution: def finalPrices(self, prices: List[int]) -> List[int]: # each number in the array is a price # there is a discount where you will get that i term discounted by the next item in the array that is less than or equal to the current item's price # you ONLY need to look ahead of i, never before. AND you stop when you've reached that discount amount finalSale = [] for i in range(0, len(prices)): currentPrice = prices[i] discountAmount = 0 for j in range(i + 1, len(prices)): possibleDiscount = prices[j] if possibleDiscount <= currentPrice: currentPrice = currentPrice - possibleDiscount break finalSale.append(currentPrice) return finalSale
final-prices-with-a-special-discount-in-a-shop
python3 solution with explanation
RobertObrochta
-2
68
final prices with a special discount in a shop
1,475
0.755
Easy
22,033
https://leetcode.com/problems/find-two-non-overlapping-sub-arrays-each-with-target-sum/discuss/1987219/Python-Sliding-Window-O(n)-with-detail-comments.
class Solution: def minSumOfLengths(self, arr: List[int], target: int) -> int: l, windowSum, res = 0, 0, float('inf') min_till = [float('inf')] * len(arr) # records smallest lenth of subarry with target sum up till index i. for r, num in enumerate(arr): # r:right pointer and index of num in arr windowSum += num while windowSum > target: # when the sum of current window is larger then target, shrink the left end of the window one by one until windowSum <= target windowSum -= arr[l] l += 1 # the case when we found a new target sub-array, i.e. current window if windowSum == target: # length of current window curLen = r - l + 1 # min_till[l - 1]: the subarray with min len up till the previous position of left end of the current window: # avoid overlap with cur window # new_sum_of_two_subarray = length of current window + the previous min length of target subarray without overlapping # , if < res, update res. res = min(res, curLen + min_till[l - 1]) # Everytime we found a target window, update the min_till of current right end of the window, # for future use when sum up to new length of sum_of_two_subarray and update the res. min_till[r] = min(curLen, min_till[r - 1]) else: # If windowSum < target: window with current arr[r] as right end does not have any target subarry, # the min_till[r] doesn't get any new minimum update, i.e it equals to previous min_till at index r - 1. min_till[r] = min_till[r - 1] return res if res < float('inf') else -1 Time = O(n): when sliding the window, left and right pointers traverse the array once. Space = O(n): we use one additional list min_till[] to record min length of target subarray till index i.
find-two-non-overlapping-sub-arrays-each-with-target-sum
Python - Sliding Window - O(n) with detail comments.
changyou1009
5
323
find two non overlapping sub arrays each with target sum
1,477
0.37
Medium
22,034
https://leetcode.com/problems/find-two-non-overlapping-sub-arrays-each-with-target-sum/discuss/685324/Python-Prefix-Sums-Heap-(passes-given-test-cases-or-feel-free-to-modify-to-run-all-test-cases)
class Solution: def minSumOfLengths(self, arr: List[int], target: int) -> int: n = len(arr) heap = [] prefix_sum = {} prefix_sum[0] = -1 curr_sum = 0 for i in range(n): curr_sum += arr[i] if prefix_sum.get(curr_sum - target): heapq.heappush(heap,(i - prefix_sum.get(curr_sum - target), i)) prefix_sum[curr_sum] = i while len(heap) > 1: len1, end1 = heapq.heappop(heap) len2, end2 = heapq.heappop(heap) if end1 <= end2 - len2 or end2 <= end1 - len1: return len1 + len2 else: # overlap heapq.heappush(heap, (len1, end1)) return -1
find-two-non-overlapping-sub-arrays-each-with-target-sum
Python Prefix Sums, Heap (passes given test cases | feel free to modify to run all test cases)
sonaksh
2
597
find two non overlapping sub arrays each with target sum
1,477
0.37
Medium
22,035
https://leetcode.com/problems/find-two-non-overlapping-sub-arrays-each-with-target-sum/discuss/1111388/Python3-prefix-sum
class Solution: def minSumOfLengths(self, arr: List[int], target: int) -> int: ans = inf best = [inf]*len(arr) # shortest subarray ending at i prefix = 0 latest = {0: -1} for i, x in enumerate(arr): prefix += x if prefix - target in latest: ii = latest[prefix - target] if ii >= 0: ans = min(ans, i - ii + best[ii]) best[i] = i - ii if i: best[i] = min(best[i-1], best[i]) latest[prefix] = i return ans if ans < inf else -1
find-two-non-overlapping-sub-arrays-each-with-target-sum
[Python3] prefix sum
ye15
1
186
find two non overlapping sub arrays each with target sum
1,477
0.37
Medium
22,036
https://leetcode.com/problems/find-two-non-overlapping-sub-arrays-each-with-target-sum/discuss/2828077/Python-(Simple-Sliding-Window)
class Solution: def minSumOfLengths(self, arr, target): left, total, res, p = 0, 0, float("inf"), [float("inf")] for right in range(len(arr)): total += arr[right] while total > target: total -= arr[left] left += 1 if total == target: res = min(res,p[left] + right - left + 1) p += [min(p[-1],right-left+1)] else: p += [p[-1]] return res if res != float("inf") else -1
find-two-non-overlapping-sub-arrays-each-with-target-sum
Python (Simple Sliding Window)
rnotappl
0
3
find two non overlapping sub arrays each with target sum
1,477
0.37
Medium
22,037
https://leetcode.com/problems/find-two-non-overlapping-sub-arrays-each-with-target-sum/discuss/1526901/Prefix-Sum-oror-Dictionary-oror-Easy-Understand
class Solution: def minSumOfLengths(self, arr: List[int], target: int) -> int: s,res,lsize = 0,float('inf'),float('inf') dic = dict() dic[0] = -1 for i in range(len(arr)): s+=arr[i] dic[s] = i s=0 for i,val in enumerate(arr): s+=val if s-target in dic: lsize = min(i-dic[s-target],lsize) if s+target in dic and lsize!=float('inf'): rsize = dic[s+target]-i res = min(res,lsize+rsize) return -1 if res==float('inf') else res
find-two-non-overlapping-sub-arrays-each-with-target-sum
📌📌 Prefix-Sum || Dictionary || Easy-Understand 🐍
abhi9Rai
0
196
find two non overlapping sub arrays each with target sum
1,477
0.37
Medium
22,038
https://leetcode.com/problems/find-two-non-overlapping-sub-arrays-each-with-target-sum/discuss/764280/implementation-of-the-three-hints
class Solution: def minSumOfLengths(self, arr: List[int], target: int) -> int: n = len(arr) dp_len = [float('inf')]*n start, end = 0, 0 s_sum = arr[0] while start < n: while s_sum < target and end+1 < n: end += 1 s_sum += arr[end] if s_sum == target: dp_len[start] = end-start+1 s_sum -= arr[start] start += 1 prefix = [float('inf')]*n suffix = [float('inf')]*n suffix[-1] = dp_len[-1] for i in range(n-2, 0, -1): suffix[i] = min(suffix[i+1], dp_len[i]) for i,l in enumerate(dp_len): if l != float('inf') and i+l < n: prefix[i+l] = l if i != 0: prefix[i] = min(prefix[i-1], prefix[i]) ans = min(prefix[i]+suffix[i] for i in range(n)) return ans if ans != float('inf') else -1
find-two-non-overlapping-sub-arrays-each-with-target-sum
implementation of the three hints
ytb_algorithm
0
110
find two non overlapping sub arrays each with target sum
1,477
0.37
Medium
22,039
https://leetcode.com/problems/find-two-non-overlapping-sub-arrays-each-with-target-sum/discuss/685703/Python-3-Straightforward-nonoptimal-solution
class Solution: def minSumOfLengths(self, arr: List[int], target: int) -> int: complements = {0: -1} cum_sum = 0 intervals = [] for i, el in enumerate(arr): cum_sum += el if cum_sum - target in complements: intervals.append((complements[cum_sum - target] + 1, i)) complements[cum_sum] = i if len(intervals) < 2: return -1 intervals.sort(key=lambda x: x[1] - x[0]) non_overlap_intervals = filter_non_overlap(intervals) if not non_overlap_intervals: return -1 return sum(interval[1] - interval[0] + 1 for interval in non_overlap_intervals) def filter_non_overlap(intervals): for i in range(len(intervals)): for j in range(i + 1, len(intervals)): if not (intervals[i][0] <= intervals[j][1] and intervals[j][0] <= intervals[i][1]): return intervals[i], intervals[j]
find-two-non-overlapping-sub-arrays-each-with-target-sum
[Python 3] Straightforward nonoptimal solution
armifis
0
64
find two non overlapping sub arrays each with target sum
1,477
0.37
Medium
22,040
https://leetcode.com/problems/allocate-mailboxes/discuss/1496112/Beginner-Friendly-oror-Easy-to-understand-oror-DP-solution
class Solution: def minDistance(self, houses: List[int], k: int) -> int: n = len(houses) houses.sort() cost = [[0 for _ in range(n)] for _ in range(n)] for i in range(n): for j in range(i+1,n): mid_house = houses[(i+j)//2] for t in range(i,j+1): cost[i][j]+= abs(mid_house-houses[t]) @lru_cache(None) def dp(k,ind): if k==0 and ind==n: return 0 if k==0 or ind==n: return float('inf') res = float('inf') for j in range(ind,n): c = cost[ind][j] res = min(res, c + dp(k-1,j+1)) return res return dp(k,0)
allocate-mailboxes
📌📌 Beginner-Friendly || Easy-to-understand || DP solution 🐍
abhi9Rai
3
379
allocate mailboxes
1,478
0.556
Hard
22,041
https://leetcode.com/problems/allocate-mailboxes/discuss/1111625/Python3-dp-w-running-median
class Solution: def minDistance(self, houses: List[int], k: int) -> int: houses.sort() # ascending order n = len(houses) mdist = [[0]*n for _ in range(n)] # mdist[i][j] median distance of houses[i:j+1] for i in range(n): for j in range(i+1, n): mdist[i][j] = mdist[i][j-1] + houses[j] - houses[i+j >> 1] @cache def fn(n, k): """Return min distance of allocating k mailboxes to n houses.""" if n <= k: return 0 # one mailbox for each house if k == 1: return mdist[0][n-1] ans = inf for nn in range(k-1, n): ans = min(ans, fn(nn, k-1) + mdist[nn][n-1]) return ans return fn(n, k)
allocate-mailboxes
[Python3] dp w/ running median
ye15
1
208
allocate mailboxes
1,478
0.556
Hard
22,042
https://leetcode.com/problems/allocate-mailboxes/discuss/2077936/Python3-DP-O(kn2)-time-O(max(knn2))-space
class Solution: def minDistance(self, houses: List[int], k: int) -> int: houses.sort() len_houses = len(houses) if k == len_houses: return 0 cost = {**{(i,i): 0 for i in range(len_houses)}, **{(i,i+1):houses[i+1] - houses[i] for i in range(len_houses - 1)}} for i in range(len_houses): for r in range(1,min(i+1,len_houses-i)): cost[i-r,i+r] = cost[i-r+1,i+r-1] + houses[i+r] - houses[i-r] if i+r + 1 < len_houses: cost[i-r,i+r+1] = cost[i-r+1,i+r] + houses[i+r+1] - houses[i-r] dp = {(j,1):cost[0,j] for j in range(len_houses)} # dp(i,k) is total cost of using k mail boxes for first i houses for box in range(2, k + 1): for j in range(box - 1, len_houses): dp[j,box] = min(dp[i,box-1] + cost[i+1,j] for i in range(box - 2, j)) return dp[len_houses - 1, k]
allocate-mailboxes
Python3 DP O(kn^2) time O(max(kn,n^2)) space
bryceat
0
95
allocate mailboxes
1,478
0.556
Hard
22,043
https://leetcode.com/problems/running-sum-of-1d-array/discuss/2306599/Easy-to-understand-Python-solution
class Solution(object): def runningSum(self, nums): result = [] current_sum = 0 for i in range(0, len(nums)): result.append(current_sum + nums[i]) current_sum = result[i] return result
running-sum-of-1d-array
Easy to understand Python solution
Balance-Coffee
13
324
running sum of 1d array
1,480
0.892
Easy
22,044
https://leetcode.com/problems/running-sum-of-1d-array/discuss/2329597/Python-O(n)-simple-solution-easy-and-fast-_
class Solution: def runningSum(self, nums: List[int]) -> List[int]: for i in range(1, len(nums)): nums[i] = nums[i] + nums[i-1] return nums
running-sum-of-1d-array
[Python] O(n) simple solution easy and fast ^_^
luluboy168
7
263
running sum of 1d array
1,480
0.892
Easy
22,045
https://leetcode.com/problems/running-sum-of-1d-array/discuss/2321846/Python-Simple-faster-solution-oror-Prefix-Sum
class Solution: def runningSum(self, nums: List[int]) -> List[int]: result = [nums[0]] for i in range(1,len(nums)): result.append(result[i-1] + nums[i]) return result
running-sum-of-1d-array
[Python] Simple faster solution || Prefix Sum
Buntynara
4
129
running sum of 1d array
1,480
0.892
Easy
22,046
https://leetcode.com/problems/running-sum-of-1d-array/discuss/2012935/Python-3-line-solution
class Solution: def runningSum(self, nums: List[int]) -> List[int]: for i in range(1,len(nums)): nums[i] += nums[i-1] return nums
running-sum-of-1d-array
Python 3 line solution
shikha_pandey
4
198
running sum of 1d array
1,480
0.892
Easy
22,047
https://leetcode.com/problems/running-sum-of-1d-array/discuss/1273236/WEEB-DOES-PYTHON-in-O(n)
class Solution: def runningSum(self, nums: List[int]) -> List[int]: result, count = [], 0 for i in range(len(nums)): result.append(count+nums[i]) count+=nums[i] return result
running-sum-of-1d-array
WEEB DOES PYTHON in O(n)
Skywalker5423
4
279
running sum of 1d array
1,480
0.892
Easy
22,048
https://leetcode.com/problems/running-sum-of-1d-array/discuss/941089/Python-Simple-Solution
class Solution: def runningSum(self, nums: List[int]) -> List[int]: for i in range(1,len(nums)): nums[i]=nums[i]+nums[i-1] return nums
running-sum-of-1d-array
Python Simple Solution
lokeshsenthilkumar
4
745
running sum of 1d array
1,480
0.892
Easy
22,049
https://leetcode.com/problems/running-sum-of-1d-array/discuss/2496170/Python-Fast-and-Simple-(-1-Liner-)
class Solution: def runningSum(self, n: List[int]) -> List[int]: l = [ sum(n[:i+1]) for i in range(len(n)) ] return l
running-sum-of-1d-array
Python Fast & Simple ( 1 Liner )
SouravSingh49
3
215
running sum of 1d array
1,480
0.892
Easy
22,050
https://leetcode.com/problems/running-sum-of-1d-array/discuss/2096371/C%2B%2BororJavaororScala-Easy-or-Python-One-Liners
class Solution: def runningSum(self, nums: List[int]) -> List[int]: return reduce(lambda l, curr: l + [l[-1] + curr] , nums[1:], [nums[0]])
running-sum-of-1d-array
✅ C++||Java||Scala - Easy | Python One Liners
constantine786
3
302
running sum of 1d array
1,480
0.892
Easy
22,051
https://leetcode.com/problems/running-sum-of-1d-array/discuss/2096371/C%2B%2BororJavaororScala-Easy-or-Python-One-Liners
class Solution: def runningSum(self, nums: List[int]) -> List[int]: return accumulate(nums)
running-sum-of-1d-array
✅ C++||Java||Scala - Easy | Python One Liners
constantine786
3
302
running sum of 1d array
1,480
0.892
Easy
22,052
https://leetcode.com/problems/running-sum-of-1d-array/discuss/1061423/Python-solution(WITH-explanation)
class Solution: def runningSum(self, nums: List[int]) -> List[int]: output = [] output.append(nums[0]) for i in range(1, len(nums)): output.append(nums[i] + output[i-1]) return output
running-sum-of-1d-array
Python solution(WITH explanation)
Sissi0409
3
957
running sum of 1d array
1,480
0.892
Easy
22,053
https://leetcode.com/problems/running-sum-of-1d-array/discuss/2523117/C%2B%2BJavaPython-Optimized-100-faster-and-efficient-Solution
class Solution: def runningSum(self, nums: List[int]) -> List[int]: for i in range(1, len(nums)): nums[i] = nums[i-1] + nums[i] return nums
running-sum-of-1d-array
C++/Java/Python Optimized 100% faster and efficient Solution
arpit3043
2
197
running sum of 1d array
1,480
0.892
Easy
22,054
https://leetcode.com/problems/running-sum-of-1d-array/discuss/931711/Easy-Python-Solution
class Solution: def runningSum(self, nums: List[int]) -> List[int]: for i in range(1, len(nums)): nums[i] += nums[i-1] return nums; # Time Complexity: O(N) # Space Complexity: O(1)
running-sum-of-1d-array
Easy Python Solution
hbjORbj
2
299
running sum of 1d array
1,480
0.892
Easy
22,055
https://leetcode.com/problems/running-sum-of-1d-array/discuss/2572181/Python3%3A-Faster-than-92.88
class Solution: def runningSum(self, nums: List[int]) -> List[int]: addition = 0 for i in range(len(nums)): nums[i] = nums[i] + addition addition = nums[i] return nums
running-sum-of-1d-array
Python3: Faster than 92.88%
happinessss57
1
128
running sum of 1d array
1,480
0.892
Easy
22,056
https://leetcode.com/problems/running-sum-of-1d-array/discuss/2439684/Python-Super-Simple-Cake-Walk-Solution
class Solution: def runningSum(self, nums: List[int]) -> List[int]: # Loop through the list and replace each value for the sum of last value and current value for i in range(1, len(nums)): nums[i] = nums[i] + nums[i-1] return nums
running-sum-of-1d-array
🐍🐲 Python Super Simple Cake Walk Solution 🐲🐍
sHadowSparK
1
92
running sum of 1d array
1,480
0.892
Easy
22,057
https://leetcode.com/problems/running-sum-of-1d-array/discuss/2250204/Python-O(N)-oror-O(1)-Runtime%3A-51ms-oror-Memory%3A-14mb-70.94
class Solution: # O(n) || O(1) # Runtime: 51ms || Memory: 14mb 70.94% def runningSum(self, nums: List[int]) -> List[int]: if len(nums) <= 1: return nums currSum = nums[0] for i in range(len(nums) - 1): nums[i] = currSum currSum += nums[i + 1] nums[i+1] = currSum return nums
running-sum-of-1d-array
Python O(N) || O(1) Runtime: 51ms || Memory: 14mb 70.94%
arshergon
1
92
running sum of 1d array
1,480
0.892
Easy
22,058
https://leetcode.com/problems/running-sum-of-1d-array/discuss/2097219/Python-Simple-Python-Solution
class Solution: def runningSum(self, nums: List[int]) -> List[int]: dp = [0 for _ in range(len(nums))] dp[0] = nums[0] for i in range(1,len(nums)): dp[i] = dp[i-1]+nums[i] return dp
running-sum-of-1d-array
[ Python ] ✅✅ Simple Python Solution ✌👍
ASHOK_KUMAR_MEGHVANSHI
1
345
running sum of 1d array
1,480
0.892
Easy
22,059
https://leetcode.com/problems/running-sum-of-1d-array/discuss/2096677/5-Line-Python-simple-solution
class Solution: def runningSum(self, nums: List[int]) -> List[int]: result ,sum = [],0 for x in nums : sum += x result.append(sum) return result
running-sum-of-1d-array
5 Line Python simple solution
tarekllf01
1
81
running sum of 1d array
1,480
0.892
Easy
22,060
https://leetcode.com/problems/running-sum-of-1d-array/discuss/2096626/Python-or-Beats-95-or-6-solutions-or-1337-Optimized-2022
class Solution: def runningSum(self, nums):# List[int]) -> List[int]: for i, j in zip(range(1, len(nums)), range(0, len(nums) - 1)): nums[i] += nums[j] return nums
running-sum-of-1d-array
Python | Beats 95% | 6 solutions | 1337 Optimized 2022 🤹
kardopaska
1
22
running sum of 1d array
1,480
0.892
Easy
22,061
https://leetcode.com/problems/running-sum-of-1d-array/discuss/2096626/Python-or-Beats-95-or-6-solutions-or-1337-Optimized-2022
class Solution: def runningSum(self, nums):# List[int]) -> List[int]: for i in range(1, len(nums)): nums[i] += nums[i-1] return nums
running-sum-of-1d-array
Python | Beats 95% | 6 solutions | 1337 Optimized 2022 🤹
kardopaska
1
22
running sum of 1d array
1,480
0.892
Easy
22,062
https://leetcode.com/problems/running-sum-of-1d-array/discuss/2096626/Python-or-Beats-95-or-6-solutions-or-1337-Optimized-2022
class Solution: def runningSum(self, nums: List[int]) -> List[int]: total = 0 tots = [] for x in nums: total = total + x tots.append(total) return tots
running-sum-of-1d-array
Python | Beats 95% | 6 solutions | 1337 Optimized 2022 🤹
kardopaska
1
22
running sum of 1d array
1,480
0.892
Easy
22,063
https://leetcode.com/problems/running-sum-of-1d-array/discuss/2096585/RUNNING-SUM-OF-1D-ARRAY-short-and-clean-code
class Solution: def runningSum(self, nums: List[int]) -> List[int]: lst=[] for i in range(len(nums)): n=sum(nums[0:i+1]) lst.append(n) return lst
running-sum-of-1d-array
RUNNING SUM OF 1D ARRAY - short and clean code
T1n1_B0x1
1
7
running sum of 1d array
1,480
0.892
Easy
22,064
https://leetcode.com/problems/running-sum-of-1d-array/discuss/2096376/Python-3-or-2-lines-or-%3A
class Solution: def runningSum(self, nums: List[int]) -> List[int]: curr = 0 return [ curr := curr + i for i in nums ]
running-sum-of-1d-array
Python 3 | 2 lines | :=
mshanker
1
15
running sum of 1d array
1,480
0.892
Easy
22,065
https://leetcode.com/problems/running-sum-of-1d-array/discuss/2096203/PYTHON-oror-MINIMAL-MEMORY-and-TIME-oror-WITH-COMMENTS-oror-2-LINER
class Solution: def runningSum(self, nums: List[int]) -> List[int]: # Looping through all indices for i in range(1,len(nums)): # Finding running sum by adding with previously element nums[i] = nums[i]+nums[i-1] return nums
running-sum-of-1d-array
PYTHON || MINIMAL MEMORY & TIME || WITH COMMENTS || 2 LINER
klmsathish
1
114
running sum of 1d array
1,480
0.892
Easy
22,066
https://leetcode.com/problems/running-sum-of-1d-array/discuss/1717243/python-one-line
class Solution: def runningSum(self, nums: List[int]) -> List[int]: return [ sum(nums[:i]) for i in range(1,len(nums)+1) ]
running-sum-of-1d-array
python one line
jasonjfk
1
65
running sum of 1d array
1,480
0.892
Easy
22,067
https://leetcode.com/problems/running-sum-of-1d-array/discuss/1714663/running-sum-of-1d-Array
class Solution: def runningSum(self, nums: List[int]) -> List[int]: lis=[] for x in range(len(nums)): sums=0 for y in range(0,x+1): sums += nums[y] lis.append(sums) return lis
running-sum-of-1d-array
running sum of 1d Array
apoorv11jain
1
137
running sum of 1d array
1,480
0.892
Easy
22,068
https://leetcode.com/problems/running-sum-of-1d-array/discuss/1714663/running-sum-of-1d-Array
class Solution: def runningSum(self, nums: List[int]) -> List[int]: lis=[nums[0]] for x in range(1,len(nums)): lis.append(lis[x-1]+nums[x]) return lis
running-sum-of-1d-array
running sum of 1d Array
apoorv11jain
1
137
running sum of 1d array
1,480
0.892
Easy
22,069
https://leetcode.com/problems/running-sum-of-1d-array/discuss/1708191/1480-running-sum-of-1d-array
class Solution(object): def runningSum(self, nums): """ :type nums: List[int] :rtype: List[int] """ rArray = [] for i in range(len(nums)): sum = 0 for ging in range(i+1): sum += nums[ging] rArray.append(sum) return rArray
running-sum-of-1d-array
1480 - running sum of 1d array
ankit61d
1
41
running sum of 1d array
1,480
0.892
Easy
22,070
https://leetcode.com/problems/running-sum-of-1d-array/discuss/1363340/4-lines-code-using-python
class Solution: def runningSum(self, nums: List[int]) -> List[int]: total = 0 n=[] for i in range(len(nums)): total = total + nums[i] n.append(total) return n
running-sum-of-1d-array
4 lines code using python
gulsan
1
83
running sum of 1d array
1,480
0.892
Easy
22,071
https://leetcode.com/problems/running-sum-of-1d-array/discuss/968951/Python3-no-imports-O(1)-Memory-O(N)-Runtime
class Solution: def runningSum(self, nums: List[int]) -> List[int]: idx = 1 while idx < len(nums): nums[idx] = nums[idx] + nums[idx - 1] idx += 1 return nums
running-sum-of-1d-array
Python3 no imports - O(1) Memory, O(N) Runtime
JuanRodriguez
1
348
running sum of 1d array
1,480
0.892
Easy
22,072
https://leetcode.com/problems/running-sum-of-1d-array/discuss/901981/Python3-simple-with-86.44-time-complexity-and-99.99-space-complexity
class Solution: def runningSum(self, list1): ans = [list1[0]] for i in range(1,len(list1)): ans.append(ans[i-1] + list1[i]) return ans
running-sum-of-1d-array
Python3 simple with 86.44% time complexity and 99.99% space complexity
changanidhaval
1
97
running sum of 1d array
1,480
0.892
Easy
22,073
https://leetcode.com/problems/running-sum-of-1d-array/discuss/2849631/Easy-Python-Solution-1480.-Running-Sum-of-1d-Array
class Solution: def runningSum(self, nums: List[int]) -> List[int]: ans = [] ans.append(nums[0]) del nums[0] while nums: temp = int(nums[0]) + int(ans[-1]) ans.append(temp) del nums[0] return ans
running-sum-of-1d-array
✅ Easy Python Solution - 1480. Running Sum of 1d Array
Brian_Daniel_Thomas
0
1
running sum of 1d array
1,480
0.892
Easy
22,074
https://leetcode.com/problems/running-sum-of-1d-array/discuss/2849631/Easy-Python-Solution-1480.-Running-Sum-of-1d-Array
class Solution: def runningSum(self, nums: List[int]) -> List[int]: for i in range(1, int(len(nums))): nums[i] = nums[i] + nums[i-1] return nums
running-sum-of-1d-array
✅ Easy Python Solution - 1480. Running Sum of 1d Array
Brian_Daniel_Thomas
0
1
running sum of 1d array
1,480
0.892
Easy
22,075
https://leetcode.com/problems/running-sum-of-1d-array/discuss/2849361/Adding-1-D-Array
class Solution: def runningSum(self, nums): for i in range(len(nums) - 1): nums[i + 1] += nums[i] return nums Solution()
running-sum-of-1d-array
Adding 1-D Array
JayeshMinigi
0
2
running sum of 1d array
1,480
0.892
Easy
22,076
https://leetcode.com/problems/running-sum-of-1d-array/discuss/2838892/Python-Fast-and-Simple-Solution
class Solution: def runningSum(self, nums): summed = [] length = len(nums) total = nums[0] for i in range(length): if i == 0: summed.append(nums[i]) else: try: total += nums[i] summed.append(total) except: pass return summed
running-sum-of-1d-array
Python - Fast & Simple Solution
BladeStew
0
1
running sum of 1d array
1,480
0.892
Easy
22,077
https://leetcode.com/problems/running-sum-of-1d-array/discuss/2838072/One-Line-Python3-Solution
class Solution: def runningSum(self, nums: List[int]) -> List[int]: return [sum(nums[:i+1]) for i in range(len(nums))]
running-sum-of-1d-array
One Line Python3 Solution
naveenbharath
0
1
running sum of 1d array
1,480
0.892
Easy
22,078
https://leetcode.com/problems/running-sum-of-1d-array/discuss/2837117/One-line-python-solution
class Solution: def runningSum(self, nums: List[int]) -> List[int]: return [sum(nums[0:i]) for i in range(1,len(nums)+1)]
running-sum-of-1d-array
One line python solution
lornedot
0
2
running sum of 1d array
1,480
0.892
Easy
22,079
https://leetcode.com/problems/running-sum-of-1d-array/discuss/2834910/running-sum
class Solution: def runningSum(self, nums: List[int]) -> List[int]: result = [] for i in range(len(nums)): yield sum(nums[:i + 1])
running-sum-of-1d-array
running sum
arthur54342
0
1
running sum of 1d array
1,480
0.892
Easy
22,080
https://leetcode.com/problems/running-sum-of-1d-array/discuss/2834910/running-sum
class Solution: def runningSum(self, nums: List[int]) -> List[int]: total = 0 for n in nums: total += n yield total
running-sum-of-1d-array
running sum
arthur54342
0
1
running sum of 1d array
1,480
0.892
Easy
22,081
https://leetcode.com/problems/running-sum-of-1d-array/discuss/2833247/python-solution
class Solution: def runningSum(self, nums: List[int]) -> List[int]: for i in range(len(nums)): if i==0: nums[i]=nums[i] else: nums[i]+=nums[i-1] return nums
running-sum-of-1d-array
python solution
sintin1310
0
3
running sum of 1d array
1,480
0.892
Easy
22,082
https://leetcode.com/problems/running-sum-of-1d-array/discuss/2824524/Python-easy-solution
class Solution: def runningSum(self, nums: List[int]) -> List[int]: temp = [] new_list = [] for num in nums: temp.append(num) new_list.append(sum(temp)) return new_list
running-sum-of-1d-array
Python easy solution
SivanShl
0
4
running sum of 1d array
1,480
0.892
Easy
22,083
https://leetcode.com/problems/running-sum-of-1d-array/discuss/2823200/The-simplest-solution
class Solution: def runningSum(self, nums: List[int]) -> List[int]: for i in range(1,len(nums)): nums[i] += nums[i-1] return nums
running-sum-of-1d-array
The simplest solution
sarah_di
0
1
running sum of 1d array
1,480
0.892
Easy
22,084
https://leetcode.com/problems/running-sum-of-1d-array/discuss/2822974/inspector-chewgum
class Solution: def runningSum(self, nums: List[int]) -> List[int]: c=[] s=0 for i in range(len(nums)): s=s+nums[i] c.append(s) return(c)
running-sum-of-1d-array
inspector chewgum
devbhardwaj7078
0
1
running sum of 1d array
1,480
0.892
Easy
22,085
https://leetcode.com/problems/running-sum-of-1d-array/discuss/2822664/Python3-solution-with-explanation
class Solution: def runningSum(self, nums: List[int]) -> List[int]: total, result = 0, [0] * len(nums) for i in range(len(nums)): total += nums[i] result[i] = total return result
running-sum-of-1d-array
Python3 solution with explanation
AnanyaRaval
0
3
running sum of 1d array
1,480
0.892
Easy
22,086
https://leetcode.com/problems/running-sum-of-1d-array/discuss/2819024/Python-My-O(n)-solution
class Solution: def runningSum(self, nums: List[int]) -> List[int]: out = [] running_sum = 0 for num in nums: running_sum += num out.append(running_sum) return out
running-sum-of-1d-array
[Python] My O(n) solution
manytenks
0
4
running sum of 1d array
1,480
0.892
Easy
22,087
https://leetcode.com/problems/running-sum-of-1d-array/discuss/2818425/Simple-python-solution
class Solution: def runningSum(self, nums: List[int]) -> List[int]: res=[] res.append(nums[0]) i=1 while(i<len(nums)): res.append(res[i-1]+nums[i]) i+=1 return res
running-sum-of-1d-array
Simple python solution
saisupriyavaru
0
3
running sum of 1d array
1,480
0.892
Easy
22,088
https://leetcode.com/problems/running-sum-of-1d-array/discuss/2816164/Simple-addition-without-creating-extra-variables
class Solution: def runningSum(self, nums: List[int]) -> List[int]: for i in range(1,len(nums)): nums[i]=nums[i]+nums[i-1] return nums
running-sum-of-1d-array
Simple addition without creating extra variables
pratyushjain99
0
4
running sum of 1d array
1,480
0.892
Easy
22,089
https://leetcode.com/problems/running-sum-of-1d-array/discuss/2813919/My-Solution
class Solution: def runningSum(self, nums: List[int]) -> List[int]: if len(nums) > 1: curSum = nums[0] for i in range(1, len(nums)): nums[i] += curSum curSum = nums[i] return nums
running-sum-of-1d-array
My Solution
phuocnguyenquang34
0
3
running sum of 1d array
1,480
0.892
Easy
22,090
https://leetcode.com/problems/running-sum-of-1d-array/discuss/2813203/Python3-or-prefixsum-O(n)-solution
class Solution: def runningSum(self, nums: List[int]) -> List[int]: res = [nums[0]] for i in range(1, len(nums)): res.append(res[-1] + nums[i]) return res
running-sum-of-1d-array
Python3 | prefixsum O(n) solution
LaggerKrd
0
3
running sum of 1d array
1,480
0.892
Easy
22,091
https://leetcode.com/problems/running-sum-of-1d-array/discuss/2811364/recursive-solution-on-python
class Solution: def runningSum(self, nums: List[int]) -> List[int]: for i in range(1,len(nums)): nums[i] +=nums[i-1] return nums
running-sum-of-1d-array
recursive solution on python
roger880327
0
2
running sum of 1d array
1,480
0.892
Easy
22,092
https://leetcode.com/problems/running-sum-of-1d-array/discuss/2810376/Python3-44ms-88.98
class Solution: def runningSum(self, nums: List[int]) -> List[int]: sum = 0 for i, val in enumerate(nums): sum += val nums[i] = sum return nums
running-sum-of-1d-array
Python3 44ms 88.98%
ajrs
0
2
running sum of 1d array
1,480
0.892
Easy
22,093
https://leetcode.com/problems/running-sum-of-1d-array/discuss/2803833/Python-3-easy-Solution
class Solution: def runningSum(self, nums: List[int]) -> List[int]: lst=[] inSum=0 for i in nums: lst.append(i+inSum) inSum+=i return lst
running-sum-of-1d-array
Python 3 easy Solution
jagdtri2003
0
5
running sum of 1d array
1,480
0.892
Easy
22,094
https://leetcode.com/problems/running-sum-of-1d-array/discuss/2803297/Sum-of-consecutive-numbers
class Solution: def runningSum(self, nums: List[int]) -> List[int]: runningSum = [nums[0]] for i in range(1, len(nums)): currentSum = 0 for j in range(i+1): currentSum += nums[j] runningSum.append(currentSum) return runningSum
running-sum-of-1d-array
Sum of consecutive numbers
user3189i
0
1
running sum of 1d array
1,480
0.892
Easy
22,095
https://leetcode.com/problems/least-number-of-unique-integers-after-k-removals/discuss/1269930/Beats-99-runtime-oror-98-memory-oror-python-oror-easy
class Solution: def findLeastNumOfUniqueInts(self, arr: List[int], k: int) -> int: count = Counter(arr) ans = len(count) for i in sorted(count.values()): k -= i if k < 0: break ans -= 1 return ans
least-number-of-unique-integers-after-k-removals
Beats 99% runtime || 98% memory || python || easy
chikushen99
11
1,000
least number of unique integers after k removals
1,481
0.569
Medium
22,096
https://leetcode.com/problems/least-number-of-unique-integers-after-k-removals/discuss/1564875/Greedy-oror-Well-Explained-oror-Two-Approaches-oror-Heap
class Solution: def findLeastNumOfUniqueInts(self, arr: List[int], m: int) -> int: dic = Counter(arr) heap = [] for k in dic: heapq.heappush(heap,(dic[k],k)) while heap and m>0: f,k = heapq.heappop(heap) mi = min(m,f) m-=mi f-=mi if f!=0: heapq.heappush(heap,(f,k)) return len(heap)
least-number-of-unique-integers-after-k-removals
📌📌 Greedy || Well-Explained || Two-Approaches || Heap 🐍
abhi9Rai
4
279
least number of unique integers after k removals
1,481
0.569
Medium
22,097
https://leetcode.com/problems/least-number-of-unique-integers-after-k-removals/discuss/2681207/Python-97.55-faster-EXPLAINED-or-breakdown-or-Hash-table
class Solution: def findLeastNumOfUniqueInts(self, arr: List[int], k: int) -> int: d = {} for i in arr: d[i] = d.get(i, 0)+1 unique_elements = len(d) curr_deleted = 0 count = 0 for frequency in sorted(d.values()): curr_deleted += frequency count += 1 if curr_deleted < k: continue elif curr_deleted == k: return unique_elements-count else: return unique_elements-(count-1)
least-number-of-unique-integers-after-k-removals
Python 97.55% faster [EXPLAINED] | breakdown | Hash table
diwakar_4
1
183
least number of unique integers after k removals
1,481
0.569
Medium
22,098
https://leetcode.com/problems/least-number-of-unique-integers-after-k-removals/discuss/1719848/Python-Heap-Solution-O(klogN)-time-complexity.
class Solution: def findLeastNumOfUniqueInts(self, arr: List[int], k: int) -> int: h = {} for i in arr: h[i] = 1 + h.get(i, 0) k_smallest = list(h.values()) heapq.heapify(k_smallest) removal_count =0 while (removal_count<k): removal_count+= heapq.heappop(k_smallest) if removal_count == k: return len(k_smallest) return len(k_smallest)+1
least-number-of-unique-integers-after-k-removals
Python Heap Solution - O(klogN) time complexity.
mamoonmasud
1
273
least number of unique integers after k removals
1,481
0.569
Medium
22,099