larscheng / algo

0 stars 0 forks source link

【Check 76】2024-05-17 - 121. 买卖股票的最佳时机 #182

Open larscheng opened 1 month ago

larscheng commented 1 month ago

121. 买卖股票的最佳时机

larscheng commented 1 month ago

思路

class Solution {

public int maxProfit(int[] prices) {
    int min = Integer.MAX_VALUE;
    int res = 0;
    for (int price : prices) {
        min = Math.min(min, price);
        res = Math.max(res, price - min);
    }
    return res;
}

}



### 复杂度
- 时间复杂度:O(n)
- 空间复杂度:O(1)