|
3 | 3 | * @return {boolean}
|
4 | 4 | */
|
5 | 5 | const canBeIncreasing = (nums) => {
|
6 |
| - let nbToRemove = 1; |
| 6 | + let credit = 1, maxEdge = 0; |
| 7 | + console.log(nums) |
| 8 | + for (let i = 0; i < nums.length; i++) { |
| 9 | + if (nums[i] >= nums[i + 1]) { |
| 10 | + if (0 === credit) return false; |
| 11 | + let tmpMaxEdge = nums[i]; |
| 12 | + credit--; |
| 13 | + console.log(`-> nums[i]:${nums[i]}, nums[i + 1]:${nums[i + 1]}, credit:${credit}, maxEgde:${maxEdge}`); |
| 14 | + // new edge is lower means it is going down |
| 15 | + if (tmpMaxEdge <= maxEdge) return false; |
7 | 16 |
|
8 |
| - let curr = -Infinity, tmpList = []; |
9 |
| - console.log(curr) |
10 |
| - // tmpList.push(curr); |
11 |
| - |
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(); |
26 |
| - } else { |
27 |
| - console.log(`tmping`) |
28 |
| - // tmpList.push(next); |
29 |
| - curr = next; |
| 17 | + // if my within is not incline. |
| 18 | + if (nums[i - 1] >= nums[i + 1]) { |
| 19 | + if (undefined === nums[i + 2]) { |
| 20 | + console.log("No more to iterate") |
| 21 | + continue; |
| 22 | + // return false; // No more to iterate |
| 23 | + } |
| 24 | + if (nums[i] >= nums[i + 2]) { |
| 25 | + return false; |
| 26 | + } else { |
| 27 | + i++; // Skip the next one. |
| 28 | + } |
| 29 | + } |
30 | 30 | }
|
31 |
| - |
32 | 31 | }
|
33 | 32 |
|
34 |
| - console.log(tmpList) |
35 |
| - |
36 | 33 | return true;
|
37 | 34 | };
|
38 | 35 |
|
39 | 36 |
|
40 |
| -let x = canBeIncreasing([10, 1, 2, 3]); // true |
| 37 | +// let x = canBeIncreasing([10, 1, 2, 3]); // true |
41 | 38 | // let x = canBeIncreasing([2, 3, 1, 2]); // false
|
42 | 39 | // let x = canBeIncreasing([20, 10, 1, 2, 3]); // false
|
43 | 40 | // let x = canBeIncreasing([1, 2, 10, 5, 7]); // true
|
44 | 41 | // let x = canBeIncreasing([1, 2, 5, 7]); // true
|
45 |
| -// let x = canBeIncreasing([1, 1, 1]); // false |
| 42 | +let x = canBeIncreasing([1, 1, 1]); // false |
46 | 43 | // let x = canBeIncreasing([105, 924, 32, 968]); // true
|
47 | 44 | // let x = canBeIncreasing([512, 867, 904, 997, 403]); // true
|
| 45 | +// let x = canBeIncreasing([541, 783, 433, 744]); // false |
48 | 46 | console.log(x)
|
0 commit comments