我也是杭电的 昨天恒生实习笔试 最后一道算法题好TM难 当时根本没写出来 然后今天复盘了一下,测了几个示例感觉没什么问题 class Solution{ public: double getMaxProfit(double M, int N, vector<double>&historyPrice, int K){ vector<pair<double, double>> buy(K, pair<double, double>{INT_MIN, 0}); vector<double> sell(K + 1); buy[0].first = int(M) / int(historyPrice[0]); buy[0].second = M - buy[0].first * historyPrice[0]; sell[0] = M; for (int i = 1; i < N; i++){ for (int j = K - 1; j >= 0; j--){ pair<double, double> now; now.first = int(sell[j]) / int(historyPrice[i]); now.second = sell[j] - now.first * historyPrice[i]; buy[j] = now.first > buy[j].first ? now : buy[j]; sell[j + 1] = max(sell[j + 1], buy[j].second + historyPrice[i] * buy[j].first); } } return *max_element(sell.begin(), sell.end()) - M; } };
点赞 评论

相关推荐

05-01 22:41
中南大学 Java
点赞 评论 收藏
分享
牛客网
牛客企业服务