1. Best Time to Buy and Sell Stock

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 (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.

Note that you cannot sell a stock before you buy one.

Example 1:

Input: [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
Not 7-1 = 6, as selling price needs to be larger than buying price.

Example 2:

Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.

解法1 暴力求解,计算出\(\max_{i < j} \{prices[j] - prices[i]\}\)

解法2 one-pass。在全局最低点买入,卖出一定在该点之后,因此一边寻找min_p,一边计算max_profit

class Solution {
public:
int maxProfit(vector<int>& prices) {
int n = prices.size();
int profit = 0;
int min_p = INT_MAX;
for(int i = 0; i < n; ++i){
if(prices[i] < min_p)min_p = prices[i];
profit = max(profit, prices[i] - min_p);
}
return profit;
}
};

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

  1. 30. leetcode 121. Best Time to Buy and Sell Stock

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

  2. leetcode 121. Best Time to Buy and Sell Stock 、122.Best Time to Buy and Sell Stock II 、309. Best Time to Buy and Sell Stock with Cooldown

    121. Best Time to Buy and Sell Stock 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...

  3. [LeetCode] 121. Best Time to Buy and Sell Stock 买卖股票的最佳时间

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

  4. 【leetcode刷题笔记】Best Time to Buy and Sell Stock II

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

  5. 【leetcode刷题笔记】Best Time to Buy and Sell Stock III

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

  6. Java for LeetCode 121 Best Time to Buy and Sell Stock

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

  7. LeetCode 121. Best Time to Buy and Sell Stock (买卖股票的最好时机)

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

  8. leetcode 121. Best Time to Buy and Sell Stock ----- java

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

  9. Python [Leetcode 121]Best Time to Buy and Sell Stock

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

  10. [leetcode]121. Best Time to Buy and Sell Stock 最佳炒股时机

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

随机推荐

  1. LuoguP7008 [CERC2013]What does the fox say? 题解

    Content 森林里面有很多声响,你想知道有哪些声响是由狐狸发出来的. 已知你搜集到了 \(n\) 个声响,并且还知道某些其他动物能够发出的声响,已知如果没有哪一个声响是由其他任何一种动物发出来的话 ...

  2. ORM-数据库命令操作包装实例对象学习

    http://www.cnblogs.com/alex3714/articles/5978329.html python 之路,Day11 - sqlalchemy ORM   本节内容 ORM介绍 ...

  3. 【论文笔记】 Popularity Bias in Dynamic Recommendation

    Popularity Bias in Dynamic Recommendation Authors: Ziwei Zhu, Yun He, Xing Zhao, James Caverlee KDD' ...

  4. windows10下vscode+cmake编译Qt5代码

    概述 本文演示环境: windows10 cmake version:3.18 qt version: 5.14 vscode version: 1.54.1 本文演示环境基于在线模式, 离线使用VS ...

  5. xcode导出手机的崩溃日志

    1.第一步将手机连接xcode 点击window 2.选择 Device and Simulators 3.选择View Device Logs 4.导出文件 右键鼠标选择Export Logs 5. ...

  6. xcode 常用指令

    使用LLDB进行调试时,如何打印一个数组:p *(int(*)[10])ptr或者是从ptr的第3个元素开始显示10个元素p *(int(*)[10])&ptr[3]

  7. 【LeetCode】424. 替换后的最长重复字符 Longest Repeating Character Replacement(Python)

    作者: 负雪明烛 id: fuxuemingzhu 公众号:每日算法题 本文关键词:LeetCode,力扣,算法,算法题,字符串,双指针,刷题群 目录 题目描述 题目大意 解题方法 双指针 代码 欢迎 ...

  8. 1298 - One Theorem, One Year

    1298 - One Theorem, One Year   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: ...

  9. Improving Variational Auto-Encoders using Householder Flow

    目录 概 主要内容 代码 Tomczak J. and Welling M. Improving Variational Auto-Encoders using Householder Flow. N ...

  10. On the Optimization of Deep Networks: Implicit Acceleration by Overparameterization

    目录 引 主要内容 定理1 Claim 1 Claim 2 定理2 证明 定理1的证明 Claim 1 的证明 Kronecker product (克罗内克积) Theorem 2 的证明 代码 A ...