博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Best Time to Buy and Sell Stock
阅读量:4515 次
发布时间:2019-06-08

本文共 3328 字,大约阅读时间需要 11 分钟。

149. Best Time to Buy and Sell Stock

public class Solution {    /**     * @param prices: Given an integer array     * @return: Maximum profit     */    public int maxProfit(int[] prices) {        // write your code here        if(prices==null || prices.length==0){            return 0;        }                int min = Integer.MAX_VALUE;        int res = Integer.MIN_VALUE;        for(int i=0;i
View Code

 

150. Best Time to Buy and Sell Stock II

class Solution {    public int maxProfit(int[] prices) {        if (prices == null || prices.length == 0) {            return 0;        }        int res = 0;        for (int i = 0; i < prices.length - 1; i++) {            if (prices[i + 1] - prices[i] > 0) {                res += prices[i + 1] - prices[i];            }        }        return res;    }}
View Code

 

151. Best Time to Buy and Sell Stock III

public class Solution {    /**     * @param prices: Given an integer array     * @return: Maximum profit     */    public int maxProfit(int[] prices) {        // write your code here        if(prices==null||prices.length==0){            return 0;        }        int n= prices.length;        int[][] f = new int[n+1][5+1];                f[0][0]=0;                for(int i=1;i<=n;i++){            //手中无股票            for(int j=1;j<=5;j+=2){                //情况1:昨日卖出                f[i][j] = Math.max(f[i][j],f[i-1][j]);                //情况2:昨日持有+今日卖出收益                if(j>1 && i>1){                    f[i][j] = Math.max(f[i][j],f[i-1][j-1]+prices[i-1]-prices[i-2]);                }            }                        //手中有股票            for (int j = 2; j <= 2 * 2; j += 2) {                // two option: sell or don't sell                if (i > 1) {                    //情况1:昨日持有+今日收益                    f[i][j] = Math.max(f[i][j], f[i - 1][j] + (prices[i - 1] - prices[i - 2]));                }                if (j > 1) {                    //情况2:昨日卖出                    f[i][j] = Math.max(f[i][j], f[i - 1][j - 1]);                }            }        }                int res = Integer.MIN_VALUE;        for (int j = 1; j <= 5; j += 2) {            res = Math.max(res, f[n][j]);        }                return res;    }}
View Code

 

393. Best Time to Buy and Sell Stock IV

public class Solution {    /**     * @param K: An integer     * @param prices: An integer array     * @return: Maximum profit     */    public int maxProfit(int K, int[] prices) {        // write your code here        int n = prices.length;        if(n==0){            return 0;        }                if(K>n/2){            int tmp =0;            for(int i=0;i
=2 && j>1){ //注意要加入j>1的边界,j=1时,前一状态是不可能持有股票 //昨日持有,今日卖出 f[i][j] = Math.max(f[i][j],f[i-1][j-1]+prices[i-1]-prices[i-2]); } } //持有股票 for(int j =2;j<=2*K;j+=2){ //昨日已持有,需累加今日收益 if(i>=2) f[i][j] = Math.max(f[i][j],f[i-1][j]+prices[i-1]-prices[i-2]); //昨日不持有,今日刚买入 f[i][j] = Math.max(f[i][j],f[i-1][j-1]); } } int res = 0; for(int j =1;j<=2*K+1;j+=2){ res = Math.max(res,f[n][j]); } return res; }}
View Code

 

转载于:https://www.cnblogs.com/lizzyluvcoding/p/10798136.html

你可能感兴趣的文章
职场的真相——七句话
查看>>
xcode命令行编译时:codesign命令,抛出“User interaction is not allowed.”异常 的处理...
查看>>
[转载]开机出现A disk read error occurred错误
查看>>
STM32 C++编程 002 GPIO类
查看>>
无线冲方案 MCU vs SoC
查看>>
进程装载过程分析(execve系统调用分析)
查看>>
在windows 7中禁用media sense
查看>>
ELK-Elasticsearch安装
查看>>
Android 模拟器(Emulator)访问模拟器所在主机
查看>>
删除字符串中指定子串
查看>>
day40-socket编程
查看>>
SpringBoot里mybatis查询结果为null的列不返回问题的解决方案
查看>>
为什么留不住优秀的员工
查看>>
Django后台管理admin笔记
查看>>
JavaScript中的变量
查看>>
iptables基本原理和规则配置
查看>>
ArcGIS JS 学习笔记4 实现地图联动
查看>>
ubuntu 12.04 lts安装golang并设置vim语法高亮
查看>>
编程题目:PAT 1004. 成绩排名 (20)
查看>>
使用分层实现业务处理
查看>>