Dynamic Programming - Activity Selection
<p>I am new to C++. Can someone please convert this Algorithm into a C++ Code. I am not able to understand it fully, Thanks. </p>

<p>I am able to solve the problem on paper, using a Table Matrix, and I understand its devide and conquer strategy but having difficulty in implementing the algorithm after passing both s and f arrays to DP Function. </p>

<pre><code>for  i =1  to  n
do   m[i] = max(m[i-1], 1+ m [BINARY-SEARCH(f, s[i])])
             We have P(i] = 1 if activity i is in optimal selection, and P[i] = 0
             otherwise
             i = n
            while   i &gt; 0
                 do if m[i] = m[i-1]
                         then P[i] = 0
                                    i = i - 1
                         else
                                   i = BINARY-SEARCH (f, s[i])
                                  P[i] = 1
</code></pre>

<p>So far I have been able to do it with Greedy Algorithm, </p>

<pre><code>void MaxActGreedy(int s[], int f[], int n)
{
cout&lt;&lt;"\n Entering Greedy Prgramming Fuction \n";
clock_t startTime = clock();

cout&lt;&lt;" Greedy Solution (Index no. ) :";
int i;
int j;

i=0;
cout&lt;&lt;i;

for(j=1; j&lt;n; j++)
{
    if (s[j]&gt;=f[i])
    {
        cout&lt;&lt;j;
    i=j;
    }
}

clock_t endTime= clock();

endTime = endTime - startTime;
float timeinSeconds = endTime /  (float) CLOCKS_PER_SEC;

cout&lt;&lt;"\n Greedy Time: ";
cout&lt;&lt;timeinSeconds;
cout&lt;&lt;" Seconds";

}

 void Dynamic(int s[],int f[],int n)
{
int m[]={0};

for(int i=0; i&lt;n; i++)
{



}
}

int main()
{
int s[]={1,3,0,5,3,5,6,8,8,2,12};//Start Time Si
int f[]={4,5,6,7,8,9,10,11,12,13,14};//Finish Times fi (sorted)


int n = sizeof(s)/sizeof(s[0]);

MaxActGreedy(s,f,n);
// MaxActDP(s,f,n);
Dynamic(s,f,n);



return 0;
}
</code></pre>
