|
3 | 3 | * @return {boolean}
|
4 | 4 | */
|
5 | 5 | const canBeIncreasing = (nums) => {
|
6 |
| - for (let i = 1; i < nums.length; i++) { |
7 |
| - console.log(`-> nums[i]:${nums[i]}`) |
8 |
| - if ( |
9 |
| - (nums[i] > nums[i - 1] && nums[i] > nums[i + 1]) || |
10 |
| - (nums[i] < nums[i - 1] && nums[i] < nums[i + 1]) || |
11 |
| - (nums[i] === nums[i - 1] && nums[i] < nums[i + 1]) || |
12 |
| - (nums[i] > nums[i - 1] && nums[i] < nums[i + 1]) |
13 |
| - ) { |
| 6 | + let prev = -1, nbToRemove = 1; |
| 7 | + |
| 8 | + for (let i = 0; i < nums.length; i++) { |
| 9 | + console.log(`-> nums[i]:${nums[i]}, prev:${prev}, nbToRemove:${nbToRemove}`); |
| 10 | + |
| 11 | + if (nums[i] <= prev) { |
| 12 | + console.log(`--> i:${i} -> ${nums[i]} <= ${prev}`); |
| 13 | + if (nbToRemove === 1) { |
| 14 | + console.log(`--> removing -> ${(nums[i - 2] || 0)} > ${(nums[i - 1] || 0)}`); |
| 15 | + nbToRemove--; |
| 16 | + if ((nums[i - 2] || 0) > (nums[i - 1] || 0)) { |
| 17 | + console.log(`--> A`); |
| 18 | + prev = nums[i - 1]; |
| 19 | + // prev = nums[i] |
| 20 | + } else { |
| 21 | + console.log(`--> B`); |
| 22 | + prev = nums[i] |
| 23 | + continue; |
| 24 | + } |
| 25 | + } else { |
| 26 | + console.log(`--> false`); |
| 27 | + return false; |
| 28 | + } |
14 | 29 | } else {
|
15 |
| - return false; |
| 30 | + prev = nums[i]; |
16 | 31 | }
|
17 | 32 | }
|
18 | 33 |
|
19 | 34 | return true;
|
20 | 35 | };
|
21 | 36 |
|
22 | 37 |
|
| 38 | +// let x = canBeIncreasing([10, 1, 2, 3]); // true |
| 39 | +// let x = canBeIncreasing([20, 10, 1, 2, 3]); // false |
23 | 40 | // let x = canBeIncreasing([1, 2, 10, 5, 7]); // true
|
24 |
| -let x = canBeIncreasing([1, 2, 5, 7]); // true |
25 |
| -// let x = canBeIncreasing([2, 3, 1, 2]); // false |
| 41 | +// let x = canBeIncreasing([1, 2, 5, 7]); // true |
| 42 | +let x = canBeIncreasing([2, 3, 1, 2]); // false |
26 | 43 | // let x = canBeIncreasing([1, 1, 1]); // false
|
27 | 44 | // let x = canBeIncreasing([105, 924, 32, 968]); // true
|
28 | 45 | // let x = canBeIncreasing([512, 867, 904, 997, 403]); // true
|
|
0 commit comments