Skip to content

Commit 1c9b65c

Browse files
committed
max sub subarray another implementation
1 parent 85bb700 commit 1c9b65c

19 files changed

+44
-13
lines changed

.vscode/settings.json

100644100755
File mode changed.

Max_Average_Subarray-2.js

100644100755
File mode changed.

Medium/3-Sum/3-Sum-2nd-Slightly-Less-Than-2-Pointer.js

100644100755
File mode changed.

Medium/3-Sum/3-Sum-3rd-2-Pointer-Best.js

100644100755
File mode changed.

Medium/3-Sum/3-Sum-Brute-Force.js

100644100755
File mode changed.

Medium/3-Sum/3-Sum-Combined-Performance-Test.js

100644100755
File mode changed.

RangeSumQuery.js

100644100755
File mode changed.

isSuperUgly.js

100644100755
File mode changed.

isUgly-Find-nth-Ugly.js

100644100755
File mode changed.

isUgly.js

100644100755
File mode changed.

license-Key-Formatting.js

100644100755
File mode changed.

longest_substring_without_repeating_char.js

100644100755
File mode changed.

max-contiguous-subarray-general-Solution.js

100644100755
Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
1-
/* SOLUTION-2 - General Maximum subarray problem - NOTE - This solution does not considere any conditon of that the final sub-arraay can NOT include any negaive numbers. In othere words, the its the final contiguous can have negative numbers in it.
1+
/* SOLUTION-2 - General Maximum subarray problem - NOTE - This solution does not consider any condition of that the final sub-array can NOT include any negaive numbers. In othere words, the its the final contiguous can have negative numbers in it.
22
33
Find the contiguous subarray within a one-dimensional array of numbers which has the largest sum. For example, for the sequence of values −2, 1, −3, 4, −1, 2, 1, −5, 4; the contiguous subarray with the largest sum is 4, −1, 2, 1, with sum 6. */
44

55
maxContiguousSubArray = arr => {
6-
7-
let globalMax = 0,
6+
let globalMax = 0,
87
currentMax = 0;
98

10-
for(let i = 0; i < arr.length ; i++) {
11-
currentMax = Math.max(currentMax+arr[i], arr[i]);
12-
// console.log(currentMax); // this line is only for my own debugging
13-
globalMax = Math.max(globalMax, currentMax);
14-
}
15-
return globalMax;
16-
}
9+
for (let i = 0; i < arr.length; i++) {
10+
currentMax = Math.max(currentMax + arr[i], arr[i]);
11+
// console.log(currentMax); // this line is only for my own debugging
12+
globalMax = Math.max(globalMax, currentMax);
13+
}
14+
return globalMax;
15+
};
1716

1817
let myArr = [-2, 1, -3, 4, -1, 2, 1, -5, 4]; // => 6
1918

2019
console.log(maxContiguousSubArray(myArr));
2120

22-
23-
/*Explanation
24-
A> currentMax = Math.max(currentMax+arr[i], arr[i]) => This line effectively implements the requirement that the sub-array should be contguous.
21+
/*Explanation
22+
A> currentMax = Math.max(currentMax+arr[i], arr[i]) => This line effectively implements the requirement that the sub-array should be contguous.
2523
2624
It adds the current index elements with the positive summation of previous contiguous elemtents. So, it will sometime become negative if the current index no is a large negative no.
2725

max-sum-subarray.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* It is an adaptation of Kadane's algorithm;
2+
It has O(n) linear complexity;
3+
Using 'temp' and 'result' objects results in high readability and more concise code. */
4+
5+
const maxSequence = arr => {
6+
7+
const allPositives = arr => arr.every(n => n > 0);
8+
const allNegatives = arr => arr.every(n => n < 0);
9+
10+
if(arr.length === 0 || allNegatives(arr)) return 0;
11+
12+
const temp = { start: 0, sum: 0 };
13+
let result = { start: 0, end: 0, sum: 0 };
14+
15+
for (let i = 0; i < arr.length; i++) {
16+
temp.sum += arr[i];
17+
18+
if (temp.sum > result.sum) {
19+
result = { start: temp.start, end: i, sum: temp.sum };
20+
}
21+
22+
if (temp.sum < 0) {
23+
temp.sum = 0;
24+
temp.start = i + 1;
25+
}
26+
}
27+
28+
return result;
29+
};
30+
31+
console.log(maxSequence([-2, -1, -3, -4, -1, -2, -1, -5, -4])); // 0
32+
console.log(maxSequence([])); // 0
33+
console.log(maxSequence([2, 1, 3, 4, 1, 2, 1, 5, 4])); // { start: 0, end: 8, sum: 23 }

product-of-Array-Except-Self-GOOD-LONG-EXPLANATION.js

100644100755
File mode changed.

product-of-Array-Except-Self-without-constraint.JS

100644100755
File mode changed.

self-dividing-number.js

100644100755
File mode changed.

shortest-distance-to-a-character.js

100644100755
File mode changed.

string-to-integer-atoi-2.js

100644100755
File mode changed.

0 commit comments

Comments
 (0)