Leetcode Weekly Contest 152

Now, even the elderly retired leetcode will not do a = =
do this morning leetcode third question wrong topic, with the middle game still tune submission of experiments, one mind directly gg
lesson is summarized under this title slag do now topics are not clear even begin to do. The sample should go over the questions before beginning to write, and then re 1-2 example of my own, and then start

A problem: count the number of prime numbers (prime sieve or sqrt (n) can be determined), and then calculate COUNT!

class Solution {
public:
    int numPrimeArrangements(int n) {
        vector<int> has(n + 5, 0);
        int cntPrime = 0;
        const int MOD = 1e9 + 7;
        for(int i = 2; i <= n; ++i) {
            if(has[i] == 0) cntPrime ++;
            for(int j = i * 2; j <= n; j += i) {
                has[j] = 1;
            }
        }
        int result = 1;
        for(int i = 1; i <= n - cntPrime; ++i) {
            result = 1ll * i * result % MOD;
        }
        for(int i = 1; i <= cntPrime; ++i) {
            result = 1ll * i * result % MOD;
        }
        return result;
    }
};

Problem B: Scroll sum

class Solution {
public:
    int dietPlanPerformance(vector<int>& calories, int k, int lower, int upper) {
        long long sum = 0;
        for(int i = 0; i < k; ++i) {
            sum += calories[i];
        }
        int result = 0;
        for(int i = k - 1, len = calories.size(); i < len; ++i) {
            if(sum < lower) result --;
            else if(sum > upper) result ++;
            if(i != len - 1) {
                sum -= calories[i - k + 1];
                sum += calories[i + 1];
            }
        }
        return result;
    }
};

Question C: I put the question before the entire string as Italy meet is a palindrome, a lot of this complex. . . .
The correct solution is the number of letters for each interval determination, we can know the even paired direct (symmetrically placed like), counting the number of letters is odd, odd half the like need to change

class Solution {
private:
    vector<int> has[26];
    int get(int id, int fr, int to) {
        if(fr > to) return 0;
        if(fr == 0) return has[id][to];
        else return has[id][to] - has[id][fr - 1];
    }
public:
    vector<bool> canMakePaliQueries(string s, vector<vector<int>>& queries) {
        
        int slen = s.length();
        for(int i = 0; i < 26; ++i) has[i].clear();
        
        /***statistic character count*****/
        for(int i = 0; i < 26; ++i) {
            for(int j = 0; j < slen; ++j) {
                has[i].push_back(0);
            }
        }
        for(int i = 0; i < slen; ++i) {
            has[s[i] - 'a'][i] ++;
        }
        for(int i = 0; i < 26; ++i) {
            for(int j = 1; j < slen; ++j) {
                has[i][j] += has[i][j-1];
            }
        }

        
        vector<bool> result;
        for(int i = 0, len = queries.size(); i < len; ++i) {
            int left = queries[i][0]; int right = queries[i][1]; int k = queries[i][2];

            int cnt = 0;
            for(int j = 0; j < 26; ++j) {
                int t1 = get(j, left, right);
                if(t1 % 2 == 1) {
                    cnt ++;
                }
            }
            cnt /= 2;
            if(cnt <= k) result.push_back(true);
            else result.push_back(false);
        }
        
        return result;
    }
};

D title: The basic idea is that violence inquiry, first of all we know, the letters are arranged around a string of occurrences and it does not matter (except for the first letter of the puzzle issue), this can be processed
in addition there two acceleration methods
method 1: 26-bit compressed, whether directly through the digital word is judged to meet the puzzle
method 2: Create the dictionary for word tree, let puzzle dfs carried in the dictionary tree, where each puzzle will be about 7! query complexity

The following two codes correspond two solutions, I discuss direct climb down inside the big brother

class Solution {
    vector<int> base;
    void born(vector<string>& words){
        for(auto& s: words){
            set<char> tmp;
            int bit = 0;
            for(auto c:s){
                tmp.insert(c);
                bit = bit | (1<<(c-'a'));
            }
            if(tmp.size() >7)continue;
            base.push_back(bit);
        }
    }
public:
    vector<int> findNumOfValidWords(vector<string>& words, vector<string>& puzzles) {
        vector<int> ans;
        born(words);
        
        for(auto& s: puzzles){
            int num = 0;
            int bit = 0;
            for(auto c: s){
                bit = bit | (1<<(c-'a'));
            }
            int firstBit = 1 << (s[0] - 'a');
            for(auto v: base){
                if((v & bit) == v && ((firstBit & v) == firstBit)){
                    num++;
                }
            }
            ans.push_back(num);
        }
        
        return ans;
    }
};
const int ALPHABET_SIZE = 26;

/* The structure of a trie node */
struct TrieNode
{
    struct TrieNode* children[ALPHABET_SIZE];
    int count = 0;
};

/* Creates a new trie node and returns the pointer */
struct TrieNode* getNode()
{
    struct TrieNode* newNode = new TrieNode;

    for(int i=0; i<ALPHABET_SIZE; i++)
        newNode->children[i] = nullptr;
    
    newNode->count = 0;

    return newNode;
}

/* Inserts the given string to the collection */
void insert(struct TrieNode* root, string str)
{
    struct TrieNode* pCrawl = root;

    for(int i=0; i<str.length(); i++)
    {
        int index = str[i]-'a';

        if(!pCrawl->children[index])
            pCrawl->children[index] = getNode();
        
        pCrawl = pCrawl->children[index];
    }

    pCrawl->count = (pCrawl->count + 1);
}

/* Returns the count of strings which are valid */
int search(struct TrieNode* root, string str, bool firstSeen, char firstLetter)
{
    if(!root)
        return 0;
    
    int count = 0;
    
    if(firstSeen)
        count += root->count;
    
    for(int i=0; i<str.length(); i++)
    {
        int index = str[i] - 'a';
        
        if(str[i] == firstLetter)
            count += search(root->children[index], str, true, firstLetter);
        else
            count += search(root->children[index], str, firstSeen, firstLetter);
    }

    return count;
}

class Solution
{
public:
    vector<int> findNumOfValidWords(vector<string>& words, vector<string>& puzzles);
};

vector<int> Solution :: findNumOfValidWords(vector<string>& words, vector<string>& puzzles)
{
    struct TrieNode* root = getNode();
    
    for(auto str : words)
    {
        set<char> temp;
        temp.insert(str.begin(), str.end());
        
        string sorted = "";
        for(auto ele : temp)
            sorted += ele;
        
        insert(root, sorted);
    }
    
    vector<int> count;
    for(auto puzzle : puzzles)
    {
        char firstLetter = puzzle[0];
        sort(puzzle.begin(), puzzle.end());
        count.push_back(search(root, puzzle, false, firstLetter));
    }
    
    return count;
    
}

Guess you like

Origin www.cnblogs.com/Basasuya/p/11442368.html