본문 바로가기
python-algorithm

leetcode 2706. Buy Two Chocolates

by 무적김두칠 2024. 1. 5.

https://leetcode.com/problems/buy-two-chocolates/description/

 

Buy Two Chocolates - LeetCode

Can you solve this real interview question? Buy Two Chocolates - You are given an integer array prices representing the prices of various chocolates in a store. You are also given a single integer money, which represents your initial amount of money. You m

leetcode.com

 

1
2
3
4
5
6
7
8
9
10
from itertools import combinations
class Solution:
    def buyChoco(self, prices: List[int], money: int-> int:
        com = list(combinations(prices, 2))
        com = sorted([sum(x) for x in com])
        for i in com:
            if money >= i:
                return money - i
        return money
        
cs
1
2
3
4
5
6
7
8
9
10
11
12
from itertools import combinations
class Solution:
    def buyChoco(self, prices: List[int], money: int-> int:
        com = list(combinations(prices, 2))
        com = sorted([sum(x) for x in com])
        # for i in com:
        #     if money >= i:
        #         return money - i
        if money >= com[0]:
            return money - com[0]
        return money
        
cs

사실 가장 작은 값을 찾는거라 맨 앞에만 확인해도 됩니다

반응형

댓글