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/sum-of-absolute-differences-in-a-sorted-array/discuss/2675010/Python3-Solution-oror-O(N)-Time-and-Space-Complexity
class Solution: def getSumAbsoluteDifferences(self, nums: List[int]) -> List[int]: n=len(nums) for i in range(1,n): nums[i]=nums[i]+nums[i-1] lst=[nums[-1]-nums[0]-(n-1)*nums[0]] l,r = 1,n-2 for i in range(1,n): val=(nums[-1]-nums[i]-r*(nums[i]-nums[i-1]))+((nums[i]-nums[i-1])*l-nums[i-1]) lst.append(val) l,r = l+1,r-1 return lst
sum-of-absolute-differences-in-a-sorted-array
Python3 Solution || O(N) Time & Space Complexity
akshatkhanna37
0
7
sum of absolute differences in a sorted array
1,685
0.649
Medium
24,400
https://leetcode.com/problems/sum-of-absolute-differences-in-a-sorted-array/discuss/2651637/Python3-or-Prefix-Sum-After-Math-Logic-O(N)-TIME-AND-SPACE!
class Solution: #Time-Complexity:O(n + n) -> O(n) #Space-Complexity: O(n) def getSumAbsoluteDifferences(self, nums: List[int]) -> List[int]: #brute-force -> O(N^2) -> WILL GET TIMED OUT! ans = [] n = len(nums) #Approach: Utilize math -> Basically, we know absolute value of a and b is simply defined as #max(a,b) - min(a,b) in order to gaurantee a non-negative result, since both a and b #are positive numbers according to constraints of the problem. #Thus, for all other elements that are to left of current element(smaller), #we can write (nums[i] - nums[0]) + (nums[i] - nums[1]) + ... + (nums[i] - nums[i-1]). #There are i terms -> thus we can deduce this to (i * nums[i]) - (nums[0] + nums[1] + ... nums[i-1]) #(i * nums[i]) - (prefix_sum[i-1]) #Furthermore, for all other elements that are to the right of current element(larger), #we can write (nums[i+1] - nums[i]) + (nums[i+2] - nums[i]) + ... +(nums[len(nums)-1] - nums[i])! #We can deduce this since there are n - i - 1 terms! -> #(nums[i+1] + nums[i+2] + nums[i+3] ... + nums[len(nums)-1]) - (nums[i] * (n-i-1)) #(prefix_sum[-1] - prefix_sum[i]) - (nums[i] * (n-i-1))! #So, let's first initialize the prefix sum! prefix_sum = [] prefix_sum.append(nums[0]) for a in range(1, len(nums)): prefix_sum.append(nums[a] + prefix_sum[a-1]) #once prefix sum initailized, we can start computing sum of abs diff for each and every #element! for i in range(0, len(nums)): #handle sum of abs diff w/ respect to smaller elements! #handle case where current index is 0! subtraction = None if(i == 0): subtraction = 0 else: subtraction = prefix_sum[i-1] first_sum = (nums[i] * i) - subtraction #handle sum of abs diff w/ respect to larger elements! second_sum = (prefix_sum[-1] - prefix_sum[i]) - ((n - i - 1) * nums[i]) res = first_sum + second_sum ans.append(res) return ans
sum-of-absolute-differences-in-a-sorted-array
Python3 | Prefix Sum After Math Logic O(N) TIME AND SPACE!
JOON1234
0
11
sum of absolute differences in a sorted array
1,685
0.649
Medium
24,401
https://leetcode.com/problems/sum-of-absolute-differences-in-a-sorted-array/discuss/2065185/Python-O(n)-time-O(1)-space-(no-prefix-sum)
class Solution: def getSumAbsoluteDifferences(self, nums: List[int]) -> List[int]: answer = [] # compute first number res = 0 for i in range(1, len(nums)): res += nums[i] - nums[0] answer.append(res) for i in range(1, len(nums)): diff = nums[i] - nums[i - 1] res += diff * (i - 1) res -= diff * (len(nums) - i - 1) answer.append(res) return answer
sum-of-absolute-differences-in-a-sorted-array
Python O(n) time O(1) space (no prefix sum)
SheinmanMichael
0
69
sum of absolute differences in a sorted array
1,685
0.649
Medium
24,402
https://leetcode.com/problems/sum-of-absolute-differences-in-a-sorted-array/discuss/1789900/Python-easy-to-read-and-understand-or-prefixsum
class Solution: def getSumAbsoluteDifferences(self, nums: List[int]) -> List[int]: n = len(nums) prefix, suffix = [0 for _ in range(n)], [0 for _ in range(n)] prefix[0], suffix[n-1] = nums[0], nums[n-1] for i in range(1, n): prefix[i] = prefix[i-1] + nums[i] for i in range(n-2, -1, -1): suffix[i] = suffix[i+1] + nums[i] #print(prefix, suffix) ans = [] for i in range(n): left = 0 if i == 0 else (i)*nums[i] - prefix[i-1] right = 0 if i == n-1 else suffix[i+1] - (n-1-i)*nums[i] ans.append(right+left) return ans
sum-of-absolute-differences-in-a-sorted-array
Python easy to read and understand | prefixsum
sanial2001
0
132
sum of absolute differences in a sorted array
1,685
0.649
Medium
24,403
https://leetcode.com/problems/sum-of-absolute-differences-in-a-sorted-array/discuss/1607474/Python3-Prefix-sum-solution
class Solution: def getSumAbsoluteDifferences(self, nums: List[int]) -> List[int]: res = [0] * len(nums) prefix_sum = [0] * len(nums) prefix_sum[0] = nums[0] for i in range(1, len(nums)): res[i] += i * nums[i] - prefix_sum[i - 1] prefix_sum[i] = nums[i] + prefix_sum[i - 1] prefix_sum[-1] = nums[-1] for i in range(len(nums) - 2, -1, -1): res[i] += prefix_sum[i + 1] - (len(nums) - i - 1) * nums[i] prefix_sum[i] = nums[i] + prefix_sum[i + 1] return res
sum-of-absolute-differences-in-a-sorted-array
[Python3] Prefix sum solution
maosipov11
0
66
sum of absolute differences in a sorted array
1,685
0.649
Medium
24,404
https://leetcode.com/problems/sum-of-absolute-differences-in-a-sorted-array/discuss/1566513/Python-3-Prefix-Sum-O(n)-solution
class Solution: def getSumAbsoluteDifferences(self, nums: List[int]) -> List[int]: prefix = [0] * (len(nums) + 1) for idx in range(len(nums)): prefix[idx+1] += prefix[idx] + nums[idx] print(prefix) res = [] for idx in range(len(nums)): res.append(nums[idx] * (idx+1) - prefix[idx+1] + (prefix[-1] - prefix[idx]) - nums[idx] * (len(nums) - idx)) return res
sum-of-absolute-differences-in-a-sorted-array
Python 3 Prefix Sum O(n) solution
akshaykumar19002
0
107
sum of absolute differences in a sorted array
1,685
0.649
Medium
24,405
https://leetcode.com/problems/sum-of-absolute-differences-in-a-sorted-array/discuss/1523995/Accumulate-forward-and-backward-and-one-pass-82-speed
class Solution: def getSumAbsoluteDifferences(self, nums: List[int]) -> List[int]: len_nums = len(nums) forward = [0] + list(accumulate(nums)) backward = list(accumulate(nums[::-1]))[::-1] + [0] return [(nums[i] * i - forward[i] + backward[i + 1] - nums[i] * (len_nums - i - 1)) for i in range(len_nums)]
sum-of-absolute-differences-in-a-sorted-array
Accumulate forward & backward and one pass, 82% speed
EvgenySH
0
95
sum of absolute differences in a sorted array
1,685
0.649
Medium
24,406
https://leetcode.com/problems/sum-of-absolute-differences-in-a-sorted-array/discuss/1073499/Python3-prefix-sum
class Solution: def getSumAbsoluteDifferences(self, nums: List[int]) -> List[int]: prefix = [0] for x in nums: prefix.append(prefix[-1] + x) ans = [] for i, x in enumerate(nums): ans.append(prefix[-1] - 2*prefix[i] + (2*i - len(nums)) * nums[i]) return ans
sum-of-absolute-differences-in-a-sorted-array
[Python3] prefix sum
ye15
0
75
sum of absolute differences in a sorted array
1,685
0.649
Medium
24,407
https://leetcode.com/problems/stone-game-vi/discuss/1860830/python-easy-solution-using-sort
class Solution: def stoneGameVI(self, alice: List[int], bob: List[int]) -> int: n = len(alice) arr = [alice[i] + bob[i] for i in range(n)] s = sum(bob) res = 0 k = (n+1)//2 arr.sort(reverse=True) for i in range(0, n, 2): res += arr[i] if res > s: return 1 elif res == s: return 0 else: return -1
stone-game-vi
python easy solution using sort
byuns9334
0
85
stone game vi
1,686
0.544
Medium
24,408
https://leetcode.com/problems/stone-game-vi/discuss/1073504/Python3-greedy
class Solution: def stoneGameVI(self, aliceValues: List[int], bobValues: List[int]) -> int: pq = [(-x-y, x, y) for x, y in zip(aliceValues, bobValues)] heapify(pq) s0 = s1 = i = 0 while pq: _, x, y = heappop(pq) if i: s1 += y else: s0 += x i ^= 1 if s0 > s1: return 1 if s0 < s1: return -1 return 0
stone-game-vi
[Python3] greedy
ye15
0
70
stone game vi
1,686
0.544
Medium
24,409
https://leetcode.com/problems/delivering-boxes-from-storage-to-ports/discuss/1465884/Python3-dp-%2B-greedy
class Solution: def boxDelivering(self, boxes: List[List[int]], portsCount: int, maxBoxes: int, maxWeight: int) -> int: dp = [0] + [inf]*len(boxes) trips = 2 ii = 0 for i in range(len(boxes)): maxWeight -= boxes[i][1] if i and boxes[i-1][0] != boxes[i][0]: trips += 1 while maxBoxes < i - ii + 1 or maxWeight < 0 or ii < i and dp[ii] == dp[ii+1]: maxWeight += boxes[ii][1] if boxes[ii][0] != boxes[ii+1][0]: trips-=1 ii += 1 dp[i+1] = dp[ii] + trips return dp[-1]
delivering-boxes-from-storage-to-ports
[Python3] dp + greedy
ye15
1
204
delivering boxes from storage to ports
1,687
0.385
Hard
24,410
https://leetcode.com/problems/count-of-matches-in-tournament/discuss/1276389/simple-python-solution-easy-to-understand
class Solution: def numberOfMatches(self, n: int) -> int: # the logic is, among n teams only 1 team will won, so n-1 teams will lose # hence there will be n-1 match (so that n-1 teams can lose) return n-1
count-of-matches-in-tournament
simple python solution-easy to understand
nandanabhishek
4
272
count of matches in tournament
1,688
0.832
Easy
24,411
https://leetcode.com/problems/count-of-matches-in-tournament/discuss/974774/Python3-O(logN)-and-O(1)-solutions
class Solution: def numberOfMatches(self, n: int) -> int: ans = 0 while n > 1: ans += n//2 n = n//2 + (n&amp;1) return ans
count-of-matches-in-tournament
[Python3] O(logN) & O(1) solutions
ye15
3
276
count of matches in tournament
1,688
0.832
Easy
24,412
https://leetcode.com/problems/count-of-matches-in-tournament/discuss/974774/Python3-O(logN)-and-O(1)-solutions
class Solution: def numberOfMatches(self, n: int) -> int: return n-1
count-of-matches-in-tournament
[Python3] O(logN) & O(1) solutions
ye15
3
276
count of matches in tournament
1,688
0.832
Easy
24,413
https://leetcode.com/problems/count-of-matches-in-tournament/discuss/1287147/Easy-Python-Solution(98.92)
class Solution: def numberOfMatches(self, n: int) -> int: i=n c=0 while i!=1: if(i%2==0): c+=i//2 i=i//2 else: c+=(i-1)//2 i=(i-1)//2 +1 return c
count-of-matches-in-tournament
Easy Python Solution(98.92%)
Sneh17029
2
281
count of matches in tournament
1,688
0.832
Easy
24,414
https://leetcode.com/problems/count-of-matches-in-tournament/discuss/1287147/Easy-Python-Solution(98.92)
class Solution: def numberOfMatches(self, n: int) -> int: return n-1
count-of-matches-in-tournament
Easy Python Solution(98.92%)
Sneh17029
2
281
count of matches in tournament
1,688
0.832
Easy
24,415
https://leetcode.com/problems/count-of-matches-in-tournament/discuss/971618/Constant-Time-Solution-xD
class Solution(object): def numberOfMatches(self, n): return n - 1
count-of-matches-in-tournament
Constant Time Solution xD
theriley106
2
72
count of matches in tournament
1,688
0.832
Easy
24,416
https://leetcode.com/problems/count-of-matches-in-tournament/discuss/970344/python3andc%2B%2B%3A-one-line(beat-100)
class Solution: def numberOfMatches(self, n: int) -> int: return n-1
count-of-matches-in-tournament
python3&c++: one line(beat 100%)
dqdwsdlws
2
78
count of matches in tournament
1,688
0.832
Easy
24,417
https://leetcode.com/problems/count-of-matches-in-tournament/discuss/2365571/Python-Easy-One-Liner
class Solution: def numberOfMatches(self, n: int) -> int: return n - 1
count-of-matches-in-tournament
Python Easy One-Liner ✅🐍
qing306037
1
49
count of matches in tournament
1,688
0.832
Easy
24,418
https://leetcode.com/problems/count-of-matches-in-tournament/discuss/2360844/or-Python-or-IMO-Standard-Math-or-Very-Hard-or-Calculus-IV-or
class Solution: def numberOfMatches(self, n: int) -> int: return n - 1
count-of-matches-in-tournament
| Python | IMO Standard Math | Very Hard | Calculus IV |
YudoTLE
1
17
count of matches in tournament
1,688
0.832
Easy
24,419
https://leetcode.com/problems/count-of-matches-in-tournament/discuss/2020038/I-don't-know-why-but-it-works-and-AC
class Solution: def numberOfMatches(self, n: int) -> int: return n-1
count-of-matches-in-tournament
I don't know why, but it works and AC
TUL
1
61
count of matches in tournament
1,688
0.832
Easy
24,420
https://leetcode.com/problems/count-of-matches-in-tournament/discuss/2800586/Simple-Python-Code
class Solution: def numberOfMatches(self, n: int) -> int: ans = 0 while(1): if n%2==0: ans+=n//2 n = n//2 if n==1: break else: ans+=(n-1)//2 n = (n-1)//2+1 if n==1: break return ans
count-of-matches-in-tournament
Simple Python Code
shaansrivastava
0
1
count of matches in tournament
1,688
0.832
Easy
24,421
https://leetcode.com/problems/count-of-matches-in-tournament/discuss/2784378/Python3-Easy-To-Understand-Solution
class Solution: def numberOfMatches(self, n: int) -> int: matches = 0 while n > 1: if n/2 == 0: matches += n/2 n = n/2 else: matches += (n-1)/2 n = ((n-1)/2) + 1 return round(matches)
count-of-matches-in-tournament
Python3 Easy To Understand Solution
ChuaShinYing
0
3
count of matches in tournament
1,688
0.832
Easy
24,422
https://leetcode.com/problems/count-of-matches-in-tournament/discuss/2772272/Python-easy-to-understand-solution
class Solution: def numberOfMatches(self, n: int) -> int: match=0 while(n!=1): if n%2==0: match+=n/2 n=n/2 else: match+=int(n/2) n=int(n/2)+1 return match
count-of-matches-in-tournament
Python easy to understand solution
user7798V
0
2
count of matches in tournament
1,688
0.832
Easy
24,423
https://leetcode.com/problems/count-of-matches-in-tournament/discuss/2709698/Python3-solution-few-lines
class Solution: def numberOfMatches(self, n: int) -> int: matches = 0 while n > 1: print('n:',n,'matches:',matches) matches += n / 2 n -= n / 2 print(' n:',n,'matches:',matches) return int(matches)
count-of-matches-in-tournament
Python3 solution - few lines
sipi09
0
4
count of matches in tournament
1,688
0.832
Easy
24,424
https://leetcode.com/problems/count-of-matches-in-tournament/discuss/2551706/simple-python-solution
class Solution: def numberOfMatches(self, n: int) -> int: matches_played = 0 while n > 1: if n % 2 == 0: matches_played += (n / 2) n -= (n / 2) elif n % 2 != 0: matches = n // 2 matches_played += matches n -= matches return int(matches_played)
count-of-matches-in-tournament
simple python solution
maschwartz5006
0
24
count of matches in tournament
1,688
0.832
Easy
24,425
https://leetcode.com/problems/count-of-matches-in-tournament/discuss/2476629/Python-simple-while-loop
class Solution: def numberOfMatches(self, n: int) -> int: matches = 0 while n != 1: if n % 2 == 0: n //= 2 matches += n else: matches += (n-1)//2 n = (n-1)//2 + 1 return matches
count-of-matches-in-tournament
Python simple while loop
aruj900
0
17
count of matches in tournament
1,688
0.832
Easy
24,426
https://leetcode.com/problems/count-of-matches-in-tournament/discuss/2449301/Python3-Simple-with-explanation
class Solution: def numberOfMatches(self, n: int) -> int: # Matches records total matches played, n represents teams remaining # Thankfully, most of the math is actually provided for us in the problem description! matches = 0 while n != 1: # If even number of teams remaining, add n/2 matches, resulting in n/2 teams advancing if n % 2 == 0: matches += n/2 n /= 2 # If odd number of teams remaining, add (n-1)/2 matches, and (n-1)/2 + 1 teams advancing else: matches += (n-1)/2 n = (n-1)/2 + 1 return int(matches)
count-of-matches-in-tournament
[Python3] Simple with explanation
connorthecrowe
0
26
count of matches in tournament
1,688
0.832
Easy
24,427
https://leetcode.com/problems/count-of-matches-in-tournament/discuss/2394616/C%2B%2BPython-O(N)-solution
class Solution: def numberOfMatches(self, n: int) -> int: ans=0 while(n!=1): if(n%2==0): ans = ans+(n//2) n=n/2 else: ans = ans+(n-1)//2 n=1+(n-1)//2 return int(ans)
count-of-matches-in-tournament
C++/Python - O(N) solution
arpit3043
0
16
count of matches in tournament
1,688
0.832
Easy
24,428
https://leetcode.com/problems/count-of-matches-in-tournament/discuss/2388471/Python3-or-Recursion-or-faster-than-91.2
class Solution: def numberOfMatches(self, n: int) -> int: if n == 1: return 0 elif n == 2: return 1 elif n % 2 == 0: return int(n / 2) + self.numberOfMatches(n / 2) elif n % 2 == 1: return int(n // 2) + self.numberOfMatches(n // 2 + 1)
count-of-matches-in-tournament
Python3 | Recursion | faster than 91.2%
Sergei_Gusev
0
23
count of matches in tournament
1,688
0.832
Easy
24,429
https://leetcode.com/problems/count-of-matches-in-tournament/discuss/2104021/Python-solution
class Solution: def numberOfMatches(self, n: int) -> int: ans = 0 while n != 1: if n%2 == 0: ans += n//2 n = n//2 else: ans += (n-1)//2 n = ((n-1)//2)+1 return ans
count-of-matches-in-tournament
Python solution
StikS32
0
53
count of matches in tournament
1,688
0.832
Easy
24,430
https://leetcode.com/problems/count-of-matches-in-tournament/discuss/2098303/Using-dp
class Solution: def numberOfMatches(self, n: int) -> int: dp = [0] * n for i in range(1, n): dp[i] = dp[i - 1] + 1 return dp[n - 1]
count-of-matches-in-tournament
Using dp
andrewnerdimo
0
20
count of matches in tournament
1,688
0.832
Easy
24,431
https://leetcode.com/problems/count-of-matches-in-tournament/discuss/2091879/Simple-python-solution
class Solution: def numberOfMatches(self, n: int) -> int: self.matches = 0 self.n = n def games(): if n % 2 == 0: self.matches += self.n / 2 self.n = self.n / 2 else: self.matches += (self.n - 1) / 2 self.n = (self.n - 1) / 2 + 1 while self.n > 1: games() return round(self.matches)
count-of-matches-in-tournament
Simple python solution
shreeruparel
0
51
count of matches in tournament
1,688
0.832
Easy
24,432
https://leetcode.com/problems/count-of-matches-in-tournament/discuss/2043358/Python3-using-recursive
class Solution: def numberOfMatches(self, n: int) -> int: def count(total, current): if current == 1: return int(total) else: total = total current = current if current % 2 == 0: total += current / 2 current = current / 2 else: total += (current - 1) / 2 current = (current - 1) / 2 + 1 return count(total, current) return count(0, n)
count-of-matches-in-tournament
[Python3] using recursive
Shiyinq
0
24
count of matches in tournament
1,688
0.832
Easy
24,433
https://leetcode.com/problems/count-of-matches-in-tournament/discuss/1879814/Python3-Solution
class Solution: def numberOfMatches(self, n: int) -> int: matches = 0 while n != 1: match = n //2 matches += match n = n - match return matches
count-of-matches-in-tournament
[Python3] Solution
natscripts
0
47
count of matches in tournament
1,688
0.832
Easy
24,434
https://leetcode.com/problems/count-of-matches-in-tournament/discuss/1846074/1-Line-Python-Solution-oror-65-Faster-oror-Memory-less-than-40
class Solution: def numberOfMatches(self, n: int) -> int: return n-1
count-of-matches-in-tournament
1-Line Python Solution || 65% Faster || Memory less than 40%
Taha-C
0
33
count of matches in tournament
1,688
0.832
Easy
24,435
https://leetcode.com/problems/count-of-matches-in-tournament/discuss/1792731/Python-Solution-83-Runtime-98-Memory-Easy-to-Understand
class Solution: def numberOfMatches(self, n: int) -> int: matches = 0 while n != 1: if n % 2 == 0: matches += n/2 n = n/2 else: matches += (n-1)/2 n = ((n-1)/2)+1 return int(matches)
count-of-matches-in-tournament
Python Solution - 83% Runtime, 98% Memory, Easy to Understand
sammenary
0
33
count of matches in tournament
1,688
0.832
Easy
24,436
https://leetcode.com/problems/count-of-matches-in-tournament/discuss/1766328/Python-dollarolution
class Solution: def numberOfMatches(self, n: int) -> int: return n - 1
count-of-matches-in-tournament
Python $olution
AakRay
0
45
count of matches in tournament
1,688
0.832
Easy
24,437
https://leetcode.com/problems/count-of-matches-in-tournament/discuss/1568392/Easy-one-liner-oror-Some-better-explanation-is-welcome
class Solution: def numberOfMatches(self, n: int) -> int: return n-1
count-of-matches-in-tournament
Easy one liner || Some better explanation is welcome
ana_2kacer
0
84
count of matches in tournament
1,688
0.832
Easy
24,438
https://leetcode.com/problems/count-of-matches-in-tournament/discuss/1491142/Python-solution-O(N)-understandable-for-BEGINNERS-like-me!!!
class Solution: def numberOfMatches(self, n: int) -> int: teams=n matches=0 while(teams!=1): if(teams%2==0): matches+=teams//2 teams//=2 else: matches+=(teams-1)//2 teams = (teams-1)//2 + 1 return matches
count-of-matches-in-tournament
Python solution O(N) understandable for BEGINNERS like me!!!
kabiland
0
79
count of matches in tournament
1,688
0.832
Easy
24,439
https://leetcode.com/problems/count-of-matches-in-tournament/discuss/1071479/Python-Shortcut-Solution
class Solution: def numberOfMatches(self, n: int) -> int: return n-1
count-of-matches-in-tournament
Python Shortcut Solution
aishwaryanathanii
0
107
count of matches in tournament
1,688
0.832
Easy
24,440
https://leetcode.com/problems/count-of-matches-in-tournament/discuss/970375/Python-3-or-Math-O(lgN)-or-Explanation
class Solution: def numberOfMatches(self, n: int) -> int: ans = 0 while n != 1: ans += (n+1) // 2 n //= 2 return ans
count-of-matches-in-tournament
Python 3 | Math O(lgN) | Explanation
idontknoooo
0
105
count of matches in tournament
1,688
0.832
Easy
24,441
https://leetcode.com/problems/count-of-matches-in-tournament/discuss/1451251/Simple-Python-solution-faster-than-95
class Solution: def numberOfMatches(self, n: int) -> int: matches=0 while n > 1: if n%2==0: matches+=int(n/2) n = n/2 else: matches+=int((n-1)/2) n = (n+1)/2 return matches
count-of-matches-in-tournament
Simple Python solution faster than 95%
sid_upadhyayula
-1
76
count of matches in tournament
1,688
0.832
Easy
24,442
https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/discuss/2202648/C%2B%2BJavaPython-Easy-One-liner-with-explanation
class Solution: def minPartitions(self, n: str) -> int: return int(max(n))
partitioning-into-minimum-number-of-deci-binary-numbers
[C++/Java/Python]- Easy One liner with explanation
constantine786
52
3,800
partitioning into minimum number of deci binary numbers
1,689
0.896
Medium
24,443
https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/discuss/1767864/Python-3-(80ms)-or-Simple-One-Liner-or-Easy-to-Understand
class Solution: def minPartitions(self, n: str) -> int: return max(n)
partitioning-into-minimum-number-of-deci-binary-numbers
Python 3 (80ms) | Simple One-Liner | Easy to Understand
MrShobhit
6
265
partitioning into minimum number of deci binary numbers
1,689
0.896
Medium
24,444
https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/discuss/2205199/Python3-oror-Can't-believe-this-solution-oror-99-Faster
class Solution: def minPartitions(self, n: str) -> int: return max(n)
partitioning-into-minimum-number-of-deci-binary-numbers
Python3 || Can't believe this solution || 99 % Faster 🚀
Dewang_Patil
4
141
partitioning into minimum number of deci binary numbers
1,689
0.896
Medium
24,445
https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/discuss/1397665/Just-two-words!-or-Explained
class Solution: def minPartitions(self, n: str) -> int: return max(n)
partitioning-into-minimum-number-of-deci-binary-numbers
Just two words! | Explained
shraddhapp
3
239
partitioning into minimum number of deci binary numbers
1,689
0.896
Medium
24,446
https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/discuss/2509867/Python-one-line-code
class Solution: def minPartitions(self, n: str) -> int: return max(n)
partitioning-into-minimum-number-of-deci-binary-numbers
Python - one line code
xfuxad00
2
87
partitioning into minimum number of deci binary numbers
1,689
0.896
Medium
24,447
https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/discuss/1763877/Simple-Python-One-Liner
class Solution: def minPartitions(self, n: str) -> int: return int(max(n))
partitioning-into-minimum-number-of-deci-binary-numbers
Simple Python One Liner
anCoderr
2
68
partitioning into minimum number of deci binary numbers
1,689
0.896
Medium
24,448
https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/discuss/1232187/PythonPython3-solution-with-explanation
class Solution: def minPartitions(self, n: str) -> int: numLis = map(int,list(n)) return max(numLis)
partitioning-into-minimum-number-of-deci-binary-numbers
Python/Python3 solution with explanation
prasanthksp1009
2
142
partitioning into minimum number of deci binary numbers
1,689
0.896
Medium
24,449
https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/discuss/2505858/Python-1-liner
class Solution: def minPartitions(self, n: str) -> int: return int(max(n))
partitioning-into-minimum-number-of-deci-binary-numbers
Python 1 liner
SouravSingh49
1
46
partitioning into minimum number of deci binary numbers
1,689
0.896
Medium
24,450
https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/discuss/1282971/One-Liner-Intuitive
class Solution: def minPartitions(self, n: str) -> int: return max(n)
partitioning-into-minimum-number-of-deci-binary-numbers
One Liner - Intuitive
vatsea
1
104
partitioning into minimum number of deci binary numbers
1,689
0.896
Medium
24,451
https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/discuss/970367/Python-3-or-One-liner-Greedy-or-Explanation
class Solution: def minPartitions(self, n: str) -> int: return int(sorted(list(set(n)), reverse=True)[0])
partitioning-into-minimum-number-of-deci-binary-numbers
Python 3 | One-liner Greedy | Explanation
idontknoooo
1
144
partitioning into minimum number of deci binary numbers
1,689
0.896
Medium
24,452
https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/discuss/2806102/Python-or-easy-code
class Solution: def minPartitions(self, n: str) -> int: max = 0 for i in n: if int(i) > max: max = int(i) return max
partitioning-into-minimum-number-of-deci-binary-numbers
Python | easy code
bhuvneshwar906
0
1
partitioning into minimum number of deci binary numbers
1,689
0.896
Medium
24,453
https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/discuss/2790377/Understandable-solution-**
class Solution: def minPartitions(self, n: str) -> int: if n is None: return 0 maxi=0 for ch in n: if int(ch) > max: maxi=int(ch) return maxi
partitioning-into-minimum-number-of-deci-binary-numbers
Understandable solution ʕ•ᴥ•ʔ
katerrinss
0
5
partitioning into minimum number of deci binary numbers
1,689
0.896
Medium
24,454
https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/discuss/2786348/Python-One-Line-Solution.
class Solution: def minPartitions(self, n: str) -> int: return max(n)
partitioning-into-minimum-number-of-deci-binary-numbers
[Python🐍] One Line Solution.
Saransh_MG
0
4
partitioning into minimum number of deci binary numbers
1,689
0.896
Medium
24,455
https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/discuss/2767480/Python-1-line-code
class Solution: def minPartitions(self, n: str) -> int: return max(list(map(int,list(str(n)))))
partitioning-into-minimum-number-of-deci-binary-numbers
Python 1 line code
kumar_anand05
0
3
partitioning into minimum number of deci binary numbers
1,689
0.896
Medium
24,456
https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/discuss/2763944/Python-one-line-answer
class Solution: def minPartitions(self, n: str) -> int: return max(n)
partitioning-into-minimum-number-of-deci-binary-numbers
Python one line answer
iakylbek
0
3
partitioning into minimum number of deci binary numbers
1,689
0.896
Medium
24,457
https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/discuss/2763926/Python-one-line-answer
class Solution: def minPartitions(self, n: str) -> int: return max(list(str(n)))
partitioning-into-minimum-number-of-deci-binary-numbers
Python one line answer
iakylbek
0
1
partitioning into minimum number of deci binary numbers
1,689
0.896
Medium
24,458
https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/discuss/2763717/Python3-Solution-with-using-the-maximum-digit-search
class Solution: def minPartitions(self, n: str) -> int: max_digit = -1 for char in n: max_digit = max(max_digit, int(char)) return max_digit
partitioning-into-minimum-number-of-deci-binary-numbers
[Python3] Solution with using the maximum digit search
maosipov11
0
1
partitioning into minimum number of deci binary numbers
1,689
0.896
Medium
24,459
https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/discuss/2698582/Python3-One-Liner-with-explanation
class Solution: def minPartitions(self, n: str) -> int: return max(n)
partitioning-into-minimum-number-of-deci-binary-numbers
[Python3] One Liner with explanation
connorthecrowe
0
7
partitioning into minimum number of deci binary numbers
1,689
0.896
Medium
24,460
https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/discuss/2595390/Simplest-Python-solutionororone-liner
class Solution: def minPartitions(self, n: str) -> int: return max(n)
partitioning-into-minimum-number-of-deci-binary-numbers
Simplest Python solution||one-liner
shersam999
0
35
partitioning into minimum number of deci binary numbers
1,689
0.896
Medium
24,461
https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/discuss/2234347/faster-than-94.02-of-Python3-online-submissions-less-than-85.48-of-Python3-online-submissions
class Solution: def minPartitions(self, n: str) -> int: max_num = str(0) for i in n: if( i > max_num): max_num = i if(max_num=='9'): return max_num return max_num
partitioning-into-minimum-number-of-deci-binary-numbers
faster than 94.02% of Python3 online submissions less than 85.48% of Python3 online submissions
dinesh3reddy
0
94
partitioning into minimum number of deci binary numbers
1,689
0.896
Medium
24,462
https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/discuss/2206443/Easy-python-solution-or-Partitioning-Into-Minimum-Number-Of-Deci-Binary-Numbers
class Solution: def minPartitions(self, n: str) -> int: return max(n)
partitioning-into-minimum-number-of-deci-binary-numbers
Easy python solution | Partitioning Into Minimum Number Of Deci-Binary Numbers
nishanrahman1994
0
11
partitioning into minimum number of deci binary numbers
1,689
0.896
Medium
24,463
https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/discuss/2205891/Python-or-One-Liner-Solution-or-Easy-and-Understanding
class Solution: def minPartitions(self, n: str) -> int: return int(max(n)) Please upvote if you found it better
partitioning-into-minimum-number-of-deci-binary-numbers
Python | One Liner Solution | Easy & Understanding
backpropagator
0
13
partitioning into minimum number of deci binary numbers
1,689
0.896
Medium
24,464
https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/discuss/2205795/Python-solution-with-explanation.
class Solution: def minPartitions(self, n: str) -> int: # First we'll initialize a variable as 0, to find the maximum digit. max_d = 0 # Iterate in the string for i in n: # We'll take the digit of present iteration. no = int(i) # We'll find the maximum from both the variables. max_d = max(max_d, no) # We'll return the max_d as it is the maximum number from the digit. return max_d
partitioning-into-minimum-number-of-deci-binary-numbers
Python solution with explanation.
yashkumarjha
0
11
partitioning into minimum number of deci binary numbers
1,689
0.896
Medium
24,465
https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/discuss/2204460/Python-Solution-4-lines-100-Running-(easy-understanding)
class Solution: def minPartitions(self, n: str) -> int: ns = str(n) maxx = 0 for char in ns: maxx = max(int(char),maxx) return maxx
partitioning-into-minimum-number-of-deci-binary-numbers
Python Solution - 4 lines -100% Running (easy understanding)
hackerbb
0
12
partitioning into minimum number of deci binary numbers
1,689
0.896
Medium
24,466
https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/discuss/2203928/Simplest-Python-1-liner
class Solution: def minPartitions(self, n: str) -> int: return max(list(map(int, (x for x in n))))
partitioning-into-minimum-number-of-deci-binary-numbers
Simplest Python 1 - liner
Vaibhav7860
0
10
partitioning into minimum number of deci binary numbers
1,689
0.896
Medium
24,467
https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/discuss/2203596/simple-python-solution-is-this-really-a-%22Medium%22-problem
class Solution: def minPartitions(self, n: str) -> int: return max(int(c) for c in n)
partitioning-into-minimum-number-of-deci-binary-numbers
simple python solution - is this really a "Medium" problem?
hakatom
0
9
partitioning into minimum number of deci binary numbers
1,689
0.896
Medium
24,468
https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/discuss/2203469/Python-Trivial-solution-(and-comparison-with-alternative)
class Solution: def minPartitions(self, n: str) -> int: # Complexity: # - Time: O(N) # where N is the number of digits # - Space: O(1) return int(max(n))
partitioning-into-minimum-number-of-deci-binary-numbers
[Python] Trivial solution (and comparison with alternative)
RegInt
0
2
partitioning into minimum number of deci binary numbers
1,689
0.896
Medium
24,469
https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/discuss/2203469/Python-Trivial-solution-(and-comparison-with-alternative)
class Solution: def minPartitions(self, n: str) -> int: # Complexity: # - Time: O(N) # where N is the number of digits # - Space: O(1) for char in ["9", "8", "7", "6", "5", "4", "3", "2"]: if char in n: return int(char) return 1
partitioning-into-minimum-number-of-deci-binary-numbers
[Python] Trivial solution (and comparison with alternative)
RegInt
0
2
partitioning into minimum number of deci binary numbers
1,689
0.896
Medium
24,470
https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/discuss/2203189/One-Line-Code-Ans
class Solution(object): def minPartitions(self, n): return max (n)
partitioning-into-minimum-number-of-deci-binary-numbers
One Line Code Ans
vaibhav0077
0
5
partitioning into minimum number of deci binary numbers
1,689
0.896
Medium
24,471
https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/discuss/2202614/Python-one-liner
class Solution: def minPartitions(self, n: str) -> int: return int(max(n))
partitioning-into-minimum-number-of-deci-binary-numbers
Python one-liner
blue_sky5
0
23
partitioning into minimum number of deci binary numbers
1,689
0.896
Medium
24,472
https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/discuss/2123246/Python-Solution
class Solution: def minPartitions(self, n: str) -> int: ans = 0 for i in n: if int(i) > ans: ans = int(i) return ans
partitioning-into-minimum-number-of-deci-binary-numbers
Python Solution
MS1301
0
79
partitioning into minimum number of deci binary numbers
1,689
0.896
Medium
24,473
https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/discuss/1899384/Python-one-line-solution
class Solution: def minPartitions(self, n: str) -> int: return max(list(n))
partitioning-into-minimum-number-of-deci-binary-numbers
Python one line solution
alishak1999
0
58
partitioning into minimum number of deci binary numbers
1,689
0.896
Medium
24,474
https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/discuss/1841083/One-Liner-or-Python-3-or-Explained
class Solution: def minPartitions(self, n: str) -> int: return max(n)
partitioning-into-minimum-number-of-deci-binary-numbers
One Liner | Python 3 | Explained
yuval8987
0
112
partitioning into minimum number of deci binary numbers
1,689
0.896
Medium
24,475
https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/discuss/1817762/Python-One-line-runtime-beats-96.86-of-python3-submissions.
class Solution: def minPartitions(self, n: str) -> int: return max(n)
partitioning-into-minimum-number-of-deci-binary-numbers
[Python] One line runtime beats 96.86 % of python3 submissions.
crazypuppy
0
72
partitioning into minimum number of deci binary numbers
1,689
0.896
Medium
24,476
https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/discuss/1671430/PythonC%2B%2B-Solutions
class Solution: def minPartitions(self, n: str) -> int: return max(n)
partitioning-into-minimum-number-of-deci-binary-numbers
[Python/C++] Solutions
light_1
0
98
partitioning into minimum number of deci binary numbers
1,689
0.896
Medium
24,477
https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/discuss/1656015/Python-Easy-and-Clean-Python-Solution
class Solution: def minPartitions(self, n: str) -> int: result = [] for num in n: result.append( int( num ) ) return max( result )
partitioning-into-minimum-number-of-deci-binary-numbers
[ Python ] ✅✅ Easy and Clean Python Solution 🥳✌👍
ASHOK_KUMAR_MEGHVANSHI
0
110
partitioning into minimum number of deci binary numbers
1,689
0.896
Medium
24,478
https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/discuss/1616384/faster-than-91
class Solution: def minPartitions(self, n: str) -> int: max_n = max(n) if max_n == 9: return 9 return max_n
partitioning-into-minimum-number-of-deci-binary-numbers
faster than %91
fatmakahveci
0
55
partitioning into minimum number of deci binary numbers
1,689
0.896
Medium
24,479
https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/discuss/1616381/python3-fast-and-simple
class Solution: def minPartitions(self, n: str) -> int: return max(n)
partitioning-into-minimum-number-of-deci-binary-numbers
python3 fast and simple
fatmakahveci
0
39
partitioning into minimum number of deci binary numbers
1,689
0.896
Medium
24,480
https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/discuss/1102379/Python3-single-Line-Answer
class Solution: def minPartitions(self, n: str) -> int: return int(max(n))
partitioning-into-minimum-number-of-deci-binary-numbers
Python3 single Line Answer
abhisek_
0
96
partitioning into minimum number of deci binary numbers
1,689
0.896
Medium
24,481
https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/discuss/1058944/One-Word-Answer-or-Python
class Solution: def minPartitions(self, n: str) -> int: return max(map(int,n))
partitioning-into-minimum-number-of-deci-binary-numbers
One Word Answer | Python
himansh96
0
125
partitioning into minimum number of deci binary numbers
1,689
0.896
Medium
24,482
https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/discuss/974778/Python3-max-of-digits
class Solution: def minPartitions(self, n: str) -> int: return int(max(n))
partitioning-into-minimum-number-of-deci-binary-numbers
[Python3] max of digits
ye15
0
57
partitioning into minimum number of deci binary numbers
1,689
0.896
Medium
24,483
https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/discuss/970625/Intuitive-approach-by-finding-max-number-among-digits
class Solution: def minPartitions(self, n: str) -> int: if len(n) == 1: return n else: return max([int(c) for c in n])
partitioning-into-minimum-number-of-deci-binary-numbers
Intuitive approach by finding max number among digits
puremonkey2001
0
50
partitioning into minimum number of deci binary numbers
1,689
0.896
Medium
24,484
https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/discuss/970356/Python-one-liner
class Solution: def minPartitions(self, n: str) -> int: return int(max(n))
partitioning-into-minimum-number-of-deci-binary-numbers
Python one-liner
Frikster
0
62
partitioning into minimum number of deci binary numbers
1,689
0.896
Medium
24,485
https://leetcode.com/problems/stone-game-vii/discuss/971804/Python3-Easy-code-with-explanation-DP
class Solution: def stoneGameVII(self, stones: List[int]) -> int: dp = [[0 for _ in range(len(stones))] for _ in range(len(stones))] # dp table n x n run_sum = [0] # running sum -> sum [i..j] = run_sum[j] - run_sum[i] s = 0 ## Calculation of running sum for i in stones: s += i run_sum.append(s) n = len(stones) for k in range(1, n): # no. of stones left for i in range(0, n - k): # from each starting point remove_i_stone = (run_sum[i+k+1] - run_sum[i+1]) # score after removing i th stone remove_j_stone = (run_sum[i+k] - run_sum[i]) # score after removing j th stone if (n-(k+1))%2 == 0: # alice's move dp[i][i+k] = max(remove_i_stone + dp[i+1][i+k], remove_j_stone + dp[i][i+k-1]) else: # bob's move dp[i][i+k] = min(-remove_i_stone + dp[i+1][i+k], - remove_j_stone + dp[i][i+k-1]) return dp[0][n - 1]
stone-game-vii
[Python3] Easy code with explanation - DP
mihirrane
9
851
stone game vii
1,690
0.586
Medium
24,486
https://leetcode.com/problems/stone-game-vii/discuss/974789/Python3-dp
class Solution: def stoneGameVII(self, stones: List[int]) -> int: prefix = [0] for x in stones: prefix.append(prefix[-1] + x) @lru_cache(1000) def fn(lo, hi): """Return difference of scores.""" if lo == hi: return 0 return max(prefix[hi] - prefix[lo+1] - fn(lo+1, hi), prefix[hi-1] - prefix[lo] - fn(lo, hi-1)) return fn(0, len(stones))
stone-game-vii
[Python3] dp
ye15
1
113
stone game vii
1,690
0.586
Medium
24,487
https://leetcode.com/problems/stone-game-vii/discuss/974789/Python3-dp
class Solution: def stoneGameVII(self, stones: List[int]) -> int: prefix = [0] for x in stones: prefix.append(prefix[-1] + x) @lru_cache(None) def fn(lo, hi): """Return difference of scores.""" if lo == hi: return 0 return max(prefix[hi] - prefix[lo+1] - fn(lo+1, hi), prefix[hi-1] - prefix[lo] - fn(lo, hi-1)) ans = fn(0, len(stones)) fn.cache_clear() return ans
stone-game-vii
[Python3] dp
ye15
1
113
stone game vii
1,690
0.586
Medium
24,488
https://leetcode.com/problems/stone-game-vii/discuss/974789/Python3-dp
class Solution: def stoneGameVII(self, stones: List[int]) -> int: prefix = [0] for x in stones: prefix.append(prefix[-1] + x) n = len(stones) dp = [[0]*n for _ in range(n)] for i in reversed(range(n)): for j in range(i+1, n): dp[i][j] = max(prefix[j+1] - prefix[i+1] - dp[i+1][j], prefix[j] - prefix[i] - dp[i][j-1]) return dp[0][-1]
stone-game-vii
[Python3] dp
ye15
1
113
stone game vii
1,690
0.586
Medium
24,489
https://leetcode.com/problems/stone-game-vii/discuss/1741895/Simple-dp-solution-beats-89-8-lines
class Solution: def stoneGameVII(self, stones: List[int]) -> int: n = len(stones) df = [[0 for _ in range(n)] for _ in range(n)] for i in range(n-2,-1,-1): s=stones[i] for j in range(i+1,n): s+=stones[j] df[i][j] = max(s-stones[i]-df[i+1][j],s-stones[j]-df[i][j-1]) return df[0][n-1]
stone-game-vii
Simple dp solution beats 89%, 8 lines
IsaacQ135
0
85
stone game vii
1,690
0.586
Medium
24,490
https://leetcode.com/problems/stone-game-vii/discuss/1181599/python-simple-sol-or-MCM
class Solution: def stoneGameVII(self, stones: List[int]) -> int: n=len(stones) total=[stones[0]] for i in stones[1:]: total.append(total[-1]+i) new=[[0 for i in range(0,n)] for i in range(0,n)] for gap in range(1,n): for j in range(gap,n): i=j-gap if gap==1: new[i][j]=max(stones[i],stones[j]) else: s1=total[j]-total[i] if i==0: s2=total[j-1] else: s2=total[j-1]-total[i-1] new[i][j]=max(s1-new[i+1][j],s2-new[i][j-1]) return new[0][-1]
stone-game-vii
python simple sol | MCM
heisenbarg
0
124
stone game vii
1,690
0.586
Medium
24,491
https://leetcode.com/problems/maximum-height-by-stacking-cuboids/discuss/970397/Python-3-or-DP-Sort-O(N2)-or-Explanation
class Solution: def maxHeight(self, cuboids: List[List[int]]) -> int: cuboids = sorted([sorted(cub) for cub in cuboids], reverse=True) # sort LxWxH in cube, then sort cube reversely ok = lambda x, y: (x[0] >= y[0] and x[1] >= y[1] and x[2] >= y[2]) # make a lambda function to verify whether y can be put on top of x n = len(cuboids) dp = [cu[2] for cu in cuboids] # create dp array ans = max(dp) for i in range(1, n): # iterate over each cube for j in range(i): # compare with previous calculated cube if ok(cuboids[j], cuboids[i]): # update dp[i] if cube[i] can be put on top of cube[j] dp[i] = max(dp[i], dp[j] + cuboids[i][2]) # always get the maximum ans = max(ans, dp[i]) # record the largest value return ans
maximum-height-by-stacking-cuboids
Python 3 | DP, Sort, O(N^2) | Explanation
idontknoooo
7
425
maximum height by stacking cuboids
1,691
0.541
Hard
24,492
https://leetcode.com/problems/maximum-height-by-stacking-cuboids/discuss/1025769/python-3-solution
class Solution: def maxHeight(self, cuboids: List[List[int]]) -> int: dp=[0]*len(cuboids) max_value=0 for i in range(len(cuboids)): cuboids[i].sort() cuboids.sort() for i in range(len(cuboids)): dp[i]=cuboids[i][2] for j in range(i): if cuboids[i][0]>=cuboids[j][0] and cuboids[i][1]>=cuboids[j][1] and cuboids[i][2]>=cuboids[j][2]: dp[i]=max(dp[i],dp[j]+cuboids[i][2]) if dp[i]>max_value: max_value=dp[i] return max_value
maximum-height-by-stacking-cuboids
python 3 solution
AchalGupta
2
195
maximum height by stacking cuboids
1,691
0.541
Hard
24,493
https://leetcode.com/problems/maximum-height-by-stacking-cuboids/discuss/2831321/Python-Solution-Maximum-Height-by-Stacking-Cuboids-Dynamic-Programming
class Solution: def maxHeight(self, cuboids: List[List[int]]) -> int: def check(base, box): if base[0] >= box[0] and base[1] >= box[1] and base[2] >= box[2]: return True else: return False [i.sort() for i in cuboids] cuboids.sort() n = len(cuboids) curr = [0 for i in range(n+1)] nex = [0 for i in range(n+1)] for index in range(n-1,-1,-1): for parent in range(index - 1,-2,-1): take = 0 if parent == -1 or check(cuboids[index],cuboids[parent]): take = cuboids[index][2] + nex[index+1] dont = nex[parent+1] curr[parent+1] = max(take,dont) nex = curr return nex[0]
maximum-height-by-stacking-cuboids
Python Solution Maximum Height by Stacking Cuboids Dynamic Programming
sarthakchawande14
0
2
maximum height by stacking cuboids
1,691
0.541
Hard
24,494
https://leetcode.com/problems/maximum-height-by-stacking-cuboids/discuss/2557655/python-with-clean-explanation-with-example(dp-with-sort)
class Solution: def maxHeight(self, cuboids: List[List[int]]) -> int: n = len(cuboids) dp = [0]*n for c in cuboids: c.sort() cuboids.sort() for i in range(n): dp[i] = cuboids[i][2] for j in range(i): if cuboids[j][1] <= cuboids[i][1] and cuboids[j][2] <= cuboids[i][2]: dp[i] = max(dp[i],dp[j]+cuboids[i][2]) return max(dp)
maximum-height-by-stacking-cuboids
python with clean explanation with example(dp with sort)
Kurdush
0
87
maximum height by stacking cuboids
1,691
0.541
Hard
24,495
https://leetcode.com/problems/maximum-height-by-stacking-cuboids/discuss/2316548/python-3-or-simple-top-down-dp
class Solution: def maxHeight(self, cuboids: List[List[int]]) -> int: for cuboid in cuboids: cuboid.sort() cuboids.sort() n = len(cuboids) dp = [height for _, _, height in cuboids] for i in range(n - 1, -1, -1): for j in range(i + 1, n): if cuboids[i][1] <= cuboids[j][1] and cuboids[i][2] <= cuboids[j][2]: dp[i] = max(dp[i], cuboids[i][2] + dp[j]) return max(dp)
maximum-height-by-stacking-cuboids
python 3 | simple top-down dp
dereky4
0
58
maximum height by stacking cuboids
1,691
0.541
Hard
24,496
https://leetcode.com/problems/maximum-height-by-stacking-cuboids/discuss/974798/Python3-top-down-dp
class Solution: def maxHeight(self, cuboids: List[List[int]]) -> int: cuboids = sorted((sorted(x, reverse=True) for x in cuboids), reverse=True) @lru_cache(None) def fn(i, h, l, w): """Return max heights of stacking cuboids[i:].""" if i == len(cuboids): return 0 # no cuboids left hi, li, wi = cuboids[i] if hi <= h and li <= l and wi <= w: return max(hi + fn(i+1, hi, li, wi), fn(i+1, h, l, w)) else: return fn(i+1, h, l, w) return fn(0, inf, inf, inf)
maximum-height-by-stacking-cuboids
[Python3] top-down dp
ye15
0
108
maximum height by stacking cuboids
1,691
0.541
Hard
24,497
https://leetcode.com/problems/reformat-phone-number/discuss/978512/Python3-string-processing
class Solution: def reformatNumber(self, number: str) -> str: number = number.replace("-", "").replace(" ", "") # removing - and space ans = [] for i in range(0, len(number), 3): if len(number) - i != 4: ans.append(number[i:i+3]) else: ans.extend([number[i:i+2], number[i+2:]]) break return "-".join(ans)
reformat-phone-number
[Python3] string processing
ye15
13
676
reformat phone number
1,694
0.649
Easy
24,498
https://leetcode.com/problems/reformat-phone-number/discuss/1477165/Cut-the-clean-string-into-pieces-88-speed
class Solution: def reformatNumber(self, number: str) -> str: s = number.replace(" ", "").replace("-", "") pieces = list() while s: if len(s) == 2: pieces.append(s) break elif len(s) == 4: pieces.append(s[:2]) pieces.append(s[2:]) break else: pieces.append(s[:3]) s = s[3:] return "-".join(pieces)
reformat-phone-number
Cut the clean string into pieces, 88% speed
EvgenySH
2
149
reformat phone number
1,694
0.649
Easy
24,499