LeetCode Array Easy 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 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: [,,,,,]
Output:
Explanation: Buy on day (price = ) and sell on day (price = ), profit = - = .
Then buy on day (price = ) and sell on day (price = ), profit = - = .Example 2:
Input: [,,,,]
Output:
Explanation: Buy on day (price = ) and sell on day (price = ), profit = - = .
Note that you cannot buy on day , buy on day and sell them later, as you are
engaging multiple transactions at the same time. You must sell before buying again.Example 3:
Input: [,,,,]
Output:
Explanation: In this case, no transaction is done, i.e. max profit = .
问题描述: 给定一个数组,每一位代表当前索引的价格。设计一个算法,计算出最大利润。可以多次买卖。但是买卖不能在同一天操作
思路:这一题用到了贪心的思想。通过获取局部局部最优来达到整体最优解。找到递减区间的最低点,及最后一个点,同时再找到递增区间的最高点,也是最后一个点。把他们的差值累加,获得最大利润
public static int MaxProfit2(int[] prices)
{
int len = prices.Length;
int total = ;
int i = ;
while(i < len - )
{
int sell =, buy = ;
while (i + < len && prices[i + ] < prices[i])//获取递减区最低点(局部最小)
i++;
buy = i;//将最小点作为买入点
i++;//找下一个点,卖出点至少是买入点的下一个点
while (i < len && prices[i] > prices[i - ])//获取递增区最高点(局部最大)
i++;
sell = i - ;//获取卖出点
total += prices[sell] - prices[buy];
}
return total;
}
想法来源: https://www.cnblogs.com/grandyang/p/4280803.html
LeetCode Array Easy 122. Best Time to Buy and Sell Stock II的更多相关文章
- [LeetCode&Python] Problem 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 al ...
- 31. leetcode 122. Best Time to Buy and Sell Stock II
122. Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price ...
- leetcode 121. Best Time to Buy and Sell Stock 、122.Best Time to Buy and Sell Stock II 、309. Best Time to Buy and Sell Stock with Cooldown
121. Best Time to Buy and Sell Stock 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...
- leetcode:122. Best Time to Buy and Sell Stock II(java)解答
转载请注明出处:z_zhaojun的博客 原文地址 题目地址 Best Time to Buy and Sell Stock II Say you have an array for which th ...
- 【刷题-LeetCode】122 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 ...
- 122. Best Time to Buy and Sell Stock II@python
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 买卖股票的最佳时间 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 (stock problem)
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
随机推荐
- Linux上用sublime编辑Python时候出现"SyntaxError: Non-ASCII character ‘\xe5′ in file"的问题
Reopen with Encoding UTF-8也没用,那只是在编辑界面里面显示出中文而已,编译的时候还是不能识别中文. 所以,还是按网上说的吧,在每一个有中文的文件第一行加上# -*- codi ...
- JS中有趣的知识
1.分号与换行 function fn1(){ return { name: 'javascript' }; } function fn2(){ return { name: 'javascript' ...
- 十五、API请求接口-远程服务器返回错误: (400) 错误的请求错误
一.远程服务器返回错误: (400) 错误的请求错误 捕获异常查看具体错误 using Newtonsoft.Json; using System; using System.Collections. ...
- linux随笔-06
用户身份与文件权限 用户身份与能力 Linux系统的管理员之所以是root,并不是因为它的名字叫root,而是因为该用户的身份号码即UID(User IDentification)的数值为0. 在Li ...
- 如何将excel表格中的纯数字删掉 空白行,然后删除
excel里如何删除一列中全部数字 1.选择目标区域2.按“F5”,定位条件如下设置,即可选中值为数字的单元格,在按Del. 空白行删除 选择空值,确定,就会将所有空白行选定,然后删除
- JS数字转字符串的方法(number to string)
一.双点解析 ..toString(); 二.括号先计算再转换 ().toString(); 三.加空串 + ''
- 【转载】将本地图片转成base64
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- ArrayList详解,底层是数组,实现Serializable接口
一.对于ArrayList需要掌握的七点内容 ArrayList的创建:即构造器往ArrayList中添加对象:即add(E)方法获取ArrayList中的单个对象:即get(int index)方法 ...
- 【leetcode】901. Online Stock Span
题目如下: 解题思路:和[leetcode]84. Largest Rectangle in Histogram的核心是一样的,都是要找出当前元素之前第一个大于自己的元素. 代码如下: class S ...
- PHP获取用户是否关注公众号。获取微信openid和用户信息
<?php /* * 首先填写授权地址为当前网址 * 将$appid和$secret参数替换成自己公众号对应参数,需要外网可以访问服务器环境测试 */ header("Content- ...