-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path1019.java
More file actions
80 lines (78 loc) · 2.18 KB
/
1019.java
File metadata and controls
80 lines (78 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
__________________________________________________________________________________________________
sample 3 ms submission
sample 3 ms submission
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public int[] nextLargerNodes(ListNode head) {
ListNode tmp = head;
int length = 0;
while (tmp != null) {
length++;
tmp = tmp.next;
}
int[] result = new int[length];
int top = -1;
int[] stack = new int[length];
int[] position = new int[length];
int cnt = 0;
while (head != null) {
if (top == -1) {
stack[++top] = head.val;
position[top] = cnt;
head = head.next;
cnt++;
} else if(head.val <= stack[top]) {
stack[++top] = head.val;
position[top] = cnt;
head = head.next;
cnt++;
} else {
result[position[top]] = head.val;
top--;
}
}
return result;
}
}
__________________________________________________________________________________________________
sample 38732 kb submission
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public int[] nextLargerNodes(ListNode head) {
List<Integer> ans = new ArrayList<>();
ListNode slow = head;
ListNode fast = slow.next;
while(fast!=null){
while(slow.val>=fast.val && fast.next!=null){
fast=fast.next;
}
if(slow.val>=fast.val){
ans.add(0);
}
else{
ans.add(fast.val);
}
slow=slow.next;
fast=slow.next;
}
if(slow!=null){
ans.add(0);
}
return ans.stream().mapToInt(i -> i).toArray();
}
}
__________________________________________________________________________________________________