暴力解: 提交的代码 提交时间:2018-03-12 语言:Java 运行时间: 408 ms 占用内存:23136K 状态:答案正确 不知为何才408ms package com.test;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(
new BufferedReader(new InputStreamReader(System.in)));
while (in.hasNext()) {
int n = in.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = in.nextInt();
}
int res = getRes(a);
System.out.println(res);
}
in.close();
}
private static int getRes(int[] a) {
int len = a.length;
int max = a[0];
for (int i = 0; i < len; i++) {
int minHigh = a[i];
for (int j = i + 1; j < len; j++) {
minHigh = Math.min(minHigh, a[j]);
max = Math.max((j-i+1) * minHigh, max);
}
}
return max;
}
}
public int solution(int[] heights) {
int n = heights.length;
if(n==0) return 0;
int[] left = new int[n];
int[] right = new int[n];
int res = 0;
Stack<Integer> s = new Stack<Integer>();
for(int i = 0 ;i<n;i++){
for(;!s.isEmpty()&&heights[i]<=heights[s.peek()];s.pop());
left[i] = s.isEmpty()?0:s.peek()+1;
s.push(i);
}
s.clear();
for(int i = n-1;i>=0;i--){
for(;!s.isEmpty()&&heights[i]<=heights[s.peek()];s.pop());
right[i] = s.isEmpty()?n-1:s.peek()-1;
s.push(i);
}
for(int i = 0;i<n;i++){
res = Math.max(res,(right[i]-left[i]+1)*heights[i]);
}
return res;
}