Paint House II Posted on 2018-10-29 Descriptionhttps://leetcode.com/problems/paint-house-ii/ Solution1234567891011121314151617181920212223242526272829303132333435363738394041class Solution {public: int minCostII(vector<vector<int>>& costs) { int size = costs.size(); if (size == 0) return 0; int colorNumber = costs[0].size(); vector<vector<int>> dp(size + 1, vector<int>(colorNumber, 0)); pair<int, int> minCost; minCost.second = 0; minCost.first = -1; pair<int, int> secondMinCost; secondMinCost.second = 0; secondMinCost.first = -1; for (int i = 1; i <= size; ++i) { int targetIndex = -1; int targetCost = INT_MAX; int secondIndex = -1; int secondCost = INT_MAX; for (int j = 0; j < colorNumber; ++j) { dp[i][j] = costs[i - 1][j] + (minCost.first == j ? secondMinCost.second : minCost.second); if (dp[i][j] < targetCost) { secondIndex = targetIndex; secondCost = targetCost; targetCost = dp[i][j]; targetIndex = j; }else if (dp[i][j] < secondCost) { secondIndex = j; secondCost = dp[i][j]; } } minCost.first = targetIndex; minCost.second = targetCost; secondMinCost.first = secondIndex; secondMinCost.second = secondCost; } return minCost.second; }};