题解 | #买卖股票的最好时机(一)# [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;
}
}