当前位置:网站首页>Tag stack - stack monotonically preparatory knowledge - lt. 739. The daily temperature
Tag stack - stack monotonically preparatory knowledge - lt. 739. The daily temperature
2022-08-03 05:10:00 【Caicai's big data development road】
lt.739. 每日温度
[案例需求]

[思路分析一, 双重for循环遍历]
/** * 最简单莫过于双重循环,笔试时至少不会丢分 * 时间复杂度:O(n^2) * 空间复杂度:O(n) */
//The outer loop iterates over the given array one at a time, The inner loop is constantly looking for a number larger than the element traversed by the outer loop at this time,
//并用resContinue to record the largestindex差值
public int[] dailyTemperatures(int[] T) {
int[] res = new int[T.length];
for (int i = 0; i < T.length - 1; i++) {
for (int j = i + 1; j < T.length; j++) {
if (T[j] > T[i]) {
res[i] = j - i;
break;
}
}
}
return res;
}
[思路分析二, Simple principle of monotonic stack]
通常是
一维数组, To find the position of the first element to the right or left of an element that is larger or smaller than itself, At this point we can think of using a monotonic stack.
- This problem is to find the first element to the right of an element that is larger than itself, So you can use a monotonic stack.
那么单调栈的原理是什么呢?为什么时间复杂度是O(n)就可以找到每一个元素的右边第一个比它大的元素位置呢?
单调栈的本质是
空间换时间, 因为在遍历的过程中需要用一个栈来记录右边第一个比当前元素高的元素, 优点是只需要遍历一次.
[单调栈的使用]
- Using a monotonic stack is first explicit:
- 单调栈里存放的元素是什么?
单调栈里只需要存放元素的下标i就可以了,如果需要使用对应的元素,直接T[i]就可以获取.
- Whether the elements in a monotonic stack are incremented or decremented?
从栈顶到栈底的顺序
- 本题, We're going to use increasing order(Again, it refers to the order from the top of the stack to the bottom of the stack),因为只有递增的时候,Adds an element to the stacki,才知道栈顶元素在数组中右面第一个比栈顶元素大的元素是i (curreentIndex, 即当前forThe index of the number to loop through is i).
[具体思路 + 代码实现]
- 维护一个存储下标的单调栈, 从栈底到栈顶的下标对应的温度列表中的温度依次递减, If a subscript is on the monotonic stack, 则表示尚未找到下一次温度更高的下标.
- 正向遍历温度列表, 对于温度列表中的每个元素 T[i], 如果栈为空, 则直接将i进栈, 如果栈不为空, 则比较栈顶元素preIndex对应温度T[preIndex]和当前温度T[i], 如果T[i] > T[preIndex], 则将preInndex移除, 并将preIndexThe corresponding days are assigned as i - preIndex, 重复上述操作, 直到栈为空, Or the temperature corresponding to the top element of the stack is less than or equal to the current temperature, 然后将i出栈.
class Solution {
public int[] dailyTemperatures(int[] temperatures) {
//维护递减栈,The last element on the stack is always smaller than the top element.
//Compare the size of the current element with the top element of the stack
//若当前元素 < 栈顶元素:入栈
/若当前元素 > 栈顶元素:弹出栈顶元素,Record the difference between the two subscripts as the required number of days
//The stack record is used here T 的下标.
Deque<Integer> stack = new LinkedList<>();
int len = temperatures.length;
int[] res = new int[len];
int index = 0;
for(int i = 0; i < len; i++){
int temperature = temperatures[i];
//当前元素 > 栈顶index的元素
//弹出preIndex, 记录preIndex与当前元素i的index差值, 更新到res[preIndex]
while(!stack.isEmpty() && temperature > temperatures[stack.peek()]){
int preIndex = stack.pop();
System.out.println("i为: " + preIndex);
//System.out.println("preIndex为: " + preIndex);
res[preIndex] = i - preIndex;
System.out.println(Arrays.toString(res));
}
//当前元素 <= 栈顶index的元素
//push the current element on the stack
stack.push(i);
}
return res;
}
}

边栏推荐
猜你喜欢

社交电商:流量红利已尽,裂变营销是最低成本的获客之道

接口测试如何准备测试数据

2022/08/02 学习笔记 (day22) 多线程

接口和抽象

【Harmony OS】【ARK UI】ets使用startAbility或startAbilityForResult方式调起Ability
![[Harmony OS] [ArkUI] ets development graphics and animation drawing](/img/36/f4c91f794b1321f11a24505d1617fb.png)
[Harmony OS] [ArkUI] ets development graphics and animation drawing

Fluorescent marker peptides FITC/AMC/FAM/Rhodamine TAMRA/Cy3 / Cy5 / Cy7 - Peptide

BIOTIN ALKYNE CAS: 773888-45-2 Price, Supplier

Windows 安装PostgreSQL

GIS数据漫谈(六)— 投影坐标系统
随机推荐
Bubble sort in c language structure
常见亲脂性细胞膜染料DiO, Dil, DiR, Did光谱图和实验操作流程
私域流量时代来临,电商企业如何布局?
presto安装部署教程
三丁基-巯基膦烷「tBuBrettPhos Pd(allyl)」OTf),1798782-17-8
接口管理工具YApi怎么用?颜值高、易管理、超好用
Redis连接不上的报错解决方案汇总
UV decomposition of biotin - PEG2 - azide | CAS: 1192802-98-4 biotin connectors
typescript40-class类的保护修饰符
建立树形结构
刚上线就狂吸70W粉,新型商业模式“分享购”来了,你知道吗?
mysql 创建索引的三种方式
closures in js
c语言结构体中的冒泡排序
typescript49-交叉类型
【Harmony OS】【ARK UI】轻量级数据存储
Peptides mediated PEG DSPE of phospholipids, targeted functional materials - PEG - RGD/TAT/NGR/APRPG
MySQL 出现 The table is full 的解决方法
User password encryption tool
C#异步和多线程