LeetCode Array Easy121. 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 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的更多相关文章
- 【一天一道LeetCode】#122. Best Time to Buy and Sell Stock II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Say you ...
- 【LeetCode】188. Best Time to Buy and Sell Stock IV 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】309. Best Time to Buy and Sell Stock with Cooldown 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 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 ...
- 【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 ...
- 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 ...
- 【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 ...
- 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 ...
- 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 ...
随机推荐
- java并发编程之美-阅读记录3
java并发包中的ThreadLocalRandom类,jdk1.7增加的随机数生成器 Random类的缺点:是多个线程使用同一个原子性的种子变量,导致对原子变量的更新产生竞争,降低了效率(该类是线程 ...
- ORM:Chloe
ORM的一种:Chloe注意实体类模板特色:多表连接 利用chloe实现对各表的增删查改的管理,判断现有物料是否能够支持生产规模. 一开始报错: IQuery<ProductionPlans&g ...
- javascript基础六(事件对象)
1.事件驱动 js控制页面的行为是由事件驱动的. 什么是事件?(怎么发生的) 事件就是js侦测到用户的操作或是页面上的一些行为 事件源(发生在谁身上) ...
- Alpha版本——展示博客【第二组】
成员简介 章豪 http://cnblogs.com/roar/ 角色: PM,后端 个人介绍: 努力学习开发的小菜鸡,管理小白,背锅组长 贡献: - 设计开发计划 - 跟踪项目进行 - 组织开组会 ...
- Java jvisualvm简要说明(转)
转自:http://blog.csdn.net/a19881029/article/details/8432368 jvisualvm能干什么:监控内存泄露,跟踪垃圾回收,执行时内存.cpu分析,线程 ...
- react-native学习(一)————使用react-native-tab-navigator创建底部导航
使用react-native-tab-navigator创建底部Tab导航 1.使用npm安装react-native-tab-navigator npm install react-native-t ...
- TP model where条件丢失
最近我修复了一个bug,这个bug是用户能看到所有用户的数据,经过排查发现是where条件丢失,导致查询语句直接查了所有数据. 但是代码并没有问题,然后查到了 ThinkPHP/Library/Thi ...
- 【Java架构:基础技术】一篇文章搞掂:Maven
本文篇幅较长,建议合理利用右上角目录进行查看(如果没有目录请刷新). 本文基于<Maven 实战>一书进行总结和扩展,大家也可以自行研读此书. 一.Maven简介 1.1.什么是Maven ...
- C++ pair
C++ pair Pair类型概述 pair是一种模板类型,其中包含两个数据值,两个数据的类型可以不同,基本的定义如下: pair<int, string> a; 表示a中有两个类型,第一 ...
- ubuntu18.04 点击启动器实现窗口最小化
gsettings set org.gnome.shell.extensions.dash-to-dock click-action 'minimize'