【LeetCode】剑指 Offer 63. 股票的最大利润
package offer;
public class Solution63 {
public static void main(String[] args) {
int[] prices = {7,1,5,3,6,4};
Solution63 solution = new Solution63();
System.out.println(solution.method(prices));
}
private int method(int[] prices){
int profit = 0;
int cost = Integer.MAX_VALUE;
for(int price : prices){
cost = Math.min(cost,price);
profit = Math.max(profit, price - cost);
}
return profit;
}
}
//时间复杂度为 O(n)
//空间复杂度为 O(1)