给定一个数组arr[]和一个整数K ,即第0个索引,任务是通过执行以下操作来收集可能的最大分数:
- 从数组的第0个索引开始。
- 通过在每次移动中最多跳转K个索引来到达数组的最后一个索引。
- 将每次跳转后到达的每个索引的值相加。
- 初始化一个数组dp[]来存储先前计算的结果。
- 现在,从第0个索引开始,对每个第i个索引执行以下操作:
- 如果当前索引大于或等于最后一个元素的索引,则返回数组的最后一个元素。
- 如果当前索引的值是预先计算的,则返回预先计算的值。
- 否则,计算通过移动到i + 1到i + K范围内的所有步骤可以获得的最大分数,并使用以下递推关系将各个索引的结果存储在dp[]数组中:
dp[i] = max(dp[i + 1], dp[i + 2], dp[i + 3], ….., dp[i + K]) + A[i].
- 现在,打印dp[0]作为所需的答案。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count the maximum
// score of an index
int maxScore(int i, int A[], int K, int N, int dp[])
{
// Base Case
if (i >= N - 1)
return A[N - 1];
// If the value for the current
// index is pre-calculated
if (dp[i] != -1)
return dp[i];
int score = INT_MIN;
// Calculate maximum score
// for all the steps in the
// range from i + 1 to i + k
for (int j = 1; j <= K; j++) {
// Score for index (i + j)
score = max(score, maxScore(i + j, A, K, N, dp));
}
// Update dp[i] and return
// the maximum value
return dp[i] = score + A[i];
}
// Function to get maximum score
// possible from the array A[]
int getScore(int A[], int N, int K)
{
// Array to store memoization
int dp[N];
// Initialize dp[] with -1
for (int i = 0; i < N; i++)
dp[i] = -1;
cout << maxScore(0, A, K, N, dp);
}
// Driver Code
int main()
{
int A[] = { 100, -30, -50, -15, -20, -30 };
int K = 3;
int N = sizeof(A) / sizeof(A[0]);
getScore(A, N, K);
return 0;
}
Java
// JAVA program for the above approach
import java.io.*;
import java.math.*;
import java.util.*;
public class GFG
{
// Function to count the maximum
// score of an index
static int maxScore(int i, int A[], int K, int N,
int dp[])
{
// Base Case
if (i >= N - 1)
return A[N - 1];
// If the value for the current
// index is pre-calculated
if (dp[i] != -1)
return dp[i];
int score = Integer.MIN_VALUE;
// Calculate maximum score
// for all the steps in the
// range from i + 1 to i + k
for (int j = 1; j <= K; j++)
{
// Score for index (i + j)
score = Math.max(score,
maxScore(i + j, A, K, N, dp));
}
// Update dp[i] and return
// the maximum value
return dp[i] = score + A[i];
}
// Function to get maximum score
// possible from the array A[]
static void getScore(int A[], int N, int K)
{
// Array to store memoization
int dp[] = new int[N];
// Initialize dp[] with -1
for (int i = 0; i < N; i++)
dp[i] = -1;
System.out.println(maxScore(0, A, K, N, dp));
}
// Driver Code
public static void main(String args[])
{
int A[] = { 100, -30, -50, -15, -20, -30 };
int K = 3;
int N = A.length;
getScore(A, N, K);
}
}
// This code is contributed by jyoti369
Python3
# Python program for the above approach
import sys
# Function to count the maximum
# score of an index
def maxScore(i, A, K, N, dp):
# Base Case
if (i >= N - 1):
return A[N - 1];
# If the value for the current
# index is pre-calculated
if (dp[i] != -1):
return dp[i];
score = 1-sys.maxsize;
# Calculate maximum score
# for all the steps in the
# range from i + 1 to i + k
for j in range(1, K + 1):
# Score for index (i + j)
score = max(score, maxScore(i + j, A, K, N, dp));
# Update dp[i] and return
# the maximum value
dp[i] = score + A[i];
return dp[i];
# Function to get maximum score
# possible from the array A
def getScore(A, N, K):
# Array to store memoization
dp = [0]*N;
# Initialize dp with -1
for i in range(N):
dp[i] = -1;
print(maxScore(0, A, K, N, dp));
# Driver Code
if __name__ == '__main__':
A = [100, -30, -50, -15, -20, -30];
K = 3;
N = len(A);
getScore(A, N, K);
# This code contributed by shikhasingrajput
C#
// C# program for the above approach
using System;
class GFG
{
// Function to count the maximum
// score of an index
static int maxScore(int i, int []A, int K, int N,
int []dp)
{
// Base Case
if (i >= N - 1)
return A[N - 1];
// If the value for the current
// index is pre-calculated
if (dp[i] != -1)
return dp[i];
int score = int.MinValue;
// Calculate maximum score
// for all the steps in the
// range from i + 1 to i + k
for (int j = 1; j <= K; j++)
{
// Score for index (i + j)
score = Math.Max(score,
maxScore(i + j, A, K, N, dp));
}
// Update dp[i] and return
// the maximum value
return dp[i] = score + A[i];
}
// Function to get maximum score
// possible from the array A[]
static void getScore(int []A, int N, int K)
{
// Array to store memoization
int []dp = new int[N];
// Initialize dp[] with -1
for (int i = 0; i < N; i++)
dp[i] = -1;
Console.WriteLine(maxScore(0, A, K, N, dp));
}
// Driver Code
static public void Main()
{
int []A = { 100, -30, -50, -15, -20, -30 };
int K = 3;
int N = A.Length;
getScore(A, N, K);
}
}
// This code is contributed by jana_sayantan.
Javascript
C++
// C++ program for the above approach
#include
using namespace std;
// Structure to sort a priority queue on
// the basis of first element of the pair
struct mycomp {
bool operator()(pair p1,
pair p2)
{
return p1.first < p2.first;
}
};
// Function to calculate maximum
// score possible from the array A[]
int maxScore(int A[], int K, int N)
{
// Stores the score of previous k indices
priority_queue,
vector >, mycomp>
maxheap;
// Stores the maximum
// score for current index
int maxScore = 0;
// Maximum score at first index
maxheap.push({ A[0], 0 });
// Traverse the array to calculate
// maximum score for all indices
for (int i = 1; i < N; i++) {
// Remove maximum scores for
// indices less than i - K
while (maxheap.top().second < (i - K)) {
maxheap.pop();
}
// Calculate maximum score for current index
maxScore = A[i] + maxheap.top().first;
// Push maximum score of
// current index along
// with index in maxheap
maxheap.push({ maxScore, i });
}
// Return the maximum score
return maxScore;
}
// Driver Code
int main()
{
int A[] = { -44, -17, -54, 79 };
int K = 2;
int N = sizeof(A) / sizeof(A[0]);
// Function call to calculate
// maximum score from the array A[]
cout << maxScore(A, K, N);
return 0;
}
输出
55
时间复杂度: O(N * K)
辅助空间: O(N * K)
有效方法:按照以下步骤解决问题
- 初始化一个最大堆来存储前K 个索引的结果。
- 现在,遍历数组A[]以计算所有索引的最大分数。
- 对于第0个索引,分数将是第0个索引处的值。
- 现在,对于[1, N – 1]范围内的每个第i个索引。
- 首先,从最大堆中删除小于i – K 的索引的最大分数。
- 现在计算第i个索引的最大分数。
最大分数 = A[i] +最大堆顶部的分数。 - 现在将最大分数及其索引插入到最大堆中。
- 返回获得的最大分数。
C++
// C++ program for the above approach
#include
using namespace std;
// Structure to sort a priority queue on
// the basis of first element of the pair
struct mycomp {
bool operator()(pair p1,
pair p2)
{
return p1.first < p2.first;
}
};
// Function to calculate maximum
// score possible from the array A[]
int maxScore(int A[], int K, int N)
{
// Stores the score of previous k indices
priority_queue,
vector >, mycomp>
maxheap;
// Stores the maximum
// score for current index
int maxScore = 0;
// Maximum score at first index
maxheap.push({ A[0], 0 });
// Traverse the array to calculate
// maximum score for all indices
for (int i = 1; i < N; i++) {
// Remove maximum scores for
// indices less than i - K
while (maxheap.top().second < (i - K)) {
maxheap.pop();
}
// Calculate maximum score for current index
maxScore = A[i] + maxheap.top().first;
// Push maximum score of
// current index along
// with index in maxheap
maxheap.push({ maxScore, i });
}
// Return the maximum score
return maxScore;
}
// Driver Code
int main()
{
int A[] = { -44, -17, -54, 79 };
int K = 2;
int N = sizeof(A) / sizeof(A[0]);
// Function call to calculate
// maximum score from the array A[]
cout << maxScore(A, K, N);
return 0;
}
输出
18
时间复杂度: O(N * log K)
辅助空间: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。