(greedy)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,只不过现在允许进行多次交易,只需在上升曲线累加收益即可,即所谓的贪婪算法。
(贪婪算法与动态规划的区别:贪婪算法只需在每一步求出最大收益,即可在最后一步得到总的最大利润,而对于动态规划,每一步的最大收益可能受到前面某一步的影响,导致必须在最后一步综合前面的状态才能得出最大利润)
Java解法:
public class Solution {
public int maxProfit(int[] prices) {
if(prices == null || prices.length == 0)
return 0;
int profit = 0;
int temp = prices[0];
for(int i = 1; i<prices.length; i++){
if(prices[i] > temp) //上升曲线每步累加收益
profit += prices[i] - temp;
temp = prices[i];
}
return profit;
}
}
(greedy)Best Time to Buy and Sell Stock II的更多相关文章
- [LintCode] 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 ...
- 27. Best Time to Buy and Sell Stock && Best Time to Buy and Sell Stock II && Best Time to Buy and Sell Stock III
Best Time to Buy and Sell Stock (onlineJudge: https://oj.leetcode.com/problems/best-time-to-buy-and- ...
- 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 pric ...
- 【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 ...
- 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: Best Time to Buy and Sell Stock II 解题报告
Best Time to Buy and Sell Stock IIQuestion SolutionSay you have an array for which the ith element i ...
- 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 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 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...
随机推荐
- Power(int base, int exponent) 函数实现
这个是个高效的算法,时间复杂度为 O(logn) 原理: a的n次方: #include<iostream> #include<cmath> using namespace s ...
- 两个php.ini; ubuntu下配置文件
C:\wamp\bin\apache\apache2.4.17\bin\php.ini 用于web访问时的配置文件, C:\wamp\bin\php\php5.6.15\php.ini 用于cli [ ...
- asdasd
adasdasd asdasd asdasd asd
- fetch策略
@OneToMany(mappedBy="image",cascade=CascadeType.ALL,fetch=FetchType.EAGER) @Fetch(value=Fe ...
- [ES6] Rest Parameter
Problem with the ES5: function displayTags(){ for (let i in arguments) { let tag = arguments[i]; _ad ...
- USB通讯协议 && 数据传输
USB2.0通讯协议(spalish) 1.包(packet) 包是USB系统中信息传输的基本单元,所有数据都是经过打包后在总线上传输的.USB包由五部分组成,同步字段(sync).包标识符(PI ...
- css实现ie6以上文字高度未知垂直居中
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
- 使用vs中的工具进行架构比较
使用vs自带的架构比较工具可以对不同库中的结构进行比较,也可以将源中的架构更新到目标架构中.当然这种更新只是架构的更新,数据并不会同步.
- (转)强大的JQuery表单验证插件 FormValidator使用介绍
jQuery formValidator表单验证插件是客户端表单验证插件.在做B/S开发的时候,我们经常涉及到很多表单验证,例如新用户注册,填写个人资料,录入一些常规数据等等.在这之前,页面开发者(J ...
- 64位系统 IIS不支持 Excel导入的问题
64位系统不支持读取excel的问题: 应用程序池-常规选项 启用32应用程序 启用23应用程序是为了保证32应用程序能够正常运行