从头或尾部找到最大删除元素的总和?

发布于 2025-01-22 03:01:09 字数 820 浏览 0 评论 0原文

给您一个n个整数的堆叠,使第一个元素代表堆栈的顶部,最后一个元素代表堆栈的底部。您需要从堆栈中至少弹出一个元素。在任何时候,您都可以将堆栈转换为队列。堆栈的底部表示队列的前部。您不能将队列转换回堆栈。您的任务是删除k元素,以使k删除元素的总和最大化。 打印k删除元件的最大可能总和m

输入:n = 10,k = 4
stack = [10 9 1 2 3 4 5 6 7 8]

输出:34

说明:
从堆栈中弹出两个元素。即{10,9}
然后将堆栈转换为队列,然后从 队列。即{8,7}。
最大可能的总和是10+9+8+7 = 34

我正在考虑用贪婪的算法解决它,并具有以下代码:

stk = [10, 9, 1, 30, 3, 4, 5, 100, 1, 8]
n = 10
k = 4

removed = 0
top = 0
sum = 0
bottom = len(stk)-1

while removed < k:
    if stk[top] >= stk[bottom]:
        sum += stk[top]
        top += 1
        
    else:
        sum += stk[bottom]
        bottom -= 1
    removed += 1

print(sum)

在正常测试案例下(给定一个),它将起作用,但在许多其他情况下会失败例如:

[10,9,1,30,3,4,5,100,1,8]

关于如何改进的任何建议?

You are given a stack of N integers such that the first element represents the top of the stack and the last element represents the bottom of the stack. You need to pop at least one element from the stack. At any one moment, you can convert stack into a queue. The bottom of the stack represents the front of the queue. You cannot convert the queue back into a stack. Your task is to remove exactly K elements such that the sum of the K removed elements is maximised.
Print the maximum possible sum M of the K removed elements

Input: N=10, K=4
stack = [10 9 1 2 3 4 5 6 7 8]

Output: 34

Explanation:
Pop two elements from the stack. i.e {10,9}
Then convert the stack into queue and remove first 2 elements from the
queue. i.e {8,7}.
The maximum possible sum is 10+9+8+7 = 34

I was thinking of solving it with greedy algo, with code like following:

stk = [10, 9, 1, 30, 3, 4, 5, 100, 1, 8]
n = 10
k = 4

removed = 0
top = 0
sum = 0
bottom = len(stk)-1

while removed < k:
    if stk[top] >= stk[bottom]:
        sum += stk[top]
        top += 1
        
    else:
        sum += stk[bottom]
        bottom -= 1
    removed += 1

print(sum)

under normal test case(given one) it'll work, but it'll fail in many other scenarios like:

[10, 9, 1, 30, 3, 4, 5, 100, 1, 8]

Any suggestions on how to improve on this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

安穩 2025-01-29 03:01:09

数据结构为您提供了从结构停止中选择1,...,n值的选项,然后是从k = m+n的结构底部的m元素。您可以通过总结结构的第一个k元素来找到最大值,然后通过从背面的第一个元素替换n'th元素来向后进行操作。向后工作,只能到达一个堆栈元素。跟踪一路上的最大值。

在Python中:

lst = [10, 9, 1, 30, 3, 4, 5, 100, 1, 8]

K = 4

sum_ = sum(lst[0:K])
max_so_far = sum_
for i in range(K-1):
    sum_ = sum_ - lst[K-1-i] + lst[-i-1]
    max_so_far = max(sum_, max_so_far)
print(max_so_far)

运行时间为O(n)。

The data structure gives you the option to select 1,...,n values from the stop of the structure and then m elements from the bottom of the structure where K = m+n. You can find the maximum by starting out with summing the first K elements of the structure and then work your way backwards by replacing the n'th element with the first element from the back. Working backwards to you get to only one stack element. Keep track of the maximum along the way.

In python:

lst = [10, 9, 1, 30, 3, 4, 5, 100, 1, 8]

K = 4

sum_ = sum(lst[0:K])
max_so_far = sum_
for i in range(K-1):
    sum_ = sum_ - lst[K-1-i] + lst[-i-1]
    max_so_far = max(sum_, max_so_far)
print(max_so_far)

The running time is O(n).

酸甜透明夹心 2025-01-29 03:01:09

如果您仔细查看问题,它基本上归结为:

从开始的开始和x元素和y元素从数组的末端开始,因此总和是最大的和x+y = k

这是一个非常简单的问题,基本上需要此算法:

ans = sum(last K elements)
for i in range(0, K):
    ans = max(ans, ans + array[i] - array[n-(k-i+1)]) #picking elements from the start and removing elements from the end

If you carefully look at the problem, it basically boils down to:

Select x elements from the start and y elements from the end of the array, such that the sum is maximum and x+y = K.

That is a pretty simple problem to solve, which basically requires this algorithm:

ans = sum(last K elements)
for i in range(0, K):
    ans = max(ans, ans + array[i] - array[n-(k-i+1)]) #picking elements from the start and removing elements from the end
庆幸我还是我 2025-01-29 03:01:09

// C语言代码
/*
想法:

  1. WKT即使是堆栈或队列,我们​​将在数组的结尾或启动时进行弹出/删除。我们正在利用这一点。
    2.k必须在堆栈中删除k元素,以使它们的总和是最大元素
  2. 弹出的元素,必须在堆栈的一开始或结尾处,否。必须删除的元素是“ k”。
    4.我们正在采取k的所有可能性
    即,如果我们从堆栈的前部获取n个元素,则kn elements ahs到堆栈的后面选择
    例如:k = 5 - &gt;可能性:
    (0 5)(1 4)(2 3)(3 2)(4 1)(5 0) - &gt;正如您可以观察到的第一个INDX正在增加到K,第二个INDX从K中衰减到0。
    (0 5)是指从堆栈的前面选择0个元素,从堆栈后面选择5个元素
    同样,其他可能性。
  3. 我们正在采取每种可能性并计算它们的总和,将它们存储在单独的数组“总和”中,并获得总和[]的最大值,这将是答案。而我们获得最大的可能性(假设AB)将是我们可以将堆栈转换为队列的时刻。在堆栈转换为堆栈之前,我们已经从堆栈IE堆栈IE弹出了一个元素,然后在堆栈转换为队列后,我们删除了B元素,该元素将在堆栈的后方的前面完成。
    */

在此处输入图像描述

// code in C language
/*
Idea :

  1. W.K.T even it is a stack or a queue we will pop/delete takes place at end or starting of array. we are making use of that.
    2.K elements has to be removed in a stack such that their SUM is max
  2. popping of elements has to be at start or end of stack and no. of elements has to be removed are 'K'.
    4.We are taking all possibilities of K
    i.e. if we take n elements from the front part of stack then K-n elements ahs to selected from back of stack
    eg : K = 5 --> possibilities:
    (0 5) (1 4) (2 3) (3 2) (4 1) (5 0) -->as you can observe first indx is increasing to K and second indx is decaying to 0 From K.
    (0 5) means selecting 0 elements from front of stack and 5 elementts from back of stack
    similarly, the other possibilities.
  3. We are taking each possibility and calculating their sum , storing them in separate array 'Sum' and getting max of the sum[] and it will be answer . And the possibility (suppose a b) at which we are getting Max will be our moment where you can convert stack into a queue . Before stack converted into stack we have popped a elements from stack i.e. front of array and after stack converted to queue we deleting b elements which will be done at front of queue i.e. back of the stack.
    */

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文