forked from 7oSkaaa/LeetCode_DailyChallenge_2023
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '7oSkaaa:main' into main
- Loading branch information
Showing
5 changed files
with
221 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
...olving Questions With Brainpower/12- Solving Questions With Brainpower (Ahmed Hossam).cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<vector<int>>& 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]; | ||
} | ||
}; |
43 changes: 43 additions & 0 deletions
43
...g Questions With Brainpower/12- Solving Questions With Brainpower (Mahmoud Aboelsoud).cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<vector<int>> questions; | ||
// dp: 1d array for memoizing the dp | ||
vector<long long> 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<vector<int>>& 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); | ||
} | ||
}; |
36 changes: 36 additions & 0 deletions
36
...3- Count Ways To Build Good Strings/13- Count Ways To Build Good Strings (Lama Salah).cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
}; |
63 changes: 63 additions & 0 deletions
63
...Count Ways To Build Good Strings/13- Count Ways To Build Good Strings (Mohamed Emara).cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
}; | ||
|
47 changes: 47 additions & 0 deletions
47
...3- Count Ways To Build Good Strings/13- Count Ways To Build Good Strings (Omar Sanad).cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
}; |