Skip to content

Commit 7874a12

Browse files
committed
Create 0665-non-decreasing-array.js
1 parent 324cd74 commit 7874a12

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {boolean}
4+
*/
5+
var checkPossibility = function (nums) {
6+
let changed = false;
7+
8+
for (let i = 0; i < nums.length - 1; i++) {
9+
if (nums[i] <= nums[i + 1]) continue;
10+
if (changed) return false;
11+
12+
if (i === 0 || nums[i + 1] >= nums[i - 1]) {
13+
nums[i] = nums[i + 1];
14+
} else {
15+
nums[i + 1] = nums[i];
16+
}
17+
18+
changed = true;
19+
}
20+
21+
return true;
22+
};

0 commit comments

Comments
 (0)