当前位置:网站首页>Leetcode-1552. Magnetic force between two balls
Leetcode-1552. Magnetic force between two balls
2022-06-12 05:57:00 【Taoist scholar】
link
1552. The magnetic force between the two balls
subject
Under the code name C-137 On the earth ,Rick Found that if he put two balls in his newly invented basket , There will be a special form of magnetic force between them .Rick Yes n An empty basket , The first i The position of the basket is position[i] ,Morty Want to put m Put a ball in these baskets , Between any two balls Minimum magnetic force Maximum .
It is known that if two balls are located at x and y , Then the magnetic force between them is |x - y| .
Give you an array of integers position And an integer m , Please return to maximize the minimum magnetic force .
Example

Example 1:
Input :position = [1,2,3,4,7], m = 3
Output :3
explain : take 3 Put the balls in the 1,4 and 7 The three baskets , The magnetic force between the two balls is [3, 3, 6]. The minimum magnetic force is 3 . We can't make the minimum magnetic force greater than 3 .Example 2:
Input :position = [5,4,3,2,1,1000000000], m = 2
Output :999999999
explain : We use the 1 and 1000000000 The basket has the minimum magnetic force and the maximum .
explain
- n == position.length
- 2 <= n <= 10^5
- 1 <= position[i] <= 10^9
- all position The integer Different from each other .
- 2 <= m <= position.length
Ideas
Reference resources Try to answer the official questions
If this question is derived from the front, it is specific m The position of the ball will be more complicated , However , We can reverse judgment .
As the topic requires to maximize the minimum magnetic force , So the value we are looking for is a critical value ,
Suppose the answer is ans, That is, the minimum magnetic force is ans, So we know that less than ans Your answer must also be legal ( Because the answer is ans when , The balls are as far apart as possible , Less than ans Words , The balls can get closer and closer ), But more than ans All values of are illegal , So we can do a binary search on the answer , Find the answer directly and reversely .
So how to judge whether it is legal or illegal ? It's also very simple. , With greedy thinking , When the answer is ans when , That is, the minimum distance between two balls is ans, In order to minimize the waste of space , Let the distance between each ball be ≥ans The minimum value of , If it can fit in the basket ≥m A ball is legal , Otherwise it's illegal .
We are [left,right] Interval search of , The mean for mid, Do the following :
- If the current mid legal , Then order ans=mid, And reduce the interval to [mid+1,right];
- If the current mid illegal , Then the interval is reduced to [left,mid−1].
- The minimum distance between two balls is 1, The maximum spacing is the position of the last basket - The position of the smallest basket , These two values are the initial left and right.
C++ Code
class Solution {
public:
bool check(int x, vector<int>& position, int m) {
int pre = position[0], cnt = 1;
for (int i = 1; i < position.size(); ++i)
{
if (position[i] - pre >= x)
{
pre = position[i];
cnt += 1;
}
}
return cnt >= m;
}
int maxDistance(vector<int>& position, int m) {
sort(position.begin(), position.end());
int left = 1, right = position.back() - position[0], ans = -1;
while (left <= right)
{
int mid = (left + right) / 2;
if (check(mid, position, m))
{
ans = mid;
left = mid + 1;
}
else right = mid - 1;
}
return ans;
}
};
边栏推荐
- 项目开发流程简单介绍
- MySQL master-slave, 6 minutes to master
- Beginning is an excellent emlog theme v3.1, which supports emlog Pro
- Flex / fixed Upper, Middle and Lower (Mobile end)
- BlockingQueue interface introduction
- March 4, 2021
- flex/fixed上中下(移動端)
- [untitled]
- Select gb28181, RTSP or RTMP for data push?
- [machine learning] first day of introduction
猜你喜欢

肝了一个月的 DDD,一文带你掌握

Unable to access this account. You may need to update your password or grant the account permission to synchronize to this device. Tencent corporate email

Data integration framework seatunnel learning notes
![[grpc development] go language builds simple server and client](/img/24/06c3c1219ecad7e117f4df152e9ce7.jpg)
[grpc development] go language builds simple server and client
![Leetcode buckle -10 Regular expression matching analysis [recursion and dynamic programming]](/img/25/b3c475e2b03c39b7c576b6d01f9d56.jpg)
Leetcode buckle -10 Regular expression matching analysis [recursion and dynamic programming]

Simple introduction to key Wizard

Filter的注解配置

Halcon 用点来拟合平面

Beginning is an excellent emlog theme v3.1, which supports emlog Pro
![[long time series prediction] the [4] autocorrelation mechanism of aotoformer code explanation](/img/12/27531fc791b3f49306385831309c5e.png)
[long time series prediction] the [4] autocorrelation mechanism of aotoformer code explanation
随机推荐
POI, easyexcel framework use
Project and build Publishing
json-c常用API
Why do I object so [1.01 to the power of 365 and 0.99 to the power of 365]
Niuke daily question -day1
Unity vscode cannot jump to definition
[fastapi] use pycharm to configure and use environment variables for fastapi projects
Glossary of Chinese and English terms for pressure sensors
Filter的注解配置
Brief introduction to project development process
Research Report on market supply and demand and strategy of China's digital camera lens industry
Types, functions and applications of intelligent sensors
Laravel8 when search
User login (medium)
Mysql笔记
Brief summary of software project architecture
TCP and UDP introduction
[untitled]
Leetcode simple problem: converting an integer to the sum of two zero free integers
Tabulation skills and matrix processing skills