Blocked Pipeline - Google Top Interview Questions


Problem Statement :


You are given an integer n and a two-dimensional list of integers requests.
 Consider a 2 x n matrix m where each cell can either be blocked or unblocked. 
It starts off as completely unblocked. 
Each element in requests contains [row, col, type] meaning that m[row][col] becomes blocked if type = 1 and it becomes unblocked if type = 0.

You want to process requests one by one and after processing each one check whether there is an unblocked path from left to right. 
That is, whether you can start off either m[0][0] or m[1][0] and on each step either move right, up, or down and then end up at either m[0][n - 1] or m[1][n - 1]. 
Return the number of requests that result in such unblocked path after processing

Constraints

1 ≤ n ≤ 100,000

0 ≤ r ≤ 100,000 where r is the length of requests

Example 1

Input

n = 4
requests = [

    [0, 2, 1],

    [1, 3, 1],

    [1, 3, 0]

]

Output

2

Explanation

After setting m[0][2] there is still a path from left to right. But if we block m[1][3], there is no longer a 
path. After we unblock m[1][3], there is a path again. So the first and last request result in an unblocked 
path.



Solution :



title-img




                        Solution in C++ :

int solve(int n, vector<vector<int>>& requests) {
    int num_single = 0;
    int num_pair = 0;

    vector matrix(2, vector<bool>(n));

    int res = 0;
    for (auto&& req : requests) {
        int row = req[0], col = req[1];
        bool type = req[2];
        if (type != matrix[row][col]) {
            bool old_0 = matrix[0][col];
            bool old_1 = matrix[1][col];
            matrix[row][col] = type;
            int prv = col - 1, nxt = col + 1;
            if (type) {
                // blocking
                if (matrix[0][col] && matrix[1][col]) num_single++;
                if (prv >= 0 && matrix[row ^ 1][prv]) num_pair++;
                if (nxt < n && matrix[row ^ 1][nxt]) num_pair++;
            } else {
                // unblocking
                if (old_0 && old_1) num_single--;
                if (prv >= 0 && matrix[row ^ 1][prv]) num_pair--;
                if (nxt < n && matrix[row ^ 1][nxt]) num_pair--;
            }
        }
        if (!num_single && !num_pair) res++;
    }
    return res;
}
                    


                        Solution in Java :

import java.util.*;

class Solution {
    public int solve(int n, int[][] requests) {
        int[][] a = new int[2][n];

        // successfull requests
        int ans = 0;

        // free columns with no blocked cells in both rows
        int freeCols = n;

        // cross blocks like illustrated before
        // 000x000
        // 0000x00
        int crossBlocks = 0;

        for (int[] request : requests) {
            int col = request[1];
            int row = request[0];
            int type = request[2];

            if (type == 0) {
                if (a[row][col] == 1) {
                    a[row][col] = 0;
                    if (a[1 - row][col] == 1) {
                        freeCols++;
                    }
                    if (col - 1 >= 0 && a[1 - row][col - 1] == 1) {
                        crossBlocks--;
                    }
                    if (col + 1 < n && a[1 - row][col + 1] == 1) {
                        crossBlocks--;
                    }
                }
            } else {
                if (a[row][col] == 0) {
                    a[row][col] = 1;
                    if (a[1 - row][col] == 1) {
                        freeCols--;
                    }
                    if (col - 1 >= 0 && a[1 - row][col - 1] == 1) {
                        crossBlocks++;
                    }
                    if (col + 1 < n && a[1 - row][col + 1] == 1) {
                        crossBlocks++;
                    }
                }
            }

            // Passage is possible only if all cols are free and there are not crossBlocks
            if (freeCols == n && crossBlocks == 0) {
                ans++;
            }
        }

        return ans;
    }
}
                    


                        Solution in Python : 
                            
class Solution:
    def solve(self, N, requests):

        m = [[0 for _ in range(N)] for __ in range(2)]

        neis = set()
        ans = 0
        for r, c, t in requests:

            if t == 1:
                m[r][c] = 1
                for col in (-1, 1, 0):
                    if 0 <= (c + col) < N and m[(r + 1) % 2][c + col] == 1:
                        neis.add((r, c, (r + 1) % 2, (c + col)))
            else:
                m[r][c] = 0
                for col in (-1, 1, 0):
                    if 0 <= (c + col) < N and m[(r + 1) % 2][c + col] == 1:
                        neis.discard((r, c, (r + 1) % 2, (c + col)))
                        neis.discard(((r + 1) % 2, (c + col), r, c))

            if not neis and (not (m[-1][-1] & m[0][-1])):
                ans += 1

        return ans
                    


View More Similar Problems

Fibonacci Numbers Tree

Shashank loves trees and math. He has a rooted tree, T , consisting of N nodes uniquely labeled with integers in the inclusive range [1 , N ]. The node labeled as 1 is the root node of tree , and each node in is associated with some positive integer value (all values are initially ). Let's define Fk as the Kth Fibonacci number. Shashank wants to perform 22 types of operations over his tree, T

View Solution →

Pair Sums

Given an array, we define its value to be the value obtained by following these instructions: Write down all pairs of numbers from this array. Compute the product of each pair. Find the sum of all the products. For example, for a given array, for a given array [7,2 ,-1 ,2 ] Note that ( 7 , 2 ) is listed twice, one for each occurrence of 2. Given an array of integers, find the largest v

View Solution →

Lazy White Falcon

White Falcon just solved the data structure problem below using heavy-light decomposition. Can you help her find a new solution that doesn't require implementing any fancy techniques? There are 2 types of query operations that can be performed on a tree: 1 u x: Assign x as the value of node u. 2 u v: Print the sum of the node values in the unique path from node u to node v. Given a tree wi

View Solution →

Ticket to Ride

Simon received the board game Ticket to Ride as a birthday present. After playing it with his friends, he decides to come up with a strategy for the game. There are n cities on the map and n - 1 road plans. Each road plan consists of the following: Two cities which can be directly connected by a road. The length of the proposed road. The entire road plan is designed in such a way that if o

View Solution →

Heavy Light White Falcon

Our lazy white falcon finally decided to learn heavy-light decomposition. Her teacher gave an assignment for her to practice this new technique. Please help her by solving this problem. You are given a tree with N nodes and each node's value is initially 0. The problem asks you to operate the following two types of queries: "1 u x" assign x to the value of the node . "2 u v" print the maxim

View Solution →

Number Game on a Tree

Andy and Lily love playing games with numbers and trees. Today they have a tree consisting of n nodes and n -1 edges. Each edge i has an integer weight, wi. Before the game starts, Andy chooses an unordered pair of distinct nodes, ( u , v ), and uses all the edge weights present on the unique path from node u to node v to construct a list of numbers. For example, in the diagram below, Andy

View Solution →