Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f29db46

Browse files
[LEET-504] add 504
1 parent 9c3532f commit f29db46

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

leetcode-algorithms/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
|506|[Relative Ranks](https://leetcode.com/problems/relative-ranks/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/RelativeRanks.java) | O(nlogn) |O(n) | Easy|
3636
|505|[The Maze II](https://leetcode.com/problems/the-maze-ii/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/TheMazeII.java) | O(m*n) |O(m*n) | Medium| BFS
3737
|504|[Base 7](https://leetcode.com/problems/base-7/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/Base7.java) | O(1) |O(1) | Easy|
38+
|503|[Next Greater Element II](https://leetcode.com/problems/next-greater-element-ii/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/NextGreaterElementII.java) | O(n) |O(n) | Medium| Stack
3839
|501|[Find Mode in Binary Tree](https://leetcode.com/problems/find-mode-in-binary-tree/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/FindModeinBinaryTree.java) | O(n) |O(k) | Easy| Binary Tree
3940
|500|[Keyboard Row](https://leetcode.com/problems/keyboard-row/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/KeyboardRow.java) | O(n) |O(1) | Easy|
4041
|499|[The Maze III](https://leetcode.com/problems/the-maze-iii/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/TheMazeIII.java) | O(m*n) |O(m*n) | Hard| BFS
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.stevesun.solutions;
2+
3+
import java.util.Stack;
4+
5+
/**
6+
* Given a circular array (the next element of the last element is the first element of the array),
7+
* print the Next Greater Number for every element.
8+
* The Next Greater Number of a number x is the first greater number to its traversing-order next in the array,
9+
* which means you could search circularly to find its next greater number. If it doesn't exist, output -1 for this number.
10+
11+
Example 1:
12+
Input: [1,2,1]
13+
Output: [2,-1,2]
14+
Explanation: The first 1's next greater number is 2;
15+
The number 2 can't find next greater number;
16+
The second 1's next greater number needs to search circularly, which is also 2.
17+
Note: The length of given array won't exceed 10000.
18+
*/
19+
public class NextGreaterElementII {
20+
21+
//Credit: https://discuss.leetcode.com/topic/77881/typical-ways-to-solve-circular-array-problems-java-solution
22+
//Note: we store INDEX into the stack, reversely, the larger index put at the bottom of the stack, the smaller index at the top
23+
public int[] nextGreaterElements(int[] nums) {
24+
if (nums == null || nums.length == 0) return nums;
25+
int len = nums.length;
26+
Stack<Integer> stack = new Stack<>();
27+
for (int i = len-1; i >= 0; i--) {
28+
stack.push(i);//push all indexes into the stack reversely
29+
}
30+
int[] result = new int[len];
31+
for (int i = len-1; i >= 0; i--) {
32+
result[i] = -1;//initialize it to be -1 in case we cannot find its next greater element in the array
33+
while (!stack.isEmpty() && (nums[stack.peek()] <= nums[i])) {
34+
stack.pop();
35+
}
36+
if (!stack.isEmpty()) {
37+
result[i] = nums[stack.peek()];
38+
}
39+
stack.push(i);
40+
}
41+
return result;
42+
}
43+
44+
//credit: https://leetcode.com/articles/next-greater-element-ii/
45+
public int[] nextGreaterElements_editorial_solution(int[] nums) {
46+
int[] result = new int[nums.length];
47+
Stack<Integer> stack = new Stack<>();
48+
for (int i = nums.length*2-1; i>=0 ;i--) {
49+
while (!stack.isEmpty() && nums[stack.peek()] <= nums[i%nums.length]) {
50+
stack.pop();
51+
}
52+
result[i%nums.length] = stack.isEmpty() ? -1 : nums[stack.peek()];
53+
stack.push(i%nums.length);
54+
}
55+
return result;
56+
}
57+
58+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.stevesun;
2+
3+
import com.stevesun.solutions.NextGreaterElementII;
4+
import org.junit.Before;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
import static org.junit.Assert.assertArrayEquals;
9+
10+
public class NextGreaterElementIITest {
11+
private static NextGreaterElementII test;
12+
private static int[] nums;
13+
private static int[] expected;
14+
private static int[] actual;
15+
16+
@BeforeClass
17+
public static void setup(){
18+
test = new NextGreaterElementII();
19+
}
20+
21+
@Before
22+
public void setupForEachTest(){
23+
expected = new int[]{};
24+
nums = new int[]{};
25+
}
26+
27+
@Test
28+
public void test1(){
29+
nums = new int[]{1,2,1};
30+
expected = new int[]{2, -1, 2};
31+
actual = test.nextGreaterElements(nums);
32+
assertArrayEquals(expected, actual);
33+
}
34+
}

0 commit comments

Comments
 (0)