题解 | #买卖股票的最好时机(一)#
买卖股票的最好时机(一)
http://www.nowcoder.com/practice/64b4262d4e6d4f6181cd45446a5821ec
import java.util.*;
class Solution {
public int maxProfit(int[] prices) {
// write code here
int buy = prices[0];
int money = 0;
for (int i = 1; i < prices.length; i++) {
if ( buy < prices[i]){
money = Math.max(money,prices[i] - buy);
}else {
buy = prices[i];
}
}
return money;
}
}
//假设只要能赚钱,我们就卖出去得到利润,循环一遍,记录下最大的的利润即为答案
//假设中途遇到比我们更小的成本,我们就更换成本