题目描述:

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 res = 0;
for(int i = 0; i < prices.length - 1; i++){
if(prices[i] <= prices[i + 1])
res += (prices[i + 1] - prices[i]);
}
return res;
}
}

  

Java [Leetcode 122]Best Time to Buy and Sell Stock II的更多相关文章

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

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

  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 an al ...

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

  8. [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. thinpad E43系列WIN8装WIN7系统

    昨晚WIN8系统下装WIN7 折腾了好久,故此总结一下写一篇U盘装WIN7 : 先简述一下思路:BOSS设置启动项------->U盘启动--------> 进入PE删除所有分区----- ...

  2. java 几种常见的定时器

    今天闲着没事就总结了一下在java中常用的几种定时器. 主要有3种java.util.Timer, ScheduledExecutorService和quartz 一.Timer 举个简单例子.每隔5 ...

  3. POJ 1724 Roads

    题意:有R条路,每条路都有一定的路长和花费,问在总的花费小于一定的值的情况下,从1到N的最短路程         注意:这里两点之间单向边,且可能存在很多条路,所以只能用邻接表存储.思路:用dijks ...

  4. 【leetcode】Combination Sum (middle)

    Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...

  5. SDUT1586 计算组合数(组合数)

    这个题数据量小,不容易超时. #include<stdio.h> long long fac(int n) { ; ; i <= n ; i++) { m = i*m; } retu ...

  6. POJ 2993Emag eht htiw Em Pleh

    http://poj.org/problem?id=2993 这个题与POJ2996正好反着,但个人认为,比2996好做多了,那些边边框框都挺容易输出的,剩下的注意p别忘了给输进去就行,还有白色的是大 ...

  7. happens-before通俗理解

    原文地址:http://ifeve.com/easy-happens-before/ 学习Java并发,到后面总会接触到happens-before偏序关系.初接触玩意儿简直就是不知所云,下面是经过一 ...

  8. P1011 传纸条//dp优化改进状态表示

    P1011 传纸条 时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 NOIP2008复赛提高组第三题 描述 小渊和小轩是好朋友也是同班同学,他们在一起总有谈不 ...

  9. lintcode:合并区间

    题目: 合并区间 给出若干闭合区间,合并所有重叠的部分. 样例 给出的区间列表 => 合并后的区间列表: [ [ [1, 3], [1, 6], [2, 6], => [8, 10], [ ...

  10. Simple Factory 简单工厂模式(静态工厂)

    基本概念: 1) Simple Factory模式属于创建型模式, 2) 简单工厂模式是由一个工厂(注意是一个!)对象决定创建出哪一种产品类的实例(例如你到肯德基说你要鸡腿,要薯条,要饮料还是,,,这 ...