# 2162. Minimum Cost to Set Cooking Time ###### tags: `Leetcode` `Medium` Link: https://leetcode.com/problems/minimum-cost-to-set-cooking-time/ ## 思路 对于每个time来说只有两种选择 1. Punch minutes and seconds as is, 2. or punch minutes - 1 and seconds + 60. 只要比较他们两个的cost就可以了 ## Code ```java= class Solution { public int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) { int m = targetSeconds/60, n = targetSeconds%60; return Math.min(cost(startAt, moveCost, pushCost, m, n), cost(startAt, moveCost, pushCost, m-1, n+60)); } private int cost(int startAt, int moveCost, int pushCost, int m, int n){ if(Math.max(m, n)>99) return Integer.MAX_VALUE; String str = String.valueOf(m*100+n); int prev = startAt, cost = 0; for(int i=0; i<str.length(); i++){ cost += (str.charAt(i)-'0'==prev)?0:moveCost; cost += pushCost; prev = str.charAt(i)-'0'; } return cost; } } ```