[leetcode]123. Best Time to Buy and Sell Stock III(Java)
来源:互联网 发布:关闭端口的命令 编辑:程序博客网 时间:2024/06/05 16:06
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/#/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 at most two transactions.
Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
package go.jacob.day715;public class Demo2 {public int maxProfit(int[] prices) {if (prices == null || prices.length < 2)return 0;// 四个变量分别表示经过当前操作以后的profitint firstBuy = Integer.MIN_VALUE, firstSell = 0;int secondBuy = Integer.MIN_VALUE, secondSell = 0;for (int curPrice : prices) {firstBuy = Math.max(firstBuy, -curPrice);firstSell = Math.max(firstSell, curPrice + firstBuy);secondBuy = Math.max(secondBuy, firstSell - curPrice);secondSell = Math.max(secondSell, secondBuy + curPrice);}return secondSell;}}
阅读全文
0 0
- [Leetcode] Best Time to Buy and Sell Stock III (Java)
- [LeetCOde][Java] Best Time to Buy and Sell Stock III
- LeetCode 123. Best Time to Buy and Sell Stock III
- [Leetcode]123.Best Time to Buy and Sell Stock III
- [Leetcode] 123. Best Time to Buy and Sell Stock III
- [leetcode] 123.Best Time to Buy and Sell Stock III
- 【leetcode】123. Best Time to Buy and Sell Stock III
- [leetcode] 123. Best Time to Buy and Sell Stock III
- leetcode 123. Best Time to Buy and Sell Stock III
- leetcode 123. Best Time to Buy and Sell Stock III
- [LeetCode] 123. Best Time to Buy and Sell Stock III
- Leetcode 123. Best Time to Buy and Sell Stock III
- 【leetcode】123. Best Time to Buy and Sell Stock III
- LeetCode 123. Best Time to Buy and Sell Stock III
- leetcode-123. Best Time to Buy and Sell Stock III
- [LeetCode]123. Best Time to Buy and Sell Stock III
- LeetCode 123. Best Time to Buy and Sell Stock III
- [leetcode]123. Best Time to Buy and Sell Stock III
- VMware安装Ubuntu及VMware Tools实用总结
- 《大话设计模式》学习总结
- 搭建springMVC环境并且写一个helloWorld实例
- java框架基础 静态代理和动态代理
- Log4j日志的输出使用
- [leetcode]123. Best Time to Buy and Sell Stock III(Java)
- HTML5中的两种web存储
- 分布式系统架构(二)——一致性协议
- this指针
- 实现背景颜色随着ScrollView滑动改变的ActionBar
- python list的一些特性
- 巧用郑州SEO九个小技巧 提高网站流量
- Java_基础—集合框架(练习)
- 逆置/反转单链表+查找单链表的倒数第k个节点