2240. Number of Ways to Buy Pens and Pencils

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public long waysToBuyPensPencils(int total, int cost1, int cost2) {
if (cost1 < cost2) {
// 枚举大的,降低时间复杂度
return waysToBuyPensPencils(total, cost2, cost1);
}

long ways = 0;
int count1 = 0;
while (count1 * cost1 <= total) {
int remain = total - count1 * cost1;
int count2 = remain / cost2 + 1;
ways += count2;
count1++;
}

return ways;
}
}

References

2240. Number of Ways to Buy Pens and Pencils