[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 ...
随机推荐
- CSS 点击事件
:active 伪类向激活(在鼠标点击与释放之间发生的事件)的元素添加特殊的样式. 这个伪类应用于处于激活状态的元素.最常见的例子就是在 HTML 文档中点击一个超链接:在鼠标按钮按下期间,这个链接是 ...
- 尚硅谷redis学习11-jedis操作redis
前面学习了redis的知识后,下面学习一下如何在java中通过jedis操作redis. 所需jar包:commons-pool-xxxx.jar,jedis-xxx.jar 下面看一下常用操作: 1 ...
- JS基础一-入门知识
一.什么是JavaScript JavaScript是一种基于对象和事件驱动并具有安全性能的解释性脚本语言.JavaScript不需要编译,直接嵌入在HTTP页面中,把静态页面转变成支持用户交互并响应 ...
- 讲一讲Servlet和JSP的关系
Test.jsp在运行时首先被解析成一个java类Test_jsp.java,该类继承于org.apache.jasper.runtime.HtppJspBase类,而Http又是继承HttpServ ...
- Android DevArt2:Android 5.0下 Dialog&AlertDialog 并不会影响Activity的生命周期
先给出结论:Dialog和AlertDialog并不会影响到Activity的生命周期,但会影响到Activity的优先级. 核心代码: onCreated中: Resources resources ...
- 【原】linux学习路径
1. <<The Linux Command Line A Complete Introduction>> 2. <<Advanced Programming ...
- Hibernate学习笔记3.1(Hibernate关系映射)
主要指对象之间的关系 1.一对一关联 一对一单项外键关联 比如说一夫一妻 Wifi.java package com.bjsxt.hibernate; import javax.persistence ...
- getElementsByTagName
1.getElementsByTagName * document.getelementByID(id名称)* 通过id名称获取元素,它只有一个主语,docment(整个文档)** getElemne ...
- 在使用 #import <objc/message.h>时 xcode 报 :Too many arguments to function call, expected 0 , have * 解决方法
选中项目 - Project - Build Settings -
- elasticsearch 问题
elasticsearch 的端口默认绑定到 127.0.0.1 上,对外开放 http 端口就配置 http.host,对外开放 tcp 端口就配置 network.host [1]: max fi ...