博客
关于我
L121买卖股票的最佳时机
阅读量:215 次
发布时间:2019-02-28

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

  1. 买卖股票的最佳时机
    给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。

如果你最多只允许完成一笔交易(即买入和卖出一支股票一次),设计一个算法来计算你所能获取的最大利润。

注意:你不能在买入股票前卖出股票。

示例 1:

输入: [7,1,5,3,6,4]

输出: 5
解释: 在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的时候卖出,最大利润 = 6-1 = 5 。
注意利润不能是 7-1 = 6, 因为卖出价格需要大于买入价格;同时,你不能在买入前卖出股票。
示例 2:

输入: [7,6,4,3,1]

输出: 0
解释: 在这种情况下, 没有交易完成, 所以最大利润为 0。

1.暴力方法

class Solution {       public int maxProfit(int[] prices) {           int len = prices.length;        if(len == 0) return 0;                int max = Integer.MIN_VALUE;        for(int i = 0; i < len; i++) {           	for(int j = i; j 

注意两者下标的区别,主要是怕漏了为0的结果

class Solution {       public int maxProfit(int[] prices) {           int len = prices.length;        if(len == 0) return 0;                int max = 0;//默认为0        for(int i = 0; i < len; i++) {           	for(int j = i + 1; j 

其实最小值为0未覆盖,可以使i,j重合,这样就满足最小值情况了

public int maxProfit2(int[] prices) {           //两个程序方法是相同的        int len = prices.length;        if(len  == 0) return 0;        int max = Integer.MIN_VALUE;        for(int i = 0; i < len; i++){               for(int j = i; j < len; j++){                   if(max < prices[j] - prices[i]){                       max = prices[j] - prices[i];                }            }        }        return max;    }    public int maxProfit1(int[] prices) {           int len = prices.length;        if(len == 0) return 0;        int max =Integer.MIN_VALUE;        for(int i = 0;i < len; i++){               for(int j = 0; j <= i; j++){                   if(max < prices[i] - prices[j]){                       max = prices[i] - prices[j];                }            }        }        return max;    }

2.记录最大值,利用一个变量

class Solution {       public int maxProfit(int[] prices) {           if(prices == null || prices.length == 0) return 0;        int len = prices.length;        //记录左边的最小值,一次遍历便可搞定        int minPrice = Integer.MAX_VALUE, res = 0;        for(int i = 0; i < len; i++){               if(prices[i] < minPrice) minPrice = prices[i];            if(res < prices[i] - minPrice) res = prices[i] - minPrice;        }        return res;    }}

3.动态规划

先进行状态设计
在这里插入图片描述

public int maxProfit(int[] prices) {           if(prices == null || prices.length == 0) return 0;        int len = prices.length;        if(len < 2) return 0;        int[][] dp = new int[len][2];        // dp[i][0] 下标为 i 这天结束的时候,不持股,手上拥有的现金数        // dp[i][1] 下标为 i 这天结束的时候,持股,手上拥有的现金数        dp[0][0] = 0;        dp[0][1] = -prices[0];        for(int i = 1; i < len; i++){               dp[i][0] = Math.max(dp[i - 1][0], dp[i- 1][1] + prices[i]);            dp[i][1] = Math.max(dp[i - 1][1], - prices[i]);        }        return dp[len - 1][0];        //只需要判断最后的情况即可    }

进一步压缩空间

转载地址:http://cjvp.baihongyu.com/

你可能感兴趣的文章
localhost:5000在MacOS V12(蒙特利)中不可用
查看>>
logstash mysql 准实时同步到 elasticsearch
查看>>
Luogu2973:[USACO10HOL]赶小猪
查看>>
mabatis 中出现&lt; 以及&gt; 代表什么意思?
查看>>
Mac book pro打开docker出现The data couldn’t be read because it is missing
查看>>
MAC M1大数据0-1成神篇-25 hadoop高可用搭建
查看>>
mac mysql 进程_Mac平台下启动MySQL到完全终止MySQL----终端八步走
查看>>
Mac OS 12.0.1 如何安装柯美287打印机驱动,刷卡打印
查看>>
MangoDB4.0版本的安装与配置
查看>>
Manjaro 24.1 “Xahea” 发布!具有 KDE Plasma 6.1.5、GNOME 46 和最新的内核增强功能
查看>>
mapping文件目录生成修改
查看>>
MapReduce程序依赖的jar包
查看>>
mariadb multi-source replication(mariadb多主复制)
查看>>
MariaDB的简单使用
查看>>
MaterialForm对tab页进行隐藏
查看>>
Member var and Static var.
查看>>
memcached高速缓存学习笔记001---memcached介绍和安装以及基本使用
查看>>
memcached高速缓存学习笔记003---利用JAVA程序操作memcached crud操作
查看>>
Memcached:Node.js 高性能缓存解决方案
查看>>
memcache、redis原理对比
查看>>