📌  相关文章
📜  至少K次出现在给定范围内的数字计数

📅  最后修改于: 2021-05-04 23:02:31             🧑  作者: Mango

给定N个范围和数字K,任务是查找在给定范围内至少出现K次的数字总数。

例子

建议:在继续解决方案之前,请先在{IDE}上尝试使用您的方法。

天真的方法:天真的方法是遍历每个范围并增加每个元素的计数,最后检查每个元素的计数是否足以满足所需的值。

下面是上述方法的实现:

C++
// C++ brute-force program to find count of
// numbers appearing in the given
// ranges at-least K times
#include 
using namespace std;
  
// Function to find the no of occurrence
int countNumbers(int n, int k, int rangeLvalue[],
                              int rangeRvalue[])
{
    int count = 0;
  
    // Map to store frequency of elements
    // in the range
    map freq;
  
    for (int i = 0; i < n; i++) {
  
        // increment the value of the elements
        // in all of the ranges
        for (int j = rangeLvalue[i]; j <= rangeRvalue[i]; j++)
        {
            if (freq.find(j) == freq.end())
                freq.insert(make_pair(j, 1));
            else
                freq[j]++;
        }
    }
  
    // Traverse the map to check the frequency
    // of numbers greater than equals to k
    for (auto itr = freq.begin(); itr != freq.end(); itr++)
    {
  
        // check if a number appears atleast k times
        if ((*itr).second >= k) {
  
            // increase the counter
            // if condition satisfies
            count++;
        }
    }
  
    // return the result
    return count;
}
  
// Driver Code
int main()
{
    int n = 3, k = 2;
    int rangeLvalue[] = { 91, 92, 97 };
    int rangeRvalue[] = { 94, 97, 99 };
    cout << countNumbers(n, k, rangeLvalue, rangeRvalue);
    return 0;
}


Java
// Java brute-force program to find count of 
// numbers appearing in the given 
// ranges at-least K times
import java.io.*;
import java.util.*;
  
class GFG 
{
  
    // Function to find the no of occurrence
    static int countNumbers(int n, int k, int[] rangeLvalue, 
                                            int[] rangeRvalue) 
    {
        int count = 0;
  
        // Map to store frequency of elements
        // in the range
        HashMap freq = new HashMap<>();
  
        // increment the value of the elements
        // in all of the ranges
        for (int i = 0; i < n; i++)
        {
            for (int j = rangeLvalue[i]; j <= rangeRvalue[i]; j++)
            {
                if (!freq.containsKey(j))
                    freq.put(j, 1);
                else
                    freq.put(j, freq.get(j) + 1);
            }
        }
  
        // Traverse the map to check the frequency
        // of numbers greater than equals to k
        for (HashMap.Entry entry : freq.entrySet()) 
        {
  
            // check if a number appears atleast k times
            if (entry.getValue() >= k)
  
                // increase the counter
                // if condition satisfies
                count++;
        }
  
        // return the result
        return count;
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        int n = 3, k = 2;
        int[] rangeLvalue = {91, 92, 97};
        int[] rangeRvalue = {94, 97, 99};
        System.out.println(countNumbers(n, k, rangeLvalue, rangeRvalue));
    }
}
  
// This code is contributed by
// sanjeev2552


Python3
# Python3 brute-force program to find 
# count of numbers appearing in the 
# given ranges at-least K times
  
# Function to find the no of occurrence
def countNumbers(n, k, rangeLvalue,     
                       rangeRvalue):
  
    count = 0
  
    # Map to store frequency of elements
    # in the range
    freq = dict()
  
    for i in range(n):
  
        # increment the value of the elements
        # in all of the ranges
        for j in range(rangeLvalue[i], 
                       rangeRvalue[i] + 1):
            freq[j] = freq.get(j, 0) + 1
      
    # Traverse the map to check the frequency
    # of numbers greater than equals to k
    for itr in freq:
  
        # check if a number appears 
        # atleast k times
        if (freq[itr] >= k):
  
            # increase the counter
            # if condition satisfies
            count += 1
          
    # return the result
    return count
  
# Driver Code
n, k = 3, 2
rangeLvalue = [91, 92, 97]
rangeRvalue = [94, 97, 99]
print(countNumbers(n, k, rangeLvalue,
                         rangeRvalue))
  
# This code is contributed by mohit kumar


C#
// C# brute-force program to find count of 
// numbers appearing in the given 
// ranges at-least K times
using System;
using System.Collections.Generic;
  
class GFG 
{
  
    // Function to find the no of occurrence
    static int countNumbers(int n, int k, int[] rangeLvalue, 
                                            int[] rangeRvalue) 
    {
        int count = 0;
  
        // Map to store frequency of elements
        // in the range
        Dictionary freq = new Dictionary();
  
        // increment the value of the elements
        // in all of the ranges
        for (int i = 0; i < n; i++)
        {
            for (int j = rangeLvalue[i]; j <= rangeRvalue[i]; j++)
            {
                if (!freq.ContainsKey(j))
                    freq.Add(j, 1);
                else
                    freq[j] = freq[j] + 1;
            }
        }
  
        // Traverse the map to check the frequency
        // of numbers greater than equals to k
        foreach(KeyValuePair entry in freq) 
        {
  
            // check if a number appears atleast k times
            if (entry.Value >= k)
  
                // increase the counter
                // if condition satisfies
                count++;
        }
  
        // return the result
        return count;
    }
  
    // Driver Code
    public static void Main(String[] args)
    {
        int n = 3, k = 2;
        int[] rangeLvalue = {91, 92, 97};
        int[] rangeRvalue = {94, 97, 99};
        Console.WriteLine(countNumbers(n, k, rangeLvalue, rangeRvalue));
    }
}
  
// This code is contributed by Rajput-Ji


C++
// C++ efficient program to find count of
// numbers appearing in the given
// ranges at-least K times
#include 
using namespace std;
  
// Function to find the no of occurrence
int countNumbers(int n, int k, int rangeLvalue[],
                              int rangeRvalue[])
{
    int count = 0;
  
    // maximum value of the range
    int maxn = INT_MIN;
    for (int i = 0; i < n; i++)
        if (rangeRvalue[i] > maxn)
            maxn = rangeRvalue[i];
  
    // counter array
    int preSum[maxn + 5] = { 0 };
  
    // incrementing and decrementing the
    // leftmost and next value of rightmost value
    // of each range by 1 respectively
    for (int i = 0; i < n; i++) {
        preSum[rangeLvalue[i]]++;
        preSum[rangeRvalue[i] + 1]--;
    }
  
    // presum gives the no of occurrence of
    // each element
    for (int i = 1; i <= maxn; i++) {
        preSum[i] += preSum[i - 1];
    }
  
    for (int i = 1; i <= maxn; i++) {
  
        // check if the number appears atleast k times
        if (preSum[i] >= k) {
  
            // increase the counter if
            // condition satisfies
            count++;
        }
    }
  
    // return the result
    return count;
}
  
// Driver Code
int main()
{
    int n = 3, k = 2;
  
    int rangeLvalue[] = { 91, 92, 97 };
    int rangeRvalue[] = { 94, 97, 99 };
  
    cout << countNumbers(n, k, rangeLvalue, rangeRvalue);
  
    return 0;
}


Java
// Java efficient program to find count of
// numbers appearing in the given
// ranges at-least K times
class Geeks {
  
// Function to find the no of occurrence
static int countNumbers(int n, int k, int rangeLvalue[],
                                     int rangeRvalue[])
{
    int count = 0;
  
    // maximum value of the range
    int maxn = Integer.MIN_VALUE;
    for (int i = 0; i < n; i++)
        if (rangeRvalue[i] > maxn)
            maxn = rangeRvalue[i];
  
    // counter array
    int preSum[] = new int[maxn + 5];
    for(int i = 0; i < (maxn + 5); i++)
    preSum[i] = 0;
  
    // incrementing and decrementing the
    // leftmost and next value of rightmost value
    // of each range by 1 respectively
    for (int i = 0; i < n; i++)
    {
        preSum[rangeLvalue[i]]++;
        preSum[rangeRvalue[i] + 1]--;
    }
  
    // presum gives the no of occurrence of
    // each element
    for (int i = 1; i <= maxn; i++) {
        preSum[i] += preSum[i - 1];
    }
  
    for (int i = 1; i <= maxn; i++) {
  
        // check if the number appears atleast k times
        if (preSum[i] >= k) {
  
            // increase the counter if
            // condition satisfies
            count++;
        }
    }
  
    // return the result
    return count;
}
  
// Driver Code
public static void main(String args[])
{
    int n = 3, k = 2;
  
    int rangeLvalue[] = { 91, 92, 97 };
    int rangeRvalue[] = { 94, 97, 99 };
  
    System.out.println(countNumbers(n, k, rangeLvalue,
                                        rangeRvalue));
  
}
}
  
// This code is contributed by ankita_saini


Python
# Python efficient program to find count of
# numbers appearing in the given
# ranges at-least K times
  
# Function to find the no of occurrence
def countNumbers(n, k, rangeLvalue, rangeRvalue):
    count = 0
  
    # maximum value of the range
    maxn = -float('inf')
    for i in range(n):
        if rangeRvalue[i] > maxn:
            maxn = rangeRvalue[i]
  
    # counter array
    preSum = [0]*(maxn + 5)
  
    # incrementing and decrementing the
    # leftmost and next value of rightmost value
    # of each range by 1 respectively
    for i in range(n):
        preSum[rangeLvalue[i]] += 1
        preSum[rangeRvalue[i] + 1] -= 1
  
    # presum gives the no of occurrence of
    # each element
    for i in range(1, maxn+1):
        preSum[i] += preSum[i - 1]
  
    for i in range(1, maxn+1):
  
        # check if the number appears atleast k times
        if preSum[i] >= k:
  
            # increase the counter if
            # condition satisfies
            count += 1
  
    # return the result
    return count
  
  
# Driver Code
n = 3
k = 2
  
rangeLvalue = [91, 92, 97]
rangeRvalue = [94, 97, 99]
  
print(countNumbers(n, k, rangeLvalue, rangeRvalue))
  
# This code is contributed by ankush_953


C#
// C# efficient program to 
// find count of numbers 
// appearing in the given
// ranges at-least K times
using System;
  
class GFG
{
  
// Function to find the 
// no of occurrence
static int countNumbers(int n, int k, 
                        int []rangeLvalue,
                        int []rangeRvalue)
{
    int count = 0;
  
    // maximum value of the range
    int maxn = Int32.MinValue;
    for (int i = 0; i < n; i++)
        if (rangeRvalue[i] > maxn)
            maxn = rangeRvalue[i];
  
    // counter array
    int []preSum = new int[maxn + 5];
    for(int i = 0; i < (maxn + 5); i++)
    preSum[i] = 0;
  
    // incrementing and decrementing 
    // the leftmost and next value 
    // of rightmost value of each 
    // range by 1 respectively
    for (int i = 0; i < n; i++)
    {
        preSum[rangeLvalue[i]]++;
        preSum[rangeRvalue[i] + 1]--;
    }
  
    // presum gives the no of 
    // occurrence of each element
    for (int i = 1; i <= maxn; i++) 
    {
        preSum[i] += preSum[i - 1];
    }
  
    for (int i = 1; i <= maxn; i++)
    {
  
        // check if the number 
        // appears atleast k times
        if (preSum[i] >= k) 
        {
  
            // increase the counter if
            // condition satisfies
            count++;
        }
    }
  
    // return the result
    return count;
}
  
// Driver Code
public static void Main(String []args)
{
    int n = 3, k = 2;
  
    int []rangeLvalue = { 91, 92, 97 };
    int []rangeRvalue = { 94, 97, 99 };
  
    Console.WriteLine(countNumbers(n, k, rangeLvalue,
                                        rangeRvalue));
}
}
  
// This code is contributed
// by ankita_saini


输出:
4

有效的解决方案:更好的解决方案是通过增加范围最左边的元素的值并减少计数器数组中给定范围最右边的元素的下一个元素来跟踪范围。对所有范围都执行此操作。这样做是因为它给出了在进行求和时在给定范围内一个数字发生了多少次的想法。

下面是上述方法的实现。

C++

// C++ efficient program to find count of
// numbers appearing in the given
// ranges at-least K times
#include 
using namespace std;
  
// Function to find the no of occurrence
int countNumbers(int n, int k, int rangeLvalue[],
                              int rangeRvalue[])
{
    int count = 0;
  
    // maximum value of the range
    int maxn = INT_MIN;
    for (int i = 0; i < n; i++)
        if (rangeRvalue[i] > maxn)
            maxn = rangeRvalue[i];
  
    // counter array
    int preSum[maxn + 5] = { 0 };
  
    // incrementing and decrementing the
    // leftmost and next value of rightmost value
    // of each range by 1 respectively
    for (int i = 0; i < n; i++) {
        preSum[rangeLvalue[i]]++;
        preSum[rangeRvalue[i] + 1]--;
    }
  
    // presum gives the no of occurrence of
    // each element
    for (int i = 1; i <= maxn; i++) {
        preSum[i] += preSum[i - 1];
    }
  
    for (int i = 1; i <= maxn; i++) {
  
        // check if the number appears atleast k times
        if (preSum[i] >= k) {
  
            // increase the counter if
            // condition satisfies
            count++;
        }
    }
  
    // return the result
    return count;
}
  
// Driver Code
int main()
{
    int n = 3, k = 2;
  
    int rangeLvalue[] = { 91, 92, 97 };
    int rangeRvalue[] = { 94, 97, 99 };
  
    cout << countNumbers(n, k, rangeLvalue, rangeRvalue);
  
    return 0;
}

Java

// Java efficient program to find count of
// numbers appearing in the given
// ranges at-least K times
class Geeks {
  
// Function to find the no of occurrence
static int countNumbers(int n, int k, int rangeLvalue[],
                                     int rangeRvalue[])
{
    int count = 0;
  
    // maximum value of the range
    int maxn = Integer.MIN_VALUE;
    for (int i = 0; i < n; i++)
        if (rangeRvalue[i] > maxn)
            maxn = rangeRvalue[i];
  
    // counter array
    int preSum[] = new int[maxn + 5];
    for(int i = 0; i < (maxn + 5); i++)
    preSum[i] = 0;
  
    // incrementing and decrementing the
    // leftmost and next value of rightmost value
    // of each range by 1 respectively
    for (int i = 0; i < n; i++)
    {
        preSum[rangeLvalue[i]]++;
        preSum[rangeRvalue[i] + 1]--;
    }
  
    // presum gives the no of occurrence of
    // each element
    for (int i = 1; i <= maxn; i++) {
        preSum[i] += preSum[i - 1];
    }
  
    for (int i = 1; i <= maxn; i++) {
  
        // check if the number appears atleast k times
        if (preSum[i] >= k) {
  
            // increase the counter if
            // condition satisfies
            count++;
        }
    }
  
    // return the result
    return count;
}
  
// Driver Code
public static void main(String args[])
{
    int n = 3, k = 2;
  
    int rangeLvalue[] = { 91, 92, 97 };
    int rangeRvalue[] = { 94, 97, 99 };
  
    System.out.println(countNumbers(n, k, rangeLvalue,
                                        rangeRvalue));
  
}
}
  
// This code is contributed by ankita_saini

Python

# Python efficient program to find count of
# numbers appearing in the given
# ranges at-least K times
  
# Function to find the no of occurrence
def countNumbers(n, k, rangeLvalue, rangeRvalue):
    count = 0
  
    # maximum value of the range
    maxn = -float('inf')
    for i in range(n):
        if rangeRvalue[i] > maxn:
            maxn = rangeRvalue[i]
  
    # counter array
    preSum = [0]*(maxn + 5)
  
    # incrementing and decrementing the
    # leftmost and next value of rightmost value
    # of each range by 1 respectively
    for i in range(n):
        preSum[rangeLvalue[i]] += 1
        preSum[rangeRvalue[i] + 1] -= 1
  
    # presum gives the no of occurrence of
    # each element
    for i in range(1, maxn+1):
        preSum[i] += preSum[i - 1]
  
    for i in range(1, maxn+1):
  
        # check if the number appears atleast k times
        if preSum[i] >= k:
  
            # increase the counter if
            # condition satisfies
            count += 1
  
    # return the result
    return count
  
  
# Driver Code
n = 3
k = 2
  
rangeLvalue = [91, 92, 97]
rangeRvalue = [94, 97, 99]
  
print(countNumbers(n, k, rangeLvalue, rangeRvalue))
  
# This code is contributed by ankush_953

C#

// C# efficient program to 
// find count of numbers 
// appearing in the given
// ranges at-least K times
using System;
  
class GFG
{
  
// Function to find the 
// no of occurrence
static int countNumbers(int n, int k, 
                        int []rangeLvalue,
                        int []rangeRvalue)
{
    int count = 0;
  
    // maximum value of the range
    int maxn = Int32.MinValue;
    for (int i = 0; i < n; i++)
        if (rangeRvalue[i] > maxn)
            maxn = rangeRvalue[i];
  
    // counter array
    int []preSum = new int[maxn + 5];
    for(int i = 0; i < (maxn + 5); i++)
    preSum[i] = 0;
  
    // incrementing and decrementing 
    // the leftmost and next value 
    // of rightmost value of each 
    // range by 1 respectively
    for (int i = 0; i < n; i++)
    {
        preSum[rangeLvalue[i]]++;
        preSum[rangeRvalue[i] + 1]--;
    }
  
    // presum gives the no of 
    // occurrence of each element
    for (int i = 1; i <= maxn; i++) 
    {
        preSum[i] += preSum[i - 1];
    }
  
    for (int i = 1; i <= maxn; i++)
    {
  
        // check if the number 
        // appears atleast k times
        if (preSum[i] >= k) 
        {
  
            // increase the counter if
            // condition satisfies
            count++;
        }
    }
  
    // return the result
    return count;
}
  
// Driver Code
public static void Main(String []args)
{
    int n = 3, k = 2;
  
    int []rangeLvalue = { 91, 92, 97 };
    int []rangeRvalue = { 94, 97, 99 };
  
    Console.WriteLine(countNumbers(n, k, rangeLvalue,
                                        rangeRvalue));
}
}
  
// This code is contributed
// by ankita_saini
输出:
4

时间复杂度:O(N + max(rangeRvalue []))
辅助空间:O(N)