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://en.wikipedia.org/wiki/Big_O_notation)이 됩니다. 이는 배열의 크기를 2배 늘리면 알고리즘은 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`가 음수가 된 경우는 `s`에 `0`을 할당하면 됩니다. `s`의 값 중 최댓값이 정답이 됩니다.
67
67
68
-
If the description is too vague, please see the code, it's short enough:
68
+
해설이 모호하다고 느껴지면 아래 코드를 참고해주세요.
69
69
70
70
```js run demo
71
71
functiongetMaxSubSum(arr) {
72
72
let maxSum =0;
73
73
let partialSum =0;
74
74
75
-
for (let item of arr) { //for each item of arr
76
-
partialSum += item; //add it to partialSum
77
-
maxSum =Math.max(maxSum, partialSum); //remember the maximum
78
-
if (partialSum <0) partialSum =0; //zero if negative
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
+
알고리즘에 대한 상세한 정보는 [최대합 부분 배열 문제](http://en.wikipedia.org/wiki/Maximum_subarray_problem)에서 찾을 수 있습니다. 동작원리에 대해 확실히 이해가 되지 않았다면 위 예제의 알고리즘이 어떻게 동작하는지 찬찬히 살펴보세요. 그릉ㄹ 읽는 것보다 코드를 살펴보는게 훨씬 도움이 될 겁니다.
Copy file name to clipboardExpand all lines: 1-js/05-data-types/04-array/5-array-input-sum/solution.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
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`로 입력받은 값을 숫자형으로 변경한 이후엔, 빈 문자열(정지 신호)을 0(유효한 숫자)과 구분할 수 없기 때문에, `prompt` 직후에 `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