problem

121. Best Time to Buy and Sell Stock

code

class Solution {
public:
int maxProfit(vector<int>& prices) {
int res = ;
for(int i=; i<prices.size(); i++)
{
int buy_price = prices[i];
for(int j=i+; j<prices.size(); j++)
{
int sell_price = prices[j];
int tmp = sell_price - buy_price;
if(tmp>res) res = tmp;
}
}
return res; }
};

one pass

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

参考

1. Leetcode_Best Time to Buy and Sell Stock;

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

  1. 【LeetCode】121. Best Time to Buy and Sell Stock 解题报告(Java & Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 C++ 解法 日期 ...

  2. 【leetcode】121. Best Time to Buy and Sell Stock(股票问题)

    You are given an array prices where prices[i] is the price of a given stock on the ith day. You want ...

  3. 【刷题-LeetCode】121 Best Time to Buy and Sell Stock

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

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

  5. 【LeetCode】188. Best Time to Buy and Sell Stock IV 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. 【LeetCode】309. Best Time to Buy and Sell Stock with Cooldown 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  7. 【leetcode】714. 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 ...

  8. 【一天一道LeetCode】#121. Best Time to Buy and Sell Stock

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

  9. 【LeetCode】123. Best Time to Buy and Sell Stock III 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  10. 【LeetCode】122.Best Time to Buy and Sell Stock II 解题报告(Java & Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

随机推荐

  1. Oracle RAC时间同步(NTP/CTSS)

    1.RAC 相关时间同步(time synchronization)Oracle Grid可用两种方式进行时间同步1)基于OS的NTP2)基于clusterware的CTSS(Cluster Time ...

  2. Python内置模块之time、random、hashlib、OS、sys、UUID模块

    Python常用模块 1.time模块 在Python中,通常有这三种方式来表示时间:时间戳.元组(struct_time).格式化的时间字符串: (1)时间戳(timestamp) :通常来说,时间 ...

  3. zookeeper 选举和同步

    节点状态: // org.apache.zookeeper.server.quorum.QuorumPeer.ServerState public enum ServerState { LOOKING ...

  4. ActiveMQ 集群和主从

    举例说明:假设有 3 个 broker 节点,分别是61616,61618, 61620,其中 61616 和 61618 组成主.从节点,而 61616(或61618)和 61620 构成集群.61 ...

  5. openstack安装指南和在centos7上的安装指南

    openstack安装指南官网:http://docs.openstack.org/project-install-guide/newton/ openstack在centos7上的安装指南官网:ht ...

  6. Windows服务手动关闭教程

    Windows上关闭软件自启动,经常使用360等软件的开机加速功能去优化. 但有时候有些服务开机是启动的但在360中并没有找到那如何手动关闭这些服务的自启动呢? 下边以Autodesk Applica ...

  7. 利用VisualVm和JMX远程监控Java进程

    自Java 6开始,Java程序启动时都会在JVM内部启动一个JMX agent,JMX agent会启动一个MBean server组件,把MBeans(Java平台标准的MBean + 你自己创建 ...

  8. Fiddler系列教程1:初识Http协议抓包工具

    1. Fiddler简介 Fiddler是用一款使用C#编写的http协议调试代理工具.它支持众多的http调试任务,能够记录并检查所有你的电脑和互联网之间的http通讯,可以设置断点,查看所有的“进 ...

  9. Annotation方式配置AOP

    package com.xk.spring.kp04_aop.aop.s02_annotation; public interface IStudentService { public void sa ...

  10. python全栈开发笔记-----------概要

    Python开发 开发: 开发语言: 高级语言:python.Java.php  .C#   .Go .ruby . C++ .... ===>字节码 低级语言:C.汇编            ...