📌  相关文章
📜  查询从给定索引开始计算子数组中最大数组元素的出现次数

📅  最后修改于: 2021-10-27 07:17:58             🧑  作者: Mango

给定两个由N 个整数组成的数组arr[]q[] ,任务是针对每个查询q[i]确定子数组[q[i], arr[N – 1]]

例子:

朴素的方法:最简单的方法是遍历数组并找到最大的数组元素。现在,对于每个查询 q[i],遍历子数组[q[i], arr[N – 1]]并打印子数组中最大元素的出现次数。

时间复杂度: O(N 2 )
辅助空间: O(1)

高效的方法:为了优化上述方法,想法是使用Hashing。以下是步骤:

  • 创建一个数组maxFreqVec[]来存储从给定索引q[i]N的最大元素的出现次数。
  • 从右侧遍历数组arr[]并跟踪数组中的最大元素,并使用该最大元素的出现更新该索引处的数组maxFreqVec[]
  • 在上述步骤之后,遍历数组q[]并打印值maxFreqVec[q[i] – 1]作为每个查询的每个子数组中的最大元素。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include
using namespace std;
 
// Function to find occurrence of
// max element in given subarray
void FreqOfMaxElement(vector arr,
                      vector q)
{
     
    // Store the frequency of maximum
    // element
    vector maxFreqVec(arr.size());
 
    int maxSoFar = INT_MIN;
    int maxFreq = 0;
 
    // Traverse over the array arr[]
    // from right to left
    for(int i = arr.size() - 1; i >= 0; i--)
    {
         
        // If curr element is greater
        // than maxSofar
        if (arr[i] > maxSoFar)
        {
             
            // Reset maxSofar and maxFreq
            maxSoFar = arr[i];
            maxFreq = 1;
        }
 
        // If curr is equal to maxSofar
        else if (arr[i] == maxSoFar)
        {
             
            // Increment the maxFreq
            maxFreq++;
        }
 
        // Update maxFreqVec[i]
        maxFreqVec[i] = maxFreq;
    }
 
    // Print occurrence of maximum
    // element for each query
    for(int k : q)
    {
        cout << maxFreqVec[k - 1] << " ";
    }
}
 
// Driver Code
int main()
{
    vector arr = { 5, 4, 5, 3, 2 };
    vector q = { 1, 2, 3, 4, 5 };
     
    // Function Call
    FreqOfMaxElement(arr, q);
}
 
// This code is contributed by mohit kumar 29


Java
// Java program for the above approach
import java.util.*;
import java.lang.*;
 
class GFG {
 
    // Function to find occurrence of
    // max element in given subarray
    static void FreqOfMaxElement(
        int[] arr, int[] q)
    {
 
        // Store the frequency of maximum
        // element
        int[] maxFreqVec
            = new int[arr.length];
 
        int maxSoFar = Integer.MIN_VALUE;
        int maxFreq = 0;
 
        // Traverse over the array arr[]
        // from right to left
        for (int i = arr.length - 1;
             i >= 0; i--) {
 
            // If curr element is greater
            // than maxSofar
            if (arr[i] > maxSoFar) {
 
                // Reset maxSofar and maxFreq
                maxSoFar = arr[i];
                maxFreq = 1;
            }
 
            // If curr is equal to maxSofar
            else if (arr[i] == maxSoFar) {
 
                // Increment the maxFreq
                maxFreq++;
            }
 
            // Update maxFreqVec[i]
            maxFreqVec[i] = maxFreq;
        }
 
        // Print occurrence of maximum
        // element for each query
        for (int k : q) {
            System.out.print(
                maxFreqVec[k - 1] + " ");
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int[] arr = { 5, 4, 5, 3, 2 };
        int[] q = { 1, 2, 3, 4, 5 };
 
        // Function Call
        FreqOfMaxElement(arr, q);
    }
}


Python3
# Python3 program for
# the above approach
import sys;
 
# Function to find occurrence
# of max element in given
# subarray
def FreqOfMaxElement(arr, q):
   
    # Store the frequency of
    # maximum element
    maxFreqVec = [0] * (len(arr));
 
    maxSoFar = -sys.maxsize;
    maxFreq = 0;
 
    # Traverse over the array
    # arr from right to left
    for i in range(len(arr)-1,
                   -1, -1):
 
        # If curr element is
        # greater than maxSofar
        if (arr[i] > maxSoFar):
 
            # Reset maxSofar
            # and maxFreq
            maxSoFar = arr[i];
            maxFreq = 1;
 
        # If curr is equal to
        # maxSofar
        elif (arr[i] == maxSoFar):
 
            # Increment the
            # maxFreq
            maxFreq += 1;
 
        # Update maxFreqVec[i]
        maxFreqVec[i] = maxFreq;
 
    # Proccurrence of maximum
    # element for each query
    for i in range(0, len(q)):
        print(maxFreqVec[q[i] - 1],
              end = " ");
 
# Driver Code
if __name__ == '__main__':
   
    arr = [5, 4, 5, 3, 2];
    q = [1, 2, 3, 4, 5];
 
    # Function Call
    FreqOfMaxElement(arr, q);
 
# This code is contributed by shikhasingrajput


C#
// C# program for the
// above approach
using System;
class GFG{
     
// Function to find occurrence of
// max element in given subarray
static void FreqOfMaxElement(int[] arr,
                             int[] q)
{
  // Store the frequency of
  // maximum element
  int[] maxFreqVec = new int[arr.Length];
 
  int maxSoFar = Int32.MinValue;
  int maxFreq = 0;
 
  // Traverse over the array arr[]
  // from right to left
  for (int i = arr.Length - 1;
           i >= 0; i--)
  {
    // If curr element is greater
    // than maxSofar
    if (arr[i] > maxSoFar)
    {
      // Reset maxSofar and maxFreq
      maxSoFar = arr[i];
      maxFreq = 1;
    }
 
    // If curr is equal to maxSofar
    else if (arr[i] == maxSoFar)
    {
      // Increment the maxFreq
      maxFreq++;
    }
 
    // Update maxFreqVec[i]
    maxFreqVec[i] = maxFreq;
  }
 
  // Print occurrence of maximum
  // element for each query
  foreach (int k in q)
  {
    Console.Write(maxFreqVec[k - 1] +
                  " ");
  }
}
     
// Driver code
static void Main()
{
  int[] arr = {5, 4, 5, 3, 2};
  int[] q = {1, 2, 3, 4, 5};
 
  // Function Call
  FreqOfMaxElement(arr, q);
  }
}
 
// This code is contributed by divyeshrabadiya07


Javascript


输出:
2 1 1 1 1

时间复杂度: O(N)
辅助空间: O(N)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程