📌  相关文章
📜  为每个数组元素找到K,以使至少K个前缀≥K

📅  最后修改于: 2021-05-17 17:10:38             🧑  作者: Mango

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

注意:考虑基于1的索引

例子:

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

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

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

以下是实施步骤:

  1. 将数组从索引0遍历到N-1。
  2. 对于每个索引,将元素插入多重集并检查多重集中的最小值是否小于多重集的大小。
  3. 如果为true,则擦除起始元素并打印多集的大小。
  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


输出:
1 2 3 4 4 5 5 5

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