[LeetCode] Eat Pizzas!

3457. Eat Pizzas!

You are given an integer array pizzas of size n, where pizzas[i] represents the weight of the ith 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 that n is a multiple of 4, and each pizza can be eaten only once.

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
long long maxWeight(vector<int>& pizzas) {
int n = pizzas.size(), day = n / 4, even = day / 2, odd = day - even;
sort(begin(pizzas), end(pizzas));
long long res = 0;
for(int i = 0; i < odd; i++) res += pizzas[n-i-1];
for(int i = odd + day * 2; i < n - odd; i += 2) res += pizzas[i];
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2025/02/16/PS/LeetCode/eat-pizzas/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.