LeetCode 121. Best Time to Buy and Sell Stock (stock problem)
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.
Example 1:
Input: [7, 1, 5, 3, 6, 4]
Output: 5
max. difference = 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
In this case, no transaction is done, i.e. max profit = 0.
分析
首先通过把相邻的股票价格相减得到一组差价,后面就转化成了最大子列和问题了;另外注意当传进来的prices的数组为空时,返回0;
class Solution {
public:
int maxProfit(vector<int>& prices) {
if(prices.size()==0) return 0;
vector<int> profits(prices.size());
profits[0]=0;
for(int i=1;i<profits.size();i++)
profits[i]=prices[i]-prices[i-1];
int maxsum[profits.size()]={0},maxprofit=0;
for(int i=1;i<profits.size();i++){
maxsum[i]=max(profits[i],maxsum[i-1]+profits[i]);
maxprofit=max(maxprofit,maxsum[i]);
}
return maxprofit;
}
};
LeetCode 121. Best Time to Buy and Sell Stock (stock problem)的更多相关文章
- 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] 121. Best Time to Buy and Sell Stock_Easy tag: Dynamic Programming
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 (买卖股票的最好时机)
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- 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 ----- 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 最佳股票售卖时(动态规划,数组,模拟)
题目描述 已知一个数组,第i个元素表示第i天股票的价格,你只能进行一次交易(买卖各一次),设计算法找出最大收益 测试样例 Input: [7, 1, 5, 3, 6, 4] Output: 5 最大收 ...
随机推荐
- 《windows核心编程系列》七谈谈用户模式下的线程同步
用户模式下的线程同步 系统中的线程必须访问系统资源,如堆.串口.文件.窗口以及其他资源.如果一个线程独占了对某个资源的访问,其他线程就无法完成工作.我们也必须限制线程在任何时刻都能访问任何资源.比如在 ...
- 数据结构 - 链栈的实行(C语言)
数据结构-链栈的实现 1 链栈的定义 现在来看看栈的链式存储结构,简称为链栈. 想想看栈只是栈顶来做插入和删除操作,栈顶放在链表的头部还是尾部呢?由于单链表有头指针,而栈顶指针也是必须的,那干吗不让它 ...
- 题解报告:poj 2503 Babelfish(map)
Description You have just moved from Waterloo to a big city. The people here speak an incomprehensib ...
- 12c pdb expdp use DATA_PUMP_DIR meet ORA-39145
ORA-39002: invalid operation ORA-39070: Unable to open the log file. ORA-39087: directory name DATA_ ...
- java websocket 简单使用【案例】
现很多网站为了实现即时通讯,所用的技术都是轮询(polling).轮询是在特定的的时间间隔(如每1秒),由浏览器对服务器发 出HTTP request,然后由服务器返回最新的数据给客服端的浏览器.这种 ...
- win7打开网络看不到局域网的其他电脑
双击打开桌面上的“网络”,在打开的窗口中看不到局域网的其他电脑/计算机.以前都可以看到的.可能是没有开启网络发现的原因,可是我并没有关闭网络发现.不知,怎么回事? Windows7查看网络邻居要开启g ...
- Android学习笔记(十二) 线程
Android中的线程和Java中的线程使用方法类似,参考(四)Java基础知识回顾 MainThread与WorkerThread UI相关的线程都运行在主线程(MainThread/UIThrea ...
- dede自定义表单放首页出错的解决办法
一.当自定义表单放首页提交的时候跳出这个页面怎么解决 二.解决办法 可能有多个from表单提交出错,也就是代码冲突的意思,只要把代码检查好,from提交不要重复冲突就可以了
- bat批处理如何删除本地策略里的用户权限分配中的拒绝从网络访问本机项的guest用户?
echo [Version]>mm.inf echo signature="$CHICAGO$">>mm.inf echo Revision=1>>m ...
- chosen-bootstrap使用技巧
1.页面加载完成后,通过js方式设置值,无法有效显示的问题. 解决:先设置值,让后在进行初始化操作. // 设置select选中值 $("#type").val(type); // ...