Skip to content

Commit

Permalink
Time: 272 ms (21.41%), Space: 57.9 MB (84.18%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
Dipendra-Raghav committed Feb 27, 2023
1 parent b6b90d8 commit dbdffaa
Showing 1 changed file with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class Solution {
public:
bool isChk(vector<int> &P, int mid, int M)
{
int prev = 0;
int count = 1;
for(int i=1;i<P.size();i++)
{
if(abs(P[prev]-P[i]) >= mid)
{
prev = i;
count++;
}
if(count >= M)
return true;
}
return count >= M;
}
int maxDistance(vector<int>& nums, int m) {
sort(nums.begin(),nums.end());
int s=1;
int e=nums[nums.size()-1];
int ans=0;
while(s<e)
{
int mid=(s+e)/2;
if(isChk(nums,mid,m))
{
ans=mid;
s=mid+1;
}
else
e=mid;
}
return ans;

}
};

0 comments on commit dbdffaa

Please sign in to comment.