题目要求

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

Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times).

Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).

题目分析及思路

给出一个数组,第i个元素是第i天的股票价格。需要设计一个算法找到最大的利润。可以进行多次交易,但必须是以一买一卖这样的顺序进行的,不可在再次买之前还未卖掉之前买的股票。可以遍历整个数组,若价格上升,则记录上升的差值。将所有差值求和就是最后的最大利润。

python代码

class Solution:

def maxProfit(self, prices: List[int]) -> int:

maxprofit = 0

for i in range(1,len(prices)):

if prices[i] > prices[i-1]:

maxprofit += prices[i] - prices[i-1]

return maxprofit

最新文章

  1. 斐波拉契数列(Fibonacci) 的python实现方式
  2. ubuntu host备份 ubuntu无法解析主机
  3. Redis教程(四):Hashes数据类型
  4. js判断是否为空火undefined是否给undefined加引号
  5. UTC时间与本地时间的相互转换
  6. linux shell 命令学习(4) cut - remove sections from each line of files
  7. java volatile进阶(一)
  8. iOS开发 UITableView之cell
  9. shell基础——变量定义
  10. BZOJ 3438: 小M的作物( 最小割 )
  11. ”Metro UI之磁贴(二)
  12. Android 中OKHttp请求数据get和post
  13. UIPopoverPresentationController使用
  14. Tomcat start.bat闪退:JRE_HOME环境变量配置不对
  15. 可视化&地图__公司收集
  16. 把DataTable转换为List<T>
  17. 配置Oracle访问SQL地理数据库
  18. 九度 1552 座位问题(递推DP)
  19. [转] SSH免密码登陆以及穿越跳板机
  20. 使用java代码将时间戳和时间互相转换

热门文章

  1. mysql 客户端连接报错Illegal mix of collations for operation
  2. Python-web应用 +HTTP协议 +web框架
  3. 等待activity出现(android特有的wait_activity)
  4. SQL SERVER 打开脚本报“未能完成操作,存储空间不足”
  5. Spring Boot MongoDB 查询操作 (BasicQuery ,BSON)
  6. MongoDB超级简明入门教程
  7. beta冲刺3/7
  8. Python学习(三十四)—— Django之ORM之单表、联表操作
  9. 用PHPExcel导出导入Excel
  10. Git permission denied(public key) 解决方法