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). بمعنى آخر ، إذا قمنا بزيادة حجم المصفوفه مرتين ، فستعمل الخوارزمية لفترة أطول 4 مرات.
61
+
بالنسبة للمصفوفات الكبيرة (1000 أو 10000 أو أكثر من العناصر) ، يمكن أن تؤدي هذه الخوارزميات إلى بطء خطير.
61
62
62
-
For big arrays (1000, 10000 or more items) such algorithms can lead to a serious sluggishness.
63
+
# الحل الأسرع
63
64
64
-
# Fast solution
65
+
دعنا نسير في المصفوفة ونحتفظ بالمجموع الجزئي الحالي للعناصر في المتغير `s`. إذا أصبحت `s` سالبة في وقت ما ، قم بتعيين` s = 0`. سيكون الحد الأقصى لكل هذه الإجابات هو الإجابة.
65
66
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.
67
-
68
-
If the description is too vague, please see the code, it's short enough:
67
+
إذا كان الوصف غامضًا جدًا ، فيرجى الاطلاع على الكود ، فهو قصير بما يكفي:
69
68
70
69
```js run demo
71
70
functiongetMaxSubSum(arr) {
72
71
let maxSum =0;
73
72
let partialSum =0;
74
73
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
74
+
for (let item of arr) { //لكل عنصر في المصفوفه
75
+
partialSum += item; //أضفه إلى مجموع الجزئي
76
+
maxSum =Math.max(maxSum, partialSum); //تذكر الحد الأقصى
77
+
if (partialSum <0) partialSum =0; //صفر إذا كانت سلبية
The algorithm requires exactly 1 array pass, so the time complexity is O(n).
91
+
تتطلب الخوارزمية تمريراً مصفوفه واحده ، لذا فإن تعقيد الوقت هو O (n).
93
92
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.
93
+
يمكنك العثور على مزيد من المعلومات التفصيلية حول الخوارزمية هنا: [Maximum subarray problem](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`الي رقم فورا بعد`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(" رقم من فضلك A Number Please", 0);
12
12
13
-
//should we cancel?
13
+
//يجب أن نلغي؟
14
14
if (value ===""|| value ===null||!isFinite(value)) break;
0 commit comments