Skip to content

Commit c4f9566

Browse files
committed
⚡ [1909] a3
1 parent 3cbcd86 commit c4f9566

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

1909/my_solution.js

+28-11
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,43 @@
33
* @return {boolean}
44
*/
55
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+
}
1429
} else {
15-
return false;
30+
prev = nums[i];
1631
}
1732
}
1833

1934
return true;
2035
};
2136

2237

38+
// let x = canBeIncreasing([10, 1, 2, 3]); // true
39+
// let x = canBeIncreasing([20, 10, 1, 2, 3]); // false
2340
// 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
2643
// let x = canBeIncreasing([1, 1, 1]); // false
2744
// let x = canBeIncreasing([105, 924, 32, 968]); // true
2845
// let x = canBeIncreasing([512, 867, 904, 997, 403]); // true

0 commit comments

Comments
 (0)