LeetCode Best Time to Buy and Sell Stock II

原创 2015年07月07日 16:25:15

Description:

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

Solution:

这道题目和上一道的不同之处在于可以买入卖出多次。

这个时候可以进行这样的题目转换:就是找所有递增的子序列的递增之和。

import java.util.*;

public class Solution {
	public int maxProfit(int[] prices) {
		int sum = 0;
		for (int i = 1; i < prices.length; i++)
			if (prices[i] > prices[i - 1])
				sum += prices[i] - prices[i - 1];
		return sum;
	}
}


【LeetCode】【Python题解】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...
  • u011613729
  • u011613729
  • 2014年07月24日 10:11
  • 3434

【LeetCode】 Best Time to Buy and Sell Stock I II III IV 解题报告

Best Time to Buy and Sell Stock I题意:用一个数组表示股票每天的价格,数组的第i个数表示股票在第i天的价格。 如果只允许进行一次交易,也就是说只允许买一支股票并卖掉,求...
  • ljiabin
  • ljiabin
  • 2015年04月06日 11:10
  • 21735

leetcode【121+122+123 best time to buy and sell stock】【python】

我们先拿出来前三道题,因为他们都是array中的题目。这是leetcode种经典的一系列题,涉及到动态规划和贪心算法。按照我的理解,贪心是满足当前条件的最优值我们就将它最为最优解,也就是大家说的局部最...
  • u014251967
  • u014251967
  • 2016年09月12日 21:04
  • 790

Best Time to Buy and Sell Stock II -- LeetCode

原题链接: http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/  这道题跟Best Time to Buy and...
  • linhuanmars
  • linhuanmars
  • 2014年04月09日 05:33
  • 9682

leetcode_c++:Best Time to Buy and Sell Stock II(122)

题目Say you have an array for which the ith element is the price of a given stock on day i.Design an a...
  • mijian1207mijian
  • mijian1207mijian
  • 2016年06月10日 23:19
  • 293

[LeetCode] Best Time to Buy and Sell Stock IV

Say you have an array for which the ithi^{th} element is the price of a given stock on day ii.Design...
  • u011331383
  • u011331383
  • 2015年02月23日 00:08
  • 8761

关于LeetCode中Best Time to Buy and Sell Stock一题的理解

题目如下: Say you have an array for which the ith element is the price of a given stock on day i. If yo...
  • zsy112371
  • zsy112371
  • 2016年09月09日 13:55
  • 831

[leetcode 309]Best Time to Buy and Sell Stock with Cooldown--买卖股票with冷却期(DP法)

Question: Say you have an array for which the ith element is the price of a given stock on day ...
  • Xiaohei00000
  • Xiaohei00000
  • 2016年03月10日 23:02
  • 1751

Leetcode_122_Best Time to Buy and Sell Stock II

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/43155725 Say you have an a...
  • pistolove
  • pistolove
  • 2015年01月26日 18:28
  • 1453

Leetcode算法学习日志-714 Best Time to Buy and Sell Stock with Transaction Fee

Leetcode 714 Best Time to Buy and Sell Stock with Transaction Fee 题目原文 Your are given an array o...
  • Zarlove
  • Zarlove
  • 2017年10月23日 21:16
  • 1317
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode Best Time to Buy and Sell Stock II
举报原因:
原因补充:

(最多只允许输入30个字)