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 s, int x, int n, vector<pair<int, int>> &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 = 21, x = 5, n = 2;vector<pair<int, int>> v = {{2, 4}, {4, 8}};cout << cityTravel(s, x, n, v) << "\n";return 0;}
No comments:
Post a Comment