[daily problem] Li Kou - game 280 (I really didn't know how to solve other people's problems so succinctly)

Posted by andrewgauger on Sun, 13 Feb 2022 15:18:17 +0100

⭐ New pit in winter vacation -- daily question notes of code Fox
😢 The winter vacation is about to expire 😢

6007. Maximum sum of array - Hard - game 280 weekly question 4

Give you an integer array nums with length N and an integer numSlots, satisfying 2 * numSlots > = n. There are a total of numSlots baskets numbered 1 to numSlots.

You need to divide all n integers into these baskets, and each basket has at most 2 integers. The sum of an allocation scheme is defined as the sum of the bitwise sum of each number and its basket number.

• For example, put the numbers [1,3] into basket * 1 *, [4,6] into basket * 2 *, and 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 the maximum sum of all numbers in nums into numSlots baskets.

```//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 few magic beans - Mid - game 280 weekly question 3

Give you a positive integer array beans, where each integer represents the number of magic beans in a bag.

Please take out some beans from each bag (or not), so that the number of magic beans in the remaining non empty bag (i.e. the bag with at least one magic bean) is equal. Once the magic bean is removed from the bag, you can't put it in any other bag.

```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. Minimum operands to turn an array into an alternating array - Mid - game 280 week question 3

Give you an array num with subscript starting from 0, which is composed of n positive integers.

The array nums is an alternating array if the following conditions are met:

• nums[i - 2] == nums[i], where 2 < = I < = n - 1.
• nums[i - 1] != nums[i], where 1 < = I < = n - 1.

In one step, you can select the subscript i and change num [i] to any positive integer.

Returns the minimum number of operands that make an array an alternating array.

```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. Get the operand of 0 - game 280 week question 1

Give you two nonnegative integers num1 and num2.

In each step of operation, if num1 > = num2, you must subtract num2 from num1; Otherwise, you have to subtract num1 from num2.

• For example, if num1 = 5 and num2 = 4, you should subtract num2 from num1, so you get num1 = 1 and num2 = 4. However, if num1 = 4 and num2 = 5, after one operation, num1 = 4 and num2 = 1 are obtained.

Returns the operand that makes num1 = 0 or num2 = 0.

```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;
}
}
```