Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/2240.html

2240. Number of Ways to Buy Pens and Pencils (Medium)

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

Similar Questions:

Solution 1.

Try using pen cnt = 0,1,2,... times. For the remainder remainder = total - cnt * a money, there are remainder / b + 1 ways of using pencil.

  • // OJ: https://leetcode.com/problems/number-of-ways-to-buy-pens-and-pencils/
    // Time: O(T/A)
    // Space: O(1)
    class Solution {
    public:
        long long waysToBuyPensPencils(int total, int a, int b) {
            long ans = 0;
            for (int cnt = 0; cnt * a <= total; ++cnt) {
                ans += (total - cnt * a) / b + 1;
            }
            return ans;
        }
    };
    
  • class Solution:
        def waysToBuyPensPencils(self, total: int, cost1: int, cost2: int) -> int:
            ans = 0
            for x in range(total // cost1 + 1):
                y = (total - (x * cost1)) // cost2 + 1
                ans += y
            return ans
    
    ############
    
    # 2240. Number of Ways to Buy Pens and Pencils
    # https://leetcode.com/problems/number-of-ways-to-buy-pens-and-pencils/
    
    class Solution:
        def waysToBuyPensPencils(self, total: int, cost1: int, cost2: int) -> int:
            
            
            if cost2 > cost1:
                cost1, cost2 = cost2, cost1
            
            res = 0
            
            for buy in range(total // cost1 + 1):
                curr = total - (buy * cost1)
                res += (curr // cost2) + 1
            
            return res
    
    
  • class Solution {
        public long waysToBuyPensPencils(int total, int cost1, int cost2) {
            long ans = 0;
            for (int x = 0; x <= total / cost1; ++x) {
                int y = (total - x * cost1) / cost2 + 1;
                ans += y;
            }
            return ans;
        }
    }
    
  • func waysToBuyPensPencils(total int, cost1 int, cost2 int) (ans int64) {
    	for x := 0; x <= total/cost1; x++ {
    		y := (total-x*cost1)/cost2 + 1
    		ans += int64(y)
    	}
    	return
    }
    
  • function waysToBuyPensPencils(
        total: number,
        cost1: number,
        cost2: number,
    ): number {
        let ans = 0;
        for (let x = 0; x <= Math.floor(total / cost1); ++x) {
            const y = Math.floor((total - x * cost1) / cost2) + 1;
            ans += y;
        }
        return ans;
    }
    
    
  • impl Solution {
        pub fn ways_to_buy_pens_pencils(total: i32, cost1: i32, cost2: i32) -> i64 {
            let mut ans: i64 = 0;
            for pen in 0..=total / cost1 {
                ans += ((total - pen * cost1) / cost2) as i64 + 1;
            }
            ans
        }
    }
    

Or

  • // OJ: https://leetcode.com/problems/number-of-ways-to-buy-pens-and-pencils/
    // Time: O(T/A)
    // Space: O(1)
    class Solution {
    public:
        long long waysToBuyPensPencils(int total, int a, int b) {
            long ans = 0;
            for (; total >= 0; total -= a) ans += total / b + 1;
            return ans;
        }
    };
    
  • class Solution:
        def waysToBuyPensPencils(self, total: int, cost1: int, cost2: int) -> int:
            ans = 0
            for x in range(total // cost1 + 1):
                y = (total - (x * cost1)) // cost2 + 1
                ans += y
            return ans
    
    ############
    
    # 2240. Number of Ways to Buy Pens and Pencils
    # https://leetcode.com/problems/number-of-ways-to-buy-pens-and-pencils/
    
    class Solution:
        def waysToBuyPensPencils(self, total: int, cost1: int, cost2: int) -> int:
            
            
            if cost2 > cost1:
                cost1, cost2 = cost2, cost1
            
            res = 0
            
            for buy in range(total // cost1 + 1):
                curr = total - (buy * cost1)
                res += (curr // cost2) + 1
            
            return res
    
    
  • class Solution {
        public long waysToBuyPensPencils(int total, int cost1, int cost2) {
            long ans = 0;
            for (int x = 0; x <= total / cost1; ++x) {
                int y = (total - x * cost1) / cost2 + 1;
                ans += y;
            }
            return ans;
        }
    }
    
  • func waysToBuyPensPencils(total int, cost1 int, cost2 int) (ans int64) {
    	for x := 0; x <= total/cost1; x++ {
    		y := (total-x*cost1)/cost2 + 1
    		ans += int64(y)
    	}
    	return
    }
    
  • function waysToBuyPensPencils(
        total: number,
        cost1: number,
        cost2: number,
    ): number {
        let ans = 0;
        for (let x = 0; x <= Math.floor(total / cost1); ++x) {
            const y = Math.floor((total - x * cost1) / cost2) + 1;
            ans += y;
        }
        return ans;
    }
    
    
  • impl Solution {
        pub fn ways_to_buy_pens_pencils(total: i32, cost1: i32, cost2: i32) -> i64 {
            let mut ans: i64 = 0;
            for pen in 0..=total / cost1 {
                ans += ((total - pen * cost1) / cost2) as i64 + 1;
            }
            ans
        }
    }
    

Discuss

https://leetcode.com/problems/number-of-ways-to-buy-pens-and-pencils/discuss/1953859/

All Problems

All Solutions