📜  计算给定值所在的时间间隔数

📅  最后修改于: 2021-05-04 21:29:47             🧑  作者: Mango

给定一个2D数组N整数间隔[L, R]和一个值V 。对于每个间隔[l i ,r i ] ,任务是检查V是否位于liri之间(包括两者之间)。任务是打印满足此要求的间隔计数。
注意: 1 <= l i <= r i

例子:

方法1:遍历整个数组,检查每个间隔[li,ri] (如果V不在该间隔中)。如果是这样,则增加计数。

下面是上述方法的实现:

C++
// CPP program to count the
// number of intervals in which
// a given value lies
  
#include
using namespace std;
  
    // Function to count the
    // number of intervals in which
    // a given value lies
    int countIntervals(int arr[][2], int V, int N)
    {
        // Variable to store the count of intervals
        int count = 0;
  
        // Variables to store start and end of an interval
        int li, ri;
  
        for (int i = 0; i < N; i++) 
       {
            li = arr[i][0];
            ri = arr[i][1];
  
            // Implies V lies in the interval
            // so increase count
            if (V >= li && V <= ri)
                count++;
        }
        return count;
    }
  
    // Driver code
    int main()
    {
        int arr[][2] = { { 1, 10 }, { 5, 10 },{ 15, 25 }, { 7, 12 }, { 20, 25 } };
  
        int V = 7;
  
        // length of the array
        int N = sizeof(arr)/sizeof(arr[0]);
        cout<<(countIntervals(arr, V, N))<


Java
// Java program to count the
// number of intervals in which
// a given value lies
  
import java.io.*;
import java.util.*;
import java.lang.*;
  
class GFG {
  
    // Function to count the
    // number of intervals in which
    // a given value lies
    static int countIntervals(int[][] arr, int V, int N)
    {
        // Variable to store the count of intervals
        int count = 0;
  
        // Variables to store start and end of an interval
        int li, ri;
  
        for (int i = 0; i < N; i++) {
            li = arr[i][0];
            ri = arr[i][1];
  
            // Implies V lies in the interval
            // so increase count
            if (V >= li && V <= ri)
                count++;
        }
        return count;
    }
  
    // Driver code
    public static void main(String args[])
    {
        int[][] arr = { { 1, 10 }, { 5, 10 }, 
         { 15, 25 }, { 7, 12 }, { 20, 25 } };
  
        int V = 7;
  
        // length of the array
        int N = arr.length;
  
        System.out.println(countIntervals(arr, V, N));
    }
}


Python3
# Python 3 program to count the
# number of intervals in which
# a given value lies
  
# Function to count the
# number of intervals in which
# a given value lies
def countIntervals(arr, V, N) :
      
    # Variable to store the 
    # count of intervals
    count = 0
  
    # Variables to store start and
    # end of an interval
    for i in range(N) :
              
        # Variables to store start and 
        # end of an interval
        li = arr[i][0]
        ri = arr[i][1]
  
        # Implies V lies in the interval
        # so increase count
        if (V >= li and V <= ri) :
            count += 1
          
    return count;
      
# Driver Code
if __name__ == "__main__" :
      
    arr = [ [ 1, 10 ], [ 5, 10 ], 
            [ 15, 25 ], [ 7, 12 ], 
            [ 20, 25 ] ]
  
    V = 7
  
    # length of the array
    N = len(arr)
  
    print((countIntervals(arr, V, N)))
  
# This code is contributed by Ryuga


C#
// C# program to count the
// number of intervals in which
// a given value lies
using System;
  
class GFG 
{
  
// Function to count the
// number of intervals in which
// a given value lies
static int countIntervals(int[,] arr, int V, int N)
{
    // Variable to store the count of intervals
    int count = 0;
  
    // Variables to store start and end of an interval
    int li, ri;
  
    for (int i = 0; i < N ; i++) 
    {
        li = arr[i, 0];
        ri = arr[i, 1];
  
        // Implies V lies in the interval
        // so increase count
        if (V >= li && V <= ri)
            count++;
    }
    return count;
}
  
// Driver code
public static void Main()
{
    int[,] arr = new int[,]{ { 1, 10 }, { 5, 10 }, 
    { 15, 25 }, { 7, 12 }, { 20, 25 } };
  
    int V = 7;
  
    // length of the array
    int N = arr.GetLength(0);
  
    Console.WriteLine(countIntervals(arr, V, N));
}
}
  
// This code is contributed by Akanksha Rai


PHP
= $li && $V <= $ri)
            $count++;
    }
    return $count;
}
  
// Driver code
$arr = array(array(1, 10), 
             array(5, 10), 
             array(15, 25), 
             array(7, 12), 
             array(20, 25));
  
$V = 7;
  
// length of the array
$N = sizeof($arr);
  
echo countIntervals($arr, $V, $N);
  
// This code is contributed 
// by Akanksha Rai
?>


C++
// C++ program to count the
// number of intervals in which
// a given value lies
#include
using namespace std;
  
const int MAX_VAL = 200000;
  
// Function to count the
// number of intervals in which
// a given value lies
int countIntervals(int arr[][2], int V, int N)
{
    // Variables to store overall minimum and
    // maximum of the intervals
    int min = INT_MAX;
    int max = INT_MIN;
  
    // Variables to store start and 
    // end of an interval
    int li, ri;
  
    // Frequency array to keep track of
    // how many of the given intervals 
    // an element lies in
    int freq[MAX_VAL];
  
    for (int i = 0; i < N; i++)
    {
        li = arr[i][0];
        freq[li] = freq[li] + 1;
        ri = arr[i][1];
        freq[ri + 1] = freq[ri + 1] - 1;
  
        if (li < min)
            min = li;
        if (ri > max)
            max = ri;
    }
  
    // Constructing the frequency array
    for (int i = min; i <= max; i++)
        freq[i] = freq[i] + freq[i - 1];
  
    return freq[V];
}
  
// Driver code
int main()
{
    int arr[5][2] = { { 1, 10 }, { 5, 10 }, 
                      { 15, 25 }, { 7, 12 },
                      { 20, 25 } };
  
    int V = 7;
  
    // length of the array
    int N = sizeof(arr) / sizeof(arr[0]);
  
    cout << (countIntervals(arr, V, N));
}
  
// This code is contributed by
// Surendra_Gangwar


Java
// Java program to count the
// number of intervals in which
// a given value lies
  
import java.io.*;
import java.util.*;
import java.lang.*;
  
class GFG {
  
    static final int MAX_VAL = 200000;
  
    // Function to count the
    // number of intervals in which
    // a given value lies
    static int countIntervals(int[][] arr, int V, int N)
    {
        // Variables to store overall minimum and
        // maximum of the intervals
        int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;
  
        // Variables to store start and end of an interval
        int li, ri;
  
        // Frequency array to keep track of
        // how many of the given intervals an element lies in
        int[] freq = new int[MAX_VAL];
  
        for (int i = 0; i < N; i++) {
            li = arr[i][0];
            freq[li] = freq[li] + 1;
            ri = arr[i][1];
            freq[ri + 1] = freq[ri + 1] - 1;
  
            if (li < min)
                min = li;
            if (ri > max)
                max = ri;
        }
  
        // Constructing the frequency array
        for (int i = min; i <= max; i++)
            freq[i] = freq[i] + freq[i - 1];
  
        return freq[V];
    }
  
    // Driver code
    public static void main(String args[])
    {
        int[][] arr = { { 1, 10 }, { 5, 10 }, 
         { 15, 25 }, { 7, 12 }, { 20, 25 } };
  
        int V = 7;
  
        // length of the array
        int N = arr.length;
  
        System.out.println(countIntervals(arr, V, N));
    }
}


Python3
# Python3 program to count the number of
# intervals in which a given value lies 
MAX_VAL = 200000
  
# Function to count the number of 
# intervals in which a given value lies 
def countIntervals(arr, V, N): 
      
    # Variables to store overall minimumimum 
    # and maximumimum of the intervals 
    minimum = float("inf")
    maximum = 0
  
    # Frequency array to keep track of 
    # how many of the given intervals 
    # an element lies in 
    freq = [0] * (MAX_VAL) 
  
    for i in range(0, N):
        li = arr[i][0] 
        freq[li] = freq[li] + 1
        ri = arr[i][1] 
        freq[ri + 1] = freq[ri + 1] - 1
  
        if li < minimum: 
            minimum = li 
          
        if ri > maximum:
            maximum = ri 
          
    # Constructing the frequency array 
    for i in range(minimum, maximum + 1): 
        freq[i] = freq[i] + freq[i - 1] 
  
    return freq[V] 
      
# Driver Code
if __name__ == "__main__":
      
    arr = [[1, 10], [5, 10], [15, 25], 
           [7, 12], [20, 25]] 
  
    V = 7
  
    # length of the array 
    N = len(arr) 
  
    print(countIntervals(arr, V, N)) 
  
# This code is contributed 
# by Rituraj Jain


C#
// C# program to count the
// number of intervals in which
// a given value lies
using System;
  
class GFG {
  
    static  int MAX_VAL = 200000;
  
    // Function to count the
    // number of intervals in which
    // a given value lies
    static int countIntervals(int[,] arr, int V, int N)
    {
        // Variables to store overall minimum and
        // maximum of the intervals
        int min = int.MaxValue, max = int.MinValue;
  
        // Variables to store start and end of an interval
        int li, ri;
  
        // Frequency array to keep track of
        // how many of the given intervals an element lies in
        int[] freq = new int[MAX_VAL];
  
        for (int i = 0; i < N; i++) {
            li = arr[i,0];
            freq[li] = freq[li] + 1;
            ri = arr[i,1];
            freq[ri + 1] = freq[ri + 1] - 1;
  
            if (li < min)
                min = li;
            if (ri > max)
                max = ri;
        }
  
        // Constructing the frequency array
        for (int i = min; i <= max; i++)
            freq[i] = freq[i] + freq[i - 1];
  
        return freq[V];
    }
  
    // Driver code
    public static void Main()
    {
        int[,] arr = new int[,]{ { 1, 10 }, { 5, 10 }, 
        { 15, 25 }, { 7, 12 }, { 20, 25 } };
  
        int V = 7;
  
        // length of the array
        int N = arr.Length/arr.Rank;
  
        Console.WriteLine(countIntervals(arr, V, N));
    }
}
// this code is contributed by mits


PHP
 $max)
            $max = $ri;
    }
  
    // Constructing the frequency array
    for ($i = $min; $i <= $max; $i++)
        $freq[$i] = $freq[$i] + $freq[$i - 1];
  
    return $freq[$V];
}
  
// Driver code
$arr = array(array( 1, 10 ),
             array( 5, 10 ), 
             array( 15, 25 ),
             array( 7, 12 ),
             array( 20, 25 ));
  
$V = 7;
  
// length of the array
$N = count($arr);
  
echo (countIntervals($arr, $V, $N));
  
// This code is contributed by mits
?>


输出:
3

方法2(对于查询有效):使用频率数组来跟踪元素所在的给定间隔中有多少个。

  1. 对于每个间隔[L,R],将freq [L]设置为freq [L] + 1,将freq [R + 1]设置为freq [R + 1] – 1,指示间隔的开始和结束。
  2. 跟踪整个时间间隔的最小值和最大值。
  3. 从最小到最大开始,从其前一个元素构建频率阵列,即
    freq [i] = freq [i] + freq [i – 1]。
  4. 然后,所需的时间间隔计数由freq [V]给出。

下面是上述方法的实现:

C++

// C++ program to count the
// number of intervals in which
// a given value lies
#include
using namespace std;
  
const int MAX_VAL = 200000;
  
// Function to count the
// number of intervals in which
// a given value lies
int countIntervals(int arr[][2], int V, int N)
{
    // Variables to store overall minimum and
    // maximum of the intervals
    int min = INT_MAX;
    int max = INT_MIN;
  
    // Variables to store start and 
    // end of an interval
    int li, ri;
  
    // Frequency array to keep track of
    // how many of the given intervals 
    // an element lies in
    int freq[MAX_VAL];
  
    for (int i = 0; i < N; i++)
    {
        li = arr[i][0];
        freq[li] = freq[li] + 1;
        ri = arr[i][1];
        freq[ri + 1] = freq[ri + 1] - 1;
  
        if (li < min)
            min = li;
        if (ri > max)
            max = ri;
    }
  
    // Constructing the frequency array
    for (int i = min; i <= max; i++)
        freq[i] = freq[i] + freq[i - 1];
  
    return freq[V];
}
  
// Driver code
int main()
{
    int arr[5][2] = { { 1, 10 }, { 5, 10 }, 
                      { 15, 25 }, { 7, 12 },
                      { 20, 25 } };
  
    int V = 7;
  
    // length of the array
    int N = sizeof(arr) / sizeof(arr[0]);
  
    cout << (countIntervals(arr, V, N));
}
  
// This code is contributed by
// Surendra_Gangwar

Java

// Java program to count the
// number of intervals in which
// a given value lies
  
import java.io.*;
import java.util.*;
import java.lang.*;
  
class GFG {
  
    static final int MAX_VAL = 200000;
  
    // Function to count the
    // number of intervals in which
    // a given value lies
    static int countIntervals(int[][] arr, int V, int N)
    {
        // Variables to store overall minimum and
        // maximum of the intervals
        int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;
  
        // Variables to store start and end of an interval
        int li, ri;
  
        // Frequency array to keep track of
        // how many of the given intervals an element lies in
        int[] freq = new int[MAX_VAL];
  
        for (int i = 0; i < N; i++) {
            li = arr[i][0];
            freq[li] = freq[li] + 1;
            ri = arr[i][1];
            freq[ri + 1] = freq[ri + 1] - 1;
  
            if (li < min)
                min = li;
            if (ri > max)
                max = ri;
        }
  
        // Constructing the frequency array
        for (int i = min; i <= max; i++)
            freq[i] = freq[i] + freq[i - 1];
  
        return freq[V];
    }
  
    // Driver code
    public static void main(String args[])
    {
        int[][] arr = { { 1, 10 }, { 5, 10 }, 
         { 15, 25 }, { 7, 12 }, { 20, 25 } };
  
        int V = 7;
  
        // length of the array
        int N = arr.length;
  
        System.out.println(countIntervals(arr, V, N));
    }
}

Python3

# Python3 program to count the number of
# intervals in which a given value lies 
MAX_VAL = 200000
  
# Function to count the number of 
# intervals in which a given value lies 
def countIntervals(arr, V, N): 
      
    # Variables to store overall minimumimum 
    # and maximumimum of the intervals 
    minimum = float("inf")
    maximum = 0
  
    # Frequency array to keep track of 
    # how many of the given intervals 
    # an element lies in 
    freq = [0] * (MAX_VAL) 
  
    for i in range(0, N):
        li = arr[i][0] 
        freq[li] = freq[li] + 1
        ri = arr[i][1] 
        freq[ri + 1] = freq[ri + 1] - 1
  
        if li < minimum: 
            minimum = li 
          
        if ri > maximum:
            maximum = ri 
          
    # Constructing the frequency array 
    for i in range(minimum, maximum + 1): 
        freq[i] = freq[i] + freq[i - 1] 
  
    return freq[V] 
      
# Driver Code
if __name__ == "__main__":
      
    arr = [[1, 10], [5, 10], [15, 25], 
           [7, 12], [20, 25]] 
  
    V = 7
  
    # length of the array 
    N = len(arr) 
  
    print(countIntervals(arr, V, N)) 
  
# This code is contributed 
# by Rituraj Jain

C#

// C# program to count the
// number of intervals in which
// a given value lies
using System;
  
class GFG {
  
    static  int MAX_VAL = 200000;
  
    // Function to count the
    // number of intervals in which
    // a given value lies
    static int countIntervals(int[,] arr, int V, int N)
    {
        // Variables to store overall minimum and
        // maximum of the intervals
        int min = int.MaxValue, max = int.MinValue;
  
        // Variables to store start and end of an interval
        int li, ri;
  
        // Frequency array to keep track of
        // how many of the given intervals an element lies in
        int[] freq = new int[MAX_VAL];
  
        for (int i = 0; i < N; i++) {
            li = arr[i,0];
            freq[li] = freq[li] + 1;
            ri = arr[i,1];
            freq[ri + 1] = freq[ri + 1] - 1;
  
            if (li < min)
                min = li;
            if (ri > max)
                max = ri;
        }
  
        // Constructing the frequency array
        for (int i = min; i <= max; i++)
            freq[i] = freq[i] + freq[i - 1];
  
        return freq[V];
    }
  
    // Driver code
    public static void Main()
    {
        int[,] arr = new int[,]{ { 1, 10 }, { 5, 10 }, 
        { 15, 25 }, { 7, 12 }, { 20, 25 } };
  
        int V = 7;
  
        // length of the array
        int N = arr.Length/arr.Rank;
  
        Console.WriteLine(countIntervals(arr, V, N));
    }
}
// this code is contributed by mits

的PHP

 $max)
            $max = $ri;
    }
  
    // Constructing the frequency array
    for ($i = $min; $i <= $max; $i++)
        $freq[$i] = $freq[$i] + $freq[$i - 1];
  
    return $freq[$V];
}
  
// Driver code
$arr = array(array( 1, 10 ),
             array( 5, 10 ), 
             array( 15, 25 ),
             array( 7, 12 ),
             array( 20, 25 ));
  
$V = 7;
  
// length of the array
$N = count($arr);
  
echo (countIntervals($arr, $V, $N));
  
// This code is contributed by mits
?>
输出:
3