[Preferred Algorithm Question Practice] day5


1. 904. Fruit basket

1. Introduction to the topic

904. Fruit in Baskets
You are visiting a farm with a row of fruit trees planted from left to right. These trees are represented by an integer array fruits, where fruits[i] is the type of fruit on the i-th tree.
You want to collect as many fruits as possible. However, the owner of the farm has set some strict rules that you must follow to pick the fruit: you
only have two baskets, and each basket can only contain a single type of fruit. There is no limit to the total amount of fruit that can be placed in each basket.
You can choose any tree to start picking, and you must pick exactly one fruit from each tree (including the tree you started picking from). Fruit picked should match the type of fruit in the basket. Each time you pick, you will move to the right to the next tree and continue picking.
Once you get to a tree and the fruit doesn't match the fruit type for the basket, you have to stop picking.
Given an integer array fruits, return the maximum number of fruits you can collect.
Insert image description here
Insert image description here

2. Problem-solving ideas

3.Code

class Solution {
    
    
public:
    int totalFruit(vector<int>& fruits) {
    
    
        int left = 0, right = 0;
        int ret = 0;
        int kinds = 0;
        vector<int> ma(100000);
        while(right < fruits.size())
        {
    
    
            //入窗口
            if(ma[fruits[right]] == 0) kinds++;
            ma[fruits[right++]]++;
            //出窗口
            while(kinds > 2)
            {
    
    
                ma[fruits[left]]--;
                if(ma[fruits[left]] == 0) kinds--;
                left++;
            }
            //更新结果
            ret = max(ret, right - left);
        }
        return ret;
    }
};

4. Running results

Insert image description here

2. 438. Find all letter anagrams in the string

1. Introduction to the topic

438. Find all anagrams of letters in a string.
Given two strings s and p, find the substrings of all anagrams of p in s and return the starting index of these substrings. The order of answer output is not taken into account.
Allophones refer to strings formed by the rearrangement of the same letters (including identical strings).
Insert image description here

2. Problem-solving ideas

3.Code

class Solution {
    
    
public:
    vector<int> findAnagrams(string s, string p) {
    
    
        int n = s.size(), m = p.size();
        vector<int> vp(26, 0);
        for(auto& e : p)
        {
    
    
            vp[e - 'a']++;
        }
        vector<int> vc(26, 0);
        int left = 0, right = 0;
        int count = 0;
        vector<int> ret;
        while(right < n)
        {
    
    
            //ruchaungkou
            if(++vc[s[right] - 'a'] <= vp[s[right] - 'a']) count++;
            //chuchuangkou
            if(right - left + 1 > m)
            {
    
    
                if(vc[s[left] - 'a']-- <= vp[s[left] - 'a']) count--;
                left++;
            }
            if(count == m) ret.push_back(left);
            right++;
        }
        return ret;
    }
};

4. Running results

Insert image description here

3. 30. Concatenate substrings of all words

1. Introduction to the topic

30. Concatenate the substrings of all words.
Given a string s and a string array words. All strings in words are of the same length.
The concatenated substring in s refers to a substring containing all the strings in words arranged in any order and connected.
For example, if words = ["ab", "cd", "ef"], then "abcdef", "abefcd", "cdabef", "cdefab", "efabcd", and "efcdab" are all concatenated substrings. "acdbef" is not a concatenated substring, because it is not a concatenation of any permutations of words.
Returns the starting index in s of all concatenated strings. You can return answers in any order.
Insert image description here
Insert image description here

2. Problem-solving ideas

3.Code

class Solution {
    
    
public:
    vector<int> findSubstring(string s, vector<string>& words) {
    
    
        unordered_map<string, int> ma1;//words中单词及出现的频率
        //map<第一个元素, 第二个元素>
        for(auto& e : words)
        {
    
    
            ma1[e]++;
        }
        int len = words[0].size(), m = words.size();
        int left = 0, right = 0;
        vector<int> ret;
        for(int i = 0;i < len; ++i)
        {
    
    
            unordered_map<string, int> ma2;
            int count = 0;//
            for(int left = i, right = i;right + len <= s.size(); right += len)
            {
    
    
                //入窗口
                string t1 = s.substr(right, len);
                if(++ma2[t1] <= ma1[t1]) count++;
                //出窗口
                while(right - left + 1 > len * m)
                {
    
    
                    string t2 = s.substr(left, len);
                    if(ma2[t2]-- <= ma1[t2]) count--;
                    left += len;
                }
                //更新结果
                if(count == m)//判断当前滑动窗口是否满足题目要求
                {
    
    
                    ret.push_back(left);
                }
            }
        }
        return ret;
    }
};

4. Running results

Insert image description here


Summarize

Today is the fifth day of algorithm practice.
God rewards hard work , keep up the good work.
Source of the question: LeetCode, the copyright belongs to LeetCode.
If this article has inspired you, I hope you can support the author more, thank you all!

Guess you like

Origin blog.csdn.net/xjjxjy_2021/article/details/131658541