Skip to content

Commit ef9c5e2

Browse files
authored
Create 1243-array-transformation.js
1 parent fc823c2 commit ef9c5e2

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

1243-array-transformation.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @param {number[]} arr
3+
* @return {number[]}
4+
*/
5+
const transformArray = function(arr) {
6+
let cur = arr
7+
while(true) {
8+
const clone = cur.slice()
9+
for(let i = 1; i < clone.length - 1; i++) {
10+
if(cur[i] > cur[i - 1] && cur[i] > cur[i + 1]) clone[i]--
11+
else if(cur[i] < cur[i - 1] && cur[i] < cur[i + 1]) clone[i]++
12+
}
13+
if(same(cur, clone)) return clone
14+
cur = clone
15+
}
16+
17+
return cur
18+
19+
function same(a1, a2) {
20+
for(let i = 0; i< a1.length; i++) {
21+
if(a1[i] !== a2[i]) return false
22+
}
23+
return true
24+
}
25+
};

0 commit comments

Comments
 (0)