leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

2706.cpp (461B)


0 class Solution { 1 public: 2 int buyChoco(const vector<int> &prices, int money) const { 3 int a = prices[0], b = prices[1]; 4 if (a > b) swap(a, b); 5 for (int i = 2; i < prices.size(); i++) { 6 if (a > prices[i]) { 7 b = a; 8 a = prices[i]; 9 } else if (b > prices[i]) { 10 b = prices[i]; 11 } 12 } 13 return a + b <= money ? money - (a + b) : money; 14 } 15 };