You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The solution has a time complexety of [O(n<sup>2</sup>)](https://en.wikipedia.org/wiki/Big_O_notation). In other words, if we increase the array size 2 times, the algorithm will work 4 times longer.
60
+
Таке рішення має оцінку часу виконання [O(n<sup>2</sup>)](https://uk.wikipedia.org/wiki/Нотація_Ландау). Інакше кажучи, якщо ми збільшимо розмір масиву вдвічі, алгоритм буде виконуватися в 4 рази довше.
61
61
62
-
For big arrays (1000, 10000 or more items) such algorithms can lead to a serious sluggishness.
62
+
Для великих масивів (1000, 10000 або більше елементів) подібні алгоритми можуть призводити до серйозних "пригальмувань" роботі.
63
63
64
-
# Fast solution
64
+
# Швидке рішення
65
65
66
-
Let's walk the array and keep the current partial sum of elements in the variable `s`. If `s`becomes negative at some point, then assign `s=0`. The maximum of all such `s`will be the answer.
66
+
Пройдемося по масиву і в процесі будемо накопичувати проміжну суму елементів в змінній `s`. Якщо в певний момент `s`стане меншою за 0, присвоїмо `s=0`. Максимальне значення з усіх `s`і буде відповіддю.
67
67
68
-
If the description is too vague, please see the code, it's short enough:
68
+
Якщо пояснення не дуже зрозуміле, подивіться, будь ласка, на код - він досить лаконічний:
The algorithm requires exactly 1 array pass, so the time complexity is O(n).
92
+
Цей алгоритм потребує рівно один прохід по масиву, його оціночний час виконання - O(n).
93
93
94
-
You can find more detail information about the algorithm here: [Maximum subarray problem](http://en.wikipedia.org/wiki/Maximum_subarray_problem). If it's still not obvious why that works, then please trace the algorithm on the examples above, see how it works, that's better than any words.
94
+
Ви можете дізнатися більше про цей алгоритм тут: [Maximum subarray problem](http://en.wikipedia.org/wiki/Maximum_subarray_problem). Якщо досі не цілком зрозуміло, як це працює, будь ласка, подивіться алгоритм у прикладах вище, це буде краще за будь-які слова.
If all items are negative, it means that we take none (the subarray is empty), so the sum is zero:
24
+
Якщо всі елементи менші нуля, нічого не беремо, це означає, що підмасив пустий, а сума рівна нулю:
25
25
26
26
```js
27
27
getMaxSubSum([-1, -2, -3]) =0
28
28
```
29
29
30
-
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.
30
+
Будь ласка, подумайте над швидким рішенням: [O(n<sup>2</sup>)](https://uk.wikipedia.org/wiki/Нотація_Ландау) або навіть над рішенням O(n), якщо зможете.
Copy file name to clipboardExpand all lines: 1-js/05-data-types/04-array/5-array-input-sum/solution.md
+3-4
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
Please note the subtle, but important detail of the solution. We don't convert`value`to number instantly after`prompt`, because after `value = +value`we would not be able to tell an empty string (stop sign) from the zero (valid number). We do it later instead.
1
+
Зверніть увагу на одну важливу річ у вирішенні цієї задачі. Ми не конвертуємо`value`в число одразу після`prompt`, тому що одразу після операції `value = +value`ми не зможемо відрізнити порожній рядок (зупинення роботи функції) від нуля (дійсне число). Тому ми робимо це пізніше.
2
2
3
3
4
4
```js run demo
@@ -8,9 +8,9 @@ function sumInput() {
8
8
9
9
while (true) {
10
10
11
-
let value =prompt("A number please?", 0);
11
+
let value =prompt("Введіть, будь ласка, номер", 0);
12
12
13
-
//should we cancel?
13
+
//Обриваємо введення даних?
14
14
if (value ===""|| value ===null||!isFinite(value)) break;
0 commit comments