Cities on a map are connected by a number of roads. The number of roads between each city is in an array and city 0 is the starting location. The number of roads from city 0 to city 1 is the first value in the array, from city 1 to city 2 is the second, and so on.
How many paths are there from city 0 to the last city in the list, modulo ?
Example
There are 3 roads to city 1, roads to city 2 and 5 roads to city 3. The total number of roads is .
Example:
Input: n = 3, routes = {1, 3}
Output: 3
Approach
C++
#include <bits/stdc++.h>using namespace std;int connectingTowns(int n, vector<int> routes){int res = 1;for (int i = 0; i < n - 1; i++){res = res * routes[i];res = res % 1234567;}return res;}int main(){int n = 3;vector<int> routes = {1, 3};cout << connectingTowns(n, routes) << "\n";return 0;}
No comments:
Post a Comment