JAVA_LeetCode 1351_Count Negative Numbers in a Sorted Matrix


JAVA_LeetCode 1351_Count Negative Numbers in a Sorted Matrix

JAVA_LeetCode 1351_Count Negative Numbers in a Sorted Matrix 풀이 class Solution { public int countNegatives(int[][] grid) { // 행, 열 방향 모두 증가되지 않는 순서로 정렬된다는 점을 이용 int i = 0, j = grid[0].length - 1, cnt = 0; // i(행), j(열) >> 0번째 행의 맨 끝 요소값으로부터 음수를 체크 // grid의 길이 - 행을 개수로 추가한다. while(i < grid.length && j >= 0){ if (grid[i][j] < 0) { cnt += (grid.length - i); j--; } else if (grid[i][j] >= 0) { i++; } } return cnt; } } 행, 열 방향이 모두 증가되지 않는다는 점과 행, 열의 내림차순을 이용한다. 음수를 찾았을 경우 열을 감소시키고, 양수를 찾았을 경우 행값을 더해준다...


#JAVA #JAVA_CountNegativeNumbersinaSortedMatrix #JAVA_LeetCode1351 #JAVA_LeetCode1351_CountNegativeNumbersinaSortedMatrix #LeetCode1351_CountNegativeNumbersinaSortedMatrix

원문링크 : JAVA_LeetCode 1351_Count Negative Numbers in a Sorted Matrix