<p>Given an array of&nbsp;integers <code>arr</code> and an integer <code>d</code>. In one step you can jump from index <code>i</code> to index:</p>

<ul>
	<li><code>i + x</code> where:&nbsp;<code>i + x &lt; arr.length</code> and <code> 0 &lt;&nbsp;x &lt;= d</code>.</li>
	<li><code>i - x</code> where:&nbsp;<code>i - x &gt;= 0</code> and <code> 0 &lt;&nbsp;x &lt;= d</code>.</li>
</ul>

<p>In addition, you can only jump from index <code>i</code> to index <code>j</code>&nbsp;if <code>arr[i] &gt; arr[j]</code> and <code>arr[i] &gt; arr[k]</code> for all indices <code>k</code> between <code>i</code> and <code>j</code> (More formally <code>min(i,&nbsp;j) &lt; k &lt; max(i, j)</code>).</p>

<p>You can choose any index of the array and start jumping. Return <em>the maximum number of indices</em>&nbsp;you can visit.</p>

<p>Notice that you can not jump outside of the array at any time.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/01/23/meta-chart.jpeg" style="width: 633px; height: 419px;" />
<pre>
<strong>Input:</strong> arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong> You can start at index 10. You can jump 10 --&gt; 8 --&gt; 6 --&gt; 7 as shown.
Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 &gt; 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 &gt; 9.
Similarly You cannot jump from index 3 to index 2 or index 1.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> arr = [3,3,3,3,3], d = 3
<strong>Output:</strong> 1
<strong>Explanation:</strong> You can start at any index. You always cannot jump to any index.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> arr = [7,6,5,4,3,2,1], d = 1
<strong>Output:</strong> 7
<strong>Explanation:</strong> Start at index 0. You can visit all the indicies. 
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= arr.length &lt;= 1000</code></li>
	<li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= d &lt;= arr.length</code></li>
</ul>

# well, try to find the max distance path through DFS!
# Think the position that I can jump to in 1 step as "connected neighbours"
# Then do DFS (because I follow one trace of path and walk), track the length of longest path

# But blindly do DFS makes complexity worst case O(N * d^N)! Because each node can span d nodes
# So need memoisation!! Use recursion

class Solution:
    def maxJumps(self, arr: List[int], d: int) -> int:
        # To save run time and avoid repeatedly finding neighbours, find the neighbours for all nodes from the start!
        neighbourMap = dict()
        for curr in range(len(arr)):
            neighbourMap[curr] = self.findNeighbours(curr, arr, d)

        cache = dict() # cache[i] = max jumps (DFS longest path starting from index i)

        result = float("-inf")
        for i in range(len(arr)):
            result = max(result, self.DFS(i, arr, d, cache, neighbourMap))

        return result

    def DFS(self, curr, arr, d, cache, neighbourMap):
        if not neighbourMap[curr]:
            cache[curr] = 1
            return 1

        if curr in cache:
            return cache[curr]

        maxJump = float("-inf")
        neighbours = neighbourMap[curr]

        # print(f"Now curr is {curr}, its neighbours {neighbours}")
        # print(f"Now cache is {cache}")
        for nei in neighbours: 
            temp_result = 1 + self.DFS(nei, arr, d, cache, neighbourMap)
            maxJump = max(maxJump, temp_result)
            # print(f"This neighbour is {nei} for curr {curr}, it updates maxJump to {maxJump}")
        cache[curr] = maxJump

        # print(f"Curr is {curr}, maxJump is {maxJump}")
        # print()
        return maxJump

    def findNeighbours(self, curr, arr, d):
        output = []
        
        # search left jump, there cannot be anyone who is higher! The first higher appear, stop
        for i in range(1, d+1):
            if curr - i < 0: 
                continue
            else:
                neighHeight = arr[curr - i]
                if neighHeight < arr[curr]:
                    output.append(curr - i)
                else:
                    break

        # search left jump
        for i in range(1, d+1):
            if curr + i >= len(arr):
                continue
            else:
                neighHeight = arr[curr + i]
                if neighHeight < arr[curr]:
                    output.append(curr + i)
                else:
                    break
        return output





        
29400