{x}
blog image

Number of Ways to Buy Pens and Pencils

You are given an integer total indicating the amount of money you have. You are also given two integers cost1 and cost2 indicating the price of a pen and pencil respectively. You can spend part or all of your money to buy multiple quantities (or none) of each kind of writing utensil.

Return the number of distinct ways you can buy some number of pens and pencils.

 

Example 1:

Input: total = 20, cost1 = 10, cost2 = 5
Output: 9
Explanation: The price of a pen is 10 and the price of a pencil is 5.
- If you buy 0 pens, you can buy 0, 1, 2, 3, or 4 pencils.
- If you buy 1 pen, you can buy 0, 1, or 2 pencils.
- If you buy 2 pens, you cannot buy any pencils.
The total number of ways to buy pens and pencils is 5 + 3 + 1 = 9.

Example 2:

Input: total = 5, cost1 = 10, cost2 = 10
Output: 1
Explanation: The price of both pens and pencils are 10, which cost more than total, so you cannot buy any writing utensils. Therefore, there is only 1 way: buy 0 pens and 0 pencils.

 

Constraints:

  • 1 <= total, cost1, cost2 <= 106

Solution Explanation: Number of Ways to Buy Pens and Pencils

This problem asks to find the number of distinct ways to buy pens and pencils given a total budget and the cost of each item. The solution uses a simple enumeration approach.

Approach:

The core idea is to iterate through all possible numbers of pens that can be bought. For each number of pens, we calculate the remaining budget and determine the number of pencils that can be purchased with the remaining budget. The total number of ways is the sum of all possible combinations across different numbers of pens.

Algorithm:

  1. Iterate through Pen Combinations: The outer loop iterates from 0 to the maximum number of pens that can be bought using the total budget (total // cost1 + 1). This ensures we consider all possibilities, including buying no pens at all.

  2. Calculate Remaining Budget: For each number of pens (x), calculate the remaining budget: remaining = total - x * cost1.

  3. Calculate Pencil Combinations: The maximum number of pencils that can be bought with the remaining budget is remaining // cost2. Add 1 to this to include the possibility of buying zero pencils.

  4. Accumulate Combinations: Add the number of pencil combinations for the current number of pens to the total count (ans).

  5. Return Total Combinations: After iterating through all pen combinations, the ans variable holds the total number of distinct ways to buy pens and pencils.

Time and Space Complexity Analysis:

  • Time Complexity: The time complexity is O(T/C1), where T is the total budget and C1 is the cost1 of a pen. The dominant factor is the outer loop, which iterates up to total // cost1 times.

  • Space Complexity: The space complexity is O(1) because we only use a few integer variables to store the counts and intermediate results. No additional data structures are used that scale with the input size.

Code Examples (Python):

class Solution:
    def waysToBuyPensPencils(self, total: int, cost1: int, cost2: int) -> int:
        ans = 0
        for x in range(total // cost1 + 1):  # Iterate through possible number of pens
            remaining = total - x * cost1     # Calculate remaining budget
            y = (remaining // cost2) + 1      # Calculate possible number of pencils + 1 (to include 0 pencils)
            ans += y                          # Add to total combinations
        return ans
 

This Python code directly implements the algorithm described above. The other languages (Java, C++, Go, TypeScript, Rust) would have very similar implementations, reflecting the same algorithm. The core logic remains consistent across all languages.