-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path119_Pascal's_Triangle_II.py
More file actions
45 lines (30 loc) · 906 Bytes
/
119_Pascal's_Triangle_II.py
File metadata and controls
45 lines (30 loc) · 906 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""
119. Pascal's Triangle II
Easy
462
164
Favorite
Share
Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle.
Note that the row index starts from 0.
In Pascal's triangle, each number is the sum of the two numbers directly above it.
Example:
Input: 3
Output: [1,3,3,1]
Follow up:
Could you optimize your algorithm to use only O(k) extra space?
"""
class Solution:
def getRow(self, rowIndex: int) -> List[int]:
result = [1] + [0]*rowIndex
for i in range(rowIndex):
result[0] = 1
for j in range(i+1,0,-1):
result[j] += result[j-1]
return result
"""
Success
Details
Runtime: 36 ms, faster than 85.58% of Python3 online submissions for Pascal's Triangle II.
Memory Usage: 13.1 MB, less than 66.21% of Python3 online submissions for Pascal's Triangle II.
"""