|
| 1 | +#include <iostream> |
| 2 | +using namespace std; |
| 3 | +# include<map> |
| 4 | + |
| 5 | +class TradingSystem { |
| 6 | +private: |
| 7 | + map<float, int> sellMap; // increasing order |
| 8 | + map<float, int, greater<int>> buyMap; // decreasing order |
| 9 | + float totalProfit = 0.0; |
| 10 | + |
| 11 | +public: |
| 12 | + int buy(int num_of_product, float price) { |
| 13 | + int totalBuy = 0; |
| 14 | + // for (auto it = sellMap.begin(); it != sellMap.end();) { |
| 15 | + // float s_price = it->first; |
| 16 | + // int s_count = it->second; |
| 17 | + for (auto [s_price, s_count] : sellMap) { |
| 18 | + if (price >= s_price) { |
| 19 | + int remain = num_of_product - s_count; |
| 20 | + if (remain < 0) { |
| 21 | + totalProfit += (price-s_price) * num_of_product; |
| 22 | + sellMap[s_price] = -remain; |
| 23 | + totalBuy += num_of_product; |
| 24 | + num_of_product = 0; |
| 25 | + |
| 26 | + // break; |
| 27 | + } else { |
| 28 | + totalProfit += (price-s_price) * s_count; |
| 29 | + num_of_product = remain; |
| 30 | + sellMap.erase(s_price); |
| 31 | + // it = sellMap.erase(it); |
| 32 | + totalBuy += s_count; |
| 33 | + } |
| 34 | + } else { |
| 35 | + break; |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + if (num_of_product > 0) { |
| 40 | + buyMap[price] = num_of_product; |
| 41 | + } |
| 42 | + |
| 43 | + return totalBuy; |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | + int sell(int num_of_product, float price) { |
| 48 | + int totalSell = 0; |
| 49 | + // for (auto it = buyMap.begin(); it != buyMap.end();) { |
| 50 | + // float b_price = it->first; |
| 51 | + // int b_count = it->second; |
| 52 | + for (auto [b_price, b_count] : buyMap) { |
| 53 | + if (price <= b_price) { |
| 54 | + int remain = num_of_product - b_count; |
| 55 | + if (remain < 0) { |
| 56 | + totalProfit += (b_price-price) * num_of_product; |
| 57 | + buyMap[b_price] = -remain; |
| 58 | + num_of_product = 0; |
| 59 | + totalSell += num_of_product; |
| 60 | + |
| 61 | + // break; |
| 62 | + } else { |
| 63 | + totalProfit += (b_price-price) * b_count; |
| 64 | + num_of_product = remain; |
| 65 | + buyMap.erase(b_price); |
| 66 | + // it = buyMap.erase(it); |
| 67 | + totalSell += b_count; |
| 68 | + } |
| 69 | + } else { |
| 70 | + break; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + if (num_of_product > 0) { |
| 75 | + sellMap[price] = num_of_product; |
| 76 | + } |
| 77 | + |
| 78 | + return totalSell; |
| 79 | + } |
| 80 | + |
| 81 | + float get_profits() { |
| 82 | + return totalProfit; |
| 83 | + } |
| 84 | +}; |
| 85 | + |
| 86 | +// To execute C++, please define "int main()" |
| 87 | +int main() { |
| 88 | + |
| 89 | + TradingSystem system; |
| 90 | + cout << system.sell(50, 1.5) << endl; |
| 91 | + cout << system.sell(20, 1.4) << endl; |
| 92 | + cout << system.buy(60, 1.51) << endl; |
| 93 | + cout << system.get_profits() << endl; |
| 94 | + cout << system.buy(20, 1.5) << endl; |
| 95 | + cout << system.get_profits() << endl; |
| 96 | + cout << system.sell(20, 0.7) << endl; |
| 97 | + cout << system.buy(100, 0.6) << endl; |
| 98 | + cout << system.get_profits() << endl; |
| 99 | + |
| 100 | + return 0; |
| 101 | +} |
| 102 | + |
| 103 | + |
| 104 | +/* |
| 105 | +
|
| 106 | +Implement a trading system receiving incoming stream of trade orders. |
| 107 | +
|
| 108 | +buy(num_of_product, price) |
| 109 | +Called by buyer to buy certain number of products with a maximum price |
| 110 | +num_of_product (INTEGER): number of products to buy |
| 111 | +price (FLOAT): maximal price/unit the buyer is willing to pay |
| 112 | +Return (INTEGER): number of products can be bought |
| 113 | +
|
| 114 | +
|
| 115 | +sell(num_of_product, price) |
| 116 | +Called by seller to sell certain number of products with a minimum price |
| 117 | +num_of_product (INTEGER): number of products to sell |
| 118 | +price (FLOAT): minimal price/unit the seller is willing to sell |
| 119 | +Return (INTEGER): number of products can be sold |
| 120 | +
|
| 121 | +
|
| 122 | +we take price difference between sell and buy orders as profits. We wanted to maximize the profits. |
| 123 | +get_profits() |
| 124 | +Return (FLOAT): accumulated profits |
| 125 | +
|
| 126 | +
|
| 127 | +system = TradingSystem() |
| 128 | +system.sell(50, 1.5) |
| 129 | +return: 0 |
| 130 | +
|
| 131 | +system.sell(20, 1.4) |
| 132 | +return: 0 |
| 133 | +
|
| 134 | +system.buy(60, 1.51) |
| 135 | +return: 60 |
| 136 | +
|
| 137 | +system.get_profits() |
| 138 | +return: 2.6 60*1.51 - 20*1.4-40*1.5 = 2.6 |
| 139 | +
|
| 140 | +system.buy(20, 1.5) |
| 141 | +return: 10 (10,1.5) (sell) |
| 142 | +
|
| 143 | +system.get_profits() |
| 144 | +return: 2.6. |
| 145 | +
|
| 146 | +system.sell(20, 0.7) |
| 147 | +return: 10 20*0.7=14. (10 0.7) |
| 148 | +
|
| 149 | +system.buy(100, 0.6) |
| 150 | +return: 0. 0 |
| 151 | +
|
| 152 | +system.get_profits() |
| 153 | +return: 10.6 2.6+(1.5-0.7)*10 = `10.6 |
| 154 | +*/ |
| 155 | + |
| 156 | + |
| 157 | + |
| 158 | + |
| 159 | + |
| 160 | + |
| 161 | + |
| 162 | + |
0 commit comments