问题描述:
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.
Example 1:
Input: [7, 1, 5, 3, 6, 4] Output: 5 max. difference = 6-1 = 5 (not 7-1 = 6, as selling price needs to be larger than buying price)
Example 2:
Input: [7, 6, 4, 3, 1] Output: 0 In this case, no transaction is done, i.e. max profit = 0.
原问题链接:https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
问题分析
这个问题看起来很简单,不过还是有一些细节需要注意的。首先,这里给出的数组值是一组股票价格的值,对于用户来说只能有一次交易的机会,这意味着用户只能在里面选择某个点买,而在某个点卖。而这里买的点肯定在数组的前面,而卖的点肯定在这个买点的后面。
相对这个问题来说,我们需要找到一个对于数组后面的元素和前面元素的差值,并且这个差值最大的值就是我们想要的结果。当然,如果后面的差值也可能小于等于零,在这种情况下,我们返回结果0。
那么,该怎么来找这个元素呢?我们首先取数组里的第一个元素假定为最小的值,然后从开始向后遍历数组。每次碰到一个元素的时候就和当前假定最小的元素比较,设定最小值min为比较小的那个。然后我们再计算当前值和我们当前得到的最小值min的差,取这些差值中最大的那个。这样我们将得到最终的结果。这里还有一些特殊情况要考虑,比如说数组的长度小于2的时候,没法实现买入和卖出,所以应该返回结果0。
详细的代码实现如下:
public class Solution { public int maxProfit(int[] prices) { if(prices.length < 2) return 0; int low = prices[0], result = 0; for(int i = 0; i < prices.length; i++) { low = Math.min(low, prices[i]); result = Math.max(result, prices[i] - low); } return result; } }
相关推荐
《最佳买卖股票时机II》是LeetCode中的一道经典编程题目,主要涉及动态规划和数组操作的知识点。在这个问题中,我们被赋予一个整数数组`prices`,表示某支股票每天的价格。任务是找到在允许进行多次交易的情况下,...
java java_leetcode题解之Best Time to Buy and Sell Stock with Cooldown
java java_leetcode题解之Best Time to Buy and Sell Stock IV.java
java java_leetcode题解之Best Time to Buy and Sell Stock III.java
java java_leetcode题解之Best Time to Buy and Sell Stock II.java
java java_leetcode题解之Best Time to Buy and Sell Stock I.java
leetcode LeetCode 解决题目的总数: 136/1753 微信公众号: 工程师Ruojhen 算法-动态规划 题号 名称 English Name 题解 53 最大子序和 Maximum Subarray 70 爬楼梯 Climbing Stairs 121 买卖股票的最佳时机 Best Time...
股票收益leetcode LeetCode 股票问题 Best Time to Buy and Sell Stock (Easy) 一次交易,找最大收益 for i in prices: low = min(low, i) profit = max(profit, i-low) Best Time to Buy and Sell Stock II (Easy) ...
python python_leetcode题解121_Best_Time_to_Buy_and_Sell_Stock
leetcode leetcode-java leetcode刷题笔记 已做题目列表 1.Two Sum 3.Longest Substring Without Repeating Characters 5.Longest Palindromic Substring 20.Valid Parentheses 26.Remove Duplicates from Sorted ...
python python_leetcode题解之123_Best_Time_to_Buy_and_Sell_Stock_III
python python_leetcode题解之122_Best_Time_to_Buy_and_Sell_Stock_II
javascript js_leetcode题解之121-best-time-to-buy-and-sell-stock.js
javascript js_leetcode题解之123-best-time-to-buy-and-sell-stock-iii.js
javascript js_leetcode题解之122-best-time-to-buy-and-sell-stock-ii.js
leetcode 分类 LeetCode LeetCode Java Solution 2018年5月31日 更新 最近刷了一遍 LeetCode,发现第一次的代码确实有些 low 很多代码并不简洁,本来1行的代码可以写成3行 思维混乱 没有按照题目类型分类 没有写结题...
公众号 coding 笔记、点滴记录,以后的文章也会...最近花时间分门别类整理了 LeetCode 的前 500 题,把相似的题目都放在了一起,比如Best Time to Buy and Sell Stock的6道题,这些题目放在一起刷效果更好。 简书博客:
63. 股票的最大利润题目链接Leetcode:121. Best Time to Buy and Sell Stock题目描述可以有一次买入和一次卖出,买入必
leetcode卡 leetcode exercises 3-5 solutions everyday. fighting~ TODO array Best Time to Buy and Sell Stock II Valid Sudoku linked list Palindrome linked list Linked List Cycle trees Convert Sorted ...
Best Time to Buy and Sell Stock [121]3. Climbing Stairs [70]4. Maximum Subarray [53]5. House Robber [198]6. Range Sum Query - Immutable [303]7. Counting Bits [338]8. Palindromic Substrings [647]9. ...