当前位置: 首页 > article >正文

23 动态规划解买卖股票的最佳时机含手续费

问题描述:给定一个整数数组prices,其中第i各元素代表了第i填的股票价格;非负整数fee代表了交易股票是的手续费用,你可以无限次的完成交易,但是你眉笔交易都需要付手续费,如果你已经购买了一个股票,在卖出它之前不能再继续购买股票了,返回获得利润的最大值。注意:一次交易是指买入持有并卖出股票的整个过程,你就不能再继续购买股票了。并定义卖股票才可以进行扣费。

动态规划求解:定义动态数组dp[i][0]表示第i天手里没有股票的最大值,此时有两种选择,一种是dp[i-1][1]+prices[i]-2表示在第i天卖出了股票获得了第i天的利润,第二种是不改变,与之前相同dp[i-1],dp[i][1]表示第i天手里有股票的最大值,此时有两种选择dp[i-1][0]-prices[i]或者保持dp[i-1][0]

public int maxProfit(int []prices)
{
int [][]dp=new int[prices.length][2];

dp[0][0]=0;
dp[0][1]=-prices[0];
for(int i=0;i<prices.length;i++)
{
dp[i][0]=Math.max(dp[i-1][0],dp[i-1][1]+prices[i]-2);
dp[i][1]=Math.max(dp[i-1][1],dp[i-1][0]-prices[i]);
}
return Math.max(dp[prices.length-1][0],dp[prices.length-1][1]);
}

递归方式求解:对于每一天而言:如果手上持有股票,可以选择卖或者不卖,然后进入下一天中,
如果手上没有股票,则可以选择不买或者买,然后进入下一年中,最后到达最后一天对将利润加入PriorityQueue<Integer> maxheap=new PriorityQueue<>((a,b)->{return b-a;})或者(PriorityQueue<Integer>maxheap=new PriorityQueue<>(Collections.reverseOrder()))最大堆中,最后从最大堆中弹出第一个数字即为最大利润
 

private getMaxProfit(int [] prices,int index,int profit,Boolean ishasone,PriorityQueue<Integer> queue)
{
if(index==prices.length)
{
queue.add(profit);
return;
}
if(ishasone)
{
getMaxProfit(prices,index+1,profit,ishasone);
getMaxProfit(prices,index+1,profit+prices[i]-2,!ishasone);
}else
{
getMaxProfit(prices,index+1,profit-prices[i],!ishasone);
getMaxProfit(prices,index+1,profit,ishasone);
}
}

public int GetMaxProfit(int []prices)
{
PriorityQueue<Integer>maxheap=new PriorityQueue<>(Collections.reverseOrder());
getMaxProfit(prices,0,0,false);
return maxheap.peek();
}

知识点总结:最小堆定义PriorityQueue<Integer>minHeap=new PriorityQueue<>();最大堆定义1
PriorityQueue<Integer>maxHeap=new PriorityQueue<>(Collections.reverseOrder());PriorityQueue<Integer>minheap=new PriorityQueue<>((a,b)->{return b-a});最大堆定义3PriorityQueue<Integer>maxHeap=new PriorityQueue<>(new Comparable<Integer>(){
@Override
public int Compare(Integer t1,Integer t2)
{
return t2-t1;
}
});
PriorityQueue增加操作:minHeap.add(),minheap.offer()
PriorityQueue减少操作:minheap.poll(),minheap.remove();会弹出
PriorityQueue查询操作:minheap.peek();

Map知识点:Map<Integer,Integer>map=new HashMap<Integer,Integer>map;
增加:Map.put(key,value);
访问:Map.get(key);
删除;Map.remove(key);
遍历:
 

        for (Integer i : Sites.keySet()) {
            System.out.println("key: " + i + " value: " + Sites.get(i));
        }
        // 返回所有 value 值
        for(String value: Sites.values()) {
          // 输出每一个value
          System.out.print(value + ", ");


http://www.kler.cn/news/160184.html

相关文章:

  • node切换版本
  • C++转义符及用法
  • mysql基础之DQL基本单表查询
  • 『Jmeter超级干货』| Linux下Jmeter安装配置、脚本设计执行、监控及报告完整过程
  • Windows 下 PyTorch 入门深度学习环境安装与配置 GPU 版
  • Windows server 部署iSCSI共享磁盘搭建故障转移群集
  • BearPi Std 板从入门到放弃 - 引气入体篇(9)(DAC->ADC)
  • Java LeetCode篇-深入了解二叉树经典解法(三种方式实现:获取二叉树的最大深度)
  • Redis——某马点评day03——part2:秒杀业务异步优化
  • 鸿蒙4.0开发笔记之ArkTS语法基础之应用生命周期与页面中组件的生命周期(十六)
  • Park Unpark
  • Web安全漏洞分析-XSS(下)
  • ApplicationContextAware 类
  • ELK 日志解决方案
  • AI网关究竟是什么,怎么样才算是AI算力的网关
  • 跟着GPT学习shell脚本,理论与实践相结合的学习计划。(一)
  • 团队git操作流程
  • 单片机开发常用的软件构架
  • html5各行各业官网模板源码下载(1)
  • 19、pytest通过mark标记测试函数
  • 每天一点python——day85
  • 记录一次vscode markdown的图片路径相关插件学习配置过程
  • 【微服务】分布式限流如何实现
  • Android10 Dialog bug
  • 【技术干货】宇视IPC音频问题解决步骤
  • 编程常见的问题
  • Java动态代理实现与原理详细分析
  • linux环境下编译安装OpenCV For Java(CentOS 7)
  • 判断完数(写出部分函数)
  • Uncle Maker: (Time)Stamping Out The Competition in Ethereum