Leetcode周赛189
暴力完事
class Solution {
public:
int busyStudent(vector<int>& startTime, vector<int>& endTime, int queryTime) {
// int a[1001];
int ans=0;
for(int i=0; i<startTime.size(); i++)
{
if(startTime[i]<=queryTime&&endTime[i]>=queryTime)
ans++;
}
return ans;
}
};
直接拆分排序组合
class Solution {
public:
static bool cmp(pair<int,string> &a,pair<int,string> &b){
return a.second.length()==b.second.length()?a.first<b.first:a.second.length()<b.second.length();
}
string arrangeWords(string text) {
vector<pair<int,string>> a;
string ans="";
int id=0;
int len=text.length();
int i=0;
string temp="";
while(i<len){
temp="";
while(i<len&&text[i]!=' '){
temp+=text[i++];
}
i++;
if(id==0)
temp[0]=temp[0]+32;
a.push_back(make_pair(id,temp));
id++;
}
sort(a.begin(),a.end(), cmp);
for(i=0; i<a.size()-1; i++)
ans+=a[i].second+" ";
ans += a[a.size()-1].second;
ans[0] -=32;
return ans;
}
};
先查总的独特的公司名,然后将每个人的收藏转换成bool数组,暴力就行
class Solution {
public:
bool in(bool cnt[100][1001], int i, int j, int len){
for(int k=0; k<len; k++)
if(cnt[i][k]==1&&cnt[j][k]==0)
return false;
return true;
}
vector<int> peopleIndexes(vector<vector<string>>& a) {
bool cnt[100][1001];
memset(cnt,0,sizeof cnt);
set<string> se;
for(auto i:a){
for(auto ii:i)
se.insert(ii);
}
int len=se.size();
map<string, int> mp;
int id=0;
for(auto &i:se){
mp[i]=id++;
}
for(int i=0; i<a.size(); i++){
for(auto ii:a[i])
cnt[i][mp[ii]]=1;
}
vector<int> ans;
for(int i=0; i<a.size(); i++){
bool flag=true;
for(int j=0; j<a.size(); j++)
{
if(i==j||!in(cnt,i,j,len))
continue;
else
{
flag=false;
break;
}
}
if(flag)
ans.push_back(i);
}
return ans;
}
};
没做出来,看了题解发现,最贪心的做法是让边界上起码有两个点
然后利用两个点和半径确定圆心,求当前的最大的覆盖,O(n^3)复杂度
class Solution:
def numPoints(self, points: List[List[int]], r: int) -> int:
def dist2(p1: List[int], p2: List[int]) -> int:
return (p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2
def getCircle(p1: List[int], p2: List[int]) -> List[float]:
mid = [(p1[0] + p2[0]) / 2, (p1[1] + p2[1]) / 2]
angle = math.atan((p1[0] - p2[0]) / (p2[1] - p1[1])) if p2[1] - p1[1] else math.pi / 2
d = math.sqrt(r2 - dist2(p1, mid))
return [mid[0] + d * math.cos(angle), mid[1] + d * math.sin(angle)]
n = len(points)
ans = 1
eps = 1e-6
r2 = r * r
for i in range(n):
for j in range(i + 1):
if dist2(points[i], points[j]) > 4 * r2:
continue
center = getCircle(points[i], points[j])
count = 0
for k in range(n):
if dist2(points[k], center) < r2 + eps:
count += 1
ans = max(ans, count)
return ans
一条有梦想的咸鱼