昨天开始就肚子疼 太疼了 直接把我送走
今天写个水题就跑路吧
题目
https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/
代码
class Solution {
public int maxProfit(int[] prices) {
if(prices==null) return 0;
int len=prices.length;
if(len==0) return 0;
int min=0;
int res=0;
int a=0;
for(int i=1;i<len;i++){
a=0;
if(prices[i]>=prices[min]){
a=prices[i]-prices[min];
res=Math.max(res,a);
}
else{
min=i;
}
}
return res;
}
}
复杂度
时间复杂度O(N):一次遍历
空间复杂度O(1):仅用常数个变量