fio stonewall_C ++中的StoneWall解决方案

本文介绍了C++解决石墙问题的方法,通过堆栈数据结构实现。关键在于当新墙添加时,如果与左侧同等高度的墙相匹配,无需额外石头。文章分析了算法步骤并讨论了时间复杂度。
摘要由CSDN通过智能技术生成

fio stonewall

StoneWall is an interesting problem that requires some brain cycles yet not too complex. It is good software engineer interview question.

StoneWall是一个有趣的问题,需要一些大脑周期,但又不太复杂。 是好的软件工程师面试问题。

Here is a C++ solution whose complexity is O(N).

这是C ++解决方案,其复杂度为O(N)

#include <stack>

int solution(std::vector<int> &H) {
    int stones = 0;
    std::stack<int> heights;

    for (auto h: H) {
        while (!heights.empty() && h < heights.top()) {
            heights.pop();
        }
        if (heights.empty() || h > heights.top()) {
            heights.push(h);
            ++stones;
        }
    }
    return stones;
}

石墙问题分析 (Analysis of the StoneWall problem)

The key insight here (from the example and figure too) is that when a new wall of height h is added on the most right side, if it is the same height as the right most one among those walls on its left that are not taller than h, no need to use an additional stone for the wall. The stone for the wall of the same height on its left can be re-used for this wall.

此处的关键见解 (也来自示例和图)是,当在最右侧添加高度为h的新墙时,如果它的高度与最右侧的高度相同,则在其左侧那些不高的墙中比h ,无需在墙上使用额外的石头。 左侧高度相同的墙壁的石材可以重新用于该墙壁。

逐步分析代码 (Step by step analysis of the code)

The data structure
During the walls are added one by one to the right, the taller ones than the latest one on the left one of the latest one need not to be checked again later. But the same height or shorter ones on the left should be considered still. So, this pattern is like "first in latest out", right? A stack is suitable.

数据结构
在墙的右边一一添加时,比最新的墙高一些的墙在左边的墙中,不需要在后面再检查一次。 但是,应该仍然考虑左侧的相同高度或较短的高度。 所以,这种模式就像“先进先出”,对吗? 堆栈是合适的。

The algorithm
Now we have the key insight and the data structure of a heights stack, we can process the walls one by one.

算法
现在我们有了关键的见识和heights堆栈的数据结构,我们可以一一处理墙壁了。

For each wall, first remove/pop the walls on its left that is higher until the stack is empty or the wall is not higher than it. Then, compare the right most one (highest one) if there is at least one wall left in the stack. If the height is the same, continue to next wall. If the height of the highest one in the stack (the top) is smaller, add the new wall to the stack and count one more stone.

对于每个墙,首先移除/弹出其左侧较高的墙,直到堆栈为空或墙不高于墙为止。 然后,如果堆栈中至少剩下一堵墙,则比较最右边的一堵(最高的一堵)。 如果高度相同,则继续到下一个墙。 如果堆栈中最高的那一层(顶部)的高度较小,则将新墙添加到堆栈中,再数一数石头。

复杂度分析 (Complexity analysis)

The for loop goes through each element once -> O(N) times where N is the size of the input H. Now let’s check the complexity for each element.

for循环对每个元素进行一次-> O(N)次,其中N是输入H的大小。 现在让我们检查每个元素的复杂性。

For each element, there is a while loop inside. But, for each element, it will be pushed at most once to the stack and popped at most once from the stack.

对于每个元素,内部都有一个while循环。 但是,对于每个元素,它将最多被压入堆栈一次,并从堆栈中弹出一次。

So overall, the complexity is O(N).

因此总的来说,复杂度为O(N)

翻译自: https://www.systutorials.com/a-stonewall-solution-in-c/

fio stonewall

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值