You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: 53/my_solution.js
+30-19
Original file line number
Diff line number
Diff line change
@@ -9,27 +9,38 @@ const maxSubArray = (nums) => {
9
9
if(tmpMax<0)tmpMax=0;
10
10
tmpMax+=nums[i];
11
11
if(tmpMax>max)max=tmpMax;
12
+
}
13
+
14
+
console.log("max")
15
+
console.log(max)
16
+
17
+
returnmax;
18
+
};
19
+
20
+
constmaxSubArrayOld=(nums)=>{
21
+
lettmpMax=nums[0],max=nums[0];
12
22
23
+
for(leti=1;i<nums.length;i++){
13
24
// 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.
0 commit comments