1.编辑器

我使用的是win10+vscode+leetcode+python3
环境配置参见我的博客:
链接

2.第一百二十二题

(1)题目
英文:
Say you have an array prices 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 天的价格。

设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易(多次买卖一支股票)。

注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock

(2)解法
① 动态规划
(耗时:92ms,内存:15M)

class Solution:def maxProfit(self, prices: List[int]) -> int:res = 0for i in range(1,len(prices)):if prices[i]>prices[i-1]:res += (prices[i]-prices[i-1])return res

注意:
1.这里没有显示的动态规划,公式为:

dp[i] = dp[i-1] if prices[i]<=prices[i-1] else dp[i-1]+(prices[i]-prices[i-1])

② 遍历
(耗时:84ms,内存:15.1M)

class Solution:def maxProfit(self, prices: List[int]) -> int:target = 0n = len(prices)i = 0while i<n-1:while i<n-1:if prices[i+1]<prices[i]:i += 1else:breakj = i+1while j<n-1:if prices[j+1]>prices[j]:j+=1else:breakif j>n-1:breaktarget += (prices[j]-prices[i])i = j+1return target

leetcode python3 简单题122. Best Time to Buy and Sell Stock II相关推荐

  1. leetcode python3 简单题121. Best Time to Buy and Sell Stock

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百二十一题 (1)题目 英文: Say you have an array fo ...

  2. LeetCode之路:122. Best Time to Buy and Sell Stock II

    一.引言 这是一道非常有趣的题目! 这是一道非常有趣的题目!! 这是一道非常有趣的题目!!! 重要的事情先说三遍 : ) 好了,接下来让我们看看这道题: Say you have an array f ...

  3. [LeetCode]122. Best Time to Buy and Sell Stock II

    [LeetCode]122. Best Time to Buy and Sell Stock II 题目描述 思路 I的后续 将数组分为几个小部分, 划分标准是 [i] < [i - 1](划分 ...

  4. 【贪心 和 DP + 卖股票】LeetCode 122. Best Time to Buy and Sell Stock II

    LeetCode 122. Best Time to Buy and Sell Stock II Solution1:我的答案 贪心和DP傻傻分不清! class Solution { public: ...

  5. 【Leetcode】122. Best Time to Buy and Sell Stock II买卖股票的最佳时机 II

    Best Time to Buy and Sell Stock II 买卖股票的最佳时机 II买卖股票的最佳时机 II Say you have an array for which the ith ...

  6. 【leetcode-Python】-Dynamic Programming -122. Best Time to Buy and Sell Stock II

    目录 题目链接 题目描述 示例 解题思路 Python实现 时间复杂度与空间复杂度 Python实现·优化空间复杂度 时间复杂度与空间复杂度 解题思路二·贪心算法 Python实现 时间复杂度与空间复 ...

  7. LeetCode 122 Best Time to Buy and Sell Stock II(股票买入卖出的最佳时间 II)

    翻译 话说你有一个数组,其中第i个元素表示第i天的股票价格.设计一个算法以找到最大利润.你可以尽可能多的进行交易(例如,多次买入卖出股票).然而,你不能在同一时间来多次交易.(例如,你必须在下一次买入 ...

  8. 122. Best Time to Buy and Sell Stock II

    一.题目 1.审题 2.分析 给出一个股票的每日价格的数组,可以多次交易,求最大利润是多大. 二.解答 1.思路: 方法一. 求最大利润,从后往前看,若以现在的价格卖出,前一天的价格买入的话,即可完成 ...

  9. 122 Best Time to Buy and Sell Stock II 买卖股票的最佳时机 II

    假设有一个数组,它的第 i 个元素是一个给定的股票在第 i 天的价格. 设计一个算法来找到最大的利润.你可以完成尽可能多的交易(多次买卖股票).然而,你不能同时参与多个交易(你必须在再次购买前出售股票 ...

最新文章

  1. [C#]无边框窗体缩放的虚线框怎么实现
  2. Start Activity for Result(Bug)
  3. LIKE语句也可以这样写
  4. java最少有多少线程_【并发编程】一个最简单的Java程序有多少线程?
  5. 白板机器学习笔记 P60-P65 EM算法
  6. python接口自动化(二)--什么是接口测试、为什么要做接口测试(详解)
  7. VMwar配置静态ip
  8. 图神经网络总结(GCN/GAT/GraphSAGE/DeepWalk/TransE)
  9. Windows server 2016 安装oracle
  10. qq令牌64位密钥提取_令牌QQ号代码64位数字+字母只截图保存可转换文字再来获取口令...
  11. IDEA使用教程之创建一个工程(一)
  12. php只取时间的下士_php取当时的年月日时分秒毫秒
  13. catalog英文翻译_Catalog.是什么意思
  14. 【2】Kubernetes集群安装
  15. 文件分类服务器,云服务器 搭建 文件分类存储
  16. vlan的三层交换连通
  17. php 不要平铺的代码,css怎么设置图片不平铺
  18. 一个技术主管/团队Leader的工作职责有哪些?
  19. tokuDB 使用详解及其注意事项(2)
  20. 安装tushare时报错:No module named 'lxml'的解决方法

热门文章

  1. 台达ms300变频器使用手册中文_台达变频器:满足未来驱动需求
  2. windows下客户端连接上马上会断开连接_Tunneller VS DCOM,稳定性更高的连接!
  3. postman json 中写注释_Swagger界面丑、功能弱怎么破?用Postman增强下就给力了!
  4. 一个软件公司需要多少前端_内幕!软件外包公司开发一个软件需要多少钱?
  5. 如何在html嵌入html网页
  6. java中的全等和相似
  7. 提升访问github.com网址的速度
  8. 机器学习-多元梯度下降
  9. MQTT 控制报文 - SUBSCRIBE订阅报文,SUBACK,UNSUBSCRIBE,UNSUBACK - 第5章
  10. 剑指offer——29.顺时针打印矩阵(不熟)