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 |
사실 가장 작은 값을 찾는거라 맨 앞에만 확인해도 됩니다
반응형
'python-algorithm' 카테고리의 다른 글
leetcode 2278. Percentage of Letter in String (0) | 2024.01.08 |
---|---|
leetcode 2500. Delete Greatest Value in Each Row (0) | 2024.01.07 |
leetcode 2843. Count Symmetric Integers (0) | 2024.01.05 |
leetcode 2894. Divisible and Non-divisible Sums Difference (1) | 2024.01.05 |
leetcode 2974. Minimum Number Game (0) | 2024.01.05 |
댓글