[leetcode]122. Best Time to Buy and Sell Stock II 最佳炒股时机之二
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 (i.e., buy one and sell one share of the stock multiple times).
Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).
Example 1:
- Input: [7,1,5,3,6,4]
- Output: 7
- Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.
- Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.
题目
和之前一样,不过你可以买卖任意多次。
思路
相当于从专门做短线操作(short-term operation) , 只要第二天相对前一天有上涨,就transaction
if previous price > current price, do transaction
profit += previous price - current price
代码
- class Solution {
- public int maxProfit(int[] prices) {
- int profit = 0;
- for (int i = 0; i < prices.length - 1; i++) {
- int diff = prices[i+1] - prices[i];
- if (diff > 0) {
- profit += diff;
- }
- }
- return profit;
- }
- }
[leetcode]122. Best Time to Buy and Sell Stock II 最佳炒股时机之二的更多相关文章
- [leetcode]123. Best Time to Buy and Sell Stock III 最佳炒股时机之三
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 31. leetcode 122. Best Time to Buy and Sell Stock II
122. Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price ...
- [LeetCode] 122. Best Time to Buy and Sell Stock II 买卖股票的最佳时间 II
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- LeetCode 122. Best Time to Buy and Sell Stock II (stock problem)
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- LeetCode 122. Best Time to Buy and Sell Stock II (买卖股票的最好时机之二)
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- leetcode 122. Best Time to Buy and Sell Stock II ----- java
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- Java [Leetcode 122]Best Time to Buy and Sell Stock II
题目描述: Say you have an array for which the ith element is the price of a given stock on day i. Design ...
- LeetCode 122 Best Time to Buy and Sell Stock II 解题报告
题目要求 Say you have an array for which the ith element is the price of a given stock on day i. Design ...
- Java for LeetCode 122 Best Time to Buy and Sell Stock II
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
随机推荐
- eclipse,import,导入项目显示红色叹号
[场景]eclipse导入项目后,该项目出现红色叹号.打开类文件,会有无数的“The type ... cannot be resolved.”等稀奇古怪的编译错误. [分析]当前eclipse环境中 ...
- GCD 常用API 总结
dispatch_sync:同步操作,会阻塞当前线程 dispatch_async:普通的异步操作,也就是在指定的队列中添加一个block操作,不会阻塞当前线程 dispatch_group_asyn ...
- centos 卸载mysql
1 删除Mysql yum remove mysql mysql-server mysql-libs mysql-server; find / -name mysql 将找到的相关东西delete掉 ...
- html页面跳转
button <button onclick="window.location.href='edit.jsp'">完善个人信息</button> 单击提交表 ...
- PHP多例模式介绍_PHP教程
1.多例类可以有多个实例2.多例类必须能够自我创建并管理自己的实例,并向外界提供自己的实例. 大家都知道PHP单例模式,却很少说PHP多例模式,下面是在wikipedia上看到的PHP多例模式的例子: ...
- js实现ctrl+v粘贴上传图片(兼容chrome、firefox、ie11)【转载】
我们或多或少都使用过各式各样的富文本编辑器,其中有一个很方便功能,复制一张图片然后粘贴进文本框,这张图片就被上传了,那么这个方便的功能是如何实现的呢? 原理分析 提取操作:复制=>粘贴=> ...
- [PHP]基于角色的访问控制RBAC
---------------------------------------------------------------------------------------------------- ...
- Python读取图片尺寸、图片格式
Python读取图片尺寸.图片格式 需要用到PIL模块,使用pip安装Pillow.Pillow是从PIL fork过来的Python 图片库. from PIL import Image im = ...
- kickstart自动安装部署RHEL7
Kickstart是一种无人值守的安装方式.它的工作原理是在安装过程中记录典型的需要人工干预填写的各种参数,并生成一个 名为ks.cfg的文件.如果在安装过程中(不只局限于生成Kickstart安装文 ...
- ubuntu 下安装和启动SSH 服务
安装OPENSSH 服务端 sudo apt-get install openssh-server 查看进程是否启动 ps -e | grep ssh 删除密钥文件 rm /etc/ssh/ssh_h ...