股票利润
来源:互联网 发布:网络神曲2016叫什么 编辑:程序博客网 时间:2024/04/28 04:57
在股市的交易日中,假设最多可进行两次买卖(即买和卖的次数均小于等于2),规则是必须一笔成交后进行另一笔(即买-卖-买-卖的顺序进行)。给出一天中的股票变化序列,请写一个程序计算一天可以获得的最大收益。请采用实践复杂度低的方法实现。
给定价格序列prices及它的长度n,请返回最大收益。保证长度小于等于500。
测试样例:[10,22,5,75,65,80],6返回:87
import java.util.Scanner;public class stock {/** * @param args */public static int maxprofit(int[] prices, int n){int firstBuy=Integer.MIN_VALUE;int firstSell=Integer.MIN_VALUE;int secondBuy=Integer.MIN_VALUE;int secondSell=Integer.MIN_VALUE;int profit=Integer.MIN_VALUE;for(int i=0;i<n;i++){firstBuy=Math.max(firstBuy, -prices[i]);firstSell=Math.max(firstSell, prices[i]+firstBuy);secondBuy=Math.max(secondBuy, firstSell-prices[i]);profit=Math.max(profit, prices[i]+secondBuy);}return profit;}public static void main(String[] args) {// TODO Auto-generated method stub Scanner in=new Scanner(System.in); String n=in.nextLine(); String a[]=n.split(","); int len=a.length; int[] prices=new int[len]; for(int i=0;i<len;i++){ prices[i]=Integer.parseInt(a[i]); // System.out.print(prices[i]); } // System.out.print("sfd"); System.out.println(maxprofit(prices,len));}}
0 0
- 股票利润
- 买股票最大利润
- 股票最大利润
- 非常重要的投机股票利润
- 求股票的最大利润
- 股票利润最大化(交易次数k)
- 投资股票以获得利润在一个小的时期
- 携程校招2017编程题之股票最大利润
- 122. Best Time to Buy and Sell Stock II | 股票利润最大化
- 121. Best Time to Buy and Sell Stock | 买股票的利润
- leetcode_122. Best Time to Buy and Sell Stock II 多次买卖股票,求交易的最大利润
- [LeetCode]121. Best Time to Buy and Sell Stock(求近期股票能获得的最大利润)
- LeetCode-714:Best Time to Buy and Sell Stock with Transaction Fee (带有抛售费用的股票最大利润) -- medium
- 股票
- 股票
- 股票
- 股票
- 股票
- Odoo 开发者模式
- PAT乙级训练题--1008
- Network Layers
- 数据库连接池
- hdu 5884 二分+队列
- 股票利润
- Chorme中启动阿里旺旺误点取消启动并记住选择,如何更改。
- android listview适配器里区分不同按钮的点击事件
- PAT乙级训练题--1009
- mongodb和pymongo包实践小结
- 命令模式
- 美团O2O排序解决方案——线上篇
- 显示所有元素的border
- u-boot-2016.09 make编译过程分析(一)