📌  相关文章
📜  通过避免给定索引 B – 集 3(二分搜索),指针可以在 N 步中达到的最大索引

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

通过避免给定索引 B – 集 3(二分搜索),指针可以在 N 步中达到的最大索引

给定两个整数NB ,任务是打印一个指针的最大索引,从第0开始,自然数数组(即 0、1、2、3、4、5...)中的索引可以达到,比如arr [] ,在N步中,在任何点都没有将自己置于索引B处。

例子:

高效方法:本文中讨论的简单方法也可以通过使用二分搜索进行优化。如果maximumIndex – B的值存在于前N个自然数的任何最后K个数字的总和中,则在没有索引B跳跃的情况下, maximumIndex不能减少到小于等于0 。因此,将最大 Index 递减并再次执行上述步骤。请按照以下步骤解决问题:

  • 将变量maximumIndexReached初始化为0
  • 初始化向量Ans[]
  • 使用变量i遍历范围[1, N]并将值i添加到变量maximumIndexReached并将值i推入向量Ans[]
  • 反转向量Ans[]
  • 使用变量i遍历范围[1, Ans.size())并将值Ans[i – 1]添加到Ans[i]
  • 遍历一个while循环直到maximumIndexReached大于0并执行以下任务:
    • 将一个自动变量初始化数组Ans[]中值大于或等于maximumIndexReached – B的指针。
    • 如果等于Ans.end()则跳出循环。
    • 如果*it等于maximumIndexReached – B然后将maximumIndexReached的值减少1 。否则,跳出循环。
  • 执行上述步骤后,打印maximumIndexReached的值作为答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the maximum index
// reachable
int maxIndex(int N, int B)
{
 
    // Store the answer
    int maximumIndexReached = 0;
    vector Ans;
 
    // Store the maximum index possible
    // i.e, N*(N-1)
    for (int i = 1; i <= N; i++) {
        maximumIndexReached += i;
        Ans.push_back(i);
    }
 
    reverse(Ans.begin(), Ans.end());
 
    // Add bthe previous elements
    for (int i = 1; i < (int)Ans.size(); i++) {
        Ans[i] += Ans[i - 1];
    }
 
    // Run a loop
    while (maximumIndexReached) {
 
        // Binary Search
        auto it
            = lower_bound(Ans.begin(), Ans.end(),
                          maximumIndexReached - B);
        if (it == Ans.end()) {
            break;
        }
        if (*it == maximumIndexReached - B) {
            maximumIndexReached--;
        }
        else {
            break;
        }
    }
 
    return maximumIndexReached;
}
 
// Driver Code
int main()
{
    int N = 3, B = 2;
    cout << maxIndex(N, B);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
  // Function to find the maximum index
  // reachable
  static int maxIndex(int N, int B)
  {
 
    // Store the answer
    int maximumIndexReached = 0;
    Vector Ans = new Vector<>();
 
    // Store the maximum index possible
    // i.e, N*(N-1)
    for (int i = 1; i <= N; i++) {
      maximumIndexReached += i;
      Ans.add(i+i-1);
    }
 
 
 
    // Run a loop
    while (maximumIndexReached>0) {
 
      // Binary Search
      int it
        = lower_bound(Ans, maximumIndexReached - B);
      if (it == -1) {
        break;
      }
      if (it == maximumIndexReached - B) {
        maximumIndexReached--;
      }
      else {
        break;
      }
    }
 
    return maximumIndexReached;
  }
  public static int lower_bound(Vector s, int val)
  {
    List temp = new LinkedList();
    temp.addAll(s);
    Collections.sort(temp);
    Collections.reverse(temp);
 
 
    if (temp.indexOf(val) + 1 == temp.size())
      return -1;
    else if(temp.get(temp.indexOf(val) +1)>val)
      return -1;
    else
      return temp.get(temp.indexOf(val) +1);
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int N = 3, B = 2;
    System.out.print(maxIndex(N, B));
 
  }
}
 
// This code is contributed by Rajput-Ji


Python3
# Python 3 program for the above approach
from bisect import bisect_left
 
# Function to find the maximum index
# reachable
def maxIndex(N, B):
 
    # Store the answer
    maximumIndexReached = 0
    Ans = []
 
    # Store the maximum index possible
    # i.e, N*(N-1)
    for i in range(1, N + 1):
        maximumIndexReached += i
        Ans.append(i)
 
    Ans.reverse()
 
    # Add bthe previous elements
    for i in range(len(Ans)):
        Ans[i] += Ans[i - 1]
 
    # Run a loop
    while (maximumIndexReached):
 
        # Binary Search
        it = bisect_left(Ans,
                         maximumIndexReached - B)
        if (it not in Ans):
            break
 
        if (it == maximumIndexReached - B):
            maximumIndexReached -= 1
 
        else:
            break
 
    return maximumIndexReached
 
# Driver Code
if __name__ == "__main__":
 
    N = 3
    B = 2
    print(maxIndex(N, B))
 
    # This code is contributed by ukasp.


Javascript



输出:
6

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