2706. Buy Two Chocolates LeetCode Solution

In this guide, you will get 2706. Buy Two Chocolates LeetCode Solution with the best time and space complexity. The solution to Buy Two Chocolates problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Buy Two Chocolates solution in C++
  4. Buy Two Chocolates solution in Java
  5. Buy Two Chocolates solution in Python
  6. Additional Resources
2706. Buy Two Chocolates LeetCode Solution image

Problem Statement of 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 must buy exactly two chocolates in such a way that you still have some non-negative leftover money. You would like to minimize the sum of the prices of the two chocolates you buy.
Return the amount of money you will have leftover after buying the two chocolates. If there is no way for you to buy two chocolates without ending up in debt, return money. Note that the leftover must be non-negative.

Example 1:

Input: prices = [1,2,2], money = 3
Output: 0
Explanation: Purchase the chocolates priced at 1 and 2 units respectively. You will have 3 – 3 = 0 units of money afterwards. Thus, we return 0.

Example 2:

Input: prices = [3,2,3], money = 3
Output: 3
Explanation: You cannot buy 2 chocolates without going in debt, so we return 3.

Constraints:

See also  344. Reverse String LeetCode Solution

2 <= prices.length <= 50
1 <= prices[i] <= 100
1 <= money <= 100

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

2706. Buy Two Chocolates LeetCode Solution in C++

class Solution {
 public:
  int buyChoco(vector<int>& prices, int money) {
    int min1 = INT_MAX;
    int min2 = INT_MAX;

    for (const int price : prices)
      if (price <= min1) {
        min2 = min1;
        min1 = price;
      } else if (price < min2) {
        min2 = price;
      }

    const int minCost = min1 + min2;
    return minCost > money ? money : money - minCost;
  }
};
/* code provided by PROGIEZ */

2706. Buy Two Chocolates LeetCode Solution in Java

class Solution {
  public int buyChoco(int[] prices, int money) {
    int min1 = Integer.MAX_VALUE;
    int min2 = Integer.MAX_VALUE;

    for (final int price : prices)
      if (price <= min1) {
        min2 = min1;
        min1 = price;
      } else if (price < min2) {
        min2 = price;
      }

    final int minCost = min1 + min2;
    return minCost > money ? money : money - minCost;
  }
}
// code provided by PROGIEZ

2706. Buy Two Chocolates LeetCode Solution in Python

class Solution:
  def buyChoco(self, prices: list[int], money: int) -> int:
    min1 = math.inf
    min2 = math.inf

    for price in prices:
      if price <= min1:
        min2 = min1
        min1 = price
      elif price < min2:
        min2 = price

    minCost = min1 + min2
    return money if minCost > money else money - minCost
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.