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

// Source : https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock/
// Author : Chao Zeng
// Date   : 2014-12-26
class Solution {
public:
    int maxProfit(vector<int> &prices) {
		int n = prices.size();
		if (n == 0)
			return 0;
		int mp[n];//表示以prices[i]结尾的max profit
		int minnumber = prices[0];
		memset(mp,0,sizeof(mp));
		for (int i = 1; i < n; i++){
			if (minnumber > prices[i])
				minnumber = prices[i];
			int dis = prices[i] - minnumber;
			if (mp[i-1] < dis)
				mp[i] = dis;
			else
				mp[i] =mp[i-1];
		}
		return mp[n-1];
    }
};
时间: 2024-10-19 06:04:05

LeetCode31:Best Time to Buy and Sell Stock的相关文章

LeetCode之“动态规划”:Best Time to Buy and Sell Stock I &amp;&amp; II &amp;&amp; III &amp;&amp; IV

1. Best Time to Buy and Sell Stock I 题目链接 题目要求: 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), desi

LeetCode OJ: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 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. 简单的动

leetcode笔记:Best Time to Buy and Sell Stock III

一. 题目描述 Say you have an array for which the i-th element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete at most two transactions. Note: You may not engage in multiple transactions at the same

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

leetcode笔记:Best Time to Buy and Sell Stock

一. 题目描述 Say you have an array for which the i-th 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 pro

天题系列: Best Time to Buy and Sell Stock IV

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 at most k transactions. Note:You may not engage in multiple transactions at the same time (ie, yo

[LeetCode]题解(python):123-Best Time to Buy and Sell Stock III

题目来源: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ 题意分析: 和上题类似,array[i]代表第i天物品价格,如果只能交易2次.问最大利润. 题目思路: 这是一个动态规划问题.不难想到把整个数组拆成两部分.那么用两个数组First,second分别array[:i]和array[i:]的最大利润.那么答案就等于max(First[i] + second[i + 1]). 代码(python): cla

[LeetCode]题解(python):122-Best Time to Buy and Sell Stock II

题目来源: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ 题意分析: 和上题类似,给定array,代表第i天物品i的价格.如果可以交易无数次(手上有物品不能买),问最高利润. 题目思路: 记录当前最小值,如果遇到array[i]  < min,那么加上当前的最大值:更新min. 代码(python): class Solution(object): def maxProfit(self, prices): &quo

天题系列:Best Time to Buy and Sell Stock III

public class Solution { public int maxProfit(List<Integer> prices) { // http://blog.csdn.net/linhuanmars/article/details/23236995 if(prices==null||prices.size()<1) return 0; int len = prices.size(); int[] global = new int[3] ; int[] loc = new int