Step 1 / 19Sequence a=[3,1,8,2,5,4,7,6]. Compute dp[i] = length of LIS ending at index i.
Step 2 / 19Base case: dp[0]=1. Any single element is an increasing subsequence of length 1.
Step 3 / 19i=1: a[1]=1. Check a[0]=3>1, so no valid predecessor. dp[1]=1.
Step 4 / 19dp[1]=1. No transition arrow needed.
Step 5 / 19i=2: a[2]=8. Both a[0]=3 and a[1]=1 are smaller. Best is dp[0]=1. So dp[2]=2.
Step 6 / 19dp[2]=2, coming from index 0.
Step 7 / 19i=3: a[3]=2. Only a[1]=1<2. dp[3]=dp[1]+1=2.
Step 8 / 19dp[3]=2, extending from index 1.
Step 9 / 19i=4: a[4]=5. Predecessors: a[0]=3,a[1]=1,a[3]=2 are smaller. Best is dp[3]=2. dp[4]=3.
Step 10 / 19dp[4]=3, extending the subsequence [1,2,5].
Step 11 / 19i=5: a[5]=4. Predecessors: a[0]=3,a[1]=1,a[3]=2 are smaller. Best is dp[3]=2. dp[5]=3.
Step 12 / 19dp[5]=3, extending subsequence [1,2,4].
Step 13 / 19i=6: a[6]=7. Many predecessors. Best is dp[4]=3 (via a[4]=5<7). dp[6]=4.
Step 14 / 19dp[6]=4, extending subsequence [1,2,5,7].
Step 15 / 19i=7: a[7]=6. Predecessors with smaller value: a[4]=5,a[3]=2,a[1]=1,a[0]=3. Best is dp[5]=3 (via a[5]=4<6). dp[7]=4.
Step 16 / 19dp[7]=4. The LIS length is max(dp)=4.
Step 17 / 19Traceback: one optimal LIS is a[1],a[3],a[4],a[6]=[1,2,5,7].
Step 18 / 19Recolor transition arrows along the optimal path: 1→3→4→6.
Step 19 / 19Dim the non-path cells to emphasize the solution: LIS = [1,2,5,7], length 4.