Find the minimum number of elements required so that their sum equals or exceeds S

前端 未结 5 1339
你的背包
你的背包 2021-02-05 11:46

I know this can be done by sorting the array and taking the larger numbers until the required condition is met. That would take at least nlog(n) sorting time.

Is there a

5条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-05 12:47

    1. eliminate numbers < S, if you find some number ==S, then solved
    2. pigeon-hole sort the numbers < S

    Sum elements highest to lowest in the sorted order till you exceed S.

提交回复
热议问题