2024/11/23 灵茶题单 单调栈
This commit is contained in:
parent
6ad7364f2d
commit
999996234e
|
@ -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});
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue