约翰是一位地质学家,他需要对岩石样本进行计数,以便将其送到化学实验室。他有问题。实验室只接受以 ppm(百万分之几)为单位的一定范围内的岩石样品。约翰需要你的帮助。你的任务是开发一个程序来获得实验室接受的每个范围内的岩石数量。
问题陈述:给定一个表示岩石样本大小的数组samples[]和一个 2D 数组range[] ,任务是计算在 range [i][0] 到 range[i][1]范围内的岩石样本,对于每一个可能的1 <= i <= N 。
例子:
Input: samples[] = {345, 604, 321, 433, 704, 470, 808, 718, 517, 811}, ranges[] = {{300, 380}, {400, 700}}
Output: 2 4
Explanation:
Range [300, 380]: Samples {345, 321} lie in the range. Therefore, the count is 2.
Range [400, 700]: Samples {433, 604, 517, 470} lie in the range. Therefore, the count is 4.
Input: samples[] = {400, 567, 890, 765, 987}, ranges[] = {{300, 380}, {800, 1000}
Output: 0 2
方法:想法是为每个范围[i]迭代样本[]并计算位于指定范围内的样本数。请按照以下步骤解决问题:
- 遍历数组 range[]。
- 对于每一行range[i] ,遍历数组samples[]并计算位于[ranges[i][0],ranges[i][1]]范围内的岩石样本数量。
下面是上述方法的实现:
C++
// C++ program of the
// above approach
#include
using namespace std;
void findRockSample(vector>ranges,
int n, int r, vectorarr)
{
vectora;
// Iterate over the ranges
for(int i = 0; i < r; i++)
{
int c = 0;
int l = ranges[i][0];
int h = ranges[i][1];
for(int j = 0; j < arr.size(); j++)
{
if (l <= arr[j] && arr[j] <= h)
c += 1;
}
a.push_back(c);
}
for(auto i:a)
cout << i << " ";
}
// Driver Code
int main()
{
int n = 5;
int r = 2;
vectorarr = { 400, 567, 890, 765, 987 };
vector>ranges = { { 300, 380 },
{ 800, 1000 } };
// Function call
findRockSample(ranges, n, r, arr);
}
// This code is contributed by Stream_Cipher
Java
// Java program of the
// above approach
import java.util.*;
import java.io.*;
class GFG{
// Function to find the rock
// samples in the ranges
static ArrayListfindRockSample(int ranges[][],
int n, int r,
int arr[])
{
ArrayList a = new ArrayList<>();
// Iterate over the ranges
for(int i = 0; i < r; i++)
{
int c = 0;
int l = ranges[i][0];
int h = ranges[i][1];
for(int j = 0; j < arr.length; j++)
{
if (l <= arr[j] && arr[j] <= h)
c += 1;
}
a.add(c);
}
return a;
}
// Driver Code
public static void main(String args[])
{
int n = 5;
int r = 2;
int arr[] = { 400, 567, 890, 765, 987 };
int ranges[][] = { { 300, 380 }, { 800, 1000 } };
ArrayList answer = new ArrayList<>();
// Function call
answer = findRockSample(ranges, n, r, arr);
for(int i = 0; i < answer.size(); i++)
System.out.print(answer.get(i) + " ");
System.out.println();
}
}
// This code is contributed by bikram2001jha
Python3
# Python3 program of the
# above approach
# Function to find the rock
# samples in the ranges
def findRockSample(ranges,
n, r, arr):
a = []
# Iterate over the ranges
for i in range(r):
c = 0
l, h = ranges[i][0], ranges[i][1]
for val in arr:
if l <= val <= h:
c += 1
a.append(c)
return a
# Driver Code
if __name__ == "__main__":
n = 5
r = 2
arr = [400, 567, 890, 765, 987]
ranges = [[300, 380], [800, 1000]]
# Function Call
print(*findRockSample(ranges, n, r, arr))
C#
// C# program of the
// above approach
using System.Collections.Generic;
using System;
class GFG{
// Function to find the rock
// samples in the ranges
static void findRockSample(int [,]ranges,
int n, int r,
int [] arr)
{
List a = new List();
// Iterate over the ranges
for(int i = 0; i < r; i++)
{
int c = 0;
int l = ranges[i, 0];
int h = ranges[i, 1];
for(int j = 0; j < arr.Length; j++)
{
if (l <= arr[j] && arr[j] <= h)
c += 1;
}
a.Add(c);
}
foreach (var i in a)
{
Console.Write(i + " ");
}
}
// Driver Code
public static void Main()
{
int n = 5;
int r = 2;
int []arr = { 400, 567, 890, 765, 987 };
int [,]ranges = { { 300, 380 },
{ 800, 1000 } };
// Function call
findRockSample(ranges, n, r, arr);
}
}
// This code is contributed by Stream_Cipher
0 2
时间复杂度: O(N 2 )
辅助空间: O(1)