Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,62 +8,128 @@ vector<string> split_string(string);
/*
* Complete the getMoneySpent function below.
*/
int getMoneySpent(vector<int> keyboards, vector<int> drives, int b) {
int i,sum=0,j,k=0,ans=-1;
sort(keyboards.begin(),keyboards.end());
sort(drives.begin(),drives.end());
for(i=0;i<keyboards.size();i++)
{
for(j=0;j<drives.size();j++)
int getMoneySpent(vector<int> keyboards, vector<int> drives, int b)
{
int i, sum = 0, j, k = 0, ans = -1;
sort(keyboards.begin(), keyboards.end());
sort(drives.begin(), drives.end());
for (i = 0; i < keyboards.size(); i++)
{
sum=keyboards[i]+drives[j];
if(sum<=b&&sum>ans)
ans=sum;

}
for (j = 0; j < drives.size(); j++)
{
sum = keyboards[i] + drives[j];
if (sum <= b && sum > ans)
ans = sum;
}
}
return ans;
}

int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
ofstream fout;
ostream *out_stream = nullptr;
if (const char *output_path = getenv("OUTPUT_PATH"))
{
fout.open(output_path);
if (!fout)
{
cerr << "Failed to open OUTPUT_PATH for writing" << endl;
return 1;
}
out_stream = &fout;
}
else
{
out_stream = &cout;
}

string bnm_temp;
getline(cin, bnm_temp);
if (!getline(cin, bnm_temp))
{
(*out_stream) << -1 << "\n";
if (fout.is_open())
fout.close();
return 0;
}

vector<string> bnm = split_string(bnm_temp);

int b = stoi(bnm[0]);

int n = stoi(bnm[1]);
if (bnm.size() < 3)
{
// malformed first line: cannot parse budget, n, m
(*out_stream) << -1 << "\n";
if (fout.is_open())
fout.close();
return 0;
}

int m = stoi(bnm[2]);
int b = 0, n = 0, m = 0;
try
{
b = stoi(bnm[0]);
n = stoi(bnm[1]);
m = stoi(bnm[2]);
}
catch (...)
{
// malformed numbers
(*out_stream) << -1 << "\n";
if (fout.is_open())
fout.close();
return 0;
}

string keyboards_temp_temp;
getline(cin, keyboards_temp_temp);
if (!getline(cin, keyboards_temp_temp))
{
(*out_stream) << -1 << "\n";
if (fout.is_open())
fout.close();
return 0;
}

vector<string> keyboards_temp = split_string(keyboards_temp_temp);

vector<int> keyboards(n);

for (int keyboards_itr = 0; keyboards_itr < n; keyboards_itr++) {
int keyboards_item = stoi(keyboards_temp[keyboards_itr]);

keyboards[keyboards_itr] = keyboards_item;
// safely parse the keyboard prices; ignore malformed tokens
vector<int> keyboards;
keyboards.reserve(keyboards_temp.size());
for (const string &tok : keyboards_temp)
{
try
{
keyboards.push_back(stoi(tok));
}
catch (...)
{
// skip malformed token
}
}

string drives_temp_temp;
getline(cin, drives_temp_temp);
if (!getline(cin, drives_temp_temp))
{
(*out_stream) << -1 << "\n";
if (fout.is_open())
fout.close();
return 0;
}

vector<string> drives_temp = split_string(drives_temp_temp);

vector<int> drives(m);

for (int drives_itr = 0; drives_itr < m; drives_itr++) {
int drives_item = stoi(drives_temp[drives_itr]);

drives[drives_itr] = drives_item;
// safely parse the drive prices; ignore malformed tokens
vector<int> drives;
drives.reserve(drives_temp.size());
for (const string &tok : drives_temp)
{
try
{
drives.push_back(stoi(tok));
}
catch (...)
{
// skip malformed token
}
}

/*
Expand All @@ -72,38 +138,27 @@ int main()

int moneySpent = getMoneySpent(keyboards, drives, b);

fout << moneySpent << "\n";
(*out_stream) << moneySpent << "\n";

fout.close();
if (fout.is_open())
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> split_string(string input_string)
{
// Use istringstream to safely split on whitespace. This handles empty strings
// and any amount of whitespace between tokens without manual indexing.
vector<string> splits;
char delimiter = ' ';

size_t i = 0;
size_t pos = input_string.find(delimiter);
if (input_string.empty())
return splits;

while (pos != string::npos) {
splits.push_back(input_string.substr(i, pos - i));

i = pos + 1;
pos = input_string.find(delimiter, i);
istringstream iss(input_string);
string token;
while (iss >> token)
{
splits.push_back(token);
}

splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1));

return splits;
}