work
This commit is contained in:
parent
1bffa43db4
commit
057783d216
373 changed files with 203 additions and 190 deletions
|
@ -1,30 +0,0 @@
|
|||
importance: 2
|
||||
|
||||
---
|
||||
|
||||
# A maximal subarray
|
||||
|
||||
The input is an array of numbers, e.g. `arr = [1, -2, 3, 4, -9, 6]`.
|
||||
|
||||
The task is: find the contiguous subarray of `arr` with the maximal sum of items.
|
||||
|
||||
Write the function `getMaxSubSum(arr)` that will find return that sum.
|
||||
|
||||
For instance:
|
||||
|
||||
```js
|
||||
getMaxSubSum([-1, *!*2, 3*/!*, -9]) = 5 (the sum of highlighted items)
|
||||
getMaxSubSum([*!*2, -1, 2, 3*/!*, -9]) = 6
|
||||
getMaxSubSum([-1, 2, 3, -9, *!*11*/!*]) = 11
|
||||
getMaxSubSum([-2, -1, *!*1, 2*/!*]) = 3
|
||||
getMaxSubSum([*!*100*/!*, -9, 2, -3, 5]) = 100
|
||||
getMaxSubSum([*!*1, 2, 3*/!*]) = 6 (take all)
|
||||
```
|
||||
|
||||
If all items are negative, it means that we take none (the subarray is empty), so the sum is zero:
|
||||
|
||||
```js
|
||||
getMaxSubSum([-1, -2, -3]) = 0
|
||||
```
|
||||
|
||||
Please try to think of a fast solution: [O(n<sup>2</sup>)](https://en.wikipedia.org/wiki/Big_O_notation) or even O(n) if you can.
|
Loading…
Add table
Add a link
Reference in a new issue