Merge pull request #3383 from anatoly-kussul/cct-total-ways-to-sum-2

New coding contract type: Total Ways to Sum II
This commit is contained in:
hydroflame 2022-04-11 13:21:24 -04:00 committed by GitHub
commit d5ad2118dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 2 deletions

@ -93,9 +93,15 @@ The list contains the name of (i.e. the value returned by
| Subarray with Maximum Sum | | Given an array of integers, find the contiguous subarray (containing |
| | | at least one number) which has the largest sum and return that sum. |
+------------------------------------+------------------------------------------------------------------------------------------+
| Total Ways to Sum | | Given a number, how many different ways can that number be written as |
| Total Ways to Sum | | Given a number, how many different distinct 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 the set of available integers. |
| | | How many different distinct ways can that number n be written as |
| | | a sum of integers contained in the given set? |
| | | You may use each integer in the set zero or more times. |
+------------------------------------+------------------------------------------------------------------------------------------+
| Spiralize Matrix | | Given an array of array of numbers representing a 2D matrix, return the |
| | | elements of that matrix in clockwise spiral order. |
| | | |

@ -124,7 +124,7 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
"    2 + 2\n",
"    2 + 1 + 1\n",
"    1 + 1 + 1 + 1\n\n",
`How many different ways can the number ${n} be written as a sum of at least`,
`How many different distinct ways can the number ${n} be written as a sum of at least`,
"two positive integers?",
].join(" ");
},
@ -147,6 +147,51 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
return ways[data] === parseInt(ans, 10);
},
},
{
desc: (data: [number, number[]]): string => {
const n: number = data[0];
const s: number[] = data[1];
return [
`How many different distinct ways can the number ${n} be written`,
"as a sum of integers contained in the set:\n\n",
`[${s}]?\n\n`,
"You may use each integer in the set zero or more times.",
].join(" ");
},
difficulty: 2,
gen: (): [number, number[]] => {
const n: number = getRandomInt(12, 200);
const maxLen: number = getRandomInt(8, 12);
const s: number[] = [];
// Bias towards small numbers is intentional to have much bigger answers in general
// to force people better optimize their solutions
for (let i = 1; i <= n; i++) {
if (s.length == maxLen) {
break;
}
if (Math.random() < 0.6 || n - i < maxLen - s.length) {
s.push(i);
}
}
return [n, s];
},
name: "Total Ways to Sum II",
numTries: 10,
solver: (data: [number, number[]], ans: string): boolean => {
// https://www.geeksforgeeks.org/coin-change-dp-7/?ref=lbp
const n = data[0];
const s = 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 = [