前言

 

【LeetCode 题解】系列传送门:  http://www.cnblogs.com/double-win/category/573499.html

 

1.题目描述

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.

2. 题意

给定一个整数数组Array用以表示某股票在每一天的价格,即该数组中第i个元素表示第i天的价格。

假如用户在给定的天数内只能完成一次交易(即在某一天购买该股票,然后在另一天将其抛售)。请设计一个算法,使得用户在此交易过程中获得最大利润。

3. 思路

规则:一只股票必须先买入,然后再售出。

采用动态规划的思路:

使用两个变量记录当前最小值minPrice,以及当前最大值maxPrice。

Profit的递归过程,可以按照如下状态变换方程表示:

4: 解法

class Solution {
public:
int maxProfit(vector<int> &prices) {
if(prices.size()<=1) return 0;
int min=prices.front(); // 当前最小值
int max=prices.front(); //当前最大值
int maxP=0; //最大profit for(int i=1;i<prices.size();i++){
if(prices[i]>max){ //当前值大于max
maxP=prices[i]-min;
max=prices[i];
}else if(prices[i]<min){ // 当前值比min小,那么更新min
min=prices[i];
}else{
if(prices[i]-min>maxP){ // 当前值介于min~max之间
maxP=prices[i]-min;
max=prices[i];
}
}
}
return maxP;
}
};
作者:Double_Win
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则作者保留追究法律责任的权利。  若本文对你有所帮助,您的关注推荐是我们分享知识的动力!

[LeetCode 题解]:Best Time to Buy and Sell Stock的更多相关文章

  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 Week6]Best Time to Buy and Sell Stock

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

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

  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. 03_java之基本语法

    01创建引用类型变量公式 * A: 创建引用类型变量公式 * a: 我们要学的Scanner类是属于引用数据类型,我们先了解下引用数据类型. * b: 引用数据类型的定义格式 * 与定义基本数据类型变 ...

  2. nginx 代理http配置实例

    #user nginx; worker_processes ; #error_log /var/log/nginx/error.log warn; #pid /var/run/nginx.pid; e ...

  3. [Z]Password-based encryption in Java: salt and key derivation

    http://www.javamex.com/tutorials/cryptography/pbe_salt.shtml 另外,这个网站好像有很多很深入而且很详尽的教程.目测是个类似于官方的或者大牛们 ...

  4. leetcode334

    public class Solution { public bool IncreasingTriplet(int[] nums) { var len = nums.Length; ) { retur ...

  5. Alternative PHP Cache ( APC )

    简介: Alternative PHP Cache (APC) 是一个开放自由的PHP opcode 缓存.它的目标是提供一个自由.开放和健全的框架用于缓存和优化 PHP 的中间代码,加快 PHP 执 ...

  6. 【300】◀▶ IDL - ENVI API

    参考:ENVI API 参考:ENVI Classic Display 序号 类名称   功能说明   语法 & 举例 01 ENVI 函数   ====<<<< De ...

  7. 解决Eclipse编辑JavaScript时卡的问题

    eclipse在开发JavaEE项目时容易卡,特别是在编辑JavaScript时,经过网上各种搜索,综合整理一下,对自己的eclipse设置之后,结果不在出现卡的问题了. 原文地址:http://bl ...

  8. 数组和集合(三):Set集合的使用总结

    一.概述 · 继承collection接口 · 无序(不记录添加顺序).不允许元素重复.只允许存在一个null元素 二.实现类 1. HashSet · 底层其实是包装了一个HashMap实现的 · ...

  9. redis cluster test

    cp /test/tests/redis.conf /etc redis-server /etc/redis.conf redis-trib.rb create --replicas 1 172.17 ...

  10. Scala基础:类和构造器

    类 package com.zy.scala.cls /** * 在 Scala 中,类并不用声明为 public 类型的. * Scala 源文件中可以包含多个类,所有这些类都具有共有可见性. */ ...