📜  从给定数组中找到得分最低的索引

📅  最后修改于: 2021-09-06 06:22:18             🧑  作者: Mango

给定大小为N (1 ≤ N ≤ 10 5 )的数组A[] ,由正整数组成,其中[0, N – 1]范围内的索引i的分数定义为:

任务是找到得分最低的索引。

例子:

处理方法:按照以下步骤解决问题

  1. 反向遍历数组,即从i = N – 10迭代。
  2. 对于每个i ,检查(A[i] + i)是否小于N。
  3. 将索引i 的分数 [i]更新为 S core[i] = A[i] * ((i + A[i] < N) ? Score(i + A[i]) ? 1)
  4. 打印分数最低的索引。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
 
using namespace std;
 
 
//Function to find the index
//with minimum score
void Min_Score_Index(int N, vector A){
 
    //Stores the score of current index
    vector Score(N,0);
 
    //Traverse the array in reverse
    for (int i=N-1;i>=0;i--){
 
        if (A[i] + i < N)
            Score[i] = A[i] * Score[A[i] + i];
        else
            Score[i] = A[i];
    }
 
    //Update minimum score
    int min_value = INT_MAX;
 
    for(int i:Score) min_value = min(i,min_value);
 
    //Print the index with minimum score
    int ind = 0;
 
    for(int i=0;i A ={1, 2, 3, 4, 5};
 
  Min_Score_Index(N, A);
 
}


Java
// Java program for the above approach
import java.util.*;
class GFG
{
 
// Function to find the index
// with minimum score
static void Min_Score_Index(int N, int []A)
{
 
    // Stores the score of current index
    int []Score = new int[N];
 
    // Traverse the array in reverse
    for (int i = N - 1; i >= 0; i--)
    {
 
        if (A[i] + i < N)
            Score[i] = A[i] * Score[A[i] + i];
        else
            Score[i] = A[i];
    }
 
    // Update minimum score
    int min_value = Integer.MAX_VALUE;
    for(int i:Score) min_value = Math.min(i, min_value);
 
    // Print the index with minimum score
    int ind = 0;
    for(int i = 0; i < N; i++)
    {
      if(Score[i] == min_value)
        ind = i;
    }
    System.out.print(ind);
}
 
// Driver Code
public static void main(String[] args)
{
  int N = 5;
  int []A ={1, 2, 3, 4, 5};
  Min_Score_Index(N, A);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program for the above approach
 
# Function to find the index
# with minimum score
def Min_Score_Index(N, A):
 
    # Stores the score of current index
    Score = [0] * N
 
    # Traverse the array in reverse
    for i in range(N - 1, -1, -1):
        if A[i] + i < N:
            Score[i] = A[i] * Score[A[i] + i]
        else:
            Score[i] = A[i]
 
    # Update minimum score
    min_value = min(Score)
 
    # Print the index with minimum score
    ind = Score.index(min_value)
    print(ind)
 
# Driver Code
if __name__ == "__main__":
    N = 5
    A = [1, 2, 3, 4, 5]
    Min_Score_Index(N, A)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the index
// with minimum score
static void Min_Score_Index(int N, int[] A)
{
     
    // Stores the score of current index
    int[] Score = new int[N];
 
    // Traverse the array in reverse
    for(int i = N - 1; i >= 0; i--)
    {
        if (A[i] + i < N)
            Score[i] = A[i] * Score[A[i] + i];
        else
            Score[i] = A[i];
    }
 
    // Update minimum score
    int min_value = Int32.MaxValue;
    foreach(int i in Score)
    {
        min_value = Math.Min(i, min_value);
    }
 
    // Print the index with minimum score
    int ind = 0;
    for(int i = 0; i < N; i++)
    {
        if (Score[i] == min_value)
            ind = i;
    }
    Console.WriteLine(ind);
}
 
// Driver Code
public static void Main()
{
    int N = 5;
    int[] A = { 1, 2, 3, 4, 5 };
     
    Min_Score_Index(N, A);
}
}
 
// This code is contributed by chitranayal


输出:
2

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live