当前位置:网站首页>LeetCode 1552. Magnetic force between two balls
LeetCode 1552. Magnetic force between two balls
2022-07-06 16:42:00 【Daylight629】
1552. The magnetic force between the two balls
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 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 .
Tips :
n == position.length2 <= n <= 10^51 <= position[i] <= 10^9- all
positionThe integer Different from each other . 2 <= m <= position.length
Two 、 Method 1
Two points search
class Solution {
public int maxDistance(int[] position, int m) {
Arrays.sort(position);
int l = 1;
int r = position[position.length - 1] - position[0];
int res = -1;
while (l <= r) {
int mid = l + ((r - l) >> 1);
if (check(mid, position, m)) {
res = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
return res;
}
public boolean check(int x, int[] position, int m) {
int pre = position[0];
int cnt = 1;
for (int i = 1; i < position.length; i++) {
if (position[i] - pre >= x) {
pre = position[i];
cnt++;
}
}
return cnt >= m;
}
}
Complexity analysis
Time complexity :O(nlog(nS)), among n Is the number of baskets ,S Is the upper limit of basket position . Sorting basket positions requires O(nlogn) Time complexity of , Binary search divides the basket position interval , need O(logS) Time complexity of . Whether the statistical answer meets the requirements every time O(n) Time complexity of , So the total time complexity is O(nlogn+nlogS)=O(nlog(nS)).
Spatial complexity :O(logn), That is, the stack space required for sorting .
边栏推荐
- VMware Tools和open-vm-tools的安装与使用:解决虚拟机不全屏和无法传输文件的问题
- (lightoj - 1370) Bi shoe and phi shoe (Euler function tabulation)
- SQL quick start
- (lightoj - 1236) pairs forming LCM (prime unique decomposition theorem)
- (lightoj - 1369) answering queries (thinking)
- China double brightening film (dbef) market trend report, technical dynamic innovation and market forecast
- 【锟斤拷】的故事:谈谈汉字编码和常用字符集
- Installation and configuration of MariaDB
- Soft music -js find the number of times that character appears in the string - Feng Hao's blog
- Useeffect, triggered when function components are mounted and unloaded
猜你喜欢

QT realizes window topping, topping state switching, and multi window topping priority relationship

Native JS realizes the functions of all selection and inverse selection -- Feng Hao's blog

Use JQ to realize the reverse selection of all and no selection at all - Feng Hao's blog

The concept of spark independent cluster worker and executor

Remove the border when input is focused

图像处理一百题(1-10)

Li Kou: the 81st biweekly match

Mp4 format details

第5章 NameNode和SecondaryNameNode

第6章 DataNode
随机推荐
Tree of life (tree DP)
第7章 __consumer_offsets topic
Ffmpeg command line use
CMake Error: Could not create named generator Visual Studio 16 2019解决方法
Kubernetes集群部署
Soft music -js find the number of times that character appears in the string - Feng Hao's blog
Pull branch failed, fatal: 'origin/xxx' is not a commit and a branch 'xxx' cannot be created from it
antd upload beforeUpload中禁止触发onchange
useEffect,函数组件挂载和卸载时触发
Codeforces Round #802(Div. 2)A~D
<li>圆点样式 list-style-type
input 只能输入数字,限定输入
Chapter III principles of MapReduce framework
Business system compatible database oracle/postgresql (opengauss) /mysql Trivia
Detailed explanation of FLV format
Bidirectional linked list - all operations
300th weekly match - leetcode
(lightoj - 1236) pairs forming LCM (prime unique decomposition theorem)
FLV格式详解
Educational Codeforces Round 130 (Rated for Div. 2)A~C