aboutsummaryrefslogtreecommitdiffstats
path: root/2706/main.py
blob: 697d31274549cdf010838a176087d069a36b3c4b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env python
import typing


class Solution:
    def buyChoco(self, prices: typing.List[int], money: int) -> int:
        price_dict: typing.Dict = {}
        for i in range(len(prices)):
            price_dict[prices[i]] = (
                price_dict[prices[i]] + 1 if prices[i] in price_dict else 1
            )

        mini = min(prices)
        if price_dict[mini] >= 2:
            if mini * 2 <= money:
                return money - mini * 2
            else:
                return money

        slider = mini + 1
        maxi = max(prices)
        while slider <= maxi:
            if slider in price_dict:
                if mini + slider <= money:
                    return money - mini - slider
            slider += 1

        return money


def main():
    solution = Solution()
    print(solution.buyChoco([1, 2, 1, 3, 2], 4))
    print(solution.buyChoco([10, 10, 10, 10], 20))
    print(solution.buyChoco([1, 2, 2], 3))
    print(solution.buyChoco([3, 2, 3], 3))


if __name__ == "__main__":
    main()