Posts

Hash tables aren't cost free

Image
Those of you who know me know that I'm a big fan of key;value pairs for solving problems, mainly using hash tables. But they aren't cost free. The problem below leads to a TLE if you use a hash table to store all the ones in the matrix (up to 1M ones). Interesting that all the tests pass but you still get a TLE. Solution is to replace the hash table with a simple array (a little more space being used) which does the trick. The code with the hash table that led to TLE is in the screenshot below, and the correct code written down below too. Cheers, ACC. Right Triangles - LeetCode 3128. Right Triangles Medium 37 6 Add to List Share You are given a 2D boolean matrix  grid . Return an integer that is the number of  right triangles  that can be made with the 3 elements of  grid  such that  all  of them have a value of 1. Note: A collection of 3 elements of  grid  is a  right triangle  if one of its elements is in the  same row  with another element and in the  same column  with the t