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;
    }
}