Best Time to Buy and Sell Stock I && II
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.
找到最大顺序差值
int maxProfit(int* prices, int pricesSize) {
int min = 0;
int max = 0;
int result_max = 0;
// go over array
if(pricesSize == 0)
return 0;
min = max = prices[0];
for(int i = 1; i < pricesSize; i++){
// find the first min
if(min > prices[i]){
min = prices[i];
max = prices[i];
}
// then find the first max
if(max < prices[i]){
max = prices[i];
}
// get the max result
if(result_max < max - min)
result_max = max - min;
}
return result_max;
}
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
得到可以赚到的最大值
int maxProfit(int* prices, int pricesSize) {
int min = 0;
int max = 0;
int sum = 0;
if(pricesSize == 0)
return 0;
min = max = prices[0];
// go over the array
for(int i = 1; i < pricesSize; i++){
// pass the prices less than front price
if(min > prices[i]){
min = prices[i];
max = prices[i];
}
// if we will have owner sell it and buy it again
if(max < prices[i]){
max = prices[i];
sum += max - min;
min = prices[i];
}
}
return sum;
}
- 这里min和max的名字不太符合,追赶法更合适
- 如果后面的值比前面的大就把差值加到sum上,然后重置min
Best Time to Buy and Sell Stock I && II的更多相关文章
- [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 ...
- [leetcode]_Best Time to Buy and Sell Stock I && II
一个系列三道题,我都不会做,google之答案.过了两道,第三道看不懂,放置,稍后继续. 一.Best Time to Buy and Sell Stock I 题目:一个数组表示一支股票的价格变换. ...
- 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 [leetcode]
Best Time to Buy and Sell Stock I 你只能一个操作:维修preMin拍摄前最少发生值 代码例如以下: int maxProfit(vector<int> & ...
- 解题思路: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 ...
- 【数组】Best Time to Buy and Sell Stock I/II
Best Time to Buy and Sell Stock I 题目: Say you have an array for which the ith element is the price o ...
- [LeetCode] 递推思想的美妙 Best Time to Buy and Sell Stock I, II, III O(n) 解法
题记:在求最大最小值的类似题目中,递推思想的奇妙之处,在于递推过程也就是比较求值的过程,从而做到一次遍历得到结果. LeetCode 上面的这三道题最能展现递推思想的美丽之处了. 题1 Best Ti ...
随机推荐
- 归并排序java
import java.util.Arrays; public class MergeSort { public static void main(String[] args) { MergeSort ...
- 如何合并相同数据并转置(mysql)实现
上次参加天猫大数据竞赛 是预测用户会买哪些牌子给用户推荐 拥有的字段 user_id,brand_id 最后要转成如下格式,比如用户1,要给他推荐2,3,4号品牌 原来的数据是 user_id,bra ...
- 关于Storyboard
界面的搭建器IB有两种 1.stroyboard (不仅可以搭建界面 并且可以实现界面之间的关系) 2.xib (只能是单个视图或者界面) 如何配置自己创建的storyboard 1.infoPlis ...
- HDU 4798 - Skycity
告诉你一幢楼的高度,楼的层数,每层一样高. 每一层的底边是一个圆,下一层的玻璃一定要包括进上一层的底边. 每层玻璃铺成棱柱形,玻璃有最小面积限制. 问你这层楼最小的总玻璃数是多少. 求出每层最小的玻璃 ...
- [Linked List]Convert Sorted List to Binary Search Tree
Total Accepted: 57775 Total Submissions: 198972 Difficulty: Medium Given a singly linked list where ...
- jeasyui制作计划-ajax学习
Ajax:可以无刷新状态更新页面,并且实现异步提交,提升了用户的体验. 1.load()函数的使用,可以三个参数:url(必须的参数,请求html文件的url地址,参数类型string).date(可 ...
- 深入浅析mysql引擎
mysql引擎 mysql数据库引擎取决于mysql在安装的时候是如何被编译的.要添加一个新的引擎,就必须重新编译mysql.在缺省情况下,mysql支持三个引擎:ISAM,MYISAM和HEAP.另 ...
- windows查询端口
依次点击“开始→运行”,键入“cmd”并回车,打开命令提示符窗口.在命令提示符状态下键入“netstat -an”,按下回车键后就可以看到以数字形式显示的TCP和UDP连接的端口号及状态.
- 认识JSONP
jsonp 全称是JSON with Padding 我们大家都知道ajax是不能发起跨域请求,现在可以通过jsonp来弥补ajax的这一缺陷 通过script标签的src属性就可以实现跨域请求.如( ...
- Oracle EBS-SQL (BOM-15):检查多层BOM(含common BOM).sql
select distinct b.lvl 层次, b.OPERATION_SEQ ...