-
Breaking the Records해커랭크(HackerRank) 2018. 8. 19. 17:39반응형
1. 문제
2. 알고리즘
키워드 - 구현
* 문제 풀이
- 입력 된 첫 배열의 요소에서 이전 값이 다음 값보다 크다면 highCount ++ 증가
- 입력 된 첫 배열의 요소에서 이전 값이 다음 값보다 작다면 lowCount ++ 증가
-highCount 와 lowCount 를 vector 에 추가하여 출력
3. 코드
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106#include <bits/stdc++.h>using namespace std;vector<string> split_string(string);/** Complete the breakingRecords function below.*/vector<int> breakingRecords(vector<int> score) {/** Write your code here.*/const int size = score.size();int prev = score[0];int highCount = 0 ;for(int i =1; i<size; i++) {if(prev < score[i]) {prev = score[i];highCount++;}}prev = score[0];int lowCount = 0;for(int i =1; i<size; i++) {if(prev > score[i]) {prev = score[i];lowCount++;}}vector<int> rArr;rArr.push_back(highCount);rArr.push_back(lowCount);return rArr;}int main(){ofstream fout(getenv("OUTPUT_PATH"));int n;cin >> n;cin.ignore(numeric_limits<streamsize>::max(), '\n');string score_temp_temp;getline(cin, score_temp_temp);vector<string> score_temp = split_string(score_temp_temp);vector<int> score(n);for (int score_itr = 0; score_itr < n; score_itr++) {int score_item = stoi(score_temp[score_itr]);score[score_itr] = score_item;}vector<int> result = breakingRecords(score);for (int result_itr = 0; result_itr < result.size(); result_itr++) {fout << result[result_itr];if (result_itr != result.size() - 1) {fout << " ";}}fout << "\n";fout.close();return 0;}vector<string> split_string(string input_string) {string::iterator new_end = unique(input_string.begin(), input_string.end(), [] (const char &x, const char &y) {return x == y and x == ' ';});input_string.erase(new_end, input_string.end());while (input_string[input_string.length() - 1] == ' ') {input_string.pop_back();}vector<string> splits;char delimiter = ' ';size_t i = 0;size_t pos = input_string.find(delimiter);while (pos != string::npos) {splits.push_back(input_string.substr(i, pos - i));i = pos + 1;pos = input_string.find(delimiter, i);}splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1));return splits;}cs 반응형'해커랭크(HackerRank)' 카테고리의 다른 글
Grading Students (0) 2018.08.19 Day 0: Mean, Median, and Mode (0) 2018.08.19 Birthday Chocolate (0) 2018.08.19 Divisible Sum Pairs (0) 2018.08.19 Migratory Birds (0) 2018.08.19