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的更多相关文章

  1. 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 ...

  2. [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 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. [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 ...

  8. 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 ...

  9. LeetCode 122 Best Time to Buy and Sell Stock II(股票买入卖出的最佳时间 II)

    翻译 话说你有一个数组,当中第i个元素表示第i天的股票价格. 设计一个算法以找到最大利润. 你能够尽可能多的进行交易(比如.多次买入卖出股票). 然而,你不能在同一时间来多次交易. (比如.你必须在下 ...

随机推荐

  1. 入门-Arcmap网络分析示例

    1.打开arcmap并加载网络数据西安市地图(city.mdb): 它包含的图层有: 2.显示网络中的流向: 3.在设施网络分析工具条上,点选旗标和障碍工具板下拉箭头,将旗标放在city_net_ju ...

  2. CSS 3D旋转 hover 后设置transform 是相对于正常位置

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. 理解Mac和iOS中的 Event 处理

    根据现在的理解,我把event处理分为5部分,第一是,Event处理的Architecture:第二是,Event的Dispatch到first responder之前: 第三是,Event从firs ...

  4. Oracle之ROW_NUMBER() OVER函数

    语法:ROW_NUMBER() OVER(ORDER BY COLUMN) 简单的说row_number()从1开始,为每一条分组记录返回一个数字,这里的select ACD_ID,ROW_NUMBE ...

  5. hdu 2080

    ps:水题...求夹角...先求出COS,然后用acos 代码: #include "stdio.h" #include "math.h" int main() ...

  6. python中的namespace

    python中的名称空间是名称(标识符)到对象的映射. 具体来说,python为模块.函数.类.对象保存一个字典(__dict__),里面就是重名称到对象的映射. 可以参看下面python程序的输出: ...

  7. Ubuntu安装samba的问题

    问题: root@ubuntu:~# apt-get install samba 正在读取软件包列表... 完成 正在分析软件包的依赖关系树 正在读取状态信息... 完成 有一些软件包无法被安装.如果 ...

  8. BZOJ 2083 Intelligence test

    用vector,二分. #include<iostream> #include<cstdio> #include<cstring> #include<algo ...

  9. C++面向对象编程解决三阶矩阵相加减

    /*此处用面向对象编程*/ #include<iostream> #include<string.h> using namespace std; class Matrices ...

  10. Python OpenCV —— Border

    关于border的部分,边缘处理. # -*- coding: utf-8 -*- """ Created on Wed Sep 28 00:58:51 2016 @au ...