Skip to content

Commit 91de16a

Browse files
committed
✅ [1909] a5 - max edge + jumping comparison
1 parent 56e5b86 commit 91de16a

File tree

2 files changed

+46
-31
lines changed

2 files changed

+46
-31
lines changed

1909/my_solution.js

+26-28
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,44 @@
33
* @return {boolean}
44
*/
55
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;
716

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+
}
3030
}
31-
3231
}
3332

34-
console.log(tmpList)
35-
3633
return true;
3734
};
3835

3936

40-
let x = canBeIncreasing([10, 1, 2, 3]); // true
37+
// let x = canBeIncreasing([10, 1, 2, 3]); // true
4138
// let x = canBeIncreasing([2, 3, 1, 2]); // false
4239
// let x = canBeIncreasing([20, 10, 1, 2, 3]); // false
4340
// let x = canBeIncreasing([1, 2, 10, 5, 7]); // true
4441
// let x = canBeIncreasing([1, 2, 5, 7]); // true
45-
// let x = canBeIncreasing([1, 1, 1]); // false
42+
let x = canBeIncreasing([1, 1, 1]); // false
4643
// let x = canBeIncreasing([105, 924, 32, 968]); // true
4744
// let x = canBeIncreasing([512, 867, 904, 997, 403]); // true
45+
// let x = canBeIncreasing([541, 783, 433, 744]); // false
4846
console.log(x)

1909/solution.js

+20-3
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,28 @@
33
* @return {boolean}
44
*/
55
const canBeIncreasing = (nums) => {
6-
for (let i = 1; i < nums.length; i++) {
6+
let credit = 1, maxEdge = 0;
7+
8+
for (let i = 0; i < nums.length; i++) {
79
if (nums[i] >= nums[i + 1]) {
8-
if (undefined === nums[i - 1]) continue;
10+
if (0 === credit) return false;
11+
let tmpMaxEdge = nums[i];
12+
credit--;
13+
14+
if (tmpMaxEdge <= maxEdge) return false;
15+
16+
// if my within is not incline.
17+
if (nums[i - 1] >= nums[i + 1]) {
18+
if (undefined === nums[i + 2]) {
19+
continue;
20+
}
921

10-
return (nums[i + 1] > nums[i - 1]) || nums[i] < nums[i + 2] || i + 2 == nums.length;
22+
if (nums[i] >= nums[i + 2]) {
23+
return false;
24+
} else {
25+
i++; // Skip the next one.
26+
}
27+
}
1128
}
1229
}
1330

0 commit comments

Comments
 (0)