image.png
    解法1 : 记录当天左边最小值,右边最大值
    解法2:
    最大利润只可能在上升阶段
    image.png

    1. public class Solution {
    2. public int maxProfit(int prices[]) {
    3. int minprice = Integer.MAX_VALUE;
    4. int maxprofit = 0;
    5. for (int i = 0; i < prices.length; i++) {
    6. if (prices[i] < minprice) {
    7. minprice = prices[i];
    8. } else if (prices[i] - minprice > maxprofit) {
    9. maxprofit = prices[i] - minprice;
    10. }
    11. }
    12. return maxprofit;
    13. }
    14. }
    15. 作者:LeetCode-Solution
    16. 链接:https://leetcode-cn.com/problems/gu-piao-de-zui-da-li-run-lcof/solution/gu-piao-de-zui-da-li-run-by-leetcode-sol-0l1g/
    17. 来源:力扣(LeetCode
    18. 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。