#include <limits.h>
int maxProfit(int *prices, int pricesSize) {
int mn = INT_MAX;
int profit = 0;
for (int i = 0; i < pricesSize; i++) {
if (prices[i] < mn) {
mn = prices[i];
}
if (prices[i] - mn > profit) {
profit = prices[i] - mn;
}
}
return profit;
}
#include <climits>
#include <vector>
using namespace std;
class Solution {
public:
int maxProfit(vector<int> &prices) {
int mn = INT_MAX;
int profit = 0;
for (int p : prices) {
mn = min(mn, p);
profit = max(profit, p - mn);
}
return profit;
}
};
package main
import "math"
func maxProfit(prices []int) int {
mn := math.MaxInt
profit := 0
for _, p := range prices {
if p < mn {
mn = p
}
if p-mn > profit {
profit = p - mn
}
}
return profit
}
class Solution {
public int maxProfit(int[] prices) {
int mn = Integer.MAX_VALUE;
int profit = 0;
for (int p : prices) {
mn = Math.min(mn, p);
profit = Math.max(profit, p - mn);
}
return profit;
}
}
class Solution {
maxProfit(prices) {
let mn = Infinity;
let profit = 0;
for (const p of prices) {
mn = Math.min(mn, p);
profit = Math.max(profit, p - mn);
}
return profit;
}
}
class Solution:
def maxProfit(self, prices: list[int]) -> int:
mn = float("inf")
profit = 0
for p in prices:
mn = min(mn, p)
profit = max(profit, p - mn)
return profit
struct Solution;
impl Solution {
fn max_profit(prices: Vec<i32>) -> i32 {
let mut mn = i32::MAX;
let mut profit = 0;
for p in prices {
mn = mn.min(p);
profit = profit.max(p - mn);
}
profit
}
}