Initializing a vector/matrix VS using (x,y) as the key

In many questions, you would need to initialize a 2D matrix or a vector. For example, in many matrix traversal question, you need a matrix to keep track of visited nodes. Or any 2-D DP question, you also need a 2D matrix.

People usually pre-allocate such a matrix by initiliazing a vector of vector ( or a list of lists in python).

Why not use a diciontary (or hashmap in java), whose key is the (x,y) ? This way, you never have to initialize anything and therefore won't waste space that may not be used?

Comments (0)