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
+26-28
Original file line number
Diff line number
Diff line change
@@ -3,35 +3,33 @@
3
3
* @return {number}
4
4
*/
5
5
constmaxSubArray=(nums)=>{
6
-
lettmpMax=0,max=0;
6
+
lettmpMax=nums[0],max=nums[0];
7
7
8
-
for(leti=0;i<nums.length;i++){
9
-
if(0===i){
10
-
tmpMax=nums[i];
11
-
max=tmpMax;
12
-
continue;
13
-
}
8
+
for(leti=1;i<nums.length;i++){
9
+
if(tmpMax<0)tmpMax=0;
10
+
tmpMax+=nums[i];
11
+
if(tmpMax>max)max=tmpMax;
14
12
15
13
// 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.
Copy file name to clipboardexpand all lines: 53/solution.js
+36-21
Original file line number
Diff line number
Diff line change
@@ -3,28 +3,43 @@
3
3
* @return {number}
4
4
*/
5
5
constmaxSubArray=(nums)=>{
6
-
lettmpMax=0,max=0;
7
-
8
-
for(leti=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
+
lettmpMax=nums[0],max=nums[0];
7
+
8
+
for(leti=1;i<nums.length;i++){
9
+
if(tmpMax<0)tmpMax=0;
10
+
tmpMax+=nums[i];
11
+
if(tmpMax>max)max=tmpMax;
27
12
}
28
13
29
14
returnmax;
30
15
};
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).
0 commit comments