Skip to content

Commit

Permalink
feat: Add solution for Pascal's Triangle problem
Browse files Browse the repository at this point in the history
  • Loading branch information
pragusga25 committed Jul 13, 2024
1 parent d542214 commit 3eae736
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions easy/pascals-triangle.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Problem: https://leetcode.com/problems/pascals-triangle/
// Doc: https://leetcode.com/problems/pascals-triangle/solutions/5471283/generating-pascal-s-triangle-efficiently/
const generate = (numRows: number): number[][] => {
const result = [[1]];
if (numRows === 1) return result;

for (let i = 1; i < numRows; i++) {
const prev = result[i - 1];
const row = [1];
for (let j = 0; j < prev.length - 1; j++) {
const l = prev[j];
const r = prev[j + 1];
row.push(l + r);
}
row.push(1);
result.push(row);
}

return result;
};

describe("Pascal's Triangle", () => {
it('should return the pascal triangle', () => {
expect(generate(5)).toEqual([
[1],
[1, 1],
[1, 2, 1],
[1, 3, 3, 1],
[1, 4, 6, 4, 1],
]);
expect(generate(1)).toEqual([[1]]);
});
});

0 comments on commit 3eae736

Please sign in to comment.