java.lang.Object
g2201_2300.s2240_number_of_ways_to_buy_pens_and_pencils.Solution

public class Solution extends Object
2240 - Number of Ways to Buy Pens and Pencils.<p>Medium</p> <p>You are given an integer <code>total</code> indicating the amount of money you have. You are also given two integers <code>cost1</code> and <code>cost2</code> indicating the price of a pen and pencil respectively. You can spend <strong>part or all</strong> of your money to buy multiple quantities (or none) of each kind of writing utensil.</p> <p>Return <em>the <strong>number of distinct ways</strong> you can buy some number of pens and pencils.</em></p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> total = 20, cost1 = 10, cost2 = 5</p> <p><strong>Output:</strong> 9</p> <p><strong>Explanation:</strong> The price of a pen is 10 and the price of a pencil is 5.</p> <ul> <li> <p>If you buy 0 pens, you can buy 0, 1, 2, 3, or 4 pencils.</p> </li> <li> <p>If you buy 1 pen, you can buy 0, 1, or 2 pencils.</p> </li> <li> <p>If you buy 2 pens, you cannot buy any pencils.</p> </li> </ul> <p>The total number of ways to buy pens and pencils is 5 + 3 + 1 = 9.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> total = 5, cost1 = 10, cost2 = 10</p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong> 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.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= total, cost1, cost2 <= 10<sup>6</sup></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • waysToBuyPensPencils

      public long waysToBuyPensPencils(int totalMoney, int costPen, int costPencil)