最小化要为每个元素添加的连续元素,以使 Array 的长度至少为 C
给定一个大小为N的排序数组arr[] ,其中arr[i]表示序列的起始位置,任务是找到可以为每个 Array 元素添加的连续元素的最小数量(例如K ),以使数组长度至少C 。
注意:连续数字可以相加直到arr[i]+K或(arr[i+1]-1)的最小值。
例子:
Input: arr[] = { 1, 2, 4, 5, 7}, C = 3
Output: 1
Explanation: For K = 1, 5 numbers are possible(one from each position).
From position 1 – select ‘1’.
From position 2 – select ‘2’
From position 4 – select ‘4’
From position 5 – select ‘5’
From position 7 – select ‘7’
Total elements of sequence =5, which is greater than 3
Input: arr [] = {2, 4, 10}, C = 10
Output: 4
Explanation: Given sequences are: {2, 3}, {4, 5, 6, 7, 8, 9}, {10, 11, 12, 13, 14 . . . }
Selected elements = { 2, 3, 4, 5, 6, 7, 10, 11, 12, 13}
Minimum value should be 4.
From position 2 -> 2, 3 because
the next sequence starts at 4 so 4 elements cannot be selected from this sequence
From position 4 – 4, 5, 6, 7
From position 10 – 10, 11, 12, 13
Total elements of sequence = 10 { 2, 3, 4, 5, 6, 7, 10, 11, 12, 13}.
朴素方法:解决问题的基本思想是简单地遍历数组。
请按照以下步骤操作:
- 从取K = 1 开始,对每个 K 检查是否有可能获得序列的C个元素。
- 如果可能,则K是答案,否则将K加 1 并继续直到满足条件。
时间复杂度: O(N*C)
辅助空间: O(1)
高效的方法:可以基于以下思路解决问题:
Use Binary Search on K. If for any value of K it is possible to find C elements in the sequence then try for a lower value in the lower half. Otherwise, try for a higher value of K in the higher half.
请按照以下步骤解决问题:
- 通过设置low = 1和high = 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)