Skip to content

Commit e4cb2de

Browse files
committed
Day - 75 work
1 parent 068b9fb commit e4cb2de

File tree

2 files changed

+80
-3
lines changed

2 files changed

+80
-3
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
| Current Status| Stats |
88
| :------------: | :----------: |
9-
| Total Problems | 109 |
10-
| Current Streak | 74 days |
11-
| Longest Streak | 74 ( August 17, 2015 - October 29, 2015 ) |
9+
| Total Problems | 110 |
10+
| Current Streak | 75 days |
11+
| Longest Streak | 75 ( August 17, 2015 - October 30, 2015 ) |
1212

1313
</center>
1414

@@ -133,6 +133,7 @@ Include contains single header implementation of data structures and some algori
133133
| Given a M x N matrix, rotate it by R rotations anticlockwise, and show the resulting matrix. | [rotate_matrix.cpp](common_ds_problems/rotate_matrix.cpp)|
134134
| Rotate an array by r elements ( left or right ) | [array_rotation.cpp](common_ds_problems/array_rotation.cpp)
135135
| Given an array of repeating/non-repeating intergeres, determine the first non-repeating int in this array | [first_non_repeating_int.cpp](common_ds_problems/first_non_repeating_int.cpp)|
136+
| In Quantumland, there are n cities numbered from 1 to n. Here, c<sub>i</sub> denotes the i<sup>th</sup> city. There are n−1 roads in Quantumland. Here, c<sub>i</sub> and c<sub>i+1</sub> have a bidirectional road between them for each i < n.There is a rumor that Flatland is going to attack Quantumland, and the queen wants to keep her land safe. The road between c<sub>i</sub> and c<sub>i+1</sub> is safe if there is a guard in c<sub>i</sub> or c<sub>i+1</sub>. The queen has already placed a few guards in some of the cities, but she is not sure if they are enough to keep the roads safe. She wants to know the minimum number of new guards she needs to hire. | [save_quantamland.cpp](common_ds_problems/save_quantumland.cpp)|
136137

137138
### Math Problems
138139
| Problem | Solution |
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* In Quantumland, there are n cities numbered from 1 to n. Here, ci denotes the ith city.
3+
* There are n−1 roads in Quantumland. Here, ci and ci+1 have a bidirectional road between them for each i<n.
4+
* There is a rumor that Flatland is going to attack Quantumland, and the queen wants to keep her land safe.
5+
* The road between ci and ci+1 is safe if there is a guard in ci or ci+1.
6+
* The queen has already placed a few guards in some of the cities,
7+
* but she is not sure if they are enough to keep the roads safe.
8+
* She wants to know the minimum number of new guards she needs to hire.
9+
*
10+
* Input format:
11+
* The first line will contain an integer n.
12+
* In the next line, there will be n integers b1,b2...bn.
13+
*
14+
* If bi=1, that means there is a guard in city ci.
15+
* If bi=0, that means there are no guards in city ci.
16+
*
17+
* Example input
18+
* 5
19+
* 1 1 0 1 0
20+
* 5
21+
* 0 0 1 0 0
22+
*
23+
* Output:
24+
* 0
25+
* 2
26+
*
27+
*/
28+
29+
#include <vector>
30+
#include <iostream>
31+
32+
33+
34+
int min_guards_required(std::vector<int> & cities ) {
35+
int guardCount = 0;
36+
int unsafeRoads = 0;
37+
int n = cities.size();
38+
if ( n == 1 ) {
39+
std::cout << guardCount << std::endl;
40+
return 0;
41+
}
42+
int i = 0;
43+
while ( i < n - 1 ) {
44+
unsafeRoads = 0;
45+
if (cities[i] == 0 && cities[i+1] == 0) {
46+
while(i < n-1 && cities[i] == 0 && cities[i+1] == 0) {
47+
++unsafeRoads;
48+
++i;
49+
}
50+
if ( unsafeRoads <= 2 ) {
51+
guardCount += 1;
52+
} else {
53+
if ( unsafeRoads % 2 ) {
54+
guardCount += (unsafeRoads/2) + 1;
55+
} else {
56+
guardCount += unsafeRoads/2;
57+
}
58+
}
59+
}
60+
++i;
61+
}
62+
return guardCount;
63+
}
64+
65+
66+
int main() {
67+
int n;
68+
std::cin >> n;
69+
std::vector<int> cities(n);
70+
for(int i = 0; i < n; i++){
71+
std::cin >> cities[i];
72+
}
73+
std::cout << min_guards_required(cities) << std::endl;
74+
return 0;
75+
}
76+

0 commit comments

Comments
 (0)