From 384c07366c7aa0bafc1c1452bf0442c586ed9725 Mon Sep 17 00:00:00 2001 From: lamasalah32 Date: Sat, 20 May 2023 01:01:33 +0300 Subject: [PATCH 1/3] add 19- Is Graph Bipartite? (Lama Salah).cpp --- .../19- Is Graph Bipartite? (Lama Salah).cpp | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 05- May/19- Is Graph Bipartite?/19- Is Graph Bipartite? (Lama Salah).cpp diff --git a/05- May/19- Is Graph Bipartite?/19- Is Graph Bipartite? (Lama Salah).cpp b/05- May/19- Is Graph Bipartite?/19- Is Graph Bipartite? (Lama Salah).cpp new file mode 100644 index 000000000..beb259c0b --- /dev/null +++ b/05- May/19- Is Graph Bipartite?/19- Is Graph Bipartite? (Lama Salah).cpp @@ -0,0 +1,49 @@ +// Author: Lama Salah + +class Solution { +public: + vector> adj; // Adjacency list to represent the graph. + vector vis; // Array to keep track of node colors. + + // Recursive function to check if the graph is not bipartite. + bool isNotBipartite(int node, int color) { + vis[node] = color; // Assign the current node the specified color (0 or 1). + + bool ans = false; // Variable to store the result. + + for (auto& i : adj[node]) { + if (vis[i] == -1) { + // If the neighbor hasn't been visited, recursively call the function with the opposite color. + ans |= isNotBipartite(i, color ^ 1); + } else if ((color ^ 1) != vis[i]) { + // If the neighbor has been visited and has the same color as the current node, the graph is not bipartite. + return true; + } + } + + return ans; + } + + bool isBipartite(vector>& graph) { + adj.assign(graph.size(), vector()); // Initialize the adjacency list. + vis.assign(graph.size(), -1); // Initialize the vis array. + + // Build the adjacency list based on the given graph. + for (int i = 0; i < graph.size(); i++) { + for (auto& j : graph[i]) { + adj[i].push_back(j); + adj[j].push_back(i); + } + } + + // Iterate over each node in the graph. + for (int i = 0; i < graph.size(); i++) { + if (vis[i] == -1 && isNotBipartite(i, 0)) { + // If the node hasn't been visited and the graph is not bipartite, return false. + return false; + } + } + + return true; // The graph is bipartite. + } +}; \ No newline at end of file From 8de9b999ad907f5b22dad6b223e8bd9c16168385 Mon Sep 17 00:00:00 2001 From: lamasalah32 Date: Sun, 21 May 2023 23:21:42 +0300 Subject: [PATCH 2/3] add 21- Shortest Bridge (Lama Salah).cpp --- .../21- Shortest Bridge (Lama Salah).cpp | 105 ++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 05- May/21- Shortest Bridge/21- Shortest Bridge (Lama Salah).cpp diff --git a/05- May/21- Shortest Bridge/21- Shortest Bridge (Lama Salah).cpp b/05- May/21- Shortest Bridge/21- Shortest Bridge (Lama Salah).cpp new file mode 100644 index 000000000..3086c10f1 --- /dev/null +++ b/05- May/21- Shortest Bridge/21- Shortest Bridge (Lama Salah).cpp @@ -0,0 +1,105 @@ +// Author: Lama Salah + +/* + +-- THE IDEA -- + +Use a combination of depth-first search (DFS) and breadth-first search (BFS) algorithms. +The DFS function is used to mark the first island as visited and push its cells into the queue. +The BFS function then performs a breadth-first search to find the shortest distance from the first island to the second island. + +*/ + +class Solution { + int n; // Size of the grid. + + // Direction arrays for exploring neighboring cells (right, left, down, up). + int dx[4] = {0, 0, 1, -1}; + int dy[4] = {1, -1, 0, 0}; + + vector> grid; // Input grid + queue> q; // Queue for BFS + +public: + // 'valid' function to check if coordinates (x, y) are within the grid bounds. + bool valid(int x, int y) { + return x >= 0 && y >= 0 && x < n && y < n; + } + + void dfs(int i, int j) { + q.push({i, j}); // Push the current cell (i, j) into the queue. + grid[i][j] = -1; // Mark the current cell as visited (-1). + + // Explore all four neighboring cells. + for (int k = 0; k < 4; k++) { + // Calculate the new x-coordinate and y-coordinate of the neighboring cell. + int nx = i + dx[k]; + int ny = j + dy[k]; + + // Check if the neighboring cell is within bounds and unvisited. + if (valid(nx, ny) && grid[nx][ny] == 1) { + dfs(nx, ny); // Recursively call DFS on the neighboring cell. + } + } + } + + int bfs(int i, int j) { + // Initialize the distance between the two islands as 0. + int distance = 0; + + // Process cells in the queue until it becomes empty. + while (!q.empty()) { + int sz = q.size(); // Get the current number of cells in the queue. + while (sz--) { + // Get the front cell from the queue and pop it. + auto curr = q.front(); + q.pop(); + + // Explore all four neighboring cells. + for (int k = 0; k < 4; k++) { + // Calculate the new x-coordinate and y-coordinate of the neighboring cell. + int nx = curr[0] + dx[k]; + int ny = curr[1] + dy[k]; + + // Check if the neighboring cell is within bounds and unvisited. + if (valid(nx, ny) && grid[nx][ny] != -1) { + // Check if the neighboring cell is the second island (the second island is found). + // If it is, return the current distance. + if (grid[nx][ny] == 1) { + return distance; + } + + // Mark the neighboring cell as visited (-1). + grid[nx][ny] = -1; + // Push the neighboring cell into the queue. + q.push({nx, ny}); + } + } + } + + // Increment the distance after processing all cells in the current level. + distance++; + } + + // Return the shortest distance between the two islands. + return distance; + } + + int shortestBridge(vector>& g) { + this->grid = g; // Store the input grid. + this->n = grid.size(); // Get the size of the grid. + + // Iterate through the grid to find the first island. + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + // If a cell with value 1 is found (first island). + if (g[i][j] == 1) { + dfs(i, j); // Perform DFS to mark the first island as visited and push its cells into the queue. + return bfs(i, j); // Call BFS to find the shortest bridge between the two islands. + } + } + } + + return 0; // If no islands are found, return 0. + } +}; From 9537eefb2a20d3d832467d5e503302855c3db18f Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Mon, 22 May 2023 00:00:12 +0000 Subject: [PATCH 3/3] Add new daily problem --- .github/data/problems.json | 4 ++++ 05- May/22- Top K Frequent Elements/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/22- Top K Frequent Elements/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index b79aba910..d0e77ebf4 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -571,6 +571,10 @@ { "day": "21", "title": "Shortest Bridge" + }, + { + "day": "22", + "title": "Top K Frequent Elements" } ] } \ No newline at end of file diff --git a/05- May/22- Top K Frequent Elements/.gitkeep b/05- May/22- Top K Frequent Elements/.gitkeep new file mode 100644 index 000000000..e69de29bb