122. Best Time to Buy and Sell Stock II
来源:互联网 发布:茶叶推广方案 知乎 编辑:程序博客网 时间:2024/06/05 11:17
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/#/description
这个题允许股票进行多次买卖,目标是计算总的最大获益。
基本思路是用贪心算法,只要能挣到钱(下一个数字比上一个数字大),就进行买卖。
public class Solution {
public int maxProfit(int[] prices) {
int sum = 0;
if(prices.length == 0)
return 0;
//只要挣钱,就卖了
for(int i = 1;i < prices.length;i ++){
if(prices[i] > prices[i-1]){
sum += prices[i] - prices[i-1];
}
}
return sum;
}
}
0 0
- 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] 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】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
- 122. Best Time to Buy and Sell Stock II
- 122. Best Time to Buy and Sell Stock II
- 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 122. Best Time to Buy and Sell Stock II
- leetcode 122. Best Time to Buy and Sell Stock II
- ubuntu 下的PDF阅读器
- cs231n 课程作业 Assignment 2
- 微信视频直播服务
- RHEL7--UNI3--MAN使用方法
- LeetCode-Easy部分中标签为String 58. Length of Last Word
- 122. Best Time to Buy and Sell Stock II
- 海尔“小微”怎么玩
- OPENCV threshold type类型选择
- javascript中prototype、constructor以及__proto__之间的三角关系
- java,hibernate和sql server对应的数据类型表
- React之ref详细用法
- Pixhawk原生固件PX4之驱动ID
- OpenCV图像拷贝方法与注意点(=、copy、clone)
- Struts2高位漏洞升级到struts2.3.32