forked from oleg-cherednik/DailyCodingProblem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
63 lines (49 loc) · 1.52 KB
/
Solution.java
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
import java.util.Arrays;
/**
* @author Oleg Cherednik
* @since 02.01.2019
*/
public class Solution {
public static void main(String... args) {
int[] arr = { 4, 3, 5, 9, 9, 9, 4, 5, 1, 9, 2, 7, 1, 1, 4, 1, 2, 2, 1, 9 };
System.out.println(Arrays.toString(arr));
Stack stack = new Stack(arr.length);
for (int val : arr) {
stack.push(val);
System.out.print(stack.max() + ", ");
}
System.out.println();
for (int i = 0; i < arr.length; i++) {
int max = stack.max();
int val = stack.pop();
System.out.print(val + ":" + max + " | ");
}
System.out.println();
}
public static final class Stack {
private final int[] values;
private final int[] maxValues;
private int top = -1;
public Stack(int size) {
values = new int[size];
maxValues = new int[size];
}
public void push(int val) {
if (top + 1 == values.length)
throw new StackOverflowError();
top++;
values[top] = val;
maxValues[top] = Math.max(top == 0 ? Integer.MIN_VALUE : maxValues[top - 1], values[top]);
}
public int pop() {
if (top == -1)
throw new RuntimeException();
return values[top--];
}
public int max() {
if (top == -1)
throw new RuntimeException();
return maxValues[top];
}
}
}