diff --git a/greedy-algorithms/notes.md b/greedy-algorithms/notes.md index 74f0e4c05b161fd21b61430a806630d91eea758c..09b242b645b7b91e92034af2fa2fb4ab97dd53e8 100644 --- a/greedy-algorithms/notes.md +++ b/greedy-algorithms/notes.md @@ -122,8 +122,8 @@ Problem: [same as above] ## Choosing a Forward Edge * Exhaustive search: - * Generate … - * Check that … + * Generate all following locations out to a distance of `range` as long as we don't go past the final destination + * Check that the chosen destination maximizes distance travelled (always favors the candidate) ## Example @@ -139,12 +139,15 @@ Problem: [same as above] Location Next Location -------- ------------- - a … - … + a c + c d + d e + e h + h i Path ---- - a → … + a → c → d → e → h → i -------------------------------------------------------------------------------- diff --git a/greedy-algorithms/src/features/fueling/solver.js b/greedy-algorithms/src/features/fueling/solver.js index 18326349a6727b0110a14ffee9f92a0950e54315..1d81f060bb6b80fb63614420e00c52388bf88566 100644 --- a/greedy-algorithms/src/features/fueling/solver.js +++ b/greedy-algorithms/src/features/fueling/solver.js @@ -7,7 +7,16 @@ export function planFuelings(gaps, range) { gaps.every((gap) => gap <= range), `Tried to find a fueling plan crossing a gap longer than the range ${range}.`, ); - const results = []; - // TODO: stub + const results = [0]; + const destination = gaps.length; + while (results[results.length - 1] < destination) { + let best = undefined; + for (let distance = …, candidate = …; + distance < … && candidate < …; + distance += …, candidate += …) { + best = candidate; + } + results.push(best); + } return results; }