Given a rod of length n, maximize the total number of segments can be cutted in length of x, y, and z.
The first cut can have one of three possible lengths: x, y, or z. The remaining length to be cut is reduced accordingly. For example, if the first cut is of length x, the remaining length becomes n - x. The key to this algorithm lies in its recursive nature. This recursive pattern continues until the remaining length is too small for any further cuts.
def cut(n, x, y, z): if n <= 0: return 0 return 1 + max(cut(n - x, x, y, z), cut(n - y, x, y, z), cut(n - z, x, y, z)) n = 4 x = 2 y = 1 z = 1 print(cut(n, x, y, z))
function cut(n, x, y, z) { if (n <= 0) { return 0; } return 1 + Math.max(cut(n - x, x, y, z), cut(n - y, x, y, z), cut(n - z, x, y, z)); } const n = 4; const x = 2; const y = 1; const z = 1; console.log(cut(n, x, y, z));