Best Time to Buy and Sell Stock I,II,III [leetcode]
Best Time to Buy and Sell Stock I
你只能一个操作:维修preMin拍摄前最少发生值
代码例如以下:
int maxProfit(vector<int> &prices) {
if (prices.size() == 0) return 0;
int profit = 0;
int preMin = prices[0];
for (int i = 1; i < prices.size(); i++)
{
if (prices[i] < preMin) preMin = prices[i];
profit = max(profit, prices[i] - preMin);
}
return profit;
}
Best Time to Buy and Sell Stock II
能无限次操作时:当==>当前价格 > 买入价格的时候,卖出
代码例如以下:
int maxProfit(vector<int> &prices) {
if (prices.size() == 0) return 0;
int profit = 0;
int buy = prices[0];
for (int i = 1; i < prices.size(); i++)
{
if (buy < prices[i])
profit += prices[i] - buy;
buy = prices[i];
}
return profit;
}
Best Time to Buy and Sell Stock III
仅仅能操作两次时:
两次操作不能重叠。能够分成两部分:0...i的最大利润fst 和i...n-1的最大利润snd
代码例如以下:
int maxProfit(vector<int> &prices) {
if (prices.size() == 0) return 0;
int size = prices.size();
vector<int> fst(size);
vector<int> snd(size);
int preMin = prices[0];
for (int i = 1; i < size; i++)
{
if (preMin > prices[i]) preMin = prices[i];
fst[i] = max(fst[i - 1], prices[i] - preMin);
}
int profit = fst[size - 1];
int postMax = prices[size - 1];
for (int i = size - 2; i >= 0; i--)
{
if (postMax < prices[i]) postMax = prices[i];
snd[i] = max(snd[i + 1], postMax - prices[i]);
//update profit
profit = max(profit, snd[i] + fst[i]);
}
return profit;
}
版权声明:本文博主原创文章。博客,未经同意不得转载。
Best Time to Buy and Sell Stock I,II,III [leetcode]的更多相关文章
- [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 ...
- LeetCode:Best Time to Buy and Sell Stock I II III
LeetCode:Best Time to Buy and Sell Stock Say you have an array for which the ith element is the pric ...
- 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 ...
- leetcode day6 -- String to Integer (atoi) && Best Time to Buy and Sell Stock I II III
1. String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully con ...
- 解题思路:best time to buy and sell stock i && ii && iii
这三道题都是同一个背景下的变形:给定一个数组,数组里的值表示当日的股票价格,问你如何通过爱情买卖来发家致富? best time to buy and sell stock i: 最多允许买卖一次 b ...
- LeetCode之“动态规划”:Best Time to Buy and Sell Stock I && II && III && IV
Best Time to Buy and Sell Stock I 题目链接 题目要求: Say you have an array for which the ith element is the ...
- [LeetCode] 递推思想的美妙 Best Time to Buy and Sell Stock I, II, III O(n) 解法
题记:在求最大最小值的类似题目中,递推思想的奇妙之处,在于递推过程也就是比较求值的过程,从而做到一次遍历得到结果. LeetCode 上面的这三道题最能展现递推思想的美丽之处了. 题1 Best Ti ...
- Best Time to Buy and Sell Stock I II III IV
一.Best Time to Buy and Sell Stock I Say you have an array for which the ith element is the price of ...
- 123. Best Time to Buy and Sell Stock (三) leetcode解题笔记
123. Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the pric ...
随机推荐
- linux使用进阶(一)
本文依据<应该知道的Linux技巧>coolshell上的一篇文章提到的Linux技巧,结合自己掌握的情况进行扩展和总结得来.主要包含下面内容: 一.日常操作 二.数据处理 ...
- Extjs4.2 Desktop 拖动黑色和白色的桌面图标的解决方案
最近做了一个extjs4.2的desktop桌面demo,该desktop从原来的包中剥离出来,并实现了桌面图标休息,拖动桌面图标,但是,用户抱怨拖动桌面图标会出现黑色和白色,测试,在 extjs4. ...
- selenium让人摸不着头脑的问题
selenium让人摸不着头脑的问题 问题一 使用webdriver驱动firefox浏览器时如果不设置参数,默认使用的Firefox的profile和平时打开浏览器使用的firefox不一样,如果要 ...
- Maven POM入门
Super POM(project object model) Maven内置了一个默认的POM(不在项目中,因此不可见),每一个project都会继承自这个默认的POM,因此叫Super POM.除 ...
- 基于FP-Tree的关联规则FP-Growth推荐算法Java实现
基于FP-Tree的关联规则FP-Growth推荐算法Java实现 package edu.test.ch8; import java.util.ArrayList; import java.util ...
- hdu4035(概率dp)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4035 题意:有n个房间,由n-1条隧道连通起来,实际上就形成了一棵树, 从结点1出发,开始走,在每个结 ...
- RequireJS学习资料
RequireJS学习资料汇总 入门系列 [1]阮一峰 RequireJS用法 [2]RequireJS入门指南 文档系列 [1]RequireJS中文文档 [2]RequireJS英文文档 代码 ...
- linux yum命令
1 安装yum install 全部安装yum install package1 安装指定的安装包package1yum groupinsall group1 安装程序组group1 2 更新和升级y ...
- 【Java】运用JDBC实现一个注册、登录系统的编写
数据库的建立 首先,建立一个数据库,存储注册成功的账户信息. 其SQL的DDL语句如下: CREATE TABLE `jdbctest` ( `id` int(10) NOT NULL auto_in ...
- ZooKeeper场景实践:(2)集中式配置管理
1. 基本介绍 在分布式的环境中,可能会有多个对等的程序读取相同的配置文件,程序能够部署在多台机器上,假设配置採用文件的话,则须要为部署该程序的机器也部署一个配置文件,一旦要改动配置的时候就会很麻烦, ...