<p>You are given three integers <code>n</code>, <code>m</code> and <code>k</code>. Consider the following algorithm to find the maximum element of an array of positive integers:</p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/04/02/e.png" style="width: 424px; height: 372px;" />
<p>You should build the array arr which has the following properties:</p>

<ul>
	<li><code>arr</code> has exactly <code>n</code> integers.</li>
	<li><code>1 &lt;= arr[i] &lt;= m</code> where <code>(0 &lt;= i &lt; n)</code>.</li>
	<li>After applying the mentioned algorithm to <code>arr</code>, the value <code>search_cost</code> is equal to <code>k</code>.</li>
</ul>

<p>Return <em>the number of ways</em> to build the array <code>arr</code> under the mentioned conditions. As the answer may grow large, the answer <strong>must be</strong> computed modulo <code>10<sup>9</sup> + 7</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> n = 2, m = 3, k = 1
<strong>Output:</strong> 6
<strong>Explanation:</strong> The possible arrays are [1, 1], [2, 1], [2, 2], [3, 1], [3, 2] [3, 3]
</pre>

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

<pre>
<strong>Input:</strong> n = 5, m = 2, k = 3
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no possible arrays that satisfy the mentioned conditions.
</pre>

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

<pre>
<strong>Input:</strong> n = 9, m = 1, k = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> The only possible array is [1, 1, 1, 1, 1, 1, 1, 1, 1]
</pre>

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

<ul>
	<li><code>1 &lt;= n &lt;= 50</code></li>
	<li><code>1 &lt;= m &lt;= 100</code></li>
	<li><code>0 &lt;= k &lt;= n</code></li>
</ul>

class Solution {
public:
    int t[52][152][152];
    int d=1e9+7;
    int solve(int n,int prev,int m, int k)
    {
        if(k<0)
        return 0;

        if(n<=0)
        {
            if(k<=0)
            return 1;

            return 0;
        }

        if(t[n][prev+1][k]!=-1)
        return t[n][prev+1][k];

        int a=0,b=0,c=0,result=0;
        for(int j=1;j<=m;j++)
        {

            if (j > prev) {
                // Increase search cost if j is greater than the previous max
                result = (result + solve(n - 1, j, m, k - 1)) % d;
            } else {
                // Keep search cost the same
                result = (result + solve(n - 1, prev, m, k)) % d;
            }

            // if(n<=k)
            // {
            //     if(prev==-1 || (k>0 && j>prev && n>0))
            //     a=(a + solve(n-1,j,m,k-1))%d;
            //     else if(k==0 && n>0 && j<=prev)
            //     b=(b + solve(n-1,j,m,k))%d;
            // }
            // else
            // {
            //     if(prev==-1)
            //     c=(c + solve(n-1,j,m,k-1))%d;
            //     else
            //     {
            //         if(k-1>=0)
            //         c=(c + solve(n-1,j,m,(j>prev?k-1:k)))%d;
            //         else if(j<=prev)
            //         c=(c + solve(n-1,j,m,k))%d;
            //     }
                
        }
            
        


        return t[n][prev+1][k]=result;
    }
    int numOfArrays(int n, int m, int k) {
        memset(t,-1,sizeof(t));
        return solve(n,-1,m,k);
    }
};
12700