作者oin1104 (是oin的說)
看板Marginalman
標題Re: [閒聊] 每日leetcode
時間Thu Jun 20 11:35:50 2024
In the universe Earth C-137, Rick discovered a special form of magnetic force be
tween two balls if they are put in his new invented basket. Rick has n empty bas
kets, the ith basket is at position[i], Morty has m balls and needs to distribut
e the balls into the baskets such that the minimum magnetic force between any tw
o balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y
is |x - y|.
Given the integer array position and the integer m. Return the required force.
翻譯 :
m個男人上廁所 廁所的位子是position[i]
怎麼才可以讓大家上廁所的間隔距離都最遠
思路 :
一開始看到 幹 該不會又是二分搜
跑去留言區偷看
看到這個翻譯 好好笑
就是 要對距離二分搜
每種距離都試試看可不可以塞進m個男人
二分搜真的寫起來超麻煩的 姆咪
```cpp
class Solution {
public:
bool find(vector<int>& position , int m , int d)
{
int len = position.size();
int pre = position[0];
int all = 1;
for(int i = 1 ; i < len ; i ++)
{
if(position[i]-pre >= d)
{
pre = position[i];
all ++;
}
}
return (all >= m);
}
int maxDistance(vector<int>& position, int m)
{
int len = position.size();
sort(position.begin() , position.end());
int l = 1;
int r = position[len-1]-position[0];
int mid = 0;
while(l<=r)
{
mid = (l+r)/2;
bool ok = find(position,m,mid);
if(ok)l = mid +1;
else r = mid -1;
}
return r;
}
};
```
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 1.162.53.5 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1718854552.A.42A.html
→ yam276: 我以為你的答案是尿在PY裡面 06/20 11:37
→ oin1104: 我吐了 甲甲退散 06/20 11:39
→ SydLrio: 整天做這種死人骨頭,就不能像個正常 06/20 11:56
→ SydLrio: 男生玩屁眼和雞雞嗎? 06/20 11:56
推 Neuenmuller: 笑死這個翻譯100分 06/20 12:34