HackerEarth ‘Cells in a matrix’ Solution

Short Problem Definition:

You are given an integer N denoting an N×N matrix. Initially, each cell of the matrix is empty. You are given K tasks. In each task, you are given a cell (i,j) where cell (i,j) represents the ith row and jth column of the given matrix.

You have to perform each task sequentially in the given order. Each task is described in cell (i,j). For each task, you have to place X in each cell of row i and each cell column j. After you complete each task, you are required to print the number of empty cells in the matrix.  


Cells In A Matrix


time complexity is O(K*log(N))

space complexity is O(N)


There are many solutions to this problem statement. I picked this one, since its easy to understand and is correct.

def cells_sol (N, K, tasks):
    rows = set([i+1 for i in xrange(N)])
    columns = set([i+1 for i in xrange(N)])
    result = []

    for task in tasks:

    return result

N, K = map(int, raw_input().split())
task = []
for _ in range(K):
    i, j = map(int, raw_input().split())
    X = i, j

out_ = cells_sol(N, K, task)
print (' '.join(map(str, out_)))

If you enjoyed this post, then make sure you subscribe to my Newsletter and/or Feed.