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. Python进阶之map()、reduce()、filter()

    map()函数 .note-content {font-family: "Helvetica Neue",Arial,"Hiragino Sans GB",&q ...

  2. Storm基础理论

    Storm流式计算基础 .note-content {font-family: "Helvetica Neue",Arial,"Hiragino Sans GB" ...

  3. windows下载安装requests

    1.下载地址:https://github.com/kennethreitz/requests 2.解压缩后,cd requests 3.安装 python setup.py install

  4. mini-httpd源码分析-port.h

    针对不同系统的宏定义,对于Linux而言 /* port.h - portability defines */ #elif defined(linux) # define OS_Linux # def ...

  5. openNebula libvirt-virsh attach disk device for kvm

    1,新建文件硬盘 qemu-img create -f qcow2 testdisk.img 2G

  6. 剑指offer26 复杂链表的复制

    /* struct RandomListNode { int label; struct RandomListNode *next, *random; RandomListNode(int x) : ...

  7. DLL导出函数和类 之 __declspec(dllexport)

    可利用__declspec(dllexport)导出函数或类. 若要指定C类型约定导出,则需在前面加extern “C”. 若要导出函数,__declspec(dllexport) 关键字必须出现在调 ...

  8. 1.一步一步学c#(一):.NET体系结构(知识点)

    一.C#和.NET关系     1. 首先C#语言有两个很重要的方面:第一它是为了Microsoft的.NET Framework的结合而设计的,其次它是一种为问题而设计解决问题的方法的语言,它有很多 ...

  9. asp.net ToString()方法介绍

      C 货币 2.5.ToString("C") ¥2.50 D 十进制数 25.ToString("D5") 00025 E 科学型 25000.ToStri ...

  10. SQL学习之使用常用函数处理数据

    一.在介绍使用函数处理数据前,先说下使用DBMS(数据库管理系统)处理数据所带来的问题! 1.与几乎所有的DBMS都同等的支持SQL语句(如SELECT)不同,每一个DBMS都有特定的函数,事实上,只 ...