// 要在回溯基础上加上贪心的想法,优先把可能的给功劳最大的 // 首先需要排好序的一个数组,数组中存放的就是原有功劳数组对应的下标, // 只不过这个数组是按照功劳大小排好序的, // 例如 [3,1,2,0] 的第一个3代表原有功劳数组中索引3位置处的功劳最大 // 这里省去排序的这一步,下面就是 arr1 为该数组,arr2 为功劳数组 // index 代表当前进行到第几个了,candies 代表剩余糖果数 let total = 0 function recurse(arr1, arr2, index, candies) { if (candies === 0) { total++ } if (index === arr1.length) { return } for (let i = candies; i >= 0; i--) { if (arr2[index] 位置处能放下 i 个糖果) { recurse(arr1, arr2, index + 1, candies - i) } } }