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 ...
随机推荐
- Redis 如何实现持久化
1.RDB 持久化,将 Redis 在内存中的的状态保存到硬盘中,相当于备份数据库状态. 2.AOF 持久化(Append-Only-File),AOF 持久化是通过保存 Redis 服务器锁执行的写 ...
- 第二周 数据分析之展示 Matplotlib库入门
Matplotlib库介绍:优秀的数据可视化第三方库 使用:Matplotlib库由各种可视化类构成,内部结构复杂,受Matlab启发,matplotlib.pyplot是绘制各类可视化图形的命令子库 ...
- 【easy】111. Minimum Depth of Binary Tree求二叉树的最小深度
求二叉树的最小深度: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; ...
- HDR拍照
HDR 拍照: (High Dynamic Range Imaging)高动态范围成像,是用来实现比普通数字图像技术更大曝光动态范围(即更大的明暗差别)的一组技术.高动态范围成像的目的就 ...
- js 中arguments,call,apply,bind的使用
//对于 arguments和this, 每个函数都有自己独有的arguments和this, 且不进行链式查找 //arguments是什么? //答:1:arguments是收到的实参副本 //2 ...
- ReSharper反编译C#类库
经常会在使用C#类中的某个函数时想了解其中具体的代码,可是F12转到定义后只能看到函数简单的声明, 看不到方法体中的代码,这挺让人沮丧的.. 如下: F12进入后显示的是元数据, Equals函数只能 ...
- c# 向数据库插数据超过1000条
今天想着如何大批量插入数据到数据库中 最开始的想法是使用insert循环插入 但每次都要创建数据库连接 这是非常消耗时间和消耗资源的事.后在网上看到了说使用SqlBulkCopy ,就看了看文档和别 ...
- python经典书籍推荐:python编码规范
目录INF-qa Python 编码规范................................................................................ ...
- 深入理解Java虚拟机阅读心得(二)
垃圾收集 程序计数器.虚拟机栈.本地方法栈三个区域随线程而生,随线程而灭:这几个区域的内存分配和回收都具备稳定性,不需要过多的考虑回收的问题.而Java堆和方法区则不一样. Java堆中存储了几乎所有 ...
- python 类和元类(metaclass)的理解和简单运用
(一) python中的类 首先这里讨论的python类,都基于继承于object的新式类进行讨论. 首先在python中,所有东西都是对象.这句话非常重要要理解元类我要重新来理解一下python中的 ...