Блог пользователя jobin491

Автор jobin491, история, 13 месяцев назад, По-английски

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";
        }
        
    }
};
  • Проголосовать: нравится
  • +8
  • Проголосовать: не нравится

»
13 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by jobin491 (previous revision, new revision, compare).

»
13 месяцев назад, скрыть # |
 
Проголосовать: нравится +11 Проголосовать: не нравится

We can observe that when two consecutive steps of while loop are executed, number of voters halve: Each voter banned another voter or got banned. Time complexity of operations of one iteration of while loop is O(n) where n is the current number of voters. Then, total time complexity T(n) = 2*O(n)+T(n/2) = O(n)+T(n/2) = O(n+n/2+n/4+...) = O(n).

»
13 месяцев назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

I believe that your solution is also $$$O(N)$$$, though I am unable to give a formal proof at this time. The intuition is that even in the "worst case" $$$O(\log N)$$$ loops, you don't work with the whole string. First you work with a string of length $$$N$$$, but then the string gets cut in half, then a quarter, ... . So you actually get $$$N+\frac N2+\frac N4 + ... = N\cdot \left( 1+\frac 12 + \frac 14 + ... \right) = 2N \in O(N)$$$.