Home

Holi And Colorful houses

This Holi, Monk wants to distribute sweets to the houses of his colony. The houses of the colony are present in a circular order (i.e. house 1 and house N are adjacent to each other).

Kids love to play with colors but currently, there is a supply of only 2 types of colors (Red represented by R and Green represented by G). Due to the festive season, each of the kids has colored their houses with either green or red color.

Monk is given the task of distributing sweets Q a number of times. Every time he is asked to travel from xth house to yth house to distribute the sweets.
The distribution strategy is that he starts from xth house and he has to give 1 sweet to a house only when he travels from a greenhouse to a red house or vice-versa. Monk can travel from xth house to yth house either in the clockwise direction or in the anti-clockwise direction.
Monk wants your help to find the minimum number of sweets he should carry to complete his journey.

Example:

Input:  n = 5, q = 2, s = "RRRGG", queries={{1,5},{3,2}}

Output:

1 0

Approach:

C++

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

void colourfulHouses(string sint nint q
vector<vector<int>> &queries)
{
    for (int i = 0i < qi++)
    {
        int l = queries[i][0]r = queries[i][1];

        if (l < r)
        {
            l = l - 1;
            r = r - 1;
            int cnt = 0ans = INT_MAX;
            for (int i = li < ri++)
                if (s[i] != s[i + 1])
                    cnt++;
            ans = min(anscnt);
            cnt = 0;
            for (int i = li > 0i--)
                if (s[i] != s[i - 1])
                    cnt++;
            if (s[0] != s[n - 1])
                cnt++;
            for (int i = n - 2i >= ri--)
                if (s[i] != s[i + 1])
                    cnt++;
            ans = min(anscnt);
            cout << ans << "\n";
        }
        else
        {
            l = l - 1;
            r = r - 1;
            int cnt = 0ans1 = INT_MAX;
            for (int i = li > ri--)
                if (s[i] != s[i - 1])
                    cnt++;
            ans1 = min(ans1cnt);
            cnt = 0;
            for (int i = li < n - 1i++)
                if (s[i] != s[i + 1])
                    cnt++;
            if (s[0] != s[n - 1])
                cnt++;
            for (int i = 0i < ri++)
                if (s[i] != s[i + 1])
                    cnt++;
            ans1 = min(ans1cnt);
            cout << ans1 << "\n";
        }
    }
}
int main()
{

    int n = 5q = 2;

    string s = "RRRGG";

    vector<vector<int>> queries = {{15}, {32}};

    colourfulHouses(snqqueries);

    return 0;
}


No comments:

Post a Comment