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 1:
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.
Example 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
Example 3:
Input: m = 3, n = 3, ops = [] Output: 9
Constraints:
1 <= m, n <= 4 * 104
0 <= ops.length <= 104
ops[i].length == 2
1 <= ai <= m
1 <= bi <= n
program main
implicit none
integer :: m, n, i, j, max_val, count
integer, allocatable :: M(:, :)
integer, allocatable :: ops(:, :)
read(*,*) m, n
allocate(M(m,n))
do i = 1, m
do j = 1, n
M(i,j) = 0
end do
end do
read(*,*) ops
do i = 1, size(ops, 1)
M(ops(i,1), ops(i,2)) = M(ops(i,1), ops(i,2)) + 1
end do
max_val = -huge(max_val)
count = 0
do i = 1, m
do j = 1, n
if (M(i,j) > max_val) then
max_val = M(i,j)
count = count + 1
end if
end do
end do
print '(I0)', count
end program main
temp.f95:5:29: 5 | integer, allocatable :: M(:, :) | 1 Error: Symbol ‘m’ at (1) already has basic type of INTEGER temp.f95:9:13: 9 | allocate(M(m,n)) | 1 Error: Allocate-object at (1) is neither a data pointer nor an allocatable variable temp.f95:23:18: 23 | if (M(i,j) > max_val) then | 1 Error: Syntax error in IF-expression at (1) temp.f95:26:15: 26 | end if | 1 Error: Expecting END DO statement at (1)
module MaxIntegers
implicit none
private
public :: maxIntegers
contains
function maxIntegers(m, n, ops) result(max)
integer, intent(in) :: m
integer, intent(in) :: n
integer, intent(in) :: ops(:, :)
integer :: max
integer :: M(m, n)
integer :: i, j, ai, bi
! Initialize the matrix with all 0's
M = 0
! Perform the operations
do i = 1, size(ops, 1)
ai = ops(i, 1)
bi = ops(i, 2)
do j = 1, bi
M(1:ai, j) = M(1:ai, j) + 1
end do
end do
! Count the maximum integers
max = 0
do i = 1, m
do j = 1, n
if (M(i, j) == 0) then
exit
end if
max = max + 1
end do
end do
end function maxIntegers
end module MaxIntegers
! Test the function with some examples
program test_maxIntegers
use MaxIntegers
implicit none
integer :: m, n, ops(2, 2)
integer :: max
! Example 1
m = 3
n = 3
ops = reshape([2, 2, 3, 3], shape(ops))
max = maxIntegers(m, n, ops)
write (*, '(A, I0)') 'Example 1: ', max
! Example 2
m = 3
n = 3
ops = reshape([2, 2, 3, 3, 3, 3, 3, 3, 2, 3, 3, 3, 3, 3, 3, 3], shape(ops))
max = maxIntegers(m, n, ops)
write (*, '(A, I0)') 'Example 2: ', max
! Example 3
m = 3
n = 3
ops = reshape([], shape(ops))
max = maxIntegers(m, n, ops)
write (*, '(A, I0)') 'Example 3: ', max
end program test_maxIntegers
temp.f95:4:25: 4 | public :: maxIntegers | 1 Error: PUBLIC attribute applied to MODULE maxintegers at (1) temp.f95:8:24: 8 | function maxIntegers(m, n, ops) result(max) | 1 Error: MODULE attribute of ‘maxintegers’ conflicts with PROCEDURE attribute at (1) temp.f95:9:32: 9 | integer, intent(in) :: m | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:10:32: 10 | integer, intent(in) :: n | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:11:40: 11 | integer, intent(in) :: ops(:, :) | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:12:22: 12 | integer :: max | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:13:26: 13 | integer :: M(m, n) | 1 Error: Explicit array shape at (1) must be constant of INTEGER type and not UNKNOWN type temp.f95:14:31: 14 | integer :: i, j, ai, bi | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:17:13: 17 | M = 0 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:20:30: 20 | do i = 1, size(ops, 1) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:21:26: 21 | ai = ops(i, 1) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:22:26: 22 | bi = ops(i, 2) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:23:24: 23 | do j = 1, bi | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:25:15: 25 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:26:11: 26 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:29:15: 29 | max = 0 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:30:19: 30 | do i = 1, m | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:31:23: 31 | do j = 1, n | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:32:38: 32 | if (M(i, j) == 0) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:33:24: 33 | exit | 1 Error: EXIT statement at (1) is not within a construct temp.f95:34:19: 34 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:35:29: 35 | max = max + 1 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:36:15: 36 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:37:11: 37 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:38:7: 38 | end function maxIntegers | 1 Error: Expecting END MODULE statement at (1) temp.f95:43:9: 43 | use MaxIntegers | 1 Fatal Error: Cannot open module file ‘maxintegers.mod’ for reading at (1): No such file or directory compilation terminated.
def maxCount(m: int, n: int, ops: List[List[int]]) -> int:
for op in ops:
m = min(m, op[0])
n = min(n, op[1])
return m * n
The algorithm consists of iterating through each operation in the ops array and updating the values of m and n to be the minimum of their current value and the corresponding values in the operation. This is because the maximum integers will always be in the top-left corner of the matrix (0 <= x < ai and 0 <= y < bi). Once all operations have been considered, we multiply the final values of m and n to get the count of maximum integers in the matrix.
#include <vector>
using namespace std;
int maxCount(int m, int n, vector<vector<int>>& ops) {
for (vector<int>& op : ops) {
m = min(m, op[0]);
n = min(n, op[1]);
}
return m * n;
}
The algorithm consists of iterating through each operation in the ops array and updating the values of m and n to be the minimum of their current value and the corresponding values in the operation. This is because the maximum integers will always be in the top-left corner of the matrix (0 <= x < ai and 0 <= y < bi). Once all operations have been considered, we multiply the final values of m and n to get the count of maximum integers in the matrix.