-
Notifications
You must be signed in to change notification settings - Fork 0
/
Count Negative
53 lines (37 loc) · 1.07 KB
/
Count Negative
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
Method 1:
class Solution:
def countNegatives(self, grid: List[List[int]]) -> int:
import numpy as np
arr = (np.array(grid)).flatten()
c = arr[arr < 0]
return len(c)
Method 2:
class Solution:
def countNegatives(self, grid: List[List[int]]) -> int:
import numpy as np
arr = np.array(grid)
ar = np.where( arr < 0)
print(ar)
return len(ar[0])
Method 3:
class Solution:
def countNegatives(self, grid: List[List[int]]) -> int:
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] < 0:
count += 1
return count
Method 4:
class Solution:
def countNegatives(self, grid: List[List[int]]) -> int:
n = len(grid)
left, right = 0, len(grid[0])-1
total = 0
while right >=0 and left < n:
if grid[left][right] < 0:
total += n - left
right -= 1
else:
left += 1
return total