当前位置:网站首页>[daily problem insight] Li Kou - the 280th weekly match (I really didn't know it could be so simple to solve other people's problems)
[daily problem insight] Li Kou - the 280th weekly match (I really didn't know it could be so simple to solve other people's problems)
2022-07-05 02:37:00 【Code Fox】
️ Winter vacation xinkeng —— Code Fox's daily notes
The winter vacation is about to expire
6007. The maximum sum of the array -Hard- The first 280 Weekly competition questions 4
Give you a length of n Array of integers for nums And an integer numSlots , Satisfy 2 * numSlots >= n . All in all numSlots Baskets , The number is 1 To numSlots .
You need to put all n Divide the whole number into these baskets , And each basket at most Yes 2 It's an integer . Of a distribution scheme With and Defined as the number of each number and its basket Bitwise and operation The sum of the results .
- For example , The digital
[1, 3]Put it in the basket *1* in ,[4, 6]Put it in the basket *2* in , The sum of this scheme is(1 AND ***1\***) + (3 AND ***1\***) + (4 AND ***2***) + (6 AND ***2***) = 1 + 1 + 0 + 2 = 4.
Please return and will nums Put all numbers in numSlots The largest of the baskets and .
// The solution comes from ——https://leetcode-cn.com/u/endlesscheng/
class Solution {
public int maximumANDSum(int[] nums, int numSlots) {
var ans = 0;
var f = new int[1 << (numSlots * 2)];
for (var i = 0; i < f.length; i++) {
var c = Integer.bitCount(i);
if (c >= nums.length) continue;
for (var j = 0; j < numSlots * 2; ++j) {
if ((i & (1 << j)) == 0) {
// Enumerate empty baskets j
var s = i | (1 << j);
f[s] = Math.max(f[s], f[i] + ((j / 2 + 1) & nums[c]));
ans = Math.max(ans, f[s]);
}
}
}
return ans;
}
}
6006. Take out the smallest number of magic beans -Mid- The first 280 Weekly competition questions 3
To give you one just An array of integers beans , Each integer represents the number of magic beans in a bag .
Please... From each bag take out Some beans ( It's fine too Don't take out ), Make the rest Non empty In the bag ( namely At least also One Magic bean bag ) Number of magic beans equal . Once the magic beans are removed from the bag , You can't put it in any other bag .
Please return to where you need to take out the magic beans Minimum number .
class Solution {
public long minimumRemoval(int[] beans) {
Arrays.sort(beans);
long sum=0;
for(int i:beans){
sum+=i;
}
int n=beans.length;
if(n==1){
return 0;
}
long dp=0;
long sumF=0;
long min=Long.MAX_VALUE;
for(int i=n-1;i>=0;i--){
if(i!=n-1){
dp=(sum+sumF-(long)beans[i+1]*(long)(n-1-i));
}
else{
dp=sum;
}
sum-=beans[i];
sumF+=beans[i];
if(min>dp){
min=dp;
}
}
dp=sumF-(long)beans[0]*(long)(n);
if(min>dp){
min=dp;
}
return min;
}
}
6005. The minimum number of operands to make an array an alternating array -Mid- The first 280 Weekly competition questions 3
I'll give you a subscript from 0 Starting array nums , The array consists of n It's made up of four positive integers .
If the following conditions are met , The array nums It's a Alternating array :
nums[i - 2] == nums[i], among2 <= i <= n - 1.nums[i - 1] != nums[i], among1 <= i <= n - 1.
In one step operation in , You can choose the subscript i And will nums[i] change by any Positive integer .
Returns the that makes the array an alternating array The minimum number of operands .
class Solution {
public int minimumOperations(int[] nums) {
int n=nums.length;
if(n==1){
return 0;
}
int[][] same=new int[2][2];
int[][] same2=new int[2][2];
int count=0;
int cur=0;
same=get(nums,n,2);
same2=get(nums,n,1);
int m=0;
if(same[0][1]!=same2[0][1]){
m=same[0][0]+same2[0][0];
}
m=Math.max(same2[1][0]+same[0][0],m);
m=Math.max(same2[0][0]+same[1][0],m);
return n-m;
}
int[][] get(int[] nums,int n,int k){
int count=0;
int cur=0;
Map<Integer,Integer> map=new HashMap<>();
int[][] same=new int[2][2];
for(int i=k%2;i<n;i+=2){
map.put(nums[i],map.getOrDefault(nums[i],0)+1);
}
for(Map.Entry<Integer,Integer> e:map.entrySet()){
if(e.getValue()>same[0][0]){
same[1][0]=same[0][0];
same[1][1]=same[0][1];
same[0][0]=e.getValue();
same[0][1]=e.getKey();
}
else if(e.getValue()>same[1][0]){
same[1][0]=e.getValue();
same[1][1]=e.getKey();
}
}
return same;
}
}
6004. obtain 0 The number of operations - The first 280 Weekly competition questions 1
Here are two for you non-negative Integers num1 and num2 .
Each step operation in , If num1 >= num2 , You have to use num1 reduce num2 ; otherwise , You have to use num2 reduce num1 .
- for example ,
num1 = 5Andnum2 = 4, Should use thenum1reducenum2, therefore , obtainnum1 = 1andnum2 = 4. However , Ifnum1 = 4Andnum2 = 5, After one step operation , obtainnum1 = 4andnum2 = 1.
Return to make num1 = 0 or num2 = 0 Of Operands .
class Solution {
public int countOperations(int num1, int num2) {
int a=0;
while(num1!=0&&num2!=0){
if(num1>=num2){
a+=(num1/num2);
num1%=num2;
}
else{
a+=(num2/num1);
num2%=num1;
}
}
return a;
}
}
ending
Title source : Power button (LeetCode) link :https://leetcode-cn.com/problems
️ Focus on the author , Take you to brush the questions , Learn the most commonly used algorithm skills from simple algorithm problems ( Winter vacation every day )
️ Pay attention to the author's questions —— Simple to advanced , Let you unknowingly become a ruthless problem brushing machine , If you have any questions, please send a private letter
边栏推荐
- Redis distributed lock, lock code logic
- 丸子百度小程序详细配置教程,审核通过。
- Missile interception -- UPC winter vacation training match
- Using druid to connect to MySQL database reports the wrong type
- [illumination du destin - 38]: Ghost Valley - chapitre 5 Flying clamp - one of the Warnings: There is a kind of killing called "hold Kill"
- [機緣參悟-38]:鬼穀子-第五飛箝篇 - 警示之一:有一種殺稱為“捧殺”
- [understanding of opportunity -38]: Guiguzi - Chapter 5 flying clamp - warning one: there is a kind of killing called "killing"
- Android advanced interview question record in 2022
- Limited query of common SQL operations
- [技术发展-26]:新型信息与通信网络的数据安全
猜你喜欢

Can you really learn 3DMAX modeling by self-study?

The steering wheel can be turned for one and a half turns. Is there any difference between it and two turns

Marubeni Baidu applet detailed configuration tutorial, approved.

Practice of tdengine in TCL air conditioning energy management platform

【附源码】基于知识图谱的智能推荐系统-Sylvie小兔
![ASP. Net core 6 framework unveiling example demonstration [01]: initial programming experience](/img/3b/0ae9fbadec5dbdfc6981f1f55546a5.jpg)
ASP. Net core 6 framework unveiling example demonstration [01]: initial programming experience

A tab Sina navigation bar
![ASP. Net core 6 framework unveiling example demonstration [01]: initial programming experience](/img/22/08617736a8b943bc9c254aac60c8cb.jpg)
ASP. Net core 6 framework unveiling example demonstration [01]: initial programming experience
![[source code attached] Intelligent Recommendation System Based on knowledge map -sylvie rabbit](/img/3e/ab14f3a0ddf31c7176629d891e44b4.png)
[source code attached] Intelligent Recommendation System Based on knowledge map -sylvie rabbit

Single line function*
随机推荐
[download white paper] does your customer relationship management (CRM) really "manage" customers?
丸子百度小程序详细配置教程,审核通过。
Missile interception -- UPC winter vacation training match
ELK日志分析系统
[200 opencv routines] 99 Modified alpha mean filter
Using druid to connect to MySQL database reports the wrong type
RichView TRVStyle MainRVStyle
Binary tree traversal - middle order traversal (golang)
Design and implementation of kindergarten management system
JVM's responsibility - load and run bytecode
Elfk deployment
Richview trvunits image display units
如何做一个炫酷的墨水屏电子钟?
Pytest (4) - test case execution sequence
When to catch an exception and when to throw an exception- When to catch the Exception vs When to throw the Exceptions?
使用druid连接MySQL数据库报类型错误
【LeetCode】501. Mode in binary search tree (2 wrong questions)
Design of KTV intelligent dimming system based on MCU
Write a thread pool by hand, and take you to learn the implementation principle of ThreadPoolExecutor thread pool
openresty ngx_lua变量操作