Task Scheduling

  • + 0 comments

    My C++ solution:

    map<int,int> mp = {{0,0}};                  //d->exceed
    map<int,int>::iterator maxdif = mp.begin();   //iterator to mp item with most exceed
    
    
    map<int,int>::iterator getIterator(int d) {
        if (d<=maxdif->first) return maxdif;
        auto iv = mp.insert({d,0});
        auto it =iv.first;
        if (iv.second) {    //new d;
            auto previt = it;
            previt--;
            it->second = previt->second+previt->first-it->first;
        }
        return it;
    }
    
    int taskScheduling(int d, int m) {
        map<int,int>::iterator it = getIterator(d);
        while (it!=mp.end()) {
            it->second += m;
            if (it->second > maxdif->second) {
                maxdif=it;
            }
            it++;
        }
        return maxdif->second;
    }