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 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).
可以多次买入卖出,求出最大利润。
由于可以多次买入卖出,所以每次遇到有利润都可以卖出,所以一直循环然后能交易就交易就可以了。
public class Solution {
public int maxProfit(int[] prices) {
int len = prices.length;
if( len < 2)
return 0;
int result = 0;
int start = 0;
while( start < len ){
start = getStart(prices,start);
if( start == len-1)
break;
result+=(prices[start+1]-prices[start]);
start++;
}
return result;
} public int getStart(int[] prices,int start){
int buy = prices[start];
while( start < prices.length ){
if( buy >= prices[start]){
buy = prices[start];
start++;
}else
break;
}
return start-1; }
}
leetcode 122. Best Time to Buy and Sell Stock II ----- java的更多相关文章
- 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 ...
- 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 ...
- [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 ...
- 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 ...
- LeetCode 122 Best Time to Buy and Sell Stock II(股票买入卖出的最佳时间 II)
翻译 话说你有一个数组,当中第i个元素表示第i天的股票价格. 设计一个算法以找到最大利润. 你能够尽可能多的进行交易(比如.多次买入卖出股票). 然而,你不能在同一时间来多次交易. (比如.你必须在下 ...
随机推荐
- STL-算法
#include <algorithm> 1. max_element(v.begin(), v.end()); 注意,所有的区间全部是半开区间,如果数组包含20-40,通过find找出2 ...
- JSTL标准标签库
有时使用EL和标准动作达不到目的,于是就引入定制标记. 对于JSP页面创作人员来说,定制标记使用起来比脚本要容易一些.不过对于JAVA程序员来说,简历定制标记处理器反而更困难.幸运的是,已经有了一个标 ...
- PowerShell并发控制-命令行参数之四问
传教士问: win下如何 获取进程命令行,及命令行参数? 传教士答: 可以用这个powershell命令(实际上是wmi查询): (get-wmiobject -query "select ...
- iOS 下如果存在UIScrollerView 使用UIScreenEdgePanGestureRecognizer实现侧滑效果失效的问题
当你在使用UIScreenEdgePanGestureRecognizer手势实现侧滑的时候,如果后期你导航控制器push出的界面中包含UIScrollerView,这个时候你会发现,侧滑效果无法实现 ...
- (转)tomcat与地址栏图标之研究(多浏览器)
原文:http://hi.baidu.com/hebo_thu/item/fc8c81bb164f5cee4fc7fd90 tomcat与地址栏图标之研究(多浏览器) 最近在做一个java网络应用程序 ...
- hdu 2047
PS:又是上课偷懒..去递推.. 代码: #include "stdio.h"#include "math.h"long long dp[55];long lo ...
- C++11 不抛异常的new operator
在google cpp style guide里面明确指出:we don't use exceptions C++11的noexcept关键字为这种选择提供了便利. C++11以前,提及malloc和 ...
- 关于binary search的一点解惑
在写binary search时对于mid的计算我最开始使用的是 mid = (low + high)/2; 后来看到在很多的实现为 mid = low + (high - low)/2; 想了一下两 ...
- UNIX,基础知识,文件IO,文件和目录
2015.1.27星期二,早晨阴天,中午下雪了今天上午老师不上课,程序语句,记一下:main(void){ int c; while((c = getc(stdin)) != EOF) if(putc ...
- 【转】C 宏
http://www.cs.yale.edu/homes/aspnes/pinewiki/C%282f%29Macros.html See KernighanRitchie Appendix A12. ...