博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LintCode] Best Time to Buy and Sell Stock 买卖股票的最佳时间
阅读量:7285 次
发布时间:2019-06-30

本文共 724 字,大约阅读时间需要 2 分钟。

 

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

Given array [3,2,3,1,2], return 1.

 

LeetCode上的原题,请参见我之前的解法。

 

class Solution {public:    /**     * @param prices: Given an integer array     * @return: Maximum profit     */    int maxProfit(vector
&prices) { int res = 0, mn = INT_MAX; for (int i = 0; i < prices.size(); ++i) { mn = min(mn, prices[i]); res = max(res, prices[i] - mn); } return res; }};

 

转载地址:http://gvpjm.baihongyu.com/

你可能感兴趣的文章
FreeMarker循环变量内建函数
查看>>
Python中time模块详解
查看>>
java 的模板方式设计模式
查看>>
跳转到servlet出现java.lang.InstantiationException:
查看>>
RedHat7 配置VNCServer
查看>>
git 回滚版本
查看>>
Nginx反向代理实现会话(session)保持的两种方式
查看>>
Nginx配置指令location匹配符优先级和安全问题
查看>>
sc create 创建启动服务带参数 目录不能有空格
查看>>
Glusterfs初体验
查看>>
Centos搭建SVN服务器三步曲
查看>>
NC-ERP IUFO系统管理要点
查看>>
linux下将文件设置为swap
查看>>
jquery filter()方法
查看>>
make和makefile
查看>>
eclipse git 强制覆盖本地文件
查看>>
elasticsearch查询关键字slop
查看>>
[Unity3d]Player Settings导出设置
查看>>
Python成长之路第一篇(2)-初识列表和元组
查看>>
Docker EE/Docker CE简介与版本规划
查看>>