/** * You are building a diving board by placing a bunch of planks of wood end to end. * There are two types of planks, one of length shorter and one of length longer. * You must use exactly K planks of wood. Write a method to generate all possible lengths for diving board. * * N short plank * L long plank * * Questions? * - K input int * * Must use exactly K planks. * K = 5 * S = 10 cm * L = 20 cm * S + S + S + S + S = 50 * S + S + S + S + L = 60 * S + S + S + L + L = 70 * S + S + L + L + L = 80 * S + L + L + L + L = 90 * L + L + L + L + L = 100 * * * 6 * */ /** * hashtable lengths; * getlength(k, count, shortPlankSize, longPlankSize, lengths) * if(k < 0){ * return lengths; * } * lengths[k] = k * shortPlankSize + count * longPlankSize * return getlength(k-1, count+1, shortPlankSize, longPlankSize, lengths) * */ // min O(k) function calculateAllLengths(k, shortPlankSize, longPlankSize) { const allLengths = {}; calculateLengths(k, 0, shortPlankSize, longPlankSize, allLengths); return allLengths; } function calculateLengths(k, count, shortPlankSize, longPlankSize, allLengths) { if (k < 0) { return allLengths; } allLengths[k] = k * shortPlankSize + count * longPlankSize; return calculateLengths( k - 1, count + 1, shortPlankSize, longPlankSize, allLengths ); } const result = calculateAllLengths(5, 10, 20); console.log(result);