/*
 * 演算法 / Algorithm:
 *   與 C 版本相同：雙指針從兩端往中間，每步算面積、移動較矮的一邊。
 *   Same as the C version: two pointers shrinking inward, always
 *   moving the shorter side. Time O(n), Space O(1).
 */

#include <vector>      // std::vector — 動態陣列容器 / dynamic array container
#include <algorithm>   // std::max, std::min — 比較工具 / comparison helpers

class Solution {
public:
    int maxArea(std::vector<int>& height) {        // LeetCode 給的是 vector<int>& / signature uses vector reference
        int left = 0;                              // 左指針 / left pointer
        int right = static_cast<int>(height.size()) - 1;  // 右指針；size() 回傳 size_t (unsigned)，先轉 int 避免比較警告 / cast to int to avoid signed/unsigned warning
        int best = 0;                              // 目前最大面積 / current best area

        while (left < right) {                     // 指針未相遇就繼續 / loop while pointers haven't met
            int h = std::min(height[left], height[right]);  // 較矮的牆高 / shorter wall height
            int w = right - left;                  // 寬度 / width
            best = std::max(best, h * w);          // 若更大就更新 / take the max with current

            if (height[left] < height[right]) {    // 左邊較矮 / left is the bottleneck
                ++left;                            // 前置遞增更習慣 / prefix increment is idiomatic in C++
            } else {                               // 右邊較矮或相等 / right is shorter or equal
                --right;                           // 右指針往左移 / move right inward
            }
        }

        return best;                               // 回傳結果 / return the answer
    }
};
