New coding contract type: Total Ways to Sum II

This commit is contained in:
Anatoly Kussul 2022-04-09 22:38:42 +03:00
parent 758b0e1127
commit 27180ebccb
2 changed files with 51 additions and 0 deletions

@ -96,6 +96,12 @@ The list contains the name of (i.e. the value returned by
| Total Ways to Sum | | Given a number, how many different ways can that number be written as |
| | | a sum of at least two positive integers? |
+------------------------------------+------------------------------------------------------------------------------------------+
| Total Ways to Sum II | | You are given an array with two elements. The first element is an integer n. |
| | | The second element is an array of numbers representing set of available integers. |
| | | How many different ways can that number n be written as |
| | | a sum of integers containing in a given set? |
| | | You can use same integer from a set infinitely many times. |
+------------------------------------+------------------------------------------------------------------------------------------+
| Spiralize Matrix | | Given an array of array of numbers representing a 2D matrix, return the |
| | | elements of that matrix in clockwise spiral order. |
| | | |

@ -147,6 +147,51 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
return ways[data] === parseInt(ans, 10);
},
},
{
desc: (data: any[]): string => {
const n: number = data[0];
const s: number[] = data[1];
return [
`How many different ways can the number ${n} be written`,
"as a sum of integers containing in set\n\n",
`[${s}]?\n\n`,
"You can use same integer from a set infinitely many times.",
].join(" ");
},
difficulty: 2,
gen: (): any[] => {
const n: number = getRandomInt(8, 200);
const maxLen: number = getRandomInt(8, 12);
let s: number[] = [];
while (s.length < 4) {
s = [];
for (let i = 1; i <= n; i++) {
if (s.length == maxLen) {
break;
}
if (Math.random() < 0.6) {
s.push(i);
}
}
}
return [n, s];
},
name: "Total Ways to Sum II",
numTries: 10,
solver: (data: any[], ans: string): boolean => {
const n: number = data[0];
const s: number[] = data[1];
const ways: number[] = [1];
ways.length = n + 1;
ways.fill(0, 1);
for (let i = 0; i < s.length; i++) {
for (let j = s[i]; j <= n; j++) {
ways[j] += ways[j - s[i]];
}
}
return ways[n] === parseInt(ans, 10);
},
},
{
desc: (n: number[][]): string => {
let d: string = [