[Leetcode]-Best Time to Buy and Sell Stock II
2015-07-22 19:09
471 查看
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).
Hide Tags: Array , Greedy
Hide Similar Problems:
(M) Best Time to Buy and Sell Stock
(H) Best Time to Buy and Sell Stock III
(H) Best Time to Buy and Sell Stock IV
题目:可以买卖多次股票,但是买之前必须要将手上的股票卖出。
思路:贪恋算法,只要prices[i+1]>prices[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).
Hide Tags: Array , Greedy
Hide Similar Problems:
(M) Best Time to Buy and Sell Stock
(H) Best Time to Buy and Sell Stock III
(H) Best Time to Buy and Sell Stock IV
题目:可以买卖多次股票,但是买之前必须要将手上的股票卖出。
思路:贪恋算法,只要prices[i+1]>prices[i],那么就执行买卖。
int maxProfit(int* prices, int pricesSize) { int i = 0; int maxprofit = 0; for(i=0;i<pricesSize-1;i++) { if(prices[i+1] > prices[i]) maxprofit += prices[i+1] - prices[i]; } return maxprofit; }
相关文章推荐
- 记忆化搜索 POJ1579
- vmware虚拟化(二)安装vsphere5.1
- 手机安全卫士------Splash页面
- C语言总结
- HDOJ 1862 EXCEL排序
- 不规则图片气泡蒙版的制作
- HDU 2476 String painter
- 解决安卓开发文档docs打开过慢的问题
- HDU-1106 排序
- 用户及文件权限管理
- Java集合
- 新手搭建ThinkPHP框架
- HDOJ 2087 剪花布条----strstr函数
- Android blueZ HCI(一个):hciconfig实施和经常使用
- NOJ1064快速排序(基础的快速排序,以及动态数组的使用)
- JAVA之堆内存和栈内存的区别
- 透视投影
- Android开发初学者问题
- 实用中时间戳与日期互转
- HDOJ 2027 单词数