City Travel

You are going from City A to City B. The distance between A and B is S km. On most days, you can go at most X km one day. But there are N exceptions, in the T_i the day, you can go at most Y_i km. You need to find out the minimum number of days required to reach city B from city A. 

Example:

Input:  s = 21, x = 5, n = 2, v = {{2,4},{4,8}}
Output: 4

Approach

C++

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

int cityTravel(int sint xint nvector<pair<intint>> &v)
{
    sort(v.begin(), v.end());
    int cnt = 0;
    int i = 1;
    int j = 0;
    while (s > 0)
    {
        if (v[j].first == i)
        {
            s = s - v[j].second;
            i++;
            j++;
            cnt++;
        }
        else
        {
            s = s - x;
            cnt++;
            i++;
        }
    }
    return cnt;
}
int main()
{
    int s = 21x = 5n = 2;

    vector<pair<intint>> v = {{24}, {48}};

    cout << cityTravel(sxnv<< "\n";

    return 0;
}


No comments:

Post a Comment