-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7bb772e
commit 4b3eaf5
Showing
4 changed files
with
38 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
}; |