jobin491's blog

By jobin491, history, 13 months ago, In English

Problem

You have n tasks and m workers. Each task has a strength requirement stored in a 0-indexed integer array tasks, with the ith task requiring tasks[i] strength to complete. The strength of each worker is stored in a 0-indexed integer array workers, with the jth worker having workers[j] strength. Each worker can only be assigned to a single task and must have a strength greater than or equal to the task's strength requirement (i.e., workers[j] >= tasks[i]). Additionally, you have pills magical pills that will increase a worker's strength by strength. You can decide which workers receive the magical pills, however, you may only give each worker at most one magical pill. Given the 0-indexed integer arrays tasks and workers and the integers pills and strength, return the maximum number of tasks that can be completed.

below is the code that i tried with a complexity of N(logN)(logN). N=max(n,m); approach: binary search on answer, check the mid smallest tasks: start from the largest task and worker if(task<worker) this worker completes task without pill. if(task>worker) we check for the smallest worker who can complete this task with a pill.

class Solution {
public:
    int maxTaskAssign(vector<int>& tasks, vector<int>& workers, int p, int strength) {
        int ans = 0;
        int n=tasks.size();
        int m=workers.size();
        sort(workers.rbegin(),workers.rend());
        sort(tasks.rbegin(),tasks.rend());
        int j=0;
        int hi=m;
        int lo=0;
        int fin=0;
        int pills=p;
        while(lo<=hi){
            int mid= lo+hi;mid/=2;
            // check the mid smallest tasks;
            pills=p;
            if(mid>n){
                hi=mid-1;
                continue;
            }
            vector<bool> t(m+1,false);
            j=0;
            ans=0;
            for(int i=n-mid;i<n;i++){
                while(j<=m && t[j]) j++;
                if(j>=m) break;
                if(tasks[i]<=workers[j]){
                    t[j]=true;
                    ans++;
                }else{
                    if(strength+workers[j]<tasks[i]){
                        continue;
                    }
                    if(pills>0){
                        int req= tasks[i]-strength;
                        int llo=j,lhi=m-1;
                        int ind=j;
                        while(llo<=lhi){
                            int lmid= (llo+lhi)/2;
                            if(workers[lmid]<req){
                                lhi=lmid-1;
                            }else{
                                ind=lmid;
                                llo=lmid+1;
                            }
                        }
                        while(ind>0 && t[ind]) ind--;
                        t[ind]=true;
                        ans++;
                        pills--;
                    }
                }
            }
            if(ans>=mid){
                lo=mid+1;
                fin=mid;
            }else{
                hi=mid-1;
            }
        }
        return fin;
    }
};

this gives a right answer on all testcases but exceeds time limit. when i checked the correct solution it was with the same complexity using mulitsets

class Solution {
public:
    int maxTaskAssign(vector<int>& tasks, vector<int>& workers, int p, int strength) {
        int n = tasks.size(), m = workers.size();
        
        // Sorting the tasks and workers in increasing order
        sort(tasks.begin(), tasks.end());
        sort(workers.begin(), workers.end());
        int lo = 0, hi = min(m, n);
        int ans;
        
        while(lo <= hi) {
            int mid = lo + (hi - lo) / 2;
            int count = 0;
            bool flag = true;
            
            // Inserting all workers in a multiset
            multiset<int> st(workers.begin(), workers.end());
            
            // Checking if the mid smallest tasks can be assigned
            for(int i = mid - 1; i >= 0; i--) {
                
                // Case 1: Trying to assing to a worker without the pill
                auto it = prev(st.end());
                if(tasks[i] <= *it) {
                    
                    // Case 1 satisfied!
                    st.erase(it);
                } else {
                    
                    // Case 2: Trying to assign to a worker with the pill
                    auto it = st.lower_bound(tasks[i] - strength);
                    if(it != st.end()) {
                        
                        // Case 2 satisfied!
                        count++;
                        st.erase(it);
                    } else {
                        
                        // Case 3: Impossible to assign mid tasks
                        flag = false;
                        break;
                    }
                }
                
                // If at any moment, the number of pills require for mid tasks exceeds 
                // the allotted number of pills, we stop the loop
                if(count > p) {
                    flag = false;
                    break;
                }
            }
            
            if(flag) {
                ans = mid;
                lo = mid + 1;
            } else {
                hi = mid - 1;
            }
        }
        return ans;
    }
};

I am unable to figure out the reason for failure in my code if anyone could help me with it I'd really appreciate it.

Full text and comments »

  • Vote: I like it
  • +6
  • Vote: I do not like it

By jobin491, history, 14 months ago, In English

Recently I solved a leetcode problem with a complexity of O(n*logn) however it beats almost all O(n) solutions in terms of runtime. Could anyone help me analyze the complexity of my solution.

i also noticed that the while loop only goes on for about 3-4 iterations on some test cases that i tried although i am not sure if this is the case for all testcases.

Problem: In the world of Dota2, there are two parties: the Radiant and the Dire.

The Dota2 senate consists of senators coming from two parties. Now the Senate wants to decide on a change in the Dota2 game. The voting for this change is a round-based procedure. In each round, each senator can exercise one of the two rights:

Ban one senator's right: A senator can make another senator lose all his rights in this and all the following rounds. Announce the victory: If this senator found the senators who still have rights to vote are all from the same party, he can announce the victory and decide on the change in the game. Given a string senate representing each senator's party belonging. The character 'R' and 'D' represent the Radiant party and the Dire party. Then if there are n senators, the size of the given string will be n.

The round-based procedure starts from the first senator to the last senator in the given order. This procedure will last until the end of voting. All the senators who have lost their rights will be skipped during the procedure.

Suppose every senator is smart enough and will play the best strategy for his own party. Predict which party will finally announce the victory and change the Dota2 game. The output should be "Radiant" or "Dire".

My submission: here

class Solution {
public:
    string predictPartyVictory(string senate) {
        int br=0;
        int bd=0;
        string s=senate;
        while(s.size()){
            string temp="";
            for(auto it:s){
                if(it=='R'){
                    if(br>0){
                        br--;
                    }else{
                        temp+='R';
                        bd++;
                    }
                }else{
                    if(bd>0){
                        bd--;
                    }else{
                        temp+='D';
                        br++;
                    }
                }
            }
            bool flag=false;
            cout<<temp<<" "<<br<<" "<<bd<<endl;
            for(int i=0;i<temp.size()-1;i++){
                if(temp[i]!=temp[i+1]){
                    flag=true;break;
                }
            }
            // cout<<falg;
            s=temp;
            if(!flag || s.size()==1){
                break;
            }
        }
        
        if(s[0]=='R'){
            return "Radiant";
        }else{
            return "Dire";
        }
        
    }
};

Full text and comments »

  • Vote: I like it
  • +8
  • Vote: I do not like it