Skip to content

Commit eae6ce4

Browse files
committed
✨ [53] a2 - Runtime 80 ms Beats 66.33% Memory 50.3 MB Beats 59.37%
1 parent 854c02d commit eae6ce4

File tree

2 files changed

+62
-49
lines changed

2 files changed

+62
-49
lines changed

53/my_solution.js

+26-28
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,33 @@
33
* @return {number}
44
*/
55
const maxSubArray = (nums) => {
6-
let tmpMax = 0, max = 0;
6+
let tmpMax = nums[0], max = nums[0];
77

8-
for (let i = 0; i < nums.length; i++) {
9-
if (0 === i) {
10-
tmpMax = nums[i];
11-
max = tmpMax;
12-
continue;
13-
}
8+
for (let i = 1; i < nums.length; i++) {
9+
if (tmpMax < 0) tmpMax = 0;
10+
tmpMax += nums[i];
11+
if (tmpMax > max) max = tmpMax;
1412

1513
// if c > m, restart m
16-
if (0 >= tmpMax && nums[i] > tmpMax) {
17-
console.log(nums[i] + " is nbiggger!")
18-
console.log(tmpMax)
19-
console.log(tmpMax + nums[i])
20-
21-
// when pre max and curr value sum is more than max, we restart max
22-
if ((tmpMax + nums[i]) > max) { // c + m > max - restart the max
23-
tmpMax = nums[i];
24-
max = tmpMax;
25-
} 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.
26-
console.log("WTF?")
27-
tmpMax = nums[i];
28-
if (tmpMax > max) max = tmpMax;
29-
30-
}
31-
} else {
32-
tmpMax += nums[i];
33-
if (tmpMax > max) max = tmpMax;
34-
}
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+
// }
3533
}
3634

3735
console.log("max")
@@ -40,10 +38,10 @@ const maxSubArray = (nums) => {
4038
return max;
4139
};
4240

43-
// maxSubArray([-2, 1, -3, 4, -1, 2, 1, -5, 4])
41+
maxSubArray([-2, 1, -3, 4, -1, 2, 1, -5, 4])
4442
// maxSubArray([5, 4, -1, 7, 8])
4543
// maxSubArray([2, 5, 9, 2, 6, 8, -10])
4644
// maxSubArray([-2, -1])
4745
// maxSubArray([1, -1])
4846
// maxSubArray([-11, -1])
49-
maxSubArray([3, -3, 2, -3]) // 3
47+
// maxSubArray([3, -3, 2, -3]) // 3

53/solution.js

+36-21
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,43 @@
33
* @return {number}
44
*/
55
const maxSubArray = (nums) => {
6-
let tmpMax = 0, max = 0;
7-
8-
for (let i = 0; i < nums.length; i++) {
9-
if (0 === i) {
10-
tmpMax = nums[i];
11-
max = tmpMax;
12-
continue;
13-
}
14-
15-
if (0 >= tmpMax && nums[i] > tmpMax) {
16-
if ((tmpMax + nums[i]) > max) {
17-
tmpMax = nums[i];
18-
max = tmpMax;
19-
} else {
20-
tmpMax = nums[i];
21-
if (tmpMax > max) max = tmpMax;
22-
}
23-
} else {
24-
tmpMax += nums[i];
25-
if (tmpMax > max) max = tmpMax;
26-
}
6+
let tmpMax = nums[0], max = nums[0];
7+
8+
for (let i = 1; i < nums.length; i++) {
9+
if (tmpMax < 0) tmpMax = 0;
10+
tmpMax += nums[i];
11+
if (tmpMax > max) max = tmpMax;
2712
}
2813

2914
return max;
3015
};
16+
17+
/*
18+
19+
2nd approach
20+
21+
Time Complexity:
22+
The code uses a loop to iterate over the input array nums, starting from index 1. Inside the loop, there are some conditional statements and arithmetic operations that take constant time. Therefore, the time complexity of the code is determined by the loop itself.
23+
24+
The loop iterates through the array once, from index 1 to the end. Hence, the time complexity is O(n-1), which simplifies to O(n), where n is the length of the input array nums.
25+
26+
Space Complexity:
27+
The code uses a constant amount of additional space to store variables tmpMax and max. Regardless of the size of the input array, the space complexity remains constant. Therefore, the space complexity of the code is O(1).
28+
29+
In summary, the time complexity of the code is O(n), and the space complexity is O(1).
30+
31+
*/
32+
33+
/*
34+
35+
1st approach
36+
37+
Time Complexity:
38+
The code uses a loop to iterate over the input array nums. Inside the loop, there are some conditional statements and arithmetic operations. Since the operations inside the loop take constant time, the time complexity of the code is determined by the loop itself. Therefore, the time complexity of the code is O(n), where n is the length of the input array nums.
39+
40+
Space Complexity:
41+
The code uses a constant amount of additional space to store variables tmpMax and max. Regardless of the size of the input array, the space complexity remains constant. Therefore, the space complexity of the code is O(1).
42+
43+
In summary, the time complexity of the code is O(n), and the space complexity is O(1).
44+
45+
*/

0 commit comments

Comments
 (0)