时间轴
时间轴
2025-11-03
init
贪心
题目:
贪心,如果一组气球相同那么把这组需要最大时间的留下,其他都移除。
12345678910111213141516171819202122232425262728293031323334353637383940414243 | using std::string;using std::vector;class Solution { public: int minCost(string colors, vector<int> &neededTime) { int i = 0, j=1, n = colors.size(); int res = 0; int max_time; int total; while (j < n) { j = i + 1; total = neededTime[i]; max_time = neededTime[i]; while (colors[i] == colors[j]) { max_time = std::max(neededTime[j], max_time); total += neededTime[j]; j++; } if (total == neededTime[i]) { i++; } else { res += total - max_time; i = j; } } return res; }};int main(){ Solution s; string colors = "abaac"; vector<int> neededTime = { 1,2,3,4,5 }; s.minCost(colors, neededTime);} |
