Professional Documents
Culture Documents
01h : 19m
Codenation - Campus Pool -15th October
to test end
N
Pizza store
Kevin has just opened a new pizza store in a city.
1
Fortunately, right after the launch, he got enough pizza orders from various parts of the city.
So he hired pizza delivery guys and computed the paths from the pizza store to each
2
delivery point. Now, Kevin wants to give the best customer satisfaction by getting the
pizzas delivered in lesser time.
3
You are given a list of n paths for delivery starting from the current pizza store and each
road in the path is a ONE-WAY road and traveling each road takes 1 unit time.
From the end of each road, there are two roads one to the left and the other to the right.
The path is a string composed of 'L' and 'R' where 'L' denotes taking the left road and 'R'
denotes taking the right road. You need to suggest Kevin a new pizza store location such
that the maximum waiting time for any customer is minimized and all the delivery points
are reachable from that location. You also need to tell the path of the new location from the
old location of the pizza store.
If there is no way to shift the pizza store, output a string "-1" ( excluding double quotes ).
Function Description
Complete the function newLocationPath in the editor below. The function must return a
string denoting the path of the new location from the current location of the pizza store.
newLocationPath has the following parameter(s):
paths[path[0],...path[n-1]]: an array of strings representing the path of the ith destination.
Constraints
1 ≤ n ≤ 105
1 ≤ length of a path ≤ 60
https://www.hackerrank.com/tests/b2aa5a9llnh/questions/fmgttobf87j 1/5
15/10/2018 Codenation - Campus Pool -15th October :: powered by HackerRank
Input from stdin will be processed as follows and passed to the function:
The first line contains an integer n, the number of path in paths.
Each of the n subsequent lines contains a string describing path[i] where 0 ≤ i < n.
Sample Case 0
Sample Input 0
3
LLLRLL
LLLRLRR
LLLRRRL
Sample Output 0
LLLR
Explanation 0
Every destination is reachable from the new location using the following paths:
LL
LRR
RRL
Maximum waiting time after shifting to the new location is 3 i.e LRR or RRL
YOUR ANSWER
We recommend you take a quick tour of our editor before you proceed. The timer ✖
will pause up to 90 seconds for the tour. Start tour
1 ▸ #include ↔
2
3 using namespace std;
4
5 string ltrim(const string &);
6 string rtrim(const string &);
7
8
9 ▾ /*
https://www.hackerrank.com/tests/b2aa5a9llnh/questions/fmgttobf87j 2/5
15/10/2018 Codenation - Campus Pool -15th October :: powered by HackerRank
21 int main()
22 ▾ {
23 ofstream fout(getenv("OUTPUT_PATH"));
24
25 string paths_count_temp;
26 getline(cin, paths_count_temp);
27
28 int paths_count = stoi(ltrim(rtrim(paths_count_temp)));
29
30 vector<string> paths(paths_count);
31
32 ▾ for (int i = 0; i < paths_count; i++) {
33 string paths_item;
34 getline(cin, paths_item);
35
36 ▾ paths[i] = paths_item;
37 }
38
39 string result = newLocationPath(paths);
40
41 fout << result << "\n";
42
43 fout.close();
44
45 return 0;
46 }
47
48 ▾ string ltrim(const string &str) {
49 string s(str);
50
51 s.erase(
52 s.begin(),
53 find_if(s.begin(), s.end(), not1(ptr_fun<int, int>
(isspace)))
54 );
https://www.hackerrank.com/tests/b2aa5a9llnh/questions/fmgttobf87j 3/5
15/10/2018 Codenation - Campus Pool -15th October :: powered by HackerRank
55
56 return s;
57 }
58
59 ▾ string rtrim(const string &str) {
60 string s(str);
61
62 s.erase(
63 find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>
(isspace))).base(),
64 s.end()
65 );
66
67 return s;
68 }
69
Line: 16 Col: 58
Test against custom input Run Code Submit code & Continue
Download sample test cases The input/output files have Unix line endings. Do not use
Notepad to edit them on windows.
3
LLLRLL
LLLRLRR
LLLRRRL
Your Output
LLLR
https://www.hackerrank.com/tests/b2aa5a9llnh/questions/fmgttobf87j 4/5
15/10/2018 Codenation - Campus Pool -15th October :: powered by HackerRank
https://www.hackerrank.com/tests/b2aa5a9llnh/questions/fmgttobf87j 5/5