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
39 changes: 39 additions & 0 deletions EmployeeImportance.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
// Definition for Employee.
class Employee {
public:
int id;
int importance;
vector<int> subordinates;
};
*/

class Solution {
public:
int getImportance(vector<Employee*> employees, int id) {
unordered_map<int, vector<int>> idsMap;
unordered_map<int, Employee*> usEmp;
vector<int> inorder;
for (int i=0;i<employees.size();i++) {
idsMap[employees[i]->id] = employees[i]->subordinates;
usEmp[employees[i]->id] = employees[i];
}

queue<int> q;
q.push(id);
int answer = usEmp[id]->importance;

while (!q.empty()) {
auto node = q.front();
q.pop();

vector<int> direct = idsMap[node];
for (int i=0;i<direct.size();i++) {
answer += usEmp[direct[i]]->importance;
q.push(direct[i]);
}
}

return answer;
}
};
62 changes: 62 additions & 0 deletions RottingOranges.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
class Solution {
public:
vector<vector<int>> dirs = {{1,0}, {-1,0}, {0,1}, {0,-1}};
int orangesRotting(vector<vector<int>>& grid) {
queue<pair<int ,int>> q;
unordered_set<string> us, freshO;

auto key = [&](int x, int y) {
return to_string(x)+"+"+to_string(y);
};

auto markVisited = [&](int x, int y) {
string str = key(x, y);
us.insert(str);
freshO.erase(str);
};

auto isSafe = [&](int x, int y) {
if (x >= 0 && y >= 0 && x < grid.size() && y <grid[0].size()) {
return true;
}
return false;
};

for (int i=0;i<grid.size();i++) {
for (int j=0;j<grid[0].size();j++) {
if (grid[i][j] == 2) {
q.push({i,j});
// grid[i][j] = 0;
markVisited(i, j);
} else if (grid[i][j] == 1) {
freshO.insert(key(i, j));
}
}
}


int time = -1;
if (freshO.empty() && us.empty()) return 0;

while (!q.empty()) {
int size = q.size();
while (size--) {
auto node = q.front();
q.pop();

for (int i=0;i<dirs.size();i++) {
int newX = node.first + dirs[i][0];
int newY = node.second + dirs[i][1];

if (isSafe(newX, newY) && !us.contains(key(newX, newY)) && grid[newX][newY] == 1) {
markVisited(newX, newY);
grid[newX][newY] = 2;
q.push({newX, newY});
}
}
}
time++;
}
return freshO.empty() ? time : -1;
}
};