diff --git a/doc/source/basicgameplay/codingcontracts.rst b/doc/source/basicgameplay/codingcontracts.rst index 3edb89453..389d52195 100644 --- a/doc/source/basicgameplay/codingcontracts.rst +++ b/doc/source/basicgameplay/codingcontracts.rst @@ -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. | | | | | diff --git a/src/data/codingcontracttypes.ts b/src/data/codingcontracttypes.ts index bbdc17ba3..fb6709f6d 100644 --- a/src/data/codingcontracttypes.ts +++ b/src/data/codingcontracttypes.ts @@ -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 = [