当前位置:网站首页>Leetcode 2171 removing minimum number of magic beans (prefix and recommendation)
Leetcode 2171 removing minimum number of magic beans (prefix and recommendation)
2022-06-11 01:44:00 【_ TCgogogo_】
You are given an array of positive integers beans, where each integer represents the number of magic beans found in a particular magic bag.
Remove any number of beans (possibly none) from each bag such that the number of beans in each remaining non-empty bag (still containing at least one bean) is equal. Once a bean has been removed from a bag, you are not allowed to return it to any of the bags.
Return the minimum number of magic beans that you have to remove.
Example 1:
Input: beans = [4,1,6,5] Output: 4 Explanation: - We remove 1 bean from the bag with only 1 bean. This results in the remaining bags: [4,0,6,5] - Then we remove 2 beans from the bag with 6 beans. This results in the remaining bags: [4,0,4,5] - Then we remove 1 bean from the bag with 5 beans. This results in the remaining bags: [4,0,4,4] We removed a total of 1 + 2 + 1 = 4 beans to make the remaining non-empty bags have an equal number of beans. There are no other solutions that remove 4 beans or fewer.
Example 2:
Input: beans = [2,10,3,2] Output: 7 Explanation: - We remove 2 beans from one of the bags with 2 beans. This results in the remaining bags: [0,10,3,2] - Then we remove 2 beans from the other bag with 2 beans. This results in the remaining bags: [0,10,3,0] - Then we remove 3 beans from the bag with 3 beans. This results in the remaining bags: [0,10,0,0] We removed a total of 2 + 2 + 3 = 7 beans to make the remaining non-empty bags have an equal number of beans. There are no other solutions that removes 7 beans or fewer.
Constraints:
1 <= beans.length <= 10^51 <= beans[i] <= 10^5
Topic link :https://leetcode.com/problems/removing-minimum-number-of-magic-beans/
The main idea of the topic : Each basket has n Items , You can take out several items at will , Ask at least how many items you can take out to make the number of items in the non empty basket the same
Topic analysis : The general idea is to enumerate each number , Calculate the total number of items to be taken out when the current number is used as the final number of items in the basket , Because the amount of data is only 10^5, The frequency of occurrence of each number can be calculated directly from the array frq, Then calculate the prefix value and sum And the number of prefixes and cnt, For numbers num, Anything smaller should obviously be removed , namely sum[num - 1], The larger part is bigger than it sum[N] - sum[num] (N Is the total length ), If all values in this part are num, Then the sum is num * (cnt[N] - cnt[num]), So you want all of these values to be num, A total of sum[N] - sum[num] - num * (cnt[N] - cnt[num]) So many items , Plus less than num Take out all the parts , We can O(1) Calculate the final number of solutions with each number , Take the minimum
26ms, Time beats 99.45%
class Solution {
final int N = 100000;
long[] frq = new long[N + 5];
long[] sum = new long[N + 5];
long[] cnt = new long[N + 5];
public long minimumRemoval(int[] beans) {
for (int num : beans) {
frq[num]++;
}
for (int i = 1; i <= N; i++) {
sum[i] = sum[i - 1] + i * frq[i];
cnt[i] = cnt[i - 1] + frq[i];
}
long ans = Long.MAX_VALUE;
for (int num : beans) {
ans = Math.min(ans, magic(num));
}
return ans;
}
private long magic(int num) {
return sum[num - 1] + sum[N] - sum[num] - num * (cnt[N] - cnt[num]);
}
}边栏推荐
- 2021-7-18 ROS笔记-xml语言相关
- 立个flag--重构promise
- Configurable custom implementation 1 Implementation interface, 2 Custom configuration 3 Default configuration
- Millions of visits - resolution of high concurrency problems
- Middleware_ Redis_ 05_ Persistence of redis
- 项目_基于网络爬虫的疫情数据可视化分析
- QGC地面站使用教程
- Leetcode permutation and combination problem backtracking
- Yunna Qingyuan fixed assets management and barcode inventory system
- China-open-ssl编译的一些记录
猜你喜欢

SAS因子分析(proc factor过程和因子旋转以及回归法求因子得分函数)

Threejs: streamer effect encapsulation

threejs:流光效果封装

Middleware_ Redis_ 06_ Redis transactions

PX4装机教程(六)垂起固定翼(倾转)
![[ROS introduction] cmakelist Txt and packages XML interpretation](/img/26/bae82a457fb83b2214d2f8c20955e2.jpg)
[ROS introduction] cmakelist Txt and packages XML interpretation
![[Li mu] how to read papers [intensive reading of papers]](/img/41/7e1ff1db2f7a848c8702c186c79fe5.jpg)
[Li mu] how to read papers [intensive reading of papers]

Project_ Visual analysis of epidemic data based on Web Crawler

2021-02-27MATLAB的图像处理

Inventory management and strategy mode
随机推荐
云呐|PDA无线固定资产盘点管理系统
[ongoing update...] 2021 National Electronic Design Competition for college students (III) interpretation of the anonymous four axis space developer flight control system design
[VBA Script] extract the information and pending status of all annotations in the word document
What are programmers in big factories looking at? It took me two years to sort it out, and I will look at it and cherish it!
SAS principal component analysis (finding correlation matrix, eigenvalue, unit eigenvector, principal component expression, contribution rate and cumulative contribution rate, and data interpretation)
Leetcode divide and conquer method
Leetcode 1574 shortest subarray to be removed to make array sorted
Middleware_ Redis_ 05_ Persistence of redis
记录打包GoogleChrome浏览器插件
I was so excited about the college entrance examination in 2022
Hao expresses his opinions: what small good habits have you adhered to?
SSH Remote Login configuration sshd_ Config file details
Role of handlermethodargumentresolver + use case
ROS parameter server
Digital IC Design self-study ing
项目_基于网络爬虫的疫情数据可视化分析
[recommended by Zhihu knowledge master] castle in UAV - focusing on the application of UAV in different technical fields
MATLAB数字运算函数笔记
Yunna Qingyuan fixed assets management and barcode inventory system
2021-02-27MATLAB的图像处理