[LeetCode] Range Addition II

아르당·1일 전

LeetCode

목록 보기
131/134
post-thumbnail

문제를 이해하고 있다면 바로 풀이를 보면 됨
전체 코드로 바로 넘어가도 됨
마음대로 번역해서 오역이 있을 수 있음

Problem

모든 요소가 0으로 초기화된 m x n 행렬 M과 연산 배열 ops가 주어진다. ops[i] = [a[i], b[i]]는 0 <= x < a[i], 0 <= y < b[i]인 모든 경우에 대해 M[x][y]를 1씩 증가시켜야 한다.

모든 연산을 수행한 후 행렬에서 최댓값을 갖는 정수의 개수를 세어 반환해라.

Example

#1

Input: m = 3, n = 3, ops = [[2, 3], [3, 3]]
Output: 4
Explanation: M에서 가장 큰 정수는 2이고, M에 4개가 있다. 그래서 4를 반환한다.

#2
Input: m = 3, n = 3, ops = [[2, 2], [3, 3], [3, 3], [3, 3], [2, 2], [3, 3], [3, 3], [3, 3], [2, 2], [3, 3], [3, 3], [3, 3]]
Output: 4

#3
Input: m = 3, n = 3, ops = []
Output: 9

Constraints

  • 1 <= m, n <= 4 * 10^4
  • 0 <= ops.length <= 10^4
  • ops[i].length == 2
  • 1 <= a[i] <= m
  • 1 <= b[i] <= n

Solved

class Solution {
    public int maxCount(int m, int n, int[][] ops) {
        if(ops == null || ops.length == 0) return m * n;

        int row = Integer.MAX_VALUE;
        int col = Integer.MAX_VALUE;

        for(int[] op : ops){
            row = Math.min(row, op[0]);
            col = Math.min(col, op[1]);
        }

        return row * col;
    }
}
profile
내 마음대로 코드 작성하는 세상

0개의 댓글