Skip to content

Commit

Permalink
246th Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Oct 29, 2024
1 parent 7bb772e commit 4b3eaf5
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 15 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,12 @@ Ace Coding Interview with 75 Qs
| 1137. N-th Tribonacci Number | [Solution][1137] | Easy |
| 746. Min Cost Climbing Stairs | [Solution][746] | Easy |
| 198. House Robber | [Solution][198] | Medium |
| 790. Domino and Tromino Tiling | Solution | Medium |
| 790. Domino and Tromino Tiling | [Solution][790] | Medium |

[1137]: ./src/page-11/1137.%20N-th%20Tribonacci%20Number/tribonacci.ts
[746]: ./src/page-7/746.%20Min%20Cost%20Climbing%20Stairs/minCostClimbingStairs.ts
[198]: ./src/page-2/198.%20House%20Robber/rob.ts
[790]: ./src/page-8/790.%20Domino%20and%20Tromino%20Tiling/numTilings.ts

| DP - Multidimensional | | |
| --------------------------------------------------------- | -------- | ------ |
Expand Down
20 changes: 6 additions & 14 deletions src/page-2/118. Pascal's Triangle/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,16 @@ type Generate = (numRows: number) => number[][];
* Accepted
*/
export const generate: Generate = (numRows) => {
if (numRows <= 0) return [];
const triangle: number[][] = [];

const triangle = [[1]]; // first row
for (let i = 0; i < numRows; i++) {
const row: number[] = Array(i + 1).fill(1);

if (numRows === 1) return triangle;

for (let i = 1; i < numRows; i++) {
const newRow = [1]; // first item of each row is 1
const prevRow = triangle[i - 1];

for (let j = 1; j <= i; j++) {
const prev = prevRow[j - 1];
const cur = prevRow[j] || 0; // last item plus 0

newRow.push(prev + cur);
for (let j = 1; j < i; j++) {
row[j] = triangle[i - 1][j - 1] + triangle[i - 1][j];
}

triangle.push(newRow);
triangle.push(row);
}

return triangle;
Expand Down
8 changes: 8 additions & 0 deletions src/page-8/790. Domino and Tromino Tiling/numTilings.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { numTilings } from './numTilings';

describe('790. Domino and Tromino Tiling', () => {
test('numTilings', () => {
expect(numTilings(3)).toBe(5);
expect(numTilings(1)).toBe(1);
});
});
22 changes: 22 additions & 0 deletions src/page-8/790. Domino and Tromino Tiling/numTilings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
type NumTilings = (n: number) => number;

/**
* Accepted
*/
export const numTilings: NumTilings = (n) => {
const MOD = 1_000_000_007;

// Create a DP array to store the number of ways
const dp: number[] = Array(n + 1).fill(0);
dp[0] = 1; // 1 way to tile a 2 x 0 board (empty board)
dp[1] = 1; // 1 way to tile a 2 x 1 board
dp[2] = 2; // 2 ways to tile a 2 x 2 board

// Fill the dp array for all values from 3 to n
for (let i = 3; i <= n; i++) {
dp[i] = (2 * dp[i - 1] + dp[i - 3]) % MOD;
}

// The result is stored in dp[n]
return dp[n];
};

0 comments on commit 4b3eaf5

Please sign in to comment.