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) {
int size = prices.size();
int minV = INT_MAX;
int maxP = ; for (int i = ; i < size; ++i) {
minV = min(minV, prices[i]);
maxP = max(maxP, prices[i] - minV);
}
return maxP;
}
};

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

  1. &lt;LeetCode OJ&gt; 121. /122. Best Time to Buy and Sell Stock(I / II)

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

  2. leetcode 第188题,我的解法,Best Time to Buy and Sell Stock IV

    <span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...

  3. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  4. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  5. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

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

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

  7. Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】

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

  8. 【LeetCode】Best Time to Buy and Sell Stock IV

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

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

    Problem Link: http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ Linear Time Solut ...

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

    @requires_authorization @author johnsondu @create_time 2015.7.22 19:04 @url [Best Time to Buy and Se ...

随机推荐

  1. Python+Selenium之常用模块

    要用webdriver:from selenium import webdriver eg: driver = webdriver.Chrome()      driver.maximize_wind ...

  2. C# 字符串转枚举类型

    Enum.Parse(typeof(枚举类型), 字符串类型) 如:在Unity中, Enum.Parse(typeof(SystemLanguage), "Chinese")

  3. 关于null的操作

    空值 空值一般用NULL表示 一般表示未知的.不确定的值,也不是空格 一般运算符与其进行运算时,都会为空 空不与任何值相等 表示某个列为空用:IS NULL  不能使用COMM=NULL这种形式 某个 ...

  4. vim实战:插件安装(Vundle,NerdTree)

    一:插件管理器Vundle 1.简介 Vundle是vim的一个插件管理器, 同时它本身也是vim的一个插件.插件管理器用于方便.快速的安装.删除.Vim更新插件.vim Vundle插件官方地址:h ...

  5. ZOJ 3769 Diablo III

    描述 Diablo III is an action role-playing video game. A few days ago, Reaper of Souls (ROS), the new e ...

  6. 在VIM中添加行号的方法

    VIM编辑器是可以显示行号的.但是,有时候我们需要在整个代码的行首添加行号.怎么实现呢?实现的方法有很多,这里就介绍我知道的一种吧. 在每行行首添加某个字符串 :%s/^/your_string/ 在 ...

  7. springboot入门记

    在idea中直接新建: 选择web: 在idea中启动“项目名称”Application中的main即可: 或者在项目目录下运行:mvn spring-boot:run: 或者: mvn instal ...

  8. Python基础(3) - 数据类型:4元组类型

    Python Tuple 是不可变 list. 一旦创建了一个 tuple 就不能以任何方式改变它.Tuple是用()包括起来的. Tuple与List都是按照定义的顺序进行排序的,索引从0开始,与 ...

  9. Scrapy框架学习(三)Spider、Downloader Middleware、Spider Middleware、Item Pipeline的用法

    Spider有以下属性: Spider属性 name 爬虫名称,定义Spider名字的字符串,必须是唯一的.常见的命名方法是以爬取网站的域名来命名,比如爬取baidu.com,那就将Spider的名字 ...

  10. SSO单点登录的实现原理是怎样的

    单点登录在现在的系统架构中广泛存在,他将多个子系统的认证体系打通,实现了一个入口多处使用,而在架构单点登录时,也会遇到一些小问题,在不同的应用环境中可以采用不同的单点登录实现方案来满足需求.我将以我所 ...