leetcode 155 Zhou race

Links: https://leetcode-cn.com/contest/weekly-contest-155

Give you a integer array arr, where each element is different.
You have to find all the elements of the minimum absolute difference, and returned in ascending order.

Ideas:
1. First sort
2. Find the smallest gap
3. traversed once again find the answer
4. The time complexity is mainly Sort nlogn

class Solution {
public:
    vector<vector<int>> minimumAbsDifference(vector<int>& arr) {
        vector<vector<int>> res;
        sort(arr.begin(),arr.end());
        int minx=INT_MAX;
        for(int i=1;i<arr.size();++i){
            minx=min(minx,arr[i]-arr[i-1]);
        }
        for(int i=1;i<arr.size();++i){
            if(minx==arr[i]-arr[i-1]){
                vector<int> v;
                v.push_back(arr[i-1]);
                v.push_back(arr[i]);
                res.push_back(v);
            }
        }
        return res;
    }
};

Ask you to help design a program used to find the n-th ugly numbers.
The number can be ugly or a positive integer divisible by b or c.

Ideas:
1. half answer
2. Each request to obtain the number of mid ugly number
3. The inclusion and exclusion seeking the number
4. The three least common multiple is equal to number to obtain the first two least common multiple of the least common multiple and the third number to give

class Solution {
public:
    #define LL long long
    int nthUglyNumber(int n, int a, int b, int c) {
        LL x=1ll*a*b/gcd(a,b);
        LL y=1ll*a*c/gcd(a,c);
        LL z=1ll*b*c/gcd(b,c);
        LL l=1,r=2000000000;
        LL t=1ll*x*c/gcd(x,c);
        while(l<r)
        {
            LL mid = (l+r)>>1;
            int cot = mid/a+mid/b+mid/c-mid/x-mid/y-mid/z+mid/t;
            if(cot>=n)r=mid;
            else l=mid+1;
        }
        return l;
    }
    int gcd(int a,int b)
    {
        return b==0?a:gcd(b,a%b);
    }
};

You give a string s, and some of the string "index" array pairs, wherein the pairs [i] = [a, b] represents the index of the string in two (numbered from zero).
You can exchange any number of times any character index in pairs in a pair.
Returns the minimum lexicographical ordering string after several exchange, s can become a.

Ideas:
1. disjoint-set
2. linked to each other to maintain a set of
3. "boss" as the index to add characters in the collection come in and sort
4. followed by fill

class Solution {
public:
    vector<int> p;
    string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {
        int n=s.size();
        p=vector<int>(n+1);
        for(int i=0;i<n;++i)p[i]=i;
        for(auto t:pairs){
            int px=find(t[0]);
            int py=find(t[1]);
            if(px!=py){
                p[px]=py;
            }
        }
        vector<vector<char>> f(n);
        for(int i=0;i<n;++i){
            f[find(i)].push_back(s[i]);
        }
        for(int i=0;i<n;++i){
            sort(f[i].begin(),f[i].end());
            reverse(f[i].begin(),f[i].end());
        }
        string t;
        for(int i=0;i<n;++i){
            t+=f[find(i)].back();
            f[find(i)].pop_back();
        }
        return t;
    }
    int find(int x)
    {
        if(p[x]!=x)p[x]=find(p[x]);
        return p[x];
    }
};

Guess you like

Origin www.cnblogs.com/clear-love/p/11568468.html