Best Time to Buy and Sell Stock i
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.
Example 1:
Input: [7, 1, 5, 3, 6, 4]
Output: 5 max. difference = 6-1 = 5 (not 7-1 = 6, as selling price needs to be larger than buying price)
Example 2:
Input: [7, 6, 4, 3, 1]
Output: 0 In this case, no transaction is done, i.e. max profit = 0
只允许一次交易,算出最大利益。利益是卖的价格减去买的价格,卖在买后面。
这个题目的变形出现过:一个数组,0<=a<b<=n。。。a,b是数组的两个下标,b在后面。求num[b]-num[a]的最大值。跟这个题是一个意思。
依次遍历数组,每个元素跟它前面的最小值做差,会使得此元素的差值最大,然后找出最大差值就行。见代码。
class Solution {
public int maxProfit(int[] prices) {
if(prices==null||prices.length<2) return 0;
int maxProfit=0;
int min=prices[0];
for(int i=1;i<prices.length;i++){
if(prices[i]-min>maxProfit)
maxProfit=prices[i]-min;
if(min>prices[i])//找出当前元素前面的最小值,在一次遍历的时候就可以找出来了
min=prices[i];
}
return maxProfit;
}
}
Best Time to Buy and Sell Stock i的更多相关文章
- [LeetCode] 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 ...
- [LeetCode] Best Time to Buy and Sell Stock IV 买卖股票的最佳时间之四
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] 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] Best Time to Buy and Sell Stock II 买股票的最佳时间之二
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] 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 ...
- [LintCode] Best Time to Buy and Sell Stock II 买股票的最佳时间之二
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LintCode] 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 ...
- LeetCode——Best Time to Buy and Sell Stock II (股票买卖时机问题2)
问题: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...
- 123. Best Time to Buy and Sell Stock (三) leetcode解题笔记
123. Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the pric ...
- 122. Best Time to Buy and Sell Stock(二) leetcode解题笔记
122. Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price ...
随机推荐
- Java数据类型及类型转换
http://blog.csdn.net/pipisorry/article/details/51290064 java浮点数保留n位小数 import java.text.DecimalFormat ...
- android分包方案
当一个app的功能越来越复杂,代码量越来越多,也许有一天便会突然遇到下列现象: 1. 生成的apk在2.3以前的机器无法安装,提示INSTALL_FAILED_DEXOPT 2. 方法数量过多,编译时 ...
- 【Android 应用开发】 FastJson 使用详解
博客地址 :http://blog.csdn.net/shulianghan/article/details/41011605 fastjson 源码地址 : -- GitHub : https:// ...
- oracle ebs应用产品安全性-数据访问权限集
定义 数据访问权限集是一个重要的.必须设定的系统配置文件选项.对具有相同科目表.日历和期间类型的分类帐及其所有平衡段值或管理段值的定义读写权限,系统管理员将其分配至不同的责任以控制不同的责任对分类帐数 ...
- 简谈高通Trustzone的实现
从trust zone之我见知道,支持trustzone的芯片会跑在两个世界. 普通世界.安全世界,对应高通这边是HLOS,QSEE. 如下图: 如下是HLOS与QSEE的软件架构图 HLOS这两分为 ...
- 无法与域Active Directory域控制器(AD DC)连接(虚机加域出错问题)
今天建了两台虚机用的VMWARE,一台做域控,一台做应用服务器,但是部署好域控要把应用服务器加入域时候报错 虚机网卡设置桥接并设置好IP使两台虚机在同一个局域网内,通过ip地址互ping能ping通, ...
- java文件的基本操作示例
一.获得控制台用户输入的信息 public String getInputMessage() throws IOException...{ System.out.println("请输入您的 ...
- ListView中ConvertView和ViewHolder
1.概述 ListView是Android中非常常见的控件通过Adapter架起数据与界面显示的桥梁,MVC思想在其中得到了很好地体现: M:model 数据模型 添加到ListView中显示的 ...
- 018-继承-OC笔记
学习目标 1.[掌握]Xcode开发文档 2.[掌握]static关键字 3.[掌握]self关键字 4.[掌握]继承 5.[掌握]NSObject 6.[掌握]访问修饰符 7.[掌握]私有实例变量和 ...
- 《java入门第一季》之Arrays类前传(排序案例以二分查找注意的问题)
根据排序算法,可以解决一些小案例.举例如下: /* * 把字符串中的字符进行排序. * 举例:"dacgebf" * 结果:"abcdefg" * * 分析: ...