Dynamic Programming?

flylib

Im struggling how to find the maximum amount of dollars that you can achieve with a specified limit on the number of transactions using Dynamic Programming

Jonathan

This not an elegant solution but it will work for this particular problem (I'm guessing we have the same professor).

The logic is that for each V[n][c] we want to find the highest value possible for each unit of currency, and in order to do this we must calculate the maximum value out of 6 vales.

There are 6 values because there are 3 currencies, and each of those currencies has two possible ways that it can be converted into the target currency.

In this case since there are only 2 exchanges I simply do two statements rather than another loop. This is represented by the 0 in the array: rates[0][i][c]

I hope this helps!

    for (int n = 1; n <= numberOfTransactions; n++) {
        for (int c = 0; c < numberOfcurrencies; c++) {
            double max = Double.NEGATIVE_INFINITY;
            double temp;
            for (int i = 0; i < numberOfcurrencies;i++) {
                temp = rates[0][i][c]*V[n-1][i];
                if (temp > max)
                    max = temp;
                temp = rates[1][i][c]*V[n-1][i];
                if (temp > max)
                    max = temp;
            }
            V[n][c] = max;
        }
    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related