C++ easy O(n) greedy solution

  • 0
        vector<int> constructArray(int n, int k) {
            vector<int> res(1, 1);
            unordered_set<int> used={1};
            for(int i=0;i<k-1;i++) {
                if(i%2==0) res.push_back(res[i]+k-i);
                else res.push_back(res[i]-k+i);
            for(int i=1;i<=n;i++) {
                if(used.find(i)==used.end()) res.push_back(i);
            return res;

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.