Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 671c504

Browse files
Diego Flores CastilloDiego Flores Castillo
Diego Flores Castillo
authored and
Diego Flores Castillo
committed
maximunSubarray-008 (feature): implement maximunSubarray
1 parent f0352ba commit 671c504

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

.idea/workspace.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

LeetcodeProblems/Algorithms/Maximun_Subarray.js

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,31 @@ Follow up:
1414
1515
*/
1616

17-
var maxSubArray = function (nums) {
18-
if (nums.length == 0) return 0;
19-
var maxSub = nums[0];
20-
var currentMax = nums[0];
21-
22-
for (var i = 1; i < nums.length; i++) {
23-
currentMax = max(nums[i], currentMax + nums[i]);
24-
if (currentMax > maxSub) maxSub = currentMax;
17+
const maxSubArray = (nums) => {
18+
const numsQuantity = nums.length;
19+
20+
if (numsQuantity === 0) return 0;
21+
if (numsQuantity === 1) return nums[0];
22+
let amount = nums[0];
23+
for (let i = 0; i < numsQuantity; i++) {
24+
for (let j = i + 1; j < numsQuantity; j++) {
25+
let result = sum(i, j, nums);
26+
if (result > amount) {
27+
amount = result;
28+
}
29+
}
2530
}
2631

27-
return maxSub;
32+
return amount;
2833
};
2934

30-
var max = function (i, j) {
31-
return i > j ? i : j;
35+
const sum = (start, finish, nums) => {
36+
let sum = 0;
37+
for (let i = start; i <= finish; i++) {
38+
sum = nums[i] + sum;
39+
}
40+
return sum;
41+
3242
};
3343

3444
module.exports.maxSubArray = maxSubArray;

0 commit comments

Comments
 (0)