C++ solution with multimap


  • 0
    J
    class LogSystem {
    private:
        multimap<string, int> timetoid;
        unordered_map<string, int> gratosize;
    public:
        LogSystem() {
            gratosize.insert({"Year",4});
            gratosize.insert({"Month",7});
            gratosize.insert({"Day",10});
            gratosize.insert({"Hour",13});
            gratosize.insert({"Minute",16});
            gratosize.insert({"Second",19});
        }
        
        void put(int id, string timestamp) {
            timetoid.insert({timestamp, id});
        }
        
        vector<int> retrieve(string s, string e, string gra) {
            vector<int> res;
            string news = s.substr(0, gratosize[gra]);
            string newe = e.substr(0, gratosize[gra]);
            for(auto it = timetoid.begin(); it != timetoid.end(); it++) {
                if(it->first.compare(news) >= 0) {
                    if(it->first.substr(0, gratosize[gra]) <= newe)
                        res.push_back(it->second);
                    else
                        break;
                }
            }
            return res;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.