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:
r... | 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:
re... | 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), C... | 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 ... | 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 a... | 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)
... | 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]:
... | 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 ... | 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)
... | 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)
r... | 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)
... | 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(s... | 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)... | 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:
... | 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]]) ->... | 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 l... | 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 ... | 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... | 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
... | 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:
co... | 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 countNegativ... | 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
... | 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
... | 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
r... | 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])):
#... | 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
... | 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
... | 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
righ... | 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 le... | 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:
mi... | 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... | 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... | 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... | 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:
... | 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
... | 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
... | 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 l... | 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... | 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... | 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 n... | 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 &a... | 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)))
... | 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)
... | 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._... | 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 -... | 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 ... | 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 ... | 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)
... | 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:
... | 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.