Skip to content

Commit

Permalink
Merge branch '7oSkaaa:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
AhmedGamal2212 authored May 13, 2023
2 parents 57e4b0c + ba6de89 commit 4f143b5
Show file tree
Hide file tree
Showing 5 changed files with 259 additions and 0 deletions.
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);
}
};


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);
}
};
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;
}
}
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;
}
};
103 changes: 103 additions & 0 deletions 05- May/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
1. **[Spiral Matrix](#09--spiral-matrix)**
1. **[Spiral Matrix II](#10--spiral-matrix-ii)**
1. **[Uncrossed Lines](#11--uncrossed-lines)**
1. **[Solving Questions With Brainpower](#12--solving-questions-with-brainpower)**
1. **[Count Ways To Build Good Strings](#13--count-ways-to-build-good-strings)**

<hr>
<br><br>
Expand Down Expand Up @@ -624,4 +626,105 @@ public:
}
};
```

<hr>
<br><br>

## 12) [Solving Questions With Brainpower](https://leetcode.com/problems/solving-questions-with-brainpower/)

### Difficulty

![](https://img.shields.io/badge/Medium-orange?style=for-the-badge)

### Related Topic

`Array` `Dynamic Programming`

### Code


```cpp
// for this problem we will use dp to solve it
// we can see that we can take the question or leave it, so we will try to take it and leave it and take the maximum
// we will use dp[i] to be the maximum points we can take from the questions from i to n
// so dp[i] = max(dp[i+1], dp[i+b+1] + p) where p is the points of the question and b is the number of questions we will skip

// dp[i+1] is the maximum points we can take if we skip the current question (move to the next question)
// dp[i+b+1] + p is the maximum points we can take if we take the current question and skip the next b questions (move to the next b+1 question)
// the answer will be dp[0] which is the maximum points we can take from the first question to the last question

class Solution {
public:
long long mostPoints(vector<vector<int>>& questions) {
const int n = int(questions.size());
vector<long long> dp(n + 5);

for(int i = n - 1; ~i; --i) {
int p = questions[i][0], b = questions[i][1];
dp[i] = max(dp[i + 1], dp[min(i + b + 1, n)] + p);
}

return dp[0];
}
};
```

<hr>
<br><br>

## 13) [Count Ways To Build Good Strings](https://leetcode.com/problems/count-ways-to-build-good-strings/)

### Difficulty

![](https://img.shields.io/badge/Medium-orange?style=for-the-badge)

### Related Topic

`Dynamic Programming`

### Code


```cpp
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;
}
};
```

0 comments on commit 4f143b5

Please sign in to comment.