题目地址:Best Time to Buy and Sell Stock II - LeetCode


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

Example 1:

Input: [7,1,5,3,6,4]
Output: 7
Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.

Example 2:

Input: [1,2,3,4,5]
Output: 4Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.Note that you cannot buy on day 1, buy on day 2 and sell them later, as you areengaging multiple transactions at the same time. You must sell before buying again.

Example 3:

Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.

这道题目可以买卖多次,用贪心就可以解出来了。
Python解法如下:

class Solution:def maxProfit(self, prices: List[int]) -> int:hold=0profit=0now=-1for i in prices:if now==-1:now=ielif i<now:now=ielif i>now:profit+=i-nownow=ireturn profit

Java解法如下:

class Solution {public int maxProfit(int[] prices) {int i = 0;if (prices.length==0){return 0;}int valley = prices[0];int peak = prices[0];int maxprofit = 0;while (i < prices.length - 1) {while (i < prices.length - 1 && prices[i] >= prices[i + 1])i++;valley = prices[i];while (i < prices.length - 1 && prices[i] <= prices[i + 1])i++;peak = prices[i];maxprofit += peak - valley;}return maxprofit;}
}

C++解法如下:

class Solution {public:int maxProfit(vector<int>& prices) {int len=prices.size();int profit=0;if(len==0||len==1)return 0;for(int i=0;i<len-1;i++){if(prices[i]<prices[i+1])profit+=prices[i+1]-prices[i];}return profit;}
};

LeetCode 122. Best Time to Buy and Sell Stock II--贪心--Java,C++,Python解法相关推荐

  1. 【贪心 和 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: ...

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

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

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

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

  4. 【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 ...

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

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

  6. leetcode python3 简单题122. Best Time to Buy and Sell Stock II

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

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

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

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

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

  9. LeetCode OJ - Best Time to Buy and Sell Stock II

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/xiezhihua120/article/details/33817395 Say you have ...

最新文章

  1. 科大星云诗社动态20210416
  2. hutool 读取扩展名文件_用卓语言实现中文编程显示隐藏文件扩展名
  3. codeforces 85D. Sum of Medians
  4. 今日恐慌与贪婪指数为78 贪婪程度有所上升
  5. c 语言编写脚本优化,两周自制脚本语言-第11天 优化变量读写性能
  6. android 获取系统所有安装的应用程序
  7. 李洪强iOS开发之OC[011] - 有参方法的声明实现以及调用练习
  8. 客户端,从服务器上下载文件流程
  9. super是不是java中的标识符_java关键字super和this是什么?有什么区别?
  10. Python图片批量自动抠图去背景
  11. 一流管理者,都不会选“先做再说”
  12. 【§炫彩苹果win7主题§】
  13. java的public_java中public用法
  14. linux开机启动动画
  15. 关闭应用右下角的盾牌图标
  16. 模糊的图片怎么变清晰?
  17. bpsk的matlab仿真,用MATLAB仿真BPSK调制
  18. DW1000 学习笔记1 - 概述简报
  19. 安全的即时沟通软件主要表现在哪些方面
  20. 千万级用户的Android客户端是如何养成的

热门文章

  1. Python自然语言处理 NLTK 库用法入门教程
  2. Logistic Regression
  3. python如何自定义函数_python基础之函数(自定义函数)
  4. PostgreSQL 常用命令
  5. R语言绘制堆叠条形图
  6. iMeta:已被谷歌学术(Google Scholar)收录
  7. STE:环境菌群代谢产物可预防炎症症状
  8. Nature:功能微生物组研究典范—采用甘露糖苷选择性抑制尿路致病性大肠杆菌...
  9. 宏基因组实战4. 基因注释Prokka
  10. 宏基因组公众号7月精选文章目录