Posted on

Description

Submission

class Solution {
    long func(long a, long b, int option) {
        switch(option) {
            case 0: return a * b;
            case 1: return a / b;
            case 2: return a + b;
            case 3: return a - b;
        }
        return 0;
    }
public:
    int clumsy(int N) {
        if(N == 1) return 1;
        vector<long> operators;
        vector<int> operands;

        for(int i = N, turn = 0; i > 0; --i, turn = (turn+1)%4) {
            if(turn <= 1) {
                if(operands.empty()) operands.push_back(i--);
                operands.back() = func(operands.back(), i, turn);
            } else {
                operands.push_back(i);
                operators.push_back(turn);
            }
            
        }
        long ret = operands[0];
        for(int i = 0; i < operators.size(); ++i) {
            ret = func(ret, operands[i+1], operators[i]);
        }

        return ret;
    }
};

Leave a Reply

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