Explanation for why DP doesn't work with code
\nclass Solution:\n    def __init__(self):\n        self.PACIFIC, self.ATLANTIC = 0, 1\n        self.VISITED = -1\n        EAST, WEST = [0, 1], [0, -1]\n       
```\nclass Solution:\n    def __init__(self):\n        self.PACIFIC, self.ATLANTIC = 0, 1\n        self.VISITED = -1\n        EAST, WEST = [0, 1], [0, -1]\n        NORTH, SOUTH = [-1, 0], [1, 0]\n        self.DIRECTIONS = [EAST, WEST, NORTH, SOUTH]\n        self.ROWS, self.COLS = 0, 0\n        self.heights = []\n        self.pacific_map = {}\n        self.atlantic_map = {}\n        \n        \n    def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:\n        self.ROWS, self.COLS = len(heights), len(heights[0])\n        self.heights = heights\n        both_reachable_coordinates = []\n        \n        for row in range(self.ROWS):\n            for col in range(self.COLS):\n                if (self.ocean_reachable(row, col, self.ATLANTIC) and\n                    self.ocean_reachable(row, col, self.PACIFIC)):\n                    both_reachable_coordinates.append([row, col])\n\n        return both_reachable_coordinates\n                    \n    def in_pacific(self, row, col):\n        return row == -1 or col == -1\n    \n    def in_atlantic(self, row, col):\n        return row == self.ROWS or col == self.COLS\n    \n    def in_range(self, row, col):\n        return row in range(self.ROWS) and col in range(self.COLS)\n    \n    def can_water_flow(self, curr_height, new_row, new_col):\n        return curr_height >= self.heights[new_row][new_col]\n        \n    def not_visited(self, row, col):\n        return self.heights[row][col] != -1\n    \n    def update_dp_map(self, ocean, row, col, value):\n        if ocean == self.PACIFIC:\n            self.pacific_map[(row,col)] = value\n        else:\n            self.atlantic_map[(row,col)] = value                     \n\n    def is_cached(self, row, col, ocean):\n        if ocean == self.PACIFIC:\n            return (row,col) in self.pacific_map\n        else:\n            return (row,col) in self.atlantic_map\n        \n    def cached_value(self, row, col, ocean):\n        if ocean == self.PACIFIC:\n            return self.pacific_map[(row,col)]\n        else:\n            return self.atlantic_map[(row,col)]\n\n    def ocean_reachable(self, row, col, ocean):\n            if self.is_cached(row, col, ocean):\n                return self.cached_value(row, col, ocean)\n           \n            if ocean == self.PACIFIC and self.in_pacific(row, col):\n                self.update_dp_map(ocean, row, col, True)\n                return True\n            if ocean == self.ATLANTIC and self.in_atlantic(row, col):\n                self.update_dp_map(ocean, row, col, True)\n                return True\n            \n            if not self.in_range(row, col):\n                self.update_dp_map(ocean, row, col, False)\n                return False\n            \n            curr_height = self.heights[row][col]\n            self.heights[row][col] = self.VISITED\n            for drow, dcol in self.DIRECTIONS:\n                new_row, new_col = row + drow, col + dcol\n                if ((((self.in_range(new_row, new_col) and \n                     self.not_visited(new_row, new_col) and \n                      self.can_water_flow(curr_height, new_row, new_col)) or\n                      not self.in_range(new_row, new_col)) and\n                    self.ocean_reachable(new_row, new_col, ocean))):\n                    self.heights[row][col] = curr_height\n                    self.update_dp_map(ocean, row, col, True) \n                    self.update_dp_map(ocean, new_row, new_col, True) \n                    return True\n            \n            self.heights[row][col] = curr_height\n            self.update_dp_map(ocean, row, col, False)\n            return False\n```\n\n![image](https://assets.leetcode.com/users/images/a0a1979b-ab69-40cc-9f6d-61d122d0a7f2_1641948104.655987.png)\n\nLets say you are trying to find if you can reach Pacific from index(1,2), and you reached index(1,2) via index(1,1). Also, lets assume the only way to Pacific from index(1,2) is via index(1,1), but since we reached index(1,2) from (1,1), we cannot visit it again to prevent cycles. So, even though the we can reach pacific from (1,2), since the answer to (1,2) was dependent on (1,1) and the answer to (1,1) was dependent on (1,2), we have what we call a **Cyclic Dependency**. Because of this, DP will not work for certain cases like this.
['Dynamic Programming']