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. java虚拟机规范(se8)——class文件格式(四)

    4.7 属性 属性用于class文件格式中的ClassFile,field_info,method_info和Code_attribute结构. 所有的属性都是下面的格式: attribute_inf ...

  2. go中加号的用法

    // + 的用法 package main import "fmt" func main() { // 加号两端都是数字的话,做加法运算 var i = 1 var j = 1 v ...

  3. 2019-9-29-dotnet-对-DateTime-排序

    title author date CreateTime categories dotnet 对 DateTime 排序 lindexi 2019-09-29 14:55:49 +0800 2019- ...

  4. 本地代码上传至git仓库

    1.进入项目文件夹,初始化 git init 2.添加文件到版本库 git add . 3.提交文件 git commit -m "初次提交" 4.关联远程仓库 git remot ...

  5. python函数参数*args **kwargs

    毕业多年,把C++都就饭吃了....今天居然在纠结什么是形参什么是实参..... 定义函数里面写的参数就是形参,因为没有内存占用,实际调用时写的参数就是实参,因为有内存占用和传值 然后就是位置参数,可 ...

  6. 在Anaconda环境下使用Jupyter Notebook

    !!!Anaconda 和 Jupyter Notebook 在 zsh 环境下不能正常使用! 启动建立的 Anaconda 环境 安装 nb_conda:conda install nb_conda ...

  7. Shell及其操作环境

    来源: 鸟哥的Linux私房菜第十章.認識與學習BASH Shell是什么?1分钟理解Shell的概念! ssh在本地调用远程主机上的命令,不登录远程主机shell 一.Shell Shell 是一个 ...

  8. 集训队8月1日(拓扑排序+DFS+主席树入门)

    上午看书总结 今天上午我看了拓扑排序,DFS+剪枝,相当于回顾了一下,写了三个比较好的例题.算法竞赛指南93~109页. 1.状态压缩+拓扑排序 https://www.cnblogs.com/246 ...

  9. 【Java架构:基础技术】一篇文章搞掂:Maven

    本文篇幅较长,建议合理利用右上角目录进行查看(如果没有目录请刷新). 本文基于<Maven 实战>一书进行总结和扩展,大家也可以自行研读此书. 一.Maven简介 1.1.什么是Maven ...

  10. Django中get()和fiter()的区别

    QuerySet(查询结果集对象):从数据库中查询出来的结果一般是一个集合,这个集合叫做 QuerySet,也就是指服务器上的url里面的查询内容.Django会对查询返回的结果集QuerySet进行 ...