Code/Solution#
- Initialize a variable
minPricewithInteger.MAX_VALUE. - Initialize a variable
answerwith 0. - Iterate through the array
prices:- If the current price is lower than minPrice, update minPrice to the current price.
- Otherwise, calculate the profit we would get if we sold at the current price, and update
answerwith the maximum of the current profit and existinganswer.
class Solution {
public int maxProfit(int[] prices) {
int minPrice = Integer.MAX_VALUE;
int answer = 0;
for (var price:prices){
if (minPrice > price){
minPrice = price;
}else{
answer = Math.max(answer,price-minPrice);
}
}
return answer;
}
}class Solution {
fun maxProfit(prices: IntArray): Int {
var answer = 0;
var minPrice = Int.MAX_VALUE;
for (price in prices){
if (price< minPrice) minPrice = price
else answer = max(answer,price-minPrice);
}
return answer
}
}