给定一个数组 arr[]和一个整数 K ,任务是通过只取角元素来找到最大化数组中 K 个元素的总和。
A corner element is an element from the start of the array or from the end of the array.
例子:
Input: arr[] = {8, 4, 4, 8, 12, 3, 2, 9}, K = 3
Output: 21
Explanation:
The optimal strategy is to pick the elements form the array is, two indexes from the beginning and one index from the end. All other possible choice will yield lesser sum. Hence, arr[0] + arr[1] + arr[7] = 21.
Input: arr[] = {2, 1, 14, 6, 4, 3}, K = 3
Output: 17
Explanation:
We will get the maximum sum by picking first three elements form the array. Hence, Optimal choice is: arr[0] + arr[1] + arr[2] = 17
天真的方法:这个想法是使用递归。由于我们只能取一个开始或结束索引值,因此初始化两个变量并准确执行 K 步并返回所有可能组合中的最大和。由于其重叠子问题和最优子结构属性,递归方法具有指数复杂性。
下面是上述方法的实现:
C++
// C++ program to maximize the sum of K elements
// in the array by taking only corner elements
#include
using namespace std;
// Function to return maximum sum
int maxSum(int arr[], int K,
int start, int end,
int max_sum)
{
// Base case
if (K == 0)
return max_sum;
// Pick the start index
int max_sum_start = max_sum
+ arr[start];
// Pick the end index
int max_sum_end = max_sum + arr[end];
// Recursive function call
int ans = max(
maxSum(arr, K - 1, start + 1,
end, max_sum_start),
maxSum(arr, K - 1, start,
end - 1, max_sum_end));
// Return the final answer
return ans;
}
// Function to find the maximized sum
void maximizeSum(int arr[], int K, int n)
{
int max_sum = 0;
int start = 0;
int end = n - 1;
cout << maxSum(arr, K, start,
end, max_sum);
}
// Driver code
int main()
{
int arr[] = { 8, 4, 4, 8, 12, 3, 2, 9 };
int K = 3;
int n = sizeof(arr) / sizeof(arr[0]);
maximizeSum(arr, K, n);
return 0;
}
Java
// Java program to maximize the sum of K elements
// in the array by taking only corner elements
import java.util.*;
class GFG{
// Function to return maximum sum
static int maxSum(int arr[], int K,
int start, int end,
int max_sum)
{
// Base case
if (K == 0)
return max_sum;
// Pick the start index
int max_sum_start = max_sum + arr[start];
// Pick the end index
int max_sum_end = max_sum + arr[end];
// Recursive function call
int ans = Math.max(maxSum(arr, K - 1, start + 1,
end, max_sum_start),
maxSum(arr, K - 1, start,
end - 1, max_sum_end));
// Return the final answer
return ans;
}
// Function to find the maximized sum
static void maximizeSum(int arr[], int K, int n)
{
int max_sum = 0;
int start = 0;
int end = n - 1;
System.out.print(maxSum(arr, K, start,
end, max_sum));
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 8, 4, 4, 8, 12, 3, 2, 9 };
int K = 3;
int n = arr.length;
maximizeSum(arr, K, n);
}
}
// This code is contributed by gauravrajput1
Python3
# Python3 program to maximize the sum of K elements
# in the array by taking only corner elements
# Function to return maximum sum
def maxSum(arr, K, start, end, max_sum):
# Base case
if (K == 0):
return max_sum
# Pick the start index
max_sum_start = max_sum + arr[start]
# Pick the end index
max_sum_end = max_sum + arr[end]
# Recursive function call
ans = max(maxSum(arr, K - 1, start + 1,
end, max_sum_start),
maxSum(arr, K - 1, start,
end - 1, max_sum_end))
# Return the final answer
return ans
# Function to find the maximized sum
def maximizeSum(arr, K, n):
max_sum = 0
start = 0
end = n - 1
print(maxSum(arr, K, start, end, max_sum))
# Driver code
if __name__ == '__main__':
arr = [8, 4, 4, 8, 12, 3, 2, 9]
K = 3
n = len(arr)
maximizeSum(arr, K, n)
# This code is contributed by Bhupendra_Singh
C#
// C# program to maximize the sum of K elements
// in the array by taking only corner elements
using System;
class GFG{
// Function to return maximum sum
static int maxSum(int []arr, int K,
int start, int end,
int max_sum)
{
// Base case
if (K == 0)
return max_sum;
// Pick the start index
int max_sum_start = max_sum + arr[start];
// Pick the end index
int max_sum_end = max_sum + arr[end];
// Recursive function call
int ans = Math.Max(maxSum(arr, K - 1, start + 1,
end, max_sum_start),
maxSum(arr, K - 1, start,
end - 1, max_sum_end));
// Return the readonly answer
return ans;
}
// Function to find the maximized sum
static void maximizeSum(int []arr, int K, int n)
{
int max_sum = 0;
int start = 0;
int end = n - 1;
Console.Write(maxSum(arr, K, start,
end, max_sum));
}
// Driver code
public static void Main(String[] args)
{
int []arr = { 8, 4, 4, 8, 12, 3, 2, 9 };
int K = 3;
int n = arr.Length;
maximizeSum(arr, K, n);
}
}
// This code is contributed by sapnasingh4991
Javascript
C++
// C++ program to maximize the sum of K elements
// in the array by taking only corner elements
#include
using namespace std;
// Function to return maximum sum
int maxPointCount(int arr[], int K, int size)
{
// Initialse variables
int curr_points = 0;
int max_points = 0;
// Iterate over first K elements of array
// and update the value for curr_points
for (int i = 0; i < K; i++)
curr_points += arr[i];
// Update value for max_points
max_points = curr_points;
// j points to the end of the array
int j = size - 1;
for (int i = K - 1; i >= 0; i--) {
curr_points = curr_points
+ arr[j] - arr[i];
max_points = max(curr_points,
max_points);
j--;
}
// Return the final result
return max_points;
}
// Driver code
int main()
{
int arr[] = { 8, 4, 4, 8, 12, 3, 2, 9 };
int K = 3;
int n = sizeof(arr) / sizeof(arr[0]);
cout << maxPointCount(arr, K, n);
return 0;
}
Java
// Java program to maximize the sum
// of K elements in the array by
// taking only corner elements
import java.util.Scanner;
import java.util.Arrays;
class GFG{
// Function to return maximum sum
public static int maxPointCount(int arr[],
int K,
int size)
{
// Initialse variables
int curr_points = 0;
int max_points = 0;
// Iterate over first K elements
// of array and update the value
// for curr_points
for(int i = 0; i < K; i++)
curr_points += arr[i];
// Update value for max_points
max_points = curr_points;
// j points to the end of the array
int j = size - 1;
for(int i = K - 1; i >= 0; i--)
{
curr_points = curr_points +
arr[j] - arr[i];
max_points = Math.max(curr_points,
max_points);
j--;
}
// Return the final result
return max_points;
}
// Driver code
public static void main(String args[])
{
int []arr = { 8, 4, 4, 8, 12, 3, 2, 9 };
int K = 3;
int n = arr.length;
System.out.print( maxPointCount(arr, K, n));
}
}
// This code is contributed by SoumikMondal
Python3
# Python3 program to maximize the sum
# of K elements in the array by taking
# only corner elements
# Function to return maximum sum
def maxPointCount(arr, K, size):
# Initialse variables
curr_points = 0
max_points = 0
# Iterate over first K elements
# of array and update the value
# for curr_points
for i in range(K):
curr_points += arr[i]
# Update value for max_points
max_points = curr_points
# j points to the end of the array
j = size - 1
for i in range(K - 1, -1, -1):
curr_points = (curr_points +
arr[j] - arr[i])
max_points = max(curr_points,
max_points)
j -= 1
# Return the final result
return max_points
# Driver code
if __name__ == "__main__":
arr = [ 8, 4, 4, 8, 12, 3, 2, 9 ]
K = 3
n = len(arr)
print(maxPointCount(arr, K, n))
# This code is contributed by chitranayal
C#
// C# program to maximize the sum
// of K elements in the array by
// taking only corner elements
using System;
class GFG{
// Function to return maximum sum
public static int maxPointCount(int []arr,
int K,
int size)
{
// Initialse variables
int curr_points = 0;
int max_points = 0;
// Iterate over first K elements
// of array and update the value
// for curr_points
for(int i = 0; i < K; i++)
curr_points += arr[i];
// Update value for max_points
max_points = curr_points;
// j points to the end of the array
int j = size - 1;
for(int i = K - 1; i >= 0; i--)
{
curr_points = curr_points +
arr[j] - arr[i];
max_points = Math.Max(curr_points,
max_points);
j--;
}
// Return the readonly result
return max_points;
}
// Driver code
public static void Main(String []args)
{
int []arr = { 8, 4, 4, 8, 12, 3, 2, 9 };
int K = 3;
int n = arr.Length;
Console.Write( maxPointCount(arr, K, n));
}
}
// This code is contributed by sapnasingh4991
Javascript
21
高效方法:为了更有效地解决问题,我们将实施滑动窗口概念。
- 用0初始化两个整数, curr_points和max_points分别代表当前点和最大点。
- 现在,迭代K中的元素逐个从一开始,并形成大小K的窗口,也通过curr_points更新curr_points的值+ ARR [i]和与curr_points的值MAX_POINTS。
- 之后,在每一步中,从数组的末尾取出一个元素,并从先前选择的窗口中删除最右边的元素,其中开始元素的窗口大小始终保持为 K。相应地更新curr_points和 max_points 的值。最后,我们从数组的末尾有 K 个元素,max_points 包含必须返回的所需结果。
让我们看下图以更好地理解它:
下面是上述方法的实现:
C++
// C++ program to maximize the sum of K elements
// in the array by taking only corner elements
#include
using namespace std;
// Function to return maximum sum
int maxPointCount(int arr[], int K, int size)
{
// Initialse variables
int curr_points = 0;
int max_points = 0;
// Iterate over first K elements of array
// and update the value for curr_points
for (int i = 0; i < K; i++)
curr_points += arr[i];
// Update value for max_points
max_points = curr_points;
// j points to the end of the array
int j = size - 1;
for (int i = K - 1; i >= 0; i--) {
curr_points = curr_points
+ arr[j] - arr[i];
max_points = max(curr_points,
max_points);
j--;
}
// Return the final result
return max_points;
}
// Driver code
int main()
{
int arr[] = { 8, 4, 4, 8, 12, 3, 2, 9 };
int K = 3;
int n = sizeof(arr) / sizeof(arr[0]);
cout << maxPointCount(arr, K, n);
return 0;
}
Java
// Java program to maximize the sum
// of K elements in the array by
// taking only corner elements
import java.util.Scanner;
import java.util.Arrays;
class GFG{
// Function to return maximum sum
public static int maxPointCount(int arr[],
int K,
int size)
{
// Initialse variables
int curr_points = 0;
int max_points = 0;
// Iterate over first K elements
// of array and update the value
// for curr_points
for(int i = 0; i < K; i++)
curr_points += arr[i];
// Update value for max_points
max_points = curr_points;
// j points to the end of the array
int j = size - 1;
for(int i = K - 1; i >= 0; i--)
{
curr_points = curr_points +
arr[j] - arr[i];
max_points = Math.max(curr_points,
max_points);
j--;
}
// Return the final result
return max_points;
}
// Driver code
public static void main(String args[])
{
int []arr = { 8, 4, 4, 8, 12, 3, 2, 9 };
int K = 3;
int n = arr.length;
System.out.print( maxPointCount(arr, K, n));
}
}
// This code is contributed by SoumikMondal
蟒蛇3
# Python3 program to maximize the sum
# of K elements in the array by taking
# only corner elements
# Function to return maximum sum
def maxPointCount(arr, K, size):
# Initialse variables
curr_points = 0
max_points = 0
# Iterate over first K elements
# of array and update the value
# for curr_points
for i in range(K):
curr_points += arr[i]
# Update value for max_points
max_points = curr_points
# j points to the end of the array
j = size - 1
for i in range(K - 1, -1, -1):
curr_points = (curr_points +
arr[j] - arr[i])
max_points = max(curr_points,
max_points)
j -= 1
# Return the final result
return max_points
# Driver code
if __name__ == "__main__":
arr = [ 8, 4, 4, 8, 12, 3, 2, 9 ]
K = 3
n = len(arr)
print(maxPointCount(arr, K, n))
# This code is contributed by chitranayal
C#
// C# program to maximize the sum
// of K elements in the array by
// taking only corner elements
using System;
class GFG{
// Function to return maximum sum
public static int maxPointCount(int []arr,
int K,
int size)
{
// Initialse variables
int curr_points = 0;
int max_points = 0;
// Iterate over first K elements
// of array and update the value
// for curr_points
for(int i = 0; i < K; i++)
curr_points += arr[i];
// Update value for max_points
max_points = curr_points;
// j points to the end of the array
int j = size - 1;
for(int i = K - 1; i >= 0; i--)
{
curr_points = curr_points +
arr[j] - arr[i];
max_points = Math.Max(curr_points,
max_points);
j--;
}
// Return the readonly result
return max_points;
}
// Driver code
public static void Main(String []args)
{
int []arr = { 8, 4, 4, 8, 12, 3, 2, 9 };
int K = 3;
int n = arr.Length;
Console.Write( maxPointCount(arr, K, n));
}
}
// This code is contributed by sapnasingh4991
Javascript
21
时间复杂度: O(N) ,其中 N 是数组的大小。
辅助空间复杂度: O(1) 。
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live