返回顶部

LeetCode第 287 场周赛题解

2224. 转化时间需要的最少操作数

题目描述:给你两个24小时制的时间\(a , b(a \leq b)\),每次你可以对a增加1 , 5 , 1560分钟,问最小的操作次数使得a = b

思路:根据题意贪心即可

时间复杂度:\(O(1)\)

参考代码:

class Solution {
public:
    int convertTime(string s, string t) {
        int sh = 10 * (s[0] - '0') + (s[1] - '0'), sm = 10 * (s[3] - '0') + (s[4] - '0');
        int eh = 10 * (t[0] - '0') + (t[1] - '0'), em = 10 * (t[3] - '0') + (t[4] - '0');
        int res = 0;
        if(em >= sm) res += eh - sh;
        else res += eh - sh - 1 , em += 60;
        em -= sm;

        res += em / 15;
        em %= 15;
        res += em / 5;
        em %= 5;
        res += em;
        return res;
    }
};

2225. 找出输掉零场或一场比赛的玩家

题目描述:给你\(n\)个描述\((a , b)\)表示一场比赛中玩家a赢了玩家b,问所有文明有输掉任何比赛的玩家列表和恰好输掉一场比赛的列表。

思路:根据题意模拟即可

时间复杂度:\(O(nlogn)\)

参考代码:

class Solution {
public:
    vector<vector<int>> findWinners(vector<vector<int>>& matches) {
        vector<int>win(100005 , 0) ,lose(100005 , 0);
        set<int>s;
        for(auto&& vec : matches){
            win[vec[0]]++;
            lose[vec[1]] ++;
            s.insert(vec[0]);
            s.insert(vec[1]);
        }
        vector<vector<int>> res(2);
        for(auto&& vec : s){
            if(lose[vec] == 0) res[0].push_back(vec);
            else if(lose[vec] == 1) res[1].push_back(vec);
        }
        return res;
    }
};

2226. 每个小孩最多能分到多少糖果

题目描述:给你\(n\)堆糖果,你可以将糖果分成任意大小的堆,但不能合并,有\(k\)个孩子每人获得一堆且数量相同的糖果,问每个人分得的糖果的最大数量是多少。

思路:比较明显的二分答案

时间复杂度:\(O(nlog\mathop{max}\limits_{i = 1}^{n}\;a_i)\)

参考代码:

class Solution {
public:
    int maximumCandies(vector<int>& candies, long long k) {
        int lr = 1 , rs = 1e7 + 5 , res = 0;
        auto check = [&](int mid)->bool{
            long long cnt = 0;
            for(auto&& candie : candies) cnt += candie / mid;
            return cnt >= k;
        };
        while(lr <= rs){
            int mid = lr + rs >> 1;
            if(check(mid)) res = mid , lr = mid + 1;
            else rs = mid - 1;
        }
        return res;
    }
};

2227. 加密解密字符串

题目描述:自己读题吧

思路:对于加密操作,直接进行模拟即可,对于解密操作,可以将操作反过来,将字典内的字符串进行加密,然后用map存储下来,每次询问直接访问map进行查找即可,时间复杂度为\(O(logn)\)

时间复杂度:加密操作复杂度\(O(n)\),解密操作复杂度\(O(logn)\)

参考代码:

class Encrypter {
private:
    vector<int>idx;
    vector<vector<int>>reidx;
    vector<char> k;
    vector<string>v;
    vector<string> d;
    map<string , int>mp;
public:
    Encrypter(vector<char>& keys, vector<string>& values, vector<string>& dictionary) {
        k = keys;
        idx = vector<int>(300 , 0);
        reidx = vector<vector<int>>(10000);
        for(int i = 0 ; i < keys.size() ; ++i) idx[keys[i]] = i;
        v = values;
        d = dictionary;
        for(auto&& s : d){
            string t = encrypt(s);
            mp[t]++;
        }
    }
    
    string encrypt(string word1) {
        string res;
        for(auto&& c : word1) res += v[idx[c]];
        return res;
    }
    
    int decrypt(string word2) {
        if(mp.count(word2)) return mp[word2];
        return 0;
    }
};

/**
 * Your Encrypter object will be instantiated and called as such:
 * Encrypter* obj = new Encrypter(keys, values, dictionary);
 * string param_1 = obj->encrypt(word1);
 * int param_2 = obj->decrypt(word2);
 */
posted @ 2022-04-08 11:13  cherish-lgb  阅读(43)  评论(0编辑  收藏  举报