Question: Paypal , Recent Online Assessment Questions (19th September 2023) | Task Scheduling | Sequential String
5
Entering edit mode

ADD COMMENTlink 16 months ago Delta 2.9k
0
Entering edit mode

Sequential String 
 

vector<int> solution(const string &s, const vector<string> &q) {
    const int n = s.length(), m = q.size();
    vector<vector<int>> have(10);
    for (int i = 0; i < n; ++i) {
        have[s[i] - '0'].push_back(i + 1);
    }
    vector<int> r(m);
    for (int i = 0; i < m; ++i) {
        vector<int> num(10);
        for (char c : q[i]) {
            const int x = c - '0';
            if (++num[x] > have[x].size()) {
                r[i] = -1;
                break;
            }
            r[i] = max(r[i], have[x][num[x] - 1]);
        }
        // cout << r[i] << endl;
    }
    return r;
}

int main() {
    solution("111222333444", {"121", "3", "12345", "11234"});
}
ADD COMMENTlink 16 months ago Abhi • 0
Entering edit mode
0

Task Scheduling 
 

int solution(const vector<int> &m, const vector<int> &t, const int &limit) {
    unordered_map<int, vector<int>> have;
    for (int i = 0; i < t.size(); ++i) {
        have[t[i]].push_back(m[i]);
    }
    int r = 0;
    for (auto& p : have) {
        auto& v = p.second;
        sort(v.begin(), v.end());
        for (int i = 0, j = v.size() - 1; i <= j; ++r, --j) {
            if (i != j && v[i] + v[j] <= limit) {
                ++i;
            }
        }
    }
    return r;
}

int main() { 
    cout << solution({1, 2, 3, 4, 2}, {1, 2, 1, 2, 3}, 4) << endl;
    cout << solution({1, 2, 5}, {1, 2, 3}, 6) << endl;
    return 0;
}
ADD REPLYlink 16 months ago
Abhi
• 0
0
Entering edit mode

Post the answer in java

ADD COMMENTlink 15 months ago IamLvs • 0
0
Entering edit mode
post the answer in java
ADD COMMENTlink 15 months ago IamLvs • 0

Login before adding your answer.

Similar Posts
Loading Similar Posts