Skip to content

Commit 6933963

Browse files
add a solution for 496
1 parent 922fa18 commit 6933963

File tree

2 files changed

+42
-10
lines changed

2 files changed

+42
-10
lines changed

src/main/java/com/fishercoder/solutions/_496.java

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,57 @@
11
package com.fishercoder.solutions;
22

3+
import java.util.Deque;
34
import java.util.HashMap;
5+
import java.util.LinkedList;
46
import java.util.Map;
57
import java.util.Stack;
68

79
public class _496 {
810

911
public static class Solution1 {
10-
public int[] nextGreaterElement(int[] findNums, int[] nums) {
11-
Stack<Integer> stack = new Stack();
12+
public int[] nextGreaterElement(int[] nums1, int[] nums2) {
13+
int[] ans = new int[nums1.length];
14+
Map<Integer, Integer> map = new HashMap<>();
15+
for (int i = 0; i < nums2.length; i++) {
16+
map.put(nums2[i], i);
17+
}
18+
for (int i = 0; i < nums1.length; i++) {
19+
int start = map.get(nums1[i]);
20+
for (int j = start + 1; j < nums2.length; j++) {
21+
if (nums2[j] > nums1[i]) {
22+
ans[i] = nums2[j];
23+
break;
24+
}
25+
}
26+
if (ans[i] == 0) {
27+
ans[i] = -1;
28+
}
29+
}
30+
return ans;
31+
}
32+
}
33+
34+
public static class Solution2 {
35+
/**
36+
* Use monotonic stack, using a pen and paper to help visualize this is a great help!
37+
*/
38+
public int[] nextGreaterElement(int[] nums1, int[] nums2) {
39+
Deque<Integer> stack = new LinkedList<>();
1240
Map<Integer, Integer> map = new HashMap();
13-
for (int i = 0; i < nums.length; i++) {
14-
while (!stack.isEmpty() && nums[i] > stack.peek()) {
15-
map.put(stack.pop(), nums[i]);
41+
for (int i = 0; i < nums2.length; i++) {
42+
while (!stack.isEmpty() && nums2[i] > stack.peekLast()) {
43+
map.put(stack.pollLast(), nums2[i]);
1644
}
17-
stack.push(nums[i]);
45+
stack.addLast(nums2[i]);
1846
}
1947

2048
while (!stack.isEmpty()) {
21-
map.put(stack.pop(), -1);
49+
map.put(stack.pollLast(), -1);
2250
}
2351

24-
int[] result = new int[findNums.length];
25-
for (int i = 0; i < findNums.length; i++) {
26-
result[i] = map.get(findNums[i]);
52+
int[] result = new int[nums1.length];
53+
for (int i = 0; i < nums1.length; i++) {
54+
result[i] = map.get(nums1[i]);
2755
}
2856
return result;
2957
}

src/test/java/com/fishercoder/_496Test.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
public class _496Test {
1111
private static _496.Solution1 solution1;
12+
private static _496.Solution2 solution2;
1213
private static int[] findNums;
1314
private static int[] nums;
1415
private static int[] expected;
@@ -17,6 +18,7 @@ public class _496Test {
1718
@BeforeClass
1819
public static void setup() {
1920
solution1 = new _496.Solution1();
21+
solution2 = new _496.Solution2();
2022
}
2123

2224
@Before
@@ -34,5 +36,7 @@ public void test1() {
3436
expected = new int[]{-1, 3, -1};
3537
actual = solution1.nextGreaterElement(findNums, nums);
3638
assertArrayEquals(expected, actual);
39+
actual = solution2.nextGreaterElement(findNums, nums);
40+
assertArrayEquals(expected, actual);
3741
}
3842
}

0 commit comments

Comments
 (0)