From 2c7f48860adeebb774fcf6dd98367e5212506baa Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Fri, 12 May 2023 00:58:27 +0000 Subject: [PATCH 01/10] Add new daily problem --- .github/data/problems.json | 4 ++++ 05- May/12- Solving Questions With Brainpower/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/12- Solving Questions With Brainpower/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index 3f3b0c8fc..e90d3d656 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -531,6 +531,10 @@ { "day": "11", "title": "Uncrossed Lines" + }, + { + "day": "12", + "title": "Solving Questions With Brainpower" } ] } \ No newline at end of file diff --git a/05- May/12- Solving Questions With Brainpower/.gitkeep b/05- May/12- Solving Questions With Brainpower/.gitkeep new file mode 100644 index 000000000..e69de29bb From 13c9eba3d05f309c1ced7bb73672efc30c28fceb Mon Sep 17 00:00:00 2001 From: aboelsooud Date: Fri, 12 May 2023 15:12:01 +0300 Subject: [PATCH 02/10] add the 12-th day problem solution --- ...ns With Brainpower (Mahmoud Aboelsoud).cpp | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 05- May/12- Solving Questions With Brainpower/12- Solving Questions With Brainpower (Mahmoud Aboelsoud).cpp diff --git a/05- May/12- Solving Questions With Brainpower/12- Solving Questions With Brainpower (Mahmoud Aboelsoud).cpp b/05- May/12- Solving Questions With Brainpower/12- Solving Questions With Brainpower (Mahmoud Aboelsoud).cpp new file mode 100644 index 000000000..693891d18 --- /dev/null +++ b/05- May/12- Solving Questions With Brainpower/12- Solving Questions With Brainpower (Mahmoud Aboelsoud).cpp @@ -0,0 +1,43 @@ +// Author: Mahmoud Aboelsoud + +class Solution { +public: + // we need to find the maximum points we can get from answering the questions + // we can do that using 0/1 dp + // we can either answer the question or leave it + // if we answer the question we will get the points of this question and move to the next question after the brainpower of this question + + // questions: 2d vector of questions (points, brainpower) + vector> questions; + // dp: 1d array for memoizing the dp + vector dp; + + // function to get the maximum points we can get from answering the questions starting from idx + long long get_max(int idx){ + // if we have finished all questions return 0 + if(idx >= questions.size()) return 0; + + // if we have already calculated the answer for this state return it + if(dp[idx] != -1) return dp[idx]; + + // we can either answer the question or leave it + // 1- leave the question and move to the next question + long long ans = get_max(idx + 1); + + // 2- answer the question and move to the next question after the brainpower of this question + ans = max(ans, questions[idx][0] + get_max(idx + questions[idx][1] + 1)); + + // return the answer + return dp[idx] = ans; + } + + + long long mostPoints(vector>& questions) { + // initialize the questions and dp + this -> questions = questions; + dp.assign(questions.size() + 5, -1); + + // return the maximum points we can get from answering the questions starting from idx = 0 + return get_max(0); + } +}; From 17769e6ccc46404a9cee83f0a3b67f170e41d020 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Sat, 13 May 2023 00:46:55 +0000 Subject: [PATCH 03/10] Add new daily problem --- .github/data/problems.json | 4 ++++ 05- May/13- Count Ways To Build Good Strings/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/13- Count Ways To Build Good Strings/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index e90d3d656..ccc399845 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -535,6 +535,10 @@ { "day": "12", "title": "Solving Questions With Brainpower" + }, + { + "day": "13", + "title": "Count Ways To Build Good Strings" } ] } \ No newline at end of file diff --git a/05- May/13- Count Ways To Build Good Strings/.gitkeep b/05- May/13- Count Ways To Build Good Strings/.gitkeep new file mode 100644 index 000000000..e69de29bb From 9316158a5104515921385b2d3644ef6fa4352ce2 Mon Sep 17 00:00:00 2001 From: MohamedAEmara Date: Sat, 13 May 2023 06:42:53 +0300 Subject: [PATCH 04/10] Add 13- Count Ways To Build Good Strings (Mohamed Emara).cpp --- ...s to Build Good String (Mohamed Emara).cpp | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 05- May/13- Count Ways To Build Good Strings/13- Count Ways to Build Good String (Mohamed Emara).cpp diff --git a/05- May/13- Count Ways To Build Good Strings/13- Count Ways to Build Good String (Mohamed Emara).cpp b/05- May/13- Count Ways To Build Good Strings/13- Count Ways to Build Good String (Mohamed Emara).cpp new file mode 100644 index 000000000..adb3b5266 --- /dev/null +++ b/05- May/13- Count Ways To Build Good Strings/13- Count Ways to Build Good String (Mohamed Emara).cpp @@ -0,0 +1,63 @@ + +// Author: Mohamed Emara + +class Solution { +public: + int dp[100005]; + int mod = 1e9 + 7; + + // global variables. + int nZero, nOne; + int hi, lo; + + + int rec(int len) + { + // base case ---> if length exceeds the high limit --> return 0 (don't include it as a solution) + if(len > hi) + return 0; + + // memoization + int &ret = dp[len]; + if(~ret) + return ret; + + // if the current length is exactly equals high limit --> add 1 as a valid solution + // but can't increase lenght any more --> so return 1 + if(len == hi) + return 1; + + // if the current length is in the range (low, high) --> Add 1 as it's a valid solution + // & continue to other states + if(len >= lo && len < hi) + return ret = (1 + (rec(len+nZero)%mod + rec(len+nOne)%mod))%mod; + + + // if the current lenght is below low limit --> go to the next states but as it's not + // a valid solution we don't add 1 here. + return ret = ((rec(len+nZero)%mod + rec(len+nOne)%mod))%mod; + } + + + int countGoodStrings(int low, int high, int zero, int one) { + /* ======== IDEA =======*/ + /* + Trace the lenght of the current segment of zeros and ones + We only interested in the lenght, No need to construct the segment + As every segment is unique --> whether add '1' one times + o or add '0' zeros times + there is no overlaps + Continue increaing the length in the two different ways + Until the lenght enters the range (low, high) + start adding 1 to the result and continue till the end of the range (high) + */ + hi = high; + lo = low; + nZero = zero; + nOne = one; + + memset(dp, -1, sizeof(dp)); + return rec(0); + } +}; + From 45ea8ea778f6037e49d1483be15da646c98ff304 Mon Sep 17 00:00:00 2001 From: MohamedAEmara Date: Sat, 13 May 2023 06:46:24 +0300 Subject: [PATCH 05/10] Delete --- ...s to Build Good String (Mohamed Emara).cpp | 63 ------------------- 1 file changed, 63 deletions(-) delete mode 100644 05- May/13- Count Ways To Build Good Strings/13- Count Ways to Build Good String (Mohamed Emara).cpp diff --git a/05- May/13- Count Ways To Build Good Strings/13- Count Ways to Build Good String (Mohamed Emara).cpp b/05- May/13- Count Ways To Build Good Strings/13- Count Ways to Build Good String (Mohamed Emara).cpp deleted file mode 100644 index adb3b5266..000000000 --- a/05- May/13- Count Ways To Build Good Strings/13- Count Ways to Build Good String (Mohamed Emara).cpp +++ /dev/null @@ -1,63 +0,0 @@ - -// Author: Mohamed Emara - -class Solution { -public: - int dp[100005]; - int mod = 1e9 + 7; - - // global variables. - int nZero, nOne; - int hi, lo; - - - int rec(int len) - { - // base case ---> if length exceeds the high limit --> return 0 (don't include it as a solution) - if(len > hi) - return 0; - - // memoization - int &ret = dp[len]; - if(~ret) - return ret; - - // if the current length is exactly equals high limit --> add 1 as a valid solution - // but can't increase lenght any more --> so return 1 - if(len == hi) - return 1; - - // if the current length is in the range (low, high) --> Add 1 as it's a valid solution - // & continue to other states - if(len >= lo && len < hi) - return ret = (1 + (rec(len+nZero)%mod + rec(len+nOne)%mod))%mod; - - - // if the current lenght is below low limit --> go to the next states but as it's not - // a valid solution we don't add 1 here. - return ret = ((rec(len+nZero)%mod + rec(len+nOne)%mod))%mod; - } - - - int countGoodStrings(int low, int high, int zero, int one) { - /* ======== IDEA =======*/ - /* - Trace the lenght of the current segment of zeros and ones - We only interested in the lenght, No need to construct the segment - As every segment is unique --> whether add '1' one times - o or add '0' zeros times - there is no overlaps - Continue increaing the length in the two different ways - Until the lenght enters the range (low, high) - start adding 1 to the result and continue till the end of the range (high) - */ - hi = high; - lo = low; - nZero = zero; - nOne = one; - - memset(dp, -1, sizeof(dp)); - return rec(0); - } -}; - From f5395c6cb25097f9b0bbda541f99a041a16f2039 Mon Sep 17 00:00:00 2001 From: MohamedAEmara Date: Sat, 13 May 2023 06:51:02 +0300 Subject: [PATCH 06/10] Add 13- Count Ways To Build Good Strings (Mohamed Emara).cpp --- ... To Build Good Strings (Mohamed Emara).cpp | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 05- May/13- Count Ways To Build Good Strings/13- Count Ways To Build Good Strings (Mohamed Emara).cpp diff --git a/05- May/13- Count Ways To Build Good Strings/13- Count Ways To Build Good Strings (Mohamed Emara).cpp b/05- May/13- Count Ways To Build Good Strings/13- Count Ways To Build Good Strings (Mohamed Emara).cpp new file mode 100644 index 000000000..adb3b5266 --- /dev/null +++ b/05- May/13- Count Ways To Build Good Strings/13- Count Ways To Build Good Strings (Mohamed Emara).cpp @@ -0,0 +1,63 @@ + +// Author: Mohamed Emara + +class Solution { +public: + int dp[100005]; + int mod = 1e9 + 7; + + // global variables. + int nZero, nOne; + int hi, lo; + + + int rec(int len) + { + // base case ---> if length exceeds the high limit --> return 0 (don't include it as a solution) + if(len > hi) + return 0; + + // memoization + int &ret = dp[len]; + if(~ret) + return ret; + + // if the current length is exactly equals high limit --> add 1 as a valid solution + // but can't increase lenght any more --> so return 1 + if(len == hi) + return 1; + + // if the current length is in the range (low, high) --> Add 1 as it's a valid solution + // & continue to other states + if(len >= lo && len < hi) + return ret = (1 + (rec(len+nZero)%mod + rec(len+nOne)%mod))%mod; + + + // if the current lenght is below low limit --> go to the next states but as it's not + // a valid solution we don't add 1 here. + return ret = ((rec(len+nZero)%mod + rec(len+nOne)%mod))%mod; + } + + + int countGoodStrings(int low, int high, int zero, int one) { + /* ======== IDEA =======*/ + /* + Trace the lenght of the current segment of zeros and ones + We only interested in the lenght, No need to construct the segment + As every segment is unique --> whether add '1' one times + o or add '0' zeros times + there is no overlaps + Continue increaing the length in the two different ways + Until the lenght enters the range (low, high) + start adding 1 to the result and continue till the end of the range (high) + */ + hi = high; + lo = low; + nZero = zero; + nOne = one; + + memset(dp, -1, sizeof(dp)); + return rec(0); + } +}; + From 492c131a25ccaf4ba0f01efb2693f1390ae25d24 Mon Sep 17 00:00:00 2001 From: lamasalah32 Date: Sat, 13 May 2023 08:28:56 +0300 Subject: [PATCH 07/10] 13- Count Ways To Build Good Strings (Lama Salah).cpp --- ...ays To Build Good Strings (Lama Salah).cpp | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 05- May/13- Count Ways To Build Good Strings/13- Count Ways To Build Good Strings (Lama Salah).cpp diff --git a/05- May/13- Count Ways To Build Good Strings/13- Count Ways To Build Good Strings (Lama Salah).cpp b/05- May/13- Count Ways To Build Good Strings/13- Count Ways To Build Good Strings (Lama Salah).cpp new file mode 100644 index 000000000..a2c644a45 --- /dev/null +++ b/05- May/13- Count Ways To Build Good Strings/13- Count Ways To Build Good Strings (Lama Salah).cpp @@ -0,0 +1,36 @@ +// Author: Lama Salah + +class Solution { +public: + // a constant variable mod that is initialized to 1e9+7. + const int mod = 1e9 + 7; + int l, r, zero, one, memo[100005]; + + int dp(int i = 0){ + // If i is greater than r, then the function returns 0. + if (i > r) return 0; + + // initialize a reference to an integer variable ans with the value stored in the memo array at the index i. + int& ans = memo[i]; + + // If the value is not equal to -1, then the function returns the value, else set the value of ans equal to zero. + if (~ans) return ans; + ans = 0; + + // If i is within the range [l, r], then ans is incremented by 1. Then, the function recursively calls itself with an argument of i+zero and i+one. + // The results of these two recursive calls are added to ans. + ans = (i >= l && i <= r) + ans% mod + dp(i + zero)%mod + dp(i + one)%mod; + return ans%mod; + } + + int countGoodStrings(int low, int high, int zero, int one) { + this -> l = low; + this -> r = high; + this -> zero = zero; + this -> one = one; + + // initialize the memo array with -1 values using the memset() function. + memset(memo, -1, sizeof memo); + return dp(0); + } +}; From 31adcdc0d0d501f7968ad44070c7d97b6f505fbf Mon Sep 17 00:00:00 2001 From: Omar Sanad <108091921+OmarSanad3@users.noreply.github.com> Date: Sat, 13 May 2023 14:23:27 +0300 Subject: [PATCH 08/10] Added Sanad's Solution, May 13th --- ...ays To Build Good Strings (Omar Sanad).cpp | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 05- May/13- Count Ways To Build Good Strings/13- Count Ways To Build Good Strings (Omar Sanad).cpp diff --git a/05- May/13- Count Ways To Build Good Strings/13- Count Ways To Build Good Strings (Omar Sanad).cpp b/05- May/13- Count Ways To Build Good Strings/13- Count Ways To Build Good Strings (Omar Sanad).cpp new file mode 100644 index 000000000..692cd410f --- /dev/null +++ b/05- May/13- Count Ways To Build Good Strings/13- Count Ways To Build Good Strings (Omar Sanad).cpp @@ -0,0 +1,47 @@ +// author : Omar Sanad + +class Solution { +public: + // declare the variables inside the class itself to be able to use it anywhere in the class + // also declare a 2D array for memoization + int low, high, zero, one, dp[100001][2]; + + // recursion function, takes two parameters the current position and the last used charecter '1' or '0' + int rec(int idx, bool last_is_one) { + // if we exceeded the allowed limit, then we return + if (idx > high) + return 0; + + // assign the dp[idx][last_is_one] by reference, for clean code + int &ret = dp[idx][last_is_one]; + + // if the answer for this state has been already calculated, then return its value which is stored in the dp + if (~ret) return ret; + + // otherwise, then we have to calculate this state, + // if we are in the allowed range (low <= idx <= high), then count this state as valid + ret = idx >= low and idx <= high; + + // try to take zeros + ret = (ret + rec(idx + one, true)) % int(1e9 + 7); + + // try to take ones + ret = (ret + rec(idx + zero, false)) % int(1e9 + 7); + + // return the answer of this state + return ret; + } + int countGoodStrings(int low, int high, int zero, int one) { + // assign the passed parameters to the variables declared in the class itself + this->low = low; + this-> high = high; + this->zero = zero; + this->one = one; + + // initialize the dp with -1, mark as not calculated + memset(dp, -1, sizeof(dp)); + + // return the answer to the problem + return rec(0, true); + } +}; From 918813e9c4b4d4d5e6e4f8d87a85a64b0741a8cf Mon Sep 17 00:00:00 2001 From: Ahmed Hossam <63050133+7oSkaaa@users.noreply.github.com> Date: Sat, 13 May 2023 15:18:16 +0300 Subject: [PATCH 09/10] Create 12- Solving Questions With Brainpower (Ahmed Hossam).cpp --- ...estions With Brainpower (Ahmed Hossam).cpp | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 05- May/12- Solving Questions With Brainpower/12- Solving Questions With Brainpower (Ahmed Hossam).cpp diff --git a/05- May/12- Solving Questions With Brainpower/12- Solving Questions With Brainpower (Ahmed Hossam).cpp b/05- May/12- Solving Questions With Brainpower/12- Solving Questions With Brainpower (Ahmed Hossam).cpp new file mode 100644 index 000000000..cf3c441a0 --- /dev/null +++ b/05- May/12- Solving Questions With Brainpower/12- Solving Questions With Brainpower (Ahmed Hossam).cpp @@ -0,0 +1,32 @@ +// Author: Ahmed Hossam + +class Solution { +public: + + // Define long long as ll + typedef long long ll; + + // Define a function named mostPoints that takes a 2D vector of integers as input and returns a long long. + ll mostPoints(vector>& v) { + + // Get the number of rows in the vector. + int n = v.size(); + + // Initialize a vector of long longs of size n + 1. + vector < ll > dp(n + 1); + + // Iterate over the vector in reverse order. + for(int i = n - 1; i >= 0; i--){ + + // Get the first and second elements of the i-th row in the vector. + auto [points, brainpower] = make_pair(v[i][0], v[i][1]); + + // the first option is to skip this question + // the second option is to use this question and skip the next brainpower questions + dp[i] = max(dp[i + 1], points + dp[min(i + brainpower + 1, n)]); + } + + // Return the first element of the dp vector. + return dp[0]; + } +}; From 5ce312740698489cafe97997c71c9e7dc3674a0a Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Sun, 14 May 2023 01:41:57 +0000 Subject: [PATCH 10/10] Add new daily problem --- .github/data/problems.json | 4 ++++ 05- May/14- Maximize Score After N Operations/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/14- Maximize Score After N Operations/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index ccc399845..0986d48cb 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -539,6 +539,10 @@ { "day": "13", "title": "Count Ways To Build Good Strings" + }, + { + "day": "14", + "title": "Maximize Score After N Operations" } ] } \ No newline at end of file diff --git a/05- May/14- Maximize Score After N Operations/.gitkeep b/05- May/14- Maximize Score After N Operations/.gitkeep new file mode 100644 index 000000000..e69de29bb