LeetCode——Best Time to Buy and Sell Stock II
2015-09-27 17:17
363 查看
Description:
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).
知道股价,可以有多次买卖,必须先买后卖,求最大利润。
贪心,只要股价比买价高就卖出。
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).
知道股价,可以有多次买卖,必须先买后卖,求最大利润。
贪心,只要股价比买价高就卖出。
public class Solution { public int maxProfit(int[] prices) { int res = 0; for(int i=0; i<prices.length-1; i++) { if(prices[i] < prices[i+1]) { res += prices[i+1] - prices[i]; } } return res; } }
相关文章推荐
- C# WindowsForm 员工管理系统四【发放工资】
- 浏览器兼容、CSS3 选择器
- 笔试题
- 接口
- 第四周实训总结(实训周)
- 软件测试
- 记录——《C Primer Plus (第五版)》第八章编程练习第三题
- 局部变量与全局变量
- kvm虚拟化学习笔记(一)之kvm虚拟化环境安装
- 浮动与定位、bfc和ifc
- Linux第二次学习笔记
- [LeetCode][JavaScript]House Robber
- codeforces 580C(Kefa and Park, 搜索类型题)
- NSString字符串长度计算方法
- bond配置
- LeetCode之N-Queens
- matlab实现简单的if else 的语句
- OpenSSL与PKI
- Scala深入浅出进阶经典 第89讲:Scala中使用For表达式实现内幕思考
- getopt() 与 getopt_long() 详解