-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
80 lines (73 loc) · 1.82 KB
/
main.cpp
File metadata and controls
80 lines (73 loc) · 1.82 KB
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
75
76
77
78
79
80
// Source: https://leetcode.com/problems/pascals-triangle-ii
// Title: Pascal's Triangle II
// Difficulty: Easy
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 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:
// https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif
//
// **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?
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <utility>
#include <vector>
using namespace std;
// DP
class Solution {
public:
vector<int> getRow(int rowIndex) {
auto prevRow = vector<int>(rowIndex + 1);
auto currRow = vector<int>(rowIndex + 1);
currRow[0] = 1;
for (int r = 1; r <= rowIndex; ++r) {
swap(currRow, prevRow);
currRow[0] = 1;
currRow[r] = 1;
for (int c = 1; c < r; ++c) {
currRow[c] = prevRow[c - 1] + prevRow[c];
}
}
return currRow;
}
};
// DP, single array
class Solution2 {
public:
vector<int> getRow(int rowIndex) {
auto row = vector<int>(rowIndex + 1);
row[0] = 1;
for (int r = 1; r <= rowIndex; ++r) {
for (int c = r; c > 0; --c) {
row[c] += row[c - 1];
}
}
return row;
}
};