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

Recorded work from Wednesday.

parent 820423ad
No related branches found
No related tags found
No related merge requests found
......@@ -111,15 +111,15 @@ Problem: Wrap a paragraph to a line length, minimizing the sum of the squares of
## DAG
* Edges (actions):
*
* Place a line on the page
* Vertices (situations):
*
* The number of words placed so far
* Edge weights:
*
* The penalty for the line just placed
* Topological order:
*
* Increasing order: 0 → 1 → … → `wordCount`
* Goal:
*
* Find a shortest path from 0 to `wordCount`
## Backpointer Class
......
import { Item } from './items.js';
const KILOGRAM_OF_NOTHING = new Item(1, 0);
class Backpointer {
constructor(item, totalValue) {
this.item = item;
......@@ -9,7 +11,23 @@ class Backpointer {
function chooseBackpointer(items, backpointers) {
const currentWeight = backpointers.length;
// TODO: stub
let best = new Backpointer(
KILOGRAM_OF_NOTHING,
backpointers[currentWeight - 1].totalValue,
);
for (const item of items) {
const previousWeight = currentWeight - item.weight;
if (previousWeight >= 0) {
const candidate = new Backpointer(
item,
item.value + backpointers[previousWeight].totalValue,
);
if (candidate.totalValue > best.totalValue) {
best = candidate;
}
}
}
return best;
}
export function chooseItems(items, weightLimit) {
......@@ -20,7 +38,10 @@ export function chooseItems(items, weightLimit) {
}
const reversedPath = [];
for (let weight = weightLimit; weight > 0; weight -= backpointers[weight].item.weight) {
reversedPath.push();
const item = backpointers[weight].item;
if (item !== KILOGRAM_OF_NOTHING) {
reversedPath.push(item);
}
}
return reversedPath.reverse();
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment