FastPrepCount Combinations Using Weights One Through K

Count Combinations Using Weights One Through K

Confluent logoConfluent● MediumFULLTIMEOA
Learn

Problem statement

Given a nonnegative integer total and a positive integer k, count the combinations of integer weights 1 through k whose sum is exactly total.

  • You may use each weight any number of times.
  • A combination is determined by the number of copies of each weight. Different arrangements of the same weights count as one combination.
  • There is one combination for total = 0: selecting no weights.

Return the count modulo 10^9 + 7.

Function

countWeightCombinations(total: int, k: int) → int

Examples

Example 1

total = 8k = 2return = 5

The combinations are [1,1,1,1,1,1,1,1], [1,1,1,1,1,1,2], [1,1,1,1,2,2], [1,1,2,2,2], and [2,2,2,2].

Example 2

total = 5k = 3return = 5

The combinations are [1,1,1,1,1], [1,1,1,2], [1,2,2], [1,1,3], and [2,3].

Example 3

total = 3k = 8return = 3

The combinations are [1,1,1], [1,2], and [3]. Weights greater than 3 cannot appear in a sum of 3.

Constraints

  • 0 <= total <= 1000.
  • 1 <= k <= 1000.

More Confluent problems

See Confluent hiring insights
public int countWeightCombinations(int total, int k) {
    // write your code here
}
total8
k2
expected5
Checking account…