[Algorithm Beating Diary] day07——904. Fruits in a Basket, 438. Find all letter anagrams in the string

 904. Fruit basket

904. Fruit basket

Topic description:

You are visiting a farm with a row of fruit trees planted from left to right. The trees are  fruits represented by an array of integers, where  fruits[i] is  the type ofi  fruit on the first 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 fruits:

  • You only have  two  baskets, and each basket can only hold  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 array of integers  , return the maximumfruits number  of fruits you can collect   .

Problem-solving ideas: 

This problem can be converted into the longest continuous subarray (there can only be two types of fruits at most), and can be solved using a sliding window

We go right from left to right. When we find a subsequence that meets the conditions, we need to continue to the right to verify the longest feature. How to do this? Let’s take a look. The following [left, right] is a subsequence that meets the conditions of the question during the search process.

 At this time, right needs to move to the right++. Two situations may occur at this time:

  • After right++, kind (type of fruit) remains unchanged.
  • After right++, kind decreases,

It is worth noting here that when the types of fruits are reduced, some processing is required.

 Solution code:

class Solution {
public:
    int totalFruit(vector<int>& fruits) {
        unordered_map<int,int> hash;
        int ret=0;
        for(int left=0,right=0;right<fruits.size();right++)
        {
            hash[fruits[right]]++;
            while(hash.size()>2)
            {
                hash[fruits[left]]--;
                if(hash[fruits[left]]==0)
                hash.erase(fruits[left]);
                left++;
            }
            ret=max(ret,right-left+1);
        }
        return ret;
    }
};

438. Find all anagrams of letters in a string

LeetCode official website - the technology growth platform loved by geeks around the world

Topic description: 

Given two strings  s sum  p, find  all  substrings of allophones s  in  and return the starting index of these substrings. The order of answer output is not taken into account.p 

Allophones  refer to strings formed by the rearrangement of the same letters (including identical strings).

 Problem-solving ideas:

Let’s first analyze the problem through a brute force solution:

We can traverse each position in s and let them determine whether they are anagrams with p.

We found that this is a solution for the sliding interval [left, right]. Let’s find a way to optimize it.

First, we can use the hash table hash1 to store each character and its number in p, and we can use hash2 to record the characters and number in s during the sliding period.

We also need a variable length=p.size() and a variable count to record the number of valid characters.

1. Enter the window——hash2[s[right]]++

if(hash2[s[right]]<=hash1[s[right]]) count++;

2. Judge when right-left+1>length

3. Out of the window——hash2[s[left]]--

if(hash2[s[left]]<=hash1[s[left]]) count--;//Update count before exiting the window

4. Update result——count==length

!The hash table here can be replaced by an array of size 26 and is faster

Solution code:

class Solution {
public:
    vector<int> findAnagrams(string s, string p) {
        vector<int>ret;
        int length=p.size();
        unordered_map<char,int>hash1;
        for(int i=0;i<p.size();i++) hash1[p[i]]++;
        unordered_map<char,int>hash2;
        for(int left=0,right=0,count=0;right<s.size();right++)
        {
            char ch=s[right];
            //进窗口
            hash2[ch]++;
            if(hash2[ch]<=hash1[ch]) count++;
            //判断
            if(right-left+1>length)
            {
                char out=s[left];
                if(hash2[out]<=hash1[out]) count--;//在出窗口前更新count
                hash2[out]--;//出窗口
                left++;
            }
            //更新结果
            if(count==length)
                ret.push_back(left);
        }
        return  ret;
    }
};

Guess you like

Origin blog.csdn.net/m0_69061857/article/details/132993527