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

Largest Rectangle

Skyline Real Estate Developers is planning to demolish a number of old, unoccupied buildings and construct a shopping mall in their place. Your task is to find the largest solid area in which the mall can be constructed. There are a number of buildings in a certain two-dimensional landscape. Each building has a height, given by . If you join adjacent buildings, they will form a solid rectangle

View Solution →

Simple Text Editor

In this challenge, you must implement a simple text editor. Initially, your editor contains an empty string, S. You must perform Q operations of the following 4 types: 1. append(W) - Append W string to the end of S. 2 . delete( k ) - Delete the last k characters of S. 3 .print( k ) - Print the kth character of S. 4 . undo( ) - Undo the last (not previously undone) operation of type 1 or 2,

View Solution →

Poisonous Plants

There are a number of plants in a garden. Each of the plants has been treated with some amount of pesticide. After each day, if any plant has more pesticide than the plant on its left, being weaker than the left one, it dies. You are given the initial values of the pesticide in each of the plants. Determine the number of days after which no plant dies, i.e. the time after which there is no plan

View Solution →

AND xor OR

Given an array of distinct elements. Let and be the smallest and the next smallest element in the interval where . . where , are the bitwise operators , and respectively. Your task is to find the maximum possible value of . Input Format First line contains integer N. Second line contains N integers, representing elements of the array A[] . Output Format Print the value

View Solution →

Waiter

You are a waiter at a party. There is a pile of numbered plates. Create an empty answers array. At each iteration, i, remove each plate from the top of the stack in order. Determine if the number on the plate is evenly divisible ith the prime number. If it is, stack it in pile Bi. Otherwise, stack it in stack Ai. Store the values Bi in from top to bottom in answers. In the next iteration, do the

View Solution →

Queue using Two Stacks

A queue is an abstract data type that maintains the order in which elements were added to it, allowing the oldest elements to be removed from the front and new elements to be added to the rear. This is called a First-In-First-Out (FIFO) data structure because the first element added to the queue (i.e., the one that has been waiting the longest) is always the first one to be removed. A basic que

View Solution →