Description:

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.

只允许买卖一次,求最大利润。

动态规划,从后向前找最大股价,减去当前股价,求利润,找最大利润。O(n).

public class Solution {
public int maxProfit(int[] prices) { int len = prices.length;
if(len == 0) return 0;
int highestPrice = prices[len - 1];
int res = 0;
for(int i=len-1; i>=0; i--) {
highestPrice = max(highestPrice, prices[i]);
res = max(res, highestPrice-prices[i]);
} return res;
} int max(int a, int b) {
return a > b ? a : b;
}
}

最新文章

  1. ununtu设置开机启动服务-手工将Tomcat设为自启动服务
  2. Linux图片批处理
  3. 低功耗蓝牙4.0BLE编程-nrf51822开发(11)-蓝牙串口代码分析
  4. python_way day17 jQuery表单验证,事件绑定,插件,文本框架,正则表达式
  5. Angular 中得 scope 作用域梳理
  6. Ehcache详细解读(转)
  7. PAT 1004. Counting Leaves (30)
  8. 常见ORACLE错误,及解决方案(遇则即时更新)
  9. Arcgis API for Android之GPS定位
  10. LIS小结(O(∩_∩)O~哄哄)
  11. TOMCAT-publishing to tomcat v7.0 server at
  12. ajax_get/post_两级联动
  13. visual studio 2012打开提示 未能将网站×××配置为使用 ASP.NET 4.5 和 尚未在Web服务器上注册,您需要手动将Web服务器配置为使用ASP.NET 4.5
  14. windows下 gvim8.0 编译器配置
  15. Android中View的绘制流程(专题讲解)
  16. [luogu P3065] [USACO12DEC]第一!First!
  17. js变量的解构赋值
  18. Win server 2012 +IIS8.0下安装SSL证书
  19. cout关闭输出缓冲,调试用
  20. 【css】zSass - 用 sass 编写 css

热门文章

  1. Python3分别将list、numpy数组内容写入txt文件中
  2. kill -HUP pid 更改配置后不重新启动服务,动态更新配置文件
  3. WPF教程五:布局之Canvas面板
  4. CodeIgniter(3.1.4)框架中成功/错误跳转
  5. Windows与Linux下进程间通信技术比较
  6. Qt 线程基础(QThread、QtConcurrent等)
  7. python扩展
  8. 机器学习性能评估指标(精确率、召回率、ROC、AUC)
  9. 安装cx_Oracle 遇到的杂项问题
  10. e1087. try/catch语句