Here are two pieces of codes, both in python. They are doing the same thing. However, the first one is time-limit-exceeded, while the second one doesn't. I am confused....Anyone can help me? The only difference is that in Code 1, it defines a global variable.

Code 1

```
class Solution(object):
_res = [0]
def numSquares(self, n):
"""
:type n: int
:rtype: int
"""
res = self._res
while len(res) <=n:
res += [min([res[-i*i] for i in range(1,int(len(res)**0.5)+1)]) +1]
return res[n]
```

Code 2

```
class Solution(object):
def numSquares(self, n):
"""
:type n: int
:rtype: int
"""
res = [0]
while len(res) <=n:
res += [min([res[-i*i] for i in range(1,int(len(res)**0.5)+1)]) +1]
return res[n]
```