Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

Number of Ways to Buy Pens and Pencils LeetCode Solution

Problem – Number of Ways to Buy Pens and Pencils LeetCode Solution

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

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;
        int val=0;
        for(int x=0;;x++)
        {
            val=total-cost1*x;
            if(val < 0) break;
            ans+= (long)Math.floor((long)val /(long) cost2)+1;   
        }
         return ans;
    }
}

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 long ways = 0;
        long long penscost = 0;
        while(penscost <= total) {
            long long remainingAmount = total - penscost;
            long long pencils = remainingAmount/cost2 + 1;
            ways += pencils;
            penscost += cost1;
        }
        return ways;
    }
};

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

class Solution:
    def waysToBuyPensPencils(self, total: int, cost1: int, cost2: int) -> int:
        ways = 0;
        penscost = 0;
        while penscost <= total:
            remainingAmount = total - penscost;
            pencils = remainingAmount//cost2 + 1;
            ways += pencils;
            penscost += cost1;
        return ways
Number of Ways to Buy Pens and Pencils LeetCode Solution Review:

In our experience, we suggest you solve this Number of Ways to Buy Pens and Pencils LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Number of Ways to Buy Pens and Pencils LeetCode Solution

Find on LeetCode

Conclusion:

I hope this Number of Ways to Buy Pens and Pencils LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published. Required fields are marked *