8ms C++ plus O(N)T O(1)S


  • 0
    R
         class Solution {
         public:
         	int minCost(vector<vector<int>>& costs) {
             	if (costs.empty()) return 0;
    	         int mcehec[3] = { 0 }; //the min cost of each houses in each colour, [0]-red, [1]-blue,
    	                                //[2]-green
             	int n = costs.size();  //the number of houses
             	for (int i = 0; i <= n - 1; ++i){
             		int red = mcehec[0], blue = mcehec[1], green = mcehec[2];
             		mcehec[0] = min(blue, green) + costs[i][0];
    	         	mcehec[1] = min(red, green) + costs[i][1];
             		mcehec[2] = min(red, blue) + costs[i][2];
             	}
    	         return min(min(mcehec[0], mcehec[1]), mcehec[2]);
             }
         };

Log in to reply
 

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