Разбираемся с рекурсиейБазовый случайmedium

Combos

Create a function combos, that accepts a single positive integer num (30 > num > 0) and returns an array of arrays of positive integers that sum to num.

Notes

  1. Sub-arrays may or may not have their elements sorted.
  2. The order of sub-arrays inside the main array does not matter.
  3. For an optimal solution, the following operation should complete within 6000ms.

Sample

combos(3) === [ [ 3 ], [ 1, 1, 1 ], [ 1, 2 ] ] combos(10) === [ [ 10 ], [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ], [ 1, 1, 1, 1, 1, 1, 1, 1, 2 ], [ 1, 1, 1, 1, 1, 1, 1, 3 ], [ 1, 1, 1, 1, 1, 1, 4 ], [ 1, 1, 1, 1, 1, 5 ], [ 1, 1, 1, 1, 6 ], [ 1, 1, 1, 7 ], [ 1, 1, 8 ], [ 1, 9 ], [ 1, 1, 1, 1, 1, 1, 2, 2 ], [ 1, 1, 1, 1, 1, 2, 3 ], [ 1, 1, 1, 1, 2, 4 ], [ 1, 1, 1, 1, 2, 2, 2 ], [ 1, 1, 1, 1, 3, 3 ], [ 1, 1, 1, 2, 5 ], [ 1, 1, 1, 2, 2, 3 ], [ 1, 1, 1, 3, 4 ], [ 1, 1, 2, 6 ], [ 1, 1, 2, 2, 4 ], [ 1, 1, 2, 2, 2, 2 ], [ 1, 1, 2, 3, 3 ], [ 1, 1, 3, 5 ], [ 1, 1, 4, 4 ], [ 1, 2, 7 ], [ 1, 2, 2, 5 ], [ 1, 2, 2, 2, 3 ], [ 1, 2, 3, 4 ], [ 1, 3, 6 ], [ 1, 3, 3, 3 ], [ 1, 4, 5 ], [ 2, 8 ], [ 2, 2, 6 ], [ 2, 2, 2, 4 ], [ 2, 2, 2, 2, 2 ], [ 2, 2, 3, 3 ], [ 2, 3, 5 ], [ 2, 4, 4 ], [ 3, 7 ], [ 3, 3, 4 ], [ 4, 6 ], [ 5, 5 ], ]