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 ...
随机推荐
- 【原创】大数据基础之Zookeeper(4)应用场景
1 集群配置管理 应用服务器的配置通常会放到properties文件中,格式为: system1.module2.prop3=value4 然后启动的时候加载,这样带来的问题是启动后无法修改,想修改必 ...
- numpy数据去重
import numpy as npx = np.array([1,2,3,2,4,2,5,7,7])print(np.unique(x)) 简单散点图的绘制,没有显示出文字是因为需要下载个插件
- springboot配置详解
springboot配置详解 Author:SimpleWu properteis文件属性参考大全 springboot默认加载配置 SpringBoot使用两种全局的配置文件,全局配置文件可以对一些 ...
- python 列表 元组 字符串
列表添加: list.append() list.extend() list.insert() 列表删除: list.remove() #删除某一个元素 list.pop() #删除某一个返回删 ...
- Android 后台应用保活、消息推送
3.针对以往Android版本的各种保活技术回顾 Android P之前为了搞定客户的投诉:“为什么微信能收到消息而你们的IM却不能?”,为了解决这个“痛点”,广大的Android开发者们只能让各种黑 ...
- Invalid tld file: "/WEB-INF/tags/xxxt.tld", see JSP 2.2 specification section 7.3.1 for more details
错误描述 在jsp页面引入了自定义的TLD文件的时候,碰到了一个错误 Invalid tld file: "/WEB-INF/tags/xxxt.tld", see JSP 2.2 ...
- pandas处理丢失数据-【老鱼学pandas】
假设我们的数据集中有缺失值,该如何进行处理呢? 丢弃缺失值的行或列 首先我们定义了数据集的缺失值: import pandas as pd import numpy as np dates = pd. ...
- BZOJ3257 [Zjoi2014]力 多项式 FFT
原文链接http://www.cnblogs.com/zhouzhendong/p/8762639.html 题目传送门 - BZOJ3527 题意 给出长度为$m$的序列$q_{1..m}$,让你输 ...
- jmeter之JDBC Request各种数据库配置
URL和JDBC驱动: Datebase Driver class Database URL MySQL com.mysql.jdbc.Driver jdbc:mysql://host:port/{d ...
- Chapter_9 DP : uva1347 tour (bitonic tour)
https://cn.vjudge.net/problem/UVA-1347 这道题居然可以O(n^2)解决, 让我太吃惊了!!! 鄙人见识浅薄, 这其实是一个经典问题: bitonic tour. ...