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
13 changed files
with
736 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
39 changes: 39 additions & 0 deletions
39
.../23- Kth Largest Element in a Stream/23- Kth Largest Element in a Stream (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,39 @@ | ||
// Author: Lama Salah | ||
|
||
class KthLargest { | ||
priority_queue<int, vector<int>, greater<int>> pq; // Priority queue to store elements in sorted order. | ||
int k; // Declares an integer variable k, representing the kth largest element. | ||
|
||
public: | ||
// Constructor of the KthLargest class. | ||
KthLargest(int k, vector<int>& nums) { | ||
// Initializes the priority queue pq as a min heap. | ||
pq = priority_queue<int, vector<int>, greater<int>>(); | ||
this->k = k; // Assigns the value of k to the member variable k. | ||
|
||
// Iterate over each element in the vector nums. | ||
for (auto& i : nums) | ||
pq.push(i); // Push the current element into the priority queue pq. | ||
|
||
// Remove the smallest element from pq until its size becomes k. | ||
while (pq.size() > k) | ||
pq.pop(); | ||
} | ||
|
||
// Add a new value to the KthLargest object. | ||
int add(int val) { | ||
pq.push(val); // Push the input value val into the priority queue pq. | ||
|
||
// If the size of pq is greater than k after adding the new value, remove the smallest element from pq. | ||
if (pq.size() > k) pq.pop(); | ||
|
||
// Return the top element of pq, which represents the kth largest element. | ||
return pq.top(); | ||
} | ||
}; | ||
|
||
/** | ||
* Your KthLargest object will be instantiated and called as such: | ||
* KthLargest* obj = new KthLargest(k, nums); | ||
* int param_1 = obj->add(val); | ||
*/ |
51 changes: 51 additions & 0 deletions
51
05- May/24- Maximum Subsequence Score/24- Maximum Subsequence Score (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,51 @@ | ||
// Author: Ahmed Hossam | ||
|
||
class Solution { | ||
public: | ||
long long maxScore(vector<int>& nums1, vector<int>& nums2, int k) { | ||
// Get the size of nums1 | ||
int n = nums1.size(); | ||
|
||
// Create a vector of indices from 0 to n - 1 | ||
vector < int > idx(n); | ||
iota(idx.begin(), idx.end(), 0); | ||
|
||
// Sort the indices based on the corresponding values in nums2 | ||
sort(idx.begin(), idx.end(), [&](int i, int j){ | ||
return nums2[i] < nums2[j]; | ||
}); | ||
|
||
// Create a min-heap priority queue | ||
priority_queue < int, vector < int >, greater < int > > pq; | ||
|
||
// Variables to keep track of the current sum and maximum sequence | ||
long long curr_sum = 0, max_seq = 0; | ||
|
||
// Lambda function to add an element to the current sum and the priority queue | ||
auto add = [&](int x){ | ||
curr_sum += x; | ||
pq.push(x); | ||
}; | ||
|
||
// Lambda function to remove the smallest element from the current sum and the priority queue | ||
auto remove = [&](){ | ||
curr_sum -= pq.top(); | ||
pq.pop(); | ||
}; | ||
|
||
// Iterate over the indices in reverse order | ||
for(int i = n - 1; i >= 0; i--){ | ||
// Add the corresponding element from nums1 to the current sum and the priority queue | ||
add(nums1[idx[i]]); | ||
|
||
// If the size of the priority queue reaches k, update the maximum sequence and remove the smallest element | ||
if(pq.size() == k){ | ||
max_seq = max(max_seq, curr_sum * nums2[idx[i]]); | ||
remove(); | ||
} | ||
} | ||
|
||
// Return the maximum sequence | ||
return max_seq; | ||
} | ||
}; |
52 changes: 52 additions & 0 deletions
52
05- May/24- Maximum Subsequence Score/24- Maximum Subsequence Score (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,52 @@ | ||
// Author: Mahmoud Aboelsoud | ||
|
||
class Solution { | ||
public: | ||
long long maxScore(vector<int>& nums1, vector<int>& nums2, int k) { | ||
// we need to find the maximum score subsequence of size of k | ||
// we caclulate the score by selecting k indexes adn calculate the sum of them from nums1 adn multiply the sum by the minimum value of the selected indexes from nums2 | ||
// we can use a greedy approach to select the k indexes | ||
// we can store the values of nums1 and nums2 in a vector of pairs and sort them in a decreasing order based on the values of nums2 | ||
// after sorting we will to make the second element of the pair as the minimum value of the selected indexes from nums2 | ||
// and multipy it by the smmaion of the max sum of k elements from the first element of the pair in indeces smaller than or equal to i (the selected pos of the min second element of the pair) | ||
|
||
|
||
// v: vector of pairs to store the values of nums1 and nums2 | ||
vector<pair<int,int>> v; | ||
|
||
// store the values of nums1 and nums2 in v | ||
for(int i = 0; i < nums1.size(); i++){ | ||
v.emplace_back(nums1[i], nums2[i]); | ||
} | ||
|
||
// sort v in a decreasing order based on the values of nums2 | ||
sort(v.begin(), v.end(), [](pair<int,int>&p1, pair<int,int>&p2){ | ||
if(p1.second == p2.second) return p1.first > p2.first; | ||
|
||
return p1.second > p2.second; | ||
}); | ||
|
||
// pq: priority queue to store the first element of the pair in a decreasing order | ||
priority_queue<int, vector<int>, greater<int>> pq; | ||
// sum: the sum of the max sum of k elements from the first element of the pair in indeces smaller than or equal to i (the selected pos of the min second element of the pair) | ||
// ans: the answer | ||
long long sum = 0, ans = 0; | ||
|
||
for(int i = 0; i < v.size(); i++){ | ||
// add the first element of the pair to the sum and push it to the priority queue | ||
sum += v[i].first; | ||
pq.push(v[i].first); | ||
|
||
// if the size of pq is equal to k, update the answer by the max of the current answer and the sum multiplied by the second element of the pair | ||
if(pq.size() == k){ | ||
ans = max(ans, sum * v[i].second); | ||
// subtract the top element of the priority queue from the sum | ||
sum -= pq.top(); | ||
pq.pop(); | ||
} | ||
} | ||
|
||
// return the answer | ||
return ans; | ||
} | ||
}; |
42 changes: 42 additions & 0 deletions
42
05- May/24- Maximum Subsequence Score/24- Maximum Subsequence Score (Mina Magdy).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: Mina Magdy | ||
|
||
class Solution { | ||
public: | ||
long long maxScore(vector<int>& nums1, vector<int>& nums2, int k) { | ||
int n = nums1.size(); // Number of elements in nums1 | ||
vector<int> idx(n); // Vector to store indices of nums1 | ||
iota(idx.begin(), idx.end(), 0); // Initialize idx with indices from 0 to n-1 | ||
sort(idx.begin(), idx.end(), [&](auto &a, auto &b) { | ||
return nums1[a] < nums1[b]; // Sort idx based on the values of nums1 in ascending order | ||
}); | ||
vector<pair<int, int>> vec; | ||
for (int k = 0; k < n; k++) { | ||
vec.emplace_back(nums2[idx[k]], k); // Create a vector of pairs (nums2 value, corresponding idx[k]) | ||
} | ||
sort(vec.begin(), vec.end()); // Sort vec in ascending order based on nums2 values | ||
int j = n - 1; // Pointer for iterating nums1 in reverse order | ||
int cnt = 0; // Counter for the number of elements chosen | ||
long long sum = 0, mx = 0; // sum: sum of chosen nums1 values, mx: maximum score | ||
set<int> removedIndices; // Set to keep track of removed indices | ||
for (int v = 0; v < n; v++) { | ||
auto &[vl, id] = vec[v]; // vl: nums2 value, id: corresponding idx[k] | ||
removedIndices.insert(idx[id]); // Add the corresponding idx to removedIndices set | ||
if (id > j) { | ||
cnt--; // Decrement the counter if id is greater than j | ||
sum -= nums1[idx[id]]; // Subtract the value at idx[id] from the sum | ||
} | ||
while (cnt < k - 1 && j >= 0) { | ||
if (removedIndices.count(idx[j])) { // Skip the index if it is already removed | ||
j--; | ||
continue; | ||
} | ||
cnt++; // Increment the counter | ||
sum += nums1[idx[j--]]; // Add the value at idx[j] to the sum and move the pointer j backwards | ||
} | ||
if (cnt == k - 1) { | ||
mx = max(mx, (sum + nums1[idx[id]]) * vl); // Calculate the score and update mx if it's greater | ||
} | ||
} | ||
return mx; // Return the maximum score | ||
} | ||
}; |
59 changes: 59 additions & 0 deletions
59
05- May/24- Maximum Subsequence Score/24- Maximum Subsequence Score (Noura Algohary).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,59 @@ | ||
// Author: Noura Algohary | ||
class Solution { | ||
public: | ||
void sort2Vectors(vector<int>& vec1, vector<int>& vec2, int n) | ||
{ | ||
pair<int, int> pairt[n]; | ||
|
||
// Storing the respective array | ||
// elements in pairs. | ||
for (int i = 0; i < n; i++) | ||
{ | ||
pairt[i].first = vec2[i]; | ||
pairt[i].second = vec1[i]; | ||
} | ||
|
||
// Sorting the pair array. | ||
sort(pairt, pairt + n); | ||
|
||
// Modifying original vectors | ||
for (int i = 0; i < n; i++) | ||
{ | ||
vec2[i] = pairt[i].first; | ||
vec1[i] = pairt[i].second; | ||
} | ||
} | ||
long long maxScore(vector<int>& nums1, vector<int>& nums2, int k) { | ||
priority_queue<int> pq; | ||
long long maxProduct = 0, subSum = 0; | ||
int n = nums2.size(); | ||
|
||
// sort two vectors according to the second one | ||
sort2Vectors(nums1, nums2, n); | ||
|
||
// start with the | ||
for(int i =n-1 ; i> n-k; i--) | ||
{ | ||
subSum += nums1[i]; | ||
// negative sign to store nums in ascending order | ||
pq.push(-nums1[i]); | ||
} | ||
|
||
for(int i = n-k; i>=0; i--) | ||
{ | ||
// remove the smallest num from sum | ||
subSum += nums1[i]; | ||
|
||
// remove the smallest num from priority queue | ||
pq.push(-nums1[i]); | ||
|
||
if(subSum * nums2[i] > maxProduct) | ||
maxProduct = subSum * nums2[i]; | ||
|
||
subSum += pq.top(); | ||
|
||
pq.pop(); | ||
} | ||
return maxProduct; | ||
} | ||
}; |
36 changes: 36 additions & 0 deletions
36
05- May/25- New 21 Game/25- New 21 Game (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,36 @@ | ||
// Author: Ahmed Hossam | ||
|
||
class Solution { | ||
public: | ||
double new21Game(int n, int k, int maxPts) { | ||
if (k == 0 || n >= k + maxPts) return 1.0; | ||
|
||
// Create a vector to store the probabilities. | ||
vector < double > dp(n + 1); | ||
|
||
// Initialize the current sum and the answer variables. | ||
double currSum = 1.0, ans = 0.0; | ||
|
||
// Set the initial probability for getting 0 points to 1.0. | ||
dp[0] = 1.0; | ||
|
||
// Calculate the probabilities for getting different points. | ||
for (int i = 1; i <= n; i++) { | ||
// Calculate the probability of getting i points. | ||
dp[i] = currSum / maxPts; | ||
|
||
// Update the current sum or the answer based on the value of i. | ||
// If i is less than k, update the current sum. | ||
// Otherwise, update the answer. | ||
(i < k ? currSum : ans) += dp[i]; | ||
|
||
// If i - maxPts is greater than or equal to 0, | ||
// subtract the probability of getting (i - maxPts) points from the current sum. | ||
if (i - maxPts >= 0) | ||
currSum -= dp[i - maxPts]; | ||
} | ||
|
||
// Return the final answer. | ||
return ans; | ||
} | ||
}; |
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,33 @@ | ||
// Author: Mina Magdy | ||
|
||
class Solution { | ||
public: | ||
// Function to calculate the winning probability in the New 21 Game | ||
double new21Game(int n, int k, int maxPts, int curr = 0) { | ||
// Base case: If k is 0, the game is already over, and the probability of winning is 1. | ||
if (k == 0) return 1; | ||
|
||
double dp[n + 1]; // Array to store the probabilities | ||
memset(dp, -1, sizeof(dp)); // Initialize the array with -1 | ||
|
||
double sum = 0; // Variable to store the sum of probabilities | ||
int r = 0; // Variable to store the right endpoint of the interval | ||
|
||
// Fill the probabilities for the rightmost interval [k, min(n, (k-1)+maxPts)] | ||
for (int i = k; i <= min(n, (k - 1) + maxPts); i++) { | ||
dp[i] = 1; // Set the winning probability to 1 for these values | ||
sum += dp[i]; // Update the sum | ||
r = i; // Update the right endpoint | ||
} | ||
|
||
// Calculate the probabilities for the remaining values in reverse order | ||
for (int i = k - 1; i >= 0; i--) { | ||
dp[i] = sum / maxPts; // Calculate the average of the next maxPts values | ||
if (r - i + 1 > maxPts) | ||
sum -= dp[r--]; // If the window size exceeds maxPts, remove the leftmost probability | ||
sum += dp[i]; // Add the current probability to the sum | ||
} | ||
|
||
return dp[0]; // Return the winning probability for starting with 0 points | ||
} | ||
}; |
41 changes: 41 additions & 0 deletions
41
05- May/26- Stone Game II/26- Stone Game II (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,41 @@ | ||
// Author: Ahmed Hossam | ||
|
||
class Solution { | ||
public: | ||
int stoneGameII(vector<int>& piles) { | ||
int n = piles.size(); | ||
|
||
// dp[i][m] represents the maximum number of stones the player can obtain | ||
// when starting at pile i with a maximum pick of m. | ||
vector < vector < int > > dp(n, vector < int > (2 * n + 5)); | ||
|
||
// sum[i] stores the sum of stones from pile i to the end. | ||
vector < int > sum(n + 5); | ||
|
||
// Calculate the sum of stones from each pile to the end. | ||
for (int i = n - 1; i >= 0; i--) | ||
sum[i] = piles[i] + sum[i + 1]; | ||
|
||
// Iterate over the piles from right to left. | ||
for (int i = n - 1; i >= 0; i--) { | ||
// Iterate over the maximum pick from 1 to n. | ||
for (int m = 1; m <= n; m++) { | ||
if (i + 2 * m >= n) | ||
// If there are not enough piles remaining, take all the stones. | ||
dp[i][m] = sum[i]; | ||
else { | ||
// Consider all possible picks from 1 to 2 * m. | ||
for (int x = 1; x <= 2 * m; x++) | ||
// Calculate the maximum stones the player can obtain | ||
// by either taking x stones and recursively solving for the remaining piles, | ||
// or not taking any stones and letting the other player play. | ||
dp[i][m] = max(dp[i][m], sum[i] - dp[i + x][max(m, x)]); | ||
} | ||
} | ||
} | ||
|
||
// The maximum number of stones the first player can obtain starting from the first pile | ||
// with a maximum pick of 1 is stored in dp[0][1]. | ||
return dp[0][1]; | ||
} | ||
}; |
Oops, something went wrong.