Advertisement

Eat Pizzas! - LeetCode 3457 Solution

Eat Pizzas! - Complete Solution Guide

Eat Pizzas! is LeetCode problem 3457, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You are given an integer array pizzas of size n , where pizzas[i] represents the weight of the i th pizza. Every day, you eat exactly 4 pizzas. Due to your incredible metabolism, when you eat pizzas of weights W , X , Y , and Z , where W <= X <= Y <= Z , you gain the weight of only 1 pizza! On odd-numbered days (1-indexed) , you gain a weight of Z . On even-numbered days, you gain a weight of Y . Find the maximum total weight you can gain by eating all pizzas optimally. Note : It is guaranteed t

Detailed Explanation

The problem asks us to maximize the total weight gained from eating pizzas. We are given an array of pizza weights and must eat exactly 4 pizzas each day. The weight gained each day is equal to the second largest weight (on even days) or the largest weight (on odd days) among the four pizzas eaten that day. The goal is to find the optimal way to eat all pizzas to maximize the total weight gained, given that the number of pizzas is a multiple of 4.

Solution Approach

The solution involves sorting the pizza weights in ascending order. Then, we calculate the number of odd and even days. For odd days, we eat the largest pizzas. For even days, we select the largest available pizzas, avoiding overlapping with already eaten largest pizzas for odd days. The total weight gained is the sum of weights from odd and even days.

Step-by-Step Algorithm

  1. Step 1: Sort the `pizzas` array in ascending order.
  2. Step 2: Calculate the number of days `k` required to eat all pizzas: `k = n / 4` where `n` is the number of pizzas.
  3. Step 3: Calculate the number of odd days: `k_odd = (k + 1) // 2`.
  4. Step 4: Calculate the number of even days: `k_even = k // 2`.
  5. Step 5: Calculate the weight gained on odd days by summing the `k_odd` largest pizza weights: These are the pizzas at the end of the sorted array (from `n - k_odd` to `n`).
  6. Step 6: Calculate the weight gained on even days by summing the `k_even` largest pizza weights *after* removing the pizzas eaten on odd days: This consists of the pizzas from index `n - k - k_even` to `n - k`.
  7. Step 7: Return the sum of the weight gained on odd days and the weight gained on even days.

Key Insights

  • Insight 1: Sorting the pizzas allows us to easily identify the heaviest pizzas.
  • Insight 2: Eating the heaviest pizzas on odd days and the next heaviest on even days maximizes the total weight gained because heaviest pizza weight is considered on odd days.
  • Insight 3: Determining the optimal number of odd and even days to plan the pizza consumption.

Complexity Analysis

Time Complexity: O(n log n)

Space Complexity: O(1)

Topics

This problem involves: Array, Greedy, Sorting.

Companies

Asked at: Infosys.