Leecode weekly contest 287 | All questions solutions

Hello guys , I shared my solution for leetcode contest 287
I solved 3 questions in contest & stucked in forth questions ,(decrypt function gave TLE)
image

Q1) Minimum Number of Operations to Convert Time

class Solution {
public:
    int convertTime(string current, string correct) {
        int currMin = stoi(current.substr(0,2)) * 60 + (stoi(current.substr(3,2)));
        int corrMin = stoi(correct.substr(0,2)) * 60 + (stoi(correct.substr(3,2)));
        int diff = corrMin - currMin;
        int cnt = 0;
        if(diff == 0)return 0;
        while(diff != 0){
            if(diff >= 60){
                diff -= 60;
            }else if(diff >= 15){
                diff-=15;
            }else if(diff >= 5){
                diff -= 5;
            }else{
                diff -=1;
            }
            cnt++;
        }
        return cnt;
    }
};

Q2) Find Players With Zero or One Losses

#topic : hashtable
class Solution {
public:
    vector<vector<int>> findWinners(vector<vector<int>>& matches) {
        map<int,int> mp1,mp2;
        
        for(auto &it : matches){
            mp1[it[0]]++;
            mp2[it[1]]++;
        }
        vector<vector<int>>ans(2);
        for(auto &it : mp1){
            if(mp2.count(it.first) == 0){
                ans[0].push_back(it.first);
            }
        }
        for(auto &it : mp2){
            if(it.second == 1){
                ans[1].push_back(it.first);
            }
        }
        return ans;
    }
};

Q3 ) Maximum Candies Allocated to K Children

#topic : binary search
class Solution {
    bool isPossible(vector<int>&arr,int maxCandies,long long k){
        int n = arr.size();
        long long cnt = 0;
        for(int i = 0; i < n;i++){
                cnt += 1LL*(arr[i] / maxCandies);
        }
        // cout<<maxCandies<<endl;
        return cnt >= k;
    }
public:
    int maximumCandies(vector<int>& candies, long long k) {
        long long lo = 1,hi = 0;
        for(int x : candies){
            hi += x;
        }
        if(hi < k)return 0;
        int ans = 0;
        while(lo <= hi){
            long long  mid = lo + (hi - lo)/2;
            bool flag = isPossible(candies,mid,k);
            if(flag == true){
                lo = mid + 1;
                if(mid > ans)ans = mid;
            }else{
                hi = mid - 1;
            }
        }
        return ans;
    }
};

Q4) Encrypt and Decrypt Strings
My solution : https://leetcode.com/discuss/interview-question/1909347/encrypt-and-decrypt-strings-oror-brute-force-or-TLE-solution

Here I  took little reference for decrypt function

class Encrypter {
    unordered_map<char,string>key;
    unordered_map<string,int> dict;
public:
    Encrypter(vector<char>& keys, vector<string>& values, vector<string>& dictionary) {
        for(int i = 0; i < keys.size(); i++){
            key[keys[i]]  = values[i];
        }
        // dictionary contain decrypted data
        for(auto &it :dictionary){
		// encrypting data 
            string p = encrypt(it);
            dict[p]++;
        }
    }
    
    string encrypt(string word1) {
        string s = "";
        for(int i = 0; i < word1.size();i++){
            s+= (key[word1[i]]);
        }
        return s;
    }
    
    int decrypt(string word2) {
       auto it = dict.find(word2);
       if(it != dict.end())return it->second;
       return 0;
    }

};
Comments (0)