# 2187. Minimum Time to Complete Trips ###### tags: `Leetcode` `Medium` `Binary Search` Link: https://leetcode.com/problems/minimum-time-to-complete-trips/description/ ## 思路 Binary Search by Value 注意在```canComplete```里面 遍历```time```的回圈里面要判断```currTrip```是否大于等于```totalTrips``` 如果是的话 就break 否则```currTrip```可能会超过int范围 导致答案错误 ## Code ```java= class Solution { public long minimumTime(int[] time, int totalTrips) { Arrays.sort(time); long start = 1, end = (long)totalTrips*time[0]; while(start<end){ long mid = start+(end-start)/2; if(!canComplete(mid, time, totalTrips)){ start = mid+1; } else end = mid; } return start; } public boolean canComplete(long totalTime, int[] time, int totalTrips){ int currTrip = 0; for(int i=0; i<time.length; i++){ currTrip += (int)(totalTime/time[i]); if(currTrip>=totalTrips) break; } return currTrip>=totalTrips; } } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up