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. 让2个并列的div根据内容自动保持同等高度js

    有左右2个并列的div,2个div都不能限定高度.左div为导航,右div为内容.如何能让左div块自动获得和右div块相等的高度? 同时,也有网友提问到“如果右块高度比左块低,会不会导致左块的内容被 ...

  2. 移动端使用rem方法

    (function (doc, win) { var docEl = doc.documentElement, resizeEvt = 'orientationchange' in window ? ...

  3. Hibernate之总结

    以前做.net,最近做java项目,负责服务端的开发,直接用的jdbc,线程安全问题.缓存同步问题以及连接池什么的,都是手动写,不但麻烦而且容易出错.项目结束,赶快抽时间学了下hibernate,每天 ...

  4. windows命令行模式下无法打开python程序解决方法

    今天刚开始学Python,首先编写一个简单地hello world程序,想在命令行模式运行,结果出现下面: 经过一番思考,发现用cd命令可以解决这件事,看下图: 这样就解决了.

  5. SQL Server 缓存清理的一些原因

    1.dbcc freeproccache; 2.dbcc freesystemcache('all') | dbcc freesystemcache('pool_name'); 3.declare @ ...

  6. CentOS 删除自带的OpenJDK 和 安装SunJDK

    [root@WX32 local]# java -version java version "1.6.0" OpenJDK Runtime Environment (build - ...

  7. asp.net 使用my97 datepicker实现前后两个日期的范围界定

    说明:日期选择后,前面的日期小于等后面的日期,后面的日期大于等于前面的日期.点点看就知道了:) - 这里将周末日期不可选.代码如下: <html xmlns="http://www.w ...

  8. JavaScript之怎样获取元素节点

    JavaScript获取元素节点一共有三种方法,分别是通过元素ID.通过标签名字和通过类名字来获取: 1.通过元素ID属性的ID值来获得元素对象-getElementById() DOM提供了一个名为 ...

  9. Matlab中用内建函数代替for循环

    在使用matlab进行矩阵计算的时候,经常会遇到要使用for循环的情况.但其实很多操作可以用内部的一些函数代替. bsxfun, arrayfun, cellfun, spfun, structfun ...

  10. AdventureWorks2008 数据库安装

    我使用的操作系统是 win 8.1,由于对早前安装的sql server 2008的兼容性不太好,要安装对应的service pack来解决一下这个问题. 如何使用 SQL Server 在 Wind ...