../ Competitive Programming
-rw-r--r--aguang3.8 KiB3 min read

Leet Code Weekly Contest 287

1. Minimum Number of Operations to Convert Time

  • Greedy.
c++
class Solution {
public:
    int toMins(string time) {
        int h = 10 * (time[0] - '0') + (time[1] - '0');
        int m = 10 * (time[3] - '0') + (time[4] - '0');
        return h * 60 + m;
    }
    
    int convertTime(string current, string correct) {
        int x = toMins(current), y = toMins(correct);
        int ret = 0;
        
        while (x < y) {
            if (x + 60 <= y) {
                x += 60;
            } else if (x + 15 <= y) {
                x += 15;
            } else if (x + 5 <= y) {
                x += 5;
            } else if (x + 1 <= y) {
                x += 1;
            }
            ret++;
        }
        
        return ret;
    }
};

2. Find Players With Zero or One Losses

  • Hash-map.
c++
class Solution {
public:
    vector<vector<int>> findWinners(vector<vector<int>>& matches) {
        map<int, vector<int>> lost;
        set<int> ids;
        
        for (auto &match : matches) {
            int win = match[0], lose = match[1];
            lost[lose].push_back(win);
            ids.insert(win);
            ids.insert(lose);
        }
        
        vector<vector<int>> ans(2);
        
        for (auto &i : ids) {
            if (lost[i].size() == 0) {
                ans[0].push_back(i);
            }
            if (lost[i].size() == 1) {
                ans[1].push_back(i);
            }
        }
        
        return ans;
    }
};

3. Maximum Candies Allocated to K Children

  • Binary-search.
c++
typedef long long ll;

class Solution {
public:
    ll maxChildren(vector<int> &candies, ll size) {
        ll ret = 0;
        for (auto &c : candies) {
            ret += c / size;
        }
        return ret;
    }
    
    int maximumCandies(vector<int>& candies, long long k) {
        ll low = 1, high = 0, ret = 0;
        for (auto &c : candies) {
            high = max(high, (ll) c);
        }
        while (low <= high) {
            ll mid = (low + high) / 2;
            if (maxChildren(candies, mid) >= k) {
                ret = max(ret, mid);
                low = mid + 1;
            } else {
                high = mid - 1;
            }
        }
        return ret;
    }
};

4. Encrypt and Decrypt Strings

  • Brute-force.
c++
class Encrypter {
private:
    map<char, string> keyToValue_;
    map<string, set<char>> valueToKeys_;
    vector<string> dictionary_;
    
public:
    Encrypter(vector<char>& keys, vector<string>& values, vector<string>& dictionary) {
        for (int i = 0; i < keys.size(); i++) {
            keyToValue_[keys[i]] = values[i];
            valueToKeys_[values[i]].insert(keys[i]);
        }
        dictionary_ = dictionary;
    }
    
    string encrypt(string word1) {
        string ret = "";
        for (auto &c : word1) {
            if (keyToValue_.find(c) != keyToValue_.end()) {
                ret += keyToValue_[c];
            }
        }
        return ret;
    }
    
    int decrypt(string word2) {
        vector<set<char>> candidates;
        for (int i = 0; i < word2.size(); i += 2) {
            string part = "";
            part.push_back(word2[i]);
            part.push_back(word2[i + 1]);
            candidates.push_back(valueToKeys_[part]);
        }
        int ret = 0;
        for (auto &dict : dictionary_) {
            if (dict.length() != candidates.size()) {
                continue;
            }
            bool valid = true;
            for (int i = 0; i < dict.length(); i++) {
                if (candidates[i].find(dict[i]) == candidates[i].end()) {
                    valid = false;
                    break;
                }
            }
            if (valid) {
                ret++;
            }
        }
        return ret;
    }
};

COMMAND INTERFACE

↑ ↓ navigate · Enter execute · Esc exit · try “whoami”

Loading filesystem…

:help aguang.xyz

A personal website disguised as an operating system.

Ctrl / Cmd + K
Command palette + full-text search
Tab / Shift + Tab
Move between links and controls
Enter
Open a focused link or search result
↑ / ↓ in search
Navigate search results
In a directory filter
Enter/Done or ↓ focuses a result; Esc clears the filter
Esc
Clear a search or close a window