LeetCode:Best Time to Buy and Sell Stock II
2015-12-05 15:01
405 查看
Best Time to Buy and Sell Stock II
Total Accepted: 68970 TotalSubmissions: 171754 Difficulty: Medium
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).
题意:
给定一个股价数组,第i个数表示第i天的股价。求最大收益。
思路:
数组后一项大于前一项即为单次交易的收益,所有交易之和即为最大收益。
code:
class Solution { public: int maxProfit(vector<int>& prices) { int sum=0; for(int i=1;i<prices.size();i++){ int diff=prices[i]-prices[i-1]; if(diff>0) sum += diff; } return sum; } };
相关文章推荐
- 解决df和du查出来的已使用空间不同的问题
- 程序锁的分析一
- 【BZOJ1878】[SDOI2009]HH的项链 离线BIT
- rt3070 无线wifi模块移植到linux,并连接无线路由上网
- fatal error C1014: 包含文件太多 : 深度 = 1024
- UITableView 一直显示滚动条(ScrollBar Indicators)、滚动条Width(宽度)、滚动条Color(颜色)
- bzoj3261 最大异或和
- Vi编辑器的使用方法
- 5、UISlider的使用基于swift
- 高级语言虚拟机的一点理解,对比.NET和Java平台
- 数字转QString格式化
- 网站模板
- libEvent 使用
- 计算1/1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100 的值C语言代码
- 品牌为Synaptics的触控板如何外接鼠标后自动禁用触控板
- android 自定义流布局。实现热门标签。开源库SimpleFlowLayout
- nginx 源码学习笔记(十四)—— 全局变量ngx_cycle
- 166.Oracle数据库SQL开发之 XML和ORACLE——将XML保持到数据库中
- 第十四周实践项目3--二叉树排序
- 那些年,我遇到的bug