1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| class Solution { public:
int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) { int mins = targetSeconds / 60, secs = targetSeconds % 60; int ans1 = calTime(startAt, moveCost, pushCost, mins, secs); int ans2 = calTime(startAt, moveCost, pushCost, mins - 1, secs + 60); return min(ans1, ans2); }
int calTime(int startAt, int moveCost, int pushCost, int mins, int secs) { if (mins < 0 || mins > 99 || secs > 99) { return INT_MAX; } string s = to_string(mins * 100 + secs); int ans = 0; for (int i = 0; i < s.size(); i ++) { if (s[i] - '0' == startAt) { ans += pushCost; } else { ans += pushCost + moveCost; } startAt = s[i] - '0'; } return ans; } };
|