Skip to content

Commit

Permalink
New Problem Solution - "1815. Maximum Number of Groups Getting Fresh …
Browse files Browse the repository at this point in the history
…Donuts"
  • Loading branch information
haoel committed Apr 8, 2021
1 parent 9d268cd commit f0a4ff0
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ LeetCode
|1818|[Minimum Absolute Sum Difference](https://leetcode.com/problems/minimum-absolute-sum-difference/) | [C++](./algorithms/cpp/minimumAbsoluteSumDifference/MinimumAbsoluteSumDifference.cpp)|Medium|
|1817|[Finding the Users Active Minutes](https://leetcode.com/problems/finding-the-users-active-minutes/) | [C++](./algorithms/cpp/findingTheUsersActiveMinutes/FindingTheUsersActiveMinutes.cpp)|Medium|
|1816|[Truncate Sentence](https://leetcode.com/problems/truncate-sentence/) | [C++](./algorithms/cpp/truncateSentence/TruncateSentence.cpp)|Easy|
|1815|[Maximum Number of Groups Getting Fresh Donuts](https://leetcode.com/problems/maximum-number-of-groups-getting-fresh-donuts/) | [C++](./algorithms/cpp/maximumNumberOfGroupsGettingFreshDonuts/MaximumNumberOfGroupsGettingFreshDonuts.cpp)|Hard|
|1814|[Count Nice Pairs in an Array](https://leetcode.com/problems/count-nice-pairs-in-an-array/) | [C++](./algorithms/cpp/countNicePairsInAnArray/CountNicePairsInAnArray.cpp)|Medium|
|1813|[Sentence Similarity III](https://leetcode.com/problems/sentence-similarity-iii/) | [C++](./algorithms/cpp/sentenceSimilarity/SentenceSimilarity.III.cpp)|Medium|
|1812|[Determine Color of a Chessboard Square](https://leetcode.com/problems/determine-color-of-a-chessboard-square/) | [C++](./algorithms/cpp/determineColorOfAChessboardSquare/DetermineColorOfAChessboardSquare.cpp)|Easy|
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Source : https://leetcode.com/problems/maximum-number-of-groups-getting-fresh-donuts/
// Author : Hao Chen
// Date : 2021-04-08

/*****************************************************************************************************
*
* There is a donuts shop that bakes donuts in batches of batchSize. They have a rule where they must
* serve all of the donuts of a batch before serving any donuts of the next batch. You are given an
* integer batchSize and an integer array groups, where groups[i] denotes that there is a group of
* groups[i] customers that will visit the shop. Each customer will get exactly one donut.
*
* When a group visits the shop, all customers of the group must be served before serving any of the
* following groups. A group will be happy if they all get fresh donuts. That is, the first customer
* of the group does not receive a donut that was left over from the previous group.
*
* You can freely rearrange the ordering of the groups. Return the maximum possible number of happy
* groups after rearranging the groups.
*
* Example 1:
*
* Input: batchSize = 3, groups = [1,2,3,4,5,6]
* Output: 4
* Explanation: You can arrange the groups as [6,2,4,5,1,3]. Then the 1^st, 2^nd, 4^th, and 6^th
* groups will be happy.
*
* Example 2:
*
* Input: batchSize = 4, groups = [1,3,2,5,2,2,1,6]
* Output: 4
*
* Constraints:
*
* 1 <= batchSize <= 9
* 1 <= groups.length <= 30
* 1 <= groups[i] <= 10^9
******************************************************************************************************/

class Solution {
public:
int maxHappyGroups(int batchSize, vector<int>& groups) {
vector<int> reminder(batchSize, 0);
for(auto& g : groups) {
reminder[g % batchSize]++;
}

// greedy for reminder is zero and two groups reminder is zero.
// for example: batchSize = 3, groups = [1,2,3,4,5,6]
// then we will find: (3) (6) (1,5) (2,4)

// greedy for one group
int result = reminder[0];
//greedy for two groups
for (int i=1; i<=batchSize/2; i++){
if (reminder[i] == 0 || reminder[batchSize-i] == 0 ) continue;

int m = (i == batchSize-i) ?
reminder[i]/2 :
min(reminder[i], reminder[batchSize-i]);

reminder[i] -= m;
reminder[batchSize-i] -= m;
result += m;
}

map<vector<int>, int> cache;
//DFS for the rest groups
result += dfs(reminder, batchSize, 0, cache);
return result;
}

int dfs(vector<int>& reminder, int batchSize, int sum, map<vector<int>, int>& cache) {
if (cache.find(reminder) != cache.end()) return cache[reminder];
int ret = 0;
int bonus = sum == 0 ? 1: 0;
for(int i=1; i<batchSize; i++) {
if (reminder[i] <= 0) continue;
reminder[i]--;
ret = max(ret, bonus + dfs(reminder, batchSize, (sum + i) % batchSize, cache));
reminder[i]++;
}
cache[reminder] = ret;
return ret;
}
};

0 comments on commit f0a4ff0

Please sign in to comment.