LeetCodeWeeklyContest-160

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/SinclairWang/article/details/102766016

Topic Portal

Find a positive integer given equation

Sign new packaging question, I have not played the new version of the ship.

class Solution {
public:
    vector<vector<int>> findSolution(CustomFunction& c, int z) {
        vector<vector<int>> res;
        for(int i=1;i<=1000;i++){
            for(int j=1;j<=1000;j++){
                if(c.f(i,j)<z) continue;
                else if(c.f(i,j)==z) {
                    vector<int> v;
                    v.push_back(i),v.push_back(j);
                    res.push_back(v);
                }
                else break;
                
            }
        }
        return res;
    }
};

Cyclic permutation code

Give you two integers n and start. Your task is to return any (0,1,2, ..., 2 ^ n-1) are arranged in p, and satisfies:

  • p[0] = start
  • p [i] and p [i + 1] binary representation of only a different
  • p [0] and p [2 ^ n -1] is only a binary representation of a different

for example:

  • Input: n = 2, start = 3
  • Output: [3,2,0,1]
  • Explanation: This is the binary representation of the arrangement (11,10,00,01)
  • All adjacent elements have a different, another arrangement is effective [3,1,0,2]

Thinking

reference:

In fact, Gray code.
Bit arithmetic operation using

achieve

vector<int> circularPermutation(int n, int start) {
        vector<int> res; 
        for(int i=0;i< 1<<n;i++){
            res.push_back(start^i^i>>1);
        }
        return res;
        
    }

The board found a Gray code of the game, then changed to change, they had lost ...

class Solution {
public:
     vector<int> grayCode(int n){
        vector<int> gray;
        if (n < 1) {
            gray.push_back(0);
            return gray;
        }
        int num = pow(2,n);
        int graycode[n];
        for (int i = 0; i < num; i++) {
            IntToBit(graycode, i, n);
            BitToGray(graycode,n);
            gray.push_back(GrayBitToInt(graycode, n));
        }
        return gray;
    }
    
    void IntToBit(int *code, int n, int bit){
        int i = bit-1;
        while (i >= 0) {
            code[i--] = n%2;
            n/=2;
        }
    }
    
    void BitToGray(int *code, int bit){
        int temp[bit];
        temp[0] = 0^code[0];
        for (int i = 0; i < bit-1; i++) {
            temp[i+1] = code[i]^code[i+1];
        }
        for (int i = 0; i < bit; i++) {
            code[i] = temp[i];
        }
    }
    
    int GrayBitToInt(int *code, int bit){
        int number = 0;
        for (int i = 0; i < bit; i++) {
            if (code[i] == 1) {
                number += pow(2, bit-i-1);
            }
        }
        return number;
    }
    vector<int> circularPermutation(int n, int start) {
        vector<int> res,tmp;
        tmp = grayCode(n);
        int index,msize = pow(2,n);
        for(int i=0;i<msize;i++){
            if(tmp[i]==start){
                index = i;
                break;
            }
        }
        for(int i=index;i<msize;i++){
            res.push_back(tmp[i]);
        }
        for(int i=0;i<index;i++){
            res.push_back(tmp[i]);
        }
        return res;
    }
};

The maximum length of concatenated string

Given a string array arr, arr string s is a sequence resulting string concatenation string, if each of s character appears only once, then it is a feasible solution.
Please return all feasible solutions longest length s.

Thinking

+ DFS function is determined
discriminant function: determines whether there is a repeated string of letters, to make a size of 26 to a table.

achieve

class Solution {
public:
    int maxL = 0;
    bool pend(string s1){
        int ph[26]={0};
        for(int i=0;i<s1.length();i++){
            if(ph[s1[i]-'a']==1) return false;
            ph[s1[i]-'a']=1;
        }
        return true;
    }
    void dfs(vector<string> arr, int index, string str){
        int len = str.length();
        if(pend(str)) maxL = max(maxL,len);
        if(index == arr.size()||!pend(str)) return ;
        for(int i=index;i<arr.size();i++){
            dfs(arr,i+1,str+arr[i]);
        }
    }
    int maxLength(vector<string>& arr) {
        dfs(arr,0,"");
        return maxL;
    }
};

Tiling

You are a construction team foreman, ready for interior decoration is unique design style house set in accordance with the requirements of designers.
Living room the size of a house is nxm, in order to maintain the minimalist style, we need to use as few square tiles to blanket the ground.
Any specifications assume a square tile edge length are integers.
Please help designers to calculate the minimum number of blocks of square tiles need to use?

Thinking

reference:

Basically DFS, in fact, much like the classic problem of greed this subject, but that looks like a square, and this title has a special point is that when n == 11 && m == 13 || n == 13 && m == 11 this when the results be 6. The rest is to cross, how many square tiles need to see, and then slitting, how many square tiles see the need, take Transverse and longitudinal minimum, is the result.

achieve

class Solution {
public:
    int dp[300][300]={0}; 
    int solve(int n,int m){
        if(n==m)
            return 1;
        if(dp[n][m])
            return dp[n][m];
        int res = 2e8;
        for(int i=1;i<=n/2;i++){
            res = min(solve(i,m)+solve(n-i,m),res);
        }
        for(int j=1;j<=m/2;j++){
            res = min(solve(n,j)+solve(n,m-j),res);
        }
        dp[n][m] = res;
        return res;
    }
    int tilingRectangle(int n, int m) {
        if(n==11&&m==13||n==13&&m==11) return 6;
        return solve(n,m);
   
    }
    
};

You are a person looking down the road ... and more and more long yearning

Guess you like

Origin blog.csdn.net/SinclairWang/article/details/102766016