第十章 单调栈part01
● 739. 每日温度
● 496.下一个更大元素 I
详细布置
739. 每日温度
今天正式开始单调栈,这是单调栈一篇扫盲题目,也是经典题。
大家可以读题,思考暴力的解法,然后在看单调栈的解法。 就能感受出单调栈的巧妙
https://programmercarl.com/0739.%E6%AF%8F%E6%97%A5%E6%B8%A9%E5%BA%A6.html
496.下一个更大元素 I
本题和 739. 每日温度 看似差不多,其实 有加了点难度。
https://programmercarl.com/0496.%E4%B8%8B%E4%B8%80%E4%B8%AA%E6%9B%B4%E5%A4%A7%E5%85%83%E7%B4%A0I.htm
目录
0739_每日温度
package com.question.solve.leetcode.programmerCarl2._11_monotonicStacks;
import java.util.Deque;
import java.util.LinkedList;
/**
* 739. 每日温度
*/
class _0739_每日温度 {
public static void main(String[] args) {
}
}
class Solution0739 {
public int[] dailyTemperatures(int[] temperatures) {
int[] res = new int[temperatures.length];
for (int i = 0; i < temperatures.length; i++) {
if (i == temperatures.length - 1) {
res[i] = 0;
return res;
}
for (int j = i + 1; j < temperatures.length; j++) {
if (temperatures[i] < temperatures[j]) {
res[i] = j - i;
break;
} else {
res[i] = 0;
}
}
}
return res;
}
}
class Solution0739_2 {
//版本1
public int[] dailyTemperatures(int[] temperatures) {
int lens = temperatures.length;
int[] res = new int[lens];
/**
如果当前遍历的元素 大于栈顶元素,表示 栈顶元素的右边的最大的元素就是 当前遍历的元素,
所以弹出 栈顶元素,并记录。
如果栈不空的话,还要考虑新的栈顶与当前元素的大小关系。
否则的话,可以直接入栈。
注意,单调栈里 加入的元素是 下标。
*/
Deque<Integer> stack = new LinkedList<>();
stack.push(0);
for (int i = 1; i < lens; i++) {
if (temperatures[i] <= temperatures[stack.peek()]) {//当前遍历元素小于等于栈顶元素,入栈
stack.push(i);
} else {//当前遍历元素大于栈顶元素,出栈
while (!stack.isEmpty() && temperatures[i] > temperatures[stack.peek()]) {
res[stack.peek()] = i - stack.peek();
stack.pop();
}
stack.push(i);
}
}
return res;
}
//版本2
public int[] dailyTemperatures2(int[] temperatures) {
int lens = temperatures.length;
int[] res = new int[lens];
Deque<Integer> stack = new LinkedList<>();
for (int i = 0; i < lens; i++) {
while (!stack.isEmpty() && temperatures[i] > temperatures[stack.peek()]) {
res[stack.peek()] = i - stack.peek();
stack.pop();
}
stack.push(i);
}
return res;
}
}
0496_下一个更大元素I
package com.question.solve.leetcode.programmerCarl2._11_monotonicStacks;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Stack;
public class _0496_下一个更大元素I {
public static void main(String[] args) {
}
}
class Solution0496 {
public int[] nextGreaterElement(int[] nums1, int[] nums2) {
int[] res = new int[]{nums1.length};
Arrays.fill(res, -1);
for (int i = 0; i < nums1.length; i++) {
for (int j = 0; j < nums2.length; j++) {
if (nums1[i] == nums2[j]) {
while (j < nums2.length) {
if (nums1[i] < nums2[j]) {
res[i] = nums2[j];
break;
} else {
j++;
}
}
}
}
}
return res;
}
}
class Solution0496_2 {
public int[] nextGreaterElement(int[] nums1, int[] nums2) {
Stack<Integer> temp = new Stack<>();
int[] res = new int[nums1.length];
Arrays.fill(res, -1);
HashMap<Integer, Integer> hashMap = new HashMap<>();
for (int i = 0; i < nums1.length; i++) {
hashMap.put(nums1[i], i);
}
temp.add(0);
for (int i = 1; i < nums2.length; i++) {
if (nums2[i] <= nums2[temp.peek()]) {
temp.add(i);
} else {
while (!temp.isEmpty() && nums2[temp.peek()] < nums2[i]) {
if (hashMap.containsKey(nums2[temp.peek()])) {
Integer index = hashMap.get(nums2[temp.peek()]);
res[index] = nums2[i];
}
temp.pop();
}
temp.add(i);
}
}
return res;
}
public int[] nextGreaterElement2(int[] nums1, int[] nums2) {
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums1.length; i++) {
map.put(nums1[i], i);
}
int[] res = new int[nums1.length];
Stack<Integer> stack = new Stack<>();
Arrays.fill(res, -1);
for (int i = 0; i < nums2.length; i++) {
while (!stack.isEmpty() && nums2[stack.peek()] < nums2[i]) {
int pre = nums2[stack.pop()];
if (map.containsKey(pre)) {
res[map.get(pre)] = nums2[i];
}
}
stack.push(i);
}
return res;
}
}