-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.java
More file actions
30 lines (23 loc) · 768 Bytes
/
Copy pathsolution.java
File metadata and controls
30 lines (23 loc) · 768 Bytes
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
class Solution {
List<Integer> makeBeautiful(int[] arr) {
// List used like a stack
List<Integer> st = new ArrayList<>();
// Traverse all numbers
for (int num : arr) {
// Get stack size
int size = st.size();
// Check opposite signs with last element
if (size > 0 &&
((st.get(size - 1) >= 0 && num < 0) ||
(st.get(size - 1) < 0 && num >= 0))) {
// Remove last element
st.remove(size - 1);
} else {
// Keep current element
st.add(num);
}
}
// Return final beautiful array
return st;
}
}