Shortest Path Problems Leetcode Javascript