Skip to content

Commit c60a9a8

Browse files
refactor 496
1 parent 66f15d1 commit c60a9a8

File tree

2 files changed

+26
-49
lines changed

2 files changed

+26
-49
lines changed

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

Lines changed: 23 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
import java.util.Stack;
66

77
/**
8-
* 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.
9-
10-
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.
8+
* 496. Next Greater Element I
9+
*
10+
* You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2.
11+
* Find all the next greater numbers for nums1's elements in the corresponding places of nums2.
12+
* 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.
1113
1214
Example 1:
1315
Input: nums1 = [4,1,2], nums2 = [1,3,4,2].
@@ -16,63 +18,41 @@
1618
For number 4 in the first array, you cannot find the next greater number for it in the second array, so output -1.
1719
For number 1 in the first array, the next greater number for it in the second array is 3.
1820
For number 2 in the first array, there is no next greater number for it in the second array, so output -1.
21+
1922
Example 2:
2023
Input: nums1 = [2,4], nums2 = [1,2,3,4].
2124
Output: [3,-1]
2225
Explanation:
2326
For number 2 in the first array, the next greater number for it in the second array is 3.
2427
For number 4 in the first array, there is no next greater number for it in the second array, so output -1.
28+
2529
Note:
2630
All elements in nums1 and nums2 are unique.
2731
The length of both nums1 and nums2 would not exceed 1000.
2832
*/
2933
public class _496 {
3034

31-
public int[] nextGreaterElement_clever_way(int[] findNums, int[] nums) {
32-
Stack<Integer> stack = new Stack();
33-
Map<Integer, Integer> map = new HashMap();
34-
for (int i = 0; i < nums.length; i++) {
35-
while (!stack.isEmpty() && nums[i] > stack.peek()) {
36-
map.put(stack.pop(), nums[i]);
35+
public static class Solution1 {
36+
public int[] nextGreaterElement(int[] findNums, int[] nums) {
37+
Stack<Integer> stack = new Stack();
38+
Map<Integer, Integer> map = new HashMap();
39+
for (int i = 0; i < nums.length; i++) {
40+
while (!stack.isEmpty() && nums[i] > stack.peek()) {
41+
map.put(stack.pop(), nums[i]);
42+
}
43+
stack.push(nums[i]);
3744
}
38-
stack.push(nums[i]);
39-
}
40-
41-
while (!stack.isEmpty()) {
42-
map.put(stack.pop(), -1);
43-
}
4445

45-
int[] result = new int[findNums.length];
46-
for (int i = 0; i < findNums.length; i++) {
47-
result[i] = map.get(findNums[i]);
48-
}
49-
return result;
50-
}
51-
52-
53-
public int[] nextGreaterElement_naive_way(int[] findNums, int[] nums) {
54-
int[] result = new int[findNums.length];
55-
for (int i = 0; i < findNums.length; i++) {
56-
boolean found = false;
57-
boolean foundNext = false;
58-
for (int j = 0; j < nums.length; j++) {
59-
if (findNums[i] != nums[j] && !found) {
60-
continue;
61-
} else if (!found) {
62-
found = true;
63-
} else {
64-
if (nums[j] > findNums[i]) {
65-
result[i] = nums[j];
66-
foundNext = true;
67-
break;
68-
}
69-
}
46+
while (!stack.isEmpty()) {
47+
map.put(stack.pop(), -1);
7048
}
71-
if (!foundNext) {
72-
result[i] = -1;
49+
50+
int[] result = new int[findNums.length];
51+
for (int i = 0; i < findNums.length; i++) {
52+
result[i] = map.get(findNums[i]);
7353
}
54+
return result;
7455
}
75-
return result;
7656
}
7757

7858
}

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88
import static org.junit.Assert.assertArrayEquals;
99

1010
public class _496Test {
11-
private static _496 test;
11+
private static _496.Solution1 solution1;
1212
private static int[] findNums;
1313
private static int[] nums;
1414
private static int[] expected;
1515
private static int[] actual;
1616

1717
@BeforeClass
1818
public static void setup() {
19-
test = new _496();
19+
solution1 = new _496.Solution1();
2020
}
2121

2222
@Before
@@ -32,10 +32,7 @@ public void test1() {
3232
findNums = new int[]{4, 1, 2};
3333
nums = new int[]{1, 3, 4, 2};
3434
expected = new int[]{-1, 3, -1};
35-
actual = test.nextGreaterElement_naive_way(findNums, nums);
36-
assertArrayEquals(expected, actual);
37-
38-
actual = test.nextGreaterElement_clever_way(findNums, nums);
35+
actual = solution1.nextGreaterElement(findNums, nums);
3936
assertArrayEquals(expected, actual);
4037
}
4138
}

0 commit comments

Comments
 (0)