Skip to content

Commit 83df283

Browse files
authored
Create minimum-cost-to-set-cooking-time.cpp
1 parent 2eacb4c commit 83df283

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Time: O(1)
2+
// Space: O(1)
3+
4+
// simulation
5+
class Solution {
6+
public:
7+
int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {
8+
const auto& cost = [=](int m, int s) {
9+
if (!(0 <= m && m <= 99 && s <= 99)) {
10+
return numeric_limits<int>::max();
11+
}
12+
int result = 0, curr = startAt;
13+
for (const auto& x: to_string(m * 100 + s)) {
14+
result += (((x - '0') != curr) ? moveCost : 0) + pushCost;
15+
curr = x - '0';
16+
}
17+
return result;
18+
};
19+
int m = targetSeconds / 60, s =targetSeconds % 60;
20+
return min(cost(m, s), cost(m - 1, s + 60));
21+
}
22+
};

0 commit comments

Comments
 (0)