1.8 Learning blog

I practiced some questions about binary search today.
The first question: https://leetcode-cn.com/problems/capacity-to-ship-packages-within-d-days/
Insert picture description here
Insert picture description here
Since this question asks for the capacity of a cargo ship, it is easy to know that the minimum capacity is The maximum value of all cargo weights, and the maximum is the sum of all cargo weights. The binary search can be performed from this aspect.
Part of the code:
int shipWithinDays(int* weights, int weightsSize, int D){ int min=-1,max,sum=0; max=weights[0]; for(int i=0;i<weightsSize;i++){ sum+=weights[i]; if(max<weights[i])max=weights[i]; } int left=max,right=sum; while(left<=right){//二分int mid=(left+ right)/2; int k=0,day=0; for(int i=0;i<weightsSize;i++){ k+=weights[i];//accumulate if(k>mid){ day++; k =weights[i];//Take the weights[i] this time as the weight of the goods pulled on another day } }

















day++;//!~!! No less, the day before the cycle
if(day>D){//The capacity is too small, and the time exceeded
left=mid+1;
}
else{//In time D
right=mid -1;
if(min==-1)min=mid;// assign the mid that meets the requirements for the first time to min
min = mid<min?mid:min;//take the smallest capacity that meets the requirements
}
}
return min;
}

Question 2: https://leetcode-cn.com/problems/magnetic-force-between-two-balls/
Insert picture description here

Insert picture description here
Drawing lessons from the official problem of Leetcode and the thinking of the big guys, given n empty baskets, the positions of m balls have been determined. So how do we calculate the "minimum magnetic force"?

It is not difficult to conclude that the "minimum magnetic force" is the minimum value of the distance between two adjacent balls among the m balls. For a ball with three positions i<j<k, the minimum magnetic force must be the smaller value of j−i and k−j, not the difference k−i between i and k across position j.

The calculation method of the minimum magnetic force at a given position is clarified. In this question, the position of the m balls is determined by us. We only know the position of the empty basket, and the question hopes to "maximize the minimum magnetic force" by arranging the positions of the m balls.

We assume that the final minimum magnetic force is ans, then we know that an answer smaller than ans must be legal. Since we have a method of placing the minimum distance between adjacent balls to be greater than or equal to ans, it must be greater than any value in [1,ans−1], and any value greater than ans is illegal, so we can Perform a binary search on the answer.
For this question, our dichotomy changes the scope of the answer, and finally approaches the answer step by step. When m = 2, the maximum distance between the minimum magnetic force of all balls is the head and tail of the container. When m = 3, the maximum distance we can find is the head minus the tail divided by 2. We can take it slowly Pushing down, the idea is to split the ball as much as possible. So we can get the maximum length of every two balls is (position[positionSize-1]-position[0])/(m-1), and the minimum length is of course 1.

Okay, now we can use dichotomy. We use dichotomy to find the length after equalization and see which fits. Remember that dichotomy here is the possible length (between the minimum and maximum). Next, we have to verify whether the length of the middle that we divide at this time can successfully make the ball match after placement. So we have to verify, that is, the corresponding check function.

In the body of the check function, we need to verify one by one. The idea of ​​verification is to start from the far left, and then start to put the ball at every interval divided by two. If the number of intervals that can be dropped is greater than m or the number of intervals is greater than or equal to m-1, then the distance we divided at this time is It is feasible, so the binary search to the right is performed again. Finally find the distance separated.

Part of the code: The
Insert picture description here
third question: https://leetcode-cn.com/problems/kth-smallest-number-in-multiplication-table/ This
Insert picture description here
question is actually very simple in general, but if you use dichotomy, it is worth thinking about :
Learn from the big guy’s explanation of the problem of dichotomy
1. Finding the k-th smallest and k-th largest problem is generally solved by dichotomy, so the dichotomy requires the data points and comparison methods of dichotomy.
2. The minimum value in the current multiplication table must be 1, and the maximum value is m*n, which is the minimum and maximum value of our table, and the data points are found.
3. Comparison method, to find the k-th smallest, then when we get the mid data point, look at the current mid position. If the mid position is greater than k, it means that the mid value is too large, and we need to perform a binary solution again.
4. The question is, how to find this position. Assuming that mid is in the specified row i, then mid/i is the position of the column of the mid element. If the position of the column is greater than the maximum length of the column n, then all data in the row is less than mid, just add n.
If not, it means that the correct row has been found, only the position of the mid/i column is the number of elements in this row less than mid.
5. The processing of boundary conditions is more important in the dichotomy. We are looking for the k-th smallest value, which can be understood as assuming that when mid is at a certain value, the condition is met, and we need to lock the large value, that is, count>=k , To adjust the value of l to keep approaching.
When the value of l approaches a certain data, the condition is met again, that is, when the loop condition ends, so that the value of l must be in the original list, because +1 or -1 will cause the condition to be unsatisfied.
Of course, our >= condition is also not allowed, because the value of l is locked after it is satisfied.

Note: This question is to find the first value that satisfies K, so it is to find the left boundary.

Part of the code:
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_47529865/article/details/112370174