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

1:遍历数组;2:每个数字获得该数字之前的最小的数字,并与当时保存的最大值相比較

    int maxProfit(vector<int> &prices)
{
if(prices.size() <= 1)
{
return 0;
} int maxValue = 0;
int minPrice = prices[0];
int size = (int)prices.size(); for(int i = 1; i < size; i++)
{
if(prices[i] > minPrice)
{
maxValue = (maxValue > prices[i] - minPrice ? maxValue : prices[i] - minPrice);
}
else
{
minPrice = prices[i];
}
} return maxValue;
}

最新文章

  1. Paxos
  2. H5案例分享:移动端touch事件判断滑屏手势的方向
  3. JQuery基础一
  4. iframe页面调用父窗口JS函数
  5. c#中如何不通过后台直接用js筛选gridview中的数据条件筛选查询?
  6. 详细解析: VictorOps 是如何利用和完善 ChatOps?
  7. ORACLE的sign函数和DECODE函数
  8. C#中的switch case
  9. BS常用方法备忘
  10. ZOJ1654 Place the Robots
  11. 实现PHP服务端和c#客户端数据交换
  12. DataReader分页性能测试
  13. 文件-- 字节相互转换(word、图片、pdf...)
  14. oracle将unix 时间戳转换为date类型
  15. asp.net 增加404页面(非302、200)
  16. 2018.09.01 hdu4405 Aeroplane chess (期望dp)
  17. 使用jQuery的ajax同步请求吃过的亏
  18. Apache .htaccess文件
  19. Android滑动冲突解决方法
  20. MyBatis动态代理查询出错

热门文章

  1. sehlle脚本获取linux服务器基本信息
  2. Less功能特性
  3. Vue+Bootstrap实现购物车程序(1)
  4. org.mybatis.spring.transaction.SpringManagedTransaction - JDBC Connection [********] will not be managed by Spring
  5. 全国绿色计算大赛 模拟赛第二阶段 (Python)
  6. myBatis查询报错 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near
  7. 【memcached】memcached中flags字段的作用
  8. 51NOD 2370 奈芙莲的护符
  9. Python实现图片切割
  10. winfrom Panel 问题