Riddles
From OriWiki
The biggest difference
Suppose you are given a vector of values of a stock. Your mission is is to find the best time to buy and the best time to sell it. The algorithm should be efficient.
void biggest_difference(const vector<int>& v) {
int min=0,max=0,candidate_min=0;
for(unsigned int i=1; i<v.size(); ++i) {
if(v[i] - v[candidate_min] > v[max] - v[min]) {
min = candidate_min;
max = i;
}
else if(v[i] < v[candidate_min])
candidate_min = i;
}
cout << min << ',' << max << '\n';
}

