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
259 additions
and
0 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
...lving Questions With Brainpower/12- Solving Questions With Brainpower (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,53 @@ | ||
|
||
// Author: Mohamed Emara | ||
|
||
|
||
class Solution { | ||
public: | ||
int n; | ||
long long a[100005]; | ||
long long b[100005]; | ||
// states of dp ====> at each index whether to take or leave | ||
long long dp[100005][2]; | ||
|
||
|
||
long long rec(long long idx, bool state) | ||
{ | ||
if(idx >= n) | ||
return 0; | ||
long long &ret = dp[idx][state]; | ||
|
||
if(~ret) | ||
return ret; | ||
|
||
// if I'll take this item, I won't be able to take the next b[idx] items | ||
// So, the next item can take is (idx+b[idx]+1) | ||
|
||
long long take = a[idx] + rec(idx+b[idx]+1, 1); | ||
|
||
// Or skip this index, and go to the next one without changing the taken value | ||
long long leave = rec(idx+1, 0); | ||
|
||
// Zero and One (second paramter) is to distinguish between the two states. | ||
|
||
return ret = max(take, leave); | ||
} | ||
|
||
|
||
long long mostPoints(vector<vector<int>>& questions) { | ||
n = questions.size(); | ||
|
||
// fill the two global arrays with (earn value) and (nex unable item) | ||
for(int i=0; i<n; i++) | ||
{ | ||
a[i] = questions[i][0]; | ||
b[i] = questions[i][1]; | ||
} | ||
|
||
|
||
memset(dp, -1, sizeof(dp)); | ||
return rec(0, 0); | ||
} | ||
}; | ||
|
||
|
37 changes: 37 additions & 0 deletions
37
... Solving Questions With Brainpower/12- Solving Questions With Brainpower (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,37 @@ | ||
// author : Omar Sanad | ||
|
||
class Solution { | ||
public: | ||
// define a macro ll for long long | ||
#define ll long long | ||
|
||
// declaring the qu 2d vector (this is the 2d vec passed by the function) | ||
vector < vector < int > > qu; | ||
|
||
// declaring a 1D array for memoizing the dp | ||
ll dp[100001]; | ||
|
||
// the recursion function | ||
ll rec(int idx) { | ||
|
||
// if we have finished all elements in the qu, then return | ||
if (idx >= qu.size()) return 0; | ||
|
||
ll &ret = dp[idx]; | ||
if (~ret) return ret; | ||
|
||
ret = rec(idx + 1); // leave | ||
|
||
ret = max(ret, qu[idx][0] + rec(idx + qu[idx][1] + 1)); // take | ||
|
||
return ret; | ||
} | ||
|
||
long long mostPoints(vector<vector<int>>& questions) { | ||
memset(dp, -1, sizeof(dp)); | ||
|
||
qu = questions; | ||
|
||
return rec(0); | ||
} | ||
}; |
24 changes: 24 additions & 0 deletions
24
...olving Questions With Brainpower/12- Solving Questions With Brainpower (Osama Ayman).java
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,24 @@ | ||
// Author: Osama Ayman | ||
// Time: O(n) | ||
// Space: O(n) | ||
class Solution { | ||
public long mostPoints(int[][] questions) { | ||
return solve(questions, 0); | ||
} | ||
Map<Integer, Long> memo = new HashMap<>(); | ||
private long solve(int[][] questions, int idx){ | ||
// base case, out of bounds | ||
if(idx >= questions.length) return 0; | ||
// if already saved, return it | ||
if(memo.containsKey(idx)) return memo.get(idx); | ||
// solve | ||
long sol = questions[idx][0] + solve(questions, idx + 1 + questions[idx][1]); | ||
// skip | ||
long skip = solve(questions, idx+1); | ||
// result is the max of solve and skip | ||
long res = Math.max(sol, skip); | ||
// save result | ||
memo.put(idx, res); | ||
return res; | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
... Count Ways To Build Good Strings/13- Count Ways To Build Good Strings (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,42 @@ | ||
// Author: Ahmed Hossam | ||
|
||
class Solution { | ||
public: | ||
// Set the value of the constant MOD to 1e9+7 | ||
static constexpr int MOD = 1e9 + 7; | ||
|
||
// Function that adds a value to another and checks if the result is greater than or equal to MOD | ||
void add(int& ret, int to_add){ | ||
ret += to_add; | ||
if(ret >= MOD) | ||
ret -= MOD; | ||
} | ||
|
||
int countGoodStrings(int low, int high, int zero, int one) { | ||
// Initialize a vector called dp with size equal to high + 1 | ||
vector < int > dp(high + 1); | ||
|
||
// Set the first element of dp to 1 | ||
dp[0] = 1; | ||
|
||
// Initialize a variable called sum to 0 | ||
int sum = 0; | ||
|
||
for(int i = 1; i <= high; i++){ | ||
// If i is greater than or equal to zero, add the value of dp[i-zero] to dp[i] | ||
if(i >= zero) | ||
add(dp[i], dp[i - zero]); | ||
|
||
// If i is greater than or equal to one, add the value of dp[i-one] to dp[i] | ||
if(i >= one) | ||
add(dp[i], dp[i - one]); | ||
|
||
// If i is greater than or equal to low, add the value of dp[i] to sum | ||
if(i >= low) | ||
add(sum, dp[i]); | ||
} | ||
|
||
// Return the value of sum | ||
return sum; | ||
} | ||
}; |
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