Say you have an array for which the ith element is the price of a given stock on day i.

If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.

解题:

遍历数组,随时记录当前出现过的最小值,和出现过的最大收益;

代码:

 class Solution {
public:
int maxProfit(vector<int> &prices) {
int size = prices.size();
int minV = INT_MAX;
int maxP = ; for (int i = ; i < size; ++i) {
minV = min(minV, prices[i]);
maxP = max(maxP, prices[i] - minV);
}
return maxP;
}
};

最新文章

  1. [转]Patch文件结构详解
  2. matplotlib 和 pandas 两个包的安装
  3. 关于JNI程序中引用另外一个lib
  4. poj 2406Power Strings
  5. JavaEE开发之记事本完整案例(SpringBoot + iOS端)
  6. Java入门篇(二)——Java语言基础(下)
  7. IOS空数据页面,网络加载失败以及重新登陆View的封装(不需要继承)
  8. .NET应用程序管理服务AMS设计
  9. 在windows上搭建git server Gitblit
  10. jemter的使用
  11. Spark源码剖析 - SparkContext的初始化(八)_初始化管理器BlockManager
  12. Linux下IP SAN共享存储操作记录
  13. Vulkan入门流程
  14. iOS 设置 UIWebView UserAgent
  15. eclipse添加源码的另外一种方法
  16. Ex1—vlookup
  17. 在线升级java的版本
  18. MyBatis 事务源码分析
  19. makedown学习笔记(以后可能会用makedown写博客)
  20. ruby编码说明

热门文章

  1. APP的功能分类及打包与发布的分类方式
  2. 常用linux网络工具
  3. ServletRequestLister
  4. c++ 网络编程(三) LINUX/windows 进程间的通信原理与实现代码 基于多进程的服务端实现
  5. uwsgi错误invalid request block size
  6. 冀永楠:OCR的应用锦集及背后技术
  7. Dell解决黑苹果网卡(BCM94352ZAE/DW1560)怎么都打不开WiFi
  8. [android] socket在手机上的应用
  9. Redis整合spring总结
  10. springMVC介绍及配置