【題解】LeetCode 1686. Stone Game VI

【題目敘述】https://leetcode.com/problems/stone-game-vi/

class Solution {
public:
    int stoneGameVI(vector<int>& va, vector<int>& vb) {
        int n = va.size(), a = 0, b = 0;
        vector <int> v;
        for (int i = 0; i < n; i++){
            v.push_back(i);
        }
        sort(v.begin(), v.end(), [&](int x, int y){return va[x]+vb[x] > va[y]+vb[y];});
        for (int i = 0; i < n; i++){
            if (i%2 == 0){
                a += va[v[i]];
            }
            else{
                b += vb[v[i]];
            }
        }
        if (a > b) return 1;
        else if (a == b) return 0;
        else return -1;
    }
};
分享本文 Share with friends