You are given an m x n
matrix M
initialized with all 0
's and an array of operations ops
, where ops[i] = [ai, bi]
means M[x][y]
should be incremented by one for all 0 <= x < ai
and 0 <= y < bi
.
Count and return the number of maximum integers in the matrix after performing all the operations.
Example:
Input: m = 3, n = 3, ops = [[2,2],[3,3]]
Output: 4
Explanation: The maximum integer in M is 2, and there are four of it in M. So return 4.
Approach:
C++
#include <bits/stdc++.h>using namespace std;int maxCount(int m, int n, vector<vector<int>> &ops){if (ops.empty()){return m * n;}int min_x = m;int min_y = n;for (int i = 0; i < ops.size(); i++){min_x = min(min_x, ops[i][0]);min_y = min(min_y, ops[i][1]);}return min_x * min_y;}int main(){int m = 3, n = 3;vector<vector<int>> ops = {{2, 2}, {3, 3}};cout << maxCount(m, n, ops);return 0;}
No comments:
Post a Comment