LeetCode - BestTimetoBuyandSellStockII
来源:互联网 发布:流动商贩全国数据 编辑:程序博客网 时间:2024/06/05 10:26
/**
* 问:已知某股票的价格数组,且该股票可买卖很多次,但是只有卖了才能重新买。求最大收益。
* 解:贪心算法:只要能赚钱就卖。
*/public class BestTimetoBuyandSellStockII {
public int maxProfit(int[] prices) {
int result = 0;
for (int i=0; i<prices.length-2; i++) {
if (prices[i+1] >prices[i])
result += prices[i+1] - prices[i];
}
return result;
}public static void main(String[] args) {
int[] prices = {9, 1, 2, 8, 3, 7};
System.out.println("最大收益:" + new BestTimetoBuyandSellStockII().maxProfit(prices));
}}
0 0
- LeetCode - BestTimetoBuyandSellStockII
- 122BestTimetoBuyandSellStockII.java
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode
- leetcode:
- leetcode:
- LeetCode
- leetcode
- LEETCODE
- leetcode
- leetCode
- leetcode
- [leetcode]
- LeetCode
- leetcode
- LeetCode - BestTimetoBuyandSellStockI
- DB_NAME、DBID、DB_UNIQUE_NAME、SERVICE_NAME、SID、INSTANCE_NAME、GLOBAL_DATABASE_NAME
- 常用设计模式大全-详细介绍
- Linux ACL
- 常见SQL异常及解决方法
- LeetCode - BestTimetoBuyandSellStockII
- 并行分布式计算的同步问题
- LeetCode - BalancedBinaryTree
- 哈哈
- LeetCode - Anagrams
- js获取客户端信息
- 即时通讯demo
- 二叉树问题之summary^_^(1)
- android studio for android learning (六)在布局中常使用的单位