n种类型的数组,组成m个数字组合,类型不可重复,问最多多少组

// n个类型,m个一组,类型数量为 list
const getRes = (n, m, list, index=0) => {
  list.sort((a,b) => a-b) // 正序
  const sum = list.reduce((p,c) => p+c, 0)
  const res = Array(sum).fill().map(v => [])
  while(list.length) {
    const cur = list.length
    const curNum = list.pop()
    for(let i=0; i<curNum; i++) {
      while(res[index+i].length >= m) index++
      res[index+i].push(cur)
    }
  }
  return res.reduce((p,c) => {
    if(c.length === m) return p+1
    return p
  },0)
}

  console.log(getRes(5, 3, [1,2,3,4,5]))
  console.log(getRes(5, 4, [1,2,3,4,5]))
  console.log(getRes(10,9, [1,19,13,4,16,12,5,8,14,6]))

全部评论
这个是你的笔试题?
点赞 回复 分享
发布于 2022-09-18 16:13 陕西

相关推荐

10-31 13:04
南华大学 Java
嵌入式的小白:很多面试,面试前不会去打扰cto的,但一般cto不会在这些小事上刷人,只能说这个cto比较操心,啥重要不重要,紧急不紧急的,估计都会过问,平淡看待吧
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务