Skip to content

Commit dc59610

Browse files
authored
Problem Difficulty Level: Medium
1 parent 2e10b45 commit dc59610

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*---------------------------
2+
Time Complexity: O(nlogn)
3+
Space Complexity: O(1)
4+
----------------------------*/
5+
6+
class Solution {
7+
public:
8+
long long maxWeight(vector<int>& pizzas) {
9+
int numDays = (pizzas.size() / 4);
10+
sort(pizzas.begin(), pizzas.end());
11+
12+
int numOddDays = (numDays / 2) + (numDays % 2);
13+
int numEvenDays = (numDays / 2);
14+
int i = pizzas.size() - 1;
15+
16+
long long totalWeightGain = 0;
17+
while(numOddDays){
18+
totalWeightGain += pizzas[i];
19+
i -= 1;
20+
numOddDays -= 1;
21+
}
22+
23+
while(numEvenDays){
24+
totalWeightGain += pizzas[i - 1];
25+
i -= 2;
26+
numEvenDays -= 1;
27+
}
28+
return totalWeightGain;
29+
}
30+
};
31+
32+
/*
33+
Question Link: https://leetcode.com/problems/eat-pizzas/description/
34+
Author: M.R.Naganathan
35+
*/

0 commit comments

Comments
 (0)