LeetCode 122 Best Time to Buy and Sell Stock II 解题报告
题目要求
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
LeetCode 122 Best Time to Buy and Sell Stock II 解题报告的更多相关文章
- 【LeetCode】122.Best Time to Buy and Sell Stock II 解题报告(Java & Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 31. leetcode 122. Best Time to Buy and Sell Stock II
122. Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price ...
- [LeetCode] 122. Best Time to Buy and Sell Stock II 买卖股票的最佳时间 II
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- LeetCode: Best Time to Buy and Sell Stock II 解题报告
Best Time to Buy and Sell Stock IIQuestion SolutionSay you have an array for which the ith element i ...
- LeetCode 122. Best Time to Buy and Sell Stock II (stock problem)
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- LeetCode 122. Best Time to Buy and Sell Stock II (买卖股票的最好时机之二)
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- leetcode 122. Best Time to Buy and Sell Stock II ----- java
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- Java [Leetcode 122]Best Time to Buy and Sell Stock II
题目描述: Say you have an array for which the ith element is the price of a given stock on day i. Design ...
- [leetcode]122. Best Time to Buy and Sell Stock II 最佳炒股时机之二
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
随机推荐
- Django模型层-单表操作
ORM介绍 MVC或者MVC框架中包括一个重要的部分,就是ORM,它实现了数据模型与数据库的解耦,即数据模型的设计不需要依赖于特定的数据库,通过简单的配置就可以轻松更换数据库,这极大的减轻了开发人员的 ...
- [转载]解决在win10中webstrom无法使用命令行(Terminal)
转载地址:https://qiaolevip.iteye.com/blog/2217688 原因:计算机从win7更新到win10,webstorm9命令框无法输入,以为是webstorm问题和win ...
- Mysql基本架构及查询流程
mysql体系结构简单概述: Connectors:接入方,支持协议很多 Management Serveices & Utilities:系统管理和控制工具例如:备份恢复,mysql复制集群 ...
- 《剑指offer》连续子数组的最大和
本题来自<剑指offer> 反转链表 题目: 思路: C++ Code: Python Code: 总结:
- 动态加载机Servlet容器加载器
动态加载是Servlet 3.0中的新特性,它可以实现在 不重启Web应用的情况下加载新的Web对象(Servlet. Filter.Listener).Servlet容器加载器也是Servlet 3 ...
- sqlserver(查看被锁进程)
-- ###### 查看被锁进程 ###### select 标志, 进程ID=spid,线程ID=kpid,块进程ID=blocked,数据库ID=dbid, 数据库名=db_name(dbid), ...
- fillder--修改返回数据
fillder面板中抓到想要的URL后: ①.在需要修改的url---右键------UNclocking For Editing(解除编辑功能) ②.承接上步,在数据结果的TextView模式下,返 ...
- Java基础知识及学习规划【图】
结构图:
- Centos下安装配置Mongodb3.6
首先更新系统 yum -y update 1.安装Mongodb 编辑Mongodb安装源 vim /etc/yum.repos.d/mongodb-org-3.6.repo 编辑内容如下: [mon ...
- Linux下Apache配置HTTPS功能
Apache配置HTTPS功能 转 https://www.cnblogs.com/liaojiafa/p/6028816.html 一.yum 安装openssl和openssl-devel,ht ...