[Solution] "100 Lectures on Zero Basics of Algorithms" (Lesson 32) Multidimensional Enumeration (2) - Advanced

1385. Distance value between two arrays

iterate over all elements

class Solution {
    
    
public:
    int findTheDistanceValue(vector<int>& arr1, vector<int>& arr2, int d) {
    
    
        int ans=0;
        for(int i=0;i<arr1.size();i++) {
    
    
            int flag=1;
            for(int j=0;j<arr2.size();j++) {
    
    
                if(abs(arr1[i]-arr2[j])<=d) {
    
    
                    flag=0;
                    break;
                }
            }
            if(flag) ans++;
        }
        return ans;
    }
};

1291. Sequence Times

enumerate

The answer string must be a continuous substring of "123456789", so two layers of loops are used to enumerate the starting point and length of the substring, and the obtained substring is converted into an integer to determine whether it is in the given interval.

class Solution {
    
    
public:
    int stoi(string ch) {
    
    
        int ans=0;
        for(int i=0;i<ch.size();i++) {
    
    
            ans=ans*10+(ch[i]-'0');
        }
        return ans;
    }
    vector<int> sequentialDigits(int low, int high) {
    
    
        string res="123456789";
        vector<int>ans;
        for(int i=0;i<10;i++) {
    
    
            for(int j=1;j+i<10;j++) {
    
    
                string ch=res.substr(i,j);
                int t=stoi(ch);
                if(t>=low&&t<=high) ans.push_back(t);
            }
        }
        sort(ans.begin(),ans.end());
        return ans;
    }
};

2048. Next Larger Numerical Balance

Enumeration judgment

Enumerate all numbers that are more than n, and judge whether each number is balanced in turn

class Solution {
    
    
public:
    bool check(int n) {
    
    
        vector<int>cnt(10);
        while(n) {
    
    
            int v=n%10;
            if(v==0) return false;
            cnt[v]++;
            n/=10;
        }
        for(int i=1;i<10;i++) {
    
    
            if(cnt[i]&&cnt[i]!=i) return false;
        }
        return true;
    }
    int nextBeautifulNumber(int n) {
    
    
        for(int i=n+1;;i++)
            if(check(i)) return i;
        return -1;
    }
};

349. Intersection of Two Arrays

enumerate elements in two sets

Enumerate the elements in the first set and record which elements appear. Enumerate the second set, and if the element is found in the first set, add it to the answer set. Finally, you only need to deduplicate the answer set

class Solution {
    
    
public:
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
    
    
        unordered_map<int,int>mp;
        vector<int>ans;
        for(auto t:nums1) mp[t]++;
        for(auto t:nums2) 
            if(mp.find(t)!=mp.end()) ans.push_back(t);
        sort(ans.begin(),ans.end());
        ans.erase(unique(ans.begin(),ans.end()),ans.end());
        return ans;
    }
};

1995. Statistical special quadruples

Four layers of violence

Since the data range is so small, violence is definitely okay

class Solution {
    
    
public:
    int countQuadruplets(vector<int>& nums) {
    
    
        int ans=0;
        for(int i=0;i<nums.size();i++) {
    
    
            for(int j=i+1;j<nums.size();j++) {
    
    
                for(int k=j+1;k<nums.size();k++) {
    
    
                    for(int g=k+1;g<nums.size();g++) {
    
    
                        if(nums[i]+nums[j]+nums[k]==nums[g]) ans++;
                    }
                }
            }
        }
        return ans;
    }
};

Optimization of enumeration

a+b+c=d can be transformed into a+b=dc, and due to the stipulation of the subscript size relationship in the title, we can use c as the boundary to enumerate the value of c, and at the same time enumerate a smaller than c, b, and find their sum, then enumerate the larger d to find if it appears in the sum

class Solution {
    
    
public:
    int countQuadruplets(vector<int>& nums) {
    
    
        int ans=0;
        for(int c=0;c<nums.size();c++) {
    
    
            unordered_map<int,int>mp;
            for(int a=0;a<c;a++) {
    
    
                for(int b=a+1;b<c;b++) {
    
    
                    mp[nums[a]+nums[b]]++;
                }
            }
            for(int d=c+1;d<nums.size();d++) {
    
    
                if(mp.find(nums[d]-nums[c])!=mp.end()) ans+=mp[nums[d]-nums[c]];
            }
        }
        return ans;
    }
};

1566. A pattern of length M repeated at least K times

brute force enumeration

Violently enumerate each string of length m*k, and then check whether it is a cyclic string of period m

class Solution {
    
    
public:
    bool containsPattern(vector<int>& arr, int m, int k) {
    
    
        if(arr.size()<m*k) return false;
        for(int i=0;i<=arr.size()-m*k;i++) {
    
    
            int flag=1;
            for(int j=i+m;j<i+m*k;j++) {
    
    
                if(arr[j]!=arr[j-m]) {
    
       
                    flag=0;
                    break;
                }
            }
            if(flag) return true; 
        }
        return false;
    }
};

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324040629&siteId=291194637