translating
This commit is contained in:
parent
2b874a73be
commit
928cd2731b
165 changed files with 2046 additions and 2967 deletions
|
@ -2,29 +2,29 @@ importance: 2
|
|||
|
||||
---
|
||||
|
||||
# Подмассив наибольшей суммы
|
||||
# A maximal subarray
|
||||
|
||||
На входе массив чисел, например: `arr = [1, -2, 3, 4, -9, 6]`.
|
||||
The input is an array of numbers, e.g. `arr = [1, -2, 3, 4, -9, 6]`.
|
||||
|
||||
Задача -- найти непрерывный подмассив `arr`, сумма элементов которого максимальна.
|
||||
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 (сумма выделенных)
|
||||
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 (неотрицательные - берем всех)
|
||||
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
|
||||
```
|
||||
|
||||
Постарайтесь придумать решение, которое работает за O(n<sup>2</sup>), а лучше за O(n) операций.
|
||||
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