Description:

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.

只允许买卖一次,求最大利润。

动态规划,从后向前找最大股价,减去当前股价,求利润,找最大利润。O(n).

public class Solution {
public int maxProfit(int[] prices) { int len = prices.length;
if(len == 0) return 0;
int highestPrice = prices[len - 1];
int res = 0;
for(int i=len-1; i>=0; i--) {
highestPrice = max(highestPrice, prices[i]);
res = max(res, highestPrice-prices[i]);
} return res;
} int max(int a, int b) {
return a > b ? a : b;
}
}

LeetCode——Best Time to Buy and Sell Stock的更多相关文章

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

  10. Algorithm - 贪心算法使用场景 ( LEETCODE —— Best Time to Buy and Sell Stock II)

    先看一道leetcode题: Best Time to Buy and Sell Stock II Say you have an array for which the ith element is ...

随机推荐

  1. win7、vs2013编译webkit总结

    在windows7.vs2013环境下编译webkit,本人测试成功 1.解压cygwin到C盘下(建议放在C盘更目录下,否则编译期间会有部分文件找不到) 启动命令:C:\cygwin\bin\min ...

  2. iOS边练边学--通知机制和键盘处理

    一.通知中心(NSNotificationCenter) 每一个程序都有一个通知中心实例,专门负责协助不同对象之间的消息通信 任何一个对象都可以想通知中心发布通知(NSNotification),描述 ...

  3. DataGridView:根据条件改变单元格的颜色

    根据条件改变DataGridView行的颜色可以使用RowPrePaint事件. 示例程序界面如下: 示例程序代码如下: using System; using System.Collections. ...

  4. e647. 处理鼠标移动事件

    component.addMouseMotionListener(new MyMouseMotionListener()); public class MyMouseMotionListener ex ...

  5. C++ 数据封装

    C++ 数据封装所有的 C++ 程序都有以下两个基本要素: 程序语句(代码):这是程序中执行动作的部分,它们被称为函数.程序数据:数据是程序的信息,会受到程序函数的影响.封装是面向对象编程中的把数据和 ...

  6. (转)windows下编译最新的x264

    二:<windows下编译最新的x264> X264更新的比较快,每天都有更新,但算法模块,基本结构是没有多大变化的.x264都是用C语言写的包括C99,但C99语法是在VC中是没法用的( ...

  7. linux -- ubuntu搭建nodejs环境

    需求:在web端做一个实时性功能比较强的模块, 客户端:用websocket 服务端:node.js node.js介绍:node.js天生就是一个高效的服务端语言,可以直接使用 javascript ...

  8. UIView的几个枚举定义

    UIView是iOS开发最主要的视图,非常多控件都是继承它,掌握当中的几个基本枚举定义,有利益理解视图的载入和參数差别. 一.UIViewAnimationCurve UIView的基本动画变化规律 ...

  9. 修改php上传文件尺寸、响应时间、时区时间等设置

    修改php上传文件尺寸.响应时间 1.修改php.ini 1.post_max_size 指通过表单POST给PHP的所能接收的最大值,包括表单里的所有值,默认为8M(改为150M),看你自己需要进行 ...

  10. PHP简单的图片上传

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...