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