Best Time to Buy and Sell Stock II
来源:互联网 发布:企业危机网络 编辑:程序博客网 时间:2024/06/06 20:04
1.题目
假设有一个数组,它的第i个元素是一个给定的股票在第i天的价格。设计一个算法来找到最大的利润。你可以完成尽可能多的交易(多次买卖股票)。然而,你不能同时参与多个交易(你必须在再次购买前出售股票)。
给出一个数组样例[2,1,2,0,1], 返回 2
2.算法
从前向后遍历数组,只要当天的价格高于前一天的价格,就算入收益。
public int maxProfit(int[] prices) { if(prices == null || prices.length==0) return 0; int res = 0; for(int i=0;i<prices.length-1;i++) { int diff = prices[i+1]-prices[i]; if(diff>0) res += diff; } return res; }
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
- maven安装与配置
- Android Studio常用插件—— JsonOnlineViewer
- NOIP2007 提高组 复赛 count 统计数字
- 设计模式六大原则之--里氏替代原则(LSP)
- java项目转web
- Best Time to Buy and Sell Stock II
- Combinations
- uml(unified modeling language)用例图、类图、序列图详解
- SVN同步之Android studio 与新浪云(SAE)
- Ajax的GET与POST和Ajax文件上传进度条
- JAVA如何在窗口设置背景图片
- 75篇关于Tomcat源码和机制的文章
- 基于arcgis的城市放射状指数研究
- 设计模式六大原则之--单一职责原则(SRP)