Skip to content

Commit ec57e61

Browse files
committed
53: partial
1 parent 0dded5f commit ec57e61

File tree

2 files changed

+67
-3
lines changed

2 files changed

+67
-3
lines changed

README.md

+67-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
Literally **THE MOST** important thing in doing Leetcodes is: **MAKING SURE YOU UNDERSTOOD THE TASK**. I spent so much time thinking about non-existing problems, just because I misread or misunderstood something. For many of you English is not the first language. I know you know English well tho. I also understand English well. But understanding English and perfectly understanding the algorithmic task you are to implement, can someone mean two different things. Go slowly, it's just you and the task. You got it.
22

3+
## Learning problems
4+
5+
Problems I want to describe in-depth, so that they will serve as examples for all the other ones.
6+
37
#### 9. Palindrome Number (Easy)
48

59
```python
@@ -61,8 +65,68 @@ class Solution(object):
6165

6266
**Rewritten instruction**: Find the maximum sum of consecutive elements of the array.
6367

64-
**Main intuition**: If the current element will make the sum of the rest elements of the array smaller, than it would be without it, then skip it. Our question is basically:
68+
##### Solution(s)
69+
70+
The first, obvious and with zero intuition, approach is to just double loop over the array elements. This works, but it's not good. Wanna guess the time complexity? You're right: $O(n^2)$.
71+
72+
**Why? Quicky on double loop time complexity:**
73+
74+
For each of the outer loops iteration $i$, we get $n-i$ inner loop iterations. Therefore the total of all iterations is:
75+
76+
$$S = (n-1) + (n-2) + (n-3) +...+1$$
77+
78+
Back to primary school: how to calculate the sum of arithmetic progressions? In general the arithmetic progression is something that starts at some $a_0$, has constant additive element $d$, and is bounded by the number of elements $n$ (it turns into arithmetic series when the number of elements is not bounded: $n \to \infty$).
79+
80+
**Derivation of the arithmetic progression sum formula:**
81+
82+
We will an example of the progression in a normal form, and reversed.
83+
84+
$$S_f = 1 + 2 + ... + (n-1) + n$$
85+
86+
$$S_r = n + (n-1) + ... + 2 + 1$$
87+
88+
Now eyeball it. First term on both, second term on both...
89+
90+
$$S_f + S_r = (n+1) + (n+1) + (n+1)... = 2S$$
91+
92+
Yeaaah, it's all coming together. How many terms is there: $n$. Each term is: $n+1$. So it's all just:
93+
94+
$$2S = n(n+1)$$
95+
96+
$$S = \frac{n(n + 1)}{2}$$
97+
98+
Now, being absolutely sure, we understand how many iteration we have down to atomic level, we can get back to how we come up with complexity $O(n^2)$.
99+
100+
We calculate the big-O complexity, by finding the dominating factor as $n$ grows towards infinity. If you ever calculated series sums. It's the same principle. You look for whatever term that outgrows others by orders of magnitude as $n$ grows. In this case, when we multiply the upper term:
101+
102+
$$S = \frac{n^2+n}{2}$$
103+
104+
Quadratic function grows faster than the linear one. Graphical way to understand why $n^2$ dominates for bigger $n$. You'd literally have to take something like $n=\frac{1}{3}$, for n to be bigger.
105+
106+
![alt text](images/complexity_n2_n.png)
107+
108+
Now, we can finally see the naive and bad solution, understanding why it's naive and bad.
109+
110+
```python
111+
def maxSubArray(self, nums):
112+
"""
113+
:type nums: List[int]
114+
:rtype: int
115+
"""
116+
top = float("-inf")
117+
acc = 0
118+
n = len(nums)
119+
120+
for i in range(n):
121+
acc = nums[i]
122+
if acc > top:
123+
top = acc
124+
for j in range(i+1, n):
125+
acc += nums[j]
126+
if acc > top:
127+
top = acc
128+
return top
129+
```
65130

66-
$$\text{sum(nums)}[i] + \text{sum(nums)}[i+1:] > \text{sum(nums)}[i+1:]$$
131+
Now remember the intuition I wrote above. That's why reading the instruction **very** slowly is so important. We do not have to iterate so many times.
67132

68-
Because remember, we only care about consecutive sums.

images/complexity_n2_n.png

29.8 KB
Loading

0 commit comments

Comments
 (0)