题解 | #买卖股票的最好时机(一)# [P3]

买卖股票的最好时机(一)

http://www.nowcoder.com/practice/64b4262d4e6d4f6181cd45446a5821ec

JAVA - iterative

import java.util.*;

public class Solution {
    /**
     * 
     * @param prices int整型一维数组 
     * @return int整型
     */
    public int maxProfit (int[] prices) {
      int min = Integer.MAX_VALUE;
      int maxProfit = 0;
      
      for (int p : prices) {
        maxProfit = Math.max(maxProfit, p - min);
        min = Math.min(p, min);
      }
      
      return maxProfit;
    }
}
全部评论

相关推荐

King987:这不就是力扣的算法题吗?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务