Java实现-买卖股票的最佳时机4
来源:互联网 发布:淘宝网卖什么最赚钱 编辑:程序博客网 时间:2024/05/17 02:05
private static int maxProfit(int k,int []prices){if(prices==null || prices.length==0) return 0; if(prices.length<k){return maxProfit2(prices);} int[] local = new int[k+1]; int[] global = new int[k+1]; for(int i=0;i<prices.length-1;i++) { int diff = prices[i+1]-prices[i]; for(int j=k;j>=1;j--) { local[j] = Math.max(global[j-1]+(diff>0?diff:0), local[j]+diff); global[j] = Math.max(local[j],global[j]); } } return global[k]; }private static int maxProfit2(int[] prices){int maxMoney=0;int count=0;for(int i=0;i<prices.length-1;i++){if(prices[i]<=prices[i+1]){count++;}else{maxMoney+=prices[i]-prices[i-count];count=0;}}if(count>0){maxMoney+=prices[prices.length-1]-prices[prices.length-1-count];}return maxMoney;};
阅读全文
0 0
- Java实现-买卖股票的最佳时机4
- Java实现-买卖股票的最佳时机1
- Java实现-买卖股票的最佳时机2
- Java实现-买卖股票的最佳时机3
- 买卖股票的最佳时机
- 买卖股票的最佳时机
- 买卖股票的最佳时机
- LintCode-买卖股票的最佳时机
- 题目:买卖股票的最佳时机
- LintCode--买卖股票的最佳时机
- LintCode_149_买卖股票的最佳时机
- Lintcode:买卖股票的最佳时机
- 买卖股票的最佳时机 II
- 买卖股票的最佳时机 III
- 买卖股票的最佳时机|_1
- 买卖股票的最佳时机||_1
- 买卖股票的最佳时机,lintcode
- 【LintCode】买卖股票的最佳时机
- Java实现-买卖股票的最佳时机3
- OPENCV3.0+VS2015安装配置
- 使用Jekyll构建博客并使用
- Android多页面切换,保持Java中Socket长连接之静态方法
- jquery操作select
- Java实现-买卖股票的最佳时机4
- 通俗易懂介绍一下ZigBee的特性
- 专业课程设计之客户与服务器程序的同步与通信机制的设计(二)TCP通信
- Android视频直播的实现
- 管理者神功之“九”阴真经
- hdu4574(dfs+枚举)
- BloomFilter布隆过滤器
- Java二维数组,将古诗《相思》分别用横版和竖版的形式输出
- POJ