Created
June 20, 2019 06:55
-
-
Save kei9327/60ac48c4a959c401d99fba0579e19f8f to your computer and use it in GitHub Desktop.
HackerRank>Algorithm>implementation>migratoryBirds
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using namespace std; | |
string ltrim(const string &); | |
string rtrim(const string &); | |
vector<string> split(const string &); | |
int migratoryBirds(vector<int> ar) { | |
auto pSum = new int[ar.size()](); | |
for(auto it : ar) { pSum[it]++; } | |
int iMax = 0; | |
int iRet = 0; | |
for(int i=1; i <= 5; ++i) { | |
if ( pSum[i] > iMax ) { | |
iMax = pSum[i]; iRet = i; | |
} | |
} | |
delete[] pSum; | |
return iRet; | |
} | |
int main() | |
{ | |
ofstream fout(getenv("OUTPUT_PATH")); | |
string arr_count_temp; | |
getline(cin, arr_count_temp); | |
int arr_count = stoi(ltrim(rtrim(arr_count_temp))); | |
string arr_temp_temp; | |
getline(cin, arr_temp_temp); | |
vector<string> arr_temp = split(rtrim(arr_temp_temp)); | |
vector<int> arr(arr_count); | |
for (int i = 0; i < arr_count; i++) { | |
int arr_item = stoi(arr_temp[i]); | |
arr[i] = arr_item; | |
} | |
int result = migratoryBirds(arr); | |
fout << result << "\n"; | |
fout.close(); | |
return 0; | |
} | |
string ltrim(const string &str) { | |
string s(str); | |
s.erase( | |
s.begin(), | |
find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace))) | |
); | |
return s; | |
} | |
string rtrim(const string &str) { | |
string s(str); | |
s.erase( | |
find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(), | |
s.end() | |
); | |
return s; | |
} | |
vector<string> split(const string &str) { | |
vector<string> tokens; | |
string::size_type start = 0; | |
string::size_type end = 0; | |
while ((end = str.find(" ", start)) != string::npos) { | |
tokens.push_back(str.substr(start, end - start)); | |
start = end + 1; | |
} | |
tokens.push_back(str.substr(start)); | |
return tokens; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment