Inscription LeetCodeGOGOGO brush 04-- code optimization (pretreatment)

The first complete online simulation, the overall feeling is clear thinking is very important to finish the title retrospect difficulty is not great, but if we can take advantage of pre-treatment, making the overall thinking more clearly, you can make coding easier and more efficient

836. Rectangle Overlap

Difficulty:

Easy

Ideas:

To the two rectangles, determines whether or not two rectangles overlap.

Category talk clearly. Appears to be very complicated, there are many different situations look, however, think it through, the situation is very simple.

First, the lower left side of the matrix. If the right side of the matrix overlap, only possible on the upper side, lower side or the right side, discuss the classification of these three cases, the remaining cases are overlapping

Code:

/*
Author Owen_Q
*/

class Solution {
public:
    bool isRectangleOverlap(vector<int>& rec1, vector<int>& rec2) {
        if(rec1[0]>rec2[0])
            swap(rec1,rec2);
        if(rec2[1]>=rec1[3]||rec2[0]>=rec1[2])
            return false;
        if(rec2[3]<=rec1[1])
            return false;
        else
            return true;
    }
};

763. Partition Labels

Difficulty:

Medium

Ideas:

Given a string, it requires all of the same letters must be in the same group, seeking the maximum division scheme.

Obviously this is a simulation questions traversal strings, the basic complexity will O (n ^ {2}).

First, the outer loop through each of the divided start position, the inner loop to find the length of each partition.

However, the inner loop will be a process of several rounds, each round is to find the length of the current situation, as far as the same letter, and then to update existing letters, increasing the length, look for the farthest letter under the new length again. Until when there is a constant length, until no new elements to increase, that has found a packet.

Code:

/*
Author Owen_Q
*/


class Solution {
public:
    vector<int> partitionLabels(string S) {
        int n = S.size();
        vector<int> re;
        bool in[26];
        memset(in,false,sizeof in);
        for(int st = 0; st<n; st++)
        {
            int en = -1;
            int newend = st;
            in[S[st]-'a'] = true;
            while(en!=newend)
            {
                //cout << en << "*" << newend << endl;
                en = newend;
                for(newend=n-1;newend>en;newend--)
                {
                    if(in[S[newend]-'a'])
                        break;
                }
                for(int nowpos = en+1; nowpos < newend; nowpos++)
                    in[S[nowpos]-'a'] = true;
            }
            re.push_back(en-st+1);
            //cout << en-st+1;
            st = en;
        }
        return re;
    }
};

Code upgrade:

In fact, in this case the idea has been relatively clear, excellence, still found a way to more optimized, that is, pre-treatment.

Now that we know the complexity of the algorithm is O (n ^ {2}), and taking into account the farthest position of the same element is very important, as pretreatment directly, so that makes the whole idea is very clear.

With an array of pre-treatment in case we need only one cycle can be solved. Each element is traversed to find the longest coincidence position, if the location is the same as the current position, to obtain a packet, is very clear, and although this will somewhat affect the operation of the processing time, but it is readily understood that the code, stability It is also very high, not easily influenced by the input data

Code:

/*
Author Owen_Q
*/


class Solution {
public:
    vector<int> partitionLabels(string S) {
        int n = S.size();
        vector<int> re;
        vector<int> np;
        for(int i=0;i<n;i++)
        {
            for(int j=n-1;j>=i;j--)
            {
                if(S[i]==S[j])
                {
                    np.push_back(j);
                    break;
                }
            }
        }
        
        int nowpos = 0;
        int nexp = 0;
        for(int st = 0; st<n; st++)
        {
            nexp = max(nexp,np[st]);
            if(nexp == st)
            {
                re.push_back(st-nowpos+1);
                nowpos = st+1;
            }
        }
        return re;
    }
};

 

Published 97 original articles · won praise 89 · views 20000 +

Guess you like

Origin blog.csdn.net/Owen_Q/article/details/104046287