Skip to content

Commit b002cc9

Browse files
committed
⚡ [53] Trying ...
1 parent eae6ce4 commit b002cc9

File tree

2 files changed

+30
-19
lines changed

2 files changed

+30
-19
lines changed

53/my_solution.js

+30-19
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,38 @@ const maxSubArray = (nums) => {
99
if (tmpMax < 0) tmpMax = 0;
1010
tmpMax += nums[i];
1111
if (tmpMax > max) max = tmpMax;
12+
}
13+
14+
console.log("max")
15+
console.log(max)
16+
17+
return max;
18+
};
19+
20+
const maxSubArrayOld = (nums) => {
21+
let tmpMax = nums[0], max = nums[0];
1222

23+
for (let i = 1; i < nums.length; i++) {
1324
// if c > m, restart m
14-
// if (0 >= tmpMax && nums[i] > tmpMax) {
15-
// console.log(nums[i] + " is nbiggger!")
16-
// console.log(tmpMax)
17-
// console.log(tmpMax + nums[i])
18-
//
19-
// // when pre max and curr value sum is more than max, we restart max
20-
// if ((tmpMax + nums[i]) > max) { // c + m > max - restart the max
21-
// tmpMax = nums[i];
22-
// max = tmpMax;
23-
// } else { // but in the case where pre = -2 and current = - 1 and they are smaller than max, we reset the max if curr (tmpMax) is bigger than max, in this case -1 > -2, so replace.
24-
// console.log("WTF?")
25-
// tmpMax = nums[i];
26-
// if (tmpMax > max) max = tmpMax;
27-
//
28-
// }
29-
// } else {
30-
// tmpMax += nums[i];
31-
// if (tmpMax > max) max = tmpMax;
32-
// }
25+
if (0 >= tmpMax && nums[i] > tmpMax) {
26+
// console.log(nums[i] + " is nbiggger!")
27+
// console.log(tmpMax)
28+
// console.log(tmpMax + nums[i])
29+
30+
// when pre max and curr value sum is more than max, we restart max
31+
if ((tmpMax + nums[i]) > max) { // c + m > max - restart the max
32+
tmpMax = nums[i];
33+
max = tmpMax;
34+
} else { // but in the case where pre = -2 and current = - 1 and they are smaller than max, we reset the max if curr (tmpMax) is bigger than max, in this case -1 > -2, so replace.
35+
console.log("WTF?")
36+
tmpMax = nums[i];
37+
if (tmpMax > max) max = tmpMax;
38+
39+
}
40+
} else {
41+
tmpMax += nums[i];
42+
if (tmpMax > max) max = tmpMax;
43+
}
3344
}
3445

3546
console.log("max")

53/o_solution.js

Whitespace-only changes.

0 commit comments

Comments
 (0)