Skip to content
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions MinimumSpanningTrees
Submodule MinimumSpanningTrees added at fab214
92 changes: 92 additions & 0 deletions hackerrank/Designer PDF Viewer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#include <bits/stdc++.h>

using namespace std;

string ltrim(const string &);
string rtrim(const string &);
vector<string> split(const string &);

/*
* Complete the 'designerPdfViewer' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
* 1. INTEGER_ARRAY h
* 2. STRING word
*/

int designerPdfViewer(vector<int> h, string word) {
vector<int> ans;
for( int i =0;i<word.length();i++){
ans.push_back(h[word[i] - 'a']);
}
return (*max_element(ans.begin(),ans.end()))*(ans.size());
}

int main()
{
ofstream fout(getenv("OUTPUT_PATH"));

string h_temp_temp;
getline(cin, h_temp_temp);

vector<string> h_temp = split(rtrim(h_temp_temp));

vector<int> h(26);

for (int i = 0; i < 26; i++) {
int h_item = stoi(h_temp[i]);

h[i] = h_item;
}

string word;
getline(cin, word);

int result = designerPdfViewer(h, word);

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;
}
99 changes: 99 additions & 0 deletions hackerrank/The Hurdle Race.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#include <bits/stdc++.h>

using namespace std;

string ltrim(const string &);
string rtrim(const string &);
vector<string> split(const string &);

/*
* Complete the 'hurdleRace' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
* 1. INTEGER k
* 2. INTEGER_ARRAY height
*/

int hurdleRace(int k, vector<int> height) {
int max1 = *max_element(height.begin(),height.end());
if( max1 > k){
return max1 - k;
}else {
return 0;
}
}

int main()
{
ofstream fout(getenv("OUTPUT_PATH"));

string first_multiple_input_temp;
getline(cin, first_multiple_input_temp);

vector<string> first_multiple_input = split(rtrim(first_multiple_input_temp));

int n = stoi(first_multiple_input[0]);

int k = stoi(first_multiple_input[1]);

string height_temp_temp;
getline(cin, height_temp_temp);

vector<string> height_temp = split(rtrim(height_temp_temp));

vector<int> height(n);

for (int i = 0; i < n; i++) {
int height_item = stoi(height_temp[i]);

height[i] = height_item;
}

int result = hurdleRace(k, height);

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;
}
72 changes: 72 additions & 0 deletions hackerrank/Utopian Tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include <bits/stdc++.h>

using namespace std;
#define ll long long
string ltrim(const string &);
string rtrim(const string &);

/*
* Complete the 'utopianTree' function below.
*
* The function is expected to return an INTEGER.
* The function accepts INTEGER n as parameter.
*/

int utopianTree(int n) {
ll ht = 1;
for( int i = 0 ; i < n ; i++ ){
if(i%2==0){
ht = ht*2;
}else {
ht += 1;
}
}
return ht;
}

int main()
{
ofstream fout(getenv("OUTPUT_PATH"));

string t_temp;
getline(cin, t_temp);

int t = stoi(ltrim(rtrim(t_temp)));

for (int t_itr = 0; t_itr < t; t_itr++) {
string n_temp;
getline(cin, n_temp);

int n = stoi(ltrim(rtrim(n_temp)));

int result = utopianTree(n);

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;
}