LeetCode Best Time to Buy and Sell Stock

原创 2015年07月07日 14:36:47

Description:

Say you have an array for which the ith element is the price of a given stock on day i.

If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.

Solution:

题目的意思理解了我半天。

就是给定一个股票的数组,要求如何使得赚取的股票利润最大。也就是最大值减去最小值。

但是注意一点,股票是有时间的,从正过来的顺序,复杂度是O(n^2),但是按照时间反过来的顺序,可以降一维的复杂度,变成O(n)。

import java.util.*;

public class Solution {
	public int maxProfit(int[] prices) {
		if (prices.length == 0)
			return 0;
		int max = 0, maxPrice = prices[prices.length - 1];
		for (int i = prices.length - 2; i >= 0; i--) {
			maxPrice = Math.max(maxPrice, prices[i]);
			max = Math.max(max, maxPrice - prices[i]);
		}

		return max;
	}
}



[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
  • 1756

[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
  • 8765

【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
  • 3438

关于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
  • 832

【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
  • 21746

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

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

[leetcode] 309. Best Time to Buy and Sell Stock with Cooldown

题目: Say you have an array for which the ith element is the price of a given stock on day i.Design a...
  • u014673347
  • u014673347
  • 2016年02月22日 11:40
  • 1173

Best Time to Buy and Sell Stock III -- LeetCode

原题链接: http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/  这道题是Best Time to Buy an...
  • linhuanmars
  • linhuanmars
  • 2014年04月09日 05:34
  • 27260

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
  • 1340

121. Best Time to Buy and Sell Stock [easy] (Python)

题目链接https://leetcode.com/problems/best-time-to-buy-and-sell-stock/题目原文 Say you have an array for w...
  • coder_orz
  • coder_orz
  • 2016年05月28日 01:02
  • 984
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode Best Time to Buy and Sell Stock
举报原因:
原因补充:

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