loading...

Daily Coding Challenge #36

wingkwong profile image Wing-Kam ・4 min read

Daily Coding Challenge (87 Part Series)

1) Daily Coding Challenge #1 2) Daily Coding Challenge #2 3 ... 85 3) Daily Coding Challenge #3 4) Daily Coding Challenge #4 5) Daily Coding Challenge #5 6) Daily Coding Challenge #6 7) Daily Coding Challenge #7 8) Daily Coding Challenge #8 9) Daily Coding Challenge #9 10) Daily Coding Challenge #10 11) Daily Coding Challenge #11 12) Daily Coding Challenge #12 13) Daily Coding Challenge #13 14) Daily Coding Challenge #14 15) Daily Coding Challenge #15 16) Daily Coding Challenge #16 17) Daily Coding Challenge #17 18) Daily Coding Challenge #18 19) Daily Coding Challenge #19 20) Daily Coding Challenge #20 21) Daily Coding Challenge #21 22) Daily Coding Challenge #22 23) Daily Coding Challenge #23 24) Daily Coding Challenge #24 25) Daily Coding Challenge #25 26) Daily Coding Challenge #26 27) Daily Coding Challenge #27 28) Daily Coding Challenge #28 29) Daily Coding Challenge #29 30) Daily Coding Challenge #30 31) Daily Coding Challenge #31 32) Daily Coding Challenge #32 33) Daily Coding Challenge #33 34) Daily Coding Challenge #34 35) Daily Coding Challenge #35 36) Daily Coding Challenge #36 37) Daily Coding Challenge #37 38) Daily Coding Challenge #38 39) Daily Coding Challenge #39 40) Daily Coding Challenge #40 41) Daily Coding Challenge #41 42) Daily Coding Challenge #42 43) Daily Coding Challenge #43 44) Daily Coding Challenge #44 45) Daily Coding Challenge #45 46) Daily Coding Challenge #46 47) Daily Coding Challenge #47 48) Daily Coding Challenge #48 49) Daily Coding Challenge #49 50) Daily Coding Challenge #50 51) Daily Coding Challenge #51 52) Daily Coding Challenge #52 53) Daily Coding Challenge #53 54) Daily Coding Challenge #54 55) Daily Coding Challenge #55 56) Daily Coding Challenge #56 57) Daily Coding Challenge #57 58) Daily Coding Challenge #58 59) Daily Coding Challenge #59 60) Daily Coding Challenge #60 61) Daily Coding Challenge #61 62) Daily Coding Challenge #62 63) Daily Coding Challenge #63 64) Daily Coding Challenge #64 65) Daily Coding Challenge #65 66) Daily Coding Challenge #66 67) Daily Coding Challenge #67 68) Daily Coding Challenge #68 69) Daily Coding Challenge #69 70) Daily Coding Challenge #70 71) Daily Coding Challenge #71 72) Daily Coding Challenge #72 73) Daily Coding Challenge #73 74) Daily Coding Challenge #74 75) Daily Coding Challenge #75 76) Daily Coding Challenge #76 77) Daily Coding Challenge #77 78) Daily Coding Challenge #78 79) Daily Coding Challenge #79 80) Daily Coding Challenge #80 81) Daily Coding Challenge #81 82) Daily Coding Challenge #82 83) Daily Coding Challenge #83 84) Daily Coding Challenge #84 85) Daily Coding Challenge #85 86) Daily Coding Challenge #86 87) Daily Coding Challenge #87

About

This is a series of Daily Coding Challenge. Each day I show a few solutions written in C++. The questions are from coding practice/contest sites such as HackerRank, LeetCode, Codeforces, Atcoder and etc.


/*
CodeChef June Challenge 2020 Division 2 - Chef and Price Control
https://www.codechef.com/JUNE20B/problems/PRICECON
*/

#include <bits/stdc++.h>
using namespace std; 

#define FAST_INP  ios_base::sync_with_stdio(false);cin.tie(NULL)

int main()  
{ 
    FAST_INP;
    int t,n,p,k;
    cin >> t;
    while(t--){
        cin >> n >> k;
        int ans1=0,ans2=0;
        for(int i=0;i<n;i++){
            cin >> p;
            // for each item i such that Pi>K, 
            // its price should be reduced from Pi to K.
            if(p>k) ans2+=k;
            else ans2+=p;
            // initial revenue
            ans1+=p;
        }
        // lost revenue
        cout << ans1-ans2 << "\n";
    }
    return 0;
} 


/*
CodeChef June Challenge 2020 Division 2 - Chef and String
https://www.codechef.com/JUNE20B/problems/XYSTR
*/

#include <bits/stdc++.h>
using namespace std; 

#define FAST_INP  ios_base::sync_with_stdio(false);cin.tie(NULL)

int main()  
{ 
    FAST_INP;
    int t;
    string s;
    cin >> t;
    while(t--){
        cin >> s;
        int ans=0;
        // traverse each person starting from the second person
        for(int i=1;i<s.size();i++){
            if(
                (s[i]=='x'&&s[i-1]=='y') || // yx can be a pair
                (s[i]=='y'&&s[i-1]=='x')    // xy can be a pair
            ) {
                ans++;
                // since they are paired, increase 1 to avoid checking again 
                i++; 
            }
        }
        cout << ans << "\n";
    }
    return 0;
} 


/*
CodeChef June Challenge 2020 Division 2 - Chef and Icecream
https://www.codechef.com/JUNE20B/problems/CHFICRM
*/

#include <bits/stdc++.h>
using namespace std; 

#define FAST_INP  ios_base::sync_with_stdio(false);cin.tie(NULL)

bool solve(int n){
    vector<int> a(n);
    // store how many coins the Chef has
    int c5=0,c10=0,c15=0;
    // read input
    for(int i=0;i<n;i++) cin >> a[i];
    for(int i=0;i<n;i++){
        if(a[i]==5) {
            // no change is required
            c5++;
        } else if(a[i]==10) {
            // need to give back Rs. 5 coin
            // if no Rs .5, we can end it and the ans is NO 
            if(c5==0) {
                return false;
            }
            // update the coins
            c5--;
            c10++;
        }
        else if(a[i]==15) {
            // I got WA on my first try as I thought "exact change" means giving back Rs. 10 only in this case...
            // --------------------------------------------------------------------------------------------------------
            // The Chef needs to give back Rs. 10 coin which can further break into 2 cases
            // he either gives back 2 x Rs .5 or 1 x Rs .10
            if(c10==0&&c5<2){
                return false;
            }
            // if Chef doesn't have Rs .10 but 2 x Rs .5, deduct 2 x Rs .5
            if(c10==0) c5-=2;
            // else deduct 1 x Rs .10
            else c10--;
            // receive Rs .15
            c15++; 
        }
    }
    return true;
}

int main()  
{ 
    FAST_INP;
    int t,n;
    cin >> t;
    while(t--){
        cin >> n;
        if(solve(n)) cout << "YES\n";
        else cout << "NO\n";
    }
    return 0;
} 


/*
CodeChef June Challenge 2020 Division 2 - Even Matrix
https://www.codechef.com/JUNE20B/problems/EVENM
*/

#include <bits/stdc++.h>
using namespace std; 

#define FAST_INP  ios_base::sync_with_stdio(false);cin.tie(NULL)

int main()  
{ 
    FAST_INP;
    int t,n;
    cin >> t;

    while(t--){
        cin >> n;
        // the pattern should be easy to observe 
        if(n%2==0){
            // if it is even,
            // print in ascending order for every odd row
            // print in descending order for every even row
            // ---------------------------------------------
            // 1 2 3 4 5 6 7 8 9 10
            // 20 19 18 17 16 15 14 13 12 11
            // 21 22 23 24 25 26 27 28 29 30
            // 40 39 38 37 36 35 34 33 32 31
            // 41 42 43 44 45 46 47 48 49 50
            // 60 59 58 57 56 55 54 53 52 51
            // 61 62 63 64 65 66 67 68 69 70
            // 80 79 78 77 76 75 74 73 72 71
            // 81 82 83 84 85 86 87 88 89 90
            // 100 99 98 97 96 95 94 93 92 91
            for(int i=1;i<=n;i++){
                if(i%2==0){
                    // even row
                    for(int j=0;j<n;j++) {
                        cout << i*n-j << " ";
                    }

                } else {
                    // odd row
                    for(int j=1;j<=n;j++) {
                        cout << (i-1)*n+j  << " ";
                    }
                }
                cout << "\n";
            }
        } else {
            // if it is odd, just print all numbers in ascending order
            // ---------------------------------------------
            // 1 2 3 4 5 6 7 8 9 10 11
            // 12 13 14 15 16 17 18 19 20 21 22
            // 23 24 25 26 27 28 29 30 31 32 33
            // 34 35 36 37 38 39 40 41 42 43 44
            // 45 46 47 48 49 50 51 52 53 54 55
            // 56 57 58 59 60 61 62 63 64 65 66
            // 67 68 69 70 71 72 73 74 75 76 77
            // 78 79 80 81 82 83 84 85 86 87 88
            // 89 90 91 92 93 94 95 96 97 98 99
            // 100 101 102 103 104 105 106 107 108 109 110
            // 111 112 113 114 115 116 117 118 119 120 121
            for(int i=1;i<=n*n;i++){
                cout << i << " ";
                if(i%n==0) cout << "\n";
            }
        }
    }
    return 0;
} 


The source code is available in corresponding repo below. Star and watch for timely updates!

GitHub logo wingkwong / leetcode

🏆 A Collection of my LeetCode Solutions with Explanations 🏆

GitHub logo wingkwong / hackerrank

🏆 A Collection of my HackerRank Solutions with Explanations 🏆

GitHub logo wingkwong / codeforces

🏆 A Collection of my Codeforces Solutions with Explanations 🏆

GitHub logo wingkwong / atcoder

🏆 A Collection of my AtCoder Solutions with Explanations 🏆

Daily Coding Challenge (87 Part Series)

1) Daily Coding Challenge #1 2) Daily Coding Challenge #2 3 ... 85 3) Daily Coding Challenge #3 4) Daily Coding Challenge #4 5) Daily Coding Challenge #5 6) Daily Coding Challenge #6 7) Daily Coding Challenge #7 8) Daily Coding Challenge #8 9) Daily Coding Challenge #9 10) Daily Coding Challenge #10 11) Daily Coding Challenge #11 12) Daily Coding Challenge #12 13) Daily Coding Challenge #13 14) Daily Coding Challenge #14 15) Daily Coding Challenge #15 16) Daily Coding Challenge #16 17) Daily Coding Challenge #17 18) Daily Coding Challenge #18 19) Daily Coding Challenge #19 20) Daily Coding Challenge #20 21) Daily Coding Challenge #21 22) Daily Coding Challenge #22 23) Daily Coding Challenge #23 24) Daily Coding Challenge #24 25) Daily Coding Challenge #25 26) Daily Coding Challenge #26 27) Daily Coding Challenge #27 28) Daily Coding Challenge #28 29) Daily Coding Challenge #29 30) Daily Coding Challenge #30 31) Daily Coding Challenge #31 32) Daily Coding Challenge #32 33) Daily Coding Challenge #33 34) Daily Coding Challenge #34 35) Daily Coding Challenge #35 36) Daily Coding Challenge #36 37) Daily Coding Challenge #37 38) Daily Coding Challenge #38 39) Daily Coding Challenge #39 40) Daily Coding Challenge #40 41) Daily Coding Challenge #41 42) Daily Coding Challenge #42 43) Daily Coding Challenge #43 44) Daily Coding Challenge #44 45) Daily Coding Challenge #45 46) Daily Coding Challenge #46 47) Daily Coding Challenge #47 48) Daily Coding Challenge #48 49) Daily Coding Challenge #49 50) Daily Coding Challenge #50 51) Daily Coding Challenge #51 52) Daily Coding Challenge #52 53) Daily Coding Challenge #53 54) Daily Coding Challenge #54 55) Daily Coding Challenge #55 56) Daily Coding Challenge #56 57) Daily Coding Challenge #57 58) Daily Coding Challenge #58 59) Daily Coding Challenge #59 60) Daily Coding Challenge #60 61) Daily Coding Challenge #61 62) Daily Coding Challenge #62 63) Daily Coding Challenge #63 64) Daily Coding Challenge #64 65) Daily Coding Challenge #65 66) Daily Coding Challenge #66 67) Daily Coding Challenge #67 68) Daily Coding Challenge #68 69) Daily Coding Challenge #69 70) Daily Coding Challenge #70 71) Daily Coding Challenge #71 72) Daily Coding Challenge #72 73) Daily Coding Challenge #73 74) Daily Coding Challenge #74 75) Daily Coding Challenge #75 76) Daily Coding Challenge #76 77) Daily Coding Challenge #77 78) Daily Coding Challenge #78 79) Daily Coding Challenge #79 80) Daily Coding Challenge #80 81) Daily Coding Challenge #81 82) Daily Coding Challenge #82 83) Daily Coding Challenge #83 84) Daily Coding Challenge #84 85) Daily Coding Challenge #85 86) Daily Coding Challenge #86 87) Daily Coding Challenge #87

Posted on by:

wingkwong profile

Wing-Kam

@wingkwong

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

Discussion

markdown guide
 

Nice, learning C++ seriously, this gave me a few new ways to tackle writing in it.