-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_solution.js
46 lines (42 loc) · 1.39 KB
/
my_solution.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
* @param {number[]} nums
* @return {boolean}
*/
const canBeIncreasing = (nums) => {
let credit = 1, maxEdge = 0;
console.log(nums)
for (let i = 0; i < nums.length; i++) {
if (nums[i] >= nums[i + 1]) {
if (0 === credit) return false;
let tmpMaxEdge = nums[i];
credit--;
console.log(`-> nums[i]:${nums[i]}, nums[i + 1]:${nums[i + 1]}, credit:${credit}, maxEgde:${maxEdge}`);
// new edge is lower means it is going down
if (tmpMaxEdge <= maxEdge) return false;
// if my within is not incline.
if (nums[i - 1] >= nums[i + 1]) {
if (undefined === nums[i + 2]) {
console.log("No more to iterate")
continue;
// return false; // No more to iterate
}
if (nums[i] >= nums[i + 2]) {
return false;
} else {
i++; // Skip the next one.
}
}
}
}
return true;
};
// let x = canBeIncreasing([10, 1, 2, 3]); // true
// let x = canBeIncreasing([2, 3, 1, 2]); // false
// let x = canBeIncreasing([20, 10, 1, 2, 3]); // false
// let x = canBeIncreasing([1, 2, 10, 5, 7]); // true
// let x = canBeIncreasing([1, 2, 5, 7]); // true
let x = canBeIncreasing([1, 1, 1]); // false
// let x = canBeIncreasing([105, 924, 32, 968]); // true
// let x = canBeIncreasing([512, 867, 904, 997, 403]); // true
// let x = canBeIncreasing([541, 783, 433, 744]); // false
console.log(x)