Posted on

Description

Submission

class Solution {
public:
    int reinitializePermutation(int n) {
        vector<int> perm(n);
        for(int i = 0; i < n; ++i) perm[i] = i;
        int ret = 0;
        vector<int> arr(n);
        while(true) {
            for(int i = 0; i < n; ++i) {
                if(i%2==0) arr[i] = perm[i/2];
                else arr[i] = perm[n/2 + (i-1)/2];
            }
            ret++;
            int i = 0;
            for(; i < n; ++i) {
                if(arr[i] != i) break;
            }
            if(i == n) return ret;
            perm = arr;
        }
        return 0;
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *