Skip to content

Commit bcd3702

Browse files
committed
1243. Array Transformation
1 parent 852a635 commit bcd3702

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

array-transformation.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Runtime: 8 ms
2+
// Memory Usage: 8.6 MB
3+
class Solution {
4+
public:
5+
vector<int> transformArray(vector<int>& arr) {
6+
while (1) {
7+
vector<int> t = arr;
8+
bool changed = false;
9+
for (int i = 1; i < arr.size() - 1; i++) {
10+
if (t[i] < t[i + 1] && t[i] < t[i - 1]) {
11+
arr[i]++;
12+
changed = true;
13+
} else if (t[i] > t[i + 1] && t[i] > t[i - 1]) {
14+
arr[i]--;
15+
changed = true;
16+
}
17+
}
18+
if (!changed) return arr;
19+
}
20+
return arr;
21+
}
22+
};

0 commit comments

Comments
 (0)