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-score-from-performing-multiplication-operations/discuss/1075495/Python3-bottom-up-dp | class Solution:
def maximumScore(self, nums: List[int], multipliers: List[int]) -> int:
n, m = len(nums), len(multipliers)
dp = [[0]*m for _ in range(m+1)]
for i in reversed(range(m)):
for j in range(i, m):
k = i + m - j - 1
dp[i][j] = ma... | maximum-score-from-performing-multiplication-operations | [Python3] bottom-up dp | ye15 | 64 | 5,800 | maximum score from performing multiplication operations | 1,770 | 0.366 | Hard | 25,400 |
https://leetcode.com/problems/maximum-score-from-performing-multiplication-operations/discuss/1075495/Python3-bottom-up-dp | class Solution:
def maximumScore(self, nums: List[int], multipliers: List[int]) -> int:
@lru_cache(2000)
def fn(lo, hi, k):
"""Return max score from nums[lo:hi+1]."""
if k == len(multipliers): return 0
return max(nums[lo] * multipliers[k] + fn(lo+1, hi, k... | maximum-score-from-performing-multiplication-operations | [Python3] bottom-up dp | ye15 | 64 | 5,800 | maximum score from performing multiplication operations | 1,770 | 0.366 | Hard | 25,401 |
https://leetcode.com/problems/maximum-score-from-performing-multiplication-operations/discuss/1898863/Python-or-DP-or-With-Explanation | class Solution:
def maximumScore(self, nums: List[int], multipliers: List[int]) -> int:
# dp[i, left]: the maximum possible score if we have already done 'i' total operations
# and used 'left' numbers from the left side.
# if we know the used 'left' numbers from the leftmost side,
... | maximum-score-from-performing-multiplication-operations | Python | DP | With Explanation | Mikey98 | 6 | 620 | maximum score from performing multiplication operations | 1,770 | 0.366 | Hard | 25,402 |
https://leetcode.com/problems/maximum-score-from-performing-multiplication-operations/discuss/2581806/SIMPLE-PYTHON3-SOLUTION-98-less-memory-and-faster | class Solution:
def maximumScore(self, nums: List[int], multipliers: List[int]) -> int:
dp = [0] * (len(multipliers) + 1)
for m in range(len(multipliers) - 1, -1, -1):
pd = [0] * (m + 1)
for l in range(m, -1, -1):
pd[l] = max(dp[l + 1] + multipliers[m] * nums[... | maximum-score-from-performing-multiplication-operations | β
β SIMPLE PYTHON3 SOLUTION β
β98% less memory and faster | rajukommula | 2 | 313 | maximum score from performing multiplication operations | 1,770 | 0.366 | Hard | 25,403 |
https://leetcode.com/problems/maximum-score-from-performing-multiplication-operations/discuss/2625975/Python-Dp(Memoization)-Solution-oror-Faster-than-99.70-Python-Users | class Solution:
def maximumScore(self, nums: List[int], multi: List[int]) -> int:
memo = {}
def solve(b,e,i):
if i >= len(multi):
return 0
if (b,i) in memo:
return memo[(b,i)]
x= max(solve(b+1,e,i+1)+nums[b]*multi[i],solve(b,e-1,i+... | maximum-score-from-performing-multiplication-operations | Python Dp(Memoization) Solution || Faster than 99.70% Python Users | hoo__mann | 1 | 13 | maximum score from performing multiplication operations | 1,770 | 0.366 | Hard | 25,404 |
https://leetcode.com/problems/maximum-score-from-performing-multiplication-operations/discuss/2582278/Python-Accepted | class Solution:
def maximumScore(self, nums: List[int], mul: List[int]) -> int:
dp = [0] * (len(mul) + 1)
for m in range(len(mul) - 1, -1, -1):
pd = [0] * (m + 1)
for l in range(m, -1, -1):
pd[l] = max(dp[l + 1] + mul[m] * nums[l],
... | maximum-score-from-performing-multiplication-operations | Python Accepted β
| Khacker | 1 | 106 | maximum score from performing multiplication operations | 1,770 | 0.366 | Hard | 25,405 |
https://leetcode.com/problems/maximum-score-from-performing-multiplication-operations/discuss/2581827/Python3-Fighting-The-Ugly-Battle-of-TLE-and-MLE | class Solution1:
def maximumScore(self, nums: List[int], multipliers: List[int]) -> int:
"""MLE
"""
M, N = len(multipliers), len(nums)
@lru_cache(maxsize=None)
def dfs(lo: int, idx: int) -> int:
if idx == M:
return 0
hi = N - (... | maximum-score-from-performing-multiplication-operations | [Python3] Fighting The Ugly Battle of TLE and MLE | FanchenBao | 1 | 56 | maximum score from performing multiplication operations | 1,770 | 0.366 | Hard | 25,406 |
https://leetcode.com/problems/maximum-score-from-performing-multiplication-operations/discuss/2581827/Python3-Fighting-The-Ugly-Battle-of-TLE-and-MLE | class Solution2:
def maximumScore(self, nums: List[int], multipliers: List[int]) -> int:
"""Worse than solution 1, TLE
"""
M, N = len(multipliers), len(nums)
dp = [[-math.inf] * M for _ in range(M)]
def dfs(lo: int, idx: int) -> int:
if idx == M:
... | maximum-score-from-performing-multiplication-operations | [Python3] Fighting The Ugly Battle of TLE and MLE | FanchenBao | 1 | 56 | maximum score from performing multiplication operations | 1,770 | 0.366 | Hard | 25,407 |
https://leetcode.com/problems/maximum-score-from-performing-multiplication-operations/discuss/2581827/Python3-Fighting-The-Ugly-Battle-of-TLE-and-MLE | class Solution3:
def maximumScore(self, nums: List[int], multipliers: List[int]) -> int:
"""Bottom up 2D DP, TLE
"""
M, N = len(multipliers), len(nums)
dp = [[-math.inf] * M for _ in range(M)]
for idx in range(M - 1, -1, -1):
for lo in range(idx + 1):
... | maximum-score-from-performing-multiplication-operations | [Python3] Fighting The Ugly Battle of TLE and MLE | FanchenBao | 1 | 56 | maximum score from performing multiplication operations | 1,770 | 0.366 | Hard | 25,408 |
https://leetcode.com/problems/maximum-score-from-performing-multiplication-operations/discuss/2581827/Python3-Fighting-The-Ugly-Battle-of-TLE-and-MLE | class Solution4:
def maximumScore(self, nums: List[int], multipliers: List[int]) -> int:
"""Bottom up 2D DP, without if/else but with additional column and row
Exactly the same as official approach 3, TLE
"""
M, N = len(multipliers), len(nums)
dp = [[0] * (M + 1) for _ in ra... | maximum-score-from-performing-multiplication-operations | [Python3] Fighting The Ugly Battle of TLE and MLE | FanchenBao | 1 | 56 | maximum score from performing multiplication operations | 1,770 | 0.366 | Hard | 25,409 |
https://leetcode.com/problems/maximum-score-from-performing-multiplication-operations/discuss/2581827/Python3-Fighting-The-Ugly-Battle-of-TLE-and-MLE | class Solution5:
def maximumScore(self, nums: List[int], multipliers: List[int]) -> int:
"""Bottom up 1D DP, TLE
"""
M, N = len(multipliers), len(nums)
dp = [-math.inf] * M
for idx in range(M - 1, -1, -1):
for lo in range(idx + 1):
hi = N - (idx -... | maximum-score-from-performing-multiplication-operations | [Python3] Fighting The Ugly Battle of TLE and MLE | FanchenBao | 1 | 56 | maximum score from performing multiplication operations | 1,770 | 0.366 | Hard | 25,410 |
https://leetcode.com/problems/maximum-score-from-performing-multiplication-operations/discuss/2581827/Python3-Fighting-The-Ugly-Battle-of-TLE-and-MLE | class Solution6:
def maximumScore(self, nums: List[int], multipliers: List[int]) -> int:
"""Follow https://leetcode.com/problems/maximum-score-from-performing-multiplication-operations/discuss/1075448/Python-DP-Clear-the-Cache!
Clear cache between tests to avoid MLE.
Does avoid MLE, but no... | maximum-score-from-performing-multiplication-operations | [Python3] Fighting The Ugly Battle of TLE and MLE | FanchenBao | 1 | 56 | maximum score from performing multiplication operations | 1,770 | 0.366 | Hard | 25,411 |
https://leetcode.com/problems/maximum-score-from-performing-multiplication-operations/discuss/2581827/Python3-Fighting-The-Ugly-Battle-of-TLE-and-MLE | class Solution7:
def maximumScore(self, nums: List[int], multipliers: List[int]) -> int:
"""LeetCode 1770
Bottom up 1D DP, without if/else
What a journey!
* lru_cache memory limit exceeded (MLE)
* 2D DP top down time limit exceeded (TLE)
* 2D DP bottom up with if/e... | maximum-score-from-performing-multiplication-operations | [Python3] Fighting The Ugly Battle of TLE and MLE | FanchenBao | 1 | 56 | maximum score from performing multiplication operations | 1,770 | 0.366 | Hard | 25,412 |
https://leetcode.com/problems/maximum-score-from-performing-multiplication-operations/discuss/2581786/python-3-or-optimal-dp-or-O(m2)O(m) | class Solution:
def maximumScore(self, nums: List[int], multipliers: List[int]) -> int:
n, m = len(nums), len(multipliers)
prev = [0] * (m + 1)
for i in range(m - 1, -1, -1):
cur = [0] * (i + 2)
for left in range(i, -1, -1):
right = n + left - i - 1
... | maximum-score-from-performing-multiplication-operations | python 3 | optimal dp | O(m^2)/O(m) | dereky4 | 1 | 116 | maximum score from performing multiplication operations | 1,770 | 0.366 | Hard | 25,413 |
https://leetcode.com/problems/maximum-score-from-performing-multiplication-operations/discuss/1769349/Python3-Always-getting-time-limit-exceeded | class Solution:
def maximumScore(self, nums: List[int], multipliers: List[int]) -> int:
n, m = len(nums), len(multipliers)
dp = [[0] * (m + 1) for _ in range(m + 1)]
for left in range(1, m + 1):
dp[left][0] = dp[left - 1][0] + multipliers[left - 1] * nums[left - 1]
... | maximum-score-from-performing-multiplication-operations | Python3 - Always getting time limit exceeded | benjie | 1 | 203 | maximum score from performing multiplication operations | 1,770 | 0.366 | Hard | 25,414 |
https://leetcode.com/problems/maximum-score-from-performing-multiplication-operations/discuss/1674863/Well-coded-oror-Simple-and-Concise-oror-Memoization | class Solution:
def maximumScore(self, nums: List[int], multipliers: List[int]) -> int:
s,e = 0,len(nums)-1
res = 0
@lru_cache(2000)
def dfs(ind,s,e):
if ind>=len(multipliers):
return 0
a = multipliers[ind]*nums[s] + dfs(ind+1,s+1,e)
b = multipliers[ind]*nums[e] + dfs(ind+1,s,e-1)
return max(... | maximum-score-from-performing-multiplication-operations | ππ Well-coded || Simple & Concise || Memoization π | abhi9Rai | 1 | 229 | maximum score from performing multiplication operations | 1,770 | 0.366 | Hard | 25,415 |
https://leetcode.com/problems/maximum-score-from-performing-multiplication-operations/discuss/2584162/O(M2)-with-Iterative-DP-using-two-queues-(Example-Illustration) | class Solution:
def maximumScore(self, nums: List[int], multipliers: List[int]) -> int:
n, m = len(nums), len(multipliers)
dp1, dp2 = [0] * (m+1), [0] * (m+1)
print("nums:", nums, "mul:", multipliers)
print("dp:", dp1)
for k in range(m-1,-1,-1):
for i in r... | maximum-score-from-performing-multiplication-operations | O(M^2) with Iterative DP using two queues (Example Illustration) | dntai | 0 | 11 | maximum score from performing multiplication operations | 1,770 | 0.366 | Hard | 25,416 |
https://leetcode.com/problems/maximum-score-from-performing-multiplication-operations/discuss/2584162/O(M2)-with-Iterative-DP-using-two-queues-(Example-Illustration) | class Solution:
def maximumScore(self, nums: List[int], multipliers: List[int]) -> int:
n, m = len(nums), len(multipliers)
def fsol(i, k):
if dp[i][k] is not None:
return dp[i][k]
r1 = multipliers[k] * nums[i]
r2 = multipli... | maximum-score-from-performing-multiplication-operations | O(M^2) with Iterative DP using two queues (Example Illustration) | dntai | 0 | 11 | maximum score from performing multiplication operations | 1,770 | 0.366 | Hard | 25,417 |
https://leetcode.com/problems/maximum-score-from-performing-multiplication-operations/discuss/2583652/python-efficient-and-short-solution-using-%40lru_cache | class Solution:
def maximumScore(self, nums: List[int], multipliers: List[int]) -> int:
@lru_cache(2000)
def dynamic(st,end,idx):
if idx==len(multipliers)-1:
return max(nums[st]*multipliers[idx],nums[end]*multipliers[idx])
return max((nums[st]*multipliers[idx]... | maximum-score-from-performing-multiplication-operations | python efficient and short solution using @lru_cache | benon | 0 | 55 | maximum score from performing multiplication operations | 1,770 | 0.366 | Hard | 25,418 |
https://leetcode.com/problems/maximum-score-from-performing-multiplication-operations/discuss/2582910/Python-Iterative-DP-solution-(Explained) | class Solution:
def maximumScore(self, nums: List[int], multipliers: List[int]) -> int:
'''
Iterative DP coded in this way as it seems to be more intuitive in my opinion.
If still encountering problems understanding, trace through one or two simple testcases.
'''
# This dp li... | maximum-score-from-performing-multiplication-operations | Python Iterative DP solution (Explained) | chkmcnugget | 0 | 20 | maximum score from performing multiplication operations | 1,770 | 0.366 | Hard | 25,419 |
https://leetcode.com/problems/maximum-score-from-performing-multiplication-operations/discuss/1712969/Python-Iterative-solution-in-O(M)-space-and-O(M2)-time | class Solution:
def maximumScore(self, nums: List[int], multipliers: List[int]) -> int:
M = len(multipliers)
N = len(nums)
ind = M & 1
dp = [[0]*(M + 1) for _ in range(2)]
for mi in range(M - 1, -1, -1):
ind = mi & 1
prev = ind ^ 1
... | maximum-score-from-performing-multiplication-operations | Python Iterative solution in O(M) space and O(M^2) time | atiq1589 | 0 | 206 | maximum score from performing multiplication operations | 1,770 | 0.366 | Hard | 25,420 |
https://leetcode.com/problems/maximum-score-from-performing-multiplication-operations/discuss/1615639/Help-finding-the-bug-in-my-solution.-Passes-4362-test-cases-then-fails | class Solution:
def maximumScore(self, nums: List[int], multipliers: List[int]) -> int:
if len(nums) == 1:
return nums[0] * multipliers[0]
score = {}
# Initialize base cases
reversed_nums = list(reversed(nums))
left_sum = 0
right... | maximum-score-from-performing-multiplication-operations | Help finding the bug in my solution. Passes 43/62 test cases then fails | newbieleetcoder | 0 | 91 | maximum score from performing multiplication operations | 1,770 | 0.366 | Hard | 25,421 |
https://leetcode.com/problems/maximum-score-from-performing-multiplication-operations/discuss/1445128/Simple-Python-O(n2)-dynamic-programming-solution | class Solution:
def maximumScore(self, nums: List[int], multipliers: List[int]) -> int:
m, n = len(multipliers), len(nums)
# let dp[i][j] denote the maximum score using the first i elements
# and last j elements from nums
dp = [[-float("inf")]*(m+1) for _ in range(m+1)]
... | maximum-score-from-performing-multiplication-operations | Simple Python O(n^2) dynamic programming solution | Charlesl0129 | 0 | 293 | maximum score from performing multiplication operations | 1,770 | 0.366 | Hard | 25,422 |
https://leetcode.com/problems/maximize-palindrome-length-from-subsequences/discuss/1075709/Python3-top-down-dp | class Solution:
def longestPalindrome(self, word1: str, word2: str) -> int:
@cache
def fn(lo, hi):
"""Return length of longest palindromic subsequence."""
if lo >= hi: return int(lo == hi)
if word[lo] == word[hi]: return 2 + fn(lo+1, hi-1)
ret... | maximize-palindrome-length-from-subsequences | [Python3] top-down dp | ye15 | 13 | 425 | maximize palindrome length from subsequences | 1,771 | 0.352 | Hard | 25,423 |
https://leetcode.com/problems/maximize-palindrome-length-from-subsequences/discuss/1075551/Python-Dynamic-Programming-O((m%2Bn)2) | class Solution:
def longestPalindrome(self, word1: str, word2: str) -> int:
s1 = word1 + word2
n = len(s1)
dp = [[0] * n for i in range(n)]
ans = 0
for i in range(n-1,-1,-1):
# mark every character as a 1 length palindrome
dp[i][i] = 1
for j in range... | maximize-palindrome-length-from-subsequences | [Python] Dynamic Programming O((m+n)^2) | skele | 4 | 208 | maximize palindrome length from subsequences | 1,771 | 0.352 | Hard | 25,424 |
https://leetcode.com/problems/maximize-palindrome-length-from-subsequences/discuss/2446207/Python-3-simple-DP | class Solution:
def longestPalindrome(self, word1: str, word2: str) -> int:
res=0
new_word=word1+word2
n,mid=len(word1)+len(word2),len(word1)
dp=[[0]*n for _ in range(n)]
for i in range(n):
dp[i][i]=1
for l in range(n-2,-1,-1):
for r in range(l+1,n,1):
if new_word[l]==new_word[r]:
dp[l][r]=(... | maximize-palindrome-length-from-subsequences | [Python 3] simple DP | gabhay | 1 | 48 | maximize palindrome length from subsequences | 1,771 | 0.352 | Hard | 25,425 |
https://leetcode.com/problems/count-items-matching-a-rule/discuss/1085906/Python-3-or-2-liner-or-Explanation | class Solution:
def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
d = {'type': 0, 'color': 1, 'name': 2}
return sum(1 for item in items if item[d[ruleKey]] == ruleValue) | count-items-matching-a-rule | Python 3 | 2-liner | Explanation | idontknoooo | 46 | 3,200 | count items matching a rule | 1,773 | 0.843 | Easy | 25,426 |
https://leetcode.com/problems/count-items-matching-a-rule/discuss/1085783/Python3-1-line | class Solution:
def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
return sum(1 for t, c, n in items if (ruleKey, ruleValue) in (("type", t), ("color", c), ("name", n))) | count-items-matching-a-rule | [Python3] 1-line | ye15 | 23 | 2,100 | count items matching a rule | 1,773 | 0.843 | Easy | 25,427 |
https://leetcode.com/problems/count-items-matching-a-rule/discuss/2269882/PYTHON-3-ONE-LINER | class Solution:
def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
return sum(item_list[{"type": 0, "color": 1, "name": 2}[ruleKey]] == ruleValue for item_list in items) | count-items-matching-a-rule | [PYTHON 3] ONE LINER | omkarxpatel | 5 | 137 | count items matching a rule | 1,773 | 0.843 | Easy | 25,428 |
https://leetcode.com/problems/count-items-matching-a-rule/discuss/1176074/Python3-simple-solution | class Solution:
def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
count = 0
a = ["type", "color", "name"]
x = a.index(ruleKey)
for i in range(len(items)):
if ruleValue == items[i][x]:
count += 1
return count | count-items-matching-a-rule | Python3 simple solution | EklavyaJoshi | 5 | 336 | count items matching a rule | 1,773 | 0.843 | Easy | 25,429 |
https://leetcode.com/problems/count-items-matching-a-rule/discuss/2821411/PYTHON3 | class Solution:
def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
d = {'type': 0, 'color': 1, 'name': 2}
return sum(1 for item in items if item[d[ruleKey]] == ruleValue) | count-items-matching-a-rule | PYTHON3 | Gurugubelli_Anil | 1 | 42 | count items matching a rule | 1,773 | 0.843 | Easy | 25,430 |
https://leetcode.com/problems/count-items-matching-a-rule/discuss/2290787/python3-oror-easy-solution-oror-O(N) | class Solution:
def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
valueToCheck=None
result=0
if ruleKey=='type':
valueToCheck=0
elif ruleKey=='color':
valueToCheck=1
elif ruleKey=='name':
valueToC... | count-items-matching-a-rule | python3 || easy solution || O(N) | _soninirav | 1 | 60 | count items matching a rule | 1,773 | 0.843 | Easy | 25,431 |
https://leetcode.com/problems/count-items-matching-a-rule/discuss/1726439/Count-Items-Matching-a-Rule-solution-python | class Solution:
def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
count=0
var=0
if ruleKey=="type":
var=0
elif ruleKey=="color":
var=1
elif ruleKey=="name":
var=2
for x in items:
i... | count-items-matching-a-rule | Count Items Matching a Rule solution python | seabreeze | 1 | 122 | count items matching a rule | 1,773 | 0.843 | Easy | 25,432 |
https://leetcode.com/problems/count-items-matching-a-rule/discuss/1386918/2-line-solution-in-Python | class Solution:
def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
key_inedx = 0 if ruleKey == "type" else 1 if ruleKey == "color" else 2
return sum(item[key_inedx] == ruleValue for item in items) | count-items-matching-a-rule | 2-line solution in Python | mousun224 | 1 | 175 | count items matching a rule | 1,773 | 0.843 | Easy | 25,433 |
https://leetcode.com/problems/count-items-matching-a-rule/discuss/1241425/Python3-two-line-solution-with-list-comprehension | class Solution:
def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
key = 0 if ruleKey == "type" else 1 if ruleKey == "color" else 2
return len([item for item in items if item[key] == ruleValue]) | count-items-matching-a-rule | Python3 two line solution with list comprehension | albezx0 | 1 | 142 | count items matching a rule | 1,773 | 0.843 | Easy | 25,434 |
https://leetcode.com/problems/count-items-matching-a-rule/discuss/1223362/easiest-code-in-python-or | class Solution:
def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
a=['type','color', 'name']
b=a.index(ruleKey)
ans=0
for i in items:
if i[b] == ruleValue:
ans+=1
... | count-items-matching-a-rule | easiest code in python | | chikushen99 | 1 | 194 | count items matching a rule | 1,773 | 0.843 | Easy | 25,435 |
https://leetcode.com/problems/count-items-matching-a-rule/discuss/1221389/220ms-Python-(with-comments) | class Solution(object):
def countMatches(self, items, ruleKey, ruleValue):
"""
:type items: List[List[str]]
:type ruleKey: str
:type ruleValue: str
:rtype: int
"""
#Create a variable to store the output
result = 0
#Convert the ruleKey into equivalent index... | count-items-matching-a-rule | 220ms, Python (with comments) | Akshar-code | 1 | 78 | count items matching a rule | 1,773 | 0.843 | Easy | 25,436 |
https://leetcode.com/problems/count-items-matching-a-rule/discuss/1158672/yet-another-python-solution | class Solution:
def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
rules = {
'type': 0,
'color': 1,
'name': 2
}
counter = 0
for val in items:
if val[rules[ruleKey]] == ruleValue:
counter... | count-items-matching-a-rule | yet another python solution | Landsknecht | 1 | 95 | count items matching a rule | 1,773 | 0.843 | Easy | 25,437 |
https://leetcode.com/problems/count-items-matching-a-rule/discuss/1086719/PYTHON-VERY-SIMPLE-SOLUTION | class Solution:
def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
ruleKeys=["type","color","name"]
ruleKeyIndex=ruleKeys.index(ruleKey)
c=0
for i in range(len(items)):
if items[i][ruleKeyIndex]==ruleValue:
... | count-items-matching-a-rule | PYTHON VERY SIMPLE SOLUTION β
| lokeshsenthilkumar | 1 | 260 | count items matching a rule | 1,773 | 0.843 | Easy | 25,438 |
https://leetcode.com/problems/count-items-matching-a-rule/discuss/1085851/Python-Solution | class Solution:
def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
counter = 0
for t, c, n in items:
if ruleKey == "type" and t == ruleValue:
counter += 1
if ruleKey == "color" and c == ruleValue:
counter += 1
... | count-items-matching-a-rule | Python Solution | mariandanaila01 | 1 | 142 | count items matching a rule | 1,773 | 0.843 | Easy | 25,439 |
https://leetcode.com/problems/count-items-matching-a-rule/discuss/2847571/PYTHON-SIMPLE-AND-EASY | class Solution:
def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
op = {"type":0,"color":1,"name":2}
search = op[ruleKey]
count = 0
for i in range(0,len(items)):
if ruleValue == items[i][search]:
count = count+1
r... | count-items-matching-a-rule | PYTHON SIMPLE AND EASY | ameenusyed09 | 0 | 1 | count items matching a rule | 1,773 | 0.843 | Easy | 25,440 |
https://leetcode.com/problems/count-items-matching-a-rule/discuss/2800672/Simple-understandable-solution | class Solution:
def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
p=0
c=0
if ruleKey=='type':
c=0
elif ruleKey=='color':
c=1
else:
c=2
for j in range(len(items)):
g=items[j]
... | count-items-matching-a-rule | Simple understandable solution | priyanshupriyam123vv | 0 | 1 | count items matching a rule | 1,773 | 0.843 | Easy | 25,441 |
https://leetcode.com/problems/count-items-matching-a-rule/discuss/2783431/Simple-Python3-Solution-oror-Easy-and-Explained | class Solution:
def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
count = 0
if ruleKey == "color":
b = 1
elif ruleKey == "type":
b = 0
else:
b = 2
for i in items:
if i[b] == ruleValue:
... | count-items-matching-a-rule | Simple Python3 Solution || Easy & Explained | dnvavinash | 0 | 2 | count items matching a rule | 1,773 | 0.843 | Easy | 25,442 |
https://leetcode.com/problems/count-items-matching-a-rule/discuss/2782232/Python-Short-and-simple-solution | class Solution:
def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
d = {'type': 0, 'color': 1, 'name': 2}
ans, index = 0, d[ruleKey]
for i in items:
if i[index] == ruleValue:
ans += 1
return ans | count-items-matching-a-rule | [Python] Short and simple solution | Mark_computer | 0 | 3 | count items matching a rule | 1,773 | 0.843 | Easy | 25,443 |
https://leetcode.com/problems/count-items-matching-a-rule/discuss/2781372/Python3-2-liner-solution | class Solution:
def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
d = {'type': 0, 'color': 1, 'name': 2}
return sum(1 for item in items if item[d[ruleKey]] == ruleValue) | count-items-matching-a-rule | Python3 2 liner solution | avs-abhishek123 | 0 | 1 | count items matching a rule | 1,773 | 0.843 | Easy | 25,444 |
https://leetcode.com/problems/count-items-matching-a-rule/discuss/2745946/Simple-python-code-with-explanation | class Solution:
def countMatches(self, items, ruleKey, ruleValue):
#create a variable count to store the matching value
count = 0
#if the rulekey is "type"
if ruleKey == "type":
#then we need to check only first element in each sublist of items(list)
for i in... | count-items-matching-a-rule | Simple python code with explanation | thomanani | 0 | 5 | count items matching a rule | 1,773 | 0.843 | Easy | 25,445 |
https://leetcode.com/problems/count-items-matching-a-rule/discuss/2739521/Python-solution(faster-than-80) | class Solution:
def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
rule_keys = {'type': 0, 'color': 1, 'name': 2}
count = 0
for i in items:
if i[rule_keys[ruleKey]] == ruleValue:
count += 1
return count | count-items-matching-a-rule | Python solution(faster than 80%) | iakylbek | 0 | 5 | count items matching a rule | 1,773 | 0.843 | Easy | 25,446 |
https://leetcode.com/problems/count-items-matching-a-rule/discuss/2670043/Python3-one-line-solution-beats-85-98 | class Solution:
def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
return sum([x[{'type': 0, 'color': 1, 'name': 2}[ruleKey]]==ruleValue for x in items]) | count-items-matching-a-rule | Python3 one-line solution - beats 85% / 98% | sipi09 | 0 | 3 | count items matching a rule | 1,773 | 0.843 | Easy | 25,447 |
https://leetcode.com/problems/count-items-matching-a-rule/discuss/2616737/Count-Items-Matching-a-Rule | class Solution:
def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
count = 0
if ruleKey == 'type':
k = 0
elif ruleKey == 'color':
k = 1
else :
k = 2
for i in range(len(items)):
if items[i][k] ==... | count-items-matching-a-rule | Count Items Matching a Rule | PravinBorate | 0 | 10 | count items matching a rule | 1,773 | 0.843 | Easy | 25,448 |
https://leetcode.com/problems/count-items-matching-a-rule/discuss/2490639/Python-or-2-Different-Solutions-or-Optimal-Complexity | class Solution:
def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
# Method 1: T.C: O(n) S.C: O(1)
ans = 0
if ruleKey == "type":
check = 0
elif ruleKey == "color":
check = 1
else:
check = 2
... | count-items-matching-a-rule | Python | 2 Different Solutions | Optimal Complexity | chawlashivansh | 0 | 19 | count items matching a rule | 1,773 | 0.843 | Easy | 25,449 |
https://leetcode.com/problems/count-items-matching-a-rule/discuss/2473492/Simple-hash-solution | class Solution:
def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
res = 0
dic = {"type": 0,
"color": 1,
"name":2,
}
for item in items:
if item[dic[ruleKey]] == ruleValue:
res += 1
... | count-items-matching-a-rule | Simple hash solution | aruj900 | 0 | 24 | count items matching a rule | 1,773 | 0.843 | Easy | 25,450 |
https://leetcode.com/problems/count-items-matching-a-rule/discuss/2424522/Brute-Force-Approach-(-Easy-Python-Solution-) | class Solution:
def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
t,c,n = [],[],[]
for i in items:
for j in i:
if ruleKey == "type":
t.append(i[0])
if ruleKey == "color":
c.append(i... | count-items-matching-a-rule | Brute Force Approach ( Easy Python Solution ) | SouravSingh49 | 0 | 32 | count items matching a rule | 1,773 | 0.843 | Easy | 25,451 |
https://leetcode.com/problems/count-items-matching-a-rule/discuss/2258218/Count-Items-Matching-a-Rule | class Solution:
def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
out = 0
KeyIndex=0
if ruleKey=="color":
KeyIndex=1
if ruleKey=="type":
KeyIndex=0
if ruleKey=="name":
KeyIndex=2
for ... | count-items-matching-a-rule | Count Items Matching a Rule | dhananjayaduttmishra | 0 | 20 | count items matching a rule | 1,773 | 0.843 | Easy | 25,452 |
https://leetcode.com/problems/count-items-matching-a-rule/discuss/2229705/Beginner-Friendly-Solution-oror-Python | class Solution:
def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
count = 0
# Edge Case: ruleKey is neither of the required strings.
# In such a case, this block of code prevents the for-loop from running needlessly, thus reducing runtime and memory upta... | count-items-matching-a-rule | Beginner Friendly Solution || Python | cool-huip | 0 | 31 | count items matching a rule | 1,773 | 0.843 | Easy | 25,453 |
https://leetcode.com/problems/count-items-matching-a-rule/discuss/2187352/Python-3.10-Switch-Case-based-solution | class Solution:
def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
matching_items_count = 0
for item in items:
match ruleKey:
case "type":
if item[0] == ruleValue:
matching_items_count += 1
... | count-items-matching-a-rule | Python 3.10 Switch Case -based solution | tonimobin | 0 | 15 | count items matching a rule | 1,773 | 0.843 | Easy | 25,454 |
https://leetcode.com/problems/count-items-matching-a-rule/discuss/2147891/python-or-easy-solution-for-beginners | class Solution:
def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
c=0
for i in range(len(items)):
if ruleKey=="type":
if items[i][0]==ruleValue:
c+=1
if ruleKey=="color":
if items[i][1]==ru... | count-items-matching-a-rule | python | easy solution for beginners | T1n1_B0x1 | 0 | 56 | count items matching a rule | 1,773 | 0.843 | Easy | 25,455 |
https://leetcode.com/problems/count-items-matching-a-rule/discuss/2075866/O(N)-basic-list-comp | class Solution:
def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
keys = ["type", "color", "name"]
i = keys.index(ruleKey)
return sum([1 if item[i] == ruleValue else 0 for item in items]) | count-items-matching-a-rule | O(N) basic list comp | andrewnerdimo | 0 | 41 | count items matching a rule | 1,773 | 0.843 | Easy | 25,456 |
https://leetcode.com/problems/count-items-matching-a-rule/discuss/2040881/Python3-simple-code | class Solution:
def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
rules = ["type", "color", "name"]
count = 0
for item in items:
if item[rules.index(ruleKey)] == ruleValue:
count += 1
return count | count-items-matching-a-rule | [Python3] simple code | Shiyinq | 0 | 55 | count items matching a rule | 1,773 | 0.843 | Easy | 25,457 |
https://leetcode.com/problems/count-items-matching-a-rule/discuss/2014004/Easy-Python-solution | class Solution:
def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
res = 0
for i, (types, color, name) in enumerate(items):
if ruleKey == 'color':
if color == ruleValue:
res += 1
elif ruleKey == 'type':
... | count-items-matching-a-rule | Easy Python solution | ankurbhambri | 0 | 72 | count items matching a rule | 1,773 | 0.843 | Easy | 25,458 |
https://leetcode.com/problems/count-items-matching-a-rule/discuss/2000621/Python-Clean-and-Simple! | class Solution:
def countMatches(self, items, ruleKey, ruleValue):
ruleMap = {"type":0, "color":1, "name":2}
return len(list(filter(lambda x : x[ruleMap[ruleKey]] == ruleValue, items))) | count-items-matching-a-rule | Python - Clean and Simple! | domthedeveloper | 0 | 76 | count items matching a rule | 1,773 | 0.843 | Easy | 25,459 |
https://leetcode.com/problems/count-items-matching-a-rule/discuss/2000621/Python-Clean-and-Simple! | class Solution:
def countMatches(self, items, ruleKey, ruleValue):
ruleMap = {"type":0, "color":1, "name":2}
return sum(item[ruleMap[ruleKey]] == ruleValue for item in items) | count-items-matching-a-rule | Python - Clean and Simple! | domthedeveloper | 0 | 76 | count items matching a rule | 1,773 | 0.843 | Easy | 25,460 |
https://leetcode.com/problems/count-items-matching-a-rule/discuss/1910541/Python-Solution | class Solution:
def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
index = ((2, 1)[ruleKey == 'color'], 0)[ruleKey == 'type']
return len(list(filter(lambda x: x[index] == ruleValue, items))) | count-items-matching-a-rule | Python Solution | hgalytoby | 0 | 60 | count items matching a rule | 1,773 | 0.843 | Easy | 25,461 |
https://leetcode.com/problems/count-items-matching-a-rule/discuss/1910541/Python-Solution | class Solution:
def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
index = {'type': 0, 'color': 1, 'name': 2}
return len(list(filter(lambda x: x[index[ruleKey]] == ruleValue, items))) | count-items-matching-a-rule | Python Solution | hgalytoby | 0 | 60 | count items matching a rule | 1,773 | 0.843 | Easy | 25,462 |
https://leetcode.com/problems/count-items-matching-a-rule/discuss/1873846/Python-(Using-Dict) | class Solution:
def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
marker = {'type':0, 'color':1, 'name':2}
count=0
for i in items:
if i[marker[ruleKey]]==ruleValue:
count+=1
return count | count-items-matching-a-rule | Python (Using Dict) | sharma_nayaab | 0 | 38 | count items matching a rule | 1,773 | 0.843 | Easy | 25,463 |
https://leetcode.com/problems/count-items-matching-a-rule/discuss/1793692/PYTHON-or-Other-dictionnary-solution | class Solution:
def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
dic={"type":[],
"color":[],
"name":[]}
ans = 0
for i in range(len(items)):
for j in range(len(dic)):
if j == 0 :
dic[... | count-items-matching-a-rule | PYTHON | Other dictionnary solution | Noyha | 0 | 28 | count items matching a rule | 1,773 | 0.843 | Easy | 25,464 |
https://leetcode.com/problems/count-items-matching-a-rule/discuss/1754087/Quite-simple-and-fast | class Solution:
def countMatches(self, items: List[List[str]], rK: str, rV: str) -> int:
cnt=0
d = {"type":0, "color":1, "name":2}
for i in range(len(items)):
if items[i][d[rK]] == rV:
cnt += 1
return cnt | count-items-matching-a-rule | Quite simple and fast | flame91 | 0 | 42 | count items matching a rule | 1,773 | 0.843 | Easy | 25,465 |
https://leetcode.com/problems/count-items-matching-a-rule/discuss/1753853/Python-Solution | class Solution:
def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
rule = {'type':0, 'color':1, 'name':2}
count = 0
for i in items:
idx = rule[ruleKey]
if i[idx] == ruleValue:
count += 1
return count | count-items-matching-a-rule | Python Solution | MengyingLin | 0 | 53 | count items matching a rule | 1,773 | 0.843 | Easy | 25,466 |
https://leetcode.com/problems/count-items-matching-a-rule/discuss/1709422/Python3-accepted-solution | class Solution:
def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
keyval = {"type":0,"color":1,"name":2}
return ([i[list(keyval.keys()).index(ruleKey)] for i in items].count(ruleValue)) | count-items-matching-a-rule | Python3 accepted solution | sreeleetcode19 | 0 | 45 | count items matching a rule | 1,773 | 0.843 | Easy | 25,467 |
https://leetcode.com/problems/count-items-matching-a-rule/discuss/1699770/Python-Simple-Solution | class Solution:
def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
dic ={ "type":0, "color":1, "name":2 }
var = 0
for i in items:
if i[dic[ruleKey]] == ruleValue:
var += 1
return var | count-items-matching-a-rule | Python Simple Solution | vijayvardhan6 | 0 | 61 | count items matching a rule | 1,773 | 0.843 | Easy | 25,468 |
https://leetcode.com/problems/count-items-matching-a-rule/discuss/1647931/Easy-Python3-solution-faster-than-98 | class Solution:
def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
res = []
for i in range(len(items)):
if ruleKey == "type" and ruleValue == items[i][0]:
res.append(items[i])
elif ruleKey == "color" and r... | count-items-matching-a-rule | Easy Python3 solution - faster than 98% | y-arjun-y | 0 | 82 | count items matching a rule | 1,773 | 0.843 | Easy | 25,469 |
https://leetcode.com/problems/count-items-matching-a-rule/discuss/1642127/Easy-To-Understand-Python-Solution-Faster-Than-100 | class Solution(object):
def countMatches(self, items, ruleKey, ruleValue):
output = 0
itemsi = ["type","color","name"]
ruleKey = itemsi.index(ruleKey)
for i in items:
if i[ruleKey] == ruleValue:
output += 1
return output | count-items-matching-a-rule | Easy To Understand Python Solution Faster Than 100% | yparikh | 0 | 110 | count items matching a rule | 1,773 | 0.843 | Easy | 25,470 |
https://leetcode.com/problems/closest-dessert-cost/discuss/1085820/Python3-top-down-dp | class Solution:
def closestCost(self, baseCosts: List[int], toppingCosts: List[int], target: int) -> int:
toppingCosts *= 2
@cache
def fn(i, x):
"""Return sum of subsequence of toppingCosts[i:] closest to x."""
if x < 0 or i == len(toppingCosts): return 0
... | closest-dessert-cost | [Python3] top-down dp | ye15 | 18 | 3,000 | closest dessert cost | 1,774 | 0.468 | Medium | 25,471 |
https://leetcode.com/problems/closest-dessert-cost/discuss/1085820/Python3-top-down-dp | class Solution:
def closestCost(self, baseCosts: List[int], toppingCosts: List[int], target: int) -> int:
@cache
def fn(i, cost):
"""Return sum of subsequence closest to target."""
if cost >= target or i == len(toppingCosts): return cost
return min(fn(i+1... | closest-dessert-cost | [Python3] top-down dp | ye15 | 18 | 3,000 | closest dessert cost | 1,774 | 0.468 | Medium | 25,472 |
https://leetcode.com/problems/closest-dessert-cost/discuss/1085820/Python3-top-down-dp | class Solution:
def closestCost(self, baseCosts: List[int], toppingCosts: List[int], target: int) -> int:
top = {0}
for x in toppingCosts*2:
top |= {x + xx for xx in top}
top = sorted(top)
ans = inf
for bc in baseCosts:
k = bisect_left(top,... | closest-dessert-cost | [Python3] top-down dp | ye15 | 18 | 3,000 | closest dessert cost | 1,774 | 0.468 | Medium | 25,473 |
https://leetcode.com/problems/closest-dessert-cost/discuss/1818264/Python-or-BFS-or-207ms-or-4-line-or-fast-and-simple-solution | class Solution:
def closestCost(self, baseCosts: List[int], toppingCosts: List[int], target: int) -> int:
q = [target - basecost for basecost in baseCosts]
for toppingcost in toppingCosts:
q += [val - cost for val in q if val > 0 for cost in (toppingcost, toppingcost * 2)]
retu... | closest-dessert-cost | Python | BFS | 207ms | 4-line | fast and simple solution | xuauul | 5 | 198 | closest dessert cost | 1,774 | 0.468 | Medium | 25,474 |
https://leetcode.com/problems/closest-dessert-cost/discuss/1087622/Backtracking(%2Bmemo)-with-comments | class Solution:
def closestCost(self, baseCosts: List[int], toppingCosts: List[int], target: int) -> int:
self.cost = baseCosts[0]
# my trick here: double the toppings list since each topping can only be used at most twice -- so I don't need to care about this restriction down the road
toppi... | closest-dessert-cost | Backtracking(+memo) with comments | w_x | 3 | 263 | closest dessert cost | 1,774 | 0.468 | Medium | 25,475 |
https://leetcode.com/problems/closest-dessert-cost/discuss/1085928/Python-3-or-Backtracking-DFS-or-Explanation | class Solution:
def closestCost(self, baseCosts: List[int], toppingCosts: List[int], target: int) -> int:
m = len(toppingCosts)
ans = (baseCosts[0], abs(baseCosts[0]-target), -1 if target > baseCosts[0] else (0 if target == baseCosts[0] else 1))
def dfs(cnt, cur_idx):
no... | closest-dessert-cost | Python 3 | Backtracking, DFS | Explanation | idontknoooo | 3 | 279 | closest dessert cost | 1,774 | 0.468 | Medium | 25,476 |
https://leetcode.com/problems/closest-dessert-cost/discuss/1086226/Python-or-Backtracking-or-Simple | class Solution:
def bt(self, index, stack):
if index>=len(self.toppingCosts):
if self.minDiff>abs(self.target-sum(stack)):
self.minDiff= abs(self.target-sum(stack))
self.resStack=stack
return
for i in range(3):
self.bt(index+1, st... | closest-dessert-cost | Python | Backtracking | Simple | sarangvasishth | 2 | 231 | closest dessert cost | 1,774 | 0.468 | Medium | 25,477 |
https://leetcode.com/problems/closest-dessert-cost/discuss/2147526/Python-3-solution%3A-Minimizing-the-error-using-backtracking | class Solution:
def closestCost(self, baseCosts: List[int], toppingCosts: List[int], target: int) -> int:
toppingCosts *= 2
best_error = float("inf") # best error can be greater than `target`
@cache
def findError(index, target):
if index == len(toppingC... | closest-dessert-cost | Python 3 solution: Minimizing the error using backtracking | goelujjwal | 1 | 152 | closest dessert cost | 1,774 | 0.468 | Medium | 25,478 |
https://leetcode.com/problems/closest-dessert-cost/discuss/2793047/Python3-or-Recursion-or-Explanation | class Solution:
def closestCost(self, baseCosts: List[int], toppingCosts: List[int], target: int) -> int:
def dfs(ind,cost):
nonlocal diff,ans
if diff > abs(target - cost):
diff = abs(target - cost)
ans = cost
elif diff == abs(target - cost... | closest-dessert-cost | [Python3] | Recursion | Explanation | swapnilsingh421 | 0 | 5 | closest dessert cost | 1,774 | 0.468 | Medium | 25,479 |
https://leetcode.com/problems/closest-dessert-cost/discuss/1919472/Just-a-simple-Recursion | class Solution:
def closestCost(self, baseCosts: List[int], toppingCosts: List[int], target: int) -> int:
self.s=set()
n=len(toppingCosts)
for i in range(n):
toppingCosts.append(toppingCosts[i])
@lru_cache(None)
def rec(price,i):
self.s.add(price)
... | closest-dessert-cost | Just a simple Recursion | pbhuvaneshwar | 0 | 63 | closest dessert cost | 1,774 | 0.468 | Medium | 25,480 |
https://leetcode.com/problems/closest-dessert-cost/discuss/1086228/python3-recursion-solution-for-reference | class Solution:
def closestCost(self, baseCosts, toppingCosts, target: int) -> int:
def toppings_r(toppings, rollingCost, targetCost, index, output):
if index >= len(toppings):
return
sc = rollingCost + toppings[index]
dc = sc + toppings[index]
... | closest-dessert-cost | python3 recursion - solution for reference | vadhri_venkat | 0 | 60 | closest dessert cost | 1,774 | 0.468 | Medium | 25,481 |
https://leetcode.com/problems/closest-dessert-cost/discuss/1085931/Python-top-down-dp-30ms | class Solution:
def closestCost(self, baseCosts: List[int], toppingCosts: List[int], target: int) -> int:
@lru_cache(None)
def dp(t, i):
if t <= 0 or i == len(toppingCosts):
return t
useOne = dp(t - toppingCosts[i], i + 1)
if u... | closest-dessert-cost | Python top-down dp 30ms | tyohekim | 0 | 65 | closest dessert cost | 1,774 | 0.468 | Medium | 25,482 |
https://leetcode.com/problems/equal-sum-arrays-with-minimum-number-of-operations/discuss/1085806/Python3-two-heaps | class Solution:
def minOperations(self, nums1: List[int], nums2: List[int]) -> int:
if 6*len(nums1) < len(nums2) or 6*len(nums2) < len(nums1): return -1 # impossible
if sum(nums1) < sum(nums2): nums1, nums2 = nums2, nums1
s1, s2 = sum(nums1), sum(nums2)
nums1 =... | equal-sum-arrays-with-minimum-number-of-operations | [Python3] two heaps | ye15 | 18 | 848 | equal sum arrays with minimum number of operations | 1,775 | 0.527 | Medium | 25,483 |
https://leetcode.com/problems/equal-sum-arrays-with-minimum-number-of-operations/discuss/1085806/Python3-two-heaps | class Solution:
def minOperations(self, nums1: List[int], nums2: List[int]) -> int:
if 6*len(nums1) < len(nums2) or 6*len(nums2) < len(nums1): return -1 # impossible
if sum(nums1) < sum(nums2): nums1, nums2 = nums2, nums1
s1, s2 = sum(nums1), sum(nums2) # s1 >= s2
... | equal-sum-arrays-with-minimum-number-of-operations | [Python3] two heaps | ye15 | 18 | 848 | equal sum arrays with minimum number of operations | 1,775 | 0.527 | Medium | 25,484 |
https://leetcode.com/problems/equal-sum-arrays-with-minimum-number-of-operations/discuss/1085956/Python-3-or-Heap-Greedy-or-Explanations | class Solution:
def minOperations(self, nums1: List[int], nums2: List[int]) -> int:
s1, s2 = sum(nums1), sum(nums2)
if s1 > s2:
s1, s2 = s2, s1
nums1, nums2 = nums2, nums1
# to make s1 < s2
heapq.heapify(nums1)
nums2 = [-num for... | equal-sum-arrays-with-minimum-number-of-operations | Python 3 | Heap, Greedy | Explanations | idontknoooo | 10 | 671 | equal sum arrays with minimum number of operations | 1,775 | 0.527 | Medium | 25,485 |
https://leetcode.com/problems/equal-sum-arrays-with-minimum-number-of-operations/discuss/1088875/Priority-Queue-with-comments-(beat-100-when-posting) | class Solution:
def minOperations(self, nums1: List[int], nums2: List[int]) -> int:
# make sure nums1 has the larger sum
highSum, lowSum = sum(nums1), sum(nums2)
if highSum == lowSum:
return 0
if highSum < lowSum:
nums1, nums2 = nums2, nums1
highSu... | equal-sum-arrays-with-minimum-number-of-operations | Priority Queue with comments (beat 100% when posting) | w_x | 3 | 172 | equal sum arrays with minimum number of operations | 1,775 | 0.527 | Medium | 25,486 |
https://leetcode.com/problems/equal-sum-arrays-with-minimum-number-of-operations/discuss/1085880/Python-O(n)-time-O(1)-space-Greedy | class Solution:
def minOperations(self, nums1: List[int], nums2: List[int]) -> int:
A, B = nums1, nums2
sum_a, sum_b = sum(A), sum(B)
if sum_a > sum_b:
# keep sum(B) > sum(A)
A, B = B, A
sum_a, sum_b = sum_b, sum_a
# get element frequencies
... | equal-sum-arrays-with-minimum-number-of-operations | [Python] O(n) time O(1) space Greedy | geordgez | 3 | 392 | equal sum arrays with minimum number of operations | 1,775 | 0.527 | Medium | 25,487 |
https://leetcode.com/problems/equal-sum-arrays-with-minimum-number-of-operations/discuss/1546613/python3-2-pointers-after-sorting-w-comments | class Solution:
def minOperations(self, nums1: List[int], nums2: List[int]) -> int:
# idea is to decrement values the array having larger sum and increment values in the array having small sum
# choose biggest element always to decrement values in large_sum_array
# choose smallest element al... | equal-sum-arrays-with-minimum-number-of-operations | python3 , 2 pointers after sorting w/ comments | Code-IQ7 | 2 | 239 | equal sum arrays with minimum number of operations | 1,775 | 0.527 | Medium | 25,488 |
https://leetcode.com/problems/equal-sum-arrays-with-minimum-number-of-operations/discuss/2824971/Python-solution-with-math-check | class Solution:
def minOperations(self, nums1: List[int], nums2: List[int]) -> int:
lDiv = len(nums1) / len(nums2)
if lDiv > 6 or lDiv < 1/6:
return -1
s1, s2 = sum(nums1), sum(nums2)
if s1 < s2:
s1, s2, nums1, nums2 = s2, s1, nums2, nums1
... | equal-sum-arrays-with-minimum-number-of-operations | Python solution with math check | WhyYouCryingMama | 0 | 4 | equal sum arrays with minimum number of operations | 1,775 | 0.527 | Medium | 25,489 |
https://leetcode.com/problems/equal-sum-arrays-with-minimum-number-of-operations/discuss/2637826/Python-O(n)-solution-easy-understanding | class Solution:
def minOperations(self, nums1: List[int], nums2: List[int]) -> int:
min_arr_size = len(nums1) if len(nums1)< len(nums2) else len(nums2)
max_arr_size = len(nums1) if len(nums1)>= len(nums2) else len(nums2)
# return -1 , if the biggest sum of shorter array is ... | equal-sum-arrays-with-minimum-number-of-operations | Python O(n) solution, easy understanding | yu-che | 0 | 40 | equal sum arrays with minimum number of operations | 1,775 | 0.527 | Medium | 25,490 |
https://leetcode.com/problems/equal-sum-arrays-with-minimum-number-of-operations/discuss/2554202/Python3-or-Priority-Queue-or-O(nlogn) | class Solution:
def minOperations(self, nums1: List[int], nums2: List[int]) -> int:
if len(nums1)>6*len(nums2) or len(nums2)>6*len(nums1):
return -1
s1,s2=sum(nums1),sum(nums2)
pq=[]
cnt=0
if s1==s2:
return cnt
if s1<s2:
nums1,nums2... | equal-sum-arrays-with-minimum-number-of-operations | [Python3] | Priority Queue | O(nlogn) | swapnilsingh421 | 0 | 79 | equal sum arrays with minimum number of operations | 1,775 | 0.527 | Medium | 25,491 |
https://leetcode.com/problems/equal-sum-arrays-with-minimum-number-of-operations/discuss/1086804/Python-with-Comments | class Solution:
def minOperations(self, nums1: List[int], nums2: List[int]) -> int:
# Get the delta
# We want sum(nums1) > sum(nums2) so we can use logic below
delta = sum(nums1) - sum(nums2)
if delta < 0:
nums1, nums2 = nums2, nums1
... | equal-sum-arrays-with-minimum-number-of-operations | Python with Comments | dev-josh | 0 | 120 | equal sum arrays with minimum number of operations | 1,775 | 0.527 | Medium | 25,492 |
https://leetcode.com/problems/car-fleet-ii/discuss/1557743/Python3-Stack-Time%3A-O(n)-and-Space%3A-O(n) | class Solution:
def getCollisionTimes(self, cars: List[List[int]]) -> List[float]:
# Stack: go from back and use stack to get ans
# Time: O(n)
# Space: O(n)
stack = [] # index
ans = [-1] * len(cars)
for i in range(len(cars)-1,-1,-1):
# remove cars... | car-fleet-ii | [Python3] Stack - Time: O(n) & Space: O(n) | jae2021 | 4 | 261 | car fleet ii | 1,776 | 0.534 | Hard | 25,493 |
https://leetcode.com/problems/car-fleet-ii/discuss/2702324/Python3-or-Stack-Approach | class Solution:
def getCollisionTimes(self, cars: List[List[int]]) -> List[float]:
n=len(cars)
ans=[-1]*n
stack=[]
for i in range(n-1,-1,-1):
while stack and cars[i][1]<=cars[stack[-1]][1]:
stack.pop()
while stack:
collisionTime... | car-fleet-ii | [Python3] | Stack Approach | swapnilsingh421 | 0 | 5 | car fleet ii | 1,776 | 0.534 | Hard | 25,494 |
https://leetcode.com/problems/car-fleet-ii/discuss/1087424/Python3-stack-O(N) | class Solution:
def getCollisionTimes(self, cars: List[List[int]]) -> List[float]:
ans = [-1]*len(cars)
stack = []
for i, (x, v) in enumerate(reversed(cars)):
while stack and (v <= stack[-1][1] or (stack[-1][0] - x)/(v - stack[-1][1]) >= stack[-1][2]): stack.pop()
if... | car-fleet-ii | [Python3] stack O(N) | ye15 | 0 | 160 | car fleet ii | 1,776 | 0.534 | Hard | 25,495 |
https://leetcode.com/problems/car-fleet-ii/discuss/1086030/Python-Stack-of-Cars-O(n) | class Solution:
def getCollisionTimes(self, cars: List[List[int]]) -> List[float]:
res = []
ahead = [(math.inf, 0)]
for pos, spd in reversed(cars):
while ahead and spd <= ahead[-1][1]:
ahead.pop()
atime = -1
while len(ahead) >= 2:
... | car-fleet-ii | Python Stack of Cars O(n) | ajlai | 0 | 131 | car fleet ii | 1,776 | 0.534 | Hard | 25,496 |
https://leetcode.com/problems/car-fleet-ii/discuss/1085926/Python3-need-help-with-why-this-approach-is-wrong | class Solution:
def getCollisionTimes(self, cars: List[List[int]]) -> List[float]:
times = [float(-1) for _ in range(len(cars))]
for i in range(1, len(cars)):
if cars[i-1][1] > cars[i][1]:
times[i-1] = self.timeToCollide(cars, i-1, i)
for i in range(len(cars)-3, -... | car-fleet-ii | [Python3] need help with why this approach is wrong | hwsbjts | 0 | 69 | car fleet ii | 1,776 | 0.534 | Hard | 25,497 |
https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/discuss/1229047/Python-Easy-solution | class Solution:
def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int:
minDist = math.inf
ans = -1
for i in range(len(points)):
if points[i][0]==x or points[i][1]==y:
manDist = abs(points[i][0]-x)+abs(points[i][1]-y)
if manDis... | find-nearest-point-that-has-the-same-x-or-y-coordinate | [Python] Easy solution | arkumari2000 | 20 | 2,300 | find nearest point that has the same x or y coordinate | 1,779 | 0.673 | Easy | 25,498 |
https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/discuss/2220848/Python3-simple-naive-approach | class Solution:
def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int:
result = None
maximumDistance = float("inf")
for index,point in enumerate(points):
a,b = point
distance = abs(x-a) + abs(y-b)
if distance < maximumDistanc... | find-nearest-point-that-has-the-same-x-or-y-coordinate | π Python3 simple naive approach | Dark_wolf_jss | 5 | 302 | find nearest point that has the same x or y coordinate | 1,779 | 0.673 | Easy | 25,499 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.