Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- using namespace std;
- ifstream f("date.in");
- vector<int> stockPrices;
- int x, l, r, maxProfit;
- int main()
- {
- while(f>>x){ //citire
- stockPrices.push_back(x);
- }
- l = 0; r = stockPrices.size() - 1; //cautam in vector cu 2 variabile, stanga si dreapta
- maxProfit = stockPrices[r] - stockPrices[l];
- while(r - l > 0){
- if(stockPrices[r] - stockPrices[l] > maxProfit) //daca profitul de la pozitiile curente e mai mare decat cel maxim in actualizam
- maxProfit = stockPrices[r] - stockPrices[l];
- if(stockPrices[r] - stockPrices[l+1] > stockPrices[r-1] - stockPrices[l])//trecem la urmatoarea valoare din stanga daca obtinem un profit mai bun alegand-o pe ea si nu pe cea din dreapta
- l++;
- else r--; //altfel o alegem pe urmatoarea din dreapta
- }
- cout<<maxProfit;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement