-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPascalsTriangleIi.ts
52 lines (45 loc) · 1.35 KB
/
PascalsTriangleIi.ts
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
// Source : https://leetcode.com/problems/pascals-triangle-ii/
// Author : squxq
// Date : 2023-11-05
/*****************************************************************************************************
*
* Given an integer rowIndex, return the rowIndex^th (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?
******************************************************************************************************/
// number -> number[]
// produce all the elements in the given row, rowIndex, of a Pascal's triangle without computing other elements of factorials
// stub:
/**
function getRow(rowIndex: number): number[] {return [])
*/
// template:
/**
function getRow(rowIndex: number): number[] {
return (... rowIndex)
}
*/
export function getRow(rowIndex: number): number[] {
const row: number[] = new Array(rowIndex + 1);
row[0] = 1;
for (let k: number = 1; k <= rowIndex; k++) {
row[k] = ((row[k - 1] as number) * (rowIndex + 1 - k)) / k;
}
return row;
}