triangle solution with hashmap to memoize results for layer and rootindex pair