题解 | #买卖股票的最好时机(一)#
买卖股票的最好时机(一)
http://www.nowcoder.com/practice/64b4262d4e6d4f6181cd45446a5821ec
public class Solution {
/**
*
* @param prices int整型一维数组
* @return int整型
*/
public int maxProfit (int[] prices) {
// write code here
PriorityQueue<Integer> p = new PriorityQueue<>();
int max = 0;
for(int i = 0;i < prices.length;i ++){
if(p.isEmpty()){
p.offer(prices[i]);
continue;
}
p.offer(prices[i]);
max = Math.max(max,prices[i]-p.peek());
}
return max;
}
}