-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path119.pascals-triangle-ii.js
68 lines (67 loc) · 1.52 KB
/
119.pascals-triangle-ii.js
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
/*
* @lc app=leetcode id=119 lang=javascript
*
* [119] Pascal's Triangle II
*
* https://leetcode.com/problems/pascals-triangle-ii/description/
*
* algorithms
* Easy (47.98%)
* Likes: 738
* Dislikes: 193
* Total Accepted: 270.9K
* Total Submissions: 564.4K
* Testcase Example: '3'
*
* Given a non-negative index k where k ≤ 33, return the k^th 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?
*
*/
// @lc code=start
/**
* @param {number} rowIndex
* @return {number[]}
*/
var getRow = function (rowIndex) {
let resMap = {};
var getArr = function (row, col) {
let r;
if (col == 0 || row == col) {
//end condition
r = 1;
} else {
//looks magical right, because functional programing is more descriptive of the meaning instead of procedure like procedure programming
//so in more time you only need to translate it meaning, more easy to right it correctly
if (resMap[`${row},${col}`]) {
r = resMap[`${row},${col}`];
} else {
r = getArr(row - 1, col) + getArr(row - 1, col - 1);
}
}
resMap[`${row},${col}`] = r;
return r;
};
let arr = [];
for (let col = 0; col <= rowIndex; col++) {
arr.push(getArr(rowIndex, col));
}
return arr;
};
// @lc code=end