diff --git a/submissions/Vaishnavi_Mishra/Question1/Screenshot (116).png b/submissions/Vaishnavi_Mishra/Question1/Screenshot (116).png new file mode 100644 index 0000000..08d2bce Binary files /dev/null and b/submissions/Vaishnavi_Mishra/Question1/Screenshot (116).png differ diff --git a/submissions/Vaishnavi_Mishra/Question1/solution.cpp b/submissions/Vaishnavi_Mishra/Question1/solution.cpp new file mode 100644 index 0000000..c4994cc --- /dev/null +++ b/submissions/Vaishnavi_Mishra/Question1/solution.cpp @@ -0,0 +1,46 @@ +/*Problem 886: Possible Bipartition +*We want to split a group of n people (labeled from 1 to n) into two groups of any size. Each person may dislike some other people, and they should not go into the same group. +*Given the integer n and the array dislikes where dislikes[i] = [ai, bi] indicates that the person labeled ai does not like the person labeled bi, return true if it is possible to split everyone into two groups in this way. +*/ + +#include +#include +#include +#include + +class Solution { +public: + bool possibleBipartition(int n, std::vector>& dislikes) { + std::vector parent(n); + iota(parent.begin(), parent.end(), 0); + + std::unordered_map> graph; + for (const auto& edge : dislikes) { + int a = edge[0] - 1; + int b = edge[1] - 1; + graph[a].push_back(b); + graph[b].push_back(a); + } + + std::function find = [&](int node) -> int { + if (parent[node] != node) { + parent[node] = find(parent[node]); + } + return parent[node]; + }; + + for (int i = 0; i < n; ++i) { + if (!graph[i].empty()) { + int parentI = find(i); + int firstDislike = graph[i][0]; + for (int dislikeNode : graph[i]) { + if (find(dislikeNode) == parentI) return false; + + parent[find(dislikeNode)] = find(firstDislike); + } + } + } + + return true; + } +}; \ No newline at end of file diff --git a/submissions/Vaishnavi_Mishra/Question2/Screenshot (117).png b/submissions/Vaishnavi_Mishra/Question2/Screenshot (117).png new file mode 100644 index 0000000..aa6299e Binary files /dev/null and b/submissions/Vaishnavi_Mishra/Question2/Screenshot (117).png differ diff --git a/submissions/Vaishnavi_Mishra/Question2/solution.cpp b/submissions/Vaishnavi_Mishra/Question2/solution.cpp new file mode 100644 index 0000000..74cfa30 --- /dev/null +++ b/submissions/Vaishnavi_Mishra/Question2/solution.cpp @@ -0,0 +1,57 @@ +/*Problem 994: Rotting Oranges +*You are given an m x n grid where each cell can have one of three values: +*0 representing an empty cell, +*1 representing a fresh orange, or +*2 representing a rotten orange. +*Every minute, any fresh orange that is 4-directionally adjacent to a rotten orange becomes rotten. +*Return the minimum number of minutes that must elapse until no cell has a fresh orange. If this is impossible, return -1. +*/ + +#include +#include +using namespace std; + +class Solution { +public: + int orangesRotting(vector>& grid) { + int rows = grid.size(); + int cols = grid[0].size(); + int freshCount = 0; + queue> rottenQueue; + for (int i = 0; i < rows; ++i) { + for (int j = 0; j < cols; ++j) { + if (grid[i][j] == 2) { + rottenQueue.emplace(i, j); + } else if (grid[i][j] == 1) { + ++freshCount; + } + } + } + + int minutesElapsed = 0; + vector directions = {-1, 0, 1, 0, -1}; + + while (!rottenQueue.empty() && freshCount > 0) { + ++minutesElapsed; + for (int i = rottenQueue.size(); i > 0; --i) { + auto position = rottenQueue.front(); + rottenQueue.pop(); + for (int j = 0; j < 4; ++j) { + int newRow = position.first + directions[j]; + int newCol = position.second + directions[j + 1]; + if (newRow >= 0 && newRow < rows && newCol >= 0 && newCol < cols && grid[newRow][newCol] == 1) { + --freshCount; + grid[newRow][newCol] = 2; + rottenQueue.emplace(newRow, newCol); + } + } + } + } + + if (freshCount > 0) { + return -1; + } else { + return minutesElapsed; + } + } +}; \ No newline at end of file