给定由N个整数和三个整数X , Y和K组成的数组arr [] ,任务是找到可以通过以下操作达到的最远索引:
- 如果arr [i]≥arr [i + 1]:从索引i移至i + 1 。
- 如果ARR [I]
由1要么减小量X如果X的值> 0或递减Y(ARR第[i + 1] – ARR [I]),如果Y的值>(改编[i + 1] – arr [i]) 。
例子:
Input: arr[] = {4, 2, 7, 6, 9, 14, 12}, X = 1, Y = 5, K = 0
Output: 4
Explanation:
Initially, K = 0.
arr[0] > arr[1]: Therefore, move to index 1.
arr[1] < arr[2]: Decrement X by 1 and move to index 2. Now X = 0.
arr[2] > arr[3]: Move to index 3.
arr[3] < arr[4]: Decrement Y by 3 and move to index 4. Now Y = 2
arr[4] < arr[5]: Neither X > 0 nor Y > 5. Hence, it is not possible to move to the next index.
Therefore, the maximum index that can be reached is 4.
Input: arr[] = {14, 3, 19, 3}, X = 17, Y = 0, K = 1
Output: 3
方法:想法是使用X表示索引之间的最大差异,使用Y表示其余差异。请按照以下步骤解决此问题:
- 声明优先级队列。
- 遍历给定的数组arr []并执行以下操作:
- 如果当前元素( arr [i] )大于下一个元素( arr [i + 1] ),则移至下一个索引。
- 否则,将(arr [i + 1] – arr [i])之差推入优先级队列。
- 如果优先级队列的大小大于Y ,则将X减少优先级队列的顶部元素并弹出该元素。
- 如果X小于0 ,则可以达到的最远索引是i 。
- 完成上述步骤后,如果X的值至少为0 ,则可以达到的最远索引为(N – 1) 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the farthest index
// that can be reached
void farthestHill(int arr[], int X,
int Y, int N, int K)
{
int i, diff;
// Declare a priority queue
priority_queue pq;
// Iterate the array
for (i = K; i < N - 1; i++) {
// If current element is
// greater than the next element
if (arr[i] >= arr[i + 1])
continue;
// Otherwise, store their difference
diff = arr[i + 1] - arr[i];
// Push diff into pq
pq.push(diff);
// If size of pq exceeds Y
if (pq.size() > Y) {
// Decrease X by the
// top element of pq
X -= pq.top();
// Remove top of pq
pq.pop();
}
// If X is exhausted
if (X < 0) {
// Current index is the
// farthest possible
cout << i;
return;
}
}
// Print N-1 as farthest index
cout << N - 1;
}
// Driver Code
int main()
{
int arr[] = { 4, 2, 7, 6, 9, 14, 12 };
int X = 5, Y = 1;
int K = 0;
int N = sizeof(arr) / sizeof(arr[0]);
// Function Call
farthestHill(arr, X, Y, N, K);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find the farthest index
// that can be reached
public static void farthestHill(int arr[], int X,
int Y, int N, int K)
{
int i, diff;
// Declare a priority queue
PriorityQueue pq = new PriorityQueue();
// Iterate the array
for(i = K; i < N - 1; i++)
{
// If current element is
// greater than the next element
if (arr[i] >= arr[i + 1])
continue;
// Otherwise, store their difference
diff = arr[i + 1] - arr[i];
// Push diff into pq
pq.add(diff);
// If size of pq exceeds Y
if (pq.size() > Y)
{
// Decrease X by the
// top element of pq
X -= pq.peek();
// Remove top of pq
pq.poll();
}
// If X is exhausted
if (X < 0)
{
// Current index is the
// farthest possible
System.out.print(i);
return;
}
}
// Print N-1 as farthest index
System.out.print(N - 1);
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 4, 2, 7, 6, 9, 14, 12 };
int X = 5, Y = 1;
int K = 0;
int N = arr.length;
// Function Call
farthestHill(arr, X, Y, N, K);
}
}
// This code is contributed by divyeshrabadiya07
Python3
# Python3 program for the above approach
# Function to find the farthest index
# that can be reached
def farthestHill(arr, X, Y, N, K):
# Declare a priority queue
pq = []
# Iterate the array
for i in range(K, N - 1, 1):
# If current element is
# greater than the next element
if (arr[i] >= arr[i + 1]):
continue
# Otherwise, store their difference
diff = arr[i + 1] - arr[i]
# Push diff into pq
pq.append(diff)
# If size of pq exceeds Y
if (len(pq) > Y):
# Decrease X by the
# top element of pq
X -= pq[-1]
# Remove top of pq
pq[-1]
# If X is exhausted
if (X < 0):
# Current index is the
# farthest possible
print(i)
return
# Print N-1 as farthest index
print(N - 1)
# Driver Code
arr = [ 4, 2, 7, 6, 9, 14, 12 ]
X = 5
Y = 1
K = 0
N = len(arr)
# Function Call
farthestHill(arr, X, Y, N, K)
# This code is contributed by code_hunt
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find the farthest index
// that can be reached
public static void farthestHill(int[] arr, int X,
int Y, int N, int K)
{
int i, diff;
// Declare a priority queue
List pq = new List();
// Iterate the array
for(i = K; i < N - 1; i++)
{
// If current element is
// greater than the next element
if (arr[i] >= arr[i + 1])
continue;
// Otherwise, store their difference
diff = arr[i + 1] - arr[i];
// Push diff into pq
pq.Add(diff);
pq.Sort();
pq.Reverse();
// If size of pq exceeds Y
if (pq.Count > Y)
{
// Decrease X by the
// top element of pq
X -= pq[0];
// Remove top of pq
pq.RemoveAt(0);
}
// If X is exhausted
if (X < 0)
{
// Current index is the
// farthest possible
Console.Write(i);
return;
}
}
// Print N-1 as farthest index
Console.Write(N - 1);
}
// Driver code
public static void Main(String[] args)
{
int[] arr = { 4, 2, 7, 6, 9, 14, 12 };
int X = 5, Y = 1;
int K = 0;
int N = arr.Length;
// Function Call
farthestHill(arr, X, Y, N, K);
}
}
// This code is contributed by gauravrajput1
4
时间复杂度: O(N * log(E)),其中E是优先级队列中元素的最大数量。
辅助空间: O(E)