-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathByteDance.h
More file actions
96 lines (80 loc) · 1.52 KB
/
ByteDance.h
File metadata and controls
96 lines (80 loc) · 1.52 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#pragma once
#include <vector>
#include <algorithm>
#include <iostream>
class CBDSolution
{
public:
CBDSolution();
~CBDSolution();
public:
int CalculateKole(std::vector<int>& listVal);
private:
};
CBDSolution::CBDSolution()
{
}
CBDSolution::~CBDSolution()
{
}
int CBDSolution::CalculateKole(std::vector<int>& listVal)
{
int start = 0;
int end = 0;
int nMax = 0;
int result = 0;
std::pair<int, int> left = std::make_pair(0, listVal[0]);
std::pair<int, int> right = std::make_pair(0, listVal[0]);
int pos = 1;
while (pos < listVal.size())
{
for (auto i = pos; i < listVal.size(); ++i)
{
if (left.second > listVal[pos])
{
if (right.second <= listVal[pos])
{
right.first = pos;
right.second = listVal[pos];
}
}
else
{
right.first = i;
right.second = listVal[pos];
break;
}
}
for (auto i = left.first + 1; i < right.first; i++)
result += std::min(left.second, right.second) - listVal[i];
left = right;
pos = right.first + 1;
}
return result;
//
// while(start < listVal.size() - 1)
// {
// int nMax = 0;
// int val = 0;
// int pos = start + 1;
// for (auto i = start + 1; i < listVal.size(); i++)
// {
// if (listVal[start] <= listVal[i])
// {
// pos = i;
// val = listVal[i];
// break;
// }
//
// if (listVal[i] > listVal[start])
// {
// //TODO start 最大值, 找出其右半部分最大值。
// }
// }
//
// for (pos = pos - 1; pos > start; pos--)
// result += val - listVal[pos];
//
// start = pos;
// }
}