📌  相关文章
📜  通过只取角元素来最大化数组中最多 K 个元素的总和 | 2套

📅  最后修改于: 2021-09-03 03:54:07             🧑  作者: Mango

给定一个数组 arr[]一个整数 K ,任务是通过只取角元素来找到并最大化数组中至多 K 个元素的总和。

例子:

天真的方法:
为了解决上面提到的问题,我们将使用Recursion 。由于我们只能取一个开始或结束索引值,因此初始化两个变量并最多执行 K 步并返回所有可能组合中的最大总和。仅当最大总和大于前一个总和时才更新最大总和,否则跳到下一个可能的组合。由于其重叠子问题和最优子结构属性,递归方法具有指数复杂性。

下面是上述方法的实现:

C++
// C++ implementation to Maximize sum of atmost
// K elements in Array by taking only corner elements
#include 
using namespace std;
 
// Function to return maximum points
int maxPointCount(int arr[], int K, int start, int end,
                  int points, int max_points)
{
    if (K == 0) {
        return max_points;
    }
    // Pick the start index
    int points_start = points + arr[start];
 
    // Update maximum points if necessary
    max_points = max(max_points, points_start);
 
    // Pick the end index
    int points_end = points + arr[end];
 
    // Update maximum points if necessary
    max_points = max(max_points, points_end);
 
    // Recursive call to get max value
    return max(maxPointCount(arr, K - 1, start + 1, end,
                                points_start, max_points),
               maxPointCount(arr, K - 1, start, end - 1,
                                points_end, max_points));
}
 
// Driver code
int main()
{
    int arr[] = { -2, -1, -6, -3, 1 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    int K = 2;
 
    int points = 0;
 
    int max_points = 0;
 
    // beginning index
    int start = 0;
 
    // end index
    int end = N - 1;
 
    cout << maxPointCount(arr, K, start,
                end, points, max_points);
 
    return 0;
}


Java
// Java implementation to Maximize
// sum of atmost K elements in Array
// by taking only corner elements
import java.util.*;
 
class GFG{
 
// Function to return maximum points
static int maxPointCount(int arr[], int K,
                         int start, int end,
                         int points, int max_points)
{
    if (K == 0)
    {
        return max_points;
    }
     
    // Pick the start index
    int points_start = points + arr[start];
 
    // Update maximum points if necessary
    max_points = Math.max(max_points, points_start);
 
    // Pick the end index
    int points_end = points + arr[end];
 
    // Update maximum points if necessary
    max_points = Math.max(max_points, points_end);
 
    // Recursive call to get max value
    return Math.max(maxPointCount(arr, K - 1,
                                  start + 1, end,
                                  points_start, max_points),
                    maxPointCount(arr, K - 1,
                                  start, end - 1,
                                  points_end, max_points));
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = { -2, -1, -6, -3, 1 };
    int N = arr.length;
    int K = 2;
    int points = 0;
    int max_points = 0;
 
    // Beginning index
    int start = 0;
 
    // End index
    int end = N - 1;
 
    System.out.print(maxPointCount(arr, K, start,
                                   end, points,
                                   max_points));
}
}
 
// This code is contributed by Princi Singh


Python3
# Python3 implementation to maximize sum
# of atmost K elements in array by taking
# only corner elements
 
# Function to return maximum points
def maxPointCount(arr, K, start, end,
                  points, max_points):
 
    if (K == 0):
        return max_points
     
    # Pick the start index
    points_start = points + arr[start]
 
    # Update maximum points if necessary
    max_points = max(max_points, points_start)
 
    # Pick the end index
    points_end = points + arr[end]
 
    # Update maximum points if necessary
    max_points = max(max_points, points_end)
 
    # Recursive call to get max value
    return max(maxPointCount(arr, K - 1, start + 1, end,
                             points_start, max_points),
               maxPointCount(arr, K - 1, start, end - 1,
                             points_end, max_points))
 
# Driver code
if __name__ == "__main__":
     
    arr = [ -2, -1, -6, -3, 1 ]
    N = len(arr)
 
    K = 2
    points = 0
    max_points = 0
 
    # Beginning index
    start = 0
 
    # end index
    end = N - 1
 
    print(maxPointCount(arr, K, start,
                        end, points,
                        max_points))
 
# This code is contributed by chitranayal


C#
// C# implementation to Maximize
// sum of atmost K elements in Array
// by taking only corner elements
using System;
 
class GFG{
 
// Function to return maximum points
static int maxPointCount(int []arr, int K,
                         int start, int end,
                         int points, int max_points)
{
    if (K == 0)
    {
        return max_points;
    }
     
    // Pick the start index
    int points_start = points + arr[start];
 
    // Update maximum points if necessary
    max_points = Math.Max(max_points, points_start);
 
    // Pick the end index
    int points_end = points + arr[end];
 
    // Update maximum points if necessary
    max_points = Math.Max(max_points, points_end);
 
    // Recursive call to get max value
    return Math.Max(maxPointCount(arr, K - 1,
                                  start + 1, end,
                                  points_start, max_points),
                    maxPointCount(arr, K - 1,
                                  start, end - 1,
                                  points_end, max_points));
}
 
// Driver code
public static void Main(String[] args)
{
    int []arr = { -2, -1, -6, -3, 1 };
    int N = arr.Length;
    int K = 2;
    int points = 0;
    int max_points = 0;
 
    // Beginning index
    int start = 0;
 
    // End index
    int end = N - 1;
 
    Console.Write(maxPointCount(arr, K, start,
                                end, points,
                                max_points));
}
}
 
// This code is contributed by sapnasingh4991


Javascript


C++
// C++ implementation to Maximize sum
// of atmost K elements in Array by taking
// only corner elements
#include 
using namespace std;
 
// Function to return maximum points
int maxPointCount(int arr[], int K, int size)
{
    // Initialization of current points
    // and max points so far
    int curr_points = 0;
    int max_points = 0;
 
    // Add elements from the beginning
    for (int i = 0; i < K; i++) {
        curr_points += arr[i];
        max_points = max(curr_points, max_points);
    }
 
    // Points to the end of array element
    int j = size - 1;
 
    // Add K elements from end of array
    for (int i = K - 1; i >= 0; i--) {
        curr_points = curr_points + arr[j] - arr[i];
        max_points = max(curr_points, max_points);
 
        // Decrement the value for j
        j--;
    }
 
    j = size - K;
 
    for (; j < size; j++) {
        curr_points = curr_points - arr[j];
        max_points = max(curr_points, max_points);
    }
 
    // Return the final result
    return max_points;
}
 
// Driver code
int main()
{
    int arr[] = { -2, -1, -6, -3, 1 };
 
    int N = sizeof(arr) / sizeof(arr[0]);
 
    int K = 2;
 
    cout << maxPointCount(arr, K, N);
 
    return 0;
}


Java
// Java implementation to maximize
// sum of atmost K elements in Array
// by taking only corner elements
import java.util.Scanner;
import java.util.Arrays;
 
class GFG{
 
// Function to return maximum points
public static int maxPointCount(int arr[],
                                int K,
                                int size)
{
     
    // Initialization of current points
    // and max points so far
    int curr_points = 0;
    int max_points = 0;
 
    // Add elements from the beginning
    for(int i = 0; i < K; i++)
    {
        curr_points += arr[i];
        max_points = Math.max(curr_points,
                              max_points);
    }
 
    // Points to the end of array element
    int j = size - 1;
 
    // Add K elements from end of array
    for(int i = K - 1; i >= 0; i--)
    {
        curr_points = curr_points +
                      arr[j] - arr[i];
        max_points = Math.max(curr_points,
                              max_points);
 
        // Decrement the value for j
        j--;
    }
 
    j = size - K;
    for(; j < size; j++)
    {
        curr_points = curr_points - arr[j];
        max_points = Math.max(curr_points,
                              max_points);
    }
 
    // Return the final result
    return max_points;
}
 
// Driver code
public static void main(String args[])
{
    int []arr = { -2, -1, -6, -3, 1 };
    int N = arr.length;
    int K = 2;
 
    System.out.print(maxPointCount(arr, K, N));
}
}
 
// This code is contributed by SoumikMondal


Python3
# Python3 implementation to
# Maximize sum of atmost K
# elements in Array by taking
# only corner elements
  
# Function to return maximum
# points
def maxPointCount(arr, K, size):
 
    # Initialization of current
    # points and max points so far
    curr_points = 0;
    max_points = 0;
  
    # Add elements from
    # the beginning
    for i in range(K):
     
        curr_points += arr[i];
        max_points = max(curr_points,
                         max_points)   
  
    # Points to the end
    # of array element
    j = size - 1;
  
    # Add K elements from
    # end of array
    for i in range(K - 1, -1, -1):
     
        curr_points = (curr_points +
                       arr[j] - arr[i]);
        max_points = max(curr_points,
                         max_points);
  
        # Decrement the
        # value for j
        j -= 1;   
 
    for j in range(size - K, size):   
        curr_points = (curr_points -
                       arr[j]);
        max_points = max(curr_points,
                         max_points);   
  
    # Return the final result
    return max_points;   
 
# Driver code
if __name__ == "__main__":
     
    arr = [-2, -1, -6, -3, 1]
    N = len(arr)
    K = 2;   
    print(maxPointCount(arr,K,N))
     
# This code is contributed by rutvik_56


C#
// C# implementation to maximize 
// sum of atmost K elements in Array 
// by taking only corner elements
using System;
 
class GFG{
     
// Function to return maximum points
static int maxPointCount(int[] arr, int K,
                         int size)
{
     
    // Initialization of current points 
    // and max points so far
    int curr_points = 0;
    int max_points = 0;
   
    // Add elements from the beginning
    for(int i = 0; i < K; i++) 
    {
        curr_points += arr[i];
        max_points = Math.Max(curr_points,
                               max_points);
    }
   
    // Points to the end of array element
    int j = size - 1;
   
    // Add K elements from end of array
    for(int i = K - 1; i >= 0; i--)
    {
        curr_points = curr_points + arr[j] -
                                    arr[i];
        max_points = Math.Max(curr_points,
                               max_points);
   
        // Decrement the value for j
        j--;
    }
   
    j = size - K;
    for(; j < size; j++)
    {
        curr_points = curr_points - arr[j];
        max_points = Math.Max(curr_points,
                               max_points);
    }
   
    // Return the final result
    return max_points;
}
 
// Driver code
static void Main()
{
    int[] arr = { -2, -1, -6, -3, 1 };
    int N = arr.Length;
    int K = 2;
   
    Console.WriteLine(maxPointCount(arr, K, N));
}
}
 
// This code is contributed by divyeshrabadiya07


输出:
1

有效的方法:
为了优化上述解决方案,我们将实施滑动窗口概念

  • 最初,窗口大小为 0,因为我们不从数组中选取任何元素。我们采用两个变量curr_pointsmax_points来表示当前点和最大点。
  • 从头开始一个一个地考虑 K 个元素。因此,在每一步中,我们计算当前点并在必要时更新最大点,在包含数组中的 K 个元素之后,我们的滑动窗口大小变为 K,这是可能的最大值。
  • 之后,在每一步中,我们从最后选择元素,并从先前选择的窗口中删除最右边的元素,其中包含前 K 个元素。更新 curr_points 和 max_points。最后,该窗口包含来自数组末尾的 K 张卡片。
  • 最后,在每一步中,从前面选择的窗口中删除最左边的卡片,最后有 K 个元素。更新 curr_points 和 max_points 的值。最后,窗口大小将再次为 0。

让我们看这个例子更好地理解它, arr[] = {-2, -1, -6, -3, 1}, K = 2

下面是上述方法的实现:

C++

// C++ implementation to Maximize sum
// of atmost K elements in Array by taking
// only corner elements
#include 
using namespace std;
 
// Function to return maximum points
int maxPointCount(int arr[], int K, int size)
{
    // Initialization of current points
    // and max points so far
    int curr_points = 0;
    int max_points = 0;
 
    // Add elements from the beginning
    for (int i = 0; i < K; i++) {
        curr_points += arr[i];
        max_points = max(curr_points, max_points);
    }
 
    // Points to the end of array element
    int j = size - 1;
 
    // Add K elements from end of array
    for (int i = K - 1; i >= 0; i--) {
        curr_points = curr_points + arr[j] - arr[i];
        max_points = max(curr_points, max_points);
 
        // Decrement the value for j
        j--;
    }
 
    j = size - K;
 
    for (; j < size; j++) {
        curr_points = curr_points - arr[j];
        max_points = max(curr_points, max_points);
    }
 
    // Return the final result
    return max_points;
}
 
// Driver code
int main()
{
    int arr[] = { -2, -1, -6, -3, 1 };
 
    int N = sizeof(arr) / sizeof(arr[0]);
 
    int K = 2;
 
    cout << maxPointCount(arr, K, N);
 
    return 0;
}

Java

// Java implementation to maximize
// sum of atmost K elements in Array
// by taking only corner elements
import java.util.Scanner;
import java.util.Arrays;
 
class GFG{
 
// Function to return maximum points
public static int maxPointCount(int arr[],
                                int K,
                                int size)
{
     
    // Initialization of current points
    // and max points so far
    int curr_points = 0;
    int max_points = 0;
 
    // Add elements from the beginning
    for(int i = 0; i < K; i++)
    {
        curr_points += arr[i];
        max_points = Math.max(curr_points,
                              max_points);
    }
 
    // Points to the end of array element
    int j = size - 1;
 
    // Add K elements from end of array
    for(int i = K - 1; i >= 0; i--)
    {
        curr_points = curr_points +
                      arr[j] - arr[i];
        max_points = Math.max(curr_points,
                              max_points);
 
        // Decrement the value for j
        j--;
    }
 
    j = size - K;
    for(; j < size; j++)
    {
        curr_points = curr_points - arr[j];
        max_points = Math.max(curr_points,
                              max_points);
    }
 
    // Return the final result
    return max_points;
}
 
// Driver code
public static void main(String args[])
{
    int []arr = { -2, -1, -6, -3, 1 };
    int N = arr.length;
    int K = 2;
 
    System.out.print(maxPointCount(arr, K, N));
}
}
 
// This code is contributed by SoumikMondal

蟒蛇3

# Python3 implementation to
# Maximize sum of atmost K
# elements in Array by taking
# only corner elements
  
# Function to return maximum
# points
def maxPointCount(arr, K, size):
 
    # Initialization of current
    # points and max points so far
    curr_points = 0;
    max_points = 0;
  
    # Add elements from
    # the beginning
    for i in range(K):
     
        curr_points += arr[i];
        max_points = max(curr_points,
                         max_points)   
  
    # Points to the end
    # of array element
    j = size - 1;
  
    # Add K elements from
    # end of array
    for i in range(K - 1, -1, -1):
     
        curr_points = (curr_points +
                       arr[j] - arr[i]);
        max_points = max(curr_points,
                         max_points);
  
        # Decrement the
        # value for j
        j -= 1;   
 
    for j in range(size - K, size):   
        curr_points = (curr_points -
                       arr[j]);
        max_points = max(curr_points,
                         max_points);   
  
    # Return the final result
    return max_points;   
 
# Driver code
if __name__ == "__main__":
     
    arr = [-2, -1, -6, -3, 1]
    N = len(arr)
    K = 2;   
    print(maxPointCount(arr,K,N))
     
# This code is contributed by rutvik_56

C#

// C# implementation to maximize 
// sum of atmost K elements in Array 
// by taking only corner elements
using System;
 
class GFG{
     
// Function to return maximum points
static int maxPointCount(int[] arr, int K,
                         int size)
{
     
    // Initialization of current points 
    // and max points so far
    int curr_points = 0;
    int max_points = 0;
   
    // Add elements from the beginning
    for(int i = 0; i < K; i++) 
    {
        curr_points += arr[i];
        max_points = Math.Max(curr_points,
                               max_points);
    }
   
    // Points to the end of array element
    int j = size - 1;
   
    // Add K elements from end of array
    for(int i = K - 1; i >= 0; i--)
    {
        curr_points = curr_points + arr[j] -
                                    arr[i];
        max_points = Math.Max(curr_points,
                               max_points);
   
        // Decrement the value for j
        j--;
    }
   
    j = size - K;
    for(; j < size; j++)
    {
        curr_points = curr_points - arr[j];
        max_points = Math.Max(curr_points,
                               max_points);
    }
   
    // Return the final result
    return max_points;
}
 
// Driver code
static void Main()
{
    int[] arr = { -2, -1, -6, -3, 1 };
    int N = arr.Length;
    int K = 2;
   
    Console.WriteLine(maxPointCount(arr, K, N));
}
}
 
// This code is contributed by divyeshrabadiya07
输出:
1

时间复杂度: O(n)
辅助空间: O(1)
类似文章:通过只取角元素来最大化数组中 K 个元素的总和

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