diff --git a/04- April/23- Restore The Array/23- Restore The Array (Mahmoud Aboelsoud).cpp b/04- April/23- Restore The Array/23- Restore The Array (Mahmoud Aboelsoud).cpp new file mode 100644 index 000000000..d050a7ada --- /dev/null +++ b/04- April/23- Restore The Array/23- Restore The Array (Mahmoud Aboelsoud).cpp @@ -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); + } +}; diff --git a/04- April/23- Restore The Array/23- Restore The Array (Mina Magdy).cpp b/04- April/23- Restore The Array/23- Restore The Array (Mina Magdy).cpp new file mode 100644 index 000000000..4f3673c13 --- /dev/null +++ b/04- April/23- Restore The Array/23- Restore The Array (Mina Magdy).cpp @@ -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> 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(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; + } +}; diff --git a/04- April/24- Last Stone Weight/24- Last Stone Weight (Ahmed Hossam).cpp b/04- April/24- Last Stone Weight/24- Last Stone Weight (Ahmed Hossam).cpp new file mode 100644 index 000000000..36b3b7e10 --- /dev/null +++ b/04- April/24- Last Stone Weight/24- Last Stone Weight (Ahmed Hossam).cpp @@ -0,0 +1,36 @@ +// Author: Ahmed Hossam + +class Solution { +public: + + int lastStoneWeight(vector& stones) { + //Creates a priority queue with integers + priority_queue 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()); + } +}; diff --git a/04- April/24- Last Stone Weight/24- Last Stone Weight (Ibrahim Khalid).cpp b/04- April/24- Last Stone Weight/24- Last Stone Weight (Ibrahim Khalid).cpp new file mode 100644 index 000000000..fb62114aa --- /dev/null +++ b/04- April/24- Last Stone Weight/24- Last Stone Weight (Ibrahim Khalid).cpp @@ -0,0 +1,40 @@ +// author : Ibrahim khalid +class Solution { +public: + int lastStoneWeight(vector& stones) { + // create priority_queue to store stones in the order + priority_queueq; + + 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; + + } +}; diff --git a/04- April/24- Last Stone Weight/24- Last Stone Weight (Mahmoud Aboelsoud).cpp b/04- April/24- Last Stone Weight/24- Last Stone Weight (Mahmoud Aboelsoud).cpp new file mode 100644 index 000000000..f0586e964 --- /dev/null +++ b/04- April/24- Last Stone Weight/24- Last Stone Weight (Mahmoud Aboelsoud).cpp @@ -0,0 +1,33 @@ +// Author: Mahmoud Aboelsoud + +class Solution { +public: + int lastStoneWeight(vector& 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 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()); + } +}; diff --git a/04- April/24- Last Stone Weight/24- Last Stone Weight (Osama Ayman).java b/04- April/24- Last Stone Weight/24- Last Stone Weight (Osama Ayman).java new file mode 100644 index 000000000..b3975f004 --- /dev/null +++ b/04- April/24- Last Stone Weight/24- Last Stone Weight (Osama Ayman).java @@ -0,0 +1,22 @@ +// Author: Osama Ayman +// Time & Space: O(n) +class Solution { + public int lastStoneWeight(int[] stones) { + // sorted descendingly + PriorityQueue 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(); + } +} \ No newline at end of file diff --git a/04- April/README.md b/04- April/README.md index b8b9068a8..a653315a6 100644 --- a/04- April/README.md +++ b/04- April/README.md @@ -46,6 +46,7 @@ 1. **[Profitable Schemes](#21--profitable-schemes)** 1. **[Minimum Insertion Steps to Make a String Palindrome](#22--minimum-insertion-steps-to-make-a-string-palindrome)** 1. **[Restore The Array](#23--restore-the-array)** +1. **[Last Stone Weight](#24--last-stone-weight)**


@@ -1345,4 +1346,57 @@ public: }; ``` + +
+

+ +## 24) [Last Stone Weight](https://leetcode.com/problems/last-stone-weight/) + +### Difficulty + +![](https://img.shields.io/badge/Easy-green?style=for-the-badge) + +### Related Topic + +`Array` `Heap (Priority Queue)` + +### Code + + +```cpp +class Solution { +public: + + int lastStoneWeight(vector& stones) { + //Creates a priority queue with integers + priority_queue 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()); + } +}; +``` \ No newline at end of file