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/daily-temperatures/discuss/1842807/Python-solution-using-namedtuple
class Solution: def dailyTemperatures(self, temperatures: List[int]) -> List[int]: temp_tuple = collections.namedtuple('Temp', ('temp', 'day')) ans = [0 for _ in range(len(temperatures))] stack = [temp_tuple(temperatures[0], 0)] for i, t in enumerate(temperatures): if i =...
daily-temperatures
Python solution using namedtuple
mitani-reishi116
0
43
daily temperatures
739
0.666
Medium
12,100
https://leetcode.com/problems/daily-temperatures/discuss/1635721/Easy-Python-Solution-using-Stack-O(N)
class Solution: def dailyTemperatures(self, temperatures: List[int]) -> List[int]: n = len(temperatures) store = {} stack = [] result = [0]*n stack.append(0) for i in range(1,n): if temperatures[i]>temperatures[stack[-1]]: while le...
daily-temperatures
Easy Python Solution using Stack O(N)
RG97
0
88
daily temperatures
739
0.666
Medium
12,101
https://leetcode.com/problems/daily-temperatures/discuss/1620977/Stack-Easy-Understanding-Python-Beats-63.65-of-python3-submissions
class Solution: def dailyTemperatures(self, temperatures: List[int]) -> List[int]: stack = [] n = len(temperatures) output = [None] * n for i in range(n-1,-1,-1): while stack and temperatures[stack[-1]] <= temperatures[i]: stack.pop() ...
daily-temperatures
Stack, Easy Understanding, Python, Beats 63.65 % of python3 submissions
jasonguan0107
0
123
daily temperatures
739
0.666
Medium
12,102
https://leetcode.com/problems/daily-temperatures/discuss/1594896/Monotonic-Stack
class Solution: def dailyTemperatures(self, temperatures: List[int]) -> List[int]: stack = [] #monotonous stack n = len(temperatures) ans = [0]*n for i in range(n-1, -1, -1): while stack!=[] and temperatures[i]>=stack[-1][0]: stack.pop(-1) if ...
daily-temperatures
Monotonic Stack
steven0821
0
171
daily temperatures
739
0.666
Medium
12,103
https://leetcode.com/problems/daily-temperatures/discuss/1576122/Python-stack-solution-faster-than-79
class Solution: def dailyTemperatures(self, temperatures: List[int]) -> List[int]: stack = [] res = [0] * len(temperatures) for i, temp in enumerate(temperatures): while stack and temp > stack[-1][0]: res[stack[-1][1]] = i - stack[-1][1] stack.pop(...
daily-temperatures
Python stack solution faster than 79%
dereky4
0
99
daily temperatures
739
0.666
Medium
12,104
https://leetcode.com/problems/daily-temperatures/discuss/1574768/ez-python-stack-solution
class Solution: def dailyTemperatures(self, temperatures: List[int]) -> List[int]: stack = [] output_rev = [] day = len(temperatures) - 1 for temp in temperatures[::-1]: if not stack: output_rev.append(0) stack.append([temp,day]) ...
daily-temperatures
ez python stack solution
yingziqing123
0
62
daily temperatures
739
0.666
Medium
12,105
https://leetcode.com/problems/daily-temperatures/discuss/1517688/Python-Simple-Stack-Solution-with-Explanation
class Solution: def dailyTemperatures(self, temperatures: List[int]) -> List[int]: n, stack = len(temperatures), [] res = [0] * n for i in reversed(range(n)): while stack and temperatures[stack[-1]] <= temperatures[i]: stack.pop() ...
daily-temperatures
[Python] Simple Stack Solution with Explanation
Saksham003
0
162
daily temperatures
739
0.666
Medium
12,106
https://leetcode.com/problems/daily-temperatures/discuss/1462642/Simply-monotonic-stack-in-Python
class Solution: def dailyTemperatures(self, temperatures: List[int]) -> List[int]: n, ms = len(temperatures), [0] r = [0] * n for i in range(1, n): while ms and temperatures[ms[-1]] < temperatures[i]: r[ms.pop()] = i - ms[-1] ms += i, return r
daily-temperatures
Simply monotonic stack in Python
mousun224
0
134
daily temperatures
739
0.666
Medium
12,107
https://leetcode.com/problems/daily-temperatures/discuss/1447283/Simple-Python-O(n)-monotonic-stack-solution
class Solution: def dailyTemperatures(self, temperatures: List[int]) -> List[int]: ret = [0]*len(temperatures) mono_stack = [] for i, t in enumerate(temperatures): while mono_stack and mono_stack[-1][0] < t: _, prev_i = mono_stack.pop() ret[prev_i]...
daily-temperatures
Simple Python O(n) monotonic stack solution
Charlesl0129
0
146
daily temperatures
739
0.666
Medium
12,108
https://leetcode.com/problems/daily-temperatures/discuss/1314388/Python3-solution-using-stack
class Solution: def dailyTemperatures(self, temperatures: List[int]) -> List[int]: ans = [0]*len(temperatures) st = [] for i,j in enumerate(temperatures): while st and j > st[-1][0]: x,y = st.pop() ans[y] = i - y st.append((j,i)) ...
daily-temperatures
Python3 solution using stack
EklavyaJoshi
0
157
daily temperatures
739
0.666
Medium
12,109
https://leetcode.com/problems/daily-temperatures/discuss/761486/Python-or-Need-Suggestions-to-improve-complexity
class Solution: def dailyTemperatures(self, T: List[int]) -> List[int]: start = first = 0 res = [0]*len(T) while start<len(T)-1: first+=1 if T[first]>T[start]: res[start]=(first-start) start+=1 first = start ...
daily-temperatures
[Python] | Need Suggestions to improve complexity??
rachitsxn292
0
155
daily temperatures
739
0.666
Medium
12,110
https://leetcode.com/problems/daily-temperatures/discuss/343443/Python-3-self-explanatory-solution
class Solution: def dailyTemperatures(self, T: List[int]) -> List[int]: l=len(T) res=[0]*l stack=[(T[0],0)] i=1 while stack and i<l: while stack and T[i]>stack[-1][0]: res[stack[-1][1]]=i-stack[-1][1] stack.pop() stack.a...
daily-temperatures
Python 3 self explanatory solution
ketan35
0
439
daily temperatures
739
0.666
Medium
12,111
https://leetcode.com/problems/daily-temperatures/discuss/1435642/Python-Clean
class Solution: def dailyTemperatures(self, temperatures: List[int]) -> List[int]: N = len(temperatures) stack, answer = [], [0 for _ in range(N)] for index, value in enumerate(temperatures): while stack and stack[-1][1] < value: i = stack.pop()[0] ...
daily-temperatures
[Python] Clean
111989
-1
175
daily temperatures
739
0.666
Medium
12,112
https://leetcode.com/problems/delete-and-earn/discuss/916859/Python3-top-down-dp
class Solution: def deleteAndEarn(self, nums: List[int]) -> int: mp = {} for x in nums: mp[x] = x + mp.get(x, 0) @lru_cache(None) def fn(i): """Return maximum points one can earn from nums[i:].""" if i >= len(nums): return 0 if nums[i] +...
delete-and-earn
[Python3] top-down dp
ye15
7
273
delete and earn
740
0.573
Medium
12,113
https://leetcode.com/problems/delete-and-earn/discuss/916859/Python3-top-down-dp
class Solution: def deleteAndEarn(self, nums: List[int]) -> int: freq = Counter(nums) prev = -inf f0 = f1 = 0 for x in sorted(freq): if prev + 1 == x: f0, f1 = max(f0, f1), f0 + x*freq[x] else: f0, f1 = max(f0, f1), max(f0, f1) + x*freq[x] prev =...
delete-and-earn
[Python3] top-down dp
ye15
7
273
delete and earn
740
0.573
Medium
12,114
https://leetcode.com/problems/delete-and-earn/discuss/2438160/Python-Solution-O(n)-similar-to-House-Robber-Faster-than-89.94-solutions
class Solution: def deleteAndEarn(self, nums: List[int]) -> int: # createed a dic to store value of a number i.e the dic[n] = n*(number of times it occurs) dic = defaultdict(int) for n in nums: dic[n] += n # made a list of unique num in nums, OR -> made a list so we can use it as House ...
delete-and-earn
Python Solution O(n) similar to House Robber Faster than 89.94% solutions
notxkaran
4
393
delete and earn
740
0.573
Medium
12,115
https://leetcode.com/problems/delete-and-earn/discuss/2438160/Python-Solution-O(n)-similar-to-House-Robber-Faster-than-89.94-solutions
class Solution: def deleteAndEarn(self, nums: List[int]) -> int: # c dic = defaultdict(int) for n in nums: dic[n] += n newList = list(set(nums)) newList.sort() point = [] N = len(newList) for i in range(1,N): if n...
delete-and-earn
Python Solution O(n) similar to House Robber Faster than 89.94% solutions
notxkaran
4
393
delete and earn
740
0.573
Medium
12,116
https://leetcode.com/problems/delete-and-earn/discuss/1820848/Python-3-or-DP-Greedy-O(NLogN)-House-Robber-or-Explanation
class Solution: def deleteAndEarn(self, nums: List[int]) -> int: c = collections.Counter(nums) keys = sorted(c.keys()) prev = 0 ans = cur = c[keys[0]] * keys[0] for i in range(1, len(keys)): if keys[i] == keys[i-1] + 1: prev, cur = cur, max(cur, pr...
delete-and-earn
Python 3 | DP, Greedy, O(NLogN), House Robber | Explanation
idontknoooo
4
522
delete and earn
740
0.573
Medium
12,117
https://leetcode.com/problems/delete-and-earn/discuss/1666434/Delete-and-Earn-greater-House-Robber-variation-DP-bottom-up
class Solution: def deleteAndEarn(self, nums: List[int]) -> int: gold_houses = [0] * max(nums) for num in nums: gold_houses[num-1] += num # below is the House Robber question, with gold_houses as the input # recurrence relation: max_points(i) = max(nums[i] + max_points(i-...
delete-and-earn
Delete and Earn --> House Robber variation; DP bottom-up
NinjaBlack
3
155
delete and earn
740
0.573
Medium
12,118
https://leetcode.com/problems/delete-and-earn/discuss/1666434/Delete-and-Earn-greater-House-Robber-variation-DP-bottom-up
class Solution: def deleteAndEarn(self, nums: List[int]) -> int: gold = [0] * (max(nums)+1) for num in nums: gold[num] += num g0, g1 = 0, 0 for i in range(1, len(gold)): g1, g0 = max(gold[i] + g0, g1), g1 return g1
delete-and-earn
Delete and Earn --> House Robber variation; DP bottom-up
NinjaBlack
3
155
delete and earn
740
0.573
Medium
12,119
https://leetcode.com/problems/delete-and-earn/discuss/1541098/Python-Easy-House-Robber-variation
class Solution: def deleteAndEarn(self, nums: List[int]) -> int: d = [0]*(max(nums) + 1) for i in nums: d[i] += i p2, p1 = 0, 0 for i in range(len(d)): p1, p2 = max(d[i]+p2, p1), p1 return max(p1...
delete-and-earn
[Python] Easy , House Robber variation
lokeshsenthilkumar
3
273
delete and earn
740
0.573
Medium
12,120
https://leetcode.com/problems/delete-and-earn/discuss/1189565/Well-Explained-(with-example)-oror-Python-oror-96-faster-oror-DP-oror-O(nlogn)
class Solution: def deleteAndEarn(self, nums: List[int]) -> int: m=max(nums) dic=Counter(nums) dp=[0]*(m+1) for k in dic: dp[k]=k*dic[k] for i in range(2,m+1): dp[i]=max(dp[i-1], dp[i-2]+ dp[i]) return dp[-1]
delete-and-earn
Well-Explained (with example) || Python || 96% faster || DP || O(nlogn)
abhi9Rai
3
263
delete and earn
740
0.573
Medium
12,121
https://leetcode.com/problems/delete-and-earn/discuss/2394588/python-oror-dp-oror-top-down-oror-recursion-oror-memoization
class Solution: def deleteAndEarn(self, nums: List[int]) -> int: count = Counter(nums) m = max(nums) memo = {} def choose(num): if num > m: return 0 if num not in count: count[num] = 0 if num in memo: ...
delete-and-earn
python || dp || top down || recursion || memoization
Yared_betsega
2
134
delete and earn
740
0.573
Medium
12,122
https://leetcode.com/problems/delete-and-earn/discuss/1475424/PyPy3-Solution-using-House-Robber-Part-1-Logic-w-comments
class Solution: def deleteAndEarn(self, nums: List[int]) -> int: # Frequency of numbers freq = [0] * (max(nums)+1) for num in nums: freq[num] += num # House Robber Problem from here on # apply house robber logic on freq array # In...
delete-and-earn
[Py/Py3] Solution using House Robber Part 1 Logic w/ comments
ssshukla26
2
219
delete and earn
740
0.573
Medium
12,123
https://leetcode.com/problems/delete-and-earn/discuss/1323959/better-than-99.95
class Solution: def deleteAndEarn(self, nums: List[int]) -> int: dict=Counter(nums) m=max(nums) dp=[0]*(m+1) for i in range(1,m+1): if i in dict: if i==1: dp[i]=dict[i]*i else: dp[i]=max(dp[i-1],dp[i-...
delete-and-earn
better than 99.95%
pramodh18
2
204
delete and earn
740
0.573
Medium
12,124
https://leetcode.com/problems/delete-and-earn/discuss/2480368/Python-DP-Solution-with-DP
class Solution: def deleteAndEarn(self, nums: List[int]) -> int: points = defaultdict(lambda:0) max_number = max(nums) points = Counter(nums) max_points = [0] * (max_number + 2) for num in range(len(max_points)): max_points[num] = max(max_points[num - 1],...
delete-and-earn
Python DP Solution with DP
Xiang-Pan
1
114
delete and earn
740
0.573
Medium
12,125
https://leetcode.com/problems/delete-and-earn/discuss/2104026/Python3-or-DP-or-Easy-Understand
class Solution: def deleteAndEarn(self, nums: List[int]) -> int: counter = Counter(nums) max_n = max(nums) dp = [0] * (max_n + 1) dp[1] = counter[1] for i in range(2, max_n + 1): dp[i] = max(dp[i-1], dp[i-2] + counter[i] * i) return dp[-1]
delete-and-earn
Python3 | DP | Easy-Understand
Yilin_Bai
1
92
delete and earn
740
0.573
Medium
12,126
https://leetcode.com/problems/delete-and-earn/discuss/1822613/Simple-Python3-Solution
class Solution: def deleteAndEarn(self, nums: List[int]) -> int: maps = Counter(nums) nums = sorted(list(set(nums))) prev = prevprev = 0 for i,num in enumerate(nums): currPoint = num*maps[num] tmp=prev if i>0 and num==nums[i-1]+1: ...
delete-and-earn
Simple Python3 Solution
user6774u
1
67
delete and earn
740
0.573
Medium
12,127
https://leetcode.com/problems/delete-and-earn/discuss/1821143/Super-Easy-Super-Optimized
class Solution: def deleteAndEarn(self, nums: List[int]) -> int: dict1=dict() dp=[0]*(max(nums)+1) for i in nums: dict1[i]=dict1.get(i,0)+i for i in range(1,max(nums)+1): dp[i]=max(dp[i-1],dict1.get(i,0)+dp[i-2]) return dp[-1]
delete-and-earn
Super Easy, Super Optimized
vedank98
1
77
delete and earn
740
0.573
Medium
12,128
https://leetcode.com/problems/delete-and-earn/discuss/1820399/DP-oror-House-Robber-Variation
class Solution: def deleteAndEarn(self, nums: List[int]) -> int: points = [0] * (max(nums)+1) for num in nums: points[num] += num previous = points[0] current = points[1] for idx in range(2,len(points)): current, previous = max(current, previous + poin...
delete-and-earn
DP || House Robber Variation
beginne__r
1
34
delete and earn
740
0.573
Medium
12,129
https://leetcode.com/problems/delete-and-earn/discuss/1764708/Java-Python3-Simple-DP-Solution-(Top-Down-and-Bottom-Up)
class Solution: def deleteAndEarn(self, nums: List[int]) -> int: nums_counter = Counter(nums) prev = -1 avoid = using = 0 for i in sorted(nums_counter): if i - 1 != prev: avoid, using = max(avoid, using), i * \ nums_counter[i] + max(avo...
delete-and-earn
✅ [Java / Python3] Simple DP Solution (Top-Down & Bottom-Up)
JawadNoor
1
153
delete and earn
740
0.573
Medium
12,130
https://leetcode.com/problems/delete-and-earn/discuss/1764708/Java-Python3-Simple-DP-Solution-(Top-Down-and-Bottom-Up)
class Solution: def deleteAndEarn(self, nums: List[int]) -> int: memo = {} counter = Counter(nums) sorted_nums = sorted(counter) def dp(i: int) -> int: if i == 0: return sorted_nums[0]*counter[sorted_nums[0]] if i == 1: if sort...
delete-and-earn
✅ [Java / Python3] Simple DP Solution (Top-Down & Bottom-Up)
JawadNoor
1
153
delete and earn
740
0.573
Medium
12,131
https://leetcode.com/problems/delete-and-earn/discuss/1716577/Python-4-Approaches-or-Brute-Force-to-Optimal-or-Time-and-Space-Complexity
class Solution: def deleteAndEarn(self, nums: List[int]) -> int: def helper(ind,nums): if ind==0: return nums[0] if ind<0: return 0 #TAKE take=nums[ind]+helper(ind-2,nums) #NOT TAKE not...
delete-and-earn
Python 4 Approaches | Brute Force to Optimal | Time and Space Complexity
aryanagrawal2310
1
283
delete and earn
740
0.573
Medium
12,132
https://leetcode.com/problems/delete-and-earn/discuss/1716577/Python-4-Approaches-or-Brute-Force-to-Optimal-or-Time-and-Space-Complexity
class Solution: def deleteAndEarn(self, nums: List[int]) -> int: def helper(ind,nums,dp): if ind==0: return nums[0] if ind<0: return 0 if dp[ind]!=-1: return dp[ind] #TAKE take=nums[...
delete-and-earn
Python 4 Approaches | Brute Force to Optimal | Time and Space Complexity
aryanagrawal2310
1
283
delete and earn
740
0.573
Medium
12,133
https://leetcode.com/problems/delete-and-earn/discuss/1716577/Python-4-Approaches-or-Brute-Force-to-Optimal-or-Time-and-Space-Complexity
class Solution: def deleteAndEarn(self, nums: List[int]) -> int: def helper(arr): n=len(arr) dp=[-1]*(n) dp[0]=arr[0] for i in range(1,n): first=dp[i-1] second=arr[i] if i>1: second=arr[i]+dp[...
delete-and-earn
Python 4 Approaches | Brute Force to Optimal | Time and Space Complexity
aryanagrawal2310
1
283
delete and earn
740
0.573
Medium
12,134
https://leetcode.com/problems/delete-and-earn/discuss/1716577/Python-4-Approaches-or-Brute-Force-to-Optimal-or-Time-and-Space-Complexity
class Solution: def deleteAndEarn(self, nums: List[int]) -> int: def helper(arr): n=len(arr) prev2=0 prev=arr[0] for i in range(1,n): ans=max(prev,prev2+arr[i]) prev2=prev prev=ans r...
delete-and-earn
Python 4 Approaches | Brute Force to Optimal | Time and Space Complexity
aryanagrawal2310
1
283
delete and earn
740
0.573
Medium
12,135
https://leetcode.com/problems/delete-and-earn/discuss/1628981/Python3-oror-Dynamic-Programming-oror-O(nlogn)-time-complexity
class Solution: def deleteAndEarn(self, nums: List[int]) -> int: if len(nums) == 1: return nums[0] occurences = dict() for i in nums: if i in occurences: occurences[i] += 1 else: occurences[i] = 1 #remove repeating e...
delete-and-earn
Python3 || Dynamic Programming || O(nlogn) time complexity
s_m_d_29
1
239
delete and earn
740
0.573
Medium
12,136
https://leetcode.com/problems/delete-and-earn/discuss/1562760/Python3-solution-97.44-faster
class Solution: def deleteAndEarn(self, nums: List[int]) -> int: maximum: int = max(nums) count: List[int] = [0] * (maximum + 1) for num in nums: count[num] += num previous: int = 0 current: int = 0 for i in range(0, len(count)):...
delete-and-earn
Python3 solution 97.44% faster
sirenescx
1
308
delete and earn
740
0.573
Medium
12,137
https://leetcode.com/problems/delete-and-earn/discuss/1512397/Any-optimisation-suggestions
class Solution: def deleteAndEarn(self, nums: List[int]) -> int: if len(set(nums)) == 1: return sum(nums) count = dict(collections.Counter(sorted(nums))) num, freq = [], [] for i, j in count.items(): num.append(i) freq.append(j) dp = [0]*len(num) d...
delete-and-earn
Any optimisation suggestions ??
siddp6
1
422
delete and earn
740
0.573
Medium
12,138
https://leetcode.com/problems/delete-and-earn/discuss/1467110/python-O(n)-time-O(n)-space-solution-no-sorting
class Solution: def deleteAndEarn(self, nums: List[int]) -> int: count = defaultdict(int) for num in nums: count[num] += 1 dp = [0 for _ in range(max(nums) + 1)] for k in count: dp[k] = k*count[k] def rob(nums: List[int]) -> int: ...
delete-and-earn
python O(n) time / O(n) space solution, no sorting
byuns9334
1
99
delete and earn
740
0.573
Medium
12,139
https://leetcode.com/problems/delete-and-earn/discuss/2822124/Python-Like-House-Robber-or-Track-Max-By-Adding-Last-OR-Max-of-Skipping-Last-or-Curr
class Solution: def deleteAndEarn(self, nums: List[int]) -> int: # We get a hash map of counts and sort the values counts = {} for num in nums: counts[num] = counts.get(num, 0) + 1 points = sorted(counts.keys()) ''' Rest is similar to House Robber...
delete-and-earn
[Python] Like House Robber | Track Max By Adding Last OR Max of Skipping Last or Curr
graceiscoding
0
2
delete and earn
740
0.573
Medium
12,140
https://leetcode.com/problems/delete-and-earn/discuss/2711752/Python-DP-solution
class Solution: def deleteAndEarn(self, arr: List[int]) -> int: arr = sorted(arr)[::-1] uniq = [arr[0]] for i in range(1, len(arr)): if arr[i] != arr[i - 1]: uniq.append(arr[i]) hm = dict(Counter(arr)) d = [-1] * len(uniq) def dp(i): ...
delete-and-earn
Python DP solution
JSTM2022
0
5
delete and earn
740
0.573
Medium
12,141
https://leetcode.com/problems/delete-and-earn/discuss/2702659/Easy-to-understand-Python-DP-solution-based-on-House-Robber
class Solution: def deleteAndEarn(self, nums: List[int]) -> int: count = collections.Counter(nums) dp = [0]*(max(nums)+1) #max len is 10001 #base cases dp[0] = 0*count[0] dp[1] = 1*count[1] for i in range(2, len(dp)): #recurrence relation dp[...
delete-and-earn
Easy to understand Python DP solution based on House Robber
sc1233
0
9
delete and earn
740
0.573
Medium
12,142
https://leetcode.com/problems/delete-and-earn/discuss/2686768/python-working-solution-or-dp-or-memoization
class Solution: def deleteAndEarn(self, nums: List[int]) -> int: d= {} for i in nums: d[i] = 1 + d.get(i,0) l = list(d.keys()) l.sort() dp = {} def dfs(i,s): if i>=len(l): return 0 if l[i] in s: retur...
delete-and-earn
python working solution | dp | memoization
Sayyad-Abdul-Latif
0
19
delete and earn
740
0.573
Medium
12,143
https://leetcode.com/problems/delete-and-earn/discuss/2681690/Python-oror-Memoization-Solution-oror-93.77-Faster
class Solution: def deleteAndEarn(self, nums: List[int]) -> int: n = len(nums) m = max(nums) # arr is used to store count of each element in nums with respect to index arr = [0 for i in range(m+1)] dp = [-1 for i in range(m+1)] for i in range(len(nums)): ...
delete-and-earn
Python || Memoization Solution || 93.77% Faster
shreya_pattewar
0
9
delete and earn
740
0.573
Medium
12,144
https://leetcode.com/problems/delete-and-earn/discuss/2665074/Python-or-O(n)-solution-with-comments
class Solution: def deleteAndEarn(self, nums: List[int]) -> int: # First step is simple count sort stat = [0]*(10**4 + 1) for num in nums: stat[num] += 1 # For second step we use dynamic programming approach # We can either choose previous value or pre-previous va...
delete-and-earn
Python | O(n) solution with comments
LordVader1
0
22
delete and earn
740
0.573
Medium
12,145
https://leetcode.com/problems/delete-and-earn/discuss/2458019/python3-easy-solution-(change-to-leetcode-198)
class Solution: def deleteAndEarn(self, nums: List[int]) -> int: house=[] temp=0 nums.sort() for i in range(len(nums)): #create house list, nums[i] as index of house if i!=len(nums)-1: if nums[i]==nums[i+1]: temp+=nums[i] else: temp+=nums[i] house.append(temp) temp=0 for j ...
delete-and-earn
python3 easy solution (change to leetcode 198)
kyoko0810
0
94
delete and earn
740
0.573
Medium
12,146
https://leetcode.com/problems/delete-and-earn/discuss/2413530/Python3-or-Can't-Figure-Out-Bug-in-My-Bottom-UP-DP-Code
class Solution: def deleteAndEarn(self, nums: List[int]) -> int: #if you think about this problem recursively, basically this problem exhibits both #optimal substructure property as well as overlapping subproblems! #For example, let's take first test case! #nums = ...
delete-and-earn
Python3 | Can't Figure Out Bug in My Bottom-UP DP Code
JOON1234
0
15
delete and earn
740
0.573
Medium
12,147
https://leetcode.com/problems/delete-and-earn/discuss/2413109/Python3-or-Question-on-Top-Down-Approach%3A-How-to-Memo-Array-Instances!
class Solution: def deleteAndEarn(self, nums: List[int]) -> int: #if you think about this problem recursively, basically this problem exhibits both #optimal substructure property as well as overlapping subproblems! #For example, let's take first test case! #nums = ...
delete-and-earn
Python3 | Question on Top-Down Approach: How to Memo Array Instances!
JOON1234
0
31
delete and earn
740
0.573
Medium
12,148
https://leetcode.com/problems/delete-and-earn/discuss/2305019/Python-DP-easy
class Solution: def deleteAndEarn(self, nums: List[int]) -> int: count = collections.Counter(nums) nums = sorted(set(nums)) n = len(count) dp = [0]*n dp[0] = nums[0]*count[nums[0]] print(nums) for i in range(1,n): if nums[i] -1 == nums[i-1]: ...
delete-and-earn
Python DP easy
Abhi_009
0
79
delete and earn
740
0.573
Medium
12,149
https://leetcode.com/problems/delete-and-earn/discuss/1941502/Python3-oror-Bottom-Up-Solution
class Solution: def deleteAndEarn(self, nums: List[int]) -> int: max_val = max(nums) dp = [0] * ( max_val + 1 ) for i in nums : dp[i] += i for i in range(2,len(dp)): dp[i] = max(dp[i-1],dp[i] + dp[i-2]) return dp[max_val]
delete-and-earn
Python3 || Bottom Up Solution
subhamsubhasispatra
0
151
delete and earn
740
0.573
Medium
12,150
https://leetcode.com/problems/delete-and-earn/discuss/1821905/Python-easy-and-clean-solution-or-Explained
class Solution: def deleteAndEarn(self, nums: List[int]) -> int: dic, prev = defaultdict(int), -1 prevMax = leftMax = 0 # prevMax: maximum till the previous, leftMax: maximum just before the prevous for i in nums: dic[i] += 1 # counting the frequency ...
delete-and-earn
✅ Python easy and clean solution | Explained
dhananjay79
0
70
delete and earn
740
0.573
Medium
12,151
https://leetcode.com/problems/delete-and-earn/discuss/1821397/Python3-Dynamic-Programming-Bottom-Up-Approach
class Solution: def deleteAndEarn(self, nums: List[int]) -> int: d=Counter(nums) N = len(d) temp = [[None]*2 for _ in range(N+1)] # Base case temp[0][0]=0 # j=0 --> Exclude temp[0][1]=0 # j=1 --> Include # Choice diagram # Traverse dict instead of array ...
delete-and-earn
Python3 Dynamic Programming Bottom Up Approach
KiranRaghavendra
0
39
delete and earn
740
0.573
Medium
12,152
https://leetcode.com/problems/delete-and-earn/discuss/1820561/Python-Simple-Python-Solution-Using-Dynamic-Programming-and-Dictionary
class Solution: def deleteAndEarn(self, nums: List[int]) -> int: count_frequency = defaultdict(int) max_number = max(nums) for num in nums: count_frequency[num] = count_frequency[num] + num dp = [0]*(max_number + 1) dp[1] = count_frequency[1] for j in range(2,len(dp)): dp[j] = max(dp[j-1], dp...
delete-and-earn
[ Python ] ✔✔ Simple Python Solution Using Dynamic Programming and Dictionary 🔥✌
ASHOK_KUMAR_MEGHVANSHI
0
114
delete and earn
740
0.573
Medium
12,153
https://leetcode.com/problems/delete-and-earn/discuss/1792477/Easily-understanding-DP-solution-in-python-3
class Solution: def deleteAndEarn(self, nums: List[int]) -> int: cnt = [0] * (max(nums) + 1) for num in nums: cnt[num] += 1 # record the occurrence of each num dp = [[0, 0] for i in range(max(nums)+1)] res = 0 #Base case: dp[0][0], dp[0][1] = 0 ...
delete-and-earn
Easily understanding DP solution in python 3
WhATEVERnameIS
0
115
delete and earn
740
0.573
Medium
12,154
https://leetcode.com/problems/delete-and-earn/discuss/1771319/Python-easy-to-read-and-understand-or-DP
class Solution: def solve(self, nums): n = len(nums) t = [[0 for _ in range(n)] for _ in range(2)] t[0][0] = nums[0] for i in range(1, n): t[0][i] = t[1][i-1] + nums[i] t[1][i] = max(t[0][i-1], t[1][i-1]) return max(t[0][n-1], t[1][n-1]) def d...
delete-and-earn
Python easy to read and understand | DP
sanial2001
0
131
delete and earn
740
0.573
Medium
12,155
https://leetcode.com/problems/delete-and-earn/discuss/1748290/Python-O(nlogn)-solution
class Solution: def deleteAndEarn(self, nums: List[int]) -> int: sortedNums = sorted(nums) aggr = {} result = {} prev = (0,0) prev2 = (0,0) for e in sortedNums: aggr[e] = aggr.get(e, 0) + e for key,val in aggr.items():...
delete-and-earn
Python O(nlogn) solution
ganyue246
0
116
delete and earn
740
0.573
Medium
12,156
https://leetcode.com/problems/delete-and-earn/discuss/1592417/Python3-Iterative-dp-solution
class Solution: def deleteAndEarn(self, nums: List[int]) -> int: count = collections.defaultdict(int) _min, _max = float('inf'), float('-inf') for num in nums: count[num] += 1 _min = min(_min, num) _max = max(_max, num) cur = prev...
delete-and-earn
[Python3] Iterative dp solution
maosipov11
0
115
delete and earn
740
0.573
Medium
12,157
https://leetcode.com/problems/delete-and-earn/discuss/1460541/Easy-python-solutionoror-dict-and-dporor-Python3
class Solution: def deleteAndEarn(self, nums: List[int]) -> int: nums = sorted(nums,reverse=True) dct = {} for i in nums: if i in dct: dct[i]+=i else: dct[i] = i lst = list(dct.keys()) dp = [0]*len(lst) dp[0] = d...
delete-and-earn
Easy python solution|| dict & dp|| Python3
ce17b127
0
84
delete and earn
740
0.573
Medium
12,158
https://leetcode.com/problems/delete-and-earn/discuss/1422008/Python-Clean
class Solution: def deleteAndEarn(self, nums: List[int]) -> int: if len(nums) == 1: return nums[0] elif len(nums) == 2: return max(nums) if nums[0]+1 == nums[1] else sum(nums) count = collections.Counter(nums) elements = sorted(count.keys()) dp = [0...
delete-and-earn
[Python] Clean
soma28
0
91
delete and earn
740
0.573
Medium
12,159
https://leetcode.com/problems/delete-and-earn/discuss/367890/Solution-in-Python-3-(beats-~100)-(four-lines)
class Solution: def deleteAndEarn(self, n: List[int]) -> int: if not n: return 0 a, b, C = 0, n.count(1), collections.Counter(n) for i in range(2,max(n)+1): b, a = max(C[i]*i + a, b), b return b - Junaid Mansuri (LeetCode ID)@hotmail.com
delete-and-earn
Solution in Python 3 (beats ~100%) (four lines)
junaidmansuri
0
414
delete and earn
740
0.573
Medium
12,160
https://leetcode.com/problems/cherry-pickup/discuss/699169/Python3-dp
class Solution: def cherryPickup(self, grid: List[List[int]]) -> int: n = len(grid) @lru_cache(None) def fn(t, i, ii): """Return maximum cherries collected at kth step when two robots are at ith and iith row""" j, jj = t - i, t - ii #columns if n...
cherry-pickup
[Python3] dp
ye15
2
241
cherry pickup
741
0.363
Hard
12,161
https://leetcode.com/problems/cherry-pickup/discuss/2798148/(Pythong)-Simplify-the-input-%3A-dp(x1-y1-x2)-with-%40cache
class Solution: def cherryPickup(self, grid: List[List[int]]) -> int: @cache def dp(x1, y1, x2): y2 = x1+y1-x2 if not(0 <= x1 < len(grid) and 0 <= y1 < len(grid[0]) and 0 <= x2 < len(grid) and 0 <= y2 < len(grid[0])): return -float('inf') if gri...
cherry-pickup
(Pythong) Simplify the input : dp(x1, y1, x2) with @cache
SooCho_i
0
7
cherry pickup
741
0.363
Hard
12,162
https://leetcode.com/problems/cherry-pickup/discuss/1676191/Python3-or-Top-down-solution-or-DP
class Solution: def cherryPickup(self, grid: List[List[int]]) -> int: rows = len(grid) cols = len(grid[0]) #Go down or go right directions = [(1, 0), (0, 1)] #Enables cache for our dp @lru_cache(None) def dp(row_1, row_2, col_1, col_...
cherry-pickup
Python3 | Top-down solution | DP
Jujusp
0
135
cherry pickup
741
0.363
Hard
12,163
https://leetcode.com/problems/network-delay-time/discuss/2036405/Python-Simple-Dijkstra-Beats-~90
class Solution: def networkDelayTime(self, times: List[List[int]], n: int, k: int) -> int: adj_list = defaultdict(list) for x,y,w in times: adj_list[x].append((w, y)) visited=set() heap = [(0, k)] while heap: travel_time, node...
network-delay-time
Python Simple Dijkstra Beats ~90%
constantine786
39
2,200
network delay time
743
0.515
Medium
12,164
https://leetcode.com/problems/network-delay-time/discuss/1614297/Python-3-or-Different-4-Methods-or-No-Explanation
class Solution: def networkDelayTime(self, times: List[List[int]], n: int, k: int) -> int: dp = [sys.maxsize] * n dp[k-1] = 0 graph = collections.defaultdict(list) for s, e, w in times: graph[s].append((e, w)) visited = set() heap = [(0, k)] ...
network-delay-time
Python 3 | Different 4 Methods | No Explanation
idontknoooo
6
512
network delay time
743
0.515
Medium
12,165
https://leetcode.com/problems/network-delay-time/discuss/1614297/Python-3-or-Different-4-Methods-or-No-Explanation
class Solution: def networkDelayTime(self, times: List[List[int]], n: int, k: int) -> int: dp = [[sys.maxsize] * n for _ in range(n)] graph = collections.defaultdict(list) for s, e, w in times: graph[s].append((e, w)) dp[s-1][e-1] = w for ...
network-delay-time
Python 3 | Different 4 Methods | No Explanation
idontknoooo
6
512
network delay time
743
0.515
Medium
12,166
https://leetcode.com/problems/network-delay-time/discuss/1614297/Python-3-or-Different-4-Methods-or-No-Explanation
class Solution: def networkDelayTime(self, times: List[List[int]], n: int, k: int) -> int: dp = [sys.maxsize] * n dp[k-1] = 0 for _ in range(n-1): for s, e, w in times: if dp[e-1] > dp[s-1] + w: dp[e-1] = dp[s-1] + w ...
network-delay-time
Python 3 | Different 4 Methods | No Explanation
idontknoooo
6
512
network delay time
743
0.515
Medium
12,167
https://leetcode.com/problems/network-delay-time/discuss/1614297/Python-3-or-Different-4-Methods-or-No-Explanation
class Solution: def networkDelayTime(self, times: List[List[int]], n: int, k: int) -> int: dp = [sys.maxsize] * n dp[k-1] = 0 graph = collections.defaultdict(list) for s, e, w in times: graph[s].append((e, w)) dq = collections.deque([k]) visit...
network-delay-time
Python 3 | Different 4 Methods | No Explanation
idontknoooo
6
512
network delay time
743
0.515
Medium
12,168
https://leetcode.com/problems/network-delay-time/discuss/2004199/Python3%3A-Dijkstra's-Algorithm-(easy-to-understand)
class Solution: def networkDelayTime(self, times: List[List[int]], n: int, k: int) -> int: # This problem requires dijkstra's algorithm, where we find the shortest # distance between a source vertex to all the other vertices. # If any of the other vertices could not be reached, retu...
network-delay-time
Python3: Dijkstra's Algorithm (easy to understand)
anjerraa
2
137
network delay time
743
0.515
Medium
12,169
https://leetcode.com/problems/network-delay-time/discuss/2767908/Python-Dijkstra-Algorithm-(-Simple-Soln-with-comments)
class Solution: def networkDelayTime(self, times: List[List[int]], n: int, k: int) -> int: #create adjacency list adjList = {i:[] for i in range(1, n+1)} #node: [neighbour, weight] for src, dest, weight in times: adjList[src].append([dest, weight]) #create minHeap ...
network-delay-time
Python - Dijkstra Algorithm ( Simple Soln with comments)
logeshsrinivasans
1
123
network delay time
743
0.515
Medium
12,170
https://leetcode.com/problems/network-delay-time/discuss/2176431/Python3-3-solutions-DFS-BFS-Dijkstra
class Solution: def networkDelayTime(self, times: List[List[int]], n: int, k: int) -> int: ## RC ## ## APPROACH 1 : DFS + Visited ## ## Not optimized method, barely passed, always use BFS for shortest path algorithms ## graph = collections.defaultdict(list) visited = {} ...
network-delay-time
[Python3] 3 solutions DFS, BFS, Dijkstra
101leetcode
1
71
network delay time
743
0.515
Medium
12,171
https://leetcode.com/problems/network-delay-time/discuss/1590202/Python-Solution-Beats-96.99-Of-python-Solution-(-DIJKSTRA-Algo)
class Solution: def networkDelayTime(self, times: List[List[int]], n: int, k: int) -> int: distance=[float('inf')]*n distance[k-1]=0 visited=[False]*n heap=[(0,k-1)] adj=defaultdict(list) for u,v,w in times:adj[u-1].append((v-1,w)) while heap: mi,u...
network-delay-time
Python Solution Beats 96.99% Of python Solution ( DIJKSTRA Algo)
reaper_27
1
77
network delay time
743
0.515
Medium
12,172
https://leetcode.com/problems/network-delay-time/discuss/1214552/Python3-Solution-using-Djikstra-and-heap
class Solution: def networkDelayTime(self, times: List[List[int]], n: int, k: int) -> int: # implemented using dijkstra's shortest path graph = {} # create graph for u, v, w in times: if u not in graph.keys(): graph[u] = [(v, w)] else: ...
network-delay-time
Python3 Solution using Djikstra and heap
JensonC0012
1
85
network delay time
743
0.515
Medium
12,173
https://leetcode.com/problems/network-delay-time/discuss/1137280/Python-or-Dijkstra's-Algorithm-or-Beats-99
class Solution: def networkDelayTime(self, times: List[List[int]], n: int, k: int) -> int: graph = {i:dict() for i in range(1, n+1)} for u,v,w in times: graph[u][v] = w dist = [float('inf') for i in range(n+1)] dist[0], dist[k] = 0, 0 visited = [False...
network-delay-time
Python | Dijkstra's Algorithm | Beats 99%
anuraggupta29
1
118
network delay time
743
0.515
Medium
12,174
https://leetcode.com/problems/network-delay-time/discuss/491792/Python3-Dijkstra's-algo
class Solution: def networkDelayTime(self, times: List[List[int]], N: int, K: int) -> int: graph = dict() #digraph in contingency list for u, v, w in times: graph.setdefault(u, []).append((w, v)) dist = [float("inf")]*N stack = [(0, K)] #depth-first-search ...
network-delay-time
[Python3] Dijkstra's algo
ye15
1
181
network delay time
743
0.515
Medium
12,175
https://leetcode.com/problems/network-delay-time/discuss/491792/Python3-Dijkstra's-algo
class Solution: def networkDelayTime(self, times: List[List[int]], N: int, K: int) -> int: """bellman-ford algorithm""" dist = [float("inf")]*N dist[K-1] = 0 for _ in range(N-1): for u, v, w in times: dist[v-1] = min(dist[v-1], dist[u-1]+...
network-delay-time
[Python3] Dijkstra's algo
ye15
1
181
network delay time
743
0.515
Medium
12,176
https://leetcode.com/problems/network-delay-time/discuss/491792/Python3-Dijkstra's-algo
class Solution: def networkDelayTime(self, times: List[List[int]], N: int, K: int) -> int: """floyd-warshall algorithm""" dist = [[float("inf")]*N for _ in range(N)] for i in range(N): dist[i][i] = 0 for u, v, w in times: dist[u-1][v-1] = w for k in rang...
network-delay-time
[Python3] Dijkstra's algo
ye15
1
181
network delay time
743
0.515
Medium
12,177
https://leetcode.com/problems/network-delay-time/discuss/2786606/Python-Dijkstra
class Solution: # main challenge is that it seems like the value of the queue needs to change. # solved by ignoring any queue item that has been updated. def networkDelayTime(self, times: List[List[int]], n: int, k: int): # build adjacency list graph = [{} for _ in range(n)] for edge...
network-delay-time
[Python] Dijkstra
i-hate-covid
0
4
network delay time
743
0.515
Medium
12,178
https://leetcode.com/problems/network-delay-time/discuss/2043011/Python-DjikstraBFS-or-O(E-log-V)
class Solution: def networkDelayTime(self, times: List[List[int]], n: int, k: int) -> int: edges = collections.defaultdict(list) for u, v, w in times: edges[u].append((v, w)) minHeap = [(0, k)] visited = set() t = 0 while minHeap: w1, ...
network-delay-time
[Python] Djikstra/BFS | O(E log V)
tejeshreddy111
0
24
network delay time
743
0.515
Medium
12,179
https://leetcode.com/problems/network-delay-time/discuss/2039595/Python-Dijkstra-algorithm
class Solution: def networkDelayTime(self, times: List[List[int]], n: int, k: int) -> int: adj = defaultdict(list) for u, v, w in times: adj[u].append((v, w)) q = [(0, k)] seen = set() while q: if q[0][1] in seen: h...
network-delay-time
Python, Dijkstra algorithm
blue_sky5
0
22
network delay time
743
0.515
Medium
12,180
https://leetcode.com/problems/network-delay-time/discuss/2038547/Python3-Solution-with-using-dfsbfs
class Solution: def networkDelayTime(self, times: List[List[int]], n: int, k: int) -> int: g = collections.defaultdict(list) # build graph for src, dst, weight in times: g[src].append((dst, weight)) # k - node, v - weight (src to k) node2ws = {i:...
network-delay-time
[Python3] Solution with using dfs/bfs
maosipov11
0
25
network delay time
743
0.515
Medium
12,181
https://leetcode.com/problems/network-delay-time/discuss/2038547/Python3-Solution-with-using-dfsbfs
class Solution: def networkDelayTime(self, times: List[List[int]], n: int, k: int) -> int: g = collections.defaultdict(list) # build graph for src, dst, weight in times: g[src].append((dst, weight)) # k - node, v - weight (src to k) node2ws = {i:...
network-delay-time
[Python3] Solution with using dfs/bfs
maosipov11
0
25
network delay time
743
0.515
Medium
12,182
https://leetcode.com/problems/network-delay-time/discuss/2036541/python3-shortest-path-algo-%2B-BFS
class Solution: def networkDelayTime(self, times: List[List[int]], n: int, k: int) -> int: # n = num of nodes, k = source group = collections.defaultdict(list) # source: list of [target, time] for u, v, w in times: group[u].append([v, w]) ...
network-delay-time
[python3] shortest path algo + BFS
sshinyy
0
15
network delay time
743
0.515
Medium
12,183
https://leetcode.com/problems/network-delay-time/discuss/2036352/Simple-Python-Solution-oror-Dijkstra
class Solution: def networkDelayTime(self, times: List[List[int]], n: int, k: int) -> int: d=defaultdict(list) for src,dst,t in times: d[src].append((dst,t)) q=[(0,k)] visited=set() max_cost=0 while q: cost,node=heapq.heappop(q) ...
network-delay-time
Simple Python Solution || Dijkstra
a_dityamishra
0
36
network delay time
743
0.515
Medium
12,184
https://leetcode.com/problems/network-delay-time/discuss/2036341/Python-Dijkstra's-Algorithm-oror-O(E.logV)-Time-Complexity-oror-Simple-and-Easy-Solution
class Solution: def networkDelayTime(self, times: List[List[int]], n: int, k: int) -> int: edges = collections.defaultdict(list) for u, v, w in times: edges[u].append((v, w)) t = 0 minHeap = [(0, k)] visit = set() while minHeap: ...
network-delay-time
Python - Dijkstra's Algorithm || O(E.logV) Time Complexity || Simple and Easy Solution
dayaniravi123
0
17
network delay time
743
0.515
Medium
12,185
https://leetcode.com/problems/network-delay-time/discuss/2036240/Python-Solution-min-heap-defaultdict(dict)-dijkstras-algorithm
class Solution(object): def networkDelayTime(self, times, n, k): """ :type times: List[List[int]] :type n: int :type k: int :rtype: int """ dic = defaultdict(dict) for x, y, t in times: dic[x][y] = t heap = [(0, k)] seen = s...
network-delay-time
Python Solution min-heap defaultdict(dict) dijkstras algorithm
Kennyyhhu
0
50
network delay time
743
0.515
Medium
12,186
https://leetcode.com/problems/network-delay-time/discuss/2036231/beat-all
class Solution: def networkDelayTime(self, times: List[List[int]], n: int, k: int) -> int: visited, matrix = [float('inf')] * n, [[float('inf')] * n for _ in range(n)] for u, v, w in times: matrix[u-1][v-1] = w q = [(0, k-1)] visited[k-1] = 0 while q: ...
network-delay-time
beat all
BichengWang
0
24
network delay time
743
0.515
Medium
12,187
https://leetcode.com/problems/network-delay-time/discuss/1847181/Python-Dijkstra
class Solution: def networkDelayTime(self, times: List[List[int]], n: int, k: int) -> int: import heapq graph = defaultdict(dict) for i,j,w in times: graph[i][j] = w dist = [float("inf")]*(n+1) dist[k] = 0 que = [(0,k)] visit...
network-delay-time
[Python] Dijkstra
haydarevren
0
85
network delay time
743
0.515
Medium
12,188
https://leetcode.com/problems/network-delay-time/discuss/1490760/Python-Dijkstra's-with-array-and-self-made-heap
class Solution: def networkDelayTime(self, times: List[List[int]], n: int, k: int) -> int: graph = [[] for _ in range(n)] self.createGraph(times, graph) minDist = [float('inf') for _ in range(n)] minDist[k - 1] = 0 visited = set() while len(visited) != n: currDist, currVertex = self.explore(vis...
network-delay-time
Python Dijkstra's with array and self-made heap
SleeplessChallenger
0
142
network delay time
743
0.515
Medium
12,189
https://leetcode.com/problems/network-delay-time/discuss/1490760/Python-Dijkstra's-with-array-and-self-made-heap
class Solution: def networkDelayTime(self, times: List[List[int]], n: int, k: int) -> int: graph = [[] for _ in range(n)] self.createGraph(graph, times) minDist = [float('inf') for _ in range(n)] minDist[k - 1] = 0 min_heap = MinHeap([(idx, float('inf')) for idx in range(n)]) min_heap.update(k - 1...
network-delay-time
Python Dijkstra's with array and self-made heap
SleeplessChallenger
0
142
network delay time
743
0.515
Medium
12,190
https://leetcode.com/problems/network-delay-time/discuss/1174211/python-4-methods%3A-Dijkstra's-Bellman-Ford-bfs-dfs
class Solution(object): def networkDelayTime(self, times, N, K): graph = collections.defaultdict(list) for u, v, w in times: graph[u].append((w, v)) pq = [(0, K)] dist = {} while pq: d, node = heapq.heappop(pq) if node in dist: continue ...
network-delay-time
python 4 methods: Dijkstra's, Bellman Ford, bfs, dfs
dustlihy
0
185
network delay time
743
0.515
Medium
12,191
https://leetcode.com/problems/network-delay-time/discuss/1174211/python-4-methods%3A-Dijkstra's-Bellman-Ford-bfs-dfs
# class Solution: # def networkDelayTime(self, times: List[List[int]], N: int, K: int) -> int: # INF = float('inf') # dist = [INF] * N # dist[K-1] = 0 # for i in range(N): # for time in times: # 1, time[1] - 1, time[2] # dist[v] = min(dist[...
network-delay-time
python 4 methods: Dijkstra's, Bellman Ford, bfs, dfs
dustlihy
0
185
network delay time
743
0.515
Medium
12,192
https://leetcode.com/problems/network-delay-time/discuss/1174211/python-4-methods%3A-Dijkstra's-Bellman-Ford-bfs-dfs
# class Solution(object): # def networkDelayTime(self, times, N, K): # graph = collections.defaultdict(list) # for u, v, w in times: # graph[u].append((w, v)) # u --> v # INF = float('inf') # dist = {i: INF for i in range(1, N+1)} # queue = collections.de...
network-delay-time
python 4 methods: Dijkstra's, Bellman Ford, bfs, dfs
dustlihy
0
185
network delay time
743
0.515
Medium
12,193
https://leetcode.com/problems/network-delay-time/discuss/1174211/python-4-methods%3A-Dijkstra's-Bellman-Ford-bfs-dfs
# class Solution(object): # def networkDelayTime(self, times, N, K): # graph = collections.defaultdict(list) # for u, v, w in times: # graph[u].append((w, v)) # u --> v # INF = float('inf') # dist = {node: INF for node in range(1, N+1)} # def dfs...
network-delay-time
python 4 methods: Dijkstra's, Bellman Ford, bfs, dfs
dustlihy
0
185
network delay time
743
0.515
Medium
12,194
https://leetcode.com/problems/network-delay-time/discuss/625942/Python-Solution-Djikstra's-Algorithm
class Solution: def extractMin(self,visited,dist): mind=float("inf") mini=0 for i in range(len(visited)): if not visited[i] and dist[i]<mind: mini=i mind=dist[i] return mini ...
network-delay-time
Python Solution- Djikstra's Algorithm
Ayu-99
0
80
network delay time
743
0.515
Medium
12,195
https://leetcode.com/problems/network-delay-time/discuss/1456874/Python3-solution-using-Dijkstra's-Algorithm
class Solution: def networkDelayTime(self, times: List[List[int]], n: int, k: int) -> int: d = {} graph = [[] for _ in range(n)] for i in times: d[(i[0]-1,i[1]-1)] = i[2] graph[i[0]-1].append(i[1]-1) parent = [-1]*n cost = [10**9]*n vis...
network-delay-time
Python3 solution using Dijkstra's Algorithm
EklavyaJoshi
-1
38
network delay time
743
0.515
Medium
12,196
https://leetcode.com/problems/find-smallest-letter-greater-than-target/discuss/1568523/Python-easy-solution-with-detail-explanation-(modified-binary-search)
class Solution(object): def nextGreatestLetter(self, letters, target): """ :type letters: List[str] :type target: str :rtype: str """ # if the number is out of bound if target >= letters[-1] or target < letters[0]: return letters[0] ...
find-smallest-letter-greater-than-target
Python easy solution with detail explanation (modified binary search)
Abeni
71
3,300
find smallest letter greater than target
744
0.448
Easy
12,197
https://leetcode.com/problems/find-smallest-letter-greater-than-target/discuss/2507850/4-lines-of-code-easy-understandable-in-Python
class Solution: def nextGreatestLetter(self, letters: List[str], target: str) -> str: t_ord = ord(target) - ord('a') for i in letters: l_ord = ord(i) - ord('a') if l_ord > t_ord: return i return letters[0] # if no chrater is gte that target then retur...
find-smallest-letter-greater-than-target
4 lines of code easy understandable in Python
ankurbhambri
7
79
find smallest letter greater than target
744
0.448
Easy
12,198
https://leetcode.com/problems/find-smallest-letter-greater-than-target/discuss/2176655/Python3-solution-O(log-n)
class Solution: def nextGreatestLetter(self, letters: List[str], target: str) -> str: low, high = 0, len(letters) - 1 while low < high: mid = (low+high) // 2 if letters[mid] <= target: low = mid + 1 else: high = mid return l...
find-smallest-letter-greater-than-target
📌 Python3 solution O(log n)
Dark_wolf_jss
6
154
find smallest letter greater than target
744
0.448
Easy
12,199