【刷题-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 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的更多相关文章
- 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 ...
- 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 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...
- [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 ...
- 【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 ...
- 【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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- [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 ...
随机推荐
- SP16033 TIPTOP - Tip Top Game 题解
Description Alim 和 Sufian 是好朋友.他们最近找到了一个好玩的游戏,叫做 Tip Top.游戏规则如下: 确定一个整数. 找出这个整数的所有因子. Alim 先手,每人轮流取一 ...
- LuoguP4263 [Code+#3]投票统计 题解
Content 有 \(t\) 组询问,每组询问给定一个长度为 \(n\) 的数列,请将出现次数最多的数按照从小到大的顺序输出,或者这些数在数列中出现的次数都相等. 数据范围:\(t\) 未知,\(n ...
- Razor语法2
语法名称 Razor 语法 Web Forms 等效语法 代码块 @{ int x = 123; string y = "because."; } <% int x = 12 ...
- Python3 第五周大纲(模块,导入方法、import本质,导入优化,模块的分类)
1.定义: 模块:逻辑上组织python代码(变量.函数.类.逻辑:实现一个功能,本质是.py结尾的文件) 2.导入方法 import module_name,module_name2,...... ...
- ESP8266学习实战之UdpClient与UdpSever(FreeRTOS)
Udpclient 任务流程 ①判断是否获取ip地址 新建状态变量 STATION_STATUS stastatus; 调用wifi接口,并判断是否获取IP地址 ·do { stastatus = w ...
- Postman环境变量的使用
前言 请注意,Postman新版有ui上的改动,本文使用的Postman 版本8.4.0 for Mac, ui有调整,但是功能无改变. Postman是一款接口调测的软件,服务端开发的同学肯定会对自 ...
- IDEA微服务项目SpringBoot一键(批量)顺序启动
找到 搜索 RunDashboard <option name="configurationTypes"> <set> <option value=& ...
- Linux生成SSH密钥对
执行 ssh-keygen -t rsa -P "" -f "/root/.ssh/id_rsa" 进入 cd /root/.ssh目录 (这里的root 是因 ...
- Mac下好用的“visio”之 OmniGraffle Pro
!!版权声明:本文为博主原创文章,版权归原文作者和博客园共有,谢绝任何形式的 转载!! 作者:mohist 1.官方网站:https://www.omnigroup.com/omnigraffle/ ...
- 【LeetCode】688. Knight Probability in Chessboard 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/knight-pr ...