From b13ceccfb8f5549dcf8885faa0543ff8b4eb52c3 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Mon, 10 Apr 2023 00:29:13 +0000 Subject: [PATCH 01/27] Add new daily problem --- .github/data/problems.json | 4 ++++ 04- April/10- Valid Parentheses/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 04- April/10- Valid Parentheses/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index 39f3dc096..cedda9375 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -401,6 +401,10 @@ { "day": "09", "title": "Largest Color Value in a Directed Graph" + }, + { + "day": "10", + "title": "Valid Parentheses" } ] } \ No newline at end of file diff --git a/04- April/10- Valid Parentheses/.gitkeep b/04- April/10- Valid Parentheses/.gitkeep new file mode 100644 index 000000000..e69de29bb From 83e05f6fa8ac75abb6c71f8447c9e788dd557a63 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Tue, 11 Apr 2023 00:36:36 +0000 Subject: [PATCH 02/27] Add new daily problem --- .github/data/problems.json | 4 ++++ 04- April/11- Removing Stars From a String/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 04- April/11- Removing Stars From a String/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index cedda9375..8428a4643 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -405,6 +405,10 @@ { "day": "10", "title": "Valid Parentheses" + }, + { + "day": "11", + "title": "Removing Stars From a String" } ] } \ No newline at end of file diff --git a/04- April/11- Removing Stars From a String/.gitkeep b/04- April/11- Removing Stars From a String/.gitkeep new file mode 100644 index 000000000..e69de29bb From eb4e23853b5be412ea86a9d0d1d8847a16f79638 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Wed, 12 Apr 2023 00:32:31 +0000 Subject: [PATCH 03/27] Add new daily problem --- .github/data/problems.json | 4 ++++ 04- April/12- Simplify Path/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 04- April/12- Simplify Path/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index 8428a4643..cc047b5e9 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -409,6 +409,10 @@ { "day": "11", "title": "Removing Stars From a String" + }, + { + "day": "12", + "title": "Simplify Path" } ] } \ No newline at end of file diff --git a/04- April/12- Simplify Path/.gitkeep b/04- April/12- Simplify Path/.gitkeep new file mode 100644 index 000000000..e69de29bb From 844dc585ca1c18fd5f1b6cb3c8db24e65322baea Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Thu, 13 Apr 2023 00:33:37 +0000 Subject: [PATCH 04/27] Add new daily problem --- .github/data/problems.json | 4 ++++ 04- April/13- Validate Stack Sequences/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 04- April/13- Validate Stack Sequences/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index cc047b5e9..d1adac8cc 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -413,6 +413,10 @@ { "day": "12", "title": "Simplify Path" + }, + { + "day": "13", + "title": "Validate Stack Sequences" } ] } \ No newline at end of file diff --git a/04- April/13- Validate Stack Sequences/.gitkeep b/04- April/13- Validate Stack Sequences/.gitkeep new file mode 100644 index 000000000..e69de29bb From a3e7a079a5fb4bf8bf7b37210997e88225ca37e7 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Fri, 14 Apr 2023 00:35:03 +0000 Subject: [PATCH 05/27] Add new daily problem --- .github/data/problems.json | 4 ++++ 04- April/14- Longest Palindromic Subsequence/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 04- April/14- Longest Palindromic Subsequence/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index d1adac8cc..cfe997942 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -417,6 +417,10 @@ { "day": "13", "title": "Validate Stack Sequences" + }, + { + "day": "14", + "title": "Longest Palindromic Subsequence" } ] } \ No newline at end of file diff --git a/04- April/14- Longest Palindromic Subsequence/.gitkeep b/04- April/14- Longest Palindromic Subsequence/.gitkeep new file mode 100644 index 000000000..e69de29bb From 581125f71aa90423d67bd26cd34aa16839b06c65 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Sat, 15 Apr 2023 00:32:59 +0000 Subject: [PATCH 06/27] Add new daily problem --- .github/data/problems.json | 4 ++++ 04- April/15- Maximum Value of K Coins From Piles/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 04- April/15- Maximum Value of K Coins From Piles/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index cfe997942..6b1f35006 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -421,6 +421,10 @@ { "day": "14", "title": "Longest Palindromic Subsequence" + }, + { + "day": "15", + "title": "Maximum Value of K Coins From Piles" } ] } \ No newline at end of file diff --git a/04- April/15- Maximum Value of K Coins From Piles/.gitkeep b/04- April/15- Maximum Value of K Coins From Piles/.gitkeep new file mode 100644 index 000000000..e69de29bb From d7d87a1f66d6bd21f29537a966dcd6bf944db223 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Sun, 16 Apr 2023 00:33:18 +0000 Subject: [PATCH 07/27] Add new daily problem --- .github/data/problems.json | 4 ++++ .../.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 04- April/16- Number of Ways to Form a Target String Given a Dictionary/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index 6b1f35006..fbfe5ad13 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -425,6 +425,10 @@ { "day": "15", "title": "Maximum Value of K Coins From Piles" + }, + { + "day": "16", + "title": "Number of Ways to Form a Target String Given a Dictionary" } ] } \ No newline at end of file diff --git a/04- April/16- Number of Ways to Form a Target String Given a Dictionary/.gitkeep b/04- April/16- Number of Ways to Form a Target String Given a Dictionary/.gitkeep new file mode 100644 index 000000000..e69de29bb From 505456c845e9a0a8489290cfd79a3ca013cccb4a Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Mon, 17 Apr 2023 00:30:54 +0000 Subject: [PATCH 08/27] Add new daily problem --- .github/data/problems.json | 4 ++++ .../17- Kids With the Greatest Number of Candies/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 04- April/17- Kids With the Greatest Number of Candies/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index fbfe5ad13..7cc53c103 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -429,6 +429,10 @@ { "day": "16", "title": "Number of Ways to Form a Target String Given a Dictionary" + }, + { + "day": "17", + "title": "Kids With the Greatest Number of Candies" } ] } \ No newline at end of file diff --git a/04- April/17- Kids With the Greatest Number of Candies/.gitkeep b/04- April/17- Kids With the Greatest Number of Candies/.gitkeep new file mode 100644 index 000000000..e69de29bb From dffc0561539bca4c7155de0e873aa5363b9333d9 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Tue, 18 Apr 2023 00:34:04 +0000 Subject: [PATCH 09/27] Add new daily problem --- .github/data/problems.json | 4 ++++ 04- April/18- Merge Strings Alternately/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 04- April/18- Merge Strings Alternately/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index 7cc53c103..c51ee9860 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -433,6 +433,10 @@ { "day": "17", "title": "Kids With the Greatest Number of Candies" + }, + { + "day": "18", + "title": "Merge Strings Alternately" } ] } \ No newline at end of file diff --git a/04- April/18- Merge Strings Alternately/.gitkeep b/04- April/18- Merge Strings Alternately/.gitkeep new file mode 100644 index 000000000..e69de29bb From 272d3b994cc2b370040596918818e172b2b9bf5a Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Wed, 19 Apr 2023 00:39:01 +0000 Subject: [PATCH 10/27] Add new daily problem --- .github/data/problems.json | 4 ++++ 04- April/19- Longest ZigZag Path in a Binary Tree/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 04- April/19- Longest ZigZag Path in a Binary Tree/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index c51ee9860..e4093c389 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -437,6 +437,10 @@ { "day": "18", "title": "Merge Strings Alternately" + }, + { + "day": "19", + "title": "Longest ZigZag Path in a Binary Tree" } ] } \ No newline at end of file diff --git a/04- April/19- Longest ZigZag Path in a Binary Tree/.gitkeep b/04- April/19- Longest ZigZag Path in a Binary Tree/.gitkeep new file mode 100644 index 000000000..e69de29bb From 8463c8ec2fef9f21947682d76268352102e1e0f7 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Thu, 20 Apr 2023 00:37:05 +0000 Subject: [PATCH 11/27] Add new daily problem --- .github/data/problems.json | 4 ++++ 04- April/20- Maximum Width of Binary Tree/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 04- April/20- Maximum Width of Binary Tree/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index e4093c389..1187d5bf9 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -441,6 +441,10 @@ { "day": "19", "title": "Longest ZigZag Path in a Binary Tree" + }, + { + "day": "20", + "title": "Maximum Width of Binary Tree" } ] } \ No newline at end of file diff --git a/04- April/20- Maximum Width of Binary Tree/.gitkeep b/04- April/20- Maximum Width of Binary Tree/.gitkeep new file mode 100644 index 000000000..e69de29bb From 904a67f1d5c146ade03c6de5636bac7b04ad129e Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Fri, 21 Apr 2023 00:36:30 +0000 Subject: [PATCH 12/27] Add new daily problem --- .github/data/problems.json | 4 ++++ 04- April/21- Profitable Schemes/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 04- April/21- Profitable Schemes/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index 1187d5bf9..4d8eb0687 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -445,6 +445,10 @@ { "day": "20", "title": "Maximum Width of Binary Tree" + }, + { + "day": "21", + "title": "Profitable Schemes" } ] } \ No newline at end of file diff --git a/04- April/21- Profitable Schemes/.gitkeep b/04- April/21- Profitable Schemes/.gitkeep new file mode 100644 index 000000000..e69de29bb From b92dda0a9c399b5c47992311c3df53400d6dc47a Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Sat, 22 Apr 2023 00:30:17 +0000 Subject: [PATCH 13/27] Add new daily problem --- .github/data/problems.json | 4 ++++ .../.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 04- April/22- Minimum Insertion Steps to Make a String Palindrome/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index 4d8eb0687..ba0270155 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -449,6 +449,10 @@ { "day": "21", "title": "Profitable Schemes" + }, + { + "day": "22", + "title": "Minimum Insertion Steps to Make a String Palindrome" } ] } \ No newline at end of file diff --git a/04- April/22- Minimum Insertion Steps to Make a String Palindrome/.gitkeep b/04- April/22- Minimum Insertion Steps to Make a String Palindrome/.gitkeep new file mode 100644 index 000000000..e69de29bb From 92a6d03ba04f73ee5b27856cb1c0aed9c043ef5b Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Sun, 23 Apr 2023 00:34:56 +0000 Subject: [PATCH 14/27] Add new daily problem --- .github/data/problems.json | 4 ++++ 04- April/23- Restore The Array/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 04- April/23- Restore The Array/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index ba0270155..6a4bf97cd 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -453,6 +453,10 @@ { "day": "22", "title": "Minimum Insertion Steps to Make a String Palindrome" + }, + { + "day": "23", + "title": "Restore The Array" } ] } \ No newline at end of file diff --git a/04- April/23- Restore The Array/.gitkeep b/04- April/23- Restore The Array/.gitkeep new file mode 100644 index 000000000..e69de29bb From 93cb7581ce9853f1e2932e7077a1d4aac8d89a7b Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Sun, 23 Apr 2023 00:58:38 +0000 Subject: [PATCH 15/27] Add new daily problem --- .github/data/problems.json | 4 ++++ 04- April/23- Restore The Array/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 04- April/23- Restore The Array/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index ba0270155..6a4bf97cd 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -453,6 +453,10 @@ { "day": "22", "title": "Minimum Insertion Steps to Make a String Palindrome" + }, + { + "day": "23", + "title": "Restore The Array" } ] } \ No newline at end of file diff --git a/04- April/23- Restore The Array/.gitkeep b/04- April/23- Restore The Array/.gitkeep new file mode 100644 index 000000000..e69de29bb From 6a8b4367ce32defa8464082e2a331616cdc861cf Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Sun, 23 Apr 2023 01:46:22 +0000 Subject: [PATCH 16/27] Add new daily problem --- .github/data/problems.json | 4 ++++ 04- April/23- Restore The Array/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 04- April/23- Restore The Array/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index ba0270155..6a4bf97cd 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -453,6 +453,10 @@ { "day": "22", "title": "Minimum Insertion Steps to Make a String Palindrome" + }, + { + "day": "23", + "title": "Restore The Array" } ] } \ No newline at end of file diff --git a/04- April/23- Restore The Array/.gitkeep b/04- April/23- Restore The Array/.gitkeep new file mode 100644 index 000000000..e69de29bb From 1b2f17b0fe00e0648fb738b1ba6852d96c5396e9 Mon Sep 17 00:00:00 2001 From: aboelsoud Date: Sun, 23 Apr 2023 15:23:27 +0200 Subject: [PATCH 17/27] add the 23'rd day problem solution --- ... Restore The Array (Mahmoud Aboelsoud).cpp | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 04- April/23- Restore The Array/23- Restore The Array (Mahmoud Aboelsoud).cpp 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); + } +}; From 1b5292a3bedfec49ae2a23b42b22d9731f1cb3fa Mon Sep 17 00:00:00 2001 From: Mina Magdy <71466151+MiinaMagdy@users.noreply.github.com> Date: Sun, 23 Apr 2023 16:08:06 +0200 Subject: [PATCH 18/27] Create 23- Restore The Array (Mina Magdy).cpp add 23-rd solution --- .../23- Restore The Array (Mina Magdy).cpp | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 04- April/23- Restore The Array/23- Restore The Array (Mina Magdy).cpp 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; + } +}; From 4061dfe131071a40bf9aa36e849529924f4b5e30 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Mon, 24 Apr 2023 00:33:11 +0000 Subject: [PATCH 19/27] Add new daily problem --- .github/data/problems.json | 4 ++++ 04- April/24- Last Stone Weight/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 04- April/24- Last Stone Weight/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index 6a4bf97cd..89687db92 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -457,6 +457,10 @@ { "day": "23", "title": "Restore The Array" + }, + { + "day": "24", + "title": "Last Stone Weight" } ] } \ No newline at end of file diff --git a/04- April/24- Last Stone Weight/.gitkeep b/04- April/24- Last Stone Weight/.gitkeep new file mode 100644 index 000000000..e69de29bb From aa251f91d7c94cf3b9e495eee46406ec0d08968e Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Mon, 24 Apr 2023 00:57:57 +0000 Subject: [PATCH 20/27] Add new daily problem --- .github/data/problems.json | 4 ++++ 04- April/24- Last Stone Weight/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 04- April/24- Last Stone Weight/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index 6a4bf97cd..89687db92 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -457,6 +457,10 @@ { "day": "23", "title": "Restore The Array" + }, + { + "day": "24", + "title": "Last Stone Weight" } ] } \ No newline at end of file diff --git a/04- April/24- Last Stone Weight/.gitkeep b/04- April/24- Last Stone Weight/.gitkeep new file mode 100644 index 000000000..e69de29bb From 1c74411c0def7b7c62bf2f627815da71c0f7fe2d Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Mon, 24 Apr 2023 01:38:36 +0000 Subject: [PATCH 21/27] Add new daily problem --- .github/data/problems.json | 4 ++++ 04- April/24- Last Stone Weight/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 04- April/24- Last Stone Weight/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index 6a4bf97cd..89687db92 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -457,6 +457,10 @@ { "day": "23", "title": "Restore The Array" + }, + { + "day": "24", + "title": "Last Stone Weight" } ] } \ No newline at end of file diff --git a/04- April/24- Last Stone Weight/.gitkeep b/04- April/24- Last Stone Weight/.gitkeep new file mode 100644 index 000000000..e69de29bb From 1b000406e809962791736c32d590cb9fb2f19f95 Mon Sep 17 00:00:00 2001 From: Ibrahim Khaild <103538501+Hima-khalid@users.noreply.github.com> Date: Mon, 24 Apr 2023 03:59:03 +0200 Subject: [PATCH 22/27] 24- Last Stone Weight (Ibrahim Khalid) --- ...24- Last Stone Weight (Ibrahim Khalid).cpp | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 04- April/24- Last Stone Weight/24- Last Stone Weight (Ibrahim Khalid).cpp 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; + + } +}; From 61349be6723b05a667dc4dd98aa21b6326b32908 Mon Sep 17 00:00:00 2001 From: Osama Ayman Date: Mon, 24 Apr 2023 13:00:44 +0200 Subject: [PATCH 23/27] Added my sol to 24 April --- .../24- Last Stone Weight (Osama Ayman).java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 04- April/24- Last Stone Weight/24- Last Stone Weight (Osama Ayman).java 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 From aebae9284ca7d313d39008212b2b98a061f13581 Mon Sep 17 00:00:00 2001 From: aboelsoud Date: Mon, 24 Apr 2023 14:39:33 +0200 Subject: [PATCH 24/27] add the 24'th day problem solution --- ... Last Stone Weight (Mahmoud Aboelsoud).cpp | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 04- April/24- Last Stone Weight/24- Last Stone Weight (Mahmoud Aboelsoud).cpp 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()); + } +}; From 78ca858f1dc7d956c9b527fda8099aeae4b29026 Mon Sep 17 00:00:00 2001 From: Ahmed Hossam <63050133+7oSkaaa@users.noreply.github.com> Date: Mon, 24 Apr 2023 16:02:02 +0200 Subject: [PATCH 25/27] Create 24- Last Stone Weight (Ahmed Hossam).cpp --- .../24- Last Stone Weight (Ahmed Hossam).cpp | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 04- April/24- Last Stone Weight/24- Last Stone Weight (Ahmed Hossam).cpp 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()); + } +}; From 6b3782769cd5cc78f3e5d2047646299d5c23ce26 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Mon, 24 Apr 2023 14:03:17 +0000 Subject: [PATCH 26/27] Add new daily problem to README --- 04- April/README.md | 54 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) 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 From 9ffe3d161b99e1305ca0b754f5a1d77a24d59a4e Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Tue, 25 Apr 2023 00:42:44 +0000 Subject: [PATCH 27/27] Add new daily problem --- .github/data/problems.json | 4 ++++ 04- April/25- Smallest Number in Infinite Set/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 04- April/25- Smallest Number in Infinite Set/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index 89687db92..2514d4950 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -461,6 +461,10 @@ { "day": "24", "title": "Last Stone Weight" + }, + { + "day": "25", + "title": "Smallest Number in Infinite Set" } ] } \ No newline at end of file diff --git a/04- April/25- Smallest Number in Infinite Set/.gitkeep b/04- April/25- Smallest Number in Infinite Set/.gitkeep new file mode 100644 index 000000000..e69de29bb