https://www.codewars.com/kata/54521e9ec8e60bc4de000d6c
Codewars - Achieve mastery through coding practice and developer mentorship
A coding practice website for all programming levels – Join a community of over 3 million developers and improve your coding skills in over 55 programming languages!
www.codewars.com
DESCRIPTION:
The maximum sum subarray problem consists in finding the maximum sum of a contiguous subsequence in an array or list of integers:
maxSequence([-2, 1, -3, 4, -1, 2, 1, -5, 4])
// should be 6: [4, -1, 2, 1]
Easy case is when the list is made up of only positive numbers and the maximum sum is the sum of the whole array. If the list is made up of only negative numbers, return 0 instead.
Empty list is considered to have zero greatest sum. Note that the empty list or array is also a valid sublist/subarray.
코드
var maxSequence = function(arr){
let max = sum = 0;
for(let i = 0; i < arr.length; i++){
sum = Math.max(arr[i], sum + arr[i]);
max = Math.max(max, sum);
}
return max;
}
'알고리즘 > Codewars' 카테고리의 다른 글
[Codewars] Get Planet Name By ID (8 kyu) / JavaScript (0) | 2023.04.16 |
---|---|
[Codewars] Fix your code before the garden dies! (8 kyu) / JavaScript (0) | 2023.04.16 |
[Codewars] What is between? (8 kyu) / JavaScript (0) | 2023.04.14 |
[Codewars] Reverse List Order (8 kyu) / JavaScript (0) | 2023.04.13 |
[Codewars] Student's Final Grade (8 kyu) / JavaScript (0) | 2023.04.13 |
댓글