Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 데이콘
- 더현대서울 맛집
- 우분투
- Baekjoon
- 편스토랑
- Real or Not? NLP with Disaster Tweets
- Docker
- leetcode
- 맥북
- 코로나19
- ubuntu
- programmers
- PYTHON
- 프로그래머스
- 자연어처리
- github
- 프로그래머스 파이썬
- AI 경진대회
- 캐치카페
- ChatGPT
- dacon
- 편스토랑 우승상품
- 백준
- hackerrank
- 금융문자분석경진대회
- Git
- 파이썬
- SW Expert Academy
- gs25
- Kaggle
Archives
- Today
- Total
솜씨좋은장씨
[leetCode] 1351. Count Negative Numbers in a Sorted Matrix (Python) 본문
Programming/코딩 1일 1문제
[leetCode] 1351. Count Negative Numbers in a Sorted Matrix (Python)
솜씨좋은장씨 2020. 11. 3. 00:11728x90
반응형
Given a m * n matrix grid which is sorted in non-increasing order both row-wise and column-wise.
Return the number of negative numbers in grid.
Example 1:
Input: grid = [[4,3,2,-1],[3,2,1,-1],[1,1,-1,-2],[-1,-1,-2,-3]]
Output: 8
Explanation: There are 8 negatives number in the matrix.
Example 2:
Input: grid = [[3,2],[1,0]]
Output: 0
Example 3:
Input: grid = [[1,-1],[-1,-1]]
Output: 3
Example 4:
Input: grid = [[-1]]
Output: 1
Constraints:
- m == grid.length
- n == grid[i].length
- 1 <= m, n <= 100
- -100 <= grid[i][j] <= 100
Solution
class Solution:
def countNegatives(self, grid: List[List[int]]) -> int:
answer = 0
for i in range(len(grid)):
if grid[i][-1] < 0:
for j in range(len(grid[i])-1, -1, -1):
if grid[i][j] >= 0:
break
else:
answer = answer + 1
else:
continue
return answer
'Programming > 코딩 1일 1문제' 카테고리의 다른 글
[leetCode] 1287. Element Appearing More Than 25% In Sorted Array (Python) (0) | 2020.11.05 |
---|---|
[leetCode] 1380. Lucky Numbers in a Matrix (Python) (0) | 2020.11.04 |
[leetCode] 1137. N-th Tribonacci Number (Python) (0) | 2020.11.02 |
[leetCode] 509. Fibonacci Number (Python) (0) | 2020.11.01 |
[leetCode] 482. License Key Formatting (Python) (0) | 2020.10.31 |
Comments