给定的阵列ARR []由N个整数的和阵列P []包括M的整数,使得P [i]表示第由天第i工作所获得的分数。任务是为每个数组元素arr[i]找到至少达到arr[i]分数所需的最少工作天数。
例子:
Input: arr[] = {400, 200, 700}, P[] = {100, 300, 400, 500, 600}
Output: 2 2 3 4 5
Explanation:
Following are the number of days required for each array elements:
- arr[0](= 400): To earn 400 points one has to work for first 2 days making the total points equal to 100 + 300 = 400(>= arr[0]).
- arr[1](= 200): To earn 200 points one has to work for first 2 days making the total points = 100 + 300 = 400(>= arr[1]).
- arr[2](= 700): To earn 700 points one has to work for first 3 days making the total points = 100 + 300 + 400 = 800(>= arr[2]).
Input: arr[] = {1400}, P[] = {100, 300}
Output: -1
朴素的方法:解决问题的最简单的方法是遍历数组arr[]并且对于每个数组,元素找到数组P[]的最小索引,使得[0, i]范围内的子数组之和为至少 arr[i] 。
时间复杂度: O(N 2 )
辅助空间: O(1)
高效的方法:可以通过找到P[]的前缀和数组,然后使用二分搜索找到其值至少为 arr[i]的和来优化上述方法。请按照以下步骤解决问题:
- 找到数组P[]的前缀和数组。
- 遍历给定的数组arr[]并执行以下步骤:
- 在数组P[] 中找到大于当前元素arr[i]的第一个元素的索引,并将其存储在一个变量中,比如index 。
- 如果索引的值为-1 ,则打印-1 。否则,打印(index + 1) 的值作为当前索引的结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the lower bound
// of N using binary search
int binarySeach(vector P, int N)
{
// Stores the lower bound
int i = 0;
// Stores the upper bound
int j = P.size() - 1;
// Stores the minimum index
// having value is at least N
int index = -1;
// Iterater while i<=j
while (i <= j)
{
// Stores the mid index
// of the range [i, j]
int mid = i + (j - i) / 2;
// If P[mid] is at least N
if (P[mid] >= N)
{
// Update the value of
// mid to index
index = mid;
// Update the value of j
j = mid - 1;
}
// Update the value of i
else
{
i = mid + 1;
}
}
// Return the resultant index
return index;
}
// Function to find the minimum number
// of days required to work to at least
// arr[i] points for every array element
void minDays(vector P, vector arr)
{
// Traverse the array P[]
for(int i = 1; i < P.size(); i++)
{
// Find the prefix sum
P[i] += P[i] + P[i - 1];
}
// Traverse the array arr[]
for(int i = 0; i < arr.size(); i++)
{
// Find the minimum index of
// the array having value
// at least arr[i]
int index = binarySeach(P, arr[i]);
// If the index is not -1
if (index != -1)
{
cout << index + 1 << " ";
}
// Otherwise
else
{
cout << -1 << " ";
}
}
}
// Driver Code
int main()
{
vector arr = { 400, 200, 700, 900, 1400 };
vector P = { 100, 300, 400, 500, 600 };
minDays(P, arr);
return 0;
}
// This code is contributed by nirajgusain5
Java
// Java program for the above approach
public class GFG {
// Function to find the minimum number
// of days required to work to at least
// arr[i] points for every array element
static void minDays(int[] P, int[] arr)
{
// Traverse the array P[]
for (int i = 1; i < P.length; i++) {
// Find the prefix sum
P[i] += P[i] + P[i - 1];
}
// Traverse the array arr[]
for (int i = 0;
i < arr.length; i++) {
// Find the minimum index of
// the array having value
// at least arr[i]
int index = binarySeach(
P, arr[i]);
// If the index is not -1
if (index != -1) {
System.out.print(
index + 1 + " ");
}
// Otherwise
else {
System.out.print(
-1 + " ");
}
}
}
// Function to find the lower bound
// of N using binary search
static int binarySeach(
int[] P, int N)
{
// Stores the lower bound
int i = 0;
// Stores the upper bound
int j = P.length - 1;
// Stores the minimum index
// having value is at least N
int index = -1;
// Iterater while i<=j
while (i <= j) {
// Stores the mid index
// of the range [i, j]
int mid = i + (j - i) / 2;
// If P[mid] is at least N
if (P[mid] >= N) {
// Update the value of
// mid to index
index = mid;
// Update the value of j
j = mid - 1;
}
// Update the value of i
else {
i = mid + 1;
}
}
// Return the resultant index
return index;
}
// Driver Code
public static void main(String[] args)
{
int[] arr = { 400, 200, 700, 900, 1400 };
int[] P = { 100, 300, 400, 500, 600 };
minDays(P, arr);
}
}
Python3
# Python3 program for the above approach
# Function to find the minimum number
# of days required to work to at least
# arr[i] points for every array element
def minDays(P, arr):
# Traverse the array P[]
for i in range(1, len(P)):
# Find the prefix sum
P[i] += P[i] + P[i - 1]
# Traverse the array arr[]
for i in range(len(arr)):
# Find the minimum index of
# the array having value
# at least arr[i]
index = binarySeach(P, arr[i])
# If the index is not -1
if (index != -1):
print(index + 1, end = " ")
# Otherwise
else:
print(-1, end = " ")
# Function to find the lower bound
# of N using binary search
def binarySeach(P, N):
# Stores the lower bound
i = 0
# Stores the upper bound
j = len(P) - 1
# Stores the minimum index
# having value is at least N
index = -1
# Iterater while i<=j
while (i <= j):
# Stores the mid index
# of the range [i, j]
mid = i + (j - i) // 2
# If P[mid] is at least N
if (P[mid] >= N):
# Update the value of
# mid to index
index = mid
# Update the value of j
j = mid - 1
# Update the value of i
else:
i = mid + 1
# Return the resultant index
return index
# Driver Code
if __name__ == '__main__':
arr = [400, 200, 700,900,1400 ]
P = [100, 300, 400, 500, 600 ]
minDays(P, arr)
# This code is contributed by mohit kumar 29.
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the minimum number
// of days required to work to at least
// arr[i] points for every array element
static void minDays(int[] P, int[] arr)
{
// Traverse the array P[]
for(int i = 1; i < P.Length; i++)
{
// Find the prefix sum
P[i] += P[i] + P[i - 1];
}
// Traverse the array arr[]
for(int i = 0; i < arr.Length; i++)
{
// Find the minimum index of
// the array having value
// at least arr[i]
int index = binarySeach(P, arr[i]);
// If the index is not -1
if (index != -1)
{
Console.Write(index + 1 + " ");
}
// Otherwise
else
{
Console.Write(-1 + " ");
}
}
}
// Function to find the lower bound
// of N using binary search
static int binarySeach(int[] P, int N)
{
// Stores the lower bound
int i = 0;
// Stores the upper bound
int j = P.Length - 1;
// Stores the minimum index
// having value is at least N
int index = -1;
// Iterater while i<=j
while (i <= j)
{
// Stores the mid index
// of the range [i, j]
int mid = i + (j - i) / 2;
// If P[mid] is at least N
if (P[mid] >= N)
{
// Update the value of
// mid to index
index = mid;
// Update the value of j
j = mid - 1;
}
// Update the value of i
else
{
i = mid + 1;
}
}
// Return the resultant index
return index;
}
// Driver Code
public static void Main(string[] args)
{
int[] arr = { 400, 200, 700, 900, 1400 };
int[] P = { 100, 300, 400, 500, 600 };
minDays(P, arr);
}
}
// This code is contributed by ukasp
Javascript
输出:
2 2 2 3 3
时间复杂度: O(N*log N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live