-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem_1475_finalPrices.cc
67 lines (59 loc) · 1.27 KB
/
Problem_1475_finalPrices.cc
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
#include <iostream>
#include <vector>
#include "UnitTest.h"
using namespace std;
class Solution
{
public:
vector<int> finalPrices(vector<int> &prices)
{
vector<int> stack;
vector<int> ans(prices.size());
for (int i = 0; i < prices.size(); i++)
{
// 单调栈
while (!stack.empty() && prices[stack.back()] >= prices[i])
{
ans[stack.back()] = prices[stack.back()] - prices[i];
stack.pop_back();
}
stack.push_back(i);
ans[i] = prices[i];
}
return ans;
}
};
bool isVectorEqual(vector<int> lhs, vector<int> rhs)
{
if (lhs.size() != rhs.size())
{
return false;
}
for (int i = 0; i < lhs.size(); i++)
{
if (lhs[i] != rhs[i])
{
return false;
}
}
return true;
}
void testFinalPrices()
{
Solution s;
vector<int> prices1 = {8, 4, 6, 2, 3};
vector<int> fin1 = {4, 2, 4, 2, 3};
vector<int> prices2 = {1, 2, 3, 4, 5};
vector<int> fin2 = {1, 2, 3, 4, 5};
vector<int> prices3 = {10, 1, 1, 6};
vector<int> fin3 = {9, 0, 1, 6};
EXPECT_TRUE(isVectorEqual(s.finalPrices(prices1), fin1));
EXPECT_TRUE(isVectorEqual(s.finalPrices(prices2), fin2));
EXPECT_TRUE(isVectorEqual(s.finalPrices(prices3), fin3));
EXPECT_SUMMARY;
}
int main()
{
testFinalPrices();
return 0;
}