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 ...
随机推荐
- 《JavaScript 闯关记》之表达式和运算符
表达式 表达式是由数字.运算符.数字分组符号(如括号).自由变量和约束变量等以能求得数值的有意义排列方法所得的组合.JavaScript 表达式主要有以下几种形式: 原始表达式:常量.变量.保留字. ...
- ARCGIS接口详细说明
ArcGIS接口详细说明 目录 ArcGIS接口详细说明... 1 1. IField接口(esriGeoDatabase)... 2 2. IFieldEdit接口(esriGe ...
- win10下安装通过Hyper-v安装Ubuntu
一直也来在做C#的开发,Winform及Web都有所涉及,想在闲暇之余学习下Python,拓展一下自己的知识.既然决定学习Python那么就直接在Linux下进行吧,由于Ubuntu最近很火而且也有方 ...
- C++程序设计实践指导1.9统计与替换字符串中的关键字改写要求实现
改写要求1:将字符数组str改为字符指针p,动态开辟存储空间 改写要求2:增加统计关键字个数的函数void CountKeyWords() 改写要求3: 增加替换函数void FindKeyWords ...
- MySql5压缩包安装
一. 解压所有文件到一个目录:例如D:\Program Files\mysql-5.6.22-winx64 二. 配置系统的环境变量:在Path路径后追加:;D:\Program Files\mysq ...
- 七牛云Fetch第三方资源并转码(PHP版)
七牛云的图片加速一直在用,好用没得说,最近项目需要做个微信端录音,然后上传,别人试听的功能,录音和上传用的都是微信的接口,有文档,比较方便,但是上传后,微信只给保存3天,所以就下载到了七牛,也就用到了 ...
- dict.get('key')和dict['key']的区别
dict['key']只能获取存在的值,如果不存在会触发KeyError dict.get('key', default=None)如果不存在,返回一个默认值
- HDU 1312 Red and Black(bfs)
Red and Black Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Descr ...
- 03-树1. List Leaves (25)
Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. I ...
- UI产品设计流程中的14个要点
http://www.sj33.cn/digital/wyll/201404/38318.html 自从我在 Dribbble 上贴了一幅我的产品设计成果,受到了大家伙热烈的反馈,对此我深受鼓励,我决 ...