Commit 9065bc9e09cdcded751b02c8e2d853bacf8cbff3
1 parent
eb0b70f675
Exists in
main
Update project.
Showing 3 changed files with 151 additions and 0 deletions Side-by-side Diff
BasicsOfArrayStringGreedyandBitManipulation/Vector-STL/LowerBound.cpp
View file @
9065bc9
1 | +// | |
2 | +// Created by ronal on 2/12/2023. | |
3 | +// Problem Description Link. | |
4 | +// https://www.hackerrank.com/challenges/cpp-lower-bound/copy-from/307592352 | |
5 | + | |
6 | +#include <cmath> | |
7 | +#include <cstdio> | |
8 | +#include <vector> | |
9 | +#include <iostream> | |
10 | +#include <algorithm> | |
11 | +using namespace std; | |
12 | + | |
13 | + | |
14 | +void PrintLowerBouder(vector<int> values, int queries){ | |
15 | + vector<int>::iterator it; | |
16 | + int element; | |
17 | + for(int i = 0; i < queries; i++){ | |
18 | + cin >> element; | |
19 | + | |
20 | + if(binary_search(values.begin(), values.end(), element)){ | |
21 | + it = lower_bound(values.begin(), values.end(), element); | |
22 | + cout << "Yes " << it - values.begin() + 1 << endl; | |
23 | + }else{ | |
24 | + it = lower_bound(values.begin(), values.end(), element); | |
25 | + cout << "No " << it - values.begin() + 1 << endl; | |
26 | + } | |
27 | + | |
28 | + } | |
29 | +} | |
30 | +int main() { | |
31 | + /* Enter your code here. Read input from STDIN. Print output to STDOUT */ | |
32 | + vector<int> values; | |
33 | + int size, value, queries, element; | |
34 | + | |
35 | + cin >> size; | |
36 | + for(int i = 0; i < size; i++){ | |
37 | + cin >> value; | |
38 | + values.push_back(value); | |
39 | + } | |
40 | + cin >> queries; | |
41 | + PrintLowerBouder(values,queries); | |
42 | + | |
43 | + return 0; | |
44 | +} |
IMPORTANTE.md
View file @
9065bc9
SearchingSortingandBasicDataStructures/Stack/Problem4/BalancedBrackets.cpp
View file @
9065bc9
1 | +// | |
2 | +// Created by ronal on 2/12/2023. | |
3 | +// Problem Description Link. | |
4 | +// https://www.hackerrank.com/challenges/ctci-balanced-brackets/problem | |
5 | + | |
6 | +#include <bits/stdc++.h> | |
7 | + | |
8 | +using namespace std; | |
9 | + | |
10 | +string ltrim(const string &); | |
11 | +string rtrim(const string &); | |
12 | + | |
13 | +/* | |
14 | + * Complete the 'isBalanced' function below. | |
15 | + * | |
16 | + * The function is expected to return a STRING. | |
17 | + * The function accepts STRING expression as parameter. | |
18 | + */ | |
19 | + | |
20 | +string isBalanced(string str) { | |
21 | + stack<char> pilha; | |
22 | + char c; | |
23 | + | |
24 | + for(int i = 0; i < str.size(); i++){ | |
25 | + if(str[i] == '(' || str[i] == '[' || str[i] == '{'){ | |
26 | + pilha.push(str[i]); | |
27 | + continue; | |
28 | + } | |
29 | + | |
30 | + if(pilha.empty()){ | |
31 | + return "NO"; | |
32 | + }else if(str[i] == ')'){ | |
33 | + c = pilha.top(); | |
34 | + pilha.pop(); | |
35 | + if(c == '[' || c == '{'){ | |
36 | + return "NO"; | |
37 | + } | |
38 | + }else if( str[i] == ']'){ | |
39 | + c = pilha.top(); | |
40 | + pilha.pop(); | |
41 | + if(c == '(' || c == '{'){ | |
42 | + return "NO"; | |
43 | + } | |
44 | + }else if(str[i] == '}'){ | |
45 | + c = pilha.top(); | |
46 | + pilha.pop(); | |
47 | + if(c == '(' || c == '['){ | |
48 | + return "NO"; | |
49 | + } | |
50 | + } | |
51 | + | |
52 | + } | |
53 | + if(pilha.empty()){ | |
54 | + return "YES"; | |
55 | + }else{ | |
56 | + return "NO"; | |
57 | + } | |
58 | +} | |
59 | + | |
60 | +int main() | |
61 | +{ | |
62 | + ofstream fout(getenv("OUTPUT_PATH")); | |
63 | + | |
64 | + string t_temp; | |
65 | + getline(cin, t_temp); | |
66 | + | |
67 | + int t = stoi(ltrim(rtrim(t_temp))); | |
68 | + | |
69 | + for (int t_itr = 0; t_itr < t; t_itr++) { | |
70 | + string expression; | |
71 | + getline(cin, expression); | |
72 | + | |
73 | + string res = isBalanced(expression); | |
74 | + | |
75 | + fout << res << "\n"; | |
76 | + } | |
77 | + | |
78 | + fout.close(); | |
79 | + | |
80 | + return 0; | |
81 | +} | |
82 | + | |
83 | +string ltrim(const string &str) { | |
84 | + string s(str); | |
85 | + | |
86 | + s.erase( | |
87 | + s.begin(), | |
88 | + find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace))) | |
89 | + ); | |
90 | + | |
91 | + return s; | |
92 | +} | |
93 | + | |
94 | +string rtrim(const string &str) { | |
95 | + string s(str); | |
96 | + | |
97 | + s.erase( | |
98 | + find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(), | |
99 | + s.end() | |
100 | + ); | |
101 | + | |
102 | + return s; | |
103 | +} |