Short C++ Simple Solution


  • 0
    G
    class Solution {
    public:
        int minCostII(vector<vector<int>>& costs) {
    		if (costs.empty() || costs[0].empty()) return 0;
    		int n = costs.size(), k = costs[0].size(), res = INT_MAX;
            vector<vector<int>> dp = costs;
    		for (int i = 1; i < n; ++i) {
    			for (int j = 0; j < k; ++j) {
    				int mn = INT_MAX;
    				for (int d = 1; d < k; ++d) {
    					mn = min(mn, dp[i - 1][(j + d) % k]);
    				}
    				dp[i][j] += mn;
    				if (i == n - 1) res = min(res, dp[i][j]);
    			}
    		}
    		return res;
        }
    };

Log in to reply
 

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