zoukankan      html  css  js  c++  java
  • 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.

     1 public class Solution {
     2     public int maxProfit(int[] prices) 
     3     {
     4         int MaxProfit=0;
     5         int MinElement=Integer.MAX_VALUE;
     6         for(int i=0;i<prices.length;i++)
     7         {
     8                 MinElement=Math.min(MinElement,prices[i]);
     9                 MaxProfit=Math.max(MaxProfit,prices[i]-MinElement);
    10             
    11         }
    12         return MaxProfit;
    13     }
    14 }

    reference: http://lifexplorer.me/leetcode-best-time-to-buy-and-sell-stock-i-ii-iii/

  • 相关阅读:
    19-10-31-B
    19-10-30-Night-V
    19-10-30-C
    19-10-29-Night-X
    19-10-29-Z
    19-10-28-A
    19-10-27-S
    19-10-26-Night-D
    留言板
    优秀博客存档
  • 原文地址:https://www.cnblogs.com/hygeia/p/4636342.html
Copyright © 2011-2022 走看看