📌  相关文章
📜  为每个 Array 元素找到 K 使得至少 K 个前缀≥ K

📅  最后修改于: 2021-09-04 08:30:19             🧑  作者: Mango

给定一个由N 个非负整数组成的数组arr[] ,任务是为每个索引找到一个整数K ,使得数组中至少有 K 个整数,直到该索引大于或等于 K。

注意:考虑基于 1 的索引

例子:

天真的方法:
最简单的方法是为[0, i]范围内的数组的所有元素找到K的值,其中i是数组arr[]的索引,使用文章链接中使用的有效方法在这里给出.

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

有效的方法:
这个想法是使用 Multiset(Red-Black Tree)。 Multiset 按排序顺序存储值,这有助于检查 multiset 中的当前最小值是否大于或等于其大小。如果是,则整数 K 的值将是多重集的大小。

下面是实现步骤:

  1. 从索引 0 到 N-1 遍历数组。
  2. 对于每个索引,将元素插入多重集中并检查多重集中的最小值是否小于多重集的大小。
  3. 如果为真,则擦除起始元素并打印多重集的大小。
  4. 如果为 false,则只需打印多重集的大小。
  5. 多重集的大小是每个索引 i 所需的 K 值。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the K-value
// for every index in the array
int print_h_index(int arr[], int N)
{
    // Multiset to store the array
    // in the form of red-black tree
    multiset ms;
 
    // Iterating over the array
    for (int i = 0; i < N; i++) {
 
        // Inserting the current
        // value in the multiset
        ms.insert(arr[i]);
 
        // Condition to check if
        // the smallest value
        // in the set is less than
        // it's size
        if (*ms.begin()
            < ms.size()) {
 
            // Erase the smallest
            // value
            ms.erase(ms.begin());
        }
 
        // h-index value will be
        // the size of the multiset
        cout << ms.size() << " ";
    }
}
 
// Driver Code
int main()
{
 
    // array
    int arr[] = { 9, 10, 7, 5, 0,
                10, 2, 0 };
 
    // Size of the array
    int N = sizeof(arr)
            / sizeof(arr[0]);
 
    // function call
    print_h_index(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
     
// Function to find the K-value
// for every index in the array
static void print_h_index(int arr[], int N)
{
     
    // Multiset to store the array
    // in the form of red-black tree
    List ms = new ArrayList();
 
    // Iterating over the array
    for(int i = 0; i < N; i++)
    {
         
        // Inserting the current
        // value in the multiset
        ms.add(arr[i]);
 
        // Condition to check if
        // the smallest value
        // in the set is less than
        // it's size
        int t = Collections.min(ms);
        if (t < ms.size())
        {
 
            // Erase the smallest
            // value
            ms.remove(ms.indexOf(t));
        }
 
        // h-index value will be
        // the size of the multiset
        System.out.print(ms.size() + " ");
    }
}
 
// Driver code
public static void main(String[] args)
{
     
    // Array
    int arr[] = { 9, 10, 7, 5, 0,
                10, 2, 0 };
     
    // Size of the array
    int N = arr.length;
     
    // Function call
    print_h_index(arr, N);
}
}
 
// This code is contributed by offbeat


Python3
# Python3 program for the above approach
  
# Function to find the K-value
# for every index in the array
def print_h_index(arr, N):
 
    # Multiset to store the array
    # in the form of red-black tree
    ms = []
  
    # Iterating over the array
    for i in range(N):
  
        # Inserting the current
        # value in the multiset
        ms.append(arr[i])
        ms.sort()
         
        # Condition to check if
        # the smallest value
        # in the set is less than
        # it's size
        if (ms[0] < len(ms)):
  
            # Erase the smallest
            # value
            ms.pop(0)
  
        # h-index value will be
        # the size of the multiset
        print(len(ms), end = ' ')
         
# Driver Code
if __name__=='__main__':
 
    # Array
    arr = [ 9, 10, 7, 5, 0, 10, 2, 0 ]
  
    # Size of the array
    N = len(arr)
  
    # Function call
    print_h_index(arr, N)
 
# This code is contributed by pratham76


C#
// C# program for the above approach
using System;
using System.Collections;
using System.Collections.Generic;
 
class GFG{
     
// Function to find the K-value
// for every index in the array
static void print_h_index(int []arr, int N)
{
     
    // Multiset to store the array
    // in the form of red-black tree
    ArrayList ms = new ArrayList();
 
    // Iterating over the array
    for(int i = 0; i < N; i++)
    {
         
        // Inserting the current
        // value in the multiset
        ms.Add(arr[i]);
 
        // Condition to check if
        // the smallest value
        // in the set is less than
        // it's size
        int t = int.MaxValue;
        foreach(int x in ms)
        {
            if(x < t)
            {
                t = x;
            }
        }
         
        if (t < ms.Count)
        {
             
            // Erase the smallest
            // value
            ms.Remove(t);
        }
 
        // h-index value will be
        // the size of the multiset
        Console.Write(ms.Count + " ");
    }
}
 
// Driver code
public static void Main(string[] args)
{
     
    // Array
    int []arr = { 9, 10, 7, 5, 0,
                  10, 2, 0 };
     
    // Size of the array
    int N = arr.Length;
     
    // Function call
    print_h_index(arr, N);
}
}
 
// This code is contributed by rutvik_56


Javascript


输出:
1 2 3 4 4 5 5 5

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live