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. 【问题解决方案】anaconda-python在cmd-pip安装requests后依然提示No module named requests

    参考: 知乎回答:python的requests安装后idle依然提示No module named requests? 环境: win7-64位 anaconda3-Python3.7 & ...

  2. Java script-1

    什么是JavaScript? JavaScript是一种直译式脚本语言,一种轻量级的脚本语言. 什么是脚本语言? Script language指的是它不具备开发操作系统的能力,而是只用来编写控制其他 ...

  3. 2019-9-2-win10-uwp-切换主题

    title author date CreateTime categories win10 uwp 切换主题 lindexi 2019-09-02 12:57:38 +0800 2018-2-13 1 ...

  4. 愚蠢的sql语法错误(sum (xxx))

    sum和()之间打了一个空格,导致一致报sql语法错误,看了半天不知道怎么回事orz

  5. ubuntu安装samba

    1,  sudo apt-get install samba sudo apt-get install smbclient 2,  vi /etc/samba/smb.conf 在最后加字段(为可读可 ...

  6. beautifhulsoup4的使用

    Beautiful: - 基本使用 from bs4 import BeautifulSoup   - 解析器:       lxml, html.parser​   soup = Beautiful ...

  7. java-特殊字符转义转换

    常见特殊字符:如下 <>…&—\"·‘’ java替换 /* * 特殊字符转换 */ public static String replacesss(String ss) ...

  8. 【LeetCode 33】搜索旋转排序数组

    题目链接 [题解] 会发现旋转之后,假设旋转点是i 则0..i-1是递增有序的.然后i..len-1也是递增有序的. 且nums[i..len-1]<nums[0]. 而nums[1..i-1] ...

  9. ribbon学习

    spring cloud 中的负载均衡有ribbon和feign 引入ribbon依赖 <!--ribbon相关--> <dependency> <groupId> ...

  10. 调用windows的复制文件对话框

    function CopyFileDir(sDirName: String; sToDirName: String): Boolean; var fo: TSHFILEOPSTRUCT; begin ...