added coments, removed possible infinite loop, changed problem phrasing

This commit is contained in:
Anatoly Kussul 2022-04-11 00:01:52 +03:00
parent 27180ebccb
commit cb756808a2
2 changed files with 25 additions and 25 deletions

@ -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 | | 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. | | | | 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? | | | | 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. | | 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. | | | | The second element is an array of numbers representing the set of available integers. |
| | | How many different ways can that number n be written as | | | | How many different distinct ways can that number n be written as |
| | | a sum of integers containing in a given set? | | | | a sum of integers containing in the given set? |
| | | You can use same integer from a set infinitely many times. | | | | 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 | | Spiralize Matrix | | Given an array of array of numbers representing a 2D matrix, return the |
| | | elements of that matrix in clockwise spiral order. | | | | elements of that matrix in clockwise spiral order. |

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