HackerRank‐Warm up Challenges - a920604a/leetcode GitHub Wiki


title: Warm up Challenges categories: - interview-preparation-kit - HackerRank comments: false

Sales by Match

int sockMerchant(int n, vector<int> ar) {
    vector<int> pairs(101, 0);
    for(int a:ar) pairs[a]++;
    int ret= 0;
    for(int i= 1;i<=100 ; ++i){
        ret += pairs[i]/2;
    }
    return ret;
}

Counting Valleys

int countingValleys(int steps, string path) {
    int count =0;
    int cur=0;
    for(int i=0;i<steps ; ++i){
        cur+=(path[i]=='U')?1:-1;
        if(cur==0){
            if(path[i]=='U') count++;
        }
    }
    return count;
}

Jumping on the Clouds

int jumpingOnClouds(vector<int> c) {
    //  0   1   0   0   0   1   0
    //          1   +   +   +   +
    //          1   2   2   +   3 
    int n = c.size();
    vector<int> dp(n,n);
    dp[0] = 0;
    for(int i= 1 ;i<n;++i){
        if(c[i] ==1) continue;
        dp[i] = dp[i-1]+1;
        if(i>=2 ) dp[i] = min(dp[i-1]+1, dp[i-2]+1);
    }
    return dp.back();
}

Repeated String

long repeatedString(string s, long n) {
    long count =0, f=0;
    for(char c:s) f+=(c=='a');
    count+=n/s.size()*f;
    for(int i=0;i<n%s.size() ; ++i) count+=(s[i]=='a');
    return count;
}
⚠️ **GitHub.com Fallback** ⚠️