Skip to content

Commit d3389b9

Browse files
authored
Create 665-non-decreasing-array.js
1 parent 8525090 commit d3389b9

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

665-non-decreasing-array.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {boolean}
4+
*/
5+
const checkPossibility = function(nums) {
6+
let count = 0;
7+
let idx;
8+
if (nums.length === 1) return true;
9+
for (let i = 1; i < nums.length; i++) {
10+
if (nums[i - 1] > nums[i]) {
11+
count++;
12+
idx = i;
13+
}
14+
}
15+
if (count > 1) return false;
16+
if (idx === nums.length - 1 || idx === 1) return true;
17+
return (
18+
Math.max(...nums.slice(0, idx - 1)) <= Math.min(...nums.slice(idx)) ||
19+
Math.max(...nums.slice(0, idx)) <= Math.min(...nums.slice(idx + 1))
20+
);
21+
};

0 commit comments

Comments
 (0)