3457. Eat Pizzas! Medium

@problem@discussion
#Array#Greedy#Sorting



1/**
2 * [3457] Eat Pizzas!
3 *
4 * 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!
5 * 
6 * 	On <span style="box-sizing: border-box; margin: 0px; padding: 0px;">odd-numbered</span> days (1-indexed), you gain a weight of Z.
7 * 	On even-numbered days, you gain a weight of Y.
8 * 
9 * Find the maximum total weight you can gain by eating all pizzas optimally.
10 * Note: It is guaranteed that n is a multiple of 4, and each pizza can be eaten only once.
11 *  
12 * <strong class="example">Example 1:
13 * <div class="example-block">
14 * Input: <span class="example-io">pizzas = [1,2,3,4,5,6,7,8]</span>
15 * Output: <span class="example-io">14</span>
16 * Explanation:
17 * 
18 * 	On day 1, you eat pizzas at indices [1, 2, 4, 7] = [2, 3, 5, 8]. You gain a weight of 8.
19 * 	On day 2, you eat pizzas at indices [0, 3, 5, 6] = [1, 4, 6, 7]. You gain a weight of 6.
20 * 
21 * The total weight gained after eating all the pizzas is 8 + 6 = 14.
22 * </div>
23 * <strong class="example">Example 2:
24 * <div class="example-block">
25 * Input: <span class="example-io">pizzas = [2,1,1,1,1,1,1,1]</span>
26 * Output: <span class="example-io">3</span>
27 * Explanation:
28 * 
29 * 	On day 1, you eat pizzas at indices [4, 5, 6, 0] = [1, 1, 1, 2]. You gain a weight of 2.
30 * 	On day 2, you eat pizzas at indices [1, 2, 3, 7] = [1, 1, 1, 1]. You gain a weight of 1.
31 * 
32 * The total weight gained after eating all the pizzas is 2 + 1 = 3.
33 * </div>
34 *  
35 * Constraints:
36 * 
37 * 	4 <= n == pizzas.length <= 2 * 10^<span style="font-size: 10.8333px;">5</span>
38 * 	1 <= pizzas[i] <= 10^5
39 * 	n is a multiple of 4.
40 * 
41 */
42pub struct Solution {}
43
44// problem: https://leetcode.com/problems/eat-pizzas/
45// discuss: https://leetcode.com/problems/eat-pizzas/discuss/?currentPage=1&orderBy=most_votes&query=
46
47// submission codes start here
48
49impl Solution {
50    pub fn max_weight(pizzas: Vec<i32>) -> i64 {
51        
52    }
53}
54
55// submission codes end
56
57#[cfg(test)]
58mod tests {
59    use super::*;
60
61    #[test]
62    fn test_3457() {
63    }
64}
65

Back
© 2026 bowen.ge All Rights Reserved.