Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf29db46

Browse files
[LEET-504] add 504
1 parent9c3532f commitf29db46

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+
packagecom.stevesun.solutions;
2+
3+
importjava.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+
publicclassNextGreaterElementII {
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+
publicint[]nextGreaterElements(int[]nums) {
24+
if (nums ==null ||nums.length ==0)returnnums;
25+
intlen =nums.length;
26+
Stack<Integer>stack =newStack<>();
27+
for (inti =len-1;i >=0;i--) {
28+
stack.push(i);//push all indexes into the stack reversely
29+
}
30+
int[]result =newint[len];
31+
for (inti =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+
returnresult;
42+
}
43+
44+
//credit: https://leetcode.com/articles/next-greater-element-ii/
45+
publicint[]nextGreaterElements_editorial_solution(int[]nums) {
46+
int[]result =newint[nums.length];
47+
Stack<Integer>stack =newStack<>();
48+
for (inti =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+
returnresult;
56+
}
57+
58+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
packagecom.stevesun;
2+
3+
importcom.stevesun.solutions.NextGreaterElementII;
4+
importorg.junit.Before;
5+
importorg.junit.BeforeClass;
6+
importorg.junit.Test;
7+
8+
importstaticorg.junit.Assert.assertArrayEquals;
9+
10+
publicclassNextGreaterElementIITest {
11+
privatestaticNextGreaterElementIItest;
12+
privatestaticint[]nums;
13+
privatestaticint[]expected;
14+
privatestaticint[]actual;
15+
16+
@BeforeClass
17+
publicstaticvoidsetup(){
18+
test =newNextGreaterElementII();
19+
}
20+
21+
@Before
22+
publicvoidsetupForEachTest(){
23+
expected =newint[]{};
24+
nums =newint[]{};
25+
}
26+
27+
@Test
28+
publicvoidtest1(){
29+
nums =newint[]{1,2,1};
30+
expected =newint[]{2, -1,2};
31+
actual =test.nextGreaterElements(nums);
32+
assertArrayEquals(expected,actual);
33+
}
34+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp