LeetCode 122 Best Time to Buy and Sell Stock II
来源:互联网 发布:oss js 上传 跨域 编辑:程序博客网 时间:2024/05/17 02:37
题目描述
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).
分析
只要有利润就卖,就是最优解。
代码
public static int maxProfit(int[] prices) { if (prices.length <= 1) { return 0; } int hold = prices[0]; int profit = 0; for (int i = 1; i < prices.length; i++) { if (hold < prices[i]) { profit += prices[i] - hold; } hold = prices[i]; } return profit; }
1 0
- LeetCode 122: Best Time to Buy and Sell Stock II
- LeetCode 122 Best Time to Buy and Sell Stock II
- LeetCode 122 Best Time to Buy and Sell Stock II
- LeetCode: Best Time to Buy and Sell Stock II [122]
- [leetcode 122] Best Time to Buy and Sell Stock II
- leetcode-122 Best Time to Buy and Sell Stock II
- leetcode || 122、Best Time to Buy and Sell Stock II
- LeetCode 122: Best Time to Buy and Sell Stock II
- Leetcode# 122 Best Time to Buy and Sell Stock II
- leetcode 122: Best Time to Buy and Sell Stock II
- leetCode #122 Best Time to Buy and Sell Stock II
- leetcode: (122) Best Time to Buy and Sell Stock II
- 122 Best Time to Buy and Sell Stock II [Leetcode]
- LeetCode 122: Best Time to Buy and Sell Stock II
- LeetCode 122 Best Time to Buy and Sell Stock II
- 【LEETCODE】122-Best Time to Buy and Sell Stock II
- [leetcode 122] Best Time to Buy and Sell Stock II
- 【leetcode】【122】Best Time to Buy and Sell Stock II
- MapReduce InputFormat之FileInputFormat
- 易错点:循环临界条件的设置(素数版)
- html5 css3 入门教程
- CSS基础知识
- Cognos组件介绍
- LeetCode 122 Best Time to Buy and Sell Stock II
- 数据结构实例参考——“查找”的原理
- LIB和DLL的区别与使用
- Python时间,日期,时间戳之间转换
- iOS书籍(OC/Swift 3)
- 黑马程序员——javaSE_正则表达式
- JDBC连接各种数据库的字符串
- POI+EL表达式 Excel导出功能
- hdu2036 改革春风吹满地 (求多边形面积,叉积)