Choosing coins with least or no change given

前端 未结 8 1398
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-07 04:00

I am making a game which consists of coin denominations of $10, $5, $3, and $1. The player may have 0 or more of each type of currency in his inventory with a maximum of 15 coin

8条回答
  •  悲哀的现实
    2021-02-07 04:22

    The problem can be defined as:

    Return a subset of items where the sum is closest to x, but >= x.
    

    This problem is called the subset sum problem. It is NP-complete. You won't find a perfect algorithm that runs in pseudo-polynomial time, only imperfect heuristics.

    However, if the number of coins is very small, then an exhaustive search of the solution space will certainly work.

    If the number of coins is larger, then you should look at Wikipedia for an overview: https://en.wikipedia.org/wiki/Subset_sum_problem#Polynomial_time_approximate_algorithm

提交回复
热议问题