Array DP

Description:

Say you have an array for which the ith element is the price of a given stock on day i.

If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.

Example 1:

Input: [7, 1, 5, 3, 6, 4]
Output: 5 max. difference = 6-1 = 5 (not 7-1 = 6, as selling price needs to be larger than buying price)

Example 2:

Input: [7, 6, 4, 3, 1]
Output: 0 In this case, no transaction is done, i.e. max profit = 0.

先说明题目意思,刚开始我看了很久也没搞明白,去Discuss上才看明白了。就是说,Input数组表示这段时间每天的货物价格,你以某个价格买入,之后再以那天的价格卖出,获取利益。例如:

Input: [7,1,5,3,6,4]
在价格为1时买入,价格为6时卖出,可获利润为5
Input: [7,6,4,3,1]
一直再降价,所以不买该货物,利润为0
Input: [2,4,1]
第一天买,第二天卖,可获利为2
Input: [3,2,6,5,0,3]
第二天买,第三天卖出,获利为4,比价格为0时买入,价格为3时卖出获利更多

my Solution:

public class Solution {
public int maxProfit(int[] prices) {
int n = prices.length;
int profit = 0;
if (n == 0) {
return profit;
}
else {
int min = prices[0];
int max = prices[0];
for (int i = 1; i < n; i++) {
if (min >= prices[i]) {
min = prices[i];
max = min;
}
if (max <= prices[i]) {
max = prices[i];
}
profit = Math.max(profit, max-min);
}
return profit;
}
}
}

我的方法非常常规,这题最好的方法就是用动态规划的思想来解。

Best Solution:

public class Solution {
public int maxProfit(int[] prices) {
int maxCur = 0;
int maxSoFar = 0;
for (int i = 1; i < prices.length; i++) {
maxCur = Math.max(0, maxCur + prices[i] - prices[i-1]);
maxSoFar = Math.max(maxCur, maxSoFar);
}
return maxSoFar;
}
}

我对动态规划的理解还不够深刻,还需要多学习,多做这方面的题,加油!

LeetCode & Q121-Best Time to Buy and Sell Stock-Easy的更多相关文章

  1. Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  2. [LeetCode] 121. Best Time to Buy and Sell Stock 买卖股票的最佳时间

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  3. [LeetCode] 122. Best Time to Buy and Sell Stock II 买卖股票的最佳时间 II

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  4. [LeetCode] 123. Best Time to Buy and Sell Stock III 买卖股票的最佳时间 III

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  5. [LeetCode] 188. Best Time to Buy and Sell Stock IV 买卖股票的最佳时间 IV

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  6. [LeetCode] 309. Best Time to Buy and Sell Stock with Cooldown 买卖股票的最佳时间有冷却期

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  7. 【LeetCode】Best Time to Buy and Sell Stock IV

    Best Time to Buy and Sell Stock IV Say you have an array for which the ith element is the price of a ...

  8. [Leetcode Week6]Best Time to Buy and Sell Stock

    Best Time to Buy and Sell Stock 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/best-time-to-buy-and ...

  9. [Leetcode][JAVA] Best Time to Buy and Sell Stock I, II, III

    Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a gi ...

  10. 【leetcode】Best Time to Buy and Sell Stock III

    Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the price of ...

随机推荐

  1. 网络防火墙和NAT地址转换

    网络防火墙 iptables/netfilter网络防火墙: (1) 充当网关 (2) 使用filter表的FORWARD链 注意的问题: (1) 请求-响应报文均会经由FORWARD链,要注意规则的 ...

  2. 【learning】凸包

    吐槽 计算几何这种东西qwq一开始真的觉得恶心qwq(主要是总觉得为啥画图那么直观的东西非要写一大堆式子来求qwq真的难受qwq) 但其实静下心来学习的话感觉还是很妙的ovo题目思考起来也十分好玩ov ...

  3. 如何通过Spring Boot配置动态数据源访问多个数据库

    之前写过一篇博客<Spring+Mybatis+Mysql搭建分布式数据库访问框架>描述如何通过Spring+Mybatis配置动态数据源访问多个数据库.但是之前的方案有一些限制(原博客中 ...

  4. error:com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException

    问题:同样的代码,只能插入一组值,第二组值插入不了 解决:开始我将app_id作为主键,但很明显,同一个app_id会有不同的index,而同一个index也可能对应不同的app_id,因此只能添加一 ...

  5. OAutho2 请求响应格式

    1.OAuth2.PasswordGrant REQUEST: POST /token HTTP/1.1 Host: localhost: Content-Type: application/x-ww ...

  6. django Forbidden

    Forbidden (CSRF cookie not set.)解决方法 Forbidden (CSRF cookie not set.):xxx解决方法:在django项目的settings.py文 ...

  7. python实现一般最小二乘系统辨识方法

    问题: 对于一个未知参数的系统,往往需要用到系统辨识的方法,例如对于一个单输入单输出系统: Z(k)+a1*Z(k-1)+a2*Z(k-2)=b1*U(k-1)+b2*U(k-2)+V(k) 其中:V ...

  8. READ TABLE 的用法

    SORT ITAB BY '你想比较的列'. " 排序以增加二分查找的速度 READ TABLE itab with key 'itab中某列' = ‘目标列' BINARY SEARCH. ...

  9. 解决Centos7本机时间与实际时间相差8小时

    # timedatectl Local -- :: CST Universal -- :: UTC #相差8小时 RTC -- :: Time zone: Asia/Shanghai (CST, +) ...

  10. V5.7_UTF8_SP1、SP2---任意前台用户登录(cookie伪造)

    漏洞触发点在include/memberlogin.class.php中的MemberLogin类中的登录校验函数 可以看到M_ID参数是由GetNum(GetCookie("DedeUse ...