题解 | #愤怒的小鸟#
愤怒的小鸟
https://www.nowcoder.com/practice/597c32f0b3cf43beb1d01e7ddd87cc32
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] h = new int[n];
for (int i = 0; i < n; i++) {
h[i] = in.nextInt();
}
int[] res = new int[n];
Deque<Integer> stack = new LinkedList<>();
for (int i = 0; i < n; i++) {
while (!stack.isEmpty() && h[i] > h[stack.peek()]) {
stack.pop();
}
if (!stack.isEmpty()) {
res[i] += stack.size();
}
stack.push(i);
}
stack.clear();
for (int i = n - 1; i >= 0; i--) {
while (!stack.isEmpty() && h[i] > h[stack.peek()]) {
stack.pop();
}
if (!stack.isEmpty()) {
res[i] += stack.size();
}
stack.push(i);
}
for(int i=0;i<n;i++){
res[i] = n - 1- res[i];
System.out.print(res[i]+ " ");
}
}
}
凡岛公司福利 674人发布
查看30道真题和解析