0

I am working through Leetcode questions and a common scenario I am running into is where I have a solution I could use but it requires a hashtable to have the Key as a pair of X and Y coordinates. In googling this I am unable to find any help which makes me believe I am doing something wrong if I want to use them this way.

This commonly occurs in Graph questions or multidimensional array questions.

Does anyone have any thoughts on how I should be implementing this on a regular basis? Or any reasons why I SHOULDNT be doing this?

The language I am using for this is C# but I'm sure it applies to most languages.

Thanks a lot!

Max Kenney
  • 43
  • 8
  • An x,y coordinate hash is a square on the graph with corners 1) x min, y min 2) x min y max 3) x max y min 4) x max y max. So usually your has is the x min, y min and round down values to the min. So if you have a graph from (0,0) to (4,4) and you want your hash size to be 1 x 1 then the hash points for (0,0) will be all the points with x < 1 and y < 1. – jdweng Sep 04 '20 at 11:34
  • You can also combine your x/y values as a string to make a key. Just be sure to pad your values so that you always have a fixed length key that is unambiguous ("0104" for example). – Idle_Mind Sep 04 '20 at 13:33

1 Answers1

1

You could use a Tuple:

var dict = new Dictionary<(int x, int y), string>();
dict.Add((12, 34), "My Town");

See related answer.

Assuming that the coordinates are always smaller than MAX, you could calculate a combined key

x * MAX + y
Axel Kemper
  • 10,544
  • 2
  • 31
  • 54