当前位置:网站首页>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 .
边栏推荐
猜你喜欢

< li> dot style list style type

Chapter III principles of MapReduce framework

第5章 消费者组详解

(lightoj - 1369) answering queries (thinking)

Audio and video development interview questions

Anaconda下安装Jupyter notebook

新手必会的静态站点生成器——Gridsome

js封装数组反转的方法--冯浩的博客

第5章 NameNode和SecondaryNameNode

QT simulates mouse events and realizes clicking, double clicking, moving and dragging
随机推荐
MP4格式详解
第7章 __consumer_offsets topic
第2章 HFDS的Shell操作
Sublime text code formatting operation
Chapter 1 overview of MapReduce
SQL快速入门
第6章 Rebalance详解
Codeforces Round #803 (Div. 2)A~C
Chapter 2 shell operation of hfds
去掉input聚焦时的边框
第一章 MapReduce概述
Codeforces - 1526C1&&C2 - Potions
Tert butyl hydroquinone (TBHQ) Industry Research Report - market status analysis and development prospect forecast
第5章 消费者组详解
Problem - 922D、Robot Vacuum Cleaner - Codeforces
【锟斤拷】的故事:谈谈汉字编码和常用字符集
Summary of FTP function implemented by qnetworkaccessmanager
Raspberry pie 4b64 bit system installation miniconda (it took a few days to finally solve it)
音视频开发面试题
Click QT button to switch qlineedit focus (including code)