// 演算法與 C 版本相同：貪心單次掃描，維護 maxReach。
// Same algorithm as the C version: one greedy pass tracking maxReach.
// 使用 std::vector 與 std::max 讓程式碼更地道 / uses std::vector and std::max for idiomatic C++.

#include <vector>     // std::vector 動態陣列 / dynamic array container
#include <algorithm>  // std::max 取最大值 / for std::max

class Solution {
public:
    // vector<int>& 是「對 vector 的參考」，避免複製整個陣列（高效）
    // vector<int>& is a reference to the vector — avoids copying the whole array
    bool canJump(std::vector<int>& nums) {
        int n = static_cast<int>(nums.size());  // 取得長度並轉成 int / get length as int
        int maxReach = 0;                       // 目前最遠可達 index / farthest reachable index

        for (int i = 0; i < n; ++i) {  // range-for 也可以，但用 index 更貼合演算法 / classic indexed loop
            if (i > maxReach) {        // i 超出可達範圍 → 終點也到不了 / i unreachable ⇒ end unreachable
                return false;          // 直接回傳失敗 / bail out
            }

            // std::max 取兩數較大者；i + nums[i] 是從 i 跳出去能踏到的最遠處
            // std::max returns the larger of two values; i + nums[i] is the farthest landing from i
            maxReach = std::max(maxReach, i + nums[i]);

            if (maxReach >= n - 1) {  // 已涵蓋最後一格 / last index is now reachable
                return true;          // 提早回傳成功 / early exit
            }
        }

        return true;  // 迴圈正常走完代表全部可達 / fallthrough: every cell was reachable
    }
};
