给定一个matrix(mat[][]) ,它按升序按行和列排序。给出两个整数L 和 R ,我们的任务是计算矩阵 [L, R] 范围内元素的数量。
例子:
Input: L = 3, R = 11, matrix =
{{1, 6, 9}
{2, 7, 11}
{3, 8, 12}}
Output: 6
Explanation:
The elements which are in this range [3, 11] are 3, 6, 7, 8, 9, 11.
Input: L = 20, R = 26, matrix =
{{1, 6, 19}
{2, 7, 31}
{3, 8, 42}}
Output: 0
Explanation:
No element is in this range.
朴素的方法:要解决上述问题,朴素的方法是对矩阵进行逐行遍历。
对于每一行,我们检查该行的每个元素,如果它在给定的范围内,那么我们增加计数。最后,我们返回计数。
时间复杂度: O(M * N),其中 M 是行数,N 是列数。
高效方法:优化上述方法:
- 首先我们计算小于 L 的元素。让我们将其视为count1 。我们从第一列的最后一个元素开始遍历,这包括以下两个步骤:
- 如果当前迭代元素小于 L,我们将 count1 增加对应的行 + 1,因为当前元素(包括当前元素)上方的该列中的元素必须小于 L。我们增加列索引。
- 如果当前迭代元素大于或等于 L,我们递减行索引。我们这样做直到行或列索引变得无效。
- 接下来我们计算小于或等于 R 的元素。让我们将其视为count2 。我们从第一列的最后一个元素开始遍历,这包括两个步骤:
- 如果当前迭代元素小于或等于 R,我们将 count2 增加对应的行 + 1,因为当前元素(包括当前元素)上方的该列中的元素必须小于或等于 R。我们增加列索引。
- 如果当前迭代元素大于 R,我们减少行索引。我们这样做直到行或列索引变得无效。
- 最后,我们返回 column2 和 column1 的差值,这将是所需的答案。
下面是上述方法的实现:
C++
// C++ implementation to count
// all elements in a Matrix
// which lies in the given range
#include
using namespace std;
#define M 3
#define N 3
// Counting elements in range [L, R]
int countElements(int mat[M][N],
int L, int R)
{
// Count elements less than L
int count1 = 0;
int row = M - 1, col = 0;
while (row >= 0 && col < N) {
// Check if the current iterating
// element is less than L
if (mat[row][col] < L) {
count1 += (row + 1);
col++;
}
else {
row--;
}
}
// counting elements less
// than or equal to R
int count2 = 0;
row = M - 1, col = 0;
while (row >= 0 && col < N) {
// Check if the current iterating
// element is less than R
if (mat[row][col] <= R) {
count2 += (row + 1);
col++;
}
else {
row--;
}
}
// return the final result
return count2 - count1;
}
// Driver code
int main()
{
int mat[M][N] = { { 1, 6, 19 },
{ 2, 7, 31 },
{ 3, 8, 42 } };
int L = 10, R = 26;
cout << countElements(mat, L, R);
return 0;
}
Java
// Java implementation to count
// all elements in a Matrix
// which lies in the given range
import java.util.*;
import java.lang.*;
class GFG{
static int N = 3;
static int M = 3;
// Counting elements in range [L, R]
static int countElements(int[][] mat,
int L, int R)
{
// Count elements less than L
int count1 = 0;
int row = M - 1, col = 0;
while (row >= 0 && col < N)
{
// Check if the current iterating
// element is less than L
if (mat[row][col] < L)
{
count1 += (row + 1);
col++;
}
else
{
row--;
}
}
// counting elements less
// than or equal to R
int count2 = 0;
row = M - 1;
col = 0;
while (row >= 0 && col < N)
{
// Check if the current iterating
// element is less than R
if (mat[row][col] <= R)
{
count2 += (row + 1);
col++;
}
else
{
row--;
}
}
// return the final result
return count2 - count1;
}
// Driver code
public static void main(String[] args)
{
int[][] mat = { { 1, 6, 19 },
{ 2, 7, 31 },
{ 3, 8, 42 } };
int L = 10, R = 26;
System.out.println(countElements(mat, L, R));
}
}
// This code is contributed by offbeat
Python3
# Python3 implementation to count
# all elements in a matrix which
# lies in the given range
M = 3
N = 3
# Counting elements in range [L, R]
def countElements(mat, L, R):
# Count elements less than L
count1 = 0
row = M - 1
col = 0
while row >= 0 and col < N:
# Check if the current iterating
# element is less than L
if mat[row][col] < L:
count1 += (row + 1)
col += 1
else:
row -= 1
# Counting elements less
# than or equal to R
count2 = 0
row = M - 1
col = 0
while row >= 0 and col < N:
# Check if the current iterating
# element is less than R
if mat[row][col] <= R:
count2 += (row + 1)
col += 1
else:
row -= 1
# Return the final result
return count2 - count1
# Driver code
mat = [ [ 1, 6, 19 ],
[ 2, 7, 31 ],
[ 3, 8, 42 ] ]
L = 10
R = 26
print(countElements(mat, L, R))
# This code is contributed by divyamohan123
C#
// C# implementation to count
// all elements in a Matrix
// which lies in the given range
using System;
class GFG{
static int N = 3;
static int M = 3;
// Counting elements in range [L, R]
static int countElements(int[,] mat,
int L, int R)
{
// Count elements less than L
int count1 = 0;
int row = M - 1, col = 0;
while (row >= 0 && col < N)
{
// Check if the current iterating
// element is less than L
if (mat[row, col] < L)
{
count1 += (row + 1);
col++;
}
else
{
row--;
}
}
// counting elements less
// than or equal to R
int count2 = 0;
row = M - 1;
col = 0;
while (row >= 0 && col < N)
{
// Check if the current iterating
// element is less than R
if (mat[row, col] <= R)
{
count2 += (row + 1);
col++;
}
else
{
row--;
}
}
// return the final result
return count2 - count1;
}
// Driver code
public static void Main()
{
int[,] mat = { { 1, 6, 19 },
{ 2, 7, 31 },
{ 3, 8, 42 } };
int L = 10, R = 26;
Console.Write(countElements(mat, L, R));
}
}
// This code is contributed by Code_Mech
Javascript
输出:
1
时间复杂度: O(M + N)。 M 是行数,N 是列数。
辅助空间复杂度: O(1)。
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live