-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path1243.Array-Transformation.java
47 lines (39 loc) · 1.12 KB
/
1243.Array-Transformation.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
// https://leetcode.com/problems/array-transformation/
//
// algorithms
// Easy (50.1%)
// Total Accepted: 2,047
// Total Submissions: 4,086
// beats 100.0% of java submissions
class Solution {
public List<Integer> transformArray(int[] arr) {
int len = arr.length;
int[] res = new int[len];
res[0] = arr[0];
res[len - 1] = arr[len - 1];
boolean change = false;
while (true) {
change = false;
for (int i = 1; i < len - 1; i++) {
if (arr[i] > arr[i - 1] && arr[i] > arr[i + 1]) {
res[i] = arr[i] - 1;
change = true;
} else if (arr[i] < arr[i - 1] && arr[i] < arr[i + 1]) {
res[i] = arr[i] + 1;
change = true;
} else {
res[i] = arr[i];
}
}
if (!change) {
break;
}
arr = Arrays.copyOf(res, len);
}
List<Integer> r = new ArrayList<>();
for (int n : res) {
r.add(n);
}
return r;
}
}