Advertisement
knakul853

Untitled

Oct 30th, 2020
2,239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.13 KB | None | 0 0
  1. Profit Maximization:
  2. int ans = 0;
  3.     int buy = v[0];
  4.     for (int i = 1; i < n; i++)
  5.     {
  6.         ans = max(ans, v[i] - buy);
  7.         buy = min(buy, v[i]);
  8.     }
  9.  
  10.     return and;
  11.  
  12. Profit Maximization - Extended
  13.  int ans = 0;
  14.     int buy = v[0];
  15.     for (int i = 1; i < n; i++)
  16.     {
  17.         if(v[i] > v[i-1])ans += v[i]-v[i-1];
  18.     }
  19.  
  20. return and;
  21.  
  22.  
  23. Profit Model for John:
  24. string ans;
  25.  
  26.     for (int test = 0; test < d; test++){
  27.         int target = profit[test];
  28.  
  29.         int f = 1;
  30.         string res;
  31.         int id1 = 0, id2 = 100000000;
  32.         for (int i = 0; i < n; i++)
  33.         {
  34.             for (int j = i+1; j < n ; j++){
  35.  
  36.                 if(price[j] - price[i] == target){
  37.                     if(id2 - id1 > j - i){
  38.                     res = to_string(i + 1) + ' ' + to_string(j + 1);
  39.                     f = 0;
  40.                     id1 = i;
  41.                     id2 = j;
  42.                     }
  43.                 }
  44.             }
  45.         }
  46.  
  47.         if(f)
  48.             res = to_string(-1);
  49.         if(ans.size())
  50.             ans += ',' + res;
  51.         else
  52.             ans = res;
  53.     }
  54.  
  55.     return ans;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement