very straightword q but dont get the min() value cuz if we have 5,699,1,3 then we are gonna skip the 5 699 profit. Instead, once we see a smaller value that we can buy our stock at, we update count. Else everytime we see a bigger value than this count, we update answer with the profit
class Solution {
public int maxProfit(int[] prices) {
// 5,699,1,3
int check=prices[0];
int ans=0;
for(int i =1;i<prices.length;i++){
if(prices[i]<check){
check=prices[i];
} else{
ans=Math.max(ans,prices[i]-check);
}
}
return ans;
}
}
n time
1 space