题解 | #牛舍的占地面积#
牛舍的占地面积
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;
}
}
#题解#
查看5道真题和解析

