LeetCode's 20th fortnight

Posted by rcity on Wed, 26 Feb 2020 11:31:04 +0100

1.Sort according to the number of 1 under digital binary

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
 int cal(int num){
     int sum=0;
     while(num){
         sum+=num%2;
         num/=2;
     }
     return sum;
 }
int* sortByBits(int* arr, int arrSize, int* returnSize){
    int  i,tmp,j;
    int *onenum=(int*)malloc(sizeof(int)*(arrSize+1));
    for(i=0;i<arrSize;i++){
        onenum[i]=cal(arr[i]);
    }
   for(i=0;i<arrSize-1;i++){
       for(j=i+1;j<arrSize;j++){
           if(onenum[i]>onenum[j]||(onenum[i]==onenum[j]&&arr[i]>arr[j])){
               tmp=onenum[i];
               onenum[i]=onenum[j];
               onenum[j]=tmp;
               tmp=arr[i];
               arr[i]=arr[j];
               arr[j]=tmp;
           }
       }
   }
   *returnSize=arrSize;
    return arr;
}

2.Discount every n customers

typedef struct {
    int n;
    int now;
    int discount;
    int* products;
    int productsSize;
    int* prices;
    int pricesSize;
} Cashier;

Cashier* cashierCreate(int n, int discount, int* products, int productsSize, int* prices, int pricesSize) {
    Cashier* t=(Cashier*)malloc(sizeof(Cashier));
    t->n=n;t->now=0;
    t->discount=discount;
    t->products=products;
    t->productsSize=productsSize;
    t->prices=prices;
    t->pricesSize=pricesSize;
    return t;
}

double cashierGetBill(Cashier* obj, int* product, int productSize, int* amount, int amountSize) {
    obj->now++;
    double sum=0;
    for(int i=0;i<productSize;i++){
        for(int j=0;j<obj->productsSize;j++){
            if(obj->products[j]==product[i]){
                sum=sum+obj->prices[j]*amount[i];break;
            }
        }
    }
    if(obj->now==obj->n){
        sum=sum-(obj->discount*sum)/100;
        obj->now=0;
    }
    return sum;
}

void cashierFree(Cashier* obj) {
free(obj);
}

/**
 * Your Cashier struct will be instantiated and called as such:
 * Cashier* obj = cashierCreate(n, discount, products, productsSize, prices, pricesSize);
 * double param_1 = cashierGetBill(obj, product, productSize, amount, amountSize);
 
 * cashierFree(obj);
*/

3.Number of substrings containing all three characters

[idea] consider whether the substring contains abc, and the repeated substring is also calculated. Each time we start to traverse from i, we calculate how many substrings meet the requirements in the string starting from i. If a double cycle is used, it will time out. We need to optimize here. In fact, we only need to save the end j of the last traversal when traversing the string i, and the result of the previous traversal of j already exists in array d.

We use an array d[3] to store the number of occurrences of abc. If d[0],d[1],d[2] are not 0, it means that abc letters have appeared in the current substring, then the characters after j do not need to be traversed, because any additional character can form a string that meets the requirements.

int numberOfSubstrings(char * s){
    int i,j=0,len,sum=0,tmp;
    int d[3]={0};
    len=strlen(s);
    if(len<3)return 0;
    for(i=0;i<len;i++){
        while(j<len&&(d[0]==0||d[1]==0||d[2]==0)){
            d[s[j]-'a']++;
            j++;
        }
        if(!(d[0]==0||d[1]==0||d[2]==0)){
            sum+=len-j+1;
        }
        d[s[i]-'a']--;
    }
    return sum;
}

4.Number of valid Express Series

[idea] dp[i][j] is used to indicate that the current received piece is I piece and the sent piece is j piece. The current operations are divided into receiving and sending.

For receipt: dp[i][j]+=dp[i-1][j]*(n-(i-1)); where the receipt can be any one of the remaining packages n-(i-1)

For the sending piece: dp[i][j]+=dp[i][j-1]*(i-(j-1)); the sending piece can be any one of the remaining i-(j-1) pieces after the current receiving piece I removes the sent j-1

int countOrders(int n){
   long long i,j,dp[n+1][n+1];
    memset(dp,0,sizeof(dp));
   long long mod=1000000007;
    dp[0][0]=1;//0 sent, 0 received, scheme 1
    for(i=1;i<=n;i++){
        for(j=0;j<=i;j++){
//If it is currently receiving, the receiving can be any of the remaining packages n-(i-1)
            dp[i][j]+=dp[i-1][j]*(n-(i-1));
            dp[i][j]%=mod;
//If it is the current sending, the sending can be the current receiving i. remove the j-1 that has been sent, and any of the remaining i-(j-1)
            if(j>0)
            dp[i][j]+=dp[i][j-1]*(i-(j-1));
            dp[i][j]%=mod;
        }
    }
    return (int)dp[n][n];
}

 

140 original articles published, 110 praised, 240000 visitors+
Private letter follow