Complete the code to check if a problem has the greedy-choice property.
function hasGreedyChoiceProperty(problem: any): boolean {
// If local optimal choices lead to global optimum
return problem.[1];
}The greedy-choice property means that making a local optimal choice at each step leads to a global optimum.
Complete the code to check if a problem has overlapping subproblems.
function hasOverlappingSubproblems(problem: any): boolean {
// If subproblems repeat
return problem.[1];
}Overlapping subproblems means the problem can be broken into subproblems which are solved multiple times.
Fix the error in the function that decides which approach to apply.
function chooseApproach(problem: any): string {
if (problem.[1]) {
return 'Greedy';
} else if (problem.hasOverlappingSubproblems) {
return 'Dynamic Programming';
} else {
return 'Brute Force';
}
}The decision should check if the problem has the greedy-choice property first to choose Greedy.
Fill both blanks to complete the function that explains when to use DP.
function shouldUseDP(problem: any): boolean {
return problem.[1] && problem.[2];
}Dynamic Programming is used when the problem has overlapping subproblems and requires memoization.
Fill all three blanks to complete the function that decides the algorithm type based on problem properties.
function decideAlgorithm(problem: any): string {
if (problem.[1]) {
return 'Greedy';
} else if (problem.[2] && problem.[3]) {
return 'Dynamic Programming';
} else {
return 'Brute Force';
}
}The function first checks for greedy-choice property, then overlapping subproblems and memoization for DP, else brute force.