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/maximum-product-of-word-lengths/discuss/1997354/Simple-Python-Solution
class Solution: def maxProduct(self, words: List[str]) -> int: ans=0 for i in range(len(words)): for j in range(i+1, len(words)): flag=False for k in words[i]: if k in words[j]: flag=True ...
maximum-product-of-word-lengths
Simple Python Solution
Siddharth_singh
0
80
maximum product of word lengths
318
0.601
Medium
5,500
https://leetcode.com/problems/maximum-product-of-word-lengths/discuss/1798207/Python-O(26n**2)-time.-O(n)-space-solution-(no-bit-manipulation)
class Solution: def maxProduct(self, words: List[str]) -> int: n = len(words) res = 0 charset = defaultdict(set) for i in range(n): word = words[i] for c in word: charset[c].add(i) for i in range(n-1): for j in range(i+1, n...
maximum-product-of-word-lengths
Python O(26n**2) time,. O(n) space solution (no bit manipulation)
byuns9334
0
113
maximum product of word lengths
318
0.601
Medium
5,501
https://leetcode.com/problems/maximum-product-of-word-lengths/discuss/1696586/Python3-accepted-solution
class Solution: def maxProduct(self, words: List[str]) -> int: words = sorted(words,key=len)[::-1] max_ = 0 for i in range(len(words)-1): for j in range(i+1, len(words)): if(len("".join(set(words[i]).intersection(set(words[j])))) == 0): #print(...
maximum-product-of-word-lengths
Python3 accepted solution
sreeleetcode19
0
85
maximum product of word lengths
318
0.601
Medium
5,502
https://leetcode.com/problems/maximum-product-of-word-lengths/discuss/1561716/Python-solution
class Solution: def maxProduct(self, words: List[str]) -> int: max_val=0 for current_word in range(len(words)-1): for word in words[current_word+1:]: if len(set(words[current_word]).intersection(set(word)))==0: val=len(words[current_word])*len(word) ...
maximum-product-of-word-lengths
Python solution
msrini111
0
85
maximum product of word lengths
318
0.601
Medium
5,503
https://leetcode.com/problems/maximum-product-of-word-lengths/discuss/1236967/Python-Solution
class Solution: def maxProduct(self, words): mapping = {} for word in words: mapping[word] = set(word) max_value = 0 n = len(words) for i in range(n - 1): set1 = mapping[words[i]] for j in range(i + 1, n): set2 = mapping[wor...
maximum-product-of-word-lengths
Python Solution
mariandanaila01
0
157
maximum product of word lengths
318
0.601
Medium
5,504
https://leetcode.com/problems/maximum-product-of-word-lengths/discuss/1235119/Python3-calculate-pairwise-distance-and-check-orthogonal-word-vectors.
class Solution: def vec_dotprod(self, vec1, vec2): x = 0 for a, b in zip(vec1, vec2): x += a*b return x def maxProduct(self, words: List[str]) -> int: word_matrix = [] n = len(words) for w in words: vec = [0 for _ in ran...
maximum-product-of-word-lengths
[Python3] calculate pairwise distance and check orthogonal word vectors.
markxc02
0
44
maximum product of word lengths
318
0.601
Medium
5,505
https://leetcode.com/problems/maximum-product-of-word-lengths/discuss/1233707/Simple-solution-in-Python-using-Set-operations
class Solution: def maxProduct(self, words: List[str]) -> int: n = len(words) hashmap = dict() i = 0 ans = 0 for word in words: hashmap[i] = set(word) i += 1 for i in range(0, n - 1): a = hashmap[i] for j in ran...
maximum-product-of-word-lengths
Simple solution in Python using Set operations
amoghrajesh1999
0
53
maximum product of word lengths
318
0.601
Medium
5,506
https://leetcode.com/problems/bulb-switcher/discuss/535399/PythonJSJavaC%2B%2B-sol-by-perfect-square.-w-Visualization
class Solution: def bulbSwitch(self, n: int) -> int: # Only those bulds with perferct square number index will keep "ON" at last. return int(n**0.5)
bulb-switcher
Python/JS/Java/C++ sol by perfect square. [w/ Visualization]
brianchiang_tw
20
1,200
bulb switcher
319
0.481
Medium
5,507
https://leetcode.com/problems/bulb-switcher/discuss/1220041/Python3-simple-one-liner-solution
class Solution: def bulbSwitch(self, n: int) -> int: return int(n**0.5)
bulb-switcher
Python3 simple one-liner solution
EklavyaJoshi
7
479
bulb switcher
319
0.481
Medium
5,508
https://leetcode.com/problems/bulb-switcher/discuss/788489/Python3-brain-teaser
class Solution: def bulbSwitch(self, n: int) -> int: return int(sqrt(n))
bulb-switcher
[Python3] brain teaser
ye15
2
427
bulb switcher
319
0.481
Medium
5,509
https://leetcode.com/problems/bulb-switcher/discuss/788489/Python3-brain-teaser
class Solution: def bulbSwitch(self, n: int) -> int: return isqrt(n)
bulb-switcher
[Python3] brain teaser
ye15
2
427
bulb switcher
319
0.481
Medium
5,510
https://leetcode.com/problems/bulb-switcher/discuss/2835762/ONE-WORDororMATH-SOLUTION-oror-VERY-EASY-TO-UNDERSTAND-oror-50-MS
class Solution: def bulbSwitch(self, n: int) -> int: return int(n**(1/2))
bulb-switcher
ONE WORD||MATH SOLUTION || VERY EASY TO UNDERSTAND || 50 MS
thezealott
1
13
bulb switcher
319
0.481
Medium
5,511
https://leetcode.com/problems/bulb-switcher/discuss/2823581/Python-Solution-(1-Liner-or-Math-or-Perfect-Square)-%2B-Explication-!
class Solution: def bulbSwitch(self, n: int) -> int: return isqrt(n)
bulb-switcher
Python Solution (1 Liner | Math | Perfect Square) + Explication !
nvshah
0
4
bulb switcher
319
0.481
Medium
5,512
https://leetcode.com/problems/bulb-switcher/discuss/523039/Python3-simple-solution
class Solution: def bulbSwitch(self, n: int) -> int: bulb_on = 0 v = 2 while n >= 1: bulb_on += 1 n -= v + 1 v += 2 return bulb_on
bulb-switcher
Python3 simple solution
tjucoder
0
332
bulb switcher
319
0.481
Medium
5,513
https://leetcode.com/problems/create-maximum-number/discuss/2167532/O(k(m%2Bn)).-Python-Solution-with-monotonically-decreasing-stack.-Commented-for-clarity.
class Solution: def maxNumber(self, nums1: List[int], nums2: List[int], k: int) -> List[int]: def maximum_num_each_list(nums: List[int], k_i: int) -> List[int]: # monotonically decreasing stack s = [] m = len(nums) - k_i for n in nums: while s ...
create-maximum-number
O(k(m+n)). Python Solution with monotonically decreasing stack. Commented for clarity.
saqibmubarak
2
443
create maximum number
321
0.288
Hard
5,514
https://leetcode.com/problems/create-maximum-number/discuss/1467230/Python3-greedy
class Solution: def maxNumber(self, nums1: List[int], nums2: List[int], k: int) -> List[int]: def fn(arr, k): """Return largest sub-sequence of arr of size k.""" ans = [] for i, x in enumerate(arr): while ans and ans[-1] < x and len(ans) + len(ar...
create-maximum-number
[Python3] greedy
ye15
2
531
create maximum number
321
0.288
Hard
5,515
https://leetcode.com/problems/create-maximum-number/discuss/2346274/Runtime%3A-216-ms-or-Memory-Usage%3A-14.2-MB
class Solution: def maxNumber(self, n_1: List[int], n_2: List[int], k: int) -> List[int]: def f1(maxum,s1,s2): n1, n2 = -1, -1 if M - s1 > maxum: n1 = max(n_1[s1:s1+maxum]) elif s1 < M: n1 = max(n_1[s1:]) if N - s2 > maxum: n2 = max(n_2[s2:s2+...
create-maximum-number
Runtime: 216 ms, | Memory Usage: 14.2 MB
vimla_kushwaha
1
135
create maximum number
321
0.288
Hard
5,516
https://leetcode.com/problems/create-maximum-number/discuss/2105459/Python-or-Easy-to-understand-or-Greedy-%2B-Dynamic-Programminng
class Solution: def maxNumber(self, nums1: List[int], nums2: List[int], k: int) -> List[int]: m = len(nums1) n = len(nums2) dp = {} # get the max number string with "length" from index "i" in nums1 and index "j" in nums2 # using number string to easy to compare ...
create-maximum-number
Python | Easy to understand | Greedy + Dynamic Programminng
AdairCLO
0
168
create maximum number
321
0.288
Hard
5,517
https://leetcode.com/problems/create-maximum-number/discuss/1982077/Python3-or-Monotonic-Stack-or-Greedy
class Solution: def maxNumber(self, nums1: List[int], nums2: List[int], k: int) -> List[int]: def find_k_max_number_in_an_array(nums, k): drop_possible = len(nums) - k n = len(nums) stack = [] for i, val in enumerate(nums): while stack...
create-maximum-number
Python3 | Monotonic Stack | Greedy
showing_up_each_day
0
310
create maximum number
321
0.288
Hard
5,518
https://leetcode.com/problems/coin-change/discuss/2058537/Python-Easy-2-DP-approaches
class Solution: def coinChange(self, coins: List[int], amount: int) -> int: dp=[math.inf] * (amount+1) dp[0]=0 for coin in coins: for i in range(coin, amount+1): if i-coin>=0: dp[i]=min(dp[i], dp[i-coin]+1) ret...
coin-change
Python Easy 2 DP approaches
constantine786
32
3,800
coin change
322
0.416
Medium
5,519
https://leetcode.com/problems/coin-change/discuss/2058537/Python-Easy-2-DP-approaches
class Solution: def coinChange(self, coins: List[int], amount: int) -> int: def coinChangeInner(rem, cache): if rem < 0: return math.inf if rem == 0: return 0 if rem in cache: return ca...
coin-change
Python Easy 2 DP approaches
constantine786
32
3,800
coin change
322
0.416
Medium
5,520
https://leetcode.com/problems/coin-change/discuss/478739/Python3-DP-solution-with-comments-to-help-understand-what-is-happening-and-why
class Solution: def coinChange(self, coins: List[int], amount: int) -> int: numCoins = len(coins) # Values in this array equal the number of coins needed to achieve the cost of the index minCoins = [amount + 1] * (amount + 1) minCoins[0] = 0 # Loop through e...
coin-change
Python3 DP solution with comments to help understand what is happening and why
jhacker
26
2,700
coin change
322
0.416
Medium
5,521
https://leetcode.com/problems/coin-change/discuss/861503/Python3-dp
class Solution: def coinChange(self, coins: List[int], amount: int) -> int: @cache def fn(x): """Return fewest number of coins to make up to x.""" if x == 0: return 0 if x < 0: return inf return min(1 + fn(x - coin) for coin in coins) ...
coin-change
[Python3] dp
ye15
5
276
coin change
322
0.416
Medium
5,522
https://leetcode.com/problems/coin-change/discuss/861503/Python3-dp
class Solution: def coinChange(self, coins: List[int], amount: int) -> int: dp = [0] + [inf]*amount for x in range(amount): if dp[x] < inf: for coin in coins: if x + coin <= amount: dp[x+coin] = min(dp[x+coin], 1 + dp[x]) ...
coin-change
[Python3] dp
ye15
5
276
coin change
322
0.416
Medium
5,523
https://leetcode.com/problems/coin-change/discuss/861503/Python3-dp
class Solution: def coinChange(self, coins: List[int], amount: int) -> int: dp = [0] + [inf]*amount for x in range(1, amount+1): for coin in coins: if coin <= x: dp[x] = min(dp[x], 1 + dp[x-coin]) return dp[-1] if dp[-1] < inf else -1
coin-change
[Python3] dp
ye15
5
276
coin change
322
0.416
Medium
5,524
https://leetcode.com/problems/coin-change/discuss/610710/PythonGo-O(-c*n-)-sol-by-DP-w-Hint
class Solution: def coinChange(self, coins: List[int], amount: int) -> int: # initialization for dp_table dp_table = [ float('inf') for _ in range(amount+1) ] # base case for $0 dp_table[0] = 0 for value in range(1, amount+1): for coin in coin...
coin-change
Python/Go O( c*n ) sol by DP [ w/ Hint ]
brianchiang_tw
5
831
coin change
322
0.416
Medium
5,525
https://leetcode.com/problems/coin-change/discuss/610710/PythonGo-O(-c*n-)-sol-by-DP-w-Hint
class Solution: def coinChange(self, coins: List[int], amount: int) -> int: @cache def dp(cur_amount): ## base cases if cur_amount == 0: # current amount can be fully changed with given coins return 0 ...
coin-change
Python/Go O( c*n ) sol by DP [ w/ Hint ]
brianchiang_tw
5
831
coin change
322
0.416
Medium
5,526
https://leetcode.com/problems/coin-change/discuss/610710/PythonGo-O(-c*n-)-sol-by-DP-w-Hint
class Solution: def coinChange(self, coins: List[int], amount: int) -> int: # -------------------------------------------------------------------- dp_table={0:0} def dp(amount): if amount < 0: return -1 elif amount ...
coin-change
Python/Go O( c*n ) sol by DP [ w/ Hint ]
brianchiang_tw
5
831
coin change
322
0.416
Medium
5,527
https://leetcode.com/problems/coin-change/discuss/2164625/Python3-solution-DP-top-down-approach
class Solution: def coinChange(self, coins: List[int], amount: int) -> int: dp = {} for i in coins: dp[i] = 1 def getResult(amount): if amount == 0: return 0 if amount in dp: return dp[amount] ...
coin-change
📌 Python3 solution DP top down approach
Dark_wolf_jss
4
95
coin change
322
0.416
Medium
5,528
https://leetcode.com/problems/coin-change/discuss/1106130/Python-DP-recursive-easy-to-understand
class Solution: def coinChange(self, coins: List[int], amount: int) -> int: if not coins: return 0 cache = {} def dp(amount): if amount in cache: return cache[amount] if amount == 0: return 0 tmp = [] ...
coin-change
Python DP recursive easy to understand
dlog
4
699
coin change
322
0.416
Medium
5,529
https://leetcode.com/problems/coin-change/discuss/2059283/Python-Simple-Python-Solution-Using-DP-oror-82-Faster
class Solution: def coinChange(self, coins: List[int], amount: int) -> int: dp = [ 10000 for _ in range(amount+1)] dp[0] = 0 for i in range(amount+1): for coin in coins: if coin <= i: dp[i] = min(dp[i],1 + dp[i-coin]) if dp[-1] == 10000: return -1 else: return dp[-1]
coin-change
[ Python ] ✅✅ Simple Python Solution Using DP || 82 % Faster✌👍
ASHOK_KUMAR_MEGHVANSHI
3
360
coin change
322
0.416
Medium
5,530
https://leetcode.com/problems/coin-change/discuss/1847630/Python-Bottom-up-DP-solution-explained
class Solution: def coinChange(self, coins: List[int], amount: int) -> int: if amount == 0 or not coins: return 0 # this dp will hold the number of coins # required for every amount from 0..amount dp = [float('inf')] * (amount+1) # to have a sum of zero ...
coin-change
[Python] Bottom-up DP solution explained
buccatini
3
264
coin change
322
0.416
Medium
5,531
https://leetcode.com/problems/coin-change/discuss/1767785/Python-Solution-using-Dynamic-Programming-or-Detailed-Explanation
class Solution: def coinChange(self, coins: List[int], amount: int) -> int: MAX_INT = 100000 if amount == 0: return 0 if amount in coins: return 1 dp = [MAX_INT]*(amount+1) dp[0] = 0 dp[1] = 1 if 1 in coins else -1 ...
coin-change
Python Solution using Dynamic Programming | Detailed Explanation
anushkabajpai
3
487
coin change
322
0.416
Medium
5,532
https://leetcode.com/problems/coin-change/discuss/1261083/Python-O(n*amount)-8-lines-Easy-to-understand-DP-Solution
class Solution: def coinChange(self, coins: List[int], amount: int) -> int: # If given amount is zero, then there are '0' ways to form this amount using any set of coins if amount == 0: return 0 # There is no way the result ,i.e. the no. of coins to form the amount, can be gr...
coin-change
[Python] O(n*amount), 8 lines, Easy to understand DP Solution
yashjain039
3
221
coin change
322
0.416
Medium
5,533
https://leetcode.com/problems/coin-change/discuss/1838052/Simple-DFS-using-a-template
class Solution: def coinChange(self, coins, amount): @lru_cache(maxsize=None) def dfs(curr): if curr > amount: return math.inf if curr == amount: return 0 return min(dfs(curr + val) + 1 for val in coins) ...
coin-change
Simple DFS using a template;
GeneBelcher
2
284
coin change
322
0.416
Medium
5,534
https://leetcode.com/problems/coin-change/discuss/1838052/Simple-DFS-using-a-template
class Solution: def mincostTickets(self, days: List[int], costs: List[int]) -> int: travel_days = set(days) pass_duration = [1, 7, 30] @lru_cache(maxsize=None) def dfs(day): if day > 365: return 0 if day in travel_days: ...
coin-change
Simple DFS using a template;
GeneBelcher
2
284
coin change
322
0.416
Medium
5,535
https://leetcode.com/problems/coin-change/discuss/1761259/Java-Python3-Simple-DP-Solution-(Top-Down-and-Bottom-Up)
class Solution: def coinChange(self, coins: List[int], amount: int) -> int: @lru_cache(None) def dp(i: int) -> int: # base case if i == 0: return 0 if i < 0: return -1 # recurrence minimum = float('inf') ...
coin-change
✅ [Java / Python3] Simple DP Solution (Top-Down & Bottom-Up)
JawadNoor
2
183
coin change
322
0.416
Medium
5,536
https://leetcode.com/problems/coin-change/discuss/1761259/Java-Python3-Simple-DP-Solution-(Top-Down-and-Bottom-Up)
class Solution: def coinChange(self, coins: List[int], amount: int) -> int: dp = [float('inf')]*(amount+1) dp[0] = 0 for coin in coins: for x in range(coin, amount+1): dp[x] = min(dp[x], dp[x-coin]+1) return dp[amount] if dp[amount] != float('inf') else -...
coin-change
✅ [Java / Python3] Simple DP Solution (Top-Down & Bottom-Up)
JawadNoor
2
183
coin change
322
0.416
Medium
5,537
https://leetcode.com/problems/coin-change/discuss/2403302/Python3-or-Solved-Bottom-Up-Using-DP-%2B-Tabulation
class Solution: #Time-Complexity: O(amount * len(coins)), we solve amount number of subproblems, but in #worst case, we iterate through each and every coin in coins array! #Space-Complexity:O(amount) def coinChange(self, coins: List[int], amount: int) -> int: #bottom up approach -> Use dp table...
coin-change
Python3 | Solved Bottom-Up Using DP + Tabulation
JOON1234
1
43
coin change
322
0.416
Medium
5,538
https://leetcode.com/problems/coin-change/discuss/2259418/Python-Dynamic-full-working-solution-with-explanation
class Solution: def coinChange(self, coins: List[int], amount: int) -> int: # Time: O(n*n) and Space:O(n) dp = [amount + 1] * (amount + 1) # bottom-up dp from 0 to amount indexes with amount+1 value in it dp[0] = 0 # with 0 coins we can get 0 amount # we will go fro...
coin-change
Python Dynamic full working solution with explanation
DanishKhanbx
1
279
coin change
322
0.416
Medium
5,539
https://leetcode.com/problems/coin-change/discuss/2222627/Solution-with-VIDEO-EXPLANATION
class Solution(object): def coinChange(self, coins, amount): dp=[amount+1]*(amount+1) dp[0]=0 for i in range(1,amount+1): for c in coins: if i-c>=0: dp[i]=min(dp[i],1+dp[i-c]) if dp[amount]>amount: return -1 return dp[amount]
coin-change
Solution with VIDEO EXPLANATION
Egan_707
1
80
coin change
322
0.416
Medium
5,540
https://leetcode.com/problems/coin-change/discuss/2060422/Python3-oror-Recursion-oror-Memoization-oror-Faster-Solution
class Solution(object): def __init__(self): self.mem = {0: 0} def coinChange(self, coins, amount): coins.sort() minCoins = self.getMinCoins(coins, amount) if minCoins == float('inf'): return -1 return minCoins def getMinCoins(self...
coin-change
Python3 || Recursion || Memoization || Faster Solution
bvian
1
113
coin change
322
0.416
Medium
5,541
https://leetcode.com/problems/coin-change/discuss/2010907/python3-Runtime%3A-1493ms-81.19-memory%3A-14.1mb-62.20
class Solution: def coinChange(self, coins: List[int], amount: int) -> int: ways = [float('inf')] * (amount + 1) ways[0] = 0 for coin in coins: for amt in range(len(ways)): if coin <= amt: ways[amt] = min(ways[amt], ways[amt - coin] + ...
coin-change
python3 Runtime: 1493ms 81.19% memory: 14.1mb 62.20%
arshergon
1
70
coin change
322
0.416
Medium
5,542
https://leetcode.com/problems/coin-change/discuss/1669308/Straightforward-bottom-up-and-top-down-DP-solutions-for-Coin-Change
class Solution: def coinChange(self, coins: 'List[int]', amount: 'int') -> 'int': dp = [float('inf')] * (amount+1) dp[0] = 0 for amount_left in range(min(coins), amount+1): children = [dp[(amount_left-coin)]+1 for coin in coins if (amount_left-coin)>=0] if children: ...
coin-change
Straightforward bottom-up and top-down DP solutions for Coin Change
NinjaBlack
1
118
coin change
322
0.416
Medium
5,543
https://leetcode.com/problems/coin-change/discuss/1669308/Straightforward-bottom-up-and-top-down-DP-solutions-for-Coin-Change
class Solution: def coinChange(self, coins: 'List[int]', amount: 'int') -> 'int': @cache def dp(amount_left): if amount_left == 0: return 0 children = [dp(amount_left-coin)+1 for coin in coins if (amount_left-coin)>=0] if children: ...
coin-change
Straightforward bottom-up and top-down DP solutions for Coin Change
NinjaBlack
1
118
coin change
322
0.416
Medium
5,544
https://leetcode.com/problems/coin-change/discuss/1666872/Python-or-Simple-and-intuitive
class Solution: def coinChange(self, coins: List[int], amount: int) -> int: # initiate a 1d dp list mx=amount+1 dp=[0]+[mx for _ in range(amount)] #dp list is generated for smaller values first. this will be used to compute higer values - classic memoization approach for i i...
coin-change
Python | Simple and intuitive
vishyarjun1991
1
332
coin change
322
0.416
Medium
5,545
https://leetcode.com/problems/coin-change/discuss/1105374/Python-DP-Solution
class Solution: def coinChange(self, coins: List[int], sumx: int) -> int: n = len(coins) t = [[999999999 for i in range(sumx+1)] for j in range(n+1)] for i in range(1, n + 1): t[i][0] = 0 for j in range(1, (sumx + 1)): if j % coins[0] =...
coin-change
Python DP Solution
aishwaryanathanii
1
267
coin change
322
0.416
Medium
5,546
https://leetcode.com/problems/coin-change/discuss/955394/Python-DP-Top-Down-and-Bottom-Up-(%2B-Complexity-Analyses)
class Solution: def coinChange(self, coins: List[int], amount: int) -> int: ## Top Down Approach coins.sort() result = self._helper(coins, amount, {0:0}) # cache = {0:0} -> 0 amount requires 0 coins return [result, -1][result == float('inf')] # Return result iff it's not infinity. Else, return -1 d...
coin-change
Python - DP - Top Down & Bottom Up (+ Complexity Analyses)
noobie12
1
197
coin change
322
0.416
Medium
5,547
https://leetcode.com/problems/coin-change/discuss/955394/Python-DP-Top-Down-and-Bottom-Up-(%2B-Complexity-Analyses)
class Solution: def coinChange(self, coins: List[int], amount: int) -> int: ## Bottom Up Approach dp = [0] + [float('inf')] * amount # Zero amount means zero coins &amp; every other value is initialized to infinity for curr_amount in range(1, amount + 1): for coin in coins: ...
coin-change
Python - DP - Top Down & Bottom Up (+ Complexity Analyses)
noobie12
1
197
coin change
322
0.416
Medium
5,548
https://leetcode.com/problems/coin-change/discuss/2845455/DP-bottom-to-top
class Solution: def coinChange(self, coins: List[int], amount: int) -> int: memo = [amount+1 for i in range(amount+1)] memo[0] = 0 for i in range(len(memo)): for j in coins: if i-j<0: continue memo[i] = min(memo[i],1+me...
coin-change
DP bottom to top
ychhhen
0
3
coin change
322
0.416
Medium
5,549
https://leetcode.com/problems/coin-change/discuss/2845256/Python3-Use-DP-%2B-Memo-to-find-the-minimum-number-of-coins-cnt
class Solution: def coinChange(self, coins: List[int], amount: int) -> int: memo = dict() def dp(n): if n == 0: return 0 if n < 0: return -1 if n in memo: return memo[n] res = float('INF') # Use a maximum...
coin-change
[Python3] Use DP + Memo to find the minimum number of coins cnt
Cceline00
0
1
coin change
322
0.416
Medium
5,550
https://leetcode.com/problems/coin-change/discuss/2844703/Dynamic-Programming-explained-Python
class Solution: def coinChange(self, coins: List[int], amount: int) -> int: coin_change = [1e9 for _ in range(amount + 1)] coin_change[0] = 0 for i in range(amount + 1): for coin in coins: if i - coin >= 0: coin_change[i] = min(co...
coin-change
Dynamic Programming explained - Python
rere-rere
0
2
coin change
322
0.416
Medium
5,551
https://leetcode.com/problems/coin-change/discuss/2840019/BFS-Solution-beats-98
class Solution: def coinChange(self, coins: List[int], amount: int) -> int: q = deque() q.append(amount) seen = set() steps = 0 while q: for _ in range(len(q)): remain = q.popleft() if remain == 0: return steps ...
coin-change
BFS Solution beats 98%
samuelmayna
0
3
coin change
322
0.416
Medium
5,552
https://leetcode.com/problems/coin-change/discuss/2837518/Python-DP-solution
class Solution: def coinChange(self, coins: List[int], amount: int) -> int: dp = [0] for i in range(1,amount+1): m = sys.maxsize has_answer = False for coin in coins: if(i - coin >= 0 and dp[i - coin] != -1): has_answer = True ...
coin-change
Python DP solution
charleswizards
0
4
coin change
322
0.416
Medium
5,553
https://leetcode.com/problems/coin-change/discuss/2821724/Python-or-Recursive-way-or-Dynamic-Programming
class Solution: def coinChange(self, coins: List[int], amount: int) -> int: res = [float("inf")] def dfs(sum, count): if sum == amount: res[0] = min(res[0], count) return for num in coins: if sum+num <= amount: ...
coin-change
Python | Recursive way | Dynamic Programming
ajay_gc
0
7
coin change
322
0.416
Medium
5,554
https://leetcode.com/problems/coin-change/discuss/2821724/Python-or-Recursive-way-or-Dynamic-Programming
class Solution: def coinChange(self, coins: List[int], amount: int) -> int: if not coins: return -1 dptable = [float("inf")]*(amount+1) dptable[0] = 0 for amount in range(1, amount+1): min_value = dptable[amount] for num in coins: ...
coin-change
Python | Recursive way | Dynamic Programming
ajay_gc
0
7
coin change
322
0.416
Medium
5,555
https://leetcode.com/problems/coin-change/discuss/2786042/7-lines-of-DP-solution-(python)
class Solution: def coinChange(self, coins: List[int], amount: int) -> int: dp = {i:float('inf') for i in range(amount+1)} dp[0] = 0 for i in range(1,amount+1): for c in coins: if i >= c: dp[i] = min(dp[i], 1+dp[i-c]) ...
coin-change
7 lines of DP solution (python)
candymoon36
0
11
coin change
322
0.416
Medium
5,556
https://leetcode.com/problems/coin-change/discuss/2782296/Breaking-down-vs-Building-up
class Solution: coins = [] @functools.cache def soln(self, amount: int): if amount < 0: return int(10e9) if amount == 0: return 0 res = [self.soln(amount - v) + 1 for v in self.coins] return min(res) def coinChange(self, coins: List[int...
coin-change
Breaking down vs Building up
Abhi5415
0
5
coin change
322
0.416
Medium
5,557
https://leetcode.com/problems/coin-change/discuss/2782296/Breaking-down-vs-Building-up
class Solution: def coinChange(self, coins: List[int], amount: int) -> int: if amount == 0: return 0 dp = [0] * (amount +1) # dp stores min number of coins needed to make [i] for i in range(1, amount+1): # dp[i] m = int(10e9) for j in...
coin-change
Breaking down vs Building up
Abhi5415
0
5
coin change
322
0.416
Medium
5,558
https://leetcode.com/problems/coin-change/discuss/2776424/Python-Simple-3-Approach-or-Recursive-or-Iterative-Easy-DP
class Solution: def coinChange(self, coins: List[int], amount: int) -> int: dp = {} def bfs(amount, item): if item==0 and amount>=0: return float('inf')-1 if item>0 and amount==0: return 0 if item==1 and amount>0: return amount//coins[item-...
coin-change
✔️ [Python] Simple 3 Approach | Recursive | Iterative Easy DP
girraj_14581
0
20
coin change
322
0.416
Medium
5,559
https://leetcode.com/problems/coin-change/discuss/2776424/Python-Simple-3-Approach-or-Recursive-or-Iterative-Easy-DP
class Solution(): def coinChange(self, coins: List[int], amount: int) -> int: n = len(coins) dp = [[None]*(amount+1) for i in range(n+1)] for i in range(amount+1): dp[0][i] = float('inf')-1 if i%coins[0]==0: dp[1][i] = i//coins[0] else...
coin-change
✔️ [Python] Simple 3 Approach | Recursive | Iterative Easy DP
girraj_14581
0
20
coin change
322
0.416
Medium
5,560
https://leetcode.com/problems/coin-change/discuss/2762521/Pythonor-from-front-to-endor-quicker-DP
# class Solution: # def __init__(self) -> None: # self.amounts = [-666 for _ in range(10002)] # def coinChange(self, coins: List[int], amount: int) -> int: # if amount == 0 : # return 0 # if amount < 0: # return -1 # if self.amounts[amount] != -666: # ...
coin-change
Python| from front to end| quicker DP
lucy_sea
0
5
coin change
322
0.416
Medium
5,561
https://leetcode.com/problems/coin-change/discuss/2762516/Python-or-end-to-front-or-simple-but-slow-one
class Solution: def __init__(self) -> None: self.amounts = [-666 for _ in range(10002)] def coinChange(self, coins: List[int], amount: int) -> int: if amount == 0 : return 0 if amount < 0: return -1 if self.amounts[amount] != -666: return self....
coin-change
Python | end to front | simple but slow one
lucy_sea
0
4
coin change
322
0.416
Medium
5,562
https://leetcode.com/problems/coin-change/discuss/2761293/Python-Dynamic-Programming-with-Memory
class Solution: def coinChange(self, coins: List[int], amount: int) -> int: memo = {i:-666 for i in range(amount+1)} def dp(Coins, Amount): res = float('inf') if Amount == 0: return 0 if Amount < 0: return -1 ...
coin-change
Python Dynamic Programming with Memory
Rui_Liu_Rachel
0
5
coin change
322
0.416
Medium
5,563
https://leetcode.com/problems/coin-change/discuss/2760913/Python-solution
class Solution: def coinChange(self, coins: List[int], amount: int) -> int: if amount<0: return -1 if amount==0: return 0 dp = [float("inf")]*(amount+1) dp[0] = 0 for i in range(1, amount+1): for c in coins: if i>=c: ...
coin-change
Python solution
gcheng81
0
5
coin change
322
0.416
Medium
5,564
https://leetcode.com/problems/coin-change/discuss/2731569/Python-Solution-or-O(coins-*-amount)-TC-SC-or-2D-Grid-Dynamic-Programming-Based
class Solution: def coinChange(self, coins: List[int], amount: int) -> int: coins.sort() store = [[0 for _ in range(amount+1)] for _ in range(len(coins)+1)] for row in range(len(store)): for col in range(len(store[0])): if col =...
coin-change
Python Solution | O(coins * amount) TC, SC | 2D Grid Dynamic Programming Based
Gautam_ProMax
0
20
coin change
322
0.416
Medium
5,565
https://leetcode.com/problems/coin-change/discuss/2724592/Easy-Undertstanding-in-python
class Solution: def coinChange(self, arr: List[int], t: int) -> int: dp=[[-1 for i in range(t+1)]for i in range(len(arr)+1)] def fun(i,t): if t==0: return 0 if dp[i][t]!=-1: return dp[i][t] if i==len(arr): return 1e9...
coin-change
Easy Undertstanding in python
hemanth_12
0
10
coin change
322
0.416
Medium
5,566
https://leetcode.com/problems/coin-change/discuss/2723635/94-faster-85-less-space-python3-DP
class Solution: def coinChange(self, coins: List[int], amount: int) -> int: # A[i] = fewest number of coins needed to make sum i A = [0] + [-1] * amount # zero coins needed to make zero for i in range(1, amount + 1): # options are the last coin chosen options = [A[i-...
coin-change
94% faster, 85% less space python3 DP
jbradleyglenn
0
13
coin change
322
0.416
Medium
5,567
https://leetcode.com/problems/coin-change/discuss/2717615/Python-3-Sol.-easy-and-efficient
class Solution: def coinChange(self, coins: List[int], amount: int) -> int: def coinChangeInner(rem, cache): if rem < 0: return math.inf if rem == 0: return 0 if rem in cache: return ca...
coin-change
Python 3 Sol. easy and efficient
pranjalmishra334
0
12
coin change
322
0.416
Medium
5,568
https://leetcode.com/problems/coin-change/discuss/2713775/Python-3-Bottom-Up-Dynamic-Programming-Solution
class Solution: def coinChange(self, coins: List[int], amount: int) -> int: dp_array = [amount+1 for i in range(amount+1)] dp_array[0] = 0 for i in range(amount+1): for coin in coins: if coin<=i: dp_array[i] = min(dp_array[i], 1+dp_array[i-coi...
coin-change
Python 3, Bottom Up Dynamic Programming Solution
paul1202
0
6
coin change
322
0.416
Medium
5,569
https://leetcode.com/problems/coin-change/discuss/2695484/coinChange
class Solution: def coinChange(self, coins: List[int], amount: int) -> int: # dp = [float('inf')] * (amount + 1) # dp[0] = 0 # for i in range(amount+1): # for coin in coins: # if i + coin > amount : # continue # dp[i + coin] = m...
coin-change
coinChange
langtianyuyu
0
4
coin change
322
0.416
Medium
5,570
https://leetcode.com/problems/coin-change/discuss/2639443/DPpython
class Solution: def coinChange(self, coins: List[int], amount: int) -> int: memo = ['*' for _ in range(amount+1)] def dp(coins, target)->int: res = float('inf') if(target == 0): return 0 if(target < 0): return -1 if(memo...
coin-change
[DP]python
kuroko_6668
0
39
coin change
322
0.416
Medium
5,571
https://leetcode.com/problems/coin-change/discuss/2623942/Simple-python-code-with-explanation
class Solution: def coinChange(self, coins, amount): #store the amount+1 (value) in dp(array) (amount +1) times #because first index is 0 dp = [amount+1]*(amount+1) #change the firxt index value to 0 #because the no. of coins use...
coin-change
Simple python code with explanation
thomanani
0
101
coin change
322
0.416
Medium
5,572
https://leetcode.com/problems/coin-change/discuss/2623907/Memory-usage-less-than-96.98
class Solution: def coinChange(self, coins: List[int], amount: int) -> int: dp = [amount + 1]*(amount+1) dp[0] = 0 for a in range(1,amount+1): for c in coins: if a-c >= 0: dp[a] = min(dp[a],1+dp[a-c]) ...
coin-change
Memory usage less than 96.98%
jayeshvarma
0
41
coin change
322
0.416
Medium
5,573
https://leetcode.com/problems/coin-change/discuss/2577943/BFS-faster-than-90
class Solution: def coinChange(self, coins: List[int], amount: int) -> int: if not amount: return 0 n = len(coins) queue = deque() count = 1 temp = [] flex = set() for i in range(n): s = amount - coins[i] if(s == 0): ...
coin-change
BFS faster than 90%
Sukhwinder_Singh
0
57
coin change
322
0.416
Medium
5,574
https://leetcode.com/problems/coin-change/discuss/2555484/Simple-Python3-Solution-Using-Tabluar-Method-oror-DP
class Solution: def coinChange(self, arr: List[int], s: int) -> int: n = len(arr) dp = [[-1 for _ in range(s+1)] for _ in range(n+1)] # initialization of 1st column for i in range(1,n+1): dp[i][0] = 0 # initialization of 1st row with max value for ...
coin-change
Simple Python3 Solution Using Tabluar Method || DP
ajinkyabhalerao11
0
77
coin change
322
0.416
Medium
5,575
https://leetcode.com/problems/coin-change/discuss/2497234/Python-DP-Solution-(Memoization)
class Solution: def coinChange(self, coins: List[int], amount: int) -> int: d = {} def rec(index, sm): if sm == 0: return 0 if index < 0: return float('inf') else: take = float('inf') if coins[index] ...
coin-change
Python DP Solution (Memoization)
DietCoke777
0
66
coin change
322
0.416
Medium
5,576
https://leetcode.com/problems/coin-change/discuss/2402863/Python3-or-Looking-for-Feedback-Why-I'm-getting-TLE-for-Top-Down-Memoization!
class Solution: def coinChange(self, coins: List[int], amount: int) -> int: #top down recursive approach! -> With Memoization dp! dp = [-1] * (amount + 1) coins.sort() def helper(amount): nonlocal coins nonlocal dp #add another base case for memo! ...
coin-change
Python3 | Looking for Feedback Why I'm getting TLE for Top-Down Memoization!
JOON1234
0
25
coin change
322
0.416
Medium
5,577
https://leetcode.com/problems/coin-change/discuss/2305121/Simple-Python-solution-with-DP-and-memoization
class Solution: def coinChange(self, coins: List[int], amount: int) -> int: # min number of coins summing up to current amount i memo = [-666]*(amount + 1) return self.dp(coins, amount, memo) def dp(self, coins, amount, memo): if (amount == 0): return 0 if (amount < ...
coin-change
Simple Python solution with DP and memoization
leqinancy
0
44
coin change
322
0.416
Medium
5,578
https://leetcode.com/problems/wiggle-sort-ii/discuss/1322709/Definitely-not-O(n)-but-did-it-iteratively-in-O(nlog(N))-time
class Solution: def wiggleSort(self, nums: List[int]) -> None: sortedList = sorted(nums) n = len(nums) if n%2==0: small = sortedList[:((n//2))][::-1] large = (sortedList[(n//2):])[::-1] for i in range(1,n,2): nums[i] = large[i//2] ...
wiggle-sort-ii
Definitely not O(n) but did it iteratively in O(nlog(N)) time
prajwalPonnana004
1
180
wiggle sort ii
324
0.33
Medium
5,579
https://leetcode.com/problems/wiggle-sort-ii/discuss/2810053/Very-easy-solution-in-5-lines-using-python
class Solution: def wiggleSort(self, nums: List[int]) -> None: """ Do not return anything, modify nums in-place instead. """ tmp = nums.copy() tmp.sort() n = len(nums) i, j = 1, n - 1 for _ in range(2): for k in range(i, n, 2): # w...
wiggle-sort-ii
Very easy solution in 5 lines using python
ankurbhambri
0
9
wiggle sort ii
324
0.33
Medium
5,580
https://leetcode.com/problems/wiggle-sort-ii/discuss/2123283/Python3-or-O(NLogN)-time-%2B-O(N)-Space
class Solution: def wiggleSort(self, nums: List[int]) -> None: if len(nums)>1: nums.sort() temp=nums[:] r=len(nums)-1 ind=1 while r>=0: nums[ind]=temp[r] ind+=2 if ind>=len(nums): ...
wiggle-sort-ii
[Python3] | O(NLogN) time + O(N) Space
swapnilsingh421
0
105
wiggle sort ii
324
0.33
Medium
5,581
https://leetcode.com/problems/power-of-three/discuss/1179790/Simple-Python-Recursive-Solution-with-Explanation
class Solution: def isPowerOfThree(self, n: int) -> bool: if n == 1: return True if n == 0: return False else: return n % 3 == 0 and self.isPowerOfThree(n // 3)
power-of-three
Simple Python Recursive Solution with Explanation
stevenbooke
8
356
power of three
326
0.453
Easy
5,582
https://leetcode.com/problems/power-of-three/discuss/2470942/Python3-stupid-one-liner
class Solution: def isPowerOfThree(self, n: int) -> bool: return n in (1,3,9,27,81,243,729,2187,6561,19683,59049,177147,531441,1594323,4782969,14348907,43046721,129140163,387420489,1162261467)
power-of-three
Python3 stupid one-liner
leetavenger
4
558
power of three
326
0.453
Easy
5,583
https://leetcode.com/problems/power-of-three/discuss/2470942/Python3-stupid-one-liner
class Solution: def isPowerOfThree(self, n: int) -> bool: return n>=1 and log10(n)/log10(3)%1==0
power-of-three
Python3 stupid one-liner
leetavenger
4
558
power of three
326
0.453
Easy
5,584
https://leetcode.com/problems/power-of-three/discuss/2470942/Python3-stupid-one-liner
class Solution: def isPowerOfThree(self, n: int) -> bool: return n>=1 and 3**20%n==0
power-of-three
Python3 stupid one-liner
leetavenger
4
558
power of three
326
0.453
Easy
5,585
https://leetcode.com/problems/power-of-three/discuss/751445/Python3-a-few-approaches
class Solution: def isPowerOfThree(self, n: int) -> bool: if n <= 0: return False while n: n, r = divmod(n, 3) if n and r: return False return r == 1
power-of-three
[Python3] a few approaches
ye15
4
287
power of three
326
0.453
Easy
5,586
https://leetcode.com/problems/power-of-three/discuss/751445/Python3-a-few-approaches
class Solution: def isPowerOfThree(self, n: int) -> bool: return n > 0 and 3**(round(log(n)/log(3))) == n
power-of-three
[Python3] a few approaches
ye15
4
287
power of three
326
0.453
Easy
5,587
https://leetcode.com/problems/power-of-three/discuss/751445/Python3-a-few-approaches
class Solution: def isPowerOfThree(self, n: int) -> bool: return n > 0 and 3**19 % n == 0
power-of-three
[Python3] a few approaches
ye15
4
287
power of three
326
0.453
Easy
5,588
https://leetcode.com/problems/power-of-three/discuss/751445/Python3-a-few-approaches
class Solution: def isPowerOfThree(self, n: int) -> bool: if n <= 0: return False while n > 1: n, x = divmod(n, 3) if x > 0: return False return True
power-of-three
[Python3] a few approaches
ye15
4
287
power of three
326
0.453
Easy
5,589
https://leetcode.com/problems/power-of-three/discuss/406575/Python-Beats-95
class Solution: def isPowerOfThree(self, n: int) -> bool: if n==0: return False while (n%3==0): n /=3 if n==1: return True return False
power-of-three
Python Beats 95%
saffi
3
991
power of three
326
0.453
Easy
5,590
https://leetcode.com/problems/power-of-three/discuss/2345734/Python-Simplest-Solution-With-Explanation-or-Beg-to-adv-or-Math
class Solution: def isPowerOfThree(self, n: int) -> bool: if n < 1: return False # if the number is zero or in negative. if n == 1: return True # 1 could be a power of any number. while n > 1: # now will check for the number greater then 1. if n % 3 != 0: # if ...
power-of-three
Python Simplest Solution With Explanation | Beg to adv | Math
rlakshay14
2
173
power of three
326
0.453
Easy
5,591
https://leetcode.com/problems/power-of-three/discuss/1869568/Python-Clean-and-Simple!-Multiple-Solutions
class Solution: def isPowerOfThree(self, n): m, x = 0, 0 while m < n: m = 3**x x += 1 return n > 0 and n == m
power-of-three
Python - Clean and Simple! Multiple Solutions
domthedeveloper
2
210
power of three
326
0.453
Easy
5,592
https://leetcode.com/problems/power-of-three/discuss/1869568/Python-Clean-and-Simple!-Multiple-Solutions
class Solution: def isPowerOfThree(self, n): if n == 0: return False while not n % 3: n //= 3 return n == 1
power-of-three
Python - Clean and Simple! Multiple Solutions
domthedeveloper
2
210
power of three
326
0.453
Easy
5,593
https://leetcode.com/problems/power-of-three/discuss/1869568/Python-Clean-and-Simple!-Multiple-Solutions
class Solution: def isPowerOfThree(self, n): if n % 3: return n == 1 return n > 0 and self.isPowerOfThree(n//3)
power-of-three
Python - Clean and Simple! Multiple Solutions
domthedeveloper
2
210
power of three
326
0.453
Easy
5,594
https://leetcode.com/problems/power-of-three/discuss/1869568/Python-Clean-and-Simple!-Multiple-Solutions
class Solution: def isPowerOfThree(self, n): return n > 0 and n == 3**round(math.log(n,3),9)
power-of-three
Python - Clean and Simple! Multiple Solutions
domthedeveloper
2
210
power of three
326
0.453
Easy
5,595
https://leetcode.com/problems/power-of-three/discuss/1869568/Python-Clean-and-Simple!-Multiple-Solutions
class Solution: def isPowerOfThree(self, n): return n > 0 and float.is_integer(round(math.log(n,3),9))
power-of-three
Python - Clean and Simple! Multiple Solutions
domthedeveloper
2
210
power of three
326
0.453
Easy
5,596
https://leetcode.com/problems/power-of-three/discuss/1869568/Python-Clean-and-Simple!-Multiple-Solutions
class Solution: def isPowerOfThree(self, n): powersOfThree = [1] while powersOfThree[-1] < 2**31-1: powersOfThree.append(3 * powersOfThree[-1]) return n in powersOfThree
power-of-three
Python - Clean and Simple! Multiple Solutions
domthedeveloper
2
210
power of three
326
0.453
Easy
5,597
https://leetcode.com/problems/power-of-three/discuss/1869568/Python-Clean-and-Simple!-Multiple-Solutions
class Solution: def isPowerOfThree(self, n): return n in map(lambda x : 3**x, range(0,20))
power-of-three
Python - Clean and Simple! Multiple Solutions
domthedeveloper
2
210
power of three
326
0.453
Easy
5,598
https://leetcode.com/problems/power-of-three/discuss/1869568/Python-Clean-and-Simple!-Multiple-Solutions
class Solution: def isPowerOfThree(self, n): return n in [3**x for x in range(0,20)]
power-of-three
Python - Clean and Simple! Multiple Solutions
domthedeveloper
2
210
power of three
326
0.453
Easy
5,599