Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const countSubsetsOfSizeK = (n, k) => {
- if (k === 0 || k === n) return 1;
- const table = new Array(n + 1).fill(0).map(() => new Array(k + 1).fill(0));
- // all row[0] (first column) set to 1
- for (let r = 0; r <= n; r++) {
- table[r][0] = 1;
- }
- // diagonal set to 1
- for (let c = 0; c <= k; c++) {
- table[c][c] = 1;
- }
- for (let row = 2; row <= n; row++) {
- for (let col = 1; col <= Math.min(row, k); col++) {
- table[row][col] = table[row - 1][col] + table[row - 1][col - 1]
- }
- }
- return table[n][k];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement