仅使用 PUSH 和 PUSH 查找堆栈中最大的数字POP操作

发布于 2024-12-23 09:46:20 字数 108 浏览 2 评论 0原文

谁能帮我解决这个问题的算法(这是一个面试问题):

给你一个堆栈。设计一个算法来找到最大数量 仅使用 PUSH/POP 操作。

Can anyone please help me with the Algorithm for this problem (its an Interview Question):

You are given a stack. Devise an algorithm to find the maximum number
using PUSH/POP Operations only.

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

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

发布评论

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

评论(3

葬心 2024-12-30 09:46:20

鉴于原始帖子中缺乏约束,您可以弹出所有数据并计算运行最大值:

if empty(st) -> raise exception
m <- pop(st)
while not empty(st)
    n <- pop(st)
    if n > m
        m <- n

编辑(新限制是原始堆栈不变,并且第二个可用堆栈的新资源):

if empty(st) -> raise exception
m <- pop(st)
push(alt_st, m)
while not empty(st)
    n <- pop(st)
    push(alt_st, n)
    if n > m
        m <- n
while not empty(alt_st):
    n <- pop(alt_st)
    push(st, n)

Given the lack of constraints in the original post, you can just pop off all the data and compute a running maximum:

if empty(st) -> raise exception
m <- pop(st)
while not empty(st)
    n <- pop(st)
    if n > m
        m <- n

Edit (with the new restriction that the original stack is unchanged and the new resource of a second available stack):

if empty(st) -> raise exception
m <- pop(st)
push(alt_st, m)
while not empty(st)
    n <- pop(st)
    push(alt_st, n)
    if n > m
        m <- n
while not empty(alt_st):
    n <- pop(alt_st)
    push(st, n)
丑丑阿 2024-12-30 09:46:20

因为原始堆栈不能是只读的(Pop,访问数据的唯一方式,也会修改堆栈),我们必须考虑“堆栈应该不变”的限制意味着我们必须完成后将其恢复到原始状态。

我们可以通过使用 Raymond Hettinger 提出的方法中的另一个堆栈来实现:

int get_max_from_stack(Stack stack) {
    int M = stack.pop();
    Stack aux;
    aux.push(M);
    while (!stack.empty()){
        int tmp = stack.pop();
        aux.push(tmp);
        M = max(M, tmp);
    };

    while (!aux.empty())
        stack.push(aux.pop());

    return M;
};

Because original stack cannot be read-only (Pop, the only way to access the data, also modifies the stack), we have to consider that "the stack should be unchanged" restriction means that we have to restore it to its original state after we finish.

We can do it by using another stack in method proposed by Raymond Hettinger:

int get_max_from_stack(Stack stack) {
    int M = stack.pop();
    Stack aux;
    aux.push(M);
    while (!stack.empty()){
        int tmp = stack.pop();
        aux.push(tmp);
        M = max(M, tmp);
    };

    while (!aux.empty())
        stack.push(aux.pop());

    return M;
};
坐在坟头思考人生 2024-12-30 09:46:20

有两种方法可以解决这个问题,要么你可以创建一个函数get_max来给出堆栈中的最大数字,或者你可以维护一些额外的信息,使用它可以给出堆栈中的最大数字在O(1)操作中,但以额外空间为代价。我会给你后一种解决方案。

您需要做的是拥有一个额外的堆栈,对于堆栈的任何配置,该堆栈的最大元素都位于顶部。

  1. 每当您将数字推入原始堆栈时,请对 max_stack 执行以下操作,将当前值与 max_stack 的顶部进行比较,并将较大的值推入其中。
  2. 当您弹出一个数字时,只需从 max_stack 中弹出最上面的数字
  3. 当您需要找出 max 值时,只需从 max_stack 中选择堆栈顶部。

这样你就可以在O(1)时间内获得最大数量,并且push和pop操作也保持O(1)。我本可以给你代码,但里面没有太多内容,因为它很简单。例如,如果您按

5 - 2 - 6 - 8 - 1

的顺序推送以下数字,max_stack 将包含

5 - 5 - 6 - 8 - 8 code>

,当您弹出数字时,当前max将位于顶部。

我希望解决方案是明确的。

There are two ways of tackling this problem, either you can make a function get_maxwhich gives the maximum number in the stack, or you can maintain some extra information using which you can give the maximum number in the stack in O(1) operation but at the cost of extra space. I will give you the latter solution.

What you need to do is to have an extra stack that will have the maximum element of the stack at the top, for any configuration of the stack.

  1. Whenever you push a number to your original stack, do the following for max_stack, compare the current value with the top of max_stack and push the greater one onto it.
  2. When you pop a number simply pop the topmost number from the max_stack
  3. When you need to find out the max value just pick the stack top from max_stack.

This way you can get the max number in O(1) time and the push and pop operations also remain O(1). I could have give you the code but there is nothing much in it, as it is straight forward. For example if you push following numbers in the order

5 - 2 - 6 - 8 - 1

max_stack will contain

5 - 5 - 6 - 8 - 8

and as you pop of the numbers the current max will be at the top.

I hope the solution is clear.

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