【LeetCode】【Python解决问题的方法】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 (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock
before you buy again).
解释题意:一个数组储存着一仅仅股票的价格走势。第i个元素就能够看做是该股票第i天的价格,你能够买卖股票,无需考虑手上的钱够买多少股,题目意思是能够随便买卖。但要买就买一股,不卖出这一股是无法再次买入的。这题非常easy。第二天比今天价格高了就买入,低了就卖出就可以
思路:遍历数组,计算全部第二天比前一天价格高的值,加起来就是最后的最大收益。
class Solution:
# @param prices, a list of integer
# @return an integer
def maxProfit(self, prices):
profit = 0
length = len(prices)
for i in range(0,length-1):
if prices[i+1] > prices[i]:
profit += prices[i+1] - prices[i]
return profit
版权声明:本文博主原创文章。博客,未经同意不得转载。
【LeetCode】【Python解决问题的方法】Best Time to Buy and Sell Stock II的更多相关文章
- 【leetcode刷题笔记】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】【Medium】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 —— Best Time to Buy and Sell Stock II [贪心算法]
Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a ...
- Algorithm - 贪心算法使用场景 ( LEETCODE —— Best Time to Buy and Sell Stock II)
先看一道leetcode题: Best Time to Buy and Sell Stock II Say you have an array for which the ith element is ...
- Leetcode之动态规划(DP)专题-122. 买卖股票的最佳时机 II(Best Time to Buy and Sell Stock II)
Leetcode之动态规划(DP)专题-122. 买卖股票的最佳时机 II(Best Time to Buy and Sell Stock II) 股票问题: 121. 买卖股票的最佳时机 122. ...
- [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 买股票的最佳时间之二
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
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Say you ...
- 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 ...
- 【LeetCode OJ】Best Time to Buy and Sell Stock II
Problem Link: http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ We solve this prob ...
随机推荐
- Button UI Kit CSS3美丽Buttonbutton
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...
- Function 详解(一)
一直想写一系列关于javascript的东西,可惜从申请博客以来就一直抽不出时间来好好写上一番,今天终于熬到周末,是该好好整理一下,那么先从声明函数开始吧; 总所周知,在javascript中有匿名函 ...
- kill命令"-1"这个参数到底是杀进程还是reload?(转)
kill-1:重新读取一次参数的配置文件 (类似 reload) 这句话给我的感觉是把进程杀掉后重启进程,即 reload.而我查了下 man kill,-1 对应的 signal 是 SIGHUP, ...
- OCP读书笔记(1) - Oracle核心概念和工具
ohasdoracle high available service daemon OEMweb -- Database Control资料库 -- sysman Starting Oracle Re ...
- python - ImportError: No module named http.cookies error when installing cherrypy 3.2 - Stack Overflow
python - ImportError: No module named http.cookies error when installing cherrypy 3.2 - Stack Overfl ...
- iOS文件保存策略
Where You Should Put Your App’s Files To prevent the syncing and backup processes on iOS devices fro ...
- 初始化openwrt的rootpassword
更改openwrt源代码 shadow 文件 package/base-files/files/etc/shadow shadow 文件參考http://blog.csdn.net/u01164188 ...
- switch case语句里面不能定义对象,有语法错误,除非加一个花括号
最近发现一个问题呢 发现在switch的case里面不能去定义对象了,一定义对象就会报错了 仔细了解了一下在C或者C++中,只要是在任何一对花括号 “{ }”中定义的对象,那么该对象的作用域就局限在这 ...
- Android自己定义组件系列【2】——Scroller类
在上一篇中介绍了View类的scrollTo和scrollBy两个方法,对这两个方法不太了解的朋友能够先看<自己定义View及ViewGroup> scrollTo和scrollBy尽管实 ...
- C# 开发Chrome内核浏览器(WebKit.net)
原文地址:http://www.cnblogs.com/linyijia/p/4045333.html