|
| 1 | +#include <bits/stdc++.h> |
| 2 | + |
| 3 | +using namespace std; |
| 4 | + |
| 5 | +string ltrim(const string &); |
| 6 | +string rtrim(const string &); |
| 7 | +vector<string> split(const string &); |
| 8 | + |
| 9 | +/* |
| 10 | + * Complete the 'jumpingOnClouds' function below. |
| 11 | + * |
| 12 | + * The function is expected to return an INTEGER. |
| 13 | + * The function accepts INTEGER_ARRAY c as parameter. |
| 14 | + */ |
| 15 | + |
| 16 | +int jumpingOnClouds(vector<int> c) { |
| 17 | + int jumps = 0; // To count the total number of jumps |
| 18 | + int i = 0; // Current position |
| 19 | + |
| 20 | + // Traverse the cloud array |
| 21 | + while (i < c.size() - 1) { |
| 22 | + // If a jump of 2 is safe, make the jump |
| 23 | + if (i + 2 < c.size() && c[i + 2] == 0) { |
| 24 | + i += 2; |
| 25 | + } else { |
| 26 | + // Otherwise, jump only 1 cloud |
| 27 | + i += 1; |
| 28 | + } |
| 29 | + jumps++; // Increment the jump counter |
| 30 | + } |
| 31 | + |
| 32 | + return jumps; |
| 33 | +} |
| 34 | + |
| 35 | +int main() |
| 36 | +{ |
| 37 | + ofstream fout(getenv("OUTPUT_PATH")); |
| 38 | + |
| 39 | + string n_temp; |
| 40 | + getline(cin, n_temp); |
| 41 | + |
| 42 | + int n = stoi(ltrim(rtrim(n_temp))); |
| 43 | + |
| 44 | + string c_temp_temp; |
| 45 | + getline(cin, c_temp_temp); |
| 46 | + |
| 47 | + vector<string> c_temp = split(rtrim(c_temp_temp)); |
| 48 | + |
| 49 | + vector<int> c(n); |
| 50 | + |
| 51 | + for (int i = 0; i < n; i++) { |
| 52 | + int c_item = stoi(c_temp[i]); |
| 53 | + |
| 54 | + c[i] = c_item; |
| 55 | + } |
| 56 | + |
| 57 | + int result = jumpingOnClouds(c); |
| 58 | + |
| 59 | + fout << result << "\n"; |
| 60 | + |
| 61 | + fout.close(); |
| 62 | + |
| 63 | + return 0; |
| 64 | +} |
| 65 | + |
| 66 | +string ltrim(const string &str) { |
| 67 | +string s(str); |
| 68 | + |
| 69 | +s.erase( |
| 70 | + s.begin(), |
| 71 | + find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace))) |
| 72 | +); |
| 73 | + |
| 74 | +return s; |
| 75 | +} |
| 76 | + |
| 77 | +string rtrim(const string &str) { |
| 78 | +string s(str); |
| 79 | + |
| 80 | +s.erase( |
| 81 | +find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(), |
| 82 | + s.end() |
| 83 | +); |
| 84 | + |
| 85 | +return s; |
| 86 | +} |
| 87 | + |
| 88 | +vector<string> split(const string &str) { |
| 89 | +vector<string> tokens; |
| 90 | + |
| 91 | +string::size_type start = 0; |
| 92 | +string::size_type end = 0; |
| 93 | + |
| 94 | +while ((end = str.find(" ", start)) != string::npos) { |
| 95 | +tokens.push_back(str.substr(start, end - start)); |
| 96 | + |
| 97 | +start = end + 1; |
| 98 | +} |
| 99 | + |
| 100 | +tokens.push_back(str.substr(start)); |
| 101 | + |
| 102 | +return tokens; |
| 103 | +} |
0 commit comments