2240. Number of Ways to Buy Pens and Pencils LeetCode Solution

In this guide, you will get 2240. Number of Ways to Buy Pens and Pencils LeetCode Solution with the best time and space complexity. The solution to Number of Ways to Buy Pens and Pencils problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Number of Ways to Buy Pens and Pencils solution in C++
  4. Number of Ways to Buy Pens and Pencils solution in Java
  5. Number of Ways to Buy Pens and Pencils solution in Python
  6. Additional Resources
2240. Number of Ways to Buy Pens and Pencils LeetCode Solution image

Problem Statement of 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

Complexity Analysis

  • Time Complexity: O(\frac{\texttt{total}}{\texttt{cost1}})
  • Space Complexity: O(1)

2240. Number of Ways to Buy Pens and Pencils LeetCode Solution in C++

class Solution {
 public:
  long long waysToBuyPensPencils(int total, int cost1, int cost2) {
    long ans = 0;
    const int maxPen = total / cost1;

    for (int i = 0; i <= maxPen; ++i)
      ans += (total - i * cost1) / cost2 + 1;

    return ans;
  }
};
/* code provided by PROGIEZ */

2240. Number of Ways to Buy Pens and Pencils LeetCode Solution in Java

class Solution {
  public long waysToBuyPensPencils(int total, int cost1, int cost2) {
    long ans = 0;
    final int maxPen = total / cost1;

    for (int i = 0; i <= maxPen; ++i)
      ans += (total - i * cost1) / cost2 + 1;

    return ans;
  }
}
// code provided by PROGIEZ

2240. Number of Ways to Buy Pens and Pencils LeetCode Solution in Python

class Solution:
  def waysToBuyPensPencils(self, total: int, cost1: int, cost2: int) -> int:
    maxPen = total // cost1
    return sum((total - i * cost1) // cost2
               for i in range(maxPen + 1)) + maxPen + 1
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.