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 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| class Solution { public: int getNum(int h,int m){ return h*100+m; } int getLen(int n){ int res=0; while(n){ res++; n/=10; } return res; } int calNum(int startAt,int moveCost,int pushCost,int t,int l){ int res=0; int curNum=startAt; int i=1; while(l>=i){ int curN=t/int(pow(10,l-i))%10; if(curN!=curNum){ res+=moveCost; curNum=curN; } res+=pushCost; i++; } return res; } int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) { int h=targetSeconds/60; int m=targetSeconds%60; if(h>99){ m+=60*(h-99); h=99; } int t=getNum(h,m); int l=getLen(t); int res=calNum(startAt,moveCost,pushCost,t,l); if(m+60<100&&h>0){ h--,m+=60; t=getNum(h,m); l=getLen(t); res=min(res,calNum(startAt,moveCost,pushCost,t,l)); } return res; } };
|