用友 09.09 看大楼数量单调栈

    public static int[] findBuilding(int[] heights) {
        // write code here
        Stack<Integer> st = new Stack<>();
        Stack<Integer> st1 = new Stack<>();
        int[] left = new int[heights.length];
        int[] right = new int[heights.length];
        int[] res = new int[heights.length];
        Arrays.fill(res, 1);
        for (int i = 0; i < heights.length; i++) {
            while (!st.isEmpty() && heights[i] >= heights[st.peek()]) {
                Integer peek = st.peek();
                st.pop();
                left[peek] = st.size();
            }
            st.push(i);
        }
        for (int i = heights.length - 1; i >= 0; i--) {
            while (!st1.isEmpty() && heights[i] >= heights[st1.peek()]) {
                Integer peek = st1.peek();
                st1.pop();
                right[peek] = st1.size();
            }
            st1.push(i);
        }
        for (int i = 0; i < heights.length; i++) {
            if (i == 0) {
                res[i] += right[i + 1] + 1;
            } else if (i == heights.length - 1) {
                res[i] += left[i - 1] + 1;
            } else {
                res[i] += left[i - 1] + right[i + 1] + 2;
            }
        }
        return res;
    }

#用友##23届秋招笔面经#
全部评论
😭😭😭还是不太懂为什么实例里面27能看见4个楼顶,他不是最矮的被夹在中间啥也看不见吗
点赞 回复 分享
发布于 2022-09-09 21:41 陕西
最后一秒钟没有改好,就交卷了,真tm可惜
点赞 回复 分享
发布于 2022-09-09 21:05 湖北

相关推荐

点赞 评论 收藏
分享
05-27 14:57
西北大学 golang
强大的社畜在走神:27届真不用急,可以搞点项目、竞赛再沉淀沉淀,我大二的时候还在天天打游戏呢
投递华为等公司10个岗位
点赞 评论 收藏
分享
每晚夜里独自颤抖:要求太多的没必要理
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务