HACKERRANK EQUAL STACK
HACKERRANK-EQUAL STACK
The following function is used to solve the problem https://www.hackerrank.com/challenges/equal-stacks/problem
#include <bits/stdc++.h> #include<string> #include<iostream> #include<vector> using namespace std; static int t1=0; static int t2=0; static int t3=0; vector<string> split_string(string);/** Author: Y Nguyen* equalStacks function with recursive method** Space Complexity O(1)*/int equalStacks(vector<int> &h1, vector<int> &h2, vector<int> &h3) { /* * Write your code here. */ if (t1 == t2 && t1 == t3) return t1; // find the highest town else { if (t1 >= t2 && t1 >= t3) { // remove the first cylinder of the higest stack t1=t1-h1.back(); h1.pop_back(); } else if (t2 >= t1 && t2 >= t3) { t2=t2-h2.back(); h2.pop_back(); } else { t3=t3-h3.back(); h3.pop_back(); } return equalStacks(h1, h2, h3); } // end else }
/******** PLEASE LEAVE COMMENT IF YOU HAVE ANY QUESTION ***********/
/************************************************************************
*The following main function is from hackerrank website.
*I highlight the red codes where I made a change to this function
*************************************************************************/
int main() { ofstream fout(getenv("OUTPUT_PATH")); string n1N2N3_temp; getline(cin, n1N2N3_temp); vector<string> n1N2N3 = split_string(n1N2N3_temp); int n1 = stoi(n1N2N3[0]); int n2 = stoi(n1N2N3[1]); int n3 = stoi(n1N2N3[2]); string h1_temp_temp; getline(cin, h1_temp_temp); vector<string> h1_temp = split_string(h1_temp_temp); vector<int> h1(n1); for (int h1_itr = 0; h1_itr < h1_temp.size(); h1_itr++) { int h1_item = stoi(h1_temp[h1_temp.size()-1-h1_itr]); h1[h1_itr] = h1_item; } string h2_temp_temp; getline(cin, h2_temp_temp); vector<string> h2_temp = split_string(h2_temp_temp); vector<int> h2(n2); for (int h2_itr = 0; h2_itr < h2_temp.size(); h2_itr++) { int h2_item = stoi(h2_temp[h2_temp.size()-1-h2_itr]); h2[h2_itr] = h2_item; } string h3_temp_temp; getline(cin, h3_temp_temp); vector<string> h3_temp = split_string(h3_temp_temp); vector<int> h3(n3); for (int h3_itr = 0; h3_itr < h3_temp.size(); h3_itr++) { int h3_item = stoi(h3_temp[h3_temp.size()-1-h3_itr]); h3[h3_itr] = h3_item; } t1=getHeight(h1); t2=getHeight(h2); t3=getHeight(h3); int result = equalStacks(h1, h2, h3); fout << result << "\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; }
Comments
Post a Comment