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.

[Thought]

Using greedy algorithm to solve this problem . Scan from left to right , and keep track the minimum price . If the difference between the minimum value and price is bigger than the profit , replace it.

public class Solution {
public int maxProfit(int[] prices) {
if(prices.length == 0)
return 0; int profit = 0;
int min = prices[0];
for(int i=0;i<prices.length;i++){
if(prices[i]<min){
min = prices[i];//keep track the minimum value
}else{
if(prices[i] - min > profit){//if the difference between price[i] and minimum price is bigger than profit , replace it.
profit = prices[i]-min;
}
}
}
return profit;
}
}

[LeetCode] Best Time to Buy and Sell Stock Solution的更多相关文章

  1. LeetCode:Best Time to Buy and Sell Stock I II III

    LeetCode:Best Time to Buy and Sell Stock Say you have an array for which the ith element is the pric ...

  2. [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 ...

  3. [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 ...

  4. [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 ...

  5. [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 ...

  6. [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 ...

  7. LEETCODE —— Best Time to Buy and Sell Stock II [贪心算法]

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

  8. LeetCode Best Time to Buy and Sell Stock IV

    原题链接在这里:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/ 题目: Say you have an array ...

  9. [LeetCode] Best Time to Buy and Sell Stock with Transaction Fee 买股票的最佳时间含交易费

    Your are given an array of integers prices, for which the i-th element is the price of a given stock ...

随机推荐

  1. mysql 全文检索的匹配问题

    开发过程中碰到一个关于mysql 全文检索的问题,控制台打印的SQL语句拿到数据库里执行,结果不对.后来发现原来是少了双引号.下面是网上找到的资料,我是看到这个才意识到自己的问题. 这是之前在数据库执 ...

  2. warning: control reaches end of non-void function

    用gcc编译一个程序的时候出现这样的警告: warning: control reaches end of non-void function 它的意思是:控制到达非void函数的结尾.就是说你的一些 ...

  3. Linux SSH 互信

    第一步: 创建用于身份认证的两个密钥文件 ssh-keygen #注明.想省事的话打完这个命令后一直回车就行了. 第二步: 把公钥上传到目标主机上去 ssh-copy-id -i id_rsa.pub ...

  4. SAR 图像

    http://www.dlr.de/hr/en/desktopdefault.aspx/tabid-2326/3776_read-5679/

  5. Oracle EBS-SQL (GL-4):从接收追溯到接收事务

    SELECT row_id, creation_date, created_by, last_update_date, last_updated_by,last_update_login, note_ ...

  6. iOS6和iOS7代码的适配(4)——tableView

    iOS7上不少控件的样子有了变化(毕竟要扁平化嘛),不过感觉变化最大的肯定非tableView莫属.因为这个控件的高度可定制性,原先是使用及其广泛的,这样的一个改变自然也影响颇大. 1.accesso ...

  7. python-聊聊反射

    反射 对于初学python可能较难理解,但反射是非常有用. 试想一下,当别的程序传入给你写的这段代码一个变量(var=“math”),这个变量是一个字符串,这个字符串是一个模块或者一个模块下的某个方法 ...

  8. 达内TTS6.0课件oop_day05

  9. SPDY HTTP2.0

    SPDY(读作“SPeeDY”)是Google开发的基于TCP的应用层协议,用以最小化网络延迟,提升网络速度,优化用户的网络使用体验.SPDY并不是一种用于替代HTTP的协议,而是对HTTP协议的增强 ...

  10. SQL学习之数据列去空格函数

    1.LTRIM()---去掉列值左边的空格  如下代码: select * from dbo.course where tno='t003' and cno='c0013'