allcoding1_official
Photo
vector<int> lis[100001];
int vis[100001];
void initialize(int n)
{
for (int i = 0; i <= n; i++)
{
lis[i].clear();
vis[i] = 0;
}
}
set<int> found;
void dfs(int node)
{
found.insert(node);
vis[node] = 1;
for (int child : lis[node])
{
if (vis[child] == 0)
{
dfs(child);
}
}
}
vector<int> timeOfInfection(int n, int m, vector<int> initially_infected, vector<vector<int>> update)
{
vector<int> status(n, 0);
vector<int> ans(n, -1);
for (int i = 0; i < m; i++)
{
status[initially_infected[i]] = 1;
ans[initially_infected[i]]=0;
}
for (int i = 0; i < update.size(); i++)
{
int type = update[i][0];
if (type == 0)
{
int a = update[i][1];
int b = update[i][2];
if (status[a] == 0 && status[b] == 0)
{
lis[a].push_back(b);
lis[b].push_back(a);
}
else if (status[a] == 1 && status[b] == 1)
{
lis[a].push_back(b);
lis[b].push_back(a);
}
else if (status[a] == 1)
{
found.clear();
dfs(b);
for (auto x : found)
{
ans[x] = i + 1;
status[x] = 1;
}
}
else
{
found.clear();
dfs(a);
for (auto x : found)
{
ans[x] = i + 1;
status[x] = 1;
}
}
}
}
for (int i = 0; i < update.size(); i++)
{
int type = update[i][0];
if(type==1)
{
int a=update[i][1];
if(i+1<ans[a])
{
ans[a]=-1;
}
}
}
return ans;
}
int vis[100001];
void initialize(int n)
{
for (int i = 0; i <= n; i++)
{
lis[i].clear();
vis[i] = 0;
}
}
set<int> found;
void dfs(int node)
{
found.insert(node);
vis[node] = 1;
for (int child : lis[node])
{
if (vis[child] == 0)
{
dfs(child);
}
}
}
vector<int> timeOfInfection(int n, int m, vector<int> initially_infected, vector<vector<int>> update)
{
vector<int> status(n, 0);
vector<int> ans(n, -1);
for (int i = 0; i < m; i++)
{
status[initially_infected[i]] = 1;
ans[initially_infected[i]]=0;
}
for (int i = 0; i < update.size(); i++)
{
int type = update[i][0];
if (type == 0)
{
int a = update[i][1];
int b = update[i][2];
if (status[a] == 0 && status[b] == 0)
{
lis[a].push_back(b);
lis[b].push_back(a);
}
else if (status[a] == 1 && status[b] == 1)
{
lis[a].push_back(b);
lis[b].push_back(a);
}
else if (status[a] == 1)
{
found.clear();
dfs(b);
for (auto x : found)
{
ans[x] = i + 1;
status[x] = 1;
}
}
else
{
found.clear();
dfs(a);
for (auto x : found)
{
ans[x] = i + 1;
status[x] = 1;
}
}
}
}
for (int i = 0; i < update.size(); i++)
{
int type = update[i][0];
if(type==1)
{
int a=update[i][1];
if(i+1<ans[a])
{
ans[a]=-1;
}
}
}
return ans;
}
👍6
#include <iostream>
#include <vector>
using namespace std;
long makePowerNondecreasing(vector<int>& power) {
int n = power.size();
long adjustments = 0;
for (int i = 1; i < n; ++i) {
if (power[i] < power[i - 1]) {
adjustments += power[i - 1] - power[i];
}
}
return adjustments;
}
Amazon
Telegram:- @allcoding1_official
#include <vector>
using namespace std;
long makePowerNondecreasing(vector<int>& power) {
int n = power.size();
long adjustments = 0;
for (int i = 1; i < n; ++i) {
if (power[i] < power[i - 1]) {
adjustments += power[i - 1] - power[i];
}
}
return adjustments;
}
Amazon
Telegram:- @allcoding1_official
👍4❤🔥1
allcoding1_official
Photo
#include <iostream>
#include <vector>
using namespace std;
class SinglyLinkedListNode {
public:
string data;
SinglyLinkedListNode* next;
SinglyLinkedListNode(string value) {
data = value;
next = nullptr;
}
};
SinglyLinkedListNode* getShoppingCart(SinglyLinkedListNode* head, vector<vector<string>> queries) {
for (auto& query : queries) {
string action = query[0];
string item_name = query[1];
if (action == "PUSH_HEAD") {
SinglyLinkedListNode* new_node = new SinglyLinkedListNode(item_name);
new_node->next = head;
head = new_node;
} else if (action == "PUSH_TAIL") {
SinglyLinkedListNode* new_node = new SinglyLinkedListNode(item_name);
if (head == nullptr) {
head = new_node;
} else {
SinglyLinkedListNode* current = head;
while (current->next != nullptr) {
current = current->next;
}
current->next = new_node;
}
} else if (action == "POP_HEAD") {
if (head != nullptr) {
SinglyLinkedListNode* temp = head;
head = head->next;
delete temp;
}
}
}
return head;
}
Amazon
#include <vector>
using namespace std;
class SinglyLinkedListNode {
public:
string data;
SinglyLinkedListNode* next;
SinglyLinkedListNode(string value) {
data = value;
next = nullptr;
}
};
SinglyLinkedListNode* getShoppingCart(SinglyLinkedListNode* head, vector<vector<string>> queries) {
for (auto& query : queries) {
string action = query[0];
string item_name = query[1];
if (action == "PUSH_HEAD") {
SinglyLinkedListNode* new_node = new SinglyLinkedListNode(item_name);
new_node->next = head;
head = new_node;
} else if (action == "PUSH_TAIL") {
SinglyLinkedListNode* new_node = new SinglyLinkedListNode(item_name);
if (head == nullptr) {
head = new_node;
} else {
SinglyLinkedListNode* current = head;
while (current->next != nullptr) {
current = current->next;
}
current->next = new_node;
}
} else if (action == "POP_HEAD") {
if (head != nullptr) {
SinglyLinkedListNode* temp = head;
head = head->next;
delete temp;
}
}
}
return head;
}
Amazon
👍13👌1
🎯OLA is hiring for Software Developer Intern
Stipend: ₹ 30k - ₹ 50k
Apply Now:- https://docs.google.com/forms/d/e/1FAIpQLSfuOSPDwkjNlXkpo4zA9_GACQxwr3lMae2YFFu6L0-6irBF4Q/viewform
Stipend: ₹ 30k - ₹ 50k
Apply Now:- https://docs.google.com/forms/d/e/1FAIpQLSfuOSPDwkjNlXkpo4zA9_GACQxwr3lMae2YFFu6L0-6irBF4Q/viewform
👍11