From 4b3eaf5d392d5b675a6afe33b1203f4e239cae97 Mon Sep 17 00:00:00 2001 From: Shyam-Chen Date: Tue, 29 Oct 2024 10:11:21 +0800 Subject: [PATCH] 246th Commit --- README.md | 3 ++- src/page-2/118. Pascal's Triangle/generate.ts | 20 +++++------------ .../numTilings.test.ts | 8 +++++++ .../numTilings.ts | 22 +++++++++++++++++++ 4 files changed, 38 insertions(+), 15 deletions(-) create mode 100644 src/page-8/790. Domino and Tromino Tiling/numTilings.test.ts create mode 100644 src/page-8/790. Domino and Tromino Tiling/numTilings.ts diff --git a/README.md b/README.md index 034e33c..c501592 100644 --- a/README.md +++ b/README.md @@ -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 | | | | --------------------------------------------------------- | -------- | ------ | diff --git a/src/page-2/118. Pascal's Triangle/generate.ts b/src/page-2/118. Pascal's Triangle/generate.ts index f5b25a0..912e348 100644 --- a/src/page-2/118. Pascal's Triangle/generate.ts +++ b/src/page-2/118. Pascal's Triangle/generate.ts @@ -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; diff --git a/src/page-8/790. Domino and Tromino Tiling/numTilings.test.ts b/src/page-8/790. Domino and Tromino Tiling/numTilings.test.ts new file mode 100644 index 0000000..3394934 --- /dev/null +++ b/src/page-8/790. Domino and Tromino Tiling/numTilings.test.ts @@ -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); + }); +}); diff --git a/src/page-8/790. Domino and Tromino Tiling/numTilings.ts b/src/page-8/790. Domino and Tromino Tiling/numTilings.ts new file mode 100644 index 0000000..b9f3a90 --- /dev/null +++ b/src/page-8/790. Domino and Tromino Tiling/numTilings.ts @@ -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]; +};