Best Time to Buy and Sell Stock II ——LeetCode
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).
题目大意:跟上一题类似,都是求最大获利,上一题要求只能买卖一次,这次可以多次买卖。
解题思路:从第2天开始,只要是比前一天价格低就不管,比前一天价格高就卖出。
public int maxProfit(int[] prices) {
if (prices == null || prices.length == 0) {
return 0;
}
int min_pos = 0;
int max_pro = 0;
for (int i = 1; i < prices.length; i++) {
if (prices[i] > prices[i - 1]) {
max_pro += prices[i] - prices[min_pos];
}
min_pos = i;
}
return max_pro;
}
Best Time to Buy and Sell Stock II ——LeetCode的更多相关文章
- Best Time to Buy and Sell Stock II leetcode java
题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...
- 122. Best Time to Buy and Sell Stock II ——LeetCode
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- Best Time to Buy and Sell Stock II [LeetCode]
Problem Description: http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ Basic idea: ...
- [LintCode] 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 —— Best Time to Buy and Sell Stock II [贪心算法]
Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a ...
- 27. Best Time to Buy and Sell Stock && Best Time to Buy and Sell Stock II && Best Time to Buy and Sell Stock III
Best Time to Buy and Sell Stock (onlineJudge: https://oj.leetcode.com/problems/best-time-to-buy-and- ...
- 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 pric ...
- 【leetcode】Best Time to Buy and Sell Stock II
Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a ...
- 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 ...
随机推荐
- STL——静态常量整数成员在class内部直接初始化
如果class内含const static integral data member,那么根据C++标志规格,我们可以在class之内直接给予初值.所谓integral泛指所有的整数型别(包括浮点数) ...
- 用 Java 技术创建 RESTful Web 服务--转载
简介 JAX-RS (JSR-311) 是为 Java EE 环境下的 RESTful 服务能力提供的一种规范.它能提供对传统的基于 SOAP 的 Web 服务的一种可行替代. 在本文中,了解 JAX ...
- Linux磁盘管理:lvcreate 常用命令
查看当前LV及PV信息: [root@rusky ~]# hostnamectl Static hostname: localhost.localdomain Transient hostname: ...
- My.Ioc 代码示例——使用观察者机制捕获注册项状态的变化
在 My.Ioc 中,要想在服务注销/注册时获得通知,可以通过订阅 ObjectBuilderRegistered 和 ObjectBuilderUnregistering 这两个事件来实现.但是,使 ...
- ecshop首页调用指定商品分类下的商品品牌列表
转之--http://www.16css.com/ecshop/735.html 通过二次开发可以实现ECSHOP首页调用指定分类下的品牌列表. 第一步: 打开根目录下的index.php 在最后面 ...
- hibernate_validator_07
一.校验组序列 默认情况下,约束的验证是没有一定的顺序的,不管他们是属于哪个认证组的.但是在有些环境中,我们控制这些约束验证的顺序还是很有用的. 就拿我们上一个例子来说,我们可以这样:首先在我们检查车 ...
- Hibernate 主键生成策略
表示符生成器 描述 Increment 由hibernate自动以递增的方式生成表识符,每次增量为1 Identity 由底层数据库生成表识符.条件是数据库支持自动增长数据类型. Sequence H ...
- IDEA中Maven管理下添加mysql依赖
在做Java Web项目的时候,不可避免的就要使用到数据库,下面就是在IDEA中添加mysql依赖的方法. 如果你看到这里,就表示你弄懂了IDEA,maven和Tomcat等,所以... 只需要在po ...
- IOS 在Xcode 4.x以上添加静态库
参考网站:http://my.oschina.net/edwardlau/blog/95924 常用的代码可以通过静态库进行抽出来作为公共类方法,方便在其他地方调用,一般来说我们要准备2套静态库,一套 ...
- Android放大镜的实现
package chroya.demo.magnifier; import android.content.Context; import android.graphics.Bitmap; impor ...