📌  相关文章
📜  通过给定操作可以从给定数组的第 K 个索引到达的最远索引

📅  最后修改于: 2021-10-28 02:13:18             🧑  作者: Mango

给定一个由N 个整数和三个整数XYK组成的数组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])

例子:

方法:想法是使用X表示索引之间的最大差异,使用Y表示剩余差异。请按照以下步骤解决此问题:

  • 声明一个优先队列。
  • 遍历给定的数组arr[]并执行以下操作:
    • 如果当前元素 ( arr[i] ) 大于下一个元素 ( arr[i + 1] ),则移动到下一个索引。
    • 否则,将(arr[i + 1] – arr[i])的差值压入优先级队列
    • 如果优先级队列的大小大于Y ,则将X递减优先级队列的顶部元素并弹出该元素。
    • 如果X小于0 ,则可以达到的最远索引是i
  • 完成上述步骤后,如果X的值至少为 0 ,则可以达到的最远索引为(N – 1)

下面是上述方法的实现:

Java
// 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


Javascript


输出:
4

时间复杂度: O(N*log(E)),其中 E 是优先级队列中元素的最大数量。
辅助空间: O(E)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程