From fe6fca27d3754295c71216a848b431ed2ee71c30 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Tue, 25 Apr 2023 00:38:54 +0000 Subject: [PATCH 01/12] 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 From dcd989643d4ffb1cc6cd5547f892e64e568b1d04 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Wed, 26 Apr 2023 00:35:42 +0000 Subject: [PATCH 02/12] Add new daily problem --- .github/data/problems.json | 4 ++++ 04- April/26- Add Digits/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 04- April/26- Add Digits/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index 2514d4950..1174ac2c5 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -465,6 +465,10 @@ { "day": "25", "title": "Smallest Number in Infinite Set" + }, + { + "day": "26", + "title": "Add Digits" } ] } \ No newline at end of file diff --git a/04- April/26- Add Digits/.gitkeep b/04- April/26- Add Digits/.gitkeep new file mode 100644 index 000000000..e69de29bb From 64577bd4ff30180b6d7652a28ce6da1404a18fd7 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Thu, 27 Apr 2023 00:39:26 +0000 Subject: [PATCH 03/12] Add new daily problem --- .github/data/problems.json | 4 ++++ 04- April/27- Bulb Switcher/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 04- April/27- Bulb Switcher/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index 1174ac2c5..a867e1ce9 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -469,6 +469,10 @@ { "day": "26", "title": "Add Digits" + }, + { + "day": "27", + "title": "Bulb Switcher" } ] } \ No newline at end of file diff --git a/04- April/27- Bulb Switcher/.gitkeep b/04- April/27- Bulb Switcher/.gitkeep new file mode 100644 index 000000000..e69de29bb From 26d2085f96f3f644721c20102463062c752e56d5 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Fri, 28 Apr 2023 00:43:40 +0000 Subject: [PATCH 04/12] Add new daily problem --- .github/data/problems.json | 4 ++++ 04- April/28- Similar String Groups/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 04- April/28- Similar String Groups/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index a867e1ce9..ada23e045 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -473,6 +473,10 @@ { "day": "27", "title": "Bulb Switcher" + }, + { + "day": "28", + "title": "Similar String Groups" } ] } \ No newline at end of file diff --git a/04- April/28- Similar String Groups/.gitkeep b/04- April/28- Similar String Groups/.gitkeep new file mode 100644 index 000000000..e69de29bb From 96394508c73bba05aa8bc3426a99e8040a15266f Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Sat, 29 Apr 2023 00:30:54 +0000 Subject: [PATCH 05/12] Add new daily problem --- .github/data/problems.json | 4 ++++ .../.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 04- April/29- Checking Existence of Edge Length Limited Paths/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index ada23e045..acff9a3db 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -477,6 +477,10 @@ { "day": "28", "title": "Similar String Groups" + }, + { + "day": "29", + "title": "Checking Existence of Edge Length Limited Paths" } ] } \ No newline at end of file diff --git a/04- April/29- Checking Existence of Edge Length Limited Paths/.gitkeep b/04- April/29- Checking Existence of Edge Length Limited Paths/.gitkeep new file mode 100644 index 000000000..e69de29bb From e5ba5618aefd783d8347af89de78ac73655bb4f5 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Sat, 29 Apr 2023 00:58:02 +0000 Subject: [PATCH 06/12] Add new daily problem --- .github/data/problems.json | 4 ++++ .../.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 04- April/29- Checking Existence of Edge Length Limited Paths/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index ada23e045..acff9a3db 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -477,6 +477,10 @@ { "day": "28", "title": "Similar String Groups" + }, + { + "day": "29", + "title": "Checking Existence of Edge Length Limited Paths" } ] } \ No newline at end of file diff --git a/04- April/29- Checking Existence of Edge Length Limited Paths/.gitkeep b/04- April/29- Checking Existence of Edge Length Limited Paths/.gitkeep new file mode 100644 index 000000000..e69de29bb From fcbf5c0cfd36ef9a4dd7984bf9401d902f0a4cfb Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Sun, 30 Apr 2023 00:32:10 +0000 Subject: [PATCH 07/12] Add new daily problem --- .github/data/problems.json | 4 ++++ .../.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 04- April/30- Remove Max Number of Edges to Keep Graph Fully Traversable/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index acff9a3db..95f087c63 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -481,6 +481,10 @@ { "day": "29", "title": "Checking Existence of Edge Length Limited Paths" + }, + { + "day": "30", + "title": "Remove Max Number of Edges to Keep Graph Fully Traversable" } ] } \ No newline at end of file diff --git a/04- April/30- Remove Max Number of Edges to Keep Graph Fully Traversable/.gitkeep b/04- April/30- Remove Max Number of Edges to Keep Graph Fully Traversable/.gitkeep new file mode 100644 index 000000000..e69de29bb From c6f4f0d112798f20397ac22963e790c1d6e924ca Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Sun, 30 Apr 2023 01:02:12 +0000 Subject: [PATCH 08/12] Add new daily problem --- .github/data/problems.json | 4 ++++ .../.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 04- April/30- Remove Max Number of Edges to Keep Graph Fully Traversable/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index acff9a3db..95f087c63 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -481,6 +481,10 @@ { "day": "29", "title": "Checking Existence of Edge Length Limited Paths" + }, + { + "day": "30", + "title": "Remove Max Number of Edges to Keep Graph Fully Traversable" } ] } \ No newline at end of file diff --git a/04- April/30- Remove Max Number of Edges to Keep Graph Fully Traversable/.gitkeep b/04- April/30- Remove Max Number of Edges to Keep Graph Fully Traversable/.gitkeep new file mode 100644 index 000000000..e69de29bb From ce5dbefae72bacda6e2d390ac9681f32c69758d5 Mon Sep 17 00:00:00 2001 From: aboelsoud Date: Sun, 30 Apr 2023 14:45:05 +0300 Subject: [PATCH 09/12] add the 30'th day problem solution --- ... Fully Traversable (Mahmoud Aboelsoud).cpp | 100 ++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 04- April/30- Remove Max Number of Edges to Keep Graph Fully Traversable/30- Remove Max Number of Edges to Keep Graph Fully Traversable (Mahmoud Aboelsoud).cpp diff --git a/04- April/30- Remove Max Number of Edges to Keep Graph Fully Traversable/30- Remove Max Number of Edges to Keep Graph Fully Traversable (Mahmoud Aboelsoud).cpp b/04- April/30- Remove Max Number of Edges to Keep Graph Fully Traversable/30- Remove Max Number of Edges to Keep Graph Fully Traversable (Mahmoud Aboelsoud).cpp new file mode 100644 index 000000000..2476c3a67 --- /dev/null +++ b/04- April/30- Remove Max Number of Edges to Keep Graph Fully Traversable/30- Remove Max Number of Edges to Keep Graph Fully Traversable (Mahmoud Aboelsoud).cpp @@ -0,0 +1,100 @@ +// Author: Mahmoud Aboelsoud + +class Solution { +public: + // we need to find the number of edges that we can remove to make the graph fully traversable + // which mean we need the minimum number of edges to connect to make the graph fully traversable by Alice and Bob + // we can use DSU to solve this problem + // we can make two graphs one for Alice and one for Bob + // we need to find the minimum number of edges used in both graphs to make each graph connected + // then we can remove the edges that are not used in both graphs + // we can use the third type of edges to connect the nodes in both graphs + // then we can check the needed number of edges to make both graphs connected from the other two types of edges + + + // DSU implementation + struct DSU{ + + vector parent, set_size; + + DSU(int n){ + parent.assign(n + 1, 0); + set_size.assign(n + 1, 1); + + for(int i = 1; i <= n; i++) + parent[i] = i; + } + + int find_set(int v){ + if(v == parent[v]) return v; + + return parent[v] = find_set(parent[v]); + } + + void union_sets(int a, int b){ + a = find_set(a); + b = find_set(b); + if(a != b){ + if(set_size[a] < set_size[b]) + swap(a, b); + parent[b] = a; + set_size[a] += set_size[b]; + } + } + + bool same_set(int a, int b){ + a = find_set(a); + b = find_set(b); + + return a == b; + } + }; + + + int maxNumEdgesToRemove(int n, vector>& edges) { + // we make 2 DSU one for Alice and one for Bob + DSU dsu1(n), dsu2(n); + + // cnt: number of edges that is required to make each of the 2 graphs connected + int cnt = 0; + + // we can use the third type of edges to connect the nodes in both graphs + for(auto&i: edges){ + // if the edge is of type 3 and the nodes are not connected in both graphs + if(i[0] == 3 && !dsu1.same_set(i[1], i[2])){ + // we can use this edge to connect the nodes in both graphs + // we increase the number of required edges by 1 + cnt++; + // we connect the nodes in both graphs + dsu1.union_sets(i[1], i[2]); + dsu2.union_sets(i[1], i[2]); + } + } + + // we check the needed number of edges to make both graphs connected from the other two types of edges + for(auto&i: edges){ + // if the edge is of type 1 and the nodes are not connected in Alice's graph + if(i[0] == 1 && !dsu1.same_set(i[1], i[2])){ + // we can use this edge to connect the nodes in Alice's graph + dsu1.union_sets(i[1], i[2]); + // we increase the number of required edges by 1 + cnt++; + } + // if the edge is of type 2 and the nodes are not connected in Bob's graph + else if(i[0] == 2 && !dsu2.same_set(i[1], i[2])){ + // we can use this edge to connect the nodes in Bob's graph + dsu2.union_sets(i[1], i[2]); + // we increase the number of required edges by 1 + cnt++; + } + } + + // then we check if all nodes in the given graph are connected in both graphs + // we do that by checking if the number of nodes in the set of the first node is equal to the number of nodes in the graph + // if not then we return -1 + if(dsu1.set_size[dsu1.find_set(1)] != n || dsu2.set_size[dsu2.find_set(1)] != n) return -1; + + // else we return the number of edges that we can remove to make the graph fully traversable + return edges.size() - cnt; + } +}; From 765b68594c43b0db4c90860722b9bb02627be8f4 Mon Sep 17 00:00:00 2001 From: Mina Magdy <71466151+MiinaMagdy@users.noreply.github.com> Date: Sun, 30 Apr 2023 14:13:08 +0200 Subject: [PATCH 10/12] Create 30- Remove Max Number of Edges to Keep Graph Fully Traversable (Mina Magdy).cpp add last month's problem --- ...p Graph Fully Traversable (Mina Magdy).cpp | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 04- April/30- Remove Max Number of Edges to Keep Graph Fully Traversable/30- Remove Max Number of Edges to Keep Graph Fully Traversable (Mina Magdy).cpp diff --git a/04- April/30- Remove Max Number of Edges to Keep Graph Fully Traversable/30- Remove Max Number of Edges to Keep Graph Fully Traversable (Mina Magdy).cpp b/04- April/30- Remove Max Number of Edges to Keep Graph Fully Traversable/30- Remove Max Number of Edges to Keep Graph Fully Traversable (Mina Magdy).cpp new file mode 100644 index 000000000..1070d195d --- /dev/null +++ b/04- April/30- Remove Max Number of Edges to Keep Graph Fully Traversable/30- Remove Max Number of Edges to Keep Graph Fully Traversable (Mina Magdy).cpp @@ -0,0 +1,54 @@ +// Author: Mina Magdy + +// create a structure for disjoint set union data structure +struct DSU { + int cc; // number of connected components + vector p, s; // vector for storing parent and size of connected components + DSU(int n) { // constructor + cc = n; // initially number of connected components are equal to the number of vertices + p.resize(n + 1); // resize vector to hold parent of each vertex + iota(p.begin(), p.end(), 0); // initialize the parent vector with indices + s.assign(n + 1, 1); // initially all connected components have size 1 + } + int find(int x) { // function to find the parent of a vertex and do path compression + return (x == p[x] ? x : p[x] = find(p[x])); + } + bool same_group(int u, int v) { // function to check if two vertices are in the same group or not + return find(u) == find(v); + } + void join(int u, int v) { // function to join two vertices into a single group + int r1 = find(u), r2 = find(v); + if (r1 == r2) // already in the same group + return; + if (s[r1] < s[r2]) // make smaller group as child of the larger group + swap(r1, r2); + p[r2] = r1; // make parent of r2 as r1 + s[r1] += s[r2]; // add size of r2 to size of r1 + cc--; // decrease the number of connected components + } +}; + +int maxNumEdgesToRemove(int n, vector>& edges) { + sort(edges.begin(), edges.end(), greater<>()); // sort the edges in descending order of type + DSU dsu1(n), dsu2(n); // create two instances of disjoint set union data structure + int cnt = 0; // count of number of edges added to graph + for (auto& v : edges) { // iterate through all edges + int t = 0; // type of edge added to graph + if (v[0] & 1) { // check if edge is of type 1 or 3 + if (dsu1.same_group(v[1], v[2])) // if vertices are already in the same group, skip + continue; + dsu1.join(v[1], v[2]); // join the vertices and decrease number of connected components + t |= 1; // set the type of edge + } + if (v[0] & 2) { // check if edge is of type 2 or 3 + if (dsu2.same_group(v[1], v[2])) // if vertices are already in the same group, skip + continue; + dsu2.join(v[1], v[2]); // join the vertices and decrease number of connected components + t |= 2; // set the type of edge + } + cnt += t > 0; // if an edge is added to the graph, increase the count + } + if (dsu1.cc != 1 || dsu2.cc != 1) // if the graph is not connected, return -1 + return -1; + return edges.size() - cnt; // return the number of edges removed from the graph +} From 1d423591e2bdfc4464adb95c523ad52769fb96d5 Mon Sep 17 00:00:00 2001 From: Omar Sanad <108091921+osanad3@users.noreply.github.com> Date: Sun, 30 Apr 2023 16:32:35 +0200 Subject: [PATCH 11/12] Create 30- Remove Max Number of Edges to Keep Graph Fully Traversable (Omar_Sanad).cpp --- ...p Graph Fully Traversable (Omar_Sanad).cpp | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 04- April/30- Remove Max Number of Edges to Keep Graph Fully Traversable/30- Remove Max Number of Edges to Keep Graph Fully Traversable (Omar_Sanad).cpp diff --git a/04- April/30- Remove Max Number of Edges to Keep Graph Fully Traversable/30- Remove Max Number of Edges to Keep Graph Fully Traversable (Omar_Sanad).cpp b/04- April/30- Remove Max Number of Edges to Keep Graph Fully Traversable/30- Remove Max Number of Edges to Keep Graph Fully Traversable (Omar_Sanad).cpp new file mode 100644 index 000000000..1bce67f7e --- /dev/null +++ b/04- April/30- Remove Max Number of Edges to Keep Graph Fully Traversable/30- Remove Max Number of Edges to Keep Graph Fully Traversable (Omar_Sanad).cpp @@ -0,0 +1,70 @@ +// author : Omar Sanad + +// DSU Structure +template < typename T = int > struct DSU { + vector < T > par, gr_sz; + T groups; + + // DSU constructor + DSU(int numNodes) { + groups = numNodes; + gr_sz.assign(numNodes + 1, 1), par.resize(numNodes + 1); + for(int i = 1; i <= numNodes; i++) par[i] = i; + } + + // operator = , to assign one dsu to another + DSU operator = (DSU& other) { + par = other.par; + gr_sz = other.gr_sz; + groups = other.groups; + return *this; + } + + T getRoot(int node) {return par[node] = (par[node] == node ? node : getRoot(par[node]));} + T get_size(int node) {return gr_sz[getRoot(node)];} + bool SameGr(int u, int v){return getRoot(u) == getRoot(v);} + + void combine(int a, int b) { + int root_of_a = getRoot(a), root_of_b = getRoot(b); + if(root_of_a == root_of_b) return; // They are already in the same group + if(gr_sz[root_of_a] < gr_sz[root_of_b]) swap(root_of_a, root_of_b); + gr_sz[root_of_a] += gr_sz[root_of_b], par[root_of_b] = root_of_a; + groups--; + } +}; + +class Solution { +public: + int maxNumEdgesToRemove(int n, vector>& edges) { + + // Declare the dsu that has the paths that both Alice and Bob can use + DSU du(n); + + // The number of used edges + int ans = 0; + + // use all edges of type 3 (both Alice and Bob can use), (the ones you need only) + for (auto &e : edges) + if (e.front() == 3 and not du.SameGr(e[1], e[2])) + ans++, du.combine(e[1], e[2]); + + + // Declare another two DSUs, one for Alice and the other one for Bob. + DSU Alice = du, Bob = du; + + // use the edges you need in the other two types + for (auto &e : edges) + if (e[0] == 1) { // Alice + if (not Alice.SameGr(e[1], e[2])) + ans++, Alice.combine(e[1], e[2]); + } + else if (e[0] == 2) { // Bob + if (not Bob.SameGr(e[1], e[2])) + ans++, Bob.combine(e[1], e[2]); + } + + // if Both Alice and Bob can fully traverse the graph + // return the number of deleted edges. + return Alice.groups == 1 and Bob.groups == 1 ? edges.size() - ans : -1; + } +}; From d7b3bb958c5266bfb9ab9f4d7a79cf6ea719b984 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Mon, 1 May 2023 01:39:24 +0000 Subject: [PATCH 12/12] Add new daily problem --- .github/data/problems.json | 6 ++++++ .../.gitkeep | 0 2 files changed, 6 insertions(+) create mode 100644 05- May/01- Average Salary Excluding the Minimum and Maximum Salary/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index 95f087c63..2c57e7c4d 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -486,5 +486,11 @@ "day": "30", "title": "Remove Max Number of Edges to Keep Graph Fully Traversable" } + ], + "05- May": [ + { + "day": "01", + "title": "Average Salary Excluding the Minimum and Maximum Salary" + } ] } \ No newline at end of file diff --git a/05- May/01- Average Salary Excluding the Minimum and Maximum Salary/.gitkeep b/05- May/01- Average Salary Excluding the Minimum and Maximum Salary/.gitkeep new file mode 100644 index 000000000..e69de29bb