Skip to content

Commit 77425c5

Browse files
committed
完成496
1 parent 91cfae7 commit 77425c5

19 files changed

+88
-15
lines changed

README.adoc

+9-9
Original file line numberDiff line numberDiff line change
@@ -3985,15 +3985,15 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
39853985
//|{doc_base_url}/0495-teemo-attacking.adoc[题解]
39863986
//|Medium
39873987
//|
3988-
//
3989-
//|{counter:codes}
3990-
//|496
3991-
//|{leetcode_base_url}/next-greater-element-i/[Next Greater Element I]
3992-
//|{source_base_url}/_0496_NextGreaterElementI.java[Java]
3993-
//|{doc_base_url}/0496-next-greater-element-i.adoc[题解]
3994-
//|Easy
3995-
//|
3996-
//
3988+
3989+
|{counter:codes}
3990+
|496
3991+
|{leetcode_base_url}/next-greater-element-i/[Next Greater Element I]
3992+
|{source_base_url}/_0496_NextGreaterElementI.java[Java]
3993+
|{doc_base_url}/0496-next-greater-element-i.adoc[题解]
3994+
|Easy
3995+
|
3996+
39973997
//|{counter:codes}
39983998
//|497
39993999
//|{leetcode_base_url}/random-point-in-non-overlapping-rectangles/[Random Point in Non-overlapping Rectangles]

docs/0000-23-monotonic-stack.adoc

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
. xref:0084-largest-rectangle-in-histogram.adoc[84. Largest Rectangle in Histogram]
88
. xref:0503-next-greater-element-ii.adoc[503. Next Greater Element II]
99
. xref:0042-trapping-rain-water.adoc[42. Trapping Rain Water]
10+
. xref:0496-next-greater-element-i.adoc[496. Next Greater Element I]
1011

1112
== 参考资料
1213

1314
. https://cloud.tencent.com/developer/article/1998273[单调栈详解及其LeetCode应用详解^]
14-
.
15+
. https://blog.csdn.net/weixin_50348837/article/details/136304458[深入理解单调栈算法,这一篇就够了^]

docs/0000-data-structure-list.adoc

+5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
. xref:1094-car-pooling.adoc[1094. Car Pooling]
1515
. xref:1109-corporate-flight-bookings.adoc[1109. Corporate Flight Bookings]
1616

17+
=== 单调栈
18+
19+
详情见 xref:0000-23-monotonic-stack.adoc[Monotonic Stack 单调栈]
20+
21+
1722

1823
== 参考资料
1924

docs/0496-next-greater-element-i.adoc

+33-5
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,11 @@ https://leetcode.com/problems/next-greater-element-i/[LeetCode - Next Greater El
66

77
You are given two arrays *(without duplicates)* `nums1` and `nums2` where `nums1`’s elements are subset of `nums2`. Find all the next greater numbers for `nums1`'s elements in the corresponding places of `nums2`.
88
9-
10-
119
The Next Greater Number of a number *x* in `nums1` is the first greater number to its right in `nums2`. If it does not exist, output -1 for this number.
1210
1311
1412
*Example 1:*
1513
16-
1714
[subs="verbatim,quotes,macros"]
1815
----
1916
*Input:* *nums1* = [4,1,2], *nums2* = [1,3,4,2].
@@ -27,7 +24,6 @@ The Next Greater Number of a number *x* in `nums1` is the first greater number t
2724
2825
*Example 2:*
2926
30-
3127
[subs="verbatim,quotes,macros"]
3228
----
3329
*Input:* *nums1* = [2,4], *nums2* = [1,2,3,4].
@@ -38,13 +34,40 @@ The Next Greater Number of a number *x* in `nums1` is the first greater number t
3834
----
3935
4036
41-
4237
*Note:*
4338
4439
. All elements in `nums1` and `nums2` are unique.
4540
. The length of both `nums1` and `nums2` would not exceed 1000.
4641
42+
== 思路分析
43+
44+
单调栈
45+
46+
image::images/0496-01.png[{image_attr}]
47+
48+
image::images/0496-02.png[{image_attr}]
49+
50+
image::images/0496-03.png[{image_attr}]
51+
52+
image::images/0496-04.png[{image_attr}]
4753
54+
image::images/0496-05.png[{image_attr}]
55+
56+
image::images/0496-06.png[{image_attr}]
57+
58+
image::images/0496-07.png[{image_attr}]
59+
60+
image::images/0496-08.png[{image_attr}]
61+
62+
image::images/0496-09.png[{image_attr}]
63+
64+
image::images/0496-10.png[{image_attr}]
65+
66+
image::images/0496-11.png[{image_attr}]
67+
68+
image::images/0496-12.png[{image_attr}]
69+
70+
image::images/0496-13.png[{image_attr}]
4871
4972
5073
[[src-0496]]
@@ -53,3 +76,8 @@ The Next Greater Number of a number *x* in `nums1` is the first greater number t
5376
include::{sourcedir}/_0496_NextGreaterElementI.java[]
5477
----
5578
79+
== 参考资料
80+
81+
. https://leetcode.cn/problems/next-greater-element-i/solutions/1065517/xia-yi-ge-geng-da-yuan-su-i-by-leetcode-bfcoj/[496. 下一个更大元素 I - 官方题解^]
82+
. https://leetcode.cn/problems/next-greater-element-i/solutions/2820648/shi-pin-dan-diao-zhan-de-liang-chong-xie-ri0i/[496. 下一个更大元素 I - 单调栈的两种写法,附单调栈题单^]
83+

docs/images/0496-01.png

71.6 KB
Loading

docs/images/0496-02.png

82.4 KB
Loading

docs/images/0496-03.png

85.4 KB
Loading

docs/images/0496-04.png

82 KB
Loading

docs/images/0496-05.png

101 KB
Loading

docs/images/0496-06.png

87.6 KB
Loading

docs/images/0496-07.png

74 KB
Loading

docs/images/0496-08.png

77.9 KB
Loading

docs/images/0496-09.png

88.6 KB
Loading

docs/images/0496-10.png

92.5 KB
Loading

docs/images/0496-11.png

80.6 KB
Loading

docs/images/0496-12.png

87.4 KB
Loading

docs/images/0496-13.png

92.7 KB
Loading

logbook/202406.adoc

+6
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,12 @@
295295
|{doc_base_url}/0710-random-pick-with-blacklist.adoc[题解]
296296
|把映射处理好
297297

298+
|{counter:codes}
299+
|496
300+
|{leetcode_base_url}/next-greater-element-i/[Next Greater Element I]
301+
|{doc_base_url}/0496-next-greater-element-i.adoc[题解]
302+
|单调栈,理解不是很透彻,还需要加强练习
303+
298304

299305
|===
300306

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.diguage.algorithm.leetcode;
2+
3+
import java.util.Deque;
4+
import java.util.HashMap;
5+
import java.util.LinkedList;
6+
import java.util.Map;
7+
8+
/**
9+
* @author D瓜哥 · https://www.diguage.com
10+
* @since 2024-07-05 22:40:25
11+
*/
12+
public class _0496_NextGreaterElementI {
13+
/**
14+
* 单调栈,参考模板,自己书写
15+
*/
16+
public int[] nextGreaterElement(int[] nums1, int[] nums2) {
17+
Deque<Integer> stack = new LinkedList<>();
18+
Map<Integer, Integer> big = new HashMap<>();
19+
for (int i = nums2.length - 1; i >= 0; i--) {
20+
while (!stack.isEmpty() && stack.peek() < nums2[i]) {
21+
stack.pop();
22+
}
23+
int val = stack.isEmpty() ? -1 : stack.peek();
24+
big.put(nums2[i], val);
25+
stack.push(nums2[i]);
26+
}
27+
int[] result = new int[nums1.length];
28+
for (int i = 0; i < nums1.length; i++) {
29+
result[i] = big.get(nums1[i]);
30+
}
31+
return result;
32+
}
33+
}

0 commit comments

Comments
 (0)