当前位置:网站首页>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.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
- all
position
The 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 .
边栏推荐
- Base dice (dynamic programming + matrix fast power)
- 业务系统从Oracle迁移到openGauss数据库的简单记录
- Bidirectional linked list - all operations
- < li> dot style list style type
- Solve the problem of intel12 generation core CPU [small core full, large core onlookers] (win11)
- Chapter 5 yarn resource scheduler
- <li>圆点样式 list-style-type
- Educational Codeforces Round 130 (Rated for Div. 2)A~C
- Input can only input numbers, limited input
- Codeforces Round #803 (Div. 2)A~C
猜你喜欢
浏览器打印边距,默认/无边距,占满1页A4
Solve the problem that intel12 generation core CPU single thread only runs on small cores
解决Intel12代酷睿CPU单线程调度问题(二)
Oneforall installation and use
原生js实现全选和反选的功能 --冯浩的博客
Summary of game theory
软通乐学-js求字符串中字符串当中那个字符出现的次数多 -冯浩的博客
第2章 HFDS的Shell操作
Problem - 922D、Robot Vacuum Cleaner - Codeforces
MP4格式详解
随机推荐
sublime text 代码格式化操作
拉取分支失败,fatal: ‘origin/xxx‘ is not a commit and a branch ‘xxx‘ cannot be created from it
第5章 NameNode和SecondaryNameNode
Educational Codeforces Round 122 (Rated for Div. 2)
Summary of FTP function implemented by qnetworkaccessmanager
Browser print margin, default / borderless, full 1 page A4
Chapter 5 yarn resource scheduler
Market trend report, technical innovation and market forecast of double-sided foam tape in China
Useeffect, triggered when function components are mounted and unloaded
Codeforces Round #800 (Div. 2)AC
第7章 __consumer_offsets topic
useEffect,函數組件掛載和卸載時觸發
Spark的RDD(弹性分布式数据集)返回大结果集
Codeforces Round #798 (Div. 2)A~D
ffmpeg命令行使用
CMake速成
useEffect,函数组件挂载和卸载时触发
Tert butyl hydroquinone (TBHQ) Industry Research Report - market status analysis and development prospect forecast
Base dice (dynamic programming + matrix fast power)
QT implementation fillet window