-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
32 lines (31 loc) · 781 Bytes
/
Copy pathmain.cpp
File metadata and controls
32 lines (31 loc) · 781 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//
// main.cpp
// leetcode122
//
// Created by 萧天牧 on 17/5/10.
// Copyright © 2017年 萧天牧. All rights reserved.
//
#include <iostream>
#include <vector>
using namespace std;
int maxProfit(vector<int>& prices) {
if(prices.size() == 0)
return 0;
int res = 0;
int minpos = 0, maxpos = 0;
for(int i = 1; i < prices.size(); i++){
if(prices[i] < prices[i - 1] ){
maxpos = i - 1;
res += prices[maxpos] - prices[minpos];
minpos = i;
}
}
if(prices[prices.size() - 1] > prices[minpos])
res += prices[prices.size() - 1] - prices[minpos];
return res;
}
int main(int argc, const char * argv[]) {
vector<int> a = {7, 6, 4, 3, 1};
std::cout << maxProfit(a);
return 0;
}