• + 0 comments

    knapsack, ez

    O(G * N)

    string indianJob(int G, const vector<int>& A) {
        int M = 0, sum = 0;
        for (int x : A) sum = sum + x;
        vector<vector<bool>> cache(G+1, vector<bool>(A.size()+1));
        for (int j=0; j <= A.size(); j++) cache[0][j] = true;
        for (int i=1; i <= G; i++) {
            for (int j=1; j <= A.size(); j++) {
                (i >= A[j-1]) ? cache[i][j] = cache[i][j-1] or cache[i-A[j-1]][j-1] : cache[i][j] = cache[i][j-1];
                if (cache[i][j]) M = max(M, i);
            }
        }
        return (sum - M <= G) ? "YES" : "NO";
    }