博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode By Python]122. Best Time to Buy and Sell Stock II
阅读量:4055 次
发布时间:2019-05-25

本文共 856 字,大约阅读时间需要 2 分钟。

题目:

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).

解释:

这题区别于121的地方在于,121只要求一次买入一次卖出,而122要求多次买入多次卖出,且交易不可重叠,故122是贪心算法

代码:

class Solution(object):    def maxProfit(self, prices):        """        :type prices: List[int]        :rtype: int        """        if len(prices)<=1:            return 0        maxprofit = 0        for i in range(1,len(prices)):            curprofit = prices[i]-prices[i-1]            if curprofit>0:                maxprofit+=curprofit        return maxprofita = Solution()print a.maxProfit([7, 6, 9, 3, 4])

转载地址:http://qzhci.baihongyu.com/

你可能感兴趣的文章
[关注大学生]李开复给中国计算机系大学生的7点建议
查看>>
[茶余饭后]10大毕业生必听得歌曲
查看>>
VC++ MFC SQL ADO数据库访问技术使用的基本步骤及方法
查看>>
VUE-Vue.js之$refs,父组件访问、修改子组件中 的数据
查看>>
Python自动化之pytest常用插件
查看>>
Python自动化之pytest框架使用详解
查看>>
【正则表达式】以个人的理解帮助大家认识正则表达式
查看>>
性能调优之iostat命令详解
查看>>
性能调优之iftop命令详解
查看>>
非关系型数据库(nosql)介绍
查看>>
移动端自动化测试-Windows-Android-Appium环境搭建
查看>>
Xpath使用方法
查看>>
移动端自动化测试-Mac-IOS-Appium环境搭建
查看>>
Selenium之前世今生
查看>>
Selenium-WebDriverApi接口详解
查看>>
Selenium-ActionChains Api接口详解
查看>>
Selenium-Switch与SelectApi接口详解
查看>>
Selenium-Css Selector使用方法
查看>>
Linux常用统计命令之wc
查看>>
测试必会之 Linux 三剑客之 sed
查看>>