📌  相关文章
📜  使值至少为 K 的数组元素总和至少为 X 的最短天数

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

使值至少为 K 的数组元素总和至少为 X 的最短天数

给定两个整数XK和两个数组arr[]R[]都由N个正整数组成,其中R[i]表示arr[i]在一天内增加的数量,任务是找到最小的数天数大于或等于K的数组元素的总和至少变为X

例子:

天真的方法:解决给定问题的最简单方法是不断增加天数,并且每当具有至少 K值的数组元素的总和变得大于或等于X时。增加D天后,打印当前获得的天数的值。

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

高效方法:上述方法也可以通过使用二分搜索进行优化。请按照以下步骤解决问题:

  • 初始化两个变量,比如low0highX
  • 初始化一个变量,比如minDays存储最小天数。
  • 迭代直到low的值最多为high并执行以下步骤:
    • 将变量mid初始化为low + (high – low)/2和变量,比如sum0 ,以存储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)