📌  相关文章
📜  删除 k 个角元素以最大化剩余总和

📅  最后修改于: 2021-09-05 11:56:51             🧑  作者: Mango

给定一个数组,任务是从角中删除总共 k 个元素,以最大化剩余元素的总和。例如,如果我们 k = 5 并且如果我们从左角移除 2 个元素,那么我们需要从右角移除 3 个元素。
例子:

天真的方法:
1) 将结果初始化为负无穷大。
2) 计算总和。
3) 运行 x = 1 到 k 的循环
…..从左侧删除 ‘x’ 个元素,从右侧删除 k – i 个元素。
…..如果剩余元素的总和大于结果,则更新结果。

时间复杂度:O(n * k)
高效方法(使用窗口滑动技术)
1) 求数组的总和。
2) 找到前 nk 个元素的总和并将其初始化为当前总和,并将其初始化为结果。
3) 为 i = nk 运行一个循环到 n-1
….curr_sum = curr_sum – arr[i – n + k] + arr[i]
….res = max(res, curr_sum)
在步骤3中,我们主要运行滑动窗口。我们从左侧删除一个元素并从右侧添加一个元素。

下面是上述问题陈述的c++实现。

C++
#include 
using namespace std;
int calculate(int arr[], int n, int k)
{
    // calculate the total sum of all elements
    // present in the array..
    int total_sum = 0;
    for (int i = 0; i < n; i++)
        total_sum += arr[i];
 
    // now calculate the sum of all elements
    // excluding the last k elements..
    int curr_sum = 0;
    for (int i = 0; i < n - k; i++)
        curr_sum += arr[i];
 
    // now here its time to use sliding window
    // concept, remove the first element from
    // the current window and add the new element
    // in it in order to get the sum of all n-k size
    // of elements in arr.
    // Calculate the minimum sum of elements of
    // size n-k and stored it into the result
    int res = curr_sum;
    for (int i = n - k; i < n; i++) {
        curr_sum = curr_sum - arr[i - n + k] + arr[i];
        res = max(res, curr_sum);
    }
 
    // Now return result (sum of remaining n-k elements)
    return res;
}
 
// main function
int main()
{
    int arr[] = { 11, 49, 100, 20, 86, 29, 72 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int k = 4;
    cout << "Maximum sum of remaining elements "
         << calculate(arr, n, k) << "\n";
    return 0;
}


Java
// Java program for the
// above approach
import java.util.*;
class GFG{
 
static int calculate(int[] arr,
                     int n, int k)
{
  // Calculate the total
  // sum of all elements
  // present in the array..
  int total_sum = 0;
   
  for (int i = 0; i < n; i++)
    total_sum += arr[i];
 
  // Now calculate the sum
  // of all elements excluding
  // the last k elements..
  int curr_sum = 0;
   
  for (int i = 0; i < n - k; i++)
    curr_sum += arr[i];
 
  // Now here its time to use
  // sliding window concept,
  // remove the first element
  // from the current window
  // and add the new element
  // in it in order to get
  // the sum of all n-k size
  // of elements in arr.
  // Calculate the minimum
  // sum of elements of
  // size n-k and stored it
  // into the result
  int res = curr_sum;
   
  for (int i = n - k; i < n; i++)
  {
    curr_sum = curr_sum -
               arr[i - n + k] +
               arr[i];
    res = Math.max(res, curr_sum);
  }
 
  // Now return result (sum of
  // remaining n-k elements)
  return res;
}
 
// Driver code
public static void main(String[] args)
{
  int[] arr = {11, 49, 100,
               20, 86, 29, 72};
  int n = arr.length;
  int k = 4;
  System.out.print("Maximum sum of remaining " +
                   "elements " +
                    calculate(arr, n, k) + "\n");
}
}
 
// This code is contributed by Chitranayal


Python3
def calculate(arr, n, k):
     
    # calculate the total sum of all elements
    # present in the array..
    total_sum = 0
    for i in arr:
        total_sum += i
 
    # now calculate the sum of all elements
    # excluding the last k elements..
    curr_sum = 0
    for i in range(n - k):
        curr_sum += arr[i]
 
    # now here its time to use sliding window
    # concept, remove the first element from
    # the current window and add the new element
    # in it in order to get the sum of all n-k size
    # of elements in arr.
    # Calculate the minimum sum of elements of
    # size n-k and stored it into the result
    res = curr_sum
    for i in range(n - k, n):
        curr_sum = curr_sum - arr[i - n + k] + arr[i]
        res = max(res, curr_sum)
 
    # Now return result (sum of remaining n-k elements)
    return res
 
# main function
if __name__ == '__main__':
    arr=[11, 49, 100, 20, 86, 29, 72]
    n = len(arr)
    k = 4
    print("Maximum sum of remaining elements ",calculate(arr, n, k))
 
# This code is contributed by mohit kumar 29


C#
using System;
using System.Collections;
using System.Collections.Generic;
 
class GFG{
   
static int calculate(int []arr, int n, int k)
{
     
    // Calculate the total sum of all elements
    // present in the array..
    int total_sum = 0;
    for(int i = 0; i < n; i++)
        total_sum += arr[i];
  
    // Now calculate the sum of all elements
    // excluding the last k elements..
    int curr_sum = 0;
    for(int i = 0; i < n - k; i++)
        curr_sum += arr[i];
  
    // Now here its time to use sliding window
    // concept, remove the first element from
    // the current window and add the new element
    // in it in order to get the sum of all n-k size
    // of elements in arr.
    // Calculate the minimum sum of elements of
    // size n-k and stored it into the result
    int res = curr_sum;
    for(int i = n - k; i < n; i++)
    {
        curr_sum = curr_sum -
                  arr[i - n + k] + arr[i];
        res = Math.Max(res, curr_sum);
    }
  
    // Now return result (sum of
    // remaining n-k elements)
    return res;
}
 
// Driver code
public static void Main(string[] args)
{
    int []arr = { 11, 49, 100, 20, 86, 29, 72 };
    int n = arr.Length;
    int k = 4;
     
    Console.Write("Maximum sum of remaining " +
                  "elements " +
                  calculate(arr, n, k) + "\n");
}
}
 
// This code is contributed by rutvik_56


Javascript


输出:
Maximum sum of remaining elements 206

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

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