2024/11/23 灵茶题单 单调栈

This commit is contained in:
Cool 2024-11-23 22:12:15 +08:00
parent 6ad7364f2d
commit 999996234e
1 changed files with 23 additions and 0 deletions
src/main/java/com/cool/ling_cha_mount/stack

View File

@ -0,0 +1,23 @@
package com.cool.ling_cha_mount.stack;
import org.junit.Test;
import java.util.Stack;
public class Num1475 {
public int[] finalPrices(int[] prices) {
Stack<Integer> stack=new Stack<>();
for(int i=0;i<prices.length;i++){
while(!stack.isEmpty()&&prices[i]<=prices[stack.peek()]){
int j=stack.pop();
prices[j]=prices[j]-prices[i];
}
stack.push(i);
}
return prices;
}
@Test
public void test(){
finalPrices(new int[]{8,4,6,2,3});
}
}