-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path119.pascals-triangle-ii.py
74 lines (49 loc) · 1.58 KB
/
119.pascals-triangle-ii.py
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from lc import *
class Solution:
def getRow(self, rowIndex: int) -> List[int]:
dp = [1]*(rowIndex+1)
for i in range(1, len(dp)):
dp[i] = dp[i-1] * (rowIndex - i + 1) // i
return dp
class Solution:
def getRow(self, i: int) -> List[int]:
f=factorial;return[f(i)//f(x)//f(i-x)for x in range(i+1)]
# O(n)
class Solution:
def getRow(self, i: int) -> List[int]:
return[a:=1]+[a:=a*(i-k)//(k+1)for k in range(i)]
# O(n) 2x faster
class Solution:
def getRow(self, i: int) -> List[int]:
r=[a:=1]+[a:=a*(i-k)//(k+1)for k in range(i>>1)];return r+r[::-1][1^i&1:]
# https://leetcode.com/problems/pascals-triangle-ii/discuss/207686/One-Line-Python-using-numpy
from numpy.polynomial.polynomial import polypow
class Solution(object):
def getRow(self, i: int) -> List[int]:
return[int(x)for x in polypow((1,1),i)]
# https://leetcode.com/problems/pascals-triangle-ii/discuss/788411/One-Line-Python-Using-Math
class Solution:
def getRow(self, i: int) -> List[int]:
return[comb(i,x)for x in range(i+1)]
test('''
119. Pascal's Triangle II
Easy
4187
312
Add to List
Share
Given an integer rowIndex, return the rowIndexth (0-indexed) row of the Pascal's triangle.
In Pascal's triangle, each number is the sum of the two numbers directly above it as shown:
Example 1:
Input: rowIndex = 3
Output: [1,3,3,1]
Example 2:
Input: rowIndex = 0
Output: [1]
Example 3:
Input: rowIndex = 1
Output: [1,1]
Constraints:
0 <= rowIndex <= 33
Follow up: Could you optimize your algorithm to use only O(rowIndex) extra space?
''')