📌  相关文章
📜  最小化要为每个元素添加的连续元素,以使 Array 的长度至少为 C

📅  最后修改于: 2022-05-13 01:56:10.453000             🧑  作者: Mango

最小化要为每个元素添加的连续元素,以使 Array 的长度至少为 C

给定一个大小为N的排序数组arr[] ,其中arr[i]表示序列的起始位置,任务是找到可以为每个 Array 元素添加的连续元素的最小数量(例如K ),以使数组长度至少C

注意:连续数字可以相加直到arr[i]+K(arr[i+1]-1)的最小值。

例子:

朴素方法:解决问题的基本思想是简单地遍历数组。

请按照以下步骤操作:

  • 从取K = 1 开始,对每个 K 检查是否有可能获得序列的C个元素。
  • 如果可能,则K是答案,否则将K加 1 并继续直到满足条件。

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

高效的方法:可以基于以下思路解决问题:

请按照以下步骤解决问题:

  • 通过设置low = 1high = C尝试使用二分查找1 到 C的范围内找到K的值;
    • 如果至少收集了 C 个元素,则检查左半部分以找到K的最小值。
    • 如果不是,则检查上半部分(即大于中间值的值)。
  • 搜索完成后返回,这将代表最小K

下面是上述方法的实现:

C++
// C++ code to implement the above approach.
#include 
using namespace std;
 
// Function to find the min value of K
void solver(vector arr, int C)
{
  int low = 1;
  int high = C;
  int mid = 0;
  int sum = 0;
  int n = arr.size();
 
  // Binary search to find min of K
  while (low < high) {
    mid = (low + high) / 2;
    sum = mid;
    for (int k = 1; k < n; k++) {
 
      sum += min(mid, arr[k] - arr[k - 1]);
    }
 
    // If atleast C numbers are found,
    // then search in left side
    // to get minimum value of k
    if (sum >= C) {
      high = mid;
    }
    else {
      // If we are not able to get
      // atleast C elements,
      // then move in
      // right direction
      low = mid + 1;
    }
  }
  cout << low << endl;
}
 
// Driver code
int main()
{
  vector arr = { 2, 4, 10 };
  int C = 10;
  solver(arr, C);
}
 
// This code is contributed by Taranpreet


Java
// Java code to implement the above approach.
 
import java.io.*;
 
class GFG {
 
    // Function to find the min value of K
    public static void solver(int[] arr, int C)
    {
        int low = 1;
        int high = C;
        int mid = 0;
        int sum = 0;
        int n = arr.length;
 
        // Binary search to find min of K
        while (low < high) {
            mid = (low + high) / 2;
            sum = mid;
            for (int k = 1; k < n; k++) {
 
                sum
                    += Math.min(mid,
                                arr[k] - arr[k - 1]);
            }
 
            // If atleast C numbers are found,
            // then search in left side
            // to get minimum value of k
            if (sum >= C) {
                high = mid;
            }
            else {
                // If we are not able to get
                // atleast C elements,
                // then move in
                // right direction
                low = mid + 1;
            }
        }
        System.out.println(low);
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int arr[] = { 2, 4, 10 };
        int C = 10;
        solver(arr, C);
    }
}


Python3
# Python code for the above approach
 
# Function to find the min value of K
def solver(arr, C):
    low = 1
    high = C
    mid = 0
    sum = 0
    n = len(arr)
 
    # Binary search to find min of K
    while (low < high):
        mid = (low + high) // 2
        sum = mid
        for k in range(1,n):
 
            sum += min(mid,arr[k] - arr[k - 1])
  
 
        # If atleast C numbers are found,
        # then search in left side
        # to get minimum value of k
        if (sum >= C):
            high = mid
        else:
            # If we are not able to get
            # atleast C elements,
            # then move in
            # right direction
            low = mid + 1
    print(low)
 
# Driver code
arr = [2, 4, 10]
C = 10
solver(arr, C)
 
# This code is contributed byshinjanpatra


C#
// C# code to implement the above approach.
using System;
 
class GFG {
 
  // Function to find the min value of K
  static void solver(int[] arr, int C)
  {
    int low = 1;
    int high = C;
    int mid = 0;
    int sum = 0;
    int n = arr.Length;
 
    // Binary search to find min of K
    while (low < high) {
      mid = (low + high) / 2;
      sum = mid;
      for (int k = 1; k < n; k++) {
 
        sum
          += Math.Min(mid,
                      arr[k] - arr[k - 1]);
      }
 
      // If atleast C numbers are found,
      // then search in left side
      // to get minimum value of k
      if (sum >= C) {
        high = mid;
      }
      else {
        // If we are not able to get
        // atleast C elements,
        // then move in
        // right direction
        low = mid + 1;
      }
    }
    Console.WriteLine(low);
  }
 
  // Driver code
  public static void Main()
  {
    int []arr = { 2, 4, 10 };
    int C = 10;
    solver(arr, C);
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
4

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