题解 | #牛舍的占地面积#

牛舍的占地面积

https://www.nowcoder.com/practice/4d9d9bf23d874688aee6fc1ac5bf6902

题目考察的知识点是:

本题主要考察知识点是队列。

题目解答方法的文字分析:

通过循环排序计算最大的牛棚,得到牛棚的占地面积。

本题解析所用的编程语言:

java语言。

完整且正确的编程代码:

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param areas int整型一维数组
     * @return int整型
     */
    public int maxArea (int[] areas) {
        // write code here
        int n = areas.length;
        int[] arr = new int[n + 2];
        System.arraycopy(areas, 0, arr, 1, n);
        Deque<Integer> stack = new ArrayDeque<>();
        int res = 0;
        for (int i = 0; i < n + 2; i++) {
            while (!stack.isEmpty() && arr[stack.peek()] > arr[i]) {
                int l = stack.pop();
                int temp = arr[l] * (i - stack.peek() - 1);
                res = Math.max(temp, res);
            }
            stack.push(i);
        }
        return res;
    }
}

#题解#
全部评论

相关推荐

09-13 17:25
亲切的00后在笔试:我也遇到了,所以我早他一步查看图片
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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