Best Time to Buy and Sell Stock II
来源:互联网 发布:元数据是关于数据的 编辑:程序博客网 时间:2024/05/02 02:35
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 maxProfitMulti(int *prices, int n) { int i, j, sum = 0; if(n == 0) return 0; for(i = 0 ;i < n - 1; i++ ) { if(prices[i + 1] > prices[i]) { for(j = i; j < n - 1 && prices[j] < prices[j + 1]; j++); if(j != i ) { sum += prices[j] - prices[i]; //printf("%d\n", sum); } i = j; } } return sum;}void main() { int prices[] = {1,2}; int n = 2; printf("%d\n", maxProfitMulti(prices, n));}
0 0
- [LeetCode]Best Time to Buy and Sell Stock II
- LeetCode Best Time to Buy and Sell Stock II
- Leetcode: Best Time to Buy and Sell Stock II
- [Leetcode] Best Time to Buy and Sell Stock II
- Best Time to Buy and Sell Stock II
- LeetCode : Best Time to Buy and Sell Stock II
- LeetCode Best Time to Buy and Sell Stock II
- [LeetCode] Best Time to Buy and Sell Stock II
- leetcode 61: Best Time to Buy and Sell Stock II
- [LeetCode] Best time to buy and sell a stock II
- Best Time to Buy and Sell Stock II
- Best Time to Buy and Sell Stock II
- 【leetcode】Best Time to Buy and Sell Stock II
- LeetCode - Best Time to Buy and Sell Stock II
- Best Time to Buy and Sell Stock II
- Best Time to Buy and Sell Stock II
- LeetCode之Best Time to Buy and Sell Stock II
- [LeetCode]Best Time to Buy and Sell Stock II
- tcp 的自连接问题
- kindeditor学习
- sed命令详解
- 匿名内部类
- 总结数据挖掘预测分类中的样本筛选和特征处理
- Best Time to Buy and Sell Stock II
- SharePoint 2013 App 开发 (2) - 建立开发环境
- ubus [3] - cli
- 五款常用mysql slow log分析工具的比较分析
- 使用VS2012 MFC自定义消息时,处理函数不能返回void类型。
- 求1~100之间不能被3整除的数之和,还有统计1~100不能被整除的个数
- [翻译]PEP 7 -- C语言风格指南
- af62b36b0636cc52db30af91344a2f71 (学生实验,请支持谢谢)
- Objective-C中nil使用的最佳实践