【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 ...
随机推荐
- 模仿《百度音乐HD》添加到下载框动画
上次听有人说喜欢<百度音乐HD>添加到下载动画 ,我就尝试模仿了下,没想到,今天code4app(地址)也有了这个,但是 这个动画基本相同,我们的思路还是部一样的. 都可以参考 .主要关键 ...
- 使用Python在2M内存中排序一百万个32位整数
译言网 | 使用Python在2M内存中排序一百万个32位整数 使用Python在2M内存中排序一百万个32位整数 译者:小鼠 发表时间:2008-11-13浏览量:6757评论数:2挑错数:0 作者 ...
- uva-211-The Domino Effect
http://uva.onlinejudge.org/external/2/211.html http://uva.onlinejudge.org/external/2/211.pdf 题意:每一种骨 ...
- JavaScript实战
JavaScript之单例实战 一.概述 所谓单例模式,顾名思义即一个类只有一个实例. 所以,当我们创建一个实例时,就必须判断其是否已经存在了这个实例,如果已经存在了这个实例,那么就返回这个已经存在的 ...
- OC -- 第一个类
OC -- 第一个类 类名:Car 属性:轮胎个数.时速 行为:跑 完整写一个类:类的声明和实现 1. 类的声明 代码: // NSObject 再Foundation框架中 #import & ...
- CrossBridge介绍
CrossBridge介绍 作者:chszs,转载需注明.博客主页: http://blog.csdn.net/chszs CrossBridge是Adobe FlasCC的开源版本,它提供了一个完整 ...
- Android的PackageManager的使用
Android系统提供了很多服务管理的类,包括ActivityManager.PowerManager(电源管理).AudioManager(音频管理)以及PackageManager管理类.Pack ...
- Java多线程使用场景
使用多线程就一定效率高吗? 有时候使用多线程并不是为了提高效率,而是使得CPU能够同时处理多个事件. 使用场景1 为什么了不阻塞主线程,启动其他线程来做耗时的事情. 比如app开发中耗时的操作都不在U ...
- WebApi异常
WebApi异常处理解决方案 前言:上篇C#进阶系列——WebApi接口传参不再困惑:传参详解介绍了WebApi参数的传递,这篇来看看WebApi里面异常的处理.关于异常处理,作为程序员的我们肯定 ...
- Linq 导出Excel
var d = db.User; Repeater1.DataSource = d.ToList(); Repeater1.DataBind(); string guid = Guid.NewGuid ...