leetcode_Best Time to Buy and Sell Stock II
来源:互联网 发布:redis mysql 编辑:程序博客网 时间:2024/06/05 19:40
描述:
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).
思路:
由于交易次数不限,所以将其所有大于0的利润相加即可,为何要这样出题?
代码:
public int maxProfit(int[] prices) { if(prices==null) return 0; int len=prices.length; if(len==0) return 0; int temp=0; int maxProfit=0; for(int i=1;i<len;i++) { temp=prices[i]-prices[i-1]; if(temp>0) maxProfit+=temp; } return maxProfit; }
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
- leetcode_Best Time to Buy and Sell Stock
- LeetCode_best-time-to-buy-and-sell-stock
- LeetCode_Best Time to Buy and Sell Stock
- Leetcode_Best Time to Buy and Sell Stock
- Leetcode_best-time-to-buy-and-sell-stock-ii(c++ and python version)
- Leetcode_best-time-to-buy-and-sell-stock-iii
- leetcode_Best Time to Buy and Sell Stock III
- leetcode_Best Time to Buy and Sell Stock III
- Leetcode_best-time-to-buy-and-sell-stock(c++ and python version)
- [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
- 编译c#项目,在项目属性中,平台与目标平台的区别是什么?
- plsql查询结果乱码问题
- Emmet 使用说明。
- struts2 实现图片验证码(完整代码)
- 利用c++模板实现快速排序
- leetcode_Best Time to Buy and Sell Stock II
- Android 问题集锦 adb
- Nginx介绍、安装与配置
- ppt转换成pdf在线的转换方法
- db2新建实例+客户端配置连接
- 将一个用户下所有表的查询权限赋给另一个用户
- 黑马程序员-【异常处理】
- 二进制 装多个 mysql (5.6和以下版本)
- android mipmap文件夹