From 2e15a30c68fd5d168e56dedde28f205a5d70226a Mon Sep 17 00:00:00 2001 From: anuppal101 <61056558+anuppal101@users.noreply.github.com> Date: Wed, 30 Sep 2020 17:48:12 +0530 Subject: [PATCH 1/2] Update knapsack.cpp --- .../1_maximum_amount_of_gold/knapsack.cpp | 145 +++++++++++++----- 1 file changed, 108 insertions(+), 37 deletions(-) diff --git a/week6_dynamic_programming2/1_maximum_amount_of_gold/knapsack.cpp b/week6_dynamic_programming2/1_maximum_amount_of_gold/knapsack.cpp index 3b95328..a546515 100644 --- a/week6_dynamic_programming2/1_maximum_amount_of_gold/knapsack.cpp +++ b/week6_dynamic_programming2/1_maximum_amount_of_gold/knapsack.cpp @@ -1,52 +1,123 @@ -#include -#include +#include + +#define fi first + +#define se second + +#define pb push_back + +#define mp make_pair + +#define all(x) (x).begin(), (x).end() + +#define scn(x) scanf("%d", &x) + +#define print(x) printf("%d", x) + +#define SZ(x) ((int)(x).size()) + +using namespace std; + +typedef vector VI; + +typedef long long ll; + +const int INF = 1e9; + + + +long long eval(long long a, long long b, char op) -using std::vector; -void print(vector> v) { - std::cout << "Printing array:\n"; - for (auto x : v) - { - for (auto y : x) - { - std::cout << y << " "; - } - std::cout << "\n"; - } - std::cout << "\n"; + if (op == '*') + + return a * b; + + else if (op == '+') + + return a + b; + + else if (op == '-') + + return a - b; + + else + + assert(0); + } -int optimal_weight(int W, const vector &weights) + + + +string equation; + +ll Min[30][30]; + +ll Max[30][30]; + +long long get_maximum_value() + { - vector> value(weights.size() + 1, vector(W + 1, 0)); - for (size_t i = 1; i <= weights.size(); i++) - { - for (int w = 1; w <= W; w++) + int Len = SZ(equation); + + int numOperations = (SZ(equation) + 1) / 2; + + for (int i = 0; i < numOperations; ++i) + + Min[i][i] = stoll(equation.substr(2 * i, 1)), Max[i][i] = stoll(equation.substr(2 * i, 1)); + + for (int s = 0; s < numOperations - 1; ++s) + + for (int i = 0; i < numOperations - s - 1; ++i) + { - value[i][w] = value[i - 1][w]; - if (weights[i - 1] <= w) + + int j = i + s + 1; + + ll minValue = LLONG_MAX, maxValue = LLONG_MIN; + + for (int k = i; k < j; ++k) + { - int val = value[i - 1][w - weights[i - 1]] + weights[i - 1]; - if (value[i][w] < val) - { - value[i][w] = val; - } + + ll a = eval(Min[i][k], Min[k + 1][j], equation[2 * k + 1]); + + ll b = eval(Min[i][k], Max[k + 1][j], equation[2 * k + 1]); + + ll c = eval(Max[i][k], Min[k + 1][j], equation[2 * k + 1]); + + ll d = eval(Max[i][k], Max[k + 1][j], equation[2 * k + 1]); + + minValue = min(minValue, min(a, min(b, min(c, d)))); + + maxValue = max(maxValue, max(a, max(b, max(c, d)))); + } + + Min[i][j] = minValue, Max[i][j] = maxValue; + } - // print(value); - } - return value[weights.size()][W]; + + return Max[0][numOperations - 1]; + } + + int main() + { - int n, W; - std::cin >> W >> n; - vector weights(n); - for (int i = 0; i < n; i++) - { - std::cin >> weights[i]; - } - std::cout << optimal_weight(W, weights) << '\n'; + + ios::sync_with_stdio(0); + + cin.tie(0); + + cin >> equation; + + cout << get_maximum_value(); + + return 0; + } From 1a058c93a95efebd818b27c3664a8f1770ae3a4a Mon Sep 17 00:00:00 2001 From: anuppal101 <61056558+anuppal101@users.noreply.github.com> Date: Wed, 30 Sep 2020 17:50:48 +0530 Subject: [PATCH 2/2] Update placing_parentheses.cpp --- .../placing_parentheses.cpp | 137 +++++++++++++++--- 1 file changed, 114 insertions(+), 23 deletions(-) diff --git a/week6_dynamic_programming2/3_maximum_value_of_an_arithmetic_expression/placing_parentheses.cpp b/week6_dynamic_programming2/3_maximum_value_of_an_arithmetic_expression/placing_parentheses.cpp index 06861ab..a546515 100644 --- a/week6_dynamic_programming2/3_maximum_value_of_an_arithmetic_expression/placing_parentheses.cpp +++ b/week6_dynamic_programming2/3_maximum_value_of_an_arithmetic_expression/placing_parentheses.cpp @@ -1,32 +1,123 @@ -#include -#include -#include -#include - -using std::vector; -using std::string; -using std::max; -using std::min; - -long long eval(long long a, long long b, char op) { - if (op == '*') { +#include + +#define fi first + +#define se second + +#define pb push_back + +#define mp make_pair + +#define all(x) (x).begin(), (x).end() + +#define scn(x) scanf("%d", &x) + +#define print(x) printf("%d", x) + +#define SZ(x) ((int)(x).size()) + +using namespace std; + +typedef vector VI; + +typedef long long ll; + +const int INF = 1e9; + + + +long long eval(long long a, long long b, char op) + +{ + + if (op == '*') + return a * b; - } else if (op == '+') { + + else if (op == '+') + return a + b; - } else if (op == '-') { + + else if (op == '-') + return a - b; - } else { + + else + assert(0); - } + } -long long get_maximum_value(const string &exp) { - //write your code here - return 0; + + +string equation; + +ll Min[30][30]; + +ll Max[30][30]; + +long long get_maximum_value() + +{ + + int Len = SZ(equation); + + int numOperations = (SZ(equation) + 1) / 2; + + for (int i = 0; i < numOperations; ++i) + + Min[i][i] = stoll(equation.substr(2 * i, 1)), Max[i][i] = stoll(equation.substr(2 * i, 1)); + + for (int s = 0; s < numOperations - 1; ++s) + + for (int i = 0; i < numOperations - s - 1; ++i) + + { + + int j = i + s + 1; + + ll minValue = LLONG_MAX, maxValue = LLONG_MIN; + + for (int k = i; k < j; ++k) + + { + + ll a = eval(Min[i][k], Min[k + 1][j], equation[2 * k + 1]); + + ll b = eval(Min[i][k], Max[k + 1][j], equation[2 * k + 1]); + + ll c = eval(Max[i][k], Min[k + 1][j], equation[2 * k + 1]); + + ll d = eval(Max[i][k], Max[k + 1][j], equation[2 * k + 1]); + + minValue = min(minValue, min(a, min(b, min(c, d)))); + + maxValue = max(maxValue, max(a, max(b, max(c, d)))); + + } + + Min[i][j] = minValue, Max[i][j] = maxValue; + + } + + return Max[0][numOperations - 1]; + } -int main() { - string s; - std::cin >> s; - std::cout << get_maximum_value(s) << '\n'; + + +int main() + +{ + + ios::sync_with_stdio(0); + + cin.tie(0); + + cin >> equation; + + cout << get_maximum_value(); + + return 0; + }