LeetCode Best Time to Buy and Sell Stock II(贪心)
来源:互联网 发布:jquery alert json 编辑:程序博客网 时间:2024/05/22 17:36
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).
题意:给出一个数组,数组中元素表示股票在第i天的股价,可以交易任意次,求最大收益
思路:贪心算法。只要满足递增关系,就将相应的数相加即可
代码如下:
class Solution{ public int maxProfit(int[] prices) { int len = prices.length; int ans = 0; for (int i = 1; i < len; i++) { int diff = prices[i] - prices[i - 1]; if (diff > 0) { ans += diff; } } return ans; }}
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
- Leetcode 贪心 Best Time to Buy and Sell Stock III
- LeetCode 121 Best Time to Buy and Sell Stock (贪心)
- [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 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
- 【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
- utilities(C/C++)
- netty 高并发实战
- JVM memory
- 当鼠标进入li标签,给标签中的img元素加缩放效果
- VMware 11安装Mac OS X 10.10
- LeetCode Best Time to Buy and Sell Stock II(贪心)
- Android使用DrawerLayout和ToolBar实现仿知乎侧滑菜单
- JQuery DataTables
- 斯坦福机器学习在线demo
- 优质资源大搜集---你不容错过的IT课程
- PAT 1055. The World's Richest
- vim
- eclipse 常用快捷键最佳实践
- HDOJ 5605-geometry【数学】