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.

简单的DP问题,可是一开始我没读懂题目的意思。这里的买入当然应该在卖出之前,所以说不是那种取一个max一个min相减就能解决的,代码如下:

 class Solution {
public:
int maxProfit(vector<int>& prices) {
int sz = prices.size();
if(sz == || sz == ) return ;
int min, maxGain;
min = prices[];//min的初值要注意
maxGain = ;
for(int i = ; i < sz; ++i){
if(min > prices[i])
min = prices[i];
else if(maxGain < prices[i] - min)
maxGain = prices[i] - min;
}
return maxGain;
}
};

下面是java写的,runtime还行,击败了60%的runtime,方法和上面有一点不同,如下:

 public class Solution {
public int maxProfit(int[] prices) {
int maxVal = 0;
int start = 0;
for(int i = 1; i < prices.length; ++i){
if(prices[i] < prices[i-1]){
maxVal = Math.max(prices[i-1]-prices[start], maxVal);
if(prices[i] < prices[start])
start = i;
}
}
if(prices.length != 0)//排除长度是0的情况
maxVal = Math.max(prices[prices.length - 1] - prices[start], maxVal); //防止出现一直到结尾都不断增大的问题
return maxVal;
}
}

LeetCode OJ:Best Time to Buy and Sell Stock(股票买卖的最佳时期)的更多相关文章

  1. [LeetCode OJ] Best Time to Buy and Sell Stock I

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

  2. LeetCode OJ - Best Time to Buy and Sell Stock

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/xiezhihua120/article/details/32939749 Say you have ...

  3. 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 ...

  4. [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 ...

  5. [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 ...

  6. [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 ...

  7. [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 ...

  8. [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 ...

  9. 【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 ...

  10. [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 ...

随机推荐

  1. MySQL按时间查找

    RecentMutations表的结构如图,现在的需求是需要查找到2017年09月08日前10天的变体总数: SQL语句:SELECT SUM(MutantNumber) FROM RecentMut ...

  2. PHP获取客户端的IP

    function getClientIP(){    global $ip;    if (getenv("HTTP_CLIENT_IP"))        $ip = geten ...

  3. 超过两行显示省略号 -webkit-line-clamp、-webkit-box-orient vue打包后不起作用

    为了实现两行显示缩略显示,但是本地是可以显示,打包后不起作用 word-break: break-all; text-overflow: ellipsis; display: -webkit-box; ...

  4. unix 全缓冲、行缓冲、无缓冲

    基于流的操作最终会调用read或者write函数进行I/O操作.为了使程序的运行效率最高,流对象通常会提供缓冲区,以减少调用系统I/O库函数的次数. 基于流的I/O提供以下3种缓冲: 全 缓冲:直到缓 ...

  5. go——流程控制

    Go在流程控制方面的特点如下: 没有do和while循环,只有一个更广义的for语句. switch语句灵活多变,还可以用于类型判断. if语句和switch语句都可以包含一条初始化子语句. brea ...

  6. org.springframework-jdbc

    Spring JDBC模板类—org.springframework.jdbc.core.JdbcTemplate 博客分类: spring JDBCSpringSQL编程数据结构  今天看了下Spr ...

  7. HDOJ 2203 亲和串 【KMP】

    HDOJ 2203 亲和串 [KMP] Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...

  8. 程序员、架构师、技术总监、CTO

    程序员 程序员,英文名coder/programmer,大家常自嘲叫码农的阶段.这个角色职责是把需求或产品实现为用户可用的软件产品. 此职位为执行级别.另外因为经验较少,一般需要求助别人,或与别人一起 ...

  9. less预编译语言使用总结

    以前就使用过less和sass,其实很简单,就是很长时间不用,忘记语法了,现在来总结一片使用技巧 一.注释 less的注释不会被编译到css文件中,所以提倡多使用less中的注释:/**/ 二.变量 ...

  10. CSS3鼠标悬停8种动画特效

    在线演示 本地下载