leetcode周赛 (2020/05/17)

这次周赛题比较暴力,没有涉及什么算法,最后一个题是个数学题。

1. 在既定时间做作业的学生人数

leetcode周赛 (2020/05/17)_第1张图片

思路

暴力即可

代码

class Solution {
public:
    int busyStudent(vector<int>& startTime, vector<int>& endTime, int queryTime) {
        int N = startTime.size();
        int ans = 0;
        for(int i=0;i<N;i++)
        {
            if(queryTime>=startTime[i]&&queryTime<=endTime[i])
                ans++;
        }
        return ans;
    }
};

2. 重新排列句子中的单词

leetcode周赛 (2020/05/17)_第2张图片

思路

排序即可

代码

class Solution {
public:
    static bool cmp(pair<string,int> p1,pair<string,int> p2)
    {
        if(p1.first.length()==p2.first.length())
            return p1.second<p2.second;
        return p1.first.length()<p2.first.length();
    }
    string arrangeWords(string text) {
        istringstream ss(text);
        string str;
        string ans;
        vector<pair<string,int> > vec;
        int ind = 0;
        while(ss>>str)
        {
            if(str[0]<=90)
                str[0]+=32;
            vec.push_back(make_pair(str,ind++));
        }
        sort(vec.begin(),vec.end(),cmp);
        for(int i=0;i<vec.size();i++)
        {
            if(i!=0)
                ans+=" ";
            string tmp = vec[i].first;
            if(i==0)
            {
                if(tmp[0]>=97)
                    tmp[0]-=32;
            }
            ans+=tmp;
        }
        return ans;
    }
};

3. 收藏清单

leetcode周赛 (2020/05/17)_第3张图片

思路

python的issubset()函数直接判断
C++有includes()函数

代码

class Solution:
    def peopleIndexes(self, favoriteCompanies: List[List[str]]) -> List[int]:
        fav = []
        for x in favoriteCompanies:
            s = set()
            for company in x:
                s.add(company)
            fav.append(s)
        ans = []
        N = len(fav)
        for i in range(N):
            flag = True
            for j in range(N):
                if i==j:
                    continue
                if fav[i].issubset(fav[j]):
                    flag = False
                    break
            if flag:
                ans.append(i)
        return ans

4.圆形靶的最大飞镖数量

leetcode周赛 (2020/05/17)_第4张图片

思路

数学题

代码1

class Solution {
public:
    static int count(vector<vector<int>>points, double x, double y, int r) {
        int ans = 0;
        for (auto point : points) {
            double dx = x - point[0];
            double dy = y - point[1];
            if (dx * dx + dy * dy <= r * r + 1e-8) {
                ans++;
            }
        }
        return ans;
    }
    int numPoints(vector<vector<int>>& points, int r) {
        int n = points.size();
        if (n == 1) {
            return 1;
        }
        int ans = count(points, points[0][0], points[0][1], r);
        for (int i = 0; i < n; i++) {
            for (int j = i+1; j < n; j++) {
                double dx = points[i][0] - points[j][0];
                double dy = points[i][1] - points[j][1];
                if (points[i][0] == points[j][0] && points[i][1] == points[j][1]) {
                    continue;
                }
                if(dx*dx+dy*dy>4.0*r*r)
                    continue;
                double midx = (points[i][0] + points[j][0]) / 2.0;
                double midy = (points[i][1] + points[j][1]) / 2.0;
                double dis = sqrt(dx * dx + dy * dy);
                double cosx = dx/dis;
                double sinx = dy/dis;
                double h = sqrt(r * r - dis / 2 * dis / 2);
                double rx = midx - sinx * h;
                double ry = midy + cosx * h;
                int cnt = count(points, rx, ry, r);
                ans = max(ans, cnt);
            }
        }
        return ans;
    }
};

代码2

struct Point {
	double x, y;
	Point() {}
	Point(double tx, double ty) { x = tx; y = ty; }
};
const double eps = 1e-10;
class Solution {
public:
    double dist(Point p1,Point p2) {
	    return sqrt(pow(p1.x - p2.x, 2) + pow(p1.y - p2.y, 2));
    }

    Point GetCircleCenter(Point p1, Point p2, double r) {
	    Point mid = Point((p1.x + p2.x) / 2, (p1.y + p2.y) / 2);
	    double angle = atan2(p1.x - p2.x, p2.y - p1.y);
	    double d = sqrt(r*r - pow(dist(p1, mid), 2));
	    return Point(mid.x + d*cos(angle), mid.y + d*sin(angle));
    }
    
    int numPoints(vector<vector<int>>& points, int r) {
        int ans = 1;
        for (int i = 0; i < points.size(); ++i) {
            for (int j = i + 1; j < points.size(); ++j) {
                if(dist(Point(1.0 * points[i][0], 1.0 * points[i][1]), 
                        Point(1.0 * points[j][0], 1.0 * points[j][1])) > 2.0*r)
                    continue;
                Point center = GetCircleCenter(
                               Point(1.0 * points[i][0], 1.0 * points[i][1]), 
                               Point(1.0 * points[j][0], 1.0 * points[j][1]), 
                               1.0 * r
                               );
                int cnt = 0;
				for(int k = 0; k < points.size(); ++k)
                    if(dist(center, Point(1.0 * points[k][0], 1.0 * points[k][1])) 
                       < 1.0*r + eps) 
                        cnt++;
				ans = max(ans,cnt);
            } 
        }
        return ans;
    }
};

作者:dqsjysgs
链接:https://leetcode-cn.com/problems/maximum-number-of-darts-inside-of-a-circular-dartboard/solution/bao-li-mei-ju-by-dqsjysgs/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

你可能感兴趣的:(leetcode周赛,leetcode)