性能瓶颈分析:从CPU、内存到GPU的全链路排查法
2026/1/9 20:56:16
这道题最优解是使用单调栈(Monotonic Stack)。
核心思想:
为什么是 O(n)?
class Solution { public int[] dailyTemperatures(int[] temperatures) { int n=temperatures.length; Stack<Integer> stack =new Stack<>(); int[] res=new int[n]; for(int i=0;i<n;i++){ while(!stack.isEmpty()&&temperatures[i]>temperatures[stack.peek()]){ int a=stack.pop(); res[a]=i-a; } stack.push(i); } res[n-1]=0; return res; } }