// Deterministic 2-competitive: rent until cost equals buy, then buy.
function skiRentalDet(rentPerDay, buyCost, days) {
let spent = 0; let own = false;
for (let d = 1; d <= days; d++) {
if (!own) {
if (spent + rentPerDay < buyCost) { spent += rentPerDay; }
else { spent += buyCost; own = true; }
}
}
return spent;
}
// Randomized e/(e-1)-competitive: buy on day T ~ Geometric(p) with p=1/(e-1)
function skiRentalRand(rentPerDay, buyCost, days, rng=Math.random) {
const p = 1/(Math.E - 1); // ~0.582
let own = false, spent = 0;
for (let d = 1; d <= days; d++) {
if (!own && rng() < p) { spent += buyCost; own = true; }
if (!own) spent += rentPerDay;
}
return spent;
}
Cache of size k; sequence of page requests; fault if not in cache.
// Deterministic: LRU and FIFO are k-competitive; LFU can be unbounded.
// Randomized: Marking algorithms (Randomized Marking) are O(log k)-competitive; lower bound Ω(log k).