Minimize supply of Corona Vaccines for N houses if a vaccine is sufficient for immediate neighbours
Geek has developed an effective vaccine for the Coronavirus and he wants each of the N houses in Geek Land to have access to it. Given a binary tree where each node represents a house in Geek Land, find the minimum number of houses that should be supplied with the vaccine kit if one vaccine kit is sufficient for that house, its parent house, and its immediate child nodes.
Examples:
Input:
1
/ \
2 3
\
4
\
5
\
6
Output: 2
Explanation:
The vaccine kits should be supplied to house numbers 1 and 5.
Input:
1
/ \
2 3
Output: 1
Explanation:
The vaccine kits should be supplied to house number 1.
Approach: This problem can be solved using dynamic programming.
- Create a hashtable to keep check of all the nodes visited.
- The recursive function should return the number of child nodes below which are unvisited.
- If the node is NULL, return 0. This will be our base condition.
- Create a counter variable to store the number of unvisited child nodes and initialize it by the recursive call for the left and the right child nodes.
- If the counter variable is zero, all the child nodes have been visited. If the current node is unvisited and it is not the root node we return 1 as there is one node i.e. the current node which is unvisited.
- If the current node is unvisited and the counter variable is also zero and if the current node is root node then we increment the answer by 1 and return 1.
- Otherwise, if the counter variable is greater than zero, we mark the parent node, current node, and the child nodes as visited and increment the answer by 1.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node* left;
Node* right;
Node( int val)
{
data = val;
left = right = NULL;
}
};
int solve( int & ans, Node* root, Node* parent,
unordered_map<Node*, int >& dp)
{
if (root == 0)
return 0;
int cnt = solve(ans, root->left, root, dp);
cnt += solve(ans, root->right, root, dp);
if (cnt == 0) {
if (dp[root] == 0 && parent == 0) {
ans++;
return 1;
}
if (dp[root] == 0)
return 1;
return 0;
}
else {
if (root->left != 0)
dp[root->left] = 1;
if (root->right != 0)
dp[root->right] = 1;
dp[root] = 1;
if (parent != 0)
dp[parent] = 1;
ans++;
return 0;
}
}
int supplyVaccine(Node* root)
{
unordered_map<Node*, int > dp;
int ans = 0;
solve(ans, root, 0, dp);
return ans;
}
int main()
{
string treeString;
Node* root = new Node(1);
root->left = new Node(2);
root->right = new Node(3);
root->right->right = new Node(4);
root->right->right->right = new Node(5);
root->right->right->right->right
= new Node(6);
cout << supplyVaccine(root) << "\n" ;
return 0;
}
|
Java
import java.util.*;
class GFG {
static class Node {
int data;
Node left;
Node right;
Node( int val) {
data = val;
left = right = null ;
}
};
static int ans;
static HashMap<Node, Integer> dp;
static int solve(Node root, Node parent)
{
if (root == null )
return 0 ;
ans = solve(root.left, root);
ans += solve(root.right, root);
if (ans == 0 ) {
if (dp.get(root) == null && parent == null ) {
ans++;
return 1 ;
}
if (dp.get(root) == null )
return 1 ;
}
else
{
if (root.left != null )
dp.put(root.left, 1 );
if (root.right != null )
dp.put(root.right, 1 );
dp.put(root, 1 );
if (parent != null )
dp.put(parent, 1 );
}
return 0 ;
}
static void supplyVaccine(Node root) {
dp = new HashMap<>();
ans = 0 ;
solve(root, root);
}
public static void main(String[] args) {
String treeString;
Node root = new Node( 1 );
root.left = new Node( 2 );
root.right = new Node( 3 );
root.right.right = new Node( 4 );
root.right.right.right = new Node( 5 );
root.right.right.right.right = new Node( 6 );
supplyVaccine(root);
System.out.print(ans + "\n" );
}
}
|
Python3
class Node:
def __init__( self , val):
self .data = val
self .left = None
self .right = None
dp = {}
ans = 0
def solve(root, parent):
global dp
global ans
if (root = = None ):
return 0
ans = solve(root.left, root)
ans + = solve(root.right, root)
if (ans = = 0 ):
if (root not in dp and parent = = None ):
ans + = 1
return 1
if (root not in dp):
return 1
else :
if (root.left ! = None ):
dp[root.left] = 1
if (root.right ! = None ):
dp[root.right] = 1
dp[root] = 1
if (parent ! = None ):
dp[parent] = 1
return 0
def supplyVaccine(root):
global ans
solve(root, root)
root = Node( 1 )
root.left = Node( 2 )
root.right = Node( 3 )
root.right.right = Node( 4 )
root.right.right.right = Node( 5 )
root.right.right.right.right = Node( 6 )
supplyVaccine(root)
print (ans)
|
C#
using System;
using System.Collections.Generic;
public class GFG{
class Node {
public int data;
public Node left;
public Node right;
public Node( int val){
this .data=val;
this .left= this .right= null ;
}
}
static int ans;
static Dictionary<Node, int > dp;
static int solve(Node root, Node parent)
{
if (root == null )
return 0;
ans = solve(root.left, root);
ans += solve(root.right, root);
if (ans == 0) {
if (!dp.ContainsKey(root) && parent == null ) {
ans++;
return 1;
}
if (!dp.ContainsKey(root))
return 1;
}
else
{
if (root.left != null )
dp[root.left]=1;
if (root.right != null )
dp[root.right]=1;
dp[root]=1;
if (parent != null )
dp[parent]=1;
}
return 0;
}
static void supplyVaccine(Node root) {
dp = new Dictionary<Node, int >();
ans = 0;
solve(root, root);
}
static public void Main (){
Node root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.right.right = new Node(4);
root.right.right.right = new Node(5);
root.right.right.right.right = new Node(6);
supplyVaccine(root);
Console.Write(ans);
}
}
|
Javascript
<script>
class Node
{
constructor(val) {
this .data = val;
this .left = this .right = null ;
}
}
var ans;
var dp = new Map();
function solve(root, parent) {
if (root == null )
return 0;
ans = solve(root.left, root);
ans += solve(root.right, root);
if (ans == 0) {
if (dp.get(root) == null && parent == null ) {
ans++;
return 1;
}
if (dp.get(root) == null )
return 1;
}
else {
if (root.left != null )
dp.set(root.left, 1);
if (root.right != null )
dp.set(root.right, 1);
dp.set(root, 1);
if (parent != null )
dp.set(parent, 1);
}
return 0;
}
function supplyVaccine(root) {
ans = 0;
solve(root, root);
}
var root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.right.right = new Node(4);
root.right.right.right = new Node(5);
root.right.right.right.right = new Node(6);
supplyVaccine(root);
document.write(ans + "\n" );
</script>
|
Time Complexity: O(N).
Auxiliary Space: O(N)
Last Updated :
21 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...