/*
 * 演算法 / Algorithm:
 * 貪心法：把所有相鄰兩天的「正向價差」加總。
 * Greedy: sum up every positive day-to-day price difference.
 * 等價於在每一段上漲區間都執行一次低買高賣。
 * Equivalent to buying at each local low and selling at each local high.
 */

int maxProfit(int* prices, int pricesSize) {
    // profit 累計總利潤，初始化為 0 / running total of profit, starts at 0
    int profit = 0;

    // 從第 1 天開始（索引從 1），每次和前一天比較
    // Loop from day 1 onwards, comparing each day with the previous one
    for (int i = 1; i < pricesSize; i++) {
        // 計算今天比昨天貴多少 / compute how much today is higher than yesterday
        int diff = prices[i] - prices[i - 1];

        // 只有上漲 (diff > 0) 才把這段差價計入利潤；下跌就略過
        // Only count the diff when it is positive; ignore drops
        if (diff > 0) {
            profit += diff;  // 累加上漲幅度 / add the gain to total profit
        }
    }

    // 回傳累計的最大利潤 / return the accumulated maximum profit
    return profit;
}
