import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param nums int一维数组
* @return int二维数组
*/
public int[][] foundMonotoneStack (int[] nums) {
int[][] result = new int[nums.length][2];
if (nums == null || nums.length == 0) {
return result;
}
Stack<Integer> stack = new Stack<>();
// 从左往右,依次进行入栈,保存从左到右的升序的值
for (int i = 0; i < nums.length; i++) {
// 如果栈里面的值都比其大,就pop
while (!stack.isEmpty() && nums[stack.peek()] >= nums[i]) {
stack.pop();
}
if (stack.isEmpty()) {
result[i][0] = -1;
} else {
// 如果有比他小的,那么栈中的第一个元素的值就是离他最近
result[i][0] = stack.peek();
}
stack.push(i);
}
// 思路跟上面的一样,从右往左,保存升序值
stack.clear();
for (int j = nums.length - 1; j >= 0; j--) {
while (!stack.isEmpty() && nums[stack.peek()] >= nums[j]) {
stack.pop();
}
if (stack.isEmpty()) {
result[j][1] = -1;
} else {
result[j][1] = stack.peek();
}
stack.push(j);
}
return result;
}
}