Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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
- 30mf
- DIY
- 맛집
- 프라모델
- 가챠
- 밥무하마드
- 취미
- 발더스모드
- 지쿠악스
- 누룽지소금빵
- 메탈퍼즐
- 눈알빠지겠네
- 건담
- 포켓몬
- 건담헤드
- 바질토마토뭐시기
- 제프딕슨
- 게임
- LeetCode
- 노노그램
- 발더스게이트
- 롱라이플
- 우리시대의역설
- 코딩테스트
- 블라인드박스
- 지리데칼
- 코테
- javascript
- 유루건
- 미앤아이
Archives
- Today
- Total
.Zzumbong
[leetCode/JS] 1351. Count Negative Numbers in a Sorted Matrix 본문
coding test/leetCode
[leetCode/JS] 1351. Count Negative Numbers in a Sorted Matrix
쭘봉 2025. 12. 29. 09:07난이도 [ 😊 ] Easy
문제 설명
Given a m x n matrix grid which is sorted in non-increasing order both row-wise and column-wise,
return the number of negative numbers in grid.
여기서 non-increasing order는 내림차순(값이 같거나 작아짐)을 의미함.
그리고 이게 row-wise(가로)와 column-wise 모두에 적용된다는 것이 핵심.
입출력 예
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
Constraints
m == grid.lengthn == grid[i].length1 <= m, n <= 100-100 <= grid[i][j] <= 100
Follow up: Could you find an O(n + m) solution?
내 솔루션
/**
* @param {number[][]} grid
* @return {number}
*/
var countNegatives = function(grid) {
let row = grid.length - 1; // --
let col = 0; // ++
const n = grid[0].length;
let count = 0;
while(row >= 0 && col < n){
if(grid[row][col] < 0) {
count += (n - col)
row--;
} else {
col++;
}
}
return count;
};
// [[4,3,2,-1],
// [3,2,1,-1],
// [1,1,-1,-2],
// [-1,-1,-2,-3]]
감상평
- 2차원 m * n 에서 n만 내림 차순인줄 알고 바이너리 서치를 통해 해결하려다가
- O(n+m)이 가능하다 Follow up 힌트에서 가로 세로 모두 정렬 되어 있구나! 를 알아 차림.
- 영문을 너무 띄엄 띄엄 읽는게 문제야

'coding test > leetCode' 카테고리의 다른 글
| [leetCode/JS] 3074. Apple Redistribution into Boxes (0) | 2025.12.29 |
|---|---|
| [leetCode/JS] 756. Pyramid Transition Matrix (1) | 2025.12.29 |
| [leetCode/JS] 3477. Fruits Into Baskets II (1) | 2025.08.05 |
| [leetCode/JS]1408. String Matching in an Array (2) | 2025.01.07 |
| [leetCode/JS] 343. Integer Break (0) | 2023.10.06 |
Comments
