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. 重新排列句子中的單詞

在這裏插入圖片描述

思路

排序即可

代碼

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. 收藏清單

在這裏插入圖片描述

思路

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.圓形靶的最大飛鏢數量

在這裏插入圖片描述

思路

數學題

代碼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)
著作權歸作者所有。商業轉載請聯繫作者獲得授權,非商業轉載請註明出處。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章