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 (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.

Note that you cannot sell a stock before you buy one.

Example 1:

Input: [,,,,,]
Output:
Explanation: Buy on day (price = ) and sell on day (price = ), profit = - = .
Not - = , as selling price needs to be larger than buying price.

Example 2:

Input: [,,,,]
Output:
Explanation: In this case, no transaction is done, i.e. max profit = .

题目描述: 给一个数组,每一个元素都代表当前索引的价格。你只有一次买进和卖出的机会,计算买进最低点和卖出最高点。并返回最大差价

思路:首先判断当前点是否可以是买进点。及当前点是否比下一点小。

2,在当前点是买进点的时候,从当前点向尾部遍历,判断当前最大差价点。

3,不断进行比较,找到整体的出现最大差价时的买进点和卖出点

代码如下:

public int MaxProfit(int[] prices)
{
int inIndex = -, outIndex = -;//买进点, 卖出点
int subMax = int.MinValue;//存储最大差价
for (int i = ; i < prices.Length -; i++)
{
if(prices[i] < prices[i + ])//如果当前值比下一个值小,说明此时买进可以有盈利
{
int tempMax = ;
int maxIndex = -;
for (int j = i; j < prices.Length; j++)//从买进处遍历,判断最大差价点的索引
{
int temp = prices[j] - prices[i];
if(temp > tempMax)
{
tempMax = temp;
maxIndex = j;
}
}
if(tempMax > subMax)//更新买进点、卖出点
{
subMax = tempMax;
inIndex = i;
outIndex = maxIndex;
}
}
}
return subMax > ? prices[outIndex] - prices[inIndex] : ; }

LeetCode Array Easy121. Best Time to Buy and Sell Stock的更多相关文章

  1. 【一天一道LeetCode】#122. Best Time to Buy and Sell Stock II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Say you ...

  2. 【LeetCode】188. Best Time to Buy and Sell Stock IV 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  3. 【LeetCode】309. Best Time to Buy and Sell Stock with Cooldown 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  4. LeetCode 笔记23 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 ...

  5. 【LeetCode OJ】Best Time to Buy and Sell Stock III

    Problem Link: http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ Linear Time Solut ...

  6. LeetCode OJ 123. Best Time to Buy and Sell Stock 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】123. Best Time to Buy and Sell Stock III

    @requires_authorization @author johnsondu @create_time 2015.7.22 19:04 @url [Best Time to Buy and Se ...

  8. LeetCode解题报告—— Best Time to Buy and Sell Stock

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

  9. leetcode:122. Best Time to Buy and Sell Stock II(java)解答

    转载请注明出处:z_zhaojun的博客 原文地址 题目地址 Best Time to Buy and Sell Stock II Say you have an array for which th ...

随机推荐

  1. kubernetes容器集群部署Flannel网络

    Overlay Network:覆盖网络,在基础网络上叠加的一种虚拟网络技术模式,该网络中的主机通过虚拟链路连接起来. VXLAN:将源数据包封装到UDP中,并使用基础网络的IP/MAC作为外层报文头 ...

  2. js自增++与自减--运算符

    /** * 自增(++)与自减(--)运算符 */ // 自增示例 var a = 1, c, d, e; console.log(`a++ = ${a++}`); // a++ = 1 consol ...

  3. java web中的get和post(笔记)

    W3CSchool 在客户机和服务器之间进行请求-响应时,两种最常被用到的方法是:GET 和 POST. GET - 从指定的资源请求数据. POST - 向指定的资源提交要被处理的数据 GET 方法 ...

  4. SSM+Maven使用PageHelper插件分页

    官方网站: https://pagehelper.github.io/docs/howtouse/#2-%E9%85%8D%E7%BD%AE%E6%8B%A6%E6%88%AA%E5%99%A8%E6 ...

  5. 一、bootstrap-datepicker

    一.bootstrap-datepicker <!DOCTYPE html> <html> <head> <title></title> & ...

  6. private: CRITICAL_SECTION m_cs;

    CRITICAL_SECTION m_cs;   //临界区 私有数据成员  pop  数据结构 push 临界区 保护 2.类CCriticalSection的对象表示一个“临界区”,它是一个用于同 ...

  7. erlang应用程序启动

    (1)erlang应用程序启动过程中,还可以分阶段启动.          在erlang应用程序的资源文件*.app可以定义分步骤启动.            *.app中的start_phase字 ...

  8. 大数据基础环境--jdk1.8环境安装部署

    1.环境说明 1.1.机器配置说明 本次集群环境为三台linux系统机器,具体信息如下: 主机名称 IP地址 操作系统 hadoop1 10.0.0.20 CentOS Linux release 7 ...

  9. js对象的深度拷贝

    //判断对象的类型 Array Object Function String Number ..... function getObjType(obj){ return Object.prototyp ...

  10. Spring开发总结

    自动装载配置开启: spring.factories内容: org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.b ...