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
7 changed files
with
285 additions
and
0 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
04- April/23- Restore The Array/23- Restore The Array (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,56 @@ | ||
// Author: Mahmoud Aboelsoud | ||
|
||
class Solution { | ||
public: | ||
// we need to count the number of ways to split the string into numbers <= k and doesn't start with 0 | ||
// we can use dp to solve this problem | ||
// dp[i] = number of ways to split the string starting from index i | ||
|
||
// s: input string | ||
// k: maximum value of the numbers | ||
string s; | ||
int dp[100005], k, mod = 1e9 + 7; | ||
|
||
// cnt_ways(idx) = number of ways to split the string starting from index idx | ||
int cnt_ways(int idx){ | ||
// base case if we reach the end of the string we return 1 | ||
if(idx == s.size()) return 1; | ||
|
||
// if the string starts with 0 we return 0 | ||
if(s[idx] == '0') return 0; | ||
|
||
// if we already calculated the number of ways to split the string starting from index idx we return it | ||
if(dp[idx] != -1) return dp[idx]; | ||
|
||
// ans: number of ways to split the string starting from index idx | ||
// x: the number we are currently trying to split the string from | ||
int ans = 0; | ||
long long x = 0; | ||
// we try to split the string starting from index idx to all possible numbers | ||
for(int i = idx; i < s.size(); i++){ | ||
// we add the current digit to the number we are trying to split the string from | ||
x = x * 10 + (s[i] - '0'); | ||
// if the number is greater than k we break | ||
if(x <= k){ | ||
// we add the number of ways to split the string starting from index i + 1 to the answer | ||
ans += cnt_ways(i + 1); | ||
ans %= mod; | ||
}else break; | ||
} | ||
|
||
// we return the answer | ||
return dp[idx] = ans; | ||
} | ||
|
||
|
||
int numberOfArrays(string s, int k) { | ||
// we initialize the string and k | ||
this -> s = s; | ||
this -> k = k; | ||
// we initialize the dp array with -1 | ||
memset(dp, -1, sizeof(dp)); | ||
|
||
// we return the number of ways to split the string starting from index 0 | ||
return cnt_ways(0); | ||
} | ||
}; |
44 changes: 44 additions & 0 deletions
44
04- April/23- Restore The Array/23- Restore The Array (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,44 @@ | ||
// Author: Mina Magdy | ||
|
||
// Define a long long integer variable named "ll" | ||
#define ll long long | ||
// Define a constant integer variable named "MOD" with a value of 1e9+7 | ||
#define MOD 1e9 + 7 | ||
|
||
// Define a class named "Solution" | ||
class Solution { | ||
public: | ||
// Define a 2D vector "dp" to store intermediate values for dynamic programming | ||
vector<vector<int>> dp; | ||
// Constructor of the "Solution" class | ||
Solution() { | ||
// Declare two integer constants named "N" and "M" and assign them values | ||
const int N = 1e5 + 5; | ||
const int M = 12; | ||
// Initialize the 2D vector "dp" with N rows and M columns with -1 | ||
dp.assign(N, vector<int>(M, -1)); | ||
} | ||
// A function named "numberOfArrays" with three parameters: a reference to a string, an integer, and two integer variables | ||
int numberOfArrays(string &s, int k, int idx = 0, int cnt = 0, ll sum = 0) { | ||
// Add the current digit to the "sum" | ||
sum = sum * 10 + s[idx] - '0'; | ||
// Increment the "cnt" variable | ||
cnt++; | ||
// If the "sum" is greater than "k" or equal to zero, return 0 | ||
if (sum > k || sum == 0) return 0; | ||
// If the index is equal to the size of the string minus one, return 1 | ||
if (idx == s.size() - 1) return 1; | ||
// Declare an integer reference variable "ret" and assign it the value of the 2D vector "dp" at position (idx, cnt) | ||
int &ret = dp[idx][cnt]; | ||
// If the "ret" value is not equal to -1, return "ret" | ||
if (~ret) return ret; | ||
// Recursively call the "numberOfArrays" function to compute the number of arrays without including the current digit | ||
ret = numberOfArrays(s, k, idx + 1, 0, 0); | ||
// Recursively call the "numberOfArrays" function to compute the number of arrays including the current digit | ||
ret += numberOfArrays(s, k, idx + 1, cnt, sum); | ||
// If "ret" is greater than or equal to "MOD", subtract "MOD" from "ret" | ||
if (ret >= MOD) ret -= MOD; | ||
// Return "ret" | ||
return ret; | ||
} | ||
}; |
36 changes: 36 additions & 0 deletions
36
04- April/24- Last Stone Weight/24- Last Stone Weight (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: | ||
|
||
int lastStoneWeight(vector<int>& stones) { | ||
//Creates a priority queue with integers | ||
priority_queue<int> pq; | ||
|
||
//Inserts each stone in the priority queue | ||
for(auto& stone : stones) | ||
pq.push(stone); | ||
|
||
//Lambda function to get the top 2 elements from the priority queue and remove them | ||
auto get_top = [&](){ | ||
int x = pq.top(); | ||
pq.pop(); | ||
int y = pq.top(); | ||
pq.pop(); | ||
return make_pair(x, y); | ||
}; | ||
|
||
//Loops while there are more than 1 stone in the priority queue | ||
while(pq.size() > 1){ | ||
//Gets the top 2 stones and removes them from the priority queue | ||
auto [x, y] = get_top(); | ||
|
||
//If the 2 stones are different, it inserts the difference into the priority queue | ||
if(x != y) | ||
pq.push(x - y); | ||
} | ||
|
||
//Returns the weight of the last stone or 0 if the priority queue is empty | ||
return (pq.empty() ? 0 : pq.top()); | ||
} | ||
}; |
40 changes: 40 additions & 0 deletions
40
04- April/24- Last Stone Weight/24- Last Stone Weight (Ibrahim Khalid).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,40 @@ | ||
// author : Ibrahim khalid | ||
class Solution { | ||
public: | ||
int lastStoneWeight(vector<int>& stones) { | ||
// create priority_queue to store stones in the order | ||
priority_queue<int>q; | ||
|
||
for(auto i:stones){ | ||
q.push(i); | ||
} | ||
|
||
int x,y; | ||
|
||
while(!q.empty()){ | ||
// if size ==1 print the single element | ||
if(q.size()==1){ | ||
return q.top(); | ||
} | ||
// x assin to first greater element | ||
x=q.top(); | ||
q.pop(); | ||
// y assin to second greater element | ||
y=q.top(); | ||
q.pop(); | ||
|
||
if(x==y){ | ||
// because you delete them (x,y) | ||
continue; | ||
} | ||
else{ | ||
// store The difference between them | ||
q.push(x-y); | ||
} | ||
|
||
} | ||
// if queue is empty and hasn't single element | ||
return 0; | ||
|
||
} | ||
}; |
33 changes: 33 additions & 0 deletions
33
04- April/24- Last Stone Weight/24- Last Stone Weight (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,33 @@ | ||
// Author: Mahmoud Aboelsoud | ||
|
||
class Solution { | ||
public: | ||
int lastStoneWeight(vector<int>& stones) { | ||
// we need to find the last remaining stone weight | ||
// we can do this by using a max heap (priority queue) | ||
// we will push all the stones into the priority queue | ||
// then we will pop the two heaviest stones and check if they are equal | ||
// if they are equal we will do nothing | ||
// if they are not equal we will push the difference of them into the priority queue | ||
// we will repeat this process until we have at most one stone left | ||
|
||
// pq: priority queue | ||
priority_queue<int> pq; | ||
|
||
// push all the stones into the priority queue | ||
for(auto&i: stones) pq.push(i); | ||
|
||
// repeat the process until we have at most one stone left | ||
while(pq.size() > 1){ | ||
// pop the two heaviest stones | ||
int x = pq.top(); pq.pop(); | ||
int y = pq.top(); pq.pop(); | ||
|
||
// check if they are not equal and push the difference of them into the priority queue | ||
if(x != y) pq.push(x - y); | ||
} | ||
|
||
// if the priority queue is empty then return 0 otherwise return the last remaining stone weight | ||
return (pq.empty() ? 0 : pq.top()); | ||
} | ||
}; |
22 changes: 22 additions & 0 deletions
22
04- April/24- Last Stone Weight/24- Last Stone Weight (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,22 @@ | ||
// Author: Osama Ayman | ||
// Time & Space: O(n) | ||
class Solution { | ||
public int lastStoneWeight(int[] stones) { | ||
// sorted descendingly | ||
PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder()); | ||
for(int x: stones){ | ||
pq.add(x); | ||
} | ||
while(pq.size() > 1){ | ||
// get the heaviest two stones | ||
int y = pq.poll(); | ||
int x = pq.poll(); | ||
if(x!=y){ | ||
pq.add(y-x); | ||
} | ||
// if x & y are equal do not add anything to the queue. | ||
} | ||
// if empty return 0, otherwise return the element | ||
return pq.isEmpty() ? 0:pq.poll(); | ||
} | ||
} |
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