【LeetCode】Best Time to Buy and Sell Stock II 解题报告
来源:互联网 发布:成都软件专修学院 编辑:程序博客网 时间:2024/06/07 00:59
【LeetCode】Best Time to Buy and Sell Stock II 解题报告
标签(空格分隔): LeetCode
题目地址:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/#/description
题目描述:
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).
Ways
这个题有点搞笑,根本不是贪心算法,这个题不是说可以任意的挑选,而是要按照每天的顺序挑选。这样就很简单了,只要后面的一天比这天的价格高就买入卖出就可。
public class Solution { public int maxProfit(int[] prices) { int len = prices.length; int ans = 0; for(int i = 0; i < len - 1; i++){ if(prices[i + 1] > prices[i]){ ans += prices[i + 1] - prices[i]; } } return ans; }}
Date
2017 年 4 月 20 日
0 0
- LeetCode-Best Time to Buy and Sell Stock-解题报告
- 【LeetCode】Best Time to Buy and Sell Stock 解题报告
- 【LeetCode】 Best Time to Buy and Sell Stock I II III IV 解题报告
- LeetCode-Best Time to Buy and Sell Stock II-解题报告
- 【LeetCode】 Best Time to Buy and Sell Stock I II III IV 解题报告
- [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 股票买卖2 解题报告
- LeetCode解题报告 122. Best Time to Buy and Sell Stock II [medium]
- 【LeetCode】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
- 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
- Android系统设置命令— android.provider.Settings
- Maven打包war
- 日间模式切换
- Java实现MD5加密(一)
- 监听输入框
- 【LeetCode】Best Time to Buy and Sell Stock II 解题报告
- RHEL7--UNIT14-linux设备访问
- android 自定义控件
- oracle中rownum、rowid、row_number()、rank()、dense_rank()的用法
- mysql查看表计划
- linux 用vi命令的使用以及vi编辑后的后续保存退出等相关命令的使用
- lattice 包高级绘图函数
- 在Linux系统中,在防火墙中添加端口
- echarts各个配置项详细说明总结