Leetcode 贪心 Best Time to Buy and Sell Stock
本文为senlie原创。转载请保留此地址:http://blog.csdn.net/zhengsenlie
Best Time to Buy and Sell Stock
Total Accepted: 13234 Total
Submissions: 43145
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.
题意:给定一组数,表示某种股票每天的交易价格。求怎样进行一次买卖。使收益最大。
思路:贪心
买进价越低越好,卖出价越高越好。终于的目的是利润越高越好
设置两个变量,一个表示当前的最低卖出价cur_min_price,还有一个表示当前的最高利润max_profit
遍历数组prices。以当前值为卖出价
更新
max_profit = max(max_profit, prices[i] - cur_min_price)
cur_min_price = min(cur_min_price, prices[i])
复杂度:时间O(n),空间O(1)
相关题目:
Best Time to Buy and Sell Stock II
Best Time to Buy and Sell Stock III
class Solution {
public:
int maxProfit(vector<int> &prices) {
if (prices.size() < 2) return 0;
int cur_min_price = prices[0];
int max_profit = 0;
for(int i = 1; i < prices.size(); i++){
max_profit = max(max_profit, prices[i] - cur_min_price);
cur_min_price = min(cur_min_price, prices[i]);
}
return max_profit;
}
};
Leetcode 贪心 Best Time to Buy and Sell Stock的更多相关文章
- 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 ...
- [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] 122. Best Time to Buy and Sell Stock II 买卖股票的最佳时间 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] 123. Best Time to Buy and Sell Stock III 买卖股票的最佳时间 III
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] 188. Best Time to Buy and Sell Stock IV 买卖股票的最佳时间 IV
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] 309. Best Time to Buy and Sell Stock with Cooldown 买卖股票的最佳时间有冷却期
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 IV
Best Time to Buy and Sell Stock IV Say you have an array for which the ith element is the price of a ...
- [Leetcode Week6]Best Time to Buy and Sell Stock
Best Time to Buy and Sell Stock 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/best-time-to-buy-and ...
- [Leetcode][JAVA] Best Time to Buy and Sell Stock I, II, III
Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a gi ...
随机推荐
- 跳出双重for循环的案例__________跳出当前循环(continue out)
package com.etc.operator; public class demo { public static void main(String[] args) { // break out; ...
- 课上练习 script
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 【SQL】MERGE
MERGE可以合并多个表中的数据,也可实现多表中数据的同步.使用MERGE语句对表中数据进行有条件的更新和插入.当查找的行存在时,UPDATE更新行中的数据:当查找的行不存在时,INSERT插入数据. ...
- 【SQL】日期型函数
1. SYSTATE 用来返回系统当前时间 SQL> select sysdate from dual; SYSDATE ------------------- 2017-03-03 09:49 ...
- WinDbg使用
1.抓dump文件 程序崩溃(crash)的时候, 为了以后能够调试分析问题, 可以使用WinDBG要把当时程序内存空间数据都保存下来,生成的文件称为dump 文件. 步骤: 1) 打开WinDBG并 ...
- MySQL基础配置之mysql的默认字符编码的设置(my.ini设置字符编码)
MySQL基础配置之mysql的默认字符编码的设置(my.ini设置字符编码) MySQL的默认编码是Latin1,不支持中文,那么如何修改MySQL的默认编码呢,下面以设置UTF-8为例来说明. 需 ...
- cocos creator 底部按钮touch延迟
cocos论坛里有这个问题: http://forum.cocos.com/t/ios-touchstart-bug/63367我的引擎版本:"engine_version": & ...
- ubuntu18.0安装RabbitMQ
RabbitMQ是一个消息队列,用于实现应用程序的异步和解耦.生产者将生产消息传送到队列,消费中从队列中拿取消息并处理.生产者不用关心是谁来消费,消费者不用关系是谁在生产消息,从而达到解耦的目的.本文 ...
- 创建100个目录dir1-dir100一键完成
创建100个目录dir1-dir100将系统中已有文件xxx.txt复制1000份1.txt-1000.txt将文件1-10保存到第一个目录中11-20保存到第三个目录中的形式将所有文件处理完 #!/ ...
- BZOJ 1738: [Usaco2005 mar]Ombrophobic Bovines 发抖的牛 网络流 + 二分 + Floyd
Description FJ's cows really hate getting wet so much that the mere thought of getting caught in the ...