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

本文共 774 字,大约阅读时间需要 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; }};

本文转自博客园Grandyang的博客,原文链接:,如需转载请自行联系原博主。

你可能感兴趣的文章
Spark in action on Kubernetes - Playground搭建与架构浅析
查看>>
详解NodeJs流之一
查看>>
Fundebug计费标准解释:事件数是如何定义的?
查看>>
理解这几张图,你就是js小牛了
查看>>
【EOS】Cleos基础
查看>>
iOS高仿微信项目、阴影圆角渐变色效果、卡片动画、波浪动画、路由框架等源码...
查看>>
使用parted解决大于2T的磁盘分区
查看>>
oschina
查看>>
Octave 入门
查看>>
深度学习入门:10门免费线上课程推荐
查看>>
JavaScript 如何正确处理 Unicode 编码问题!
查看>>
iOS帅气加载动画、通知视图、红包助手、引导页、导航栏、朋友圈、小游戏等效果源码...
查看>>
微服务核心架构梳理
查看>>
浅谈JavaScript的面向对象和它的封装、继承、多态
查看>>
laravel with 查询列表限制条数
查看>>
Python爬虫--- 1.3 BS4库的解析器
查看>>
CentOS从零开始部署Nodejs项目
查看>>
React组件设计模式(一)
查看>>
express.js的介绍及使用
查看>>
闭包--闭包之tab栏切换(四)
查看>>