101 Logo
onenoughtone

Code Implementation

Row Extractor Implementation

Below is the implementation of the row extractor:

solution.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* Generate Full Triangle Approach
* Time Complexity: O(rowIndex²) - We generate rowIndex rows with a total of O(rowIndex²) elements
* Space Complexity: O(rowIndex) - We only store the current row
*
* @param {number} rowIndex - The index of the row to return (0-indexed)
* @return {number[]} - The rowIndex-th row of Pascal's Triangle
*/
function getRowFullTriangle(rowIndex) {
// Initialize result with the first row
let result = [1];
// Generate each row based on the previous row
for (let i = 1; i <= rowIndex; i++) {
// Create a new row
const row = [1];
// Calculate middle elements
for (let j = 1; j < i; j++) {
row.push(result[j - 1] + result[j]);
}
// End with 1
row.push(1);
// Replace result with the new row
result = row;
}
return result;
}
/**
* Iterative In-Place Calculation Approach
* Time Complexity: O(rowIndex²) - We still perform calculations for each element
* Space Complexity: O(rowIndex) - We use a single array of size rowIndex+1
*
* @param {number} rowIndex - The index of the row to return (0-indexed)
* @return {number[]} - The rowIndex-th row of Pascal's Triangle
*/
function getRowInPlace(rowIndex) {
// Initialize array with all 1s
const row = new Array(rowIndex + 1).fill(1);
// Update the array in-place
for (let i = 2; i <= rowIndex; i++) {
// Update from right to left
for (let j = i - 1; j >= 1; j--) {
row[j] = row[j] + row[j - 1];
}
}
return row;
}
/**
* Mathematical Formula (Binomial Coefficients) Approach
* Time Complexity: O(rowIndex) - We calculate each element in the row once
* Space Complexity: O(rowIndex) - We use a single array of size rowIndex+1
*
* @param {number} rowIndex - The index of the row to return (0-indexed)
* @return {number[]} - The rowIndex-th row of Pascal's Triangle
*/
function getRow(rowIndex) {
// Initialize array
const row = new Array(rowIndex + 1);
// First element is always 1
row[0] = 1;
// Calculate remaining elements using the formula
for (let j = 1; j <= rowIndex; j++) {
row[j] = row[j - 1] * (rowIndex - j + 1) / j;
}
return row;
}
// Test cases
console.log(getRow(3)); // [1, 3, 3, 1]
console.log(getRow(0)); // [1]
console.log(getRow(1)); // [1, 1]
console.log(getRow(4)); // [1, 4, 6, 4, 1]

Step-by-Step Explanation

Let's break down the implementation:

  1. 1. Understand the Problem: First, understand that we need to return the rowIndex-th row of Pascal's Triangle, where rowIndex is 0-indexed.
  2. 2. Consider Space Optimization: Note the follow-up requirement to use only O(rowIndex) extra space, which means we should avoid generating the entire triangle.
  3. 3. Choose an Approach: We can either generate the full triangle and return the last row, calculate the row in-place, or use the mathematical formula for binomial coefficients.
  4. 4. Implement the Chosen Approach: For the in-place calculation, initialize an array of 1s and update it from right to left to avoid overwriting values before they're used.
  5. 5. Handle Edge Cases: Consider edge cases like rowIndex = 0 and rowIndex = 1, which should return [1] and [1,1] respectively.
  6. 6. Test the Solution: Verify the solution with different test cases, including the provided examples.
ProblemSolutionCode
101 Logo
onenoughtone