Given a m x n
matrix grid
which is sorted in non-increasing order both row-wise and column-wise, return the number of negative numbers in grid
.
Example 1:
Input: grid = [[4,3,2,-1],[3,2,1,-1],[1,1,-1,-2],[-1,-1,-2,-3]] Output: 8 Explanation: There are 8 negatives number in the matrix.
Example 2:
Input: grid = [[3,2],[1,0]] Output: 0
Constraints:
m == grid.length
n == grid[i].length
1 <= m, n <= 100
-100 <= grid[i][j] <= 100
Follow up: Could you find an O(n + m)
solution?
program main
implicit none
integer, parameter :: m = 4, n = 4
integer :: grid(m, n)
integer :: i, j
integer :: negative_count
! Example 1
grid(:, :) = reshape([4, 3, 2, -1, 3, 2, 1, -1, 1, 1, -1, -2, -1, -1, -2, -3], shape(grid))
write(*, '(A, I0)') 'Example 1: ', count_negatives(grid)
! Example 2
grid(:, :) = reshape([3, 2, 1, 0, 1, 0], shape(grid))
write(*, '(A, I0)') 'Example 2: ', count_negatives(grid)
contains
function count_negatives(grid) result(negative_count)
implicit none
integer, intent(in) :: grid(:, :)
integer :: i, j
integer :: negative_count
negative_count = 0
do i = 1, size(grid, 1)
do j = 1, size(grid, 2)
if (grid(i, j) < 0) then
negative_count = negative_count + 1
end if
end do
end do
end function count_negatives
end program main
temp.f95:14:25: 14 | grid(:, :) = reshape([3, 2, 1, 0, 1, 0], shape(grid)) | 1 Error: Without padding, there are not enough elements in the intrinsic RESHAPE source at (1) to match the shape
module NegativeNumbersInSortedMatrix
implicit none
private
public :: negativeNumbersInSortedMatrix
contains
function negativeNumbersInSortedMatrix(grid) result(count)
integer, intent(in) :: grid(:, :)
integer :: count
integer :: i, j
count = 0
do i = 1, size(grid, 1)
do j = 1, size(grid, 2)
if (grid(i, j) < 0) then
count = count + 1
end if
end do
end do
end function negativeNumbersInSortedMatrix
end module NegativeNumbersInSortedMatrix
program test_negativeNumbersInSortedMatrix
use NegativeNumbersInSortedMatrix
implicit none
integer, parameter :: m = 4
integer, parameter :: n = 4
integer, parameter :: grid(m, n) = reshape([4, 3, 2, -1, 3, 2, 1, -1, 1, 1, -1, -2, -1, -1, -2, -3], [m, n])
integer :: count
count = negativeNumbersInSortedMatrix(grid)
write (*, *) "Negative numbers in grid:", count
contains
subroutine check(expected, actual)
integer, intent(in) :: expected, actual
if (expected /= actual) then
write (*, *) "Error: expected ", expected, " but got ", actual
stop 1
end if
end subroutine check
end program test_negativeNumbersInSortedMatrix
temp.f95:4:43: 4 | public :: negativeNumbersInSortedMatrix | 1 Error: PUBLIC attribute applied to MODULE negativenumbersinsortedmatrix at (1) temp.f95:8:42: 8 | function negativeNumbersInSortedMatrix(grid) result(count) | 1 Error: MODULE attribute of ‘negativenumbersinsortedmatrix’ conflicts with PROCEDURE attribute at (1) temp.f95:9:41: 9 | integer, intent(in) :: grid(:, :) | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:10:24: 10 | integer :: count | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:12:23: 12 | integer :: i, j | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:14:17: 14 | count = 0 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:15:31: 15 | do i = 1, size(grid, 1) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:16:35: 16 | do j = 1, size(grid, 2) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:17:40: 17 | if (grid(i, j) < 0) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:18:37: 18 | count = count + 1 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:19:19: 19 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:20:15: 20 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:21:11: 21 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:22:7: 22 | end function negativeNumbersInSortedMatrix | 1 Error: Expecting END MODULE statement at (1) temp.f95:26:9: 26 | use NegativeNumbersInSortedMatrix | 1 Fatal Error: Cannot open module file ‘negativenumbersinsortedmatrix.mod’ for reading at (1): No such file or directory compilation terminated.
def balanced_string(s):
n = len(s)
freq_map = {}
for c in s:
freq_map[c] = freq_map.get(c, 0) + 1
i = 0
result = n
for j in range(n):
freq_map[s[j]] -= 1
while i < n and all(freq_map.get(c, 0) <= n // 4 for c in "QWER"):
result = min(result, j - i + 1)
freq_map[s[i]] += 1
i += 1
return result
#include <string>
#include <unordered_map>
#include <algorithm>
using namespace std;
int balancedString(string s) {
int n = s.size();
unordered_map<char, int> freq_map;
for (char c : s)
freq_map[c]++;
int i = 0, result = n;
for (int j = 0; j < n; j++) {
freq_map[s[j]]--;
while (i < n && freq_map['Q'] <= n / 4 && freq_map['W'] <= n / 4 && freq_map['E'] <= n / 4 && freq_map['R'] <= n / 4) {
result = min(result, j - i + 1);
freq_map[s[i]]++;
i++;
}
}
return result;
}