Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 46 additions & 0 deletions submissions/Vaishnavi_Mishra/Question1/solution.cpp
Original file line number Diff line number Diff line change
@@ -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 <vector>
#include <numeric>
#include <unordered_map>
#include <functional>

class Solution {
public:
bool possibleBipartition(int n, std::vector<std::vector<int>>& dislikes) {
std::vector<int> parent(n);
iota(parent.begin(), parent.end(), 0);

std::unordered_map<int, std::vector<int>> 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<int(int)> 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;
}
};
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
57 changes: 57 additions & 0 deletions submissions/Vaishnavi_Mishra/Question2/solution.cpp
Original file line number Diff line number Diff line change
@@ -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 <vector>
#include <queue>
using namespace std;

class Solution {
public:
int orangesRotting(vector<vector<int>>& grid) {
int rows = grid.size();
int cols = grid[0].size();
int freshCount = 0;
queue<pair<int, int>> 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<int> 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;
}
}
};