Range Addition II

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 mint nvector<vector<int>> &ops)
{
    if (ops.empty())
    {
        return m * n;
    }
    int min_x = m;
    int min_y = n;
    for (int i = 0i < ops.size(); i++)
    {
        min_x = min(min_xops[i][0]);
        min_y = min(min_yops[i][1]);
    }
    return min_x * min_y;
}

int main()
{
    int m = 3n = 3;
    vector<vector<int>> ops = {{22}, {33}};

    cout << maxCount(mnops);

    return 0;
}


No comments:

Post a Comment