Skip to content

Commit 56e5b86

Browse files
committed
⚡ [1909] a4 - 2 pointer
1 parent c4f9566 commit 56e5b86

File tree

1 file changed

+26
-24
lines changed

1 file changed

+26
-24
lines changed

1909/my_solution.js

+26-24
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,45 @@
33
* @return {boolean}
44
*/
55
const canBeIncreasing = (nums) => {
6-
let prev = -1, nbToRemove = 1;
6+
let nbToRemove = 1;
77

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);
1011

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();
2926
} else {
30-
prev = nums[i];
27+
console.log(`tmping`)
28+
// tmpList.push(next);
29+
curr = next;
3130
}
31+
3232
}
3333

34+
console.log(tmpList)
35+
3436
return true;
3537
};
3638

3739

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
3942
// let x = canBeIncreasing([20, 10, 1, 2, 3]); // false
4043
// let x = canBeIncreasing([1, 2, 10, 5, 7]); // true
4144
// let x = canBeIncreasing([1, 2, 5, 7]); // true
42-
let x = canBeIncreasing([2, 3, 1, 2]); // false
4345
// let x = canBeIncreasing([1, 1, 1]); // false
4446
// let x = canBeIncreasing([105, 924, 32, 968]); // true
4547
// let x = canBeIncreasing([512, 867, 904, 997, 403]); // true

0 commit comments

Comments
 (0)