使值至少为 K 的数组元素总和至少为 X 的最短天数
给定两个整数X , K和两个数组arr[]和R[]都由N个正整数组成,其中R[i]表示arr[i]在一天内增加的数量,任务是找到最小的数天数大于或等于K的数组元素的总和至少变为X 。
例子:
Input: X = 100, K = 45, arr[] = {2, 5, 2, 6}, R[] = {10, 13, 15, 12}
Output: 4
Explanation:
Consider the following values of array after each day:
- Day 1: After the day 1, all array element modifies to {12, 18, 17, 18}. The sum of elements having values >= K(= 45) is 0.
- Day 2: After the day 2, all array element modifies to {22, 31, 32, 30}. The sum of elements having values >= K(= 45) is 0.
- Day 3: After the day 3, all array element modifies to {32, 44, 47, 42}. The sum of elements having values >= K(= 45) is 47.
- Day 4: After the day 4, all array element modifies to {42, 57, 62, 54}. The sum of elements having values >= K(= 45) is 57 + 62 + 54 = 167, which is at least X(= 100).
Therefore, the minimum number of days required is 4.
Input: X = 65, K = 10, arr[] = {1, 1, 1, 1, 3}, R[] = {2, 1, 2, 2, 1}
Output: 9
天真的方法:解决给定问题的最简单方法是不断增加天数,并且每当具有至少 K值的数组元素的总和变得大于或等于X时。增加D天后,打印当前获得的天数的值。
时间复杂度: O(N*X)
辅助空间: O(1)
高效方法:上述方法也可以通过使用二分搜索进行优化。请按照以下步骤解决问题:
- 初始化两个变量,比如low为0 , high为X 。
- 初始化一个变量,比如minDays存储最小天数。
- 迭代直到low的值最多为high并执行以下步骤:
- 将变量mid初始化为low + (high – low)/2和变量,比如sum为0 ,以存储mid天数后数组元素的总和。
- 使用变量i遍历数组arr[]并执行以下步骤:
- 将变量temp初始化为(arr[i] + R[i]*mid) 。
- 如果temp的值不小于K ,则将temp的值添加到sum 。
- 如果sum的值至少为 X ,则将minDays的值更新为mid ,将high的值更新为(mid – 1) 。
- 否则,将low的值更新为(mid + 1) 。
- 完成上述步骤后,打印minDays的值作为得到的最小天数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the minimum number
// of days such that the sum of array
// elements >= K is at least X
void findMinDays(int arr[], int R[],
int N, int X, int K)
{
// Initialize the boundaries of
// search space
int low = 0, high = X;
int minDays;
// Perform the binary search
while (low <= high) {
// Find the value of mid
int mid = (low + high) / 2;
int sum = 0;
// Traverse the array, arr[]
for (int i = 0; i < N; i++) {
// Find the value of arr[i]
// after mid number of days
int temp = arr[i] + R[i] * mid;
// Check if temp is not
// less than K
if (temp >= K) {
// Update the value
// of sum
sum += temp;
}
}
// Check if the value of sum
// is greater than X
if (sum >= X) {
// Update value of high
minDays = mid;
high = mid - 1;
}
// Update the value of low
else {
low = mid + 1;
}
}
// Print the minimum number
// of days
cout << minDays;
}
// Driver Code
int main()
{
int X = 100, K = 45;
int arr[] = { 2, 5, 2, 6 };
int R[] = { 10, 13, 15, 12 };
int N = sizeof(arr) / sizeof(arr[0]);
findMinDays(arr, R, N, X, K);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG{
// Function to find the minimum number
// of days such that the sum of array
// elements >= K is at least X
static void findMinDays(int arr[], int R[], int N,
int X, int K)
{
// Initialize the boundaries of
// search space
int low = 0, high = X;
int minDays = -1;
// Perform the binary search
while (low <= high)
{
// Find the value of mid
int mid = (low + high) / 2;
int sum = 0;
// Traverse the array, arr[]
for(int i = 0; i < N; i++)
{
// Find the value of arr[i]
// after mid number of days
int temp = arr[i] + R[i] * mid;
// Check if temp is not
// less than K
if (temp >= K)
{
// Update the value
// of sum
sum += temp;
}
}
// Check if the value of sum
// is greater than X
if (sum >= X)
{
// Update value of high
minDays = mid;
high = mid - 1;
}
// Update the value of low
else
{
low = mid + 1;
}
}
// Print the minimum number
// of days
System.out.println(minDays);
}
// Driver Code
public static void main(String[] args)
{
int X = 100, K = 45;
int arr[] = { 2, 5, 2, 6 };
int R[] = { 10, 13, 15, 12 };
int N = arr.length;
findMinDays(arr, R, N, X, K);
}
}
// This code is contributed by Potta Lokesh
C#
// C# program for the above approach
using System;
class GFG {
// Function to find the minimum number
// of days such that the sum of array
// elements >= K is at least X
static void findMinDays(int[] arr, int[] R, int N,
int X, int K)
{
// Initialize the boundaries of
// search space
int low = 0, high = X;
int minDays = -1;
// Perform the binary search
while (low <= high) {
// Find the value of mid
int mid = (low + high) / 2;
int sum = 0;
// Traverse the array, arr[]
for (int i = 0; i < N; i++) {
// Find the value of arr[i]
// after mid number of days
int temp = arr[i] + R[i] * mid;
// Check if temp is not
// less than K
if (temp >= K) {
// Update the value
// of sum
sum += temp;
}
}
// Check if the value of sum
// is greater than X
if (sum >= X) {
// Update value of high
minDays = mid;
high = mid - 1;
}
// Update the value of low
else {
low = mid + 1;
}
}
// Print the minimum number
// of days
Console.Write(minDays);
}
// Driver Code
public static void Main(string[] args)
{
int X = 100, K = 45;
int[] arr = { 2, 5, 2, 6 };
int[] R = { 10, 13, 15, 12 };
int N = arr.Length;
findMinDays(arr, R, N, X, K);
}
}
// This code is contributed by ukasp.
Javascript
Python3
# Python 3 program for the above approach
# Function to find the minimum number
# of days such that the sum of array
# elements >= K is at least X
def findMinDays(arr, R, N, X, K):
# Initialize the boundaries of
# search space
low = 0
high = X
minDays = 0
# Perform the binary search
while (low <= high):
# Find the value of mid
mid = (low + high) // 2
sum = 0
# Traverse the array, arr[]
for i in range(N):
# Find the value of arr[i]
# after mid number of days
temp = arr[i] + R[i] * mid
# Check if temp is not
# less than K
if (temp >= K):
# Update the value
# of sum
sum += temp
# Check if the value of sum
# is greater than X
if (sum >= X):
# Update value of high
minDays = mid
high = mid - 1
# Update the value of low
else:
low = mid + 1
# Print the minimum number
# of days
print(minDays)
# Driver Code
if __name__ == '__main__':
X = 100
K = 45
arr = [2, 5, 2, 6]
R = [10, 13, 15, 12]
N = len(arr)
findMinDays(arr, R, N, X, K)
# This code is contributed by SURENDRA_GANGWAR.
输出:
4
时间复杂度: O(N*log X)
辅助空间: O(1)