40 lines
1.1 KiB
Python
40 lines
1.1 KiB
Python
'''
|
|
The price of one tomato at grocery-store is a dollars, but there is a promotion where you can buy two tomatoes for b dollars. Bob needs to buy exactly n tomatoes.
|
|
When buying two tomatoes, he can choose to buy them at the regular price or at the promotion price. What is the minimum amount of dollars Bob should spend to buy
|
|
n tomatoes?
|
|
|
|
Example 1
|
|
Input:
|
|
n = 2; a = 5; b = 9
|
|
Output:
|
|
9
|
|
Example 2
|
|
Input:
|
|
n = 3; a = 5; b = 11
|
|
Output:
|
|
15
|
|
'''
|
|
|
|
class Solution(object):
|
|
def solve(self, n:int, a:int, b:int) -> int:
|
|
print(f"n: {n}, a: {a}, b: {b}")
|
|
total = 0
|
|
if n == 1:
|
|
total = a
|
|
else:
|
|
if a*2 > b:
|
|
if n % 2 == 0: # Buy at lower price
|
|
total = int((n/2)*b)
|
|
else:
|
|
total = int(((n-1)/2)*b + a)
|
|
elif b > a*2:
|
|
total = n*a
|
|
elif b == 2*a:
|
|
total = n*a
|
|
return total
|
|
|
|
s = Solution()
|
|
print(s.solve(2, 5, 9)) # Output: 9
|
|
print(s.solve(3, 5, 11)) # Output: 15
|
|
print(s.solve(1, 5, 12))
|
|
print(s.solve(9, 7, 13)) |