loading...

Daily HackerRank Challenge - Day 25

wingkwong profile image Wing-Kam ・2 min read

Daily HackerRank Challenge (30 Part Series)

1) Daily HackerRank Challenge - Day 0 2) Daily HackerRank Challenge - Day 1 3 ... 28 3) Daily HackerRank Challenge - Day 2 4) Daily HackerRank Challenge - Day 3 5) Daily HackerRank Challenge - Day 4 6) Daily HackerRank Challenge - Day 5 7) Daily HackerRank Challenge - Day 6 8) Daily HackerRank Challenge - Day 7 9) Daily HackerRank Challenge - Day 8 10) Daily HackerRank Challenge - Day 9 11) Daily HackerRank Challenge - Day 10 12) Daily HackerRank Challenge - Day 11 13) Daily HackerRank Challenge - Day 12 14) Daily HackerRank Challenge - Day 13 15) Daily HackerRank Challenge - Day 14 16) Daily HackerRank Challenge - Day 15 17) Daily HackerRank Challenge - Day 16 18) Daily HackerRank Challenge - Day 17 19) Daily HackerRank Challenge - Day 19 20) Daily HackerRank Challenge - Day 20 21) Daily HackerRank Challenge - Day 21 22) Daily HackerRank Challenge - Day 22 23) Daily HackerRank Challenge - Day 23 24) Daily HackerRank Challenge - Day 24 25) Daily HackerRank Challenge - Day 25 26) Daily HackerRank Challenge - Day 26 27) Daily HackerRank Challenge - Day 27 28) Daily HackerRank Challenge - Day 28 29) Daily HackerRank Challenge - Day 29 30) Daily HackerRank Challenge - Day 30

About

This is a series of Daily HackerRank Challenge. Each day I show the some solutions written in C++.


Hash Tables: Ransom Note

image

Sample Input 0

6 4
give me one grand today night
give one grand today

Sample Output 0

Yes

Sample Input 1

6 5
two times three is not four
two times two is four

Sample Output 1

No

Put each word in magazine into a hash map and traverse note to see if the word occurred in the map or not. If so, deduct the value m[s] by 1 and continue. If m[s] reaches 0, it means there is no enough words to build the random note.

Final Solution

void checkMagazine(vector<string> magazine, vector<string> note) {
    unordered_map<string,int> m;
    bool f = true;
    for(string s:magazine) m[s]++;
    for(string s: note) {
        if(--m[s]<0) {
            f=false;
            break;
        }
    }
    cout << (f?"Yes":"No") << "\n"; 
}

Count Triplets

image

Sample Input 0

4 2
1 2 2 4

Sample Output 0

2

Create two maps to hold the values for building 2/3 triplets and 3/3 triplets. No division is necessary in this case. Take the sample input 0 as an exampe, for the first value, ans and m3[v*r] is 0 as m3 and m2 are still empty. For the first iternation, it only set m2[1*2] to 1.

For the second iteration, the value is 2 and m3[2] is still empty. However m2[2] is 1 as it is updated in previous iteration so that m3[4] is now 1.

For the third iteration, the value is 2 again and m3[2] is still empty. m3[4] is updated again, which is 2.

For the last iternation, m3[4] is 2 and ans becomes 2. At the end, it returns 2.

i=0 - value=1
m2[2]=1

i=1 - value=2
m3[4]=1
m2[4]=1

i=2 - value=2
m3[4]=2
m2[4]=2

i=3 - value=4
ans=m3[4]
ans=2

Final Solution

long countTriplets(vector<long> arr, long r) {
    unordered_map<long,long> m2,m3;
    int ans=0;
    for(long v : arr){
        ans += m3[v];
        m3[v*r] += m2[v];
        ++m2[v*r];
    }
    return ans;
}

Complete Code

Check out the complete code via below links

Daily HackerRank Challenge (30 Part Series)

1) Daily HackerRank Challenge - Day 0 2) Daily HackerRank Challenge - Day 1 3 ... 28 3) Daily HackerRank Challenge - Day 2 4) Daily HackerRank Challenge - Day 3 5) Daily HackerRank Challenge - Day 4 6) Daily HackerRank Challenge - Day 5 7) Daily HackerRank Challenge - Day 6 8) Daily HackerRank Challenge - Day 7 9) Daily HackerRank Challenge - Day 8 10) Daily HackerRank Challenge - Day 9 11) Daily HackerRank Challenge - Day 10 12) Daily HackerRank Challenge - Day 11 13) Daily HackerRank Challenge - Day 12 14) Daily HackerRank Challenge - Day 13 15) Daily HackerRank Challenge - Day 14 16) Daily HackerRank Challenge - Day 15 17) Daily HackerRank Challenge - Day 16 18) Daily HackerRank Challenge - Day 17 19) Daily HackerRank Challenge - Day 19 20) Daily HackerRank Challenge - Day 20 21) Daily HackerRank Challenge - Day 21 22) Daily HackerRank Challenge - Day 22 23) Daily HackerRank Challenge - Day 23 24) Daily HackerRank Challenge - Day 24 25) Daily HackerRank Challenge - Day 25 26) Daily HackerRank Challenge - Day 26 27) Daily HackerRank Challenge - Day 27 28) Daily HackerRank Challenge - Day 28 29) Daily HackerRank Challenge - Day 29 30) Daily HackerRank Challenge - Day 30

Posted on May 8 by:

wingkwong profile

Wing-Kam

@wingkwong

Consultant by day. Developer by night. AWS certified. Exploring #CloudNative currently.

Discussion

markdown guide