-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path63_Max_Diff.cpp
More file actions
65 lines (47 loc) · 1.12 KB
/
63_Max_Diff.cpp
File metadata and controls
65 lines (47 loc) · 1.12 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//
// Created by mark on 2019/7/28.
// Copyright © 2019年 mark. All rights reserved.
//
/*
说明:
1. 问题:63. 股票的最大利润
2. 思路: 遍历,找出最大差值;往后找当前小值,不断往后找,求最大差值即可。
*/
#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <assert.h>
#include <cstdio>
#include <fstream>
#include <map>
#include <set>
#include <deque>
#include <algorithm>
#include <list>
using namespace std;
int maxDiff(vector<int> &nums)
{
if(nums.size() == 0)
return 0;
int min = nums[0];
int maxDiff = nums[1] - min;
for(int i = 2; i < nums.size(); i++)
{
if(nums[i-1] < min) // 更新min值
min = nums[i-1];
int curDiff = nums[i] - min; // 往后找最大差值,如果比maxdiff大, 则更新
if(curDiff > maxDiff)
maxDiff = curDiff;
}
return maxDiff;
}
int main()
{
vector<int> nums{9,11,8,5,7,12,16,14};
int maxD = maxDiff(nums);
cout << maxD << endl;
return 0;
}