<style> html, body, .ui-content { background: #222222; color: #00BFFF; } ::-webkit-scrollbar { width: 10px; } ::-webkit-scrollbar-track { background: transparent; } ::-webkit-scrollbar-thumb { background: linear-gradient(180deg, #2BE8CF60 0%, #2B83E860 100%); border-radius: 3px; } ::-webkit-scrollbar-thumb:hover { background: linear-gradient(180deg, #2BE8CF95 0%, #2B83E895 100%); } /* 設定 code 模板 */ .markdown-body code, .markdown-body tt { background-color: #ffffff36; } .markdown-body .highlight pre, .markdown-body pre { color: #ddd; background-color: #00000036; } .hljs-tag { color: #ddd; } .token.operator { background-color: transparent; } </style> ###### tags: `Leetcode` # 1870. Minimum Speed to Arrive on Time ## 題目 ###### Link : https://leetcode.com/problems/minimum-speed-to-arrive-on-time/ ## 程式碼 ```cpp= class Solution { public: bool check(vector<int> &dist, double &hour, int speed){ int n = dist.size(); double cnt = 0; for(int i = 0;i < n - 1;++i) cnt += ceil((double)dist[i] / speed); cnt += (double)dist[n - 1] / speed; if(cnt > hour) return false; return true; } int minSpeedOnTime(vector<int>& dist, double hour) { int _max = 100 * *max_element(dist.begin(), dist.end()); int left = 1, right = _max; while(left <= right){ int mid = (left + right) / 2; if(check(dist, hour, mid)) right = mid - 1; else left = mid + 1; } if(left > _max) return -1; return left; } }; ```