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

Have you met this question in a real interview?

Yes

Example

Given array [3,2,3,1,2], return 1.

时间: 2024-10-27 08:37:52

[LintCode] Best Time to Buy and Sell Stock 买卖股票的最佳时间的相关文章

[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. 这道题相

[LintCode] 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 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). Ho

[LeetCode] 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 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). Ho

[LeetCode] Best Time to Buy and Sell Stock III 买股票的最佳时间之三

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

LeetCode | 0121. Best Time to Buy and Sell Stock买卖股票的最佳时机【Python】

LeetCode 0121. Best Time to Buy and Sell Stock买卖股票的最佳时机[Easy][Python][贪心] Problem LeetCode 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 (i.e., b

121. Best Time to Buy and Sell Stock买卖股票12

[抄题]: 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. Input: [7, 1, 5, 3, 6, 4] Output: 5 max. difference = 6-1 = 5 (not 7-1 = 6, as selling

LeetCode Best Time to Buy and Sell Stock 买卖股票的最佳时机 (DP)

题意:给定一个序列,第i个元素代表第i天这支股票的价格,问在最佳时机买入和卖出能赚多少钱?只买一次,且仅1股,假设本钱无限. 思路:要找一个最低价的时候买入,在最高价的时候卖出利润会最大.但是时间是不能冲突的,比如说在明天买入,今天卖出.因此,对于今天的价格,应该要找到今天之前的该股的最低价,买入,今天卖出. 其实就是要为序列中的一个元素A[k],找到另一个元素A[e],位置满足e<k,结果使得A[k]-A[e]最大. 用动态规划解决,从左扫起,遇到一个元素就更新当前最小值,再用当前元素去减这个

[LeetCode] 123. Best Time to Buy and Sell Stock III 买卖股票的最佳时间 III

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

[LeetCode] 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