题解 | #买卖股票的最好时机(一)# | C++

买卖股票的最好时机(一)

https://www.nowcoder.com/practice/351b87e53d0d44928f4de9b6217d36bb

#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;

class Soultion {
 private:
  std::vector<int> prices_;
 public:
  explicit Soultion(std::vector<int>&& prices) {
    prices_ = std::forward<std::vector<int>>(prices);
  }
  int getMaxProfit() {
    int max_profit = 0;
    int min_prices = prices_[0];
    for (auto p : prices_) {
        max_profit = std::max(max_profit, p - min_prices);
        min_prices = std::min(min_prices, p);
    }
    return max_profit;
  }
};

int main() {
    int n;
    std::cin >> n;
    std::vector<int> prices;
    while (n-- >= 1) {
        int p;
        std::cin >> p;
        prices.push_back(p);
    }
    std::cout << Soultion(std::move(prices)).getMaxProfit();
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务