Monotonically increasing and decreasing stack usage

import java.util.ArrayList;

public class Main {
public static void main(String[] args) {
int[] nums= {1,5,2,7,9,8,4,10};
getLeftFirstLT(nums);//Solve the left side of the element in the array The subscript of the first element that is smaller than it, from front to back, constructs a monotonically increasing stack
getLeftFirstGT(nums);//Solve the subscript of the first element on the left side of the array that is larger than it, from front to back, constructs a monotone Descending stack
getRightFirstLT(nums);//Solve the subscript of the first element smaller than it on the right side of the element in the array, from back to front, construct a monotonically descending stack
getRightFirstGT(nums);//Solve the first element on the right side of the array The subscript of the element smaller than it, from back to front, constructs a monotonically increasing stack
}
public static void getRightFirstGT(int[] nums) {
int[] L=new int[nums.length];
ArrayList<Integer> arr=new ArrayList<Integer>();
int top=-1;
int index=nums.length-1;
while(index>=0) {
if(top==-1) {
L[index]=-1;
arr.add (index);
top++;
}else {
while(top>-1 && nums[index]>nums[arr.get(top)] ) {
arr.remove(top);
top--;
}
if(top==-1) {
L[index]=-1;
}else {
L[index]=arr.get(top);
}
arr.add(index);
top++;
}
index--;
}
for(int i=0;i<L.length;i++) {
System.out.print(L[i]+" ");
}
}
public static void getRightFirstLT(int[] nums) {
int[] L=new int[nums.length];
ArrayList<Integer> arr=new ArrayList<Integer>();
int top=-1;
int index=nums.length-1;
while(index>=0) {
if(top==-1) {
L[index]=-1;
arr.add(index);
top++;
}else {
while(top>-1 && nums[index]<nums[arr.get(top)] ) {
arr.remove(top);
top--;
}
if(top ==-1) {
L[index]=-1;
}else {
L[index]=arr.get(top);
}
arr.add(index);
top++;
}
index--;
}
for(int i= 0;i<L.length;i++) {
System.out.print(L[i]+" ");
}
}
public static void getLeftFirstGT(int[] nums) {
int[] L=new int[nums.length ];
ArrayList<Integer> arr=new ArrayList<Integer>();
int top=-1;
//Find the first element to the left of an element that is larger than it, and the stack stores the array subscript
int index=0;
while(index<nums.length) {
if(top==-1) {
L[index]=-1;
arr.add(index);
top++;
}else {
while(top>-1 && nums[index]>nums[arr.get(top)] ) {
arr.remove(top);
top--;
}
if(top==-1) {
L[index]=-1;
}else {
L[index]=arr.get(top);
}
arr.add(index);
top++;
}
index++;
}
for(int i=0;i<L.length;i++) {
System.out.print(L[i]+" ");
}
}
public static void getLeftFirstLT(int[] nums) {
int[] L=new int[nums.length];
ArrayList<Integer> arr=new ArrayList<Integer>();
int top=-1;
//Find the first element to the left of an element that is smaller than it, and the stack stores the array subscript
int index=0;
while(index<nums.length) {
if(top==-1 ) {
L[index]=-1;
arr.add(index);
top++;
}else {
while(top>-1 && nums[index]<nums[arr.get(top)] ) {
arr.remove(top );
top--;//If the current element is less than the top element of the stack, unstack.
}
if(top==-1) {
L[index]=-1;
}else {
L[index]=arr.get(top );
}
arr.add(index);
top++;
}
index++;
}
for(int i=0;i<L.length;i++) {
System.out.print(L[i]+" ");
}
}

}

The left/right solution differs only in the bolded part.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324709323&siteId=291194637