Advertisement
Guest User

Untitled

a guest
Feb 29th, 2020
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. class Solution
  2. {
  3. public:
  4. int minCost(std::vector<std::vector<int>>& costs)
  5. {
  6. if (costs.empty())
  7. return 0;
  8.  
  9. int minCostRed = costs[0][0];
  10. int minCostBlue = costs[0][1];
  11. int minCostGreen = costs[0][2];
  12. for (size_t i = 1; i < costs.size(); ++i)
  13. {
  14. int minCostRedNew1 = minCostBlue + costs[i][1];
  15. int minCostRedNew2 = minCostBlue + costs[i][2];
  16. int minCostRedNew = minCostRedNew1 < minCostRedNew2 ? minCostRedNew1 : minCostRedNew2;
  17.  
  18. int minCostBlueNew1 = minCostBlue + costs[i][0];
  19. int minCostBlueNew2 = minCostBlue + costs[i][2];
  20. int minCostBlueNew = minCostBlueNew1 < minCostBlueNew2 ? minCostBlueNew1 : minCostBlueNew2;
  21.  
  22. int minCostGreenNew1 = minCostBlue + costs[i][1];
  23. int minCostGreenNew2 = minCostBlue + costs[i][2];
  24. int minCostGreenNew = minCostGreenNew1 < minCostGreenNew2 ? minCostGreenNew1 : minCostGreenNew2;
  25.  
  26. minCostRed = minCostRedNew;
  27. minCostBlue = minCostBlueNew;
  28. minCostGreen = minCostGreenNew;
  29. }
  30.  
  31. int minCost = std::min(minCostRed, minCostBlue);
  32. minCost = std::min(minCost, minCostGreen);
  33.  
  34. return minCost;
  35. }
  36. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement