The maximum profit offer- face questions 63- to prove safety stock - Array

/*
topic:
    Given a sequence of stock, a transaction seeking the maximum profit.
*/
#include<iostream>
#include<vector>

using namespace std;

int MaxProfit(vector<int> numbers){
    int length = numbers.size();
    if(length < 2) return 0;
    int minVal = numbers[0];
    int maxProfit = 0;
    for(int i = 1; i < length; i++){
        if(numbers[i] < minVal){
            minVal = numbers[i];
        }else{
            maxProfit = max(maxProfit,numbers[i]-minVal);
        }
    }
    return maxProfit;
}

int main () {
    vector<int> a ={9,11,8,5,7,12,16,14};
    cout<<MaxProfit(a);
}

  

Guess you like

Origin www.cnblogs.com/buaaZhhx/p/12131024.html