Advertisement
Guest User

Untitled

a guest
Jul 18th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.94 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. ifstream f("date.in");
  8.  
  9. vector<int> stockPrices;
  10.  
  11. int x, l, r, maxProfit;
  12.  
  13. int main()
  14. {
  15.  
  16.     while(f>>x){ //citire
  17.  
  18.         stockPrices.push_back(x);
  19.  
  20.     }
  21.  
  22.     l = 0; r = stockPrices.size() - 1;  //cautam in vector cu 2 variabile, stanga si dreapta
  23.  
  24.     maxProfit = stockPrices[r] - stockPrices[l];
  25.  
  26.     while(r - l > 0){
  27.  
  28.         if(stockPrices[r] - stockPrices[l] > maxProfit) //daca profitul de la pozitiile curente e mai mare decat cel maxim in actualizam
  29.             maxProfit = stockPrices[r] - stockPrices[l];
  30.  
  31.         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
  32.             l++;
  33.  
  34.         else r--; //altfel o alegem pe urmatoarea din dreapta
  35.  
  36.     }
  37.  
  38.     cout<<maxProfit;
  39.  
  40.     return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement