Skip to content
Snippets Groups Projects
Commit 8552b033 authored by Brady James Garvin's avatar Brady James Garvin
Browse files

Designed and implmented a dynamic programming solution.

parent 5f9ada1d
Branches
No related tags found
No related merge requests found
class Backpointer {
constructor(previousIndex, count) {
this.previousIndex = previousIndex;
this.count = count;
}
}
function chooseBackpointer(list, backpointers) {
const currentIndex = backpointers.length;
const currentElement = currentIndex < list.length ? list[currentIndex] : Infinity;
let best = new Backpointer(undefined, 0);
for (let i = 0; i < currentIndex; ++i) {
if (list[i] <= currentElement) {
const candidate = new Backpointer(i, backpointers[i].count + 1);
if (candidate.count > best.count) {
best = candidate;
}
}
}
return best;
}
export function deleteToSort(list) {
return []; // TODO: stub
const backpointers = [];
while (backpointers.length <= list.length) {
backpointers.push(chooseBackpointer(list, backpointers));
}
const reversedResults = [];
for (let current = backpointers[list.length].previousIndex;
current !== undefined;
current = backpointers[current].previousIndex) {
reversedResults.push(list[current]);
}
return reversedResults.reverse();
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment