• + 0 comments

    There must be a better, prettier way to do this out there, but this worked, so good enough:

    vector<int> stones(int n, int a, int b) {
        if(a==b) return {a*n -b};
        if(a==0) return {b*n};
        if(b==0) return {a*n};
        
        if(b<a){
            int c = a;
            a = b;
            b = c;
        }
        
        vector<int> last_stones(n);
        vector<int> aVec(n-1, 1);
        vector<int> bVec(n-1, 0);
        int ab_pos = 0;
        
        while (!(ab_pos>aVec.size())) {
            int last = 0;
            for(int i = 0; i<aVec.size(); i++){
                last += aVec[i]*a + bVec[i]*b;           
            }
            
            last_stones[ab_pos] = last;
            
            if(ab_pos<aVec.size()){    
                aVec[ab_pos] = 0;
                bVec[ab_pos] = 1;
            }
            ab_pos++;
        }
        
        return last_stones;
    }