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/check-if-n-and-its-double-exist/discuss/1329127/Simplest-Python-Solution | class Solution:
def checkIfExist(self, arr):
for i in range(len(arr)):
for j in range(len(arr)):
print(2*j,i)
if 2*arr[j]==arr[i] and i!=j:
return True
else:
return False | check-if-n-and-its-double-exist | Simplest Python Solution | sangam92 | 0 | 301 | check if n and its double exist | 1,346 | 0.362 | Easy | 20,300 |
https://leetcode.com/problems/check-if-n-and-its-double-exist/discuss/1065859/Simple-Python3-solution | class Solution:
def checkIfExist(self, arr: List[int]) -> bool:
if arr.count(0) > 1:
return True
for x in arr:
for y in arr:
if ((x / 2) == y or (y / 2) == x) and ((x < 0 and y < 0) or (x > 0 and y > 0)):
return True
return False | check-if-n-and-its-double-exist | Simple Python3 solution | veevyo | 0 | 130 | check if n and its double exist | 1,346 | 0.362 | Easy | 20,301 |
https://leetcode.com/problems/check-if-n-and-its-double-exist/discuss/811436/Python-Solution-92ms-Solution | class Solution:
def checkIfExist(self, arr: List[int]) -> bool:
d = {num:num*2 for num in arr }
if arr.count(0)>1:return True
for i in range(len(arr)-1):
if arr[i]%2==0 and arr[i]!=0:
k = arr[i]//2
if k in d:
return True
return False | check-if-n-and-its-double-exist | Python Solution 92ms Solution | raviarrow88 | 0 | 216 | check if n and its double exist | 1,346 | 0.362 | Easy | 20,302 |
https://leetcode.com/problems/check-if-n-and-its-double-exist/discuss/721192/simple-of-simple | class Solution:
def checkIfExist(self, arr: List[int]) -> bool:
s = set([x*1.0 for x in arr])
from collections import Counter
if Counter(arr)[0] >= 2:
return True
for i in arr:
t = i/2
if t in s and t != 0:
return True
return False | check-if-n-and-its-double-exist | simple of simple | seunggabi | 0 | 147 | check if n and its double exist | 1,346 | 0.362 | Easy | 20,303 |
https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram/discuss/503535/Python-3-(two-lines)-(beats-100) | class Solution:
def minSteps(self, S: str, T: str) -> int:
D = collections.Counter(S) - collections.Counter(T)
return sum(max(0, D[s]) for s in set(S))
- Junaid Mansuri
- Chicago, IL | minimum-number-of-steps-to-make-two-strings-anagram | Python 3 (two lines) (beats 100%) | junaidmansuri | 9 | 2,100 | minimum number of steps to make two strings anagram | 1,347 | 0.774 | Medium | 20,304 |
https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram/discuss/1841286/Python-oror-easy-solution-oror-beat~99-oror-using-counter | class Solution:
def minSteps(self, s: str, t: str) -> int:
common = Counter(s) & Counter(t)
count = sum(common.values())
return len(s) - count | minimum-number-of-steps-to-make-two-strings-anagram | Python || easy solution || beat~99% || using counter | naveenrathore | 5 | 372 | minimum number of steps to make two strings anagram | 1,347 | 0.774 | Medium | 20,305 |
https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram/discuss/2024761/Python-simple-2-line-solution-faster-than-85 | class Solution:
def minSteps(self, s: str, t: str) -> int:
common_vals = sum((Counter(s) & Counter(t)).values())
return len(s) - common_vals | minimum-number-of-steps-to-make-two-strings-anagram | Python simple 2 line solution faster than 85% | alishak1999 | 2 | 112 | minimum number of steps to make two strings anagram | 1,347 | 0.774 | Medium | 20,306 |
https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram/discuss/2814820/Very-Easy-one-liner-Python-Solution | class Solution:
def minSteps(self, s: str, t: str) -> int:
return sum((Counter(t)-Counter(s)).values()) | minimum-number-of-steps-to-make-two-strings-anagram | Very Easy one-liner Python Solution | Abhishek004 | 1 | 96 | minimum number of steps to make two strings anagram | 1,347 | 0.774 | Medium | 20,307 |
https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram/discuss/503497/Python3-Concise-5-line | class Solution:
def minSteps(self, s: str, t: str) -> int:
dt, ds = dict(), dict() #frequency table
for tt, ss in zip(t, s):
dt[tt] = 1 + dt.get(tt, 0)
ds[ss] = 1 + ds.get(ss, 0)
return len(s) - sum(min(v, ds.get(k, 0)) for k, v in dt.items()) | minimum-number-of-steps-to-make-two-strings-anagram | [Python3] Concise 5-line | ye15 | 1 | 66 | minimum number of steps to make two strings anagram | 1,347 | 0.774 | Medium | 20,308 |
https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram/discuss/2843992/Fastest-oror-Simple-oror-One-liner-oror-Using-two-counters | class Solution(object):
def minSteps(self, s, t):
return sum((Counter(t)-Counter(s)).values()) | minimum-number-of-steps-to-make-two-strings-anagram | Fastest || Simple || One liner || Using two counters | rajkamallashkari | 0 | 1 | minimum number of steps to make two strings anagram | 1,347 | 0.774 | Medium | 20,309 |
https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram/discuss/2840100/Go-Python-easy-Solution-Hashmap | class Solution:
def minSteps(self, s: str, t: str) -> int:
s1 = Counter(s)
c=0
for i in t:
if i in s1 and s1[i] > 0:
s1[i] -=1
else:
c+=1
return c | minimum-number-of-steps-to-make-two-strings-anagram | Go Python easy Solution Hashmap | anshsharma17 | 0 | 6 | minimum number of steps to make two strings anagram | 1,347 | 0.774 | Medium | 20,310 |
https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram/discuss/2807695/Easiest-solution | class Solution:
def minSteps(self, s: str, t: str) -> int:
count = collections.Counter(s)
output = 0
for c in t:
if count[c] == 0:
output+=1
else:
count[c] -=1
return output | minimum-number-of-steps-to-make-two-strings-anagram | Easiest solution | devilabakrania | 0 | 7 | minimum number of steps to make two strings anagram | 1,347 | 0.774 | Medium | 20,311 |
https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram/discuss/2805212/Python-2-lines | class Solution:
# basically I just find the count of common characters
# in s and t. Those common characters can stay, but other
# characters gotta change. so the answer is the length
# of the string - total common characters.
def minSteps(self, s: str, t: str) -> int:
cs, ct = Counter(s), Counter(t)
return len(s) - sum([min(cs[c], ct[c]) for c in cs]) | minimum-number-of-steps-to-make-two-strings-anagram | Python 2 lines | tinmanSimon | 0 | 2 | minimum number of steps to make two strings anagram | 1,347 | 0.774 | Medium | 20,312 |
https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram/discuss/2788248/Simple-Python-solution | class Solution:
def minSteps(self, s: str, t: str) -> int:
character_count = defaultdict(int)
for c in s:
character_count[c] += 1
for c in t:
character_count[c] -= 1
steps = 0
for character in character_count.keys():
if character_count[character] > 0:
steps += character_count[character]
return steps | minimum-number-of-steps-to-make-two-strings-anagram | Simple Python solution | khaled_achech | 0 | 8 | minimum number of steps to make two strings anagram | 1,347 | 0.774 | Medium | 20,313 |
https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram/discuss/2744340/Simple-Python-Solution-O(n)-time | class Solution:
def minSteps(self, s: str, t: str) -> int:
count = [0] * 26 # 26 letters
for i in range(len(s)):
count[ord(s[i]) - ord('a')] += 1
count[ord(t[i]) - ord('a')] -= 1
imbalanced_char = 0
for num in count:
# only need to account for positive imbalances
# this is because replacing one char will fix the imbalance
if num > 0:
imbalanced_char += num
return imbalanced_char | minimum-number-of-steps-to-make-two-strings-anagram | Simple Python Solution O(n) time | avgpersonlargetoes | 0 | 23 | minimum number of steps to make two strings anagram | 1,347 | 0.774 | Medium | 20,314 |
https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram/discuss/2700328/Python-and-Go-Easy-Solution | class Solution:
def minSteps(self, s: str, t: str) -> int:
s_dict = {}
for char in s:
s_dict[char] = s_dict.get(char, 0) + 1
t_dict = {}
for char in t:
t_dict[char] = t_dict.get(char, 0) + 1
steps = 0
for s_key, s_value in s_dict.items():
t_value = t_dict.get(s_key, 0)
if 0 < (diff := s_value - t_value):
steps += diff
return steps | minimum-number-of-steps-to-make-two-strings-anagram | Python and Go Easy Solution | namashin | 0 | 16 | minimum number of steps to make two strings anagram | 1,347 | 0.774 | Medium | 20,315 |
https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram/discuss/2700328/Python-and-Go-Easy-Solution | class Solution:
def minSteps(self, s: str, t: str) -> int:
s_dict = collections.Counter(s)
t_dict = collections.Counter(t)
steps = 0
for s_key, s_value in s_dict.items():
t_value = t_dict.get(s_key, 0)
if 0 < (diff := s_value - t_value):
steps += diff
return steps | minimum-number-of-steps-to-make-two-strings-anagram | Python and Go Easy Solution | namashin | 0 | 16 | minimum number of steps to make two strings anagram | 1,347 | 0.774 | Medium | 20,316 |
https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram/discuss/2658157/python-solution | class Solution:
def minSteps(self, s: str, t: str) -> int:
counter_s = collections.Counter(list(s))
counter_t = collections.Counter(list(t))
for c in counter_s:
counter_s[c] = max(0, counter_s[c] - counter_t.get(c, 0))
return sum(counter_s.values()) | minimum-number-of-steps-to-make-two-strings-anagram | python solution | cavey621 | 0 | 12 | minimum number of steps to make two strings anagram | 1,347 | 0.774 | Medium | 20,317 |
https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram/discuss/2514756/Python-or-One-liner | class Solution:
def minSteps(self, s: str, t: str) -> int:
return len(tuple((Counter(t) - Counter(s)).elements())) | minimum-number-of-steps-to-make-two-strings-anagram | Python | One-liner | Wartem | 0 | 49 | minimum number of steps to make two strings anagram | 1,347 | 0.774 | Medium | 20,318 |
https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram/discuss/2440199/Python-using-Counter-3-lines-of-code | class Solution:
def minSteps(self, s: str, t: str) -> int:
counters, countert=Counter(s),Counter(t)
counter_diff = dict(counters - countert)
return sum(counter_diff.values()) | minimum-number-of-steps-to-make-two-strings-anagram | Python using Counter, 3 lines of code | icey5566 | 0 | 71 | minimum number of steps to make two strings anagram | 1,347 | 0.774 | Medium | 20,319 |
https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram/discuss/2208433/Python-1-Line!!! | class Solution:
def minSteps(self, s: str, t: str) -> int:
return sum([collections.Counter(t)[i]-collections.Counter(s)[i] for i in collections.Counter(t).keys() if collections.Counter(t)[i]>collections.Counter(s)[i]]) | minimum-number-of-steps-to-make-two-strings-anagram | Python 1-Line!!! | XRFXRF | 0 | 82 | minimum number of steps to make two strings anagram | 1,347 | 0.774 | Medium | 20,320 |
https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram/discuss/2111250/python-3-oror-two-line-hash-map-solution-oror-O(n)O(1) | class Solution:
def minSteps(self, s: str, t: str) -> int:
countS, countT = collections.Counter(s), collections.Counter(t)
return sum(max(0, countS[c] - countT[c]) for c in countS) | minimum-number-of-steps-to-make-two-strings-anagram | python 3 || two line hash map solution || O(n)/O(1) | dereky4 | 0 | 129 | minimum number of steps to make two strings anagram | 1,347 | 0.774 | Medium | 20,321 |
https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram/discuss/1993237/one-line-o(n)-sum((Counter(s)-Counter(t)).values()) | class Solution:
def minSteps(self, s: str, t: str) -> int:
return sum((Counter(s) - Counter(t)).values()) | minimum-number-of-steps-to-make-two-strings-anagram | one line o(n) sum((Counter(s) - Counter(t)).values()) | BichengWang | 0 | 33 | minimum number of steps to make two strings anagram | 1,347 | 0.774 | Medium | 20,322 |
https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram/discuss/1950970/Python-HashMap-Solution-62-faster-and-80-less-memory | class Solution:
def minSteps(self, s: str, t: str) -> int:
d1 ={}
d2 ={}
steps = 0
for c in s:
d1[c] = d1.get(c , 0) +1
for c in t:
d2[c] = d2.get(c , 0) +1
for key, val in d1.items():
if key in d2 and val < d2[key]:
steps += abs(d2[key] - val)
for key , val in d2.items():
if key not in d1:
steps += val
return steps | minimum-number-of-steps-to-make-two-strings-anagram | Python HashMap Solution 62% faster & 80% less memory | theReal007 | 0 | 89 | minimum number of steps to make two strings anagram | 1,347 | 0.774 | Medium | 20,323 |
https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram/discuss/1899444/1-line-straightforward-beat-100 | class Solution:
def minSteps(self, s: str, t: str) -> int:
return sum((Counter(s) - Counter(t)).values()) | minimum-number-of-steps-to-make-two-strings-anagram | 1 line straightforward beat 100% | BichengWang | 0 | 105 | minimum number of steps to make two strings anagram | 1,347 | 0.774 | Medium | 20,324 |
https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram/discuss/1838190/Simple-and-Faster-than-99.58 | class Solution:
def minSteps(self, s: str, t: str) -> int:
map1 = list(set(s))
result=0
for c in map1:
if c in t:
result+=s.count(c) - min(t.count(c), s.count(c))
else:
result+=s.count(c)
return result | minimum-number-of-steps-to-make-two-strings-anagram | Simple and Faster than 99.58% | blackmishra | 0 | 53 | minimum number of steps to make two strings anagram | 1,347 | 0.774 | Medium | 20,325 |
https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram/discuss/1661873/Python-very-easy-O(n)-time-O(n)-space-hashmap-solution | class Solution:
def minSteps(self, s: str, t: str) -> int:
cnt1 = defaultdict(int)
cnt2 = defaultdict(int)
for c in s:
cnt1[c] += 1
for c in t:
cnt2[c] += 1
for i in range(26):
x = chr(ord('a') + i)
if x in cnt1 and cnt2:
m = min(cnt1[x], cnt2[x])
cnt1[x] -= m
cnt2[x] -= m
res = sum(cnt1.values()) + sum(cnt2.values())
return res // 2 | minimum-number-of-steps-to-make-two-strings-anagram | Python very easy O(n) time, O(n) space hashmap solution | byuns9334 | 0 | 133 | minimum number of steps to make two strings anagram | 1,347 | 0.774 | Medium | 20,326 |
https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram/discuss/1544387/Easy-Python-3-Solution | class Solution:
def minSteps(self, s: str, t: str) -> int:
s_c = Counter(s)
t_c = Counter(t)
c = 0
for k in s_c:
if k in t_c:
s_c[k] -= min(s_c[k], t_c[k])
c += s_c[k]
return c | minimum-number-of-steps-to-make-two-strings-anagram | Easy Python 3 Solution | chakkakuru | 0 | 157 | minimum number of steps to make two strings anagram | 1,347 | 0.774 | Medium | 20,327 |
https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram/discuss/1524404/Python-using-two-Counter's | class Solution:
def minSteps(self, s: str, t: str) -> int:
s_counter = Counter(s)
t_counter = Counter(t)
return sum(max(count-s_counter[char], 0) for char, count in t_counter.items()) | minimum-number-of-steps-to-make-two-strings-anagram | Python, using two Counter's | blue_sky5 | 0 | 121 | minimum number of steps to make two strings anagram | 1,347 | 0.774 | Medium | 20,328 |
https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram/discuss/1483747/Python3-solution-explained-or-comments-or-O(n)-~-hashmap | class Solution:
def minSteps(self, s: str, t: str) -> int:
if s == t:
return 0
d_s = dict(collections.Counter(s))
d_t = dict(collections.Counter(t))
for i in range(len(t)): # adding the rest of elements from "t" to d_s with all the values = 0 (because they are not in s)
if t[i] not in d_s:
d_s[t[i]] = 0
for i in range(len(s)): # same for this one
if s[i] not in d_t:
d_t[s[i]] = 0
count = 0 # counting how many letters are different (the letters that are in one dictionary but not in the other, same for the latter)
for i, ele in enumerate(d_s):
count += abs(d_s[ele] - d_t[ele])
return count // 2 # we return count // 2 because if you got a number of letters you will always need to make number/2 moves to equilibrate the strings | minimum-number-of-steps-to-make-two-strings-anagram | Python3 solution explained | comments | O(n) ~ hashmap | FlorinnC1 | 0 | 142 | minimum number of steps to make two strings anagram | 1,347 | 0.774 | Medium | 20,329 |
https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram/discuss/1177058/Short-and-Simple-Solution-using-Dictionary | class Solution:
def minSteps(self, s: str, t: str) -> int:
dicS = Counter(s)
dicT = Counter(t)
ans = 0
for i in dicS:
ans+=max(0,dicS[i]-dicT.get(i,0))
return ans | minimum-number-of-steps-to-make-two-strings-anagram | Short and Simple Solution using Dictionary | Anmol_Arora | 0 | 65 | minimum number of steps to make two strings anagram | 1,347 | 0.774 | Medium | 20,330 |
https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram/discuss/707991/simple-of-simple | class Solution:
def minSteps(self, s: str, t: str) -> int:
from collections import Counter
s = Counter(s)
t = Counter(t)
now = 'a'
diff = 0
for i in range(26):
diff += abs(s[now] - t[now])
now = chr(ord(now) + 1)
return int(diff/2) | minimum-number-of-steps-to-make-two-strings-anagram | simple of simple | seunggabi | 0 | 54 | minimum number of steps to make two strings anagram | 1,347 | 0.774 | Medium | 20,331 |
https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram/discuss/592335/Easy-python-solution-using-dictionary-O(n) | class Solution:
def minSteps(self, s: str, t: str) -> int:
ds={}
dt={}
for i in range(len(s)):
ds[s[i]]=0
dt[t[i]]=0
for i in range(len(s)):
ds[s[i]]+=1
dt[t[i]]+=1
count=0
print(ds)
print(dt)
visited=[]
for i in range(len(s)):
if s[i] not in visited:
visited.append(s[i])
if s[i] in dt.keys():
if ds[s[i]]==dt[s[i]]:
continue
else:
if ds[s[i]]>dt[s[i]]:
count+=ds[s[i]]-dt[s[i]]
else:
count+=ds[s[i]]
return count | minimum-number-of-steps-to-make-two-strings-anagram | Easy python solution using dictionary - O(n) | Ayu-99 | 0 | 72 | minimum number of steps to make two strings anagram | 1,347 | 0.774 | Medium | 20,332 |
https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram/discuss/1078717/python-easy-and-fast-2-line-solution | class Solution:
def minSteps(self, s: str, t: str) -> int:
x=list((Counter(s) & Counter(t)).elements())
return len(s)-len(x) | minimum-number-of-steps-to-make-two-strings-anagram | python easy and fast 2 line solution | Underdog2000 | -2 | 260 | minimum number of steps to make two strings anagram | 1,347 | 0.774 | Medium | 20,333 |
https://leetcode.com/problems/maximum-students-taking-exam/discuss/1899957/Python-Bitmasking-dp-solution-with-explanation | class Solution:
def maxStudents(self, seats: list[list[str]]) -> int:
def count_bits(num: int) -> int:
# Count how many bits having value 1 in num.
cnt = 0
while num:
cnt += 1
num &= num - 1
return cnt
R, C = len(seats), len(seats[0])
validSeats = []
# Calculate valid seats mask for each row.
for row in seats:
curr = 0
for seat in row:
curr = (curr << 1) + (seat == '.')
validSeats.append(curr)
# dp[i][mask] stands for the maximum students on ith row with students
# following the mask.
dp = [[-1] * (1 << C) for _ in range(R + 1)]
dp[0][0] = 0
for r in range(1, R + 1):
seatMask = validSeats[r - 1]
for studentMask in range(1 << C):
validBits = count_bits(studentMask)
# 1. Check if a student mask is a subset of seatMask so that
# the target student could sit on a seat.
# 2. The student should not sit next to each other.
if (
studentMask & seatMask == studentMask and
studentMask & (studentMask >> 1) == 0
):
# Then check the upper student mask and make sure that
# 1. no student is on the upper left.
# 2. no student is on the upper right.
# Then the upper mask is a valid candidate for the current
# student mask.
for upperStudentMask in range(1 << C):
if (
studentMask & (upperStudentMask >> 1) == 0 and
studentMask & (upperStudentMask << 1) == 0 and
dp[r - 1][upperStudentMask] != -1
):
dp[r][studentMask] = max(
dp[r][studentMask],
dp[r - 1][upperStudentMask] + validBits
)
return max(dp[-1]) | maximum-students-taking-exam | [Python] Bitmasking dp solution with explanation | eroneko | 0 | 56 | maximum students taking exam | 1,349 | 0.483 | Hard | 20,334 |
https://leetcode.com/problems/maximum-students-taking-exam/discuss/1208934/Python3-bitmask-dp | class Solution:
def maxStudents(self, seats: List[List[str]]) -> int:
m, n = len(seats), len(seats[0]) # dimensions
valid = []
for i in range(m):
val = 0
for j in range(n):
if seats[i][j] == ".": val |= 1 << j
valid.append(val)
@cache
def fn(i, mask):
"""Return max students taking seats[i:] given previous row as mask."""
if i == len(seats): return 0
ans = fn(i+1, 0)
for x in range(1 << n):
if x & valid[i] == x and (x >> 1) & x == 0 and (mask >> 1) & x == 0 and (mask << 1) & x == 0:
ans = max(ans, bin(x).count("1") + fn(i+1, x))
return ans
return fn(0, 0) | maximum-students-taking-exam | [Python3] bitmask dp | ye15 | 0 | 203 | maximum students taking exam | 1,349 | 0.483 | Hard | 20,335 |
https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/discuss/2193369/Python3-slight-tweak-in-binary-search | class Solution:
def countNegatives(self, grid: List[List[int]]) -> int:
result = 0
rows = len(grid)
cols = len(grid[0])
i = 0
j = cols - 1
while i < rows and j>=0:
curr = grid[i][j]
if(curr < 0):
j-=1
else:
result+=((cols-1) - j) #capture the no.of negative number in this row and jump to next row
i+=1
#left out negative rows
while i < rows:
result+=cols
i+=1
return result | count-negative-numbers-in-a-sorted-matrix | 📌 Python3 slight tweak in binary search | Dark_wolf_jss | 6 | 73 | count negative numbers in a sorted matrix | 1,351 | 0.752 | Easy | 20,336 |
https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/discuss/1655202/Python-Easy-Solution-or-O(m-%2B-n)-Approach | # Approach 1: Brute Force Solution
# Time: O(mn)
# Space: O(n+m)
class Solution:
def countNegatives(self, grid: List[List[int]]) -> int:
return sum([1 for i in grid for j in i if j < 0])
# Approach 2: Optimal Solution
# Time: O(n + m)
# Space: O(1)
class Solution:
def countNegatives(self, grid: List[List[int]]) -> int:
n = len(grid[0]) # O(n)
row = len(grid)-1 # O(m)
clmn = 0
cnt_neg = 0
while row >= 0 and clmn < n: # O(n) + O(m)
if grid[row][clmn] < 0:
cnt_neg += n-clmn
row -= 1
else:
clmn += 1
return cnt_neg | count-negative-numbers-in-a-sorted-matrix | Python Easy Solution | O(m + n) Approach ✔ | leet_satyam | 4 | 238 | count negative numbers in a sorted matrix | 1,351 | 0.752 | Easy | 20,337 |
https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/discuss/745027/Python3-or-Binary-Search-or-Straight-Forward | class Solution:
def countNegatives(self, grid: List[List[int]]) -> int:
def bin(row):
l,h=0,len(row)
while (l<h):
m=(l+h)//2
if (row[m] <0):
h=m
elif (g[m]>=0):
l=m+1
return len(row)-h
count=0
for g in grid:
print(bin(g))
count=count+bin(g)
return count | count-negative-numbers-in-a-sorted-matrix | Python3 | Binary Search | Straight Forward | donpauly | 2 | 415 | count negative numbers in a sorted matrix | 1,351 | 0.752 | Easy | 20,338 |
https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/discuss/2848864/PYTHON-SOLUTION-BINARY-SEARCH-oror-EXPLAINED-100 | class Solution:
def countNegatives(self, grid: List[List[int]]) -> int:
#BINARY SEARCH
negative=0 #total negatives
for i in grid: #traverse every row of grid
l=0 #initial index of row
h=len(i)-1 #last index of that row
while l<=h: #traverse all indeces of any row
mid=(l+h)//2 #middle index
if i[mid]>=0: #if element is positive then no negative count
l=mid+1 #then check ->negative may be present after mid element
else:
#negative count-> from the element found negative,after that index
#every element present in non-increasing sorted row will have negatives
negative+=h-mid+1
h=mid-1 #then check if negatives present before that mid element
return negative | count-negative-numbers-in-a-sorted-matrix | PYTHON SOLUTION - BINARY SEARCH || EXPLAINED 100% | T1n1_B0x1 | 1 | 3 | count negative numbers in a sorted matrix | 1,351 | 0.752 | Easy | 20,339 |
https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/discuss/2334612/Python-Simple-Python-Solution-With-Two-Approach | class Solution:
def countNegatives(self, grid: List[List[int]]) -> int:
def BinarySearch(array):
low = 0
high = len(array) - 1
while low <= high:
mid = (low + high ) // 2
if array[mid] < 0:
high = mid - 1
elif array[mid] >= 0:
low = mid + 1
return low
result = 0
for row in grid:
result = result + len(row) - BinarySearch(row)
return result | count-negative-numbers-in-a-sorted-matrix | [ Python] ✅✅ Simple Python Solution With Two Approach 🥳✌👍 | ASHOK_KUMAR_MEGHVANSHI | 1 | 71 | count negative numbers in a sorted matrix | 1,351 | 0.752 | Easy | 20,340 |
https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/discuss/2334612/Python-Simple-Python-Solution-With-Two-Approach | class Solution:
def countNegatives(self, grid: List[List[int]]) -> int:
count = 0
for row in grid:
for num in row:
if num < 0:
count = count + 1
return count | count-negative-numbers-in-a-sorted-matrix | [ Python] ✅✅ Simple Python Solution With Two Approach 🥳✌👍 | ASHOK_KUMAR_MEGHVANSHI | 1 | 71 | count negative numbers in a sorted matrix | 1,351 | 0.752 | Easy | 20,341 |
https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/discuss/2119641/python-3-oror-optimal-solution-oror-O(m-%2B-n)O(1) | class Solution:
def countNegatives(self, grid: List[List[int]]) -> int:
n = len(grid[0])
j = 0
res = 0
for row in reversed(grid):
while j != n and row[j] >= 0:
j += 1
res += n - j
return res | count-negative-numbers-in-a-sorted-matrix | python 3 || optimal solution || O(m + n)/O(1) | dereky4 | 1 | 112 | count negative numbers in a sorted matrix | 1,351 | 0.752 | Easy | 20,342 |
https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/discuss/1928309/easy-python-code | class Solution:
def countNegatives(self, grid: List[List[int]]) -> int:
count = 0
for i in grid:
for j in i:
if j < 0:
count += 1
return count | count-negative-numbers-in-a-sorted-matrix | easy python code | dakash682 | 1 | 60 | count negative numbers in a sorted matrix | 1,351 | 0.752 | Easy | 20,343 |
https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/discuss/1710484/Faster-than-100-of-Python3-submissions | class Solution:
def countNegatives(self, grid: List[List[int]]) -> int:
r=0 #r refers to row
c=len(grid[0])-1 #c refers to column
n=len(grid)
res=0
while c>=0 and r<n:
if grid[r][c]<0:
res=res+n-r
c=c-1
else:
r=r+1
return res
#once trace an example you will get it.
#feel free to ask doubts. | count-negative-numbers-in-a-sorted-matrix | Faster than 100% of Python3 submissions | _SID_ | 1 | 99 | count negative numbers in a sorted matrix | 1,351 | 0.752 | Easy | 20,344 |
https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/discuss/1318349/faster-than-99.45-of-Python-online-submissions | class Solution(object):
def countNegatives(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
row=len(grid)
cols=len(grid[0])
count=0
i=row-1
j=0
while i>=0 and j< cols:
if grid[i][j]<0:
count+=(cols-j)
i-=1
else:
j+=1
return count | count-negative-numbers-in-a-sorted-matrix | faster than 99.45% of Python online submissions | mobashshir | 1 | 210 | count negative numbers in a sorted matrix | 1,351 | 0.752 | Easy | 20,345 |
https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/discuss/1128363/Simple-Python-Solution-or-Faster-than-98-(108-ms) | class Solution:
def countNegatives(self, grid: List[List[int]]) -> int:
c = 0
for i in grid:
for j in i:
if j < 0:
c += 1
return c | count-negative-numbers-in-a-sorted-matrix | Simple Python Solution | Faster than 98% (108 ms) | Annushams | 1 | 244 | count negative numbers in a sorted matrix | 1,351 | 0.752 | Easy | 20,346 |
https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/discuss/857655/Python-3-BruteForce-BinarySearch-and-BFS-solutions | class Solution:
def countNegatives(self, grid: List[List[int]]) -> int:
result = 0
m = len(grid)
n = len(grid[0])
for i in range(m):
for j in range(n):
if grid[i][j] < 0: result += 1
return result | count-negative-numbers-in-a-sorted-matrix | [Python 3] BruteForce, BinarySearch & BFS solutions | abstractart | 1 | 101 | count negative numbers in a sorted matrix | 1,351 | 0.752 | Easy | 20,347 |
https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/discuss/857655/Python-3-BruteForce-BinarySearch-and-BFS-solutions | class Solution:
def countNegatives(self, grid: List[List[int]]) -> int:
result = 0
m = len(grid)
for i in range(m):
result += self.countNegativesInSortedList(grid[i], 0, len(grid[i]) - 1)
return result
def countNegativesInSortedList(self, row, start, finish):
if start > finish: return 0
middle = (finish + start) // 2
if row[middle] >= 0:
return self.countNegativesInSortedList(row, middle + 1, finish)
else:
return self.countNegativesInSortedList(row, middle + 1, finish) + self.countNegativesInSortedList(row, start, middle - 1) + 1 | count-negative-numbers-in-a-sorted-matrix | [Python 3] BruteForce, BinarySearch & BFS solutions | abstractart | 1 | 101 | count negative numbers in a sorted matrix | 1,351 | 0.752 | Easy | 20,348 |
https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/discuss/821650/Python-oror-Binary-Search | class Solution:
def countNegatives(self, grid: List[List[int]]) -> int:
def bs(row):
l, r = 0, len(row)
while l < r:
mid = l + (r-l) // 2
if row[mid] < 0: r = mid
else: l = mid + 1
return len(row) - l
count = 0
for row in grid:
count += bs(row)
return count
# TC: O(mlogn)
# SC: O(1) | count-negative-numbers-in-a-sorted-matrix | Python || Binary Search | airksh | 1 | 81 | count negative numbers in a sorted matrix | 1,351 | 0.752 | Easy | 20,349 |
https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/discuss/510329/Python3-linear-scan-from-top-right-to-bottom-left | class Solution:
def countNegatives(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
ans, j = 0, n-1
for i in range(m):
while j >= 0 and grid[i][j] < 0: j -= 1
ans += n-1-j
return ans | count-negative-numbers-in-a-sorted-matrix | [Python3] linear scan from top-right to bottom-left | ye15 | 1 | 86 | count negative numbers in a sorted matrix | 1,351 | 0.752 | Easy | 20,350 |
https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/discuss/2836629/Count-Negative-Number-or-Python | class Solution:
def countNegatives(self, grid: List[List[int]]) -> int:
'''
#Brute Force Approach
count = 0
row = len(grid)
col = len(grid[0])
for i in range(row):
for j in range(col):
if grid[i][j] < 0:
count += 1
return count
'''
def bin(row):
start, end = 0, len(row)
while start<end:
mid = start +(end -start) // 2
if row[mid]<0:
end = mid
else:
start = mid+1
return len(row)- start
count = 0
for row in grid:
count += bin(row)
return(count) | count-negative-numbers-in-a-sorted-matrix | Count Negative Number | Python | jashii96 | 0 | 1 | count negative numbers in a sorted matrix | 1,351 | 0.752 | Easy | 20,351 |
https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/discuss/2797847/Python-one-liner | class Solution:
def countNegatives(self, grid: List[List[int]]) -> int:
return sum([
bisect_left(el[::-1],0)
for el in grid
]) | count-negative-numbers-in-a-sorted-matrix | Python one liner | CabianoFaruana | 0 | 2 | count negative numbers in a sorted matrix | 1,351 | 0.752 | Easy | 20,352 |
https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/discuss/2780595/Beats-100-of-Solutions-Python3-logn-x-m-solution | class Solution:
def countNegatives(self, grid: List[List[int]]) -> int:
n, res, fo = len(grid[0]), 0, 0
def bisect_neg(arr, start):
i, j = start, n
while i<j:
mid = (i+j)//2
if arr[mid]>=0: i = mid+1
else: j = mid
return i
for i in range(len(grid)-1, -1, -1):
if grid[i][fo]>=0: idx = bisect_neg(grid[i], fo)
if idx == n: return res
res += n - idx
return res | count-negative-numbers-in-a-sorted-matrix | Beats 100% of Solutions - Python3 - logn x m solution | ashishranjan2404 | 0 | 3 | count negative numbers in a sorted matrix | 1,351 | 0.752 | Easy | 20,353 |
https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/discuss/2769785/Python-solution | class Solution:
def countNegatives(self, grid: List[List[int]]) -> int:
count=0
for i in grid:
for j in i:
if j<0:
count+=1
return count | count-negative-numbers-in-a-sorted-matrix | Python solution | SheetalMehta | 0 | 4 | count negative numbers in a sorted matrix | 1,351 | 0.752 | Easy | 20,354 |
https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/discuss/2746690/simple-and-easy-python-solution | class Solution:
def countNegatives(self, grid: List[List[int]]) -> int:
c=0
for i in range(len(grid)):
start=grid[i]
for j in range(len(start)):
if start[j]<0:
c+=1
return c | count-negative-numbers-in-a-sorted-matrix | simple and easy python solution | insane_me12 | 0 | 3 | count negative numbers in a sorted matrix | 1,351 | 0.752 | Easy | 20,355 |
https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/discuss/2746689/simple-and-easy-python-solution | class Solution:
def countNegatives(self, grid: List[List[int]]) -> int:
c=0
for i in range(len(grid)):
start=grid[i]
for j in range(len(start)):
if start[j]<0:
c+=1
return c | count-negative-numbers-in-a-sorted-matrix | simple and easy python solution | insane_me12 | 0 | 2 | count negative numbers in a sorted matrix | 1,351 | 0.752 | Easy | 20,356 |
https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/discuss/2740018/Python-NumPy | class Solution:
def countNegatives(self, grid: List[List[int]]) -> int:
import numpy as np
grid=np.array(grid)
return np.count_nonzero(grid<0) | count-negative-numbers-in-a-sorted-matrix | Python NumPy | Leox2022 | 0 | 1 | count negative numbers in a sorted matrix | 1,351 | 0.752 | Easy | 20,357 |
https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/discuss/2710208/Simple-solution | class Solution:
def countNegatives(self, grid: List[List[int]]) -> int:
c = 0
for i in grid:
for j in range(-1,-len(i)-1,-1):
if i[j] < 0: c += 1
else: break
return c | count-negative-numbers-in-a-sorted-matrix | Simple solution | MockingJay37 | 0 | 1 | count negative numbers in a sorted matrix | 1,351 | 0.752 | Easy | 20,358 |
https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/discuss/2681612/Simple-python-code-with-explanation | class Solution:
def countNegatives(self, grid):
#create a variable count and assign 0 to it
count = 0
#iterate over the lists in the grid
for i in range(len(grid)):
#iterate over the elements in the each sublist
for j in range(len(grid[i])):
#if the element is less than 0
if grid[i][j] < 0:
#then increase the count value by 1
count = count + 1
#after finishing the forloop
#return count
return count | count-negative-numbers-in-a-sorted-matrix | Simple python code with explanation | thomanani | 0 | 22 | count negative numbers in a sorted matrix | 1,351 | 0.752 | Easy | 20,359 |
https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/discuss/2671762/Python-2-solutions-Clean-and-Concise | class Solution:
def countNegatives(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
ans = 0
for r in range(m):
left = 0
right = n - 1
firstNegative = n
while left <= right:
mid = (left + right) // 2
if grid[r][mid] < 0:
firstNegative = mid
right = mid - 1
else:
left = mid + 1
ans += n - firstNegative
return ans | count-negative-numbers-in-a-sorted-matrix | [Python] 2 solutions - Clean & Concise | hiepit | 0 | 15 | count negative numbers in a sorted matrix | 1,351 | 0.752 | Easy | 20,360 |
https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/discuss/2671762/Python-2-solutions-Clean-and-Concise | class Solution:
def countNegatives(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
ans = 0
for r in range(m):
c = n-1
while c >= 0 and grid[r][c] < 0:
c -= 1
ans += n - c - 1
return ans | count-negative-numbers-in-a-sorted-matrix | [Python] 2 solutions - Clean & Concise | hiepit | 0 | 15 | count negative numbers in a sorted matrix | 1,351 | 0.752 | Easy | 20,361 |
https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/discuss/2646381/Python-Solution-or-Two-Ways-One-Liner-Binary-Search-99-Faster | class Solution:
# ONE LINER
def countNegatives(self, grid: List[List[int]]) -> int:
return len([cell for row in grid for cell in row if cell < 0])
# BINARY SEARCH
def countNegatives(self, grid: List[List[int]]) -> int:
def BS(row):
low = 0
high = len(row) - 1
while low <= high:
mid = low + (high - low) // 2
if row[mid] < 0:
high = mid - 1
else:
low = mid + 1
return len(row) - low
ans = 0
for row in grid:
ans += BS(row)
return ans | count-negative-numbers-in-a-sorted-matrix | Python Solution | Two Ways - One Liner / Binary Search - 99% Faster | Gautam_ProMax | 0 | 20 | count negative numbers in a sorted matrix | 1,351 | 0.752 | Easy | 20,362 |
https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/discuss/2622633/Python | class Solution:
def countNegatives(self, grid: List[List[int]]) -> int:
negative_count = 0
for line in grid:
left = 0
right = len(line) - 1
while left <= right:
mid = (left + right) // 2
if 0 <= line[mid]:
left = mid + 1
else:
negative_count += 1
right -= 1
return negative_count
if __name__ == '__main__':
grid = [[4,3,2,-1],[3,2,1,-1],[1,1,-1,-2],[-1,-1,-2,-3]]
# grid = [[3,2],[1,0]]
S = Solution()
print(S.countNegatives(grid))
# >>> 8 | count-negative-numbers-in-a-sorted-matrix | Python答え | namashin | 0 | 2 | count negative numbers in a sorted matrix | 1,351 | 0.752 | Easy | 20,363 |
https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/discuss/2583171/SIMPLE-PYTHON3-SOLUTION-beginner-friendly | class Solution:
def countNegatives(self, grid: List[List[int]]) -> int:
count = 0
for i in grid:
for j in range(len(i)):
if i[j]<0:
count += 1
return count | count-negative-numbers-in-a-sorted-matrix | ✅✔ SIMPLE PYTHON3 SOLUTION ✅✔beginner friendly | rajukommula | 0 | 14 | count negative numbers in a sorted matrix | 1,351 | 0.752 | Easy | 20,364 |
https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/discuss/2530788/Python3-or-Solved-Using-Binary-Search-%2B-Finding-Leftmost-Negative-Logic! | class Solution:
#Time-Complexity: O(m*log(n))
#Space-Complexity: O(1)
def countNegatives(self, grid: List[List[int]]) -> int:
def binary_search(a):
#intialize search space to consist of entire array A!
L, R = 0, (len(a) - 1)
#initialize index answer as length of array a since initially we assume
#array a has no negatives until we find one!
answer = len(a)
#as long as search space has at least one element to consider...
while L <= R:
mid = (L + R) // 2
middle = a[mid]
#if current middle is pos. neg elements may exist to the right!
if(middle >= 0):
L = mid + 1
continue
#if current middle element is negative, update the answer!
else:
answer = mid
#search to the left of mid to find more leftmost postioned negatives, if it
#exists!
R = mid - 1
continue
return answer
#always get dimension of grid!
r, c = len(grid), len(grid[0])
#initialize ans: store total number of neg. elements seen so far!
ans = 0
#iterate through each and every row of grid!
for row in grid:
#perform binary search to find the leftmost index pos. of the first neg. element
#in given row!
first_neg_index = binary_search(row)
#add total number of negatives in current row to accumulator or counter!
ans += (c - first_neg_index)
return ans | count-negative-numbers-in-a-sorted-matrix | Python3 | Solved Using Binary Search + Finding Leftmost Negative Logic! | JOON1234 | 0 | 24 | count negative numbers in a sorted matrix | 1,351 | 0.752 | Easy | 20,365 |
https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/discuss/2476310/Fast-python-solution-without-using-binary-search-using-break-when-see-positive-int | class Solution:
def countNegatives(self, grid: List[List[int]]) -> int:
count = 0
for li in grid:
for j in reversed(li):
if j < 0:
count += 1
else:
break
return count | count-negative-numbers-in-a-sorted-matrix | Fast python solution without using binary search, using break when see positive int | samanehghafouri | 0 | 22 | count negative numbers in a sorted matrix | 1,351 | 0.752 | Easy | 20,366 |
https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/discuss/2432165/easy-python-code-or-O(n2) | class Solution:
def countNegatives(self, grid: List[List[int]]) -> int:
count = 0
for i in reversed(grid):
for j in reversed(i):
if j < 0:
count += 1
else:
break
return count | count-negative-numbers-in-a-sorted-matrix | easy python code | O(n^2) | dakash682 | 0 | 24 | count negative numbers in a sorted matrix | 1,351 | 0.752 | Easy | 20,367 |
https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/discuss/2380002/Python-Short-Faster-Solution-Two-Pointers | class Solution:
def countNegatives(self, grid: List[List[int]]) -> int:
cols = len(grid[0])
result, col = 0, cols-1
# check for each row
for row in grid:
while col >= 0 and row[col] < 0:
col -= 1
result += cols - 1 - col
return result | count-negative-numbers-in-a-sorted-matrix | [Python] Short Faster Solution - Two Pointers | Buntynara | 0 | 17 | count negative numbers in a sorted matrix | 1,351 | 0.752 | Easy | 20,368 |
https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/discuss/2379308/Easily-Readable-binary-search-solution-python3-O(nlogm)-time | class Solution:
# O(nlogm) time, n --> number of rows, m --> the longest col
# O(1) space,
# Approach: binary search,
def countNegatives(self, grid: List[List[int]]) -> int:
n = len(grid)
tot = 0
def findFirstNegativeIndex(start, end, lst) -> int:
mid = (start + end)//2
num = lst[mid]
if num < 0 and (mid == 0 or lst[mid-1] >= 0):
return mid
if start >= end-1:
if lst[end] < 0:
return end
return end+1
if num >= 0:
return findFirstNegativeIndex(mid, end, lst)
else:
return findFirstNegativeIndex(start, mid, lst)
for rowNum in range(n):
row = grid[rowNum]
m = len(row)
index = findFirstNegativeIndex(0, m-1, row)
tot += (m-index)
return tot | count-negative-numbers-in-a-sorted-matrix | Easily Readable binary search solution, python3, O(nlogm) time | destifo | 0 | 17 | count negative numbers in a sorted matrix | 1,351 | 0.752 | Easy | 20,369 |
https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/discuss/2377619/Python-Solution-Binary-Search | class Solution:
def countNegatives(self, grid: List[List[int]]) -> int:
count = 0
for i in grid:
low = 0
high = len(i) - 1
while low <= high:
mid = (low+high)//2
if i[mid] < 0:
high = mid - 1
elif i[mid] >= 0:
low = mid + 1
count += (len(i) - low)
return count | count-negative-numbers-in-a-sorted-matrix | Python Solution [Binary Search] | Yauhenish | 0 | 46 | count negative numbers in a sorted matrix | 1,351 | 0.752 | Easy | 20,370 |
https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/discuss/2362865/or-Python-3-or-Binary-Search-Solution-or-Explanation-or | class Solution:
def countNegatives(self, grid: List[List[int]]) -> int:
answer = 0
for arr in grid:
# Add array length into answer if the first element is negative
if arr[0] < 0:
answer += len(arr)
continue
# If the last element of array is not negative, skip to the next array
if arr[-1] >= 0:
continue
l = 0
r = len(arr) - 1
# Do binary Search
while l <= r:
m = l + (r - l) // 2
# If the array in the m-th index is not negative,
# decrease the range of search to the right
if arr[m] >= 0:
l = m + 1
# if it is negative, decrease range of search to the left
else:
r = m - 1
# Safeguard whenever arr[m] is not negative
while arr[m] >= 0:
m += 1
# Add the answer from m to array length - 1
answer += len(arr) - m
return answer | count-negative-numbers-in-a-sorted-matrix | | Python 3 | Binary Search Solution | Explanation | | YudoTLE | 0 | 20 | count negative numbers in a sorted matrix | 1,351 | 0.752 | Easy | 20,371 |
https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/discuss/2358898/Python-1-Liner-with-sum()-trick | class Solution:
def countNegatives(self, grid: List[List[int]]) -> int:
return len([n for n in sum(grid, []) if n < 0]) | count-negative-numbers-in-a-sorted-matrix | Python 1-Liner with sum() trick | amaargiru | 0 | 19 | count negative numbers in a sorted matrix | 1,351 | 0.752 | Easy | 20,372 |
https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/discuss/2323242/C%2B%2BPythonO(mxn)-Approach-and-Binary-Search-Solution-as-well | class Solution:
def countNegatives(self, grid: List[List[int]]) -> int:
row = len(grid)
col = len(grid[0])
count = 0
for i in range(row):
for j in range(col):
if(grid[i][j]<0):
count = count+1
return count | count-negative-numbers-in-a-sorted-matrix | C++/Python/O(mxn) Approach and Binary Search Solution as well | arpit3043 | 0 | 18 | count negative numbers in a sorted matrix | 1,351 | 0.752 | Easy | 20,373 |
https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/discuss/2213936/Python-solution-for-beginners-by-beginner. | class Solution:
def countNegatives(self, grid: List[List[int]]) -> int:
ans = 0
for i in grid:
for j in i:
if j < 0:
ans += 1
return ans | count-negative-numbers-in-a-sorted-matrix | Python solution for beginners by beginner. | EbrahimMG | 0 | 28 | count negative numbers in a sorted matrix | 1,351 | 0.752 | Easy | 20,374 |
https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/discuss/2179996/Python-simple-solution | class Solution:
def countNegatives(self, grid: List[List[int]]) -> int:
ans = 0
for row in grid:
for i in row:
if i < 0:
ans += 1
return ans | count-negative-numbers-in-a-sorted-matrix | Python simple solution | StikS32 | 0 | 34 | count negative numbers in a sorted matrix | 1,351 | 0.752 | Easy | 20,375 |
https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/discuss/2054273/python-binary-search | class Solution:
def countNegatives(self, grid: List[List[int]]) -> int:
answer = 0
for i in range(len(grid)):
if grid[i][0] < 0 : answer += len(grid[0])
elif grid[i][-1] < 0 :
l = 0
r = len(grid[0]) - 1
while l <= r :
m = (l+r)>>1
if grid[i][m] >= 0 : l = m + 1
else : r = m - 1
answer += len(grid[0]) - l
return answer | count-negative-numbers-in-a-sorted-matrix | python - binary search | ZX007java | 0 | 99 | count negative numbers in a sorted matrix | 1,351 | 0.752 | Easy | 20,376 |
https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/discuss/2003422/Fastest-Beginner-Friendly-Solution137-ms | class Solution:
def countNegatives(self, grid: List[List[int]]) -> int:
count = 0
grid = sum(grid,[])
grid.sort()
for i in range(len(grid)):
if grid[i] >= 0:
break
count += 1
return count | count-negative-numbers-in-a-sorted-matrix | Fastest Beginner Friendly Solution137 ms | itsmeparag14 | 0 | 38 | count negative numbers in a sorted matrix | 1,351 | 0.752 | Easy | 20,377 |
https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/discuss/1997044/Python-3-Solution-O(nlogn)-Time-Complexity-O(1)-Space-Complexity-Binary-Search | class Solution:
def binary_search(self, nums: List[int]) -> int:
counter = 0
low, high = 0, len(nums) - 1
while low <= high:
mid = (low + high) // 2
if nums[mid] < 0:
counter += high - mid + 1
high = mid - 1
else:
low = mid + 1
return counter
def countNegatives(self, grid: List[List[int]]) -> int:
total_negatives = 0
for i in range(len(grid)):
total_negatives += self.binary_search(grid[i])
return total_negatives | count-negative-numbers-in-a-sorted-matrix | Python 3 Solution, O(nlogn) Time Complexity O(1) Space Complexity, Binary Search | AprDev2011 | 0 | 65 | count negative numbers in a sorted matrix | 1,351 | 0.752 | Easy | 20,378 |
https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/discuss/1973926/Python-Solution-or-O(n-*-log(m))-Time-Complexity-or-Faster-than-89 | class Solution:
def countNegatives(self, grid: List[List[int]]) -> int:
r, n = len(grid), len(grid[0])
c, count = 0, 0
while r > 0 and c < n:
if grid[r-1][c] < 0:
count += n - c
r -= 1
else:
c += 1
return count | count-negative-numbers-in-a-sorted-matrix | Python Solution | O(n * log(m)) Time Complexity | Faster than 89% | sayantanis23 | 0 | 65 | count negative numbers in a sorted matrix | 1,351 | 0.752 | Easy | 20,379 |
https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/discuss/1966292/Python-Beginner-friendly-solution | class Solution:
def countNegatives(self, grid: List[List[int]]) -> int:
cnt = 0
for i in grid:
for j in i:
if j<0:
cnt +=1
return cnt | count-negative-numbers-in-a-sorted-matrix | Python / Beginner friendly solution | smu_12 | 0 | 27 | count negative numbers in a sorted matrix | 1,351 | 0.752 | Easy | 20,380 |
https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/discuss/1866658/Python-easy-to-read-and-understand-or-binary-search | class Solution:
def countNegatives(self, grid: List[List[int]]) -> int:
ans = 0
m, n = len(grid), len(grid[0])
i, j = 0, n-1
while i < m and j >= 0:
#print(grid[i][j])
if grid[i][j] < 0:
ans += (m-i)
j = j-1
else:
i = i+1
return ans | count-negative-numbers-in-a-sorted-matrix | Python easy to read and understand | binary search | sanial2001 | 0 | 94 | count negative numbers in a sorted matrix | 1,351 | 0.752 | Easy | 20,381 |
https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/discuss/1807873/1-Line-Python-Solution-oror-75-Faster-oror-Memory-less-than-30 | class Solution:
def countNegatives(self, grid: List[List[int]]) -> int:
return sum([x<0 for x in chain.from_iterable(grid)]) | count-negative-numbers-in-a-sorted-matrix | 1-Line Python Solution || 75% Faster || Memory less than 30% | Taha-C | 0 | 41 | count negative numbers in a sorted matrix | 1,351 | 0.752 | Easy | 20,382 |
https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/discuss/1621115/Ultra-Easy-Python3-solution | class Solution:
def countNegatives(self, grid: List[List[int]]) -> int:
count = 0
for x in grid:
for i in x:
if i < 0: count += 1
return count | count-negative-numbers-in-a-sorted-matrix | Ultra Easy Python3 solution | LazyYuki | 0 | 83 | count negative numbers in a sorted matrix | 1,351 | 0.752 | Easy | 20,383 |
https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/discuss/1518829/Enumerate | class Solution:
def countNegatives(self, grid: List[List[int]]) -> int:
count = 0
for i in range(len(grid)):
for idx, val in enumerate(grid[i]):
if val < 0:
count+= len(grid[i])-idx
break
return count | count-negative-numbers-in-a-sorted-matrix | Enumerate | wongshennan | 0 | 34 | count negative numbers in a sorted matrix | 1,351 | 0.752 | Easy | 20,384 |
https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/discuss/1261758/Python3-Binary-serach-solution | class Solution:
def negativeCounter(self, lst: List[int]) -> int:
l = 0
r = len(lst) - 1
count = 0
while l <= r:
mid = l + (r - l)//2
if lst[mid] >= 0:
l = mid + 1
else:
count += r - mid + 1
r = mid - 1
return count
def countNegatives(self, grid: List[List[int]]) -> int:
count = 0
for item in grid:
count += self.negativeCounter(item)
return count | count-negative-numbers-in-a-sorted-matrix | Python3 Binary serach solution | menglei1025 | 0 | 169 | count negative numbers in a sorted matrix | 1,351 | 0.752 | Easy | 20,385 |
https://leetcode.com/problems/maximum-number-of-events-that-can-be-attended/discuss/726456/Python3-solution-with-detailed-explanation | # Solution 1
def maxEvents(self, events: List[List[int]]) -> int:
events = sorted(events, key=lambda x: x[1])
visited = set()
for s, e in events:
for t in range(s, e+1):
if t not in visited:
visited.add(t)
break
return len(visited) | maximum-number-of-events-that-can-be-attended | Python3 solution with detailed explanation | peyman_np | 12 | 2,400 | maximum number of events that can be attended | 1,353 | 0.329 | Medium | 20,386 |
https://leetcode.com/problems/maximum-number-of-events-that-can-be-attended/discuss/726456/Python3-solution-with-detailed-explanation | # Solution 2: it use `import heapq`
import heapq
class Solution(object):
def maxEvents(self, events):
events = sorted(events, key = lambda x:x[0]) #1
total_days = max(event[1] for event in events) #2
min_heap = [] #3
day, cnt, event_id = 1, 0, 0 #4
while day <= total_days: #5
# if no events are available to attend today, let time flies to the next available event.
if event_id < len(events) and not min_heap: #6
day = events[event_id][0] #7
# all events starting from today are newly available. add them to the heap.
while event_id < len(events) and events[event_id][0] <= day: #8
heapq.heappush(min_heap, events[event_id][1]) #9
event_id += 1 #10
# if the event at heap top already ended, then discard it.
while min_heap and min_heap[0] < day: #11
heapq.heappop(min_heap) #12
# attend the event that will end the earliest
if min_heap: #13
heapq.heappop(min_heap) #14
cnt += 1 #15
elif event_id >= len(events): #16
break # no more events to attend. so stop early to save time.
day += 1 #17
return cnt #18 | maximum-number-of-events-that-can-be-attended | Python3 solution with detailed explanation | peyman_np | 12 | 2,400 | maximum number of events that can be attended | 1,353 | 0.329 | Medium | 20,387 |
https://leetcode.com/problems/maximum-number-of-events-that-can-be-attended/discuss/1604334/Well-Coded-and-Explained-oror-Heap-oror-For-Beignners | class Solution:
def maxEvents(self, events: List[List[int]]) -> int:
m = 0
for eve in events:
m = max(m,eve[1])
events.sort(key = lambda x:(x[0],x[1]))
heap = []
res = 0
event_ind = 0
n = len(events)
for day in range(1,m+1):
# Pushing all the events in heap that starts Today
while event_ind<n and events[event_ind][0]==day:
heapq.heappush(heap,events[event_ind][1])
event_ind += 1
# Poping all the events in heap that ends Today
while heap and heap[0]<day:
heapq.heappop(heap)
# If any event is there which can be attended today then pop it and add it to res count.
if heap:
heapq.heappop(heap)
res+=1
return res | maximum-number-of-events-that-can-be-attended | 📌📌 Well-Coded & Explained || Heap || For Beignners 🐍 | abhi9Rai | 11 | 765 | maximum number of events that can be attended | 1,353 | 0.329 | Medium | 20,388 |
https://leetcode.com/problems/maximum-number-of-events-that-can-be-attended/discuss/1535629/Python3-Heap | class Solution:
def maxEvents(self, events: List[List[int]]) -> int:
# 1. person can only attend one event per day, even if there are multiple events on that day.
# 2. if there are multiple events happen at one day,
# person attend the event ends close to current day.
#. so we need a data structure hold all the event happend today,
# and sorted ascedningly. minimum heap is the data structure we need.
"""
[[1,1],[1,2],[2,2],[3,4],[4,4]]
day one events: [1,1] [1,2]
events today: heap = [1,2]
events expire today: none,heap = [1,2]
events attend: 1, heap = [2]
day two events: [1,2],[2,2]
events today: heap = [2,2]
events expire today:none, heap = [2,2]
events attend: 2 heap = [2]
day three events: [2,2][3,4]
events today: heap = [2,4]
events expire today;[1,2] heap = [4]
events attend:3 heap = []
"""
events = sorted(events,key = lambda x:(x[0],x[1]))
#determine the number of days has events
n = 0
for i in range(len(events)):
n = max(n,events[i][1])
attended = 0
day = 0
eventIdx = 0
heap =[]
for day in range(1, n + 1):
#step 1: Add all the events ending time to the heap that start today
while eventIdx < len(events) and events[eventIdx][0] == day:
heappush(heap,events[eventIdx][1])
eventIdx += 1
#step 2: Remove the events expired today
while heap and heap[0] < day:
heappop(heap)
#step 3: event that can be attended today,only one event per day.
if heap:
heappop(heap)
attended += 1
return attended | maximum-number-of-events-that-can-be-attended | [Python3] Heap | zhanweiting | 8 | 753 | maximum number of events that can be attended | 1,353 | 0.329 | Medium | 20,389 |
https://leetcode.com/problems/maximum-number-of-events-that-can-be-attended/discuss/510772/Clean-Python-3-bitwise-operation | class Solution:
def maxEvents(self, events: List[List[int]]) -> int:
events.sort(key=lambda event: event[1])
bitmask = 0
for start, end in events:
mask = ((1 << (end + 1)) - 1) ^ ((1 << start) - 1)
if cover := (~bitmask & mask):
bitmask |= cover & (-cover)
return f'{bitmask:b}'.count('1') | maximum-number-of-events-that-can-be-attended | Clean Python 3, bitwise operation | lenchen1112 | 5 | 603 | maximum number of events that can be attended | 1,353 | 0.329 | Medium | 20,390 |
https://leetcode.com/problems/maximum-number-of-events-that-can-be-attended/discuss/1067747/Clean-Python-with-explanation-and-comments | class Solution:
def maxEvents(self, events: List[List[int]]) -> int:
START, END = 0, 1
FIRST_EVENT = -1 #Makes the code cleaner
# sort events by start_time, end_time, reversed so we can pop off the first-event from the end in O(1)
events = list(reversed(sorted(events)))
#keep a heap of events we should consider on a particular day
soonest_ending_events = []
#start on day 0
current_day = 0
events_attended = 0
while events or soonest_ending_events:
# if there are no attendable events then fast-forward to the next day with attendable events
if len(soonest_ending_events) == 0 and len(events) > 0: current_day = events[FIRST_EVENT][START]
#get all of the new attendable events, and add them for consideration
while events and events[FIRST_EVENT][START] <= current_day:
end = events.pop(FIRST_EVENT)[END] #O(1) since it's from the end of the list
#order by end-time ASC
heappush(soonest_ending_events, end)
#Discard all events that have already ended
while len(soonest_ending_events) and soonest_ending_events[0] < current_day:
heappop(soonest_ending_events)
#If there are any events left, then take the first one
if len(soonest_ending_events):
heappop(soonest_ending_events)
events_attended += 1
current_day += 1
return events_attended | maximum-number-of-events-that-can-be-attended | Clean Python with explanation and comments | aclwitt | 2 | 1,000 | maximum number of events that can be attended | 1,353 | 0.329 | Medium | 20,391 |
https://leetcode.com/problems/maximum-number-of-events-that-can-be-attended/discuss/511412/Python3-sorting-based-method | class Solution:
def maxEvents(self, events: List[List[int]]) -> int:
events.sort(key=lambda x: (x[1], x[0]))
attend = set()
for start, end in events:
for day in range(start, end+1):
if day not in attend:
attend.add(day)
break
return len(attend) | maximum-number-of-events-that-can-be-attended | [Python3] sorting-based method | ye15 | 1 | 266 | maximum number of events that can be attended | 1,353 | 0.329 | Medium | 20,392 |
https://leetcode.com/problems/construct-target-array-with-multiple-sums/discuss/2189540/Python-Easy-Solution-oror-Less-Line-Of-Code-oror-Heapify | class Solution:
def isPossible(self, target: List[int]) -> bool:
heapq._heapify_max(target)
s = sum(target)
while target[0] != 1:
sub = s - target[0]
if sub == 0: return False
n = max((target[0] - 1) // sub, 1)
s -= n * sub
target0 = target[0] - n * sub
if target0 < 1: return False
heapq._heapreplace_max(target, target0)
return True
``` | construct-target-array-with-multiple-sums | Python Easy Solution || Less Line Of Code || Heapify | vaibhav0077 | 14 | 1,400 | construct target array with multiple sums | 1,354 | 0.363 | Hard | 20,393 |
https://leetcode.com/problems/construct-target-array-with-multiple-sums/discuss/1200292/Python3-priority-queue | class Solution:
def isPossible(self, target: List[int]) -> bool:
if len(target) == 1: return target[0] == 1 # edge case
total = sum(target)
pq = [-x for x in target] # max heap
heapify(pq)
while -pq[0] > 1:
x = -heappop(pq)
total -= x
if x <= total: return False
x = (x-1) % total + 1
heappush(pq, -x)
total += x
return True | construct-target-array-with-multiple-sums | [Python3] priority queue | ye15 | 1 | 91 | construct target array with multiple sums | 1,354 | 0.363 | Hard | 20,394 |
https://leetcode.com/problems/construct-target-array-with-multiple-sums/discuss/2192841/python-solution-with-some-comments | class Solution:
def __isPossible(self, target: List[int], targetTotal: int) -> bool:
while 1:
if targetTotal == len(target): # All elements must be 1
return True
if targetTotal < len(target): # One element must be < 1
return False
# Can I reach a collection where we have every number but the maximum one
# and the sum total of the existing numbers will be same as the maximum?
# The means I need to get to an array where we have all the numbers except
# for the max one and a different number n' that is smaller than max.
maxNo = target[0] * (-1)
totalExcludingMax = targetTotal - maxNo
if totalExcludingMax == 0:
# this is possible only when the array contains only one element
# if that element is 1 then that condition is already covered.
return False
# nPrime = maxNo - totalExcludingMax
# If nPrime still remains max then we'll subtract totalExcludingMax from it
# and we'll continue doing it either until it becomes negative or < next max
# number in the array. That may happen only when at least
# nPrime is < totalExcludingMax. This number can be achieved as
# nPrime = maxNo % totalExcludingMax.
# At this nPrime may still be bigger than the next max number. If that
# condition happens then nPrime will become negative in the next run and
# the loop will end with a failure.
if totalExcludingMax >= maxNo:
return False # We cannot end up with a number < 1
nPrime = maxNo % totalExcludingMax
if nPrime < 1:
nPrime += totalExcludingMax # Only subtract until it stays positive
heapq.heapreplace(target, -1 * nPrime)
targetTotal = totalExcludingMax + nPrime # maxNo should be the previous target
def isPossible(self, target: List[int]) -> bool:
targetHeap = [-1 * k for k in target] # Order doesn't matter
# If generation is possible then it is possible
# irrespective of the orders
#print(target)
heapq.heapify(targetHeap)
#print(target)
return self.__isPossible(targetHeap, sum(target)) | construct-target-array-with-multiple-sums | python solution with some comments | pmajumdar1976 | 0 | 10 | construct target array with multiple sums | 1,354 | 0.363 | Hard | 20,395 |
https://leetcode.com/problems/construct-target-array-with-multiple-sums/discuss/2190767/Python-PriorityQueueHeap-Solution | class Solution:
def isPossible(self, target: List[int]) -> bool:
heap = list(map(lambda x: -x,target))
heapq.heapify(heap)
summ = sum(heap)
while True:
item = heapq.heappop(heap)
if item == -1: return True
summ-=item
if item >= summ or summ == 0: return False
item = item % summ if item % summ else summ
if item > -1:
return False
heapq.heappush(heap,item)
summ+=item | construct-target-array-with-multiple-sums | Python PriorityQueue/Heap Solution | vtalantsev | 0 | 48 | construct target array with multiple sums | 1,354 | 0.363 | Hard | 20,396 |
https://leetcode.com/problems/construct-target-array-with-multiple-sums/discuss/1199336/Python3-Greedy-method-%2B-heapq-sol-for-reference. | class Solution:
def isPossible(self, target) -> bool:
# Assuming we need to get to all 1s we would need to have the final array to contain all 1s -> sum([]) -> len(target)
expected_sum = len(target)
# The sum of the array in its current state.
current_sum = sum(target)
# Min heap to help with the largest number to pull next out.
mtarget = [0-x for x in target]
heapq.heapify(mtarget)
# loop around assumign that we are reducing current sum to reach expected sum in greedy way.
while current_sum > expected_sum:
# Get the next largest number.
max_value = heapq.heappop(mtarget)
# Min heap with negative value, hence flip the value to +ve again.
max_value = 0-max_value
# Assuming that max value was inserted in the place it was the orignal number in that place would need to be first calulated.
replacement = max_value - ( current_sum - max_value )
# There are some corner cases and also a test case below that can be dealt with in super fast way if we knew arr_sum_minus_max
# if exactly 1 then only one reason, the case below.
# I added these two cases below after tests failure, it takes mastery to think thse corner cases upfront.:)
# [1,1000000000]
# If eventually if the diff becomes zero, its a case we would not eventually reach '1's
arr_sum_minus_max = ( current_sum - max_value )
if arr_sum_minus_max == 1:
replacement = 1
elif arr_sum_minus_max == 0:
return False
# If we realise that the replacement we chose in greedy way can crawl down all the way in small increments, we can optimize that.
# An example for the two elements case, below.
# ......
# 902 [-900, -2]
# 900 [-898, -2]
# 898 [-896, -2]
# 896 [-894, -2]
# ......
if replacement > (current_sum - max_value + 1):
replacement = replacement - ((replacement // arr_sum_minus_max) * arr_sum_minus_max)
## If the replacement goes below, 1 declare failure.
if replacement < 1:
return False
# Adjust current sum since we chose / acted on the item to be inserted back in place of the pop at the begining of the loop.
current_sum = current_sum - max_value + replacement
# put the replacement back in the min heap.
heapq.heappush(mtarget, 0-replacement)
# Check if we reached the goal.
if current_sum == expected_sum:
return True
else:
return False
Runtime: 248 ms, faster than 84.67% of Python3 online submissions for Construct Target Array With Multiple Sums.
Memory Usage: 20 MB, less than 81.02% of Python3 online submissions for Construct Target Array With Multiple Sums. | construct-target-array-with-multiple-sums | [Python3] Greedy method + heapq sol for reference. | vadhri_venkat | 0 | 106 | construct target array with multiple sums | 1,354 | 0.363 | Hard | 20,397 |
https://leetcode.com/problems/construct-target-array-with-multiple-sums/discuss/510695/Python3-Math | class Solution:
def isPossible(self, target: List[int]) -> bool:
n = len(target)
sumk = 0
for x in sorted(target):
if x == 1:
continue
if (x-1) % (n-1) != 0:
return False
k = (x-1)//(n-1)
if not k > sumk:
return False
sumk += k
return True | construct-target-array-with-multiple-sums | Python3 Math | bottlecapper | -1 | 96 | construct target array with multiple sums | 1,354 | 0.363 | Hard | 20,398 |
https://leetcode.com/problems/sort-integers-by-the-number-of-1-bits/discuss/2697450/Python-or-1-liner-lambda-key | class Solution:
def sortByBits(self, arr: List[int]) -> List[int]:
return sorted(arr, key = lambda item: (str(bin(item))[2:].count("1"), item)) | sort-integers-by-the-number-of-1-bits | Python | 1-liner lambda key | LordVader1 | 4 | 791 | sort integers by the number of 1 bits | 1,356 | 0.72 | Easy | 20,399 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.