// 演算法：與 C 版本相同 — 把陣列視為環狀，數有幾個 nums[i] > nums[(i+1) % n] 的位置。
// 若 <= 1 個下降，就是「排序後旋轉」而成；否則不是。
// Algorithm: same as the C version — treat the array as circular, count positions
// where nums[i] > nums[(i+1) % n]; <= 1 such drop means sorted-then-rotated.

#include <vector>      // 引入 std::vector / pull in std::vector

class Solution {
public:
    // LeetCode C++ 介面：傳入一個 vector<int>，回傳 bool。
    // 用 const 參考避免複製整個 vector / use const-reference to avoid copying the vector.
    bool check(const std::vector<int>& nums) {
        int n = static_cast<int>(nums.size()); // 取得長度，轉成 int 避免 size_t 比較警告
                                               // get length, cast to int to avoid signed/unsigned warnings
        int drops = 0;                         // 下降次數計數器 / counter for descents
        for (int i = 0; i < n; ++i) {          // 範圍式 for 也可以，這裡用索引以便取 (i+1)%n
                                               // could use range-for, but we need the index for (i+1)%n
            int next = (i + 1) % n;            // 環狀的下一個索引 / circular next index
            if (nums[i] > nums[next]) {        // 若當前比下一個還大 → 下降 / current > next → drop
                if (++drops > 1) return false; // 先自增再比較；> 1 立刻回傳 false / pre-increment then test
            }
        }
        return true;                           // 0 或 1 個下降皆合法 / 0 or 1 drop is valid
    }
};
