Skip to content

Commit f45be5e

Browse files
Merge pull request #3 from NicolaBernini/hackerrank_cpp_20190330_1
Hackerrank cpp 20190330 1
2 parents 53831fc + 1250106 commit f45be5e

File tree

2 files changed

+152
-0
lines changed

2 files changed

+152
-0
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
2+
// Original Link
3+
// https://www.hackerrank.com/challenges/new-year-chaos/problem
4+
5+
#include <bits/stdc++.h>
6+
#include <set>
7+
8+
using namespace std;
9+
10+
vector<string> split_string(string);
11+
12+
13+
14+
15+
///////////////////// SOLUTION START /////////////////////
16+
/**
17+
* @brief Computing Min Bribes
18+
* @note The real challenge is to avoid undercounting by looking only at the delta=(val - pos )
19+
* @note In all the cases where delta<2 it is possible that the actor received n bribes but also bribed zero or one and this is the cause for undercounting
20+
* @note In order to identify this specific case, I check with respect to the elements on the left in the sequence
21+
* @note More specifically, any hidden bribe is identified by the fact the number of elements on the left of the pivot and bigger than the pivot does not match with its delta
22+
* @note However looking back at all the elements on the left of the pivot makes the complexity quadratic so even if it is a correct solution, it won't be able to scale well enough and fit into the limitations imposed by Hackerrank System
23+
* @note That's why Dynamic Programming has been used: as we linearly observe the vector, we essentially perform and insert sort into an Ordered Set
24+
* @note When we have a pivot (delta<2) instead of iterating over the input vector on [0..i] we iterate over the ordered set counting the Number bigger than the pivot condition and we break out of the loop as soon as we get an elemen that's smaller than the pivot (it's ordered)
25+
* @note This provides the right count for the hidden bribes which alongside the visible bribes provides the right total amount
26+
*/
27+
void minimumBribes(vector<int> q)
28+
{
29+
unsigned int res=0;
30+
31+
// Used for Dynamic Programming Solution
32+
// Remember the ordered list of numbers already observed
33+
// The goal is to reduce the comlexity
34+
std::set<unsigned int> d;
35+
for(unsigned int i=0; i<q.size(); ++i)
36+
{
37+
d.insert(q[i]);
38+
const int delta = q[i]-(i+1);
39+
40+
41+
// With delta=2 no hidden bribes possible so only visible bribes so just count them
42+
if(delta==2)
43+
{
44+
res += delta;
45+
}
46+
// Hidden bribes possible
47+
else if(delta<2)
48+
{
49+
// Count the visible bribes
50+
if(delta>0) res += delta;
51+
52+
unsigned int v=0;
53+
54+
// Ordered count, we get out as soon as smaller element is found
55+
for(auto e = d.rbegin(); e != d.rend(); ++e)
56+
{
57+
if(*e > q[i]) v++;
58+
if(v > abs(delta)) {res++; break;}
59+
if(*e < q[i]) break;
60+
}
61+
}
62+
else if(delta>2)
63+
{
64+
cout << "Too chaotic" << endl;
65+
return;
66+
}
67+
}
68+
69+
cout << res << endl;
70+
}
71+
72+
///////////////////// SOLUTION END //////////////////////////
73+
74+
75+
int main()
76+
{
77+
int t;
78+
cin >> t;
79+
cin.ignore(numeric_limits<streamsize>::max(), '\n');
80+
81+
for (int t_itr = 0; t_itr < t; t_itr++) {
82+
int n;
83+
cin >> n;
84+
cin.ignore(numeric_limits<streamsize>::max(), '\n');
85+
86+
string q_temp_temp;
87+
getline(cin, q_temp_temp);
88+
89+
vector<string> q_temp = split_string(q_temp_temp);
90+
91+
vector<int> q(n);
92+
93+
for (int i = 0; i < n; i++) {
94+
int q_item = stoi(q_temp[i]);
95+
96+
q[i] = q_item;
97+
}
98+
99+
minimumBribes(q);
100+
}
101+
102+
return 0;
103+
}
104+
105+
vector<string> split_string(string input_string) {
106+
string::iterator new_end = unique(input_string.begin(), input_string.end(), [] (const char &x, const char &y) {
107+
return x == y and x == ' ';
108+
});
109+
110+
input_string.erase(new_end, input_string.end());
111+
112+
while (input_string[input_string.length() - 1] == ' ') {
113+
input_string.pop_back();
114+
}
115+
116+
vector<string> splits;
117+
char delimiter = ' ';
118+
119+
size_t i = 0;
120+
size_t pos = input_string.find(delimiter);
121+
122+
while (pos != string::npos) {
123+
splits.push_back(input_string.substr(i, pos - i));
124+
125+
i = pos + 1;
126+
pos = input_string.find(delimiter, i);
127+
}
128+
129+
splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1));
130+
131+
return splits;
132+
}
133+
134+
135+

hackerrank/counting/readme.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,20 @@
22
# Overview
33

44
Some Counting Related Challenges
5+
6+
Birthday Cake Candles
7+
- [Def](https://www.hackerrank.com/challenges/birthday-cake-candles/problem)
8+
- [Sol](birthday_candles1.cpp)
9+
10+
New Year Chaos
11+
- [Def](https://www.hackerrank.com/challenges/new-year-chaos/problem)
12+
- [Sol](new_year_chaos1.cpp)
13+
14+
15+
16+
17+
18+
19+
20+
21+

0 commit comments

Comments
 (0)