From 840bdcbbeef74bf01b10551c057dfb6c399bc495 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Tue, 16 May 2023 00:25:29 +0000 Subject: [PATCH 01/17] Add new daily problem --- .github/data/problems.json | 4 ++++ 05- May/16- Swap Nodes in Pairs/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/16- Swap Nodes in Pairs/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index 52af9e62f..2464a5b42 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -547,6 +547,10 @@ { "day": "15", "title": "Swapping Nodes in a Linked List" + }, + { + "day": "16", + "title": "Swap Nodes in Pairs" } ] } \ No newline at end of file diff --git a/05- May/16- Swap Nodes in Pairs/.gitkeep b/05- May/16- Swap Nodes in Pairs/.gitkeep new file mode 100644 index 000000000..e69de29bb From eb7553b9d78b189897d838d4219c9c5823f9520a Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Tue, 16 May 2023 00:55:08 +0000 Subject: [PATCH 02/17] Add new daily problem --- .github/data/problems.json | 4 ++++ 05- May/16- Swap Nodes in Pairs/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/16- Swap Nodes in Pairs/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index 52af9e62f..2464a5b42 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -547,6 +547,10 @@ { "day": "15", "title": "Swapping Nodes in a Linked List" + }, + { + "day": "16", + "title": "Swap Nodes in Pairs" } ] } \ No newline at end of file diff --git a/05- May/16- Swap Nodes in Pairs/.gitkeep b/05- May/16- Swap Nodes in Pairs/.gitkeep new file mode 100644 index 000000000..e69de29bb From 05a103a0a680387e8b90f98c3b6a8019c646ffd5 Mon Sep 17 00:00:00 2001 From: Ibrahim Khaild <103538501+Hima-khalid@users.noreply.github.com> Date: Tue, 16 May 2023 03:57:19 +0300 Subject: [PATCH 03/17] Create 16- Swap Nodes in Pairs ( Ibrahim Khalid ).cpp --- ...Swap Nodes in Pairs ( Ibrahim Khalid ).cpp | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 05- May/16- Swap Nodes in Pairs/16- Swap Nodes in Pairs ( Ibrahim Khalid ).cpp diff --git a/05- May/16- Swap Nodes in Pairs/16- Swap Nodes in Pairs ( Ibrahim Khalid ).cpp b/05- May/16- Swap Nodes in Pairs/16- Swap Nodes in Pairs ( Ibrahim Khalid ).cpp new file mode 100644 index 000000000..46525a032 --- /dev/null +++ b/05- May/16- Swap Nodes in Pairs/16- Swap Nodes in Pairs ( Ibrahim Khalid ).cpp @@ -0,0 +1,32 @@ +// Author : Ibrahim Khalid +class Solution { +public: + ListNode* swapPairs(ListNode* head) { + // to store values + vectorv; + + ListNode* temp=head; + // move between values in linkedlist and store it in vector + while(temp!=NULL){ + v.push_back(temp->val); + temp=temp->next; + } + int n=v.size(); + // swap every two adjacent elements in vector + for(int i=0;ival=v[in]; + tmp=tmp->next; + in++; + } + + return head; + } +}; From 3a13371b25da95ecfc6a71d313478f2c072c98b9 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Tue, 16 May 2023 01:38:49 +0000 Subject: [PATCH 04/17] Add new daily problem --- .github/data/problems.json | 4 ++++ 05- May/16- Swap Nodes in Pairs/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/16- Swap Nodes in Pairs/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index 52af9e62f..2464a5b42 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -547,6 +547,10 @@ { "day": "15", "title": "Swapping Nodes in a Linked List" + }, + { + "day": "16", + "title": "Swap Nodes in Pairs" } ] } \ No newline at end of file diff --git a/05- May/16- Swap Nodes in Pairs/.gitkeep b/05- May/16- Swap Nodes in Pairs/.gitkeep new file mode 100644 index 000000000..e69de29bb From ceabcc7366ac1a7e78392fe2aa7cbe6a6c563808 Mon Sep 17 00:00:00 2001 From: Osama Ayman Date: Tue, 16 May 2023 14:07:11 +0300 Subject: [PATCH 05/17] Added my sol to 16 May --- ...16- Swap Nodes in Pairs (Osama Ayman).java | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 05- May/16- Swap Nodes in Pairs/16- Swap Nodes in Pairs (Osama Ayman).java diff --git a/05- May/16- Swap Nodes in Pairs/16- Swap Nodes in Pairs (Osama Ayman).java b/05- May/16- Swap Nodes in Pairs/16- Swap Nodes in Pairs (Osama Ayman).java new file mode 100644 index 000000000..708ef922f --- /dev/null +++ b/05- May/16- Swap Nodes in Pairs/16- Swap Nodes in Pairs (Osama Ayman).java @@ -0,0 +1,39 @@ +// Author: Osama Ayman +// Time: O(n) +// Space: O(1) +/** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode() {} + * ListNode(int val) { this.val = val; } + * ListNode(int val, ListNode next) { this.val = val; this.next = next; } + * } + */ +class Solution { + public ListNode swapPairs(ListNode head) { + // if there the list is empty of if there is only 1 node, return it + if(head==null || head.next == null) return head; + ListNode pp = null, prev = head, cur = head.next, next = null; + ListNode newHead = cur; + while(cur!=null){ + // pp is the last node from prev iteration + if(pp!=null) pp.next = cur; + // save the next, so we don't lose reference to it + next = cur.next; + // swap + cur.next = prev; + prev.next = next; + // advance the pp + pp = prev; + // advance the prev + prev = next; + // if there is no next node or only 1 node is left, break + if(next == null || next.next == null) break; + // advance the cur + cur = next.next; + } + return newHead; + } +} \ No newline at end of file From 10bd1c69958698461585930300accc137157832e Mon Sep 17 00:00:00 2001 From: Omar Sanad <108091921+OmarSanad3@users.noreply.github.com> Date: Tue, 16 May 2023 14:50:00 +0300 Subject: [PATCH 06/17] Added Sanad's Solution, May 16th --- .../16- Swap Nodes in Pairs (Omar Sanad).cpp | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 05- May/16- Swap Nodes in Pairs/16- Swap Nodes in Pairs (Omar Sanad).cpp diff --git a/05- May/16- Swap Nodes in Pairs/16- Swap Nodes in Pairs (Omar Sanad).cpp b/05- May/16- Swap Nodes in Pairs/16- Swap Nodes in Pairs (Omar Sanad).cpp new file mode 100644 index 000000000..88e65a3a2 --- /dev/null +++ b/05- May/16- Swap Nodes in Pairs/16- Swap Nodes in Pairs (Omar Sanad).cpp @@ -0,0 +1,62 @@ +// author : Omar Sanad + +/* + i j +[1]->[2]->[3]->[4]->[5]->[6] + +Suppose we have this list + +we have to swap i and j ... +how to do so? + +j->next = i +i->next = the first node of the remaining of the list after doing the swaps + + j i +[2]->[1]->[the first node of the remaining of the list after doing the swaps] + +in this step I will return j + + + */ +class Solution { +public: + + // recursion function + ListNode* rec(ListNode* i) { + + // if this node is null or if it's the last node in the list, + // then we cannot do any swaps, so we return + if (i == NULL or i->next == NULL) + return i; + + // we declare two nodes + // the node adjacent to i, (Which we will swap it with i) + ListNode *j = i->next; + // the first node of the remaining of the list + ListNode *to_go = j->next; + + // swap j and i + j->next = i; + + // mark the next to i as the first node of the remaining of the list after doing the swaps + i->next = rec(to_go); + + // return the node j, as now its directly before i + return j; + } + ListNode* swapPairs(ListNode* head) { + return rec(head); + } +}; + +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ From ffc8ccb97711511dd8ecf0d1d379d8adbbe67323 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Wed, 17 May 2023 00:35:31 +0000 Subject: [PATCH 07/17] Add new daily problem --- .github/data/problems.json | 4 ++++ 05- May/17- Maximum Twin Sum of a Linked List/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/17- Maximum Twin Sum of a Linked List/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index 2464a5b42..ec8dc0827 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -551,6 +551,10 @@ { "day": "16", "title": "Swap Nodes in Pairs" + }, + { + "day": "17", + "title": "Maximum Twin Sum of a Linked List" } ] } \ No newline at end of file diff --git a/05- May/17- Maximum Twin Sum of a Linked List/.gitkeep b/05- May/17- Maximum Twin Sum of a Linked List/.gitkeep new file mode 100644 index 000000000..e69de29bb From a532798805588e9faa1435ae94c300fb6136c8aa Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Wed, 17 May 2023 00:56:58 +0000 Subject: [PATCH 08/17] Add new daily problem --- .github/data/problems.json | 4 ++++ 05- May/17- Maximum Twin Sum of a Linked List/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/17- Maximum Twin Sum of a Linked List/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index 2464a5b42..ec8dc0827 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -551,6 +551,10 @@ { "day": "16", "title": "Swap Nodes in Pairs" + }, + { + "day": "17", + "title": "Maximum Twin Sum of a Linked List" } ] } \ No newline at end of file diff --git a/05- May/17- Maximum Twin Sum of a Linked List/.gitkeep b/05- May/17- Maximum Twin Sum of a Linked List/.gitkeep new file mode 100644 index 000000000..e69de29bb From 63477407a4be667c80d1e2ef05e7912fac22b038 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Wed, 17 May 2023 01:38:57 +0000 Subject: [PATCH 09/17] Add new daily problem --- .github/data/problems.json | 4 ++++ 05- May/17- Maximum Twin Sum of a Linked List/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/17- Maximum Twin Sum of a Linked List/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index 2464a5b42..ec8dc0827 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -551,6 +551,10 @@ { "day": "16", "title": "Swap Nodes in Pairs" + }, + { + "day": "17", + "title": "Maximum Twin Sum of a Linked List" } ] } \ No newline at end of file diff --git a/05- May/17- Maximum Twin Sum of a Linked List/.gitkeep b/05- May/17- Maximum Twin Sum of a Linked List/.gitkeep new file mode 100644 index 000000000..e69de29bb From 1661df9c04f014c6ba42f6d61de5c15385a8c899 Mon Sep 17 00:00:00 2001 From: Omar Sanad <108091921+OmarSanad3@users.noreply.github.com> Date: Wed, 17 May 2023 14:00:04 +0300 Subject: [PATCH 10/17] Added Sanad's Solution, May 17th --- ...Twin Sum of a Linked List (Omar Sanad).cpp | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 05- May/17- Maximum Twin Sum of a Linked List/17- Maximum Twin Sum of a Linked List (Omar Sanad).cpp diff --git a/05- May/17- Maximum Twin Sum of a Linked List/17- Maximum Twin Sum of a Linked List (Omar Sanad).cpp b/05- May/17- Maximum Twin Sum of a Linked List/17- Maximum Twin Sum of a Linked List (Omar Sanad).cpp new file mode 100644 index 000000000..95f5a4e46 --- /dev/null +++ b/05- May/17- Maximum Twin Sum of a Linked List/17- Maximum Twin Sum of a Linked List (Omar Sanad).cpp @@ -0,0 +1,47 @@ +// author : Omar Sanad + +class Solution { +public: + int pairSum(ListNode* head) { + // declare a stack to store the nodes in a reversed order + stack < ListNode* > st; + + // declare a variable cur as a pointer to iterate over the list + ListNode *cur = head; + + // iterate over the list and push the nodes in the stack + while (cur){ + st.push(cur); + cur = cur->next; + } + + // declare a variable to store the answer and initialize it to 0 + int maxSUM = 0; + + // declare a variable node as a pointer to iterate over the list + ListNode *node = head; + + // iterate over the list from the front using the variable node, + // and from the back using the stack + // and maximizing the answer + while (st.size()) { + maxSUM = max(maxSUM, node->val + st.top()->val); + st.pop(); + node = node->next; + } + + // return the answer + return maxSUM; + } +}; + +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ From 56a20b710c48cf4c5d93dbf8fa584965b2931bdb Mon Sep 17 00:00:00 2001 From: Osama Ayman Date: Wed, 17 May 2023 15:11:37 +0300 Subject: [PATCH 11/17] Added my sol to 17 May --- ...in Sum of a Linked List (Osama Ayman).java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 05- May/17- Maximum Twin Sum of a Linked List/17- Maximum Twin Sum of a Linked List (Osama Ayman).java diff --git a/05- May/17- Maximum Twin Sum of a Linked List/17- Maximum Twin Sum of a Linked List (Osama Ayman).java b/05- May/17- Maximum Twin Sum of a Linked List/17- Maximum Twin Sum of a Linked List (Osama Ayman).java new file mode 100644 index 000000000..1e72cf682 --- /dev/null +++ b/05- May/17- Maximum Twin Sum of a Linked List/17- Maximum Twin Sum of a Linked List (Osama Ayman).java @@ -0,0 +1,30 @@ +// Author: Osama Ayman +// Time: O(n) +// Space: O(n) +/** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode() {} + * ListNode(int val) { this.val = val; } + * ListNode(int val, ListNode next) { this.val = val; this.next = next; } + * } + */ +class Solution { + public int pairSum(ListNode head) { + // convert to array list, so we can use the 2-pointers technique + List ls = new ArrayList<>(); + ListNode cur = head; + while(cur!=null){ + ls.add(cur.val); + cur = cur.next; + } + // 2-ptrs + int lo=0, hi=ls.size()-1, max = 0; + while(lo Date: Wed, 17 May 2023 18:47:14 +0300 Subject: [PATCH 12/17] add 17- Maximum Twin Sum of a Linked List (Lama Salah).cpp --- ...Twin Sum of a Linked List (Lama Salah).cpp | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 05- May/17- Maximum Twin Sum of a Linked List/17- Maximum Twin Sum of a Linked List (Lama Salah).cpp diff --git a/05- May/17- Maximum Twin Sum of a Linked List/17- Maximum Twin Sum of a Linked List (Lama Salah).cpp b/05- May/17- Maximum Twin Sum of a Linked List/17- Maximum Twin Sum of a Linked List (Lama Salah).cpp new file mode 100644 index 000000000..c34a0205a --- /dev/null +++ b/05- May/17- Maximum Twin Sum of a Linked List/17- Maximum Twin Sum of a Linked List (Lama Salah).cpp @@ -0,0 +1,36 @@ +// Author: Lama Salah + +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ + +class Solution { +public: + int pairSum(ListNode* head) { + // Create a vector to store the values of the linked list nodes. + vector v; + + // Iterate through the linked list and add each node's value to the vector. + while (head){ + v.emplace_back(head -> val); + head = head -> next; + } + + int l = 0, r = v.size()-1; // Initialize pointers l and r to the start and end of the vector. + int ans = 0; // Initialize a variable to store the maximum twin sum. + + while (l < r) + // Calculate the sum of current twin and update the maximum twin sum. + ans = max(ans, v[l++] + v[r--]); + + // Return the maximum twin sum. + return ans; + } +}; \ No newline at end of file From f5d1be14fc2d5ac995ef520d84decbcd711bc146 Mon Sep 17 00:00:00 2001 From: Ibrahim Khaild <103538501+Hima-khalid@users.noreply.github.com> Date: Wed, 17 May 2023 19:00:58 +0300 Subject: [PATCH 13/17] Create 17- Maximum Twin Sum of a Linked List ( Ibrahim Khalid ).cpp --- ...um of a Linked List ( Ibrahim Khalid ).cpp | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 05- May/17- Maximum Twin Sum of a Linked List/17- Maximum Twin Sum of a Linked List ( Ibrahim Khalid ).cpp diff --git a/05- May/17- Maximum Twin Sum of a Linked List/17- Maximum Twin Sum of a Linked List ( Ibrahim Khalid ).cpp b/05- May/17- Maximum Twin Sum of a Linked List/17- Maximum Twin Sum of a Linked List ( Ibrahim Khalid ).cpp new file mode 100644 index 000000000..000453c1d --- /dev/null +++ b/05- May/17- Maximum Twin Sum of a Linked List/17- Maximum Twin Sum of a Linked List ( Ibrahim Khalid ).cpp @@ -0,0 +1,25 @@ +// Author : Ibrahim Khalid +class Solution { +public: + int pairSum(ListNode* head) { + // store value + vectorv; + ListNode* temp=head; + while(temp!=NULL){ + v.push_back(temp->val); + temp=temp->next; + } + int n=v.size(); + // two pointers + int l=0,r=n-1; + // return the maximum twin sum of the linked list + int mx=0; + while(l Date: Thu, 18 May 2023 00:52:16 +0000 Subject: [PATCH 14/17] Add new daily problem --- .github/data/problems.json | 4 ++++ .../.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/18- Minimum Number of Vertices to Reach All Nodes/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index ec8dc0827..029a87496 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -555,6 +555,10 @@ { "day": "17", "title": "Maximum Twin Sum of a Linked List" + }, + { + "day": "18", + "title": "Minimum Number of Vertices to Reach All Nodes" } ] } \ No newline at end of file diff --git a/05- May/18- Minimum Number of Vertices to Reach All Nodes/.gitkeep b/05- May/18- Minimum Number of Vertices to Reach All Nodes/.gitkeep new file mode 100644 index 000000000..e69de29bb From 0ed6cf769cb1ad78069e65ae090f0e08fa0ec877 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Thu, 18 May 2023 01:37:39 +0000 Subject: [PATCH 15/17] Add new daily problem --- .github/data/problems.json | 4 ++++ .../.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/18- Minimum Number of Vertices to Reach All Nodes/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index ec8dc0827..029a87496 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -555,6 +555,10 @@ { "day": "17", "title": "Maximum Twin Sum of a Linked List" + }, + { + "day": "18", + "title": "Minimum Number of Vertices to Reach All Nodes" } ] } \ No newline at end of file diff --git a/05- May/18- Minimum Number of Vertices to Reach All Nodes/.gitkeep b/05- May/18- Minimum Number of Vertices to Reach All Nodes/.gitkeep new file mode 100644 index 000000000..e69de29bb From 17f688a0e2198616090caa73e1e191fd25da2040 Mon Sep 17 00:00:00 2001 From: aboelsooud Date: Thu, 18 May 2023 06:26:23 +0300 Subject: [PATCH 16/17] add the 18'th day problem solution --- ...to Reach All Nodes (Mahmoud Aboelsoud).cpp | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 05- May/18- Minimum Number of Vertices to Reach All Nodes/18- Minimum Number of Vertices to Reach All Nodes (Mahmoud Aboelsoud).cpp diff --git a/05- May/18- Minimum Number of Vertices to Reach All Nodes/18- Minimum Number of Vertices to Reach All Nodes (Mahmoud Aboelsoud).cpp b/05- May/18- Minimum Number of Vertices to Reach All Nodes/18- Minimum Number of Vertices to Reach All Nodes (Mahmoud Aboelsoud).cpp new file mode 100644 index 000000000..796674ec0 --- /dev/null +++ b/05- May/18- Minimum Number of Vertices to Reach All Nodes/18- Minimum Number of Vertices to Reach All Nodes (Mahmoud Aboelsoud).cpp @@ -0,0 +1,26 @@ +// Author: Mahmoud Aboelsoud + +class Solution { +public: + vector findSmallestSetOfVertices(int n, vector>& edges) { + // to find the minimum number of vertices to reach all nodes we need to find the nodes with zero in degree + // because we can reach all nodes from the nodes with zero in degree + // we can do that by counting the in degree of each node + // then loop over the nodes and add the nodes with zero in degree to the answer + + // in_degree: in_degree[i] -> the in degree of node i + // ans: the answer + vector in_degree(n), ans; + + // count the in degree of each node + for(auto&i: edges) in_degree[i[1]]++; + + // loop over the nodes and add the nodes with zero in degree to the answer + for(int i = 0; i < n; i++){ + if(!in_degree[i]) ans.emplace_back(i); + } + + // return the answer + return ans; + } +}; From f4b4dc5588ac99ec9b27c1729790141025b8c4d4 Mon Sep 17 00:00:00 2001 From: Ahmed Hossam <63050133+7oSkaaa@users.noreply.github.com> Date: Thu, 18 May 2023 18:48:13 +0300 Subject: [PATCH 17/17] Create 18- Minimum Number of Vertices to Reach All Nodes (Ahmed Hossam).cpp --- ...ices to Reach All Nodes (Ahmed Hossam).cpp | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 05- May/18- Minimum Number of Vertices to Reach All Nodes/18- Minimum Number of Vertices to Reach All Nodes (Ahmed Hossam).cpp diff --git a/05- May/18- Minimum Number of Vertices to Reach All Nodes/18- Minimum Number of Vertices to Reach All Nodes (Ahmed Hossam).cpp b/05- May/18- Minimum Number of Vertices to Reach All Nodes/18- Minimum Number of Vertices to Reach All Nodes (Ahmed Hossam).cpp new file mode 100644 index 000000000..04654a0ef --- /dev/null +++ b/05- May/18- Minimum Number of Vertices to Reach All Nodes/18- Minimum Number of Vertices to Reach All Nodes (Ahmed Hossam).cpp @@ -0,0 +1,29 @@ +// Author: Ahmed Hossam + +class Solution { +public: + vector findSmallestSetOfVertices(int n, vector>& edges) { + // Create two arrays to store the count of outgoing and incoming edges for each vertex + vector from(n), to(n); + + // Count the number of outgoing and incoming edges for each vertex + for (auto& vec : edges) { + from[vec[0]]++; // Increment the outgoing edge count for the source vertex + to[vec[1]]++; // Increment the incoming edge count for the destination vertex + } + + // Create a vector to store the result + vector < int > res; + + // Iterate over all vertices + for (int i = 0; i < n; i++) { + // Check if the vertex has outgoing edges but no incoming edges + if (from[i] && !to[i]) + res.push_back(i); // Add the vertex to the result + } + + // Return the result vector + return res; + } + +};