夜周赛76【20220416】
https://leetcode-cn.com/problems/number-of-ways-to-buy-pens-and-pencils/
请问这里加一是因为向下取整还是因为只买钢笔或者只买铅笔也算是一种方案呢?
class Solution:
def waysToBuyPensPencils(self, total: int, cost1: int, cost2: int) -> int:
"""
T: O(n)
S: O(1)
idea: 翻硬币,两个变量转换为一个变量的变化
"""
amount_cost1 = total // cost1
res = 0
for i in range(amount_cost1+1):
remainder = total - i * cost1
# 在余数范围内均可以购买
res += remainder // cost2 + 1
return res
其他解法
https://oi-wiki.org/math/number-theory/euclidean/#_1
找零问题的类推 —- DP
https://leetcode-cn.com/problems/number-of-ways-to-buy-pens-and-pencils/comments/1510665
https://leetcode-cn.com/problems/coin-change/