Statistics

Problem Statement for "PackageSizes"

Problem Statement

You are buying a product that comes in packages of varying sizes, given in int[] sizes. You wish to buy a total of total units of the product.

Return the minimum number of packages you must buy in order to purchase exactly total units. If it is not possible to do so, return -1.

Definition

Class:
PackageSizes
Method:
getMinimum
Parameters:
int[], int
Returns:
int
Method signature:
int getMinimum(int[] sizes, int total)
(be sure your method is public)

Constraints

  • sizes will contain between 1 and 10 elements, inclusive.
  • Each element of sizes will be between 1 and 100, inclusive.
  • total will be between 0 and 1000, inclusive.

Examples

  1. { 2, 3 }

    6

    Returns: 2

    We could buy 3 packages of 2, or 2 packages of 3. 2 packages is the least we need.

  2. { 6, 9, 20 }

    8

    Returns: -1

    There's no way to buy exactly 8 with these package sizes.

  3. { 2, 5, 12 }

    7

    Returns: 2

    We need a package of 5 and a package of 2.

  4. { 2, 5, 12 }

    0

    Returns: 0

    We don't have to buy any packages at all.

  5. { 13, 50, 99 }

    894

    Returns: 12

  6. { 3, 2, 7, 3 }

    10

    Returns: 2

    Package sizes aren't necessarily listed in order, and can be repeated.

  7. {1, 5, 12 }

    15

    Returns: 3

  8. {2, 3 }

    5

    Returns: 2

  9. {10 }

    10

    Returns: 1

  10. {10 }

    1000

    Returns: 100

  11. {1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }

    1000

    Returns: 1000

  12. {2, 3, 5 }

    6

    Returns: 2

  13. {1, 2, 8 }

    11

    Returns: 3

  14. {2, 3, 5, 7 }

    11

    Returns: 3

  15. {15, 7, 2 }

    53

    Returns: 6

  16. {3, 5 }

    11

    Returns: 3

  17. {2, 3 }

    6

    Returns: 2


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: