当前位置:网站首页>1815. Get the maximum number of groups of fresh donuts state compression
1815. Get the maximum number of groups of fresh donuts state compression
2022-08-11 04:26:00 【Yu Niangniang】
1815. Maximum number of groups to get fresh donuts
There is a donut shop that bakes
batchSizedoughnuts per batch.The shop has a rule that all doughnuts must be sold out before a new batch of doughnuts is baked.You are given an integerbatchSizeand an array of integersgroups, where each integer in the array represents a group of customers who came to buy donuts, wheregroups[i]indicates the number of customers in this batch.Every customer happens to want just one donut.When one group of customers arrives at the store, all of them must finish their doughnut purchases before the next group arrives.If the first customer in a group gets donuts that aren't left over from the previous group, then everyone in this group will be happy.
You can arrange the order in which each batch of customers arrives at will.Please return at most how many groups of people would be happy under this premise.
Example 1:
Input:batchSize = 3, groups = [1,2,3,4,5,6]Output:4Explanation: You can order these batches of customers as [6,2,4,5,1,3] .Then groups 1, 2, 4, and 6 will all be happy.Example 2:
Input:batchSize = 4, groups = [1,3,2,5,2,2,1,6]Output:4Tip:
1 <= batchSize <= 91 <= groups.length <= 301 <= groups[i] <= 1e9
Source: LeetCode
Link: https://leetcode.cn/problems/maximum-number-of-groups-getting-fresh-donuts
The copyright belongs to LeetCode.com.For commercial reprints, please contact the official authorization, and for non-commercial reprints, please indicate the source.
Results of the questions
Failure, no matter how it is timed out, it's a headache, I want to compress the state, but it's not convenient, the numbers are different
Method: State Compression
This problem is unequal-length state compression that has never been encountered before.What does that mean?That is, for example, the current number can be up to 5, then for this digit, its base number is 6, the full 6 is just the remainder of 0 to 5, the next largest number is 2, and the full 3 is just enough to enumerate 0 to 1.2, so there are a total of 6*3 possibilities for these two bits together, here is a number mapping: 7->[2,1] (2+1*5).This solves the problem of discontinuous enumeration.
The second question is how to get one of the numbers.Take the current decimal as an example: 120, the middle 2 is required, we can use 120%100/10 to take out the middle one, that is, the base that needs the next and current bits.So preprocessing calculates the value of each base, in particular, enumerates all the product values by one more bit to prevent overflow
Specific steps:
1. Take the remainder of all numbers for statistics, and get the number corresponding to each remainder
2. Calculate the base of the remainder when reaching a certain digit
3. For every possible situation of binary enumeration, take out the specific value by taking the remainder and then rounding it up and sum it up
4. If the remainder is 0 in the front, the latter will be happy no matter what the result is, so if the remainder is 0, the result will be added by 1; if the remainder is not 0, the resultconstant.The enumeration can currently take any one of the values, they may be the last number, and take the best result.
5. Accumulate the result of the calculation, taking the maximum value into the answer.
6. In the previous steps, you can not count the part with the remainder of 0.Because the remainder is 0 must be happy.Values with a remainder of 0 are accumulated directly into the answer.
class Solution {public int maxHappyGroups(int batchSize, int[] groups) {if(batchSize==1) return groups.length;int[] mods = new int[batchSize];for(int group:groups) mods[group%batchSize]++;int[]base = new int[batchSize+1];base[1]=1;for(int i = 2;i<=batchSize;i++){base[i]=base[i-1]*(mods[i-1]+1);}int total = base[batchSize];int ans = 0;int[] dp = new int[total];for(int i = 1; i < total; i++){int[] tms = new int[batchSize];int sum = 0;for(int j = 1; j 0){dp[i] = Math.max(dp[i-base[j]]+((sum-j)%batchSize==0?1:0),dp[i]);}}ans = Math.max(ans,dp[i]);}return ans+mods[0];}} 边栏推荐
- 【服务器安装Redis】Centos7离线安装redis
- Self-research capability was recognized again, and Tencent Cloud Database was included in the Forrester Translytical report
- 【FPGA】SDRAM
- 洛谷P4061 大吉大利,晚上吃鸡
- WPF DataGrid 使用数据模板(2)
- 移动端地图开发选择哪家?
- "125 Palindrome Verification" of the 10th day string series of LeetCode brushing questions
- MYSQLg advanced ------ clustered and non-clustered indexes
- LeetCode814算题第15天二叉树系列值《814 二叉树剪枝》
- Graphical LeetCode - 640. Solving Equations (Difficulty: Moderate)
猜你喜欢

leetcode刷题第13天二叉树系列之《98 BST及其验证》

"125 Palindrome Verification" of the 10th day string series of LeetCode brushing questions

es-head插件插入查询以及条件查询(五)

How to learn machine learning?machine learning process

「转」“搜索”的原理,架构,实现,实践,面试不用再怕了

【FPGA】day20-I2C读写EEPROM

What is ensemble learning in machine learning?

校园兼职平台项目反思

简历里写了会代码,却依然过不了面试这一关

LeetCode刷题第12天二叉树系列之《104 二叉树的最大深度》
随机推荐
Clang Code Model: Error: The clangbackend executable “X:/clangbackend.exe“ could not be started
洛谷P4061 大吉大利,晚上吃鸡
JwsManager service interface implementation class - the jni implementation
【服务器安装mysql】centos7下使用mysql离线安装包安装mysql5.7
使用百度EasyDL实现智能垃圾箱
.NET自定义中间件
蹭个热度-请勿打开
Get the length of the linked list
2022-08-10 The sixth group Hiding spring study notes
MySQL database storage engine and database creation, modification and deletion
set_new_handler(0)是什么意思?有什么用?
【FPGA】SDRAM
Provincial level of Echart maps, as well as all prefecture-level download and use
Interchangeability and Measurement Technology—Surface Roughness Selection and Marking Method
【FPGA】名词缩写
1815. 得到新鲜甜甜圈的最多组数 状态压缩
洛谷P2245 星际导航
"98 BST and Its Verification" of the 13th day of leetcode brushing series of binary tree series
《卫星界》刊评“星辰大海”计划:孙宇晨为太空旅游带来新的机遇
"110 Balanced Binary Tree Judgment" in leetCode's 14-day binary tree series