-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem-1243.cpp
More file actions
30 lines (28 loc) · 777 Bytes
/
Problem-1243.cpp
File metadata and controls
30 lines (28 loc) · 777 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
//Problem - 1243
// https://leetcode.com/problems/array-transformation/
// O(n*k) time complexity and O(1) space complexity sol
class Solution {
public:
vector<int> transformArray(vector<int>& arr) {
int len = arr.size();
int ctr;
vector <int> temp(len);
while(1) {
ctr = 0;
temp = arr;
for(int i = 1; i < len-1; i++) {
if(arr[i] < arr[i-1] && arr[i] < arr[i+1]) {
temp[i]++;
ctr++;
}
if(arr[i] > arr[i-1] && arr[i] > arr[i+1]) {
temp[i]--;
ctr++;
}
}
arr = temp;
if(ctr == 0)
return arr;
}
}
};