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]; + } +}; 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); + } +}; 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); + } +}; 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); + } +}; + 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); + } +};