diff --git a/doc/source/basicgameplay/codingcontracts.rst b/doc/source/basicgameplay/codingcontracts.rst index 389d52195..7f82e6466 100644 --- a/doc/source/basicgameplay/codingcontracts.rst +++ b/doc/source/basicgameplay/codingcontracts.rst @@ -93,14 +93,14 @@ 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 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. | +| | | 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 containing 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. | diff --git a/src/data/codingcontracttypes.ts b/src/data/codingcontracttypes.ts index fb6709f6d..66f1d6d99 100644 --- a/src/data/codingcontracttypes.ts +++ b/src/data/codingcontracttypes.ts @@ -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(" "); }, @@ -148,39 +148,39 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [ }, }, { - desc: (data: any[]): string => { + desc: (data: [number, number[]]): 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", + `How many different distinct ways can the number ${n} be written`, + "as a sum of integers containing in the set\n\n", `[${s}]?\n\n`, - "You can use same integer from a set infinitely many times.", + "You may use each integer in the set zero or more times.", ].join(" "); }, difficulty: 2, - gen: (): any[] => { - const n: number = getRandomInt(8, 200); + gen: (): [number, number[]] => { + const n: number = getRandomInt(12, 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); - } + 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: any[], ans: string): boolean => { - const n: number = data[0]; - const s: number[] = data[1]; + 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);