mardi 19 décembre 2017

buy low sell high c++ without using imperative loop

I have something like this in tradeTest.cpp

int main() {
    vector<int> prices{28, 18, 20, 26, 24};

    int profit = bestProfit(prices.begin(), prices.end());

    if (profit == 8) {
        cout << "Profit of 8 is correct\n";
    } else {
        cout << "Profit of " << profit << " is incorrect\n";
    }
}

and currently this in trade.h

template <class RandomAccessIterator, class T>
int bestProfit (Iterator begin, Iterator end)
{

}

and I am only allowed to use these libraries:

#include <iostream>
#include <algorithm>
#include <numeric>
#include <utility>
#include <cstdint>

What I want to do is buy low and sell high without the possibility of going back. so In this example I want to buy at 18 and sell at 26. Not using imperative loops and having only one function (bestProfit) makes it much harder for me. I am not sure what I need to do.

Thanks for your itme.

Aucun commentaire:

Enregistrer un commentaire