【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 ...
随机推荐
- js中substring或split方法取得URL中的域名
1.split方式 <html> <head></head> <body onload="convertTemp()"> <s ...
- XCode所有版本
You can find the DMGs for Xcode and other development tools onhttps://developer.apple.com/downloads/ ...
- H264 Decoder
http://www.cnblogs.com/mcodec/category/213433.html
- android一个上传图片的样例,包含怎样终止上传过程,假设在上传的时候更新进度条(一)
先上效果图: Layout为: <? xml version="1.0" encoding="utf-8"?> <LinearLayout x ...
- ubuntu安装软件的方式
ubuntu安装软件的方式: 通常的我们能够在ubuntu软件中心和新立得软件包管理器找到自己想要的软件,直接选择就能够自己主动下载并安装到电脑中,不想要的时候随时能够再从那里面卸载.这是第一种方法, ...
- crm操作安全字段
using System; using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Messages; /// <summary> ...
- C++设计模式之建造者模式(三)
4.引入钩子方法的建造者模式 建造者模式除了逐步构建一个复杂产品对象外.还能够通过Director类来更加精细地控制产品的创建过程.比如添加一类称之为钩子方法(HookMethod)的特殊方法来控制是 ...
- Effective C++:条款38:通过一个复杂的模具has-a要么“基于一些实现”
(一) public继承是"is-a"关联,"has-a"或"依据某物实现出(is-implemented-in-terms-of)"的意思 ...
- Conversion to Dalvik format failed: Unable to execute dex
最近莫名奇妙遇到“Conversion to Dalvik format failed: Unable to execute dex”错误,stackoverflow以后得到结果 把项目中classp ...
- Thinkphp常用标签
告:在使用下列所说的任何标签库都需要 HTML第一行加入 <tarlib name=”cx,html” /> 如果想单独引入cx标签库就直接写成<tarlib name=”cx” / ...