题目:

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.

代码:

class Solution {
public:
int maxProfit(vector<int>& prices) {
if (prices.size()==) return ;
int min_price = prices[], max_profit = ;
for ( size_t i = ; i < prices.size(); ++i )
{
min_price = std::min(min_price, prices[i]);
max_profit = std::max(max_profit, prices[i]-min_price);
}
return max_profit;
}
};

tips:

Greedy

核心在于维护两个变量:

min_price: 算上当前元素的最小值

max_profit:算上当前元素的最大利润

走完所有元素,可以获得max_profit

================================================

第二次过这道题,犹豫了一下才想起来思路。

class Solution {
public:
int maxProfit(vector<int>& prices) {
int globalProfit = ;
if ( prices.empty() ) return globalProfit;
int minPrice = prices[];
for ( int i=; i<prices.size(); ++i )
{
minPrice = min(minPrice, prices[i]);
globalProfit = max(globalProfit, prices[i]-minPrice);
}
return globalProfit;
}
};

【Best Time to Buy and Sell Stock】cpp的更多相关文章

  1. leetcode 【 Best Time to Buy and Sell Stock 】python 实现

    思路: Say you have an array for which the ith element is the price of a given stock on day i. If you w ...

  2. 【LeetCode-面试算法经典-Java实现】【121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)】

    [121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Say you have ...

  3. leetcode 【 Best Time to Buy and Sell Stock II 】python 实现

    题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...

  4. 【Best Time to Buy and Sell Stock III 】cpp

    题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...

  5. 【Best Time to Buy and Sell Stock II】cpp

    题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...

  6. leetcode 【 Best Time to Buy and Sell Stock III 】python 实现

    题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...

  7. 【一天一道LeetCode】#122. Best Time to Buy and Sell Stock II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Say you ...

  8. 121. Best Time to Buy and Sell Stock【easy】

    121. Best Time to Buy and Sell Stock[easy] Say you have an array for which the ith element is the pr ...

  9. 【leetcode】Best Time to Buy and Sell Stock III

    Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the price of ...

随机推荐

  1. 零基础逆向工程30_Win32_04_资源文件_消息断点

    1 资源文件,创建对话框 详细步骤: 1.创建一个空的Win32应用程序 2.在VC6中新增资源 File -> New -> Resource Script 创建成功后会新增2个文件:x ...

  2. vue组件总结(三)

    一.什么是组件 组件(component)是Vue最强大的功能之一.组件可以扩展HTML元素,封装可重用的代码,根据项目需求,抽象出一些组件,每个组件里包含了展现.功能和样式.每个页面,根据自己的需要 ...

  3. mysql服务器系统优化

    1.选择合适的IO调度 对于mysql的系统,如果是SSD,那么应该使用NOOP调度算法,如果是磁盘,就应该使用Deadline调度算法.默认是CFQ echo dealine > /sys/b ...

  4. 远程登录事件ID

    4672.4624 删除本机记录 HKEY_CURRENT_USER \ Software\Microsoft  \ Terminal ServerClientDefault: 删除“此电脑\文档”下 ...

  5. mysql 疑难问题-django

    1不能存储中文 问题解决1: 确认表设计时,字段name_vn字符集是utf8,改成utf8后可以存储中文

  6. 【BZOJ1064】[NOI2008] 假面舞会(图上DFS)

    点此看题面 大致题意:有\(k\)种面具(\(k\)是一个未知数且\(k≥3\),每种面具可能有多个),已知戴第\(i\)种面具的人能看到第\(i+1\)种面具上的编号,特殊的,戴第\(k\)种面具的 ...

  7. WQS二分学习笔记

    前言 \(WQS\)二分听起来是个很难的算法,其实学起来也并不是那么难. 适用范围 在某些题目中,会对于某个取得越多越优的物品,限定你最多选择\(k\)个,问你能得到的最优答案. 例如这道题目:[CF ...

  8. 安装软件出现缺少vcruntime140dll的解决方法

    转自:http://jingyan.baidu.com/article/49711c617e4000fa441b7c92.html 首先下载vc++2015,注意自己系统是32位还是64位的,下载对应 ...

  9. css中如何把鼠标变成手

    css中鼠标放上去变成手型怎么设置:其实就是一个属性的问题, css的cursor属性 cursor:pointer; 其实这个属性我也记了很多,到现在都容易拼写错误,不过好在编辑器有提示. defa ...

  10. java基础面试题:写clone()方法时,通常都有一行代码,是什么?

    clone()方法 与new constructor()构造器创建对象不同 是克隆一个新的对象 package com.swift; public class Clone_Test { public ...